Previously, 1) we showed backup settings for main users (including secondary non-profile users) only when backup was activated 2) for profile users, we always showed backup settings. However, this results in a nullpointer when opening Settings when backup is deactivated for both main and profile user. This CL fixes that nullpointer(and also changes existing functionality since keeping existing functionality and fixing the nullpointer would be too large a code change) For more details, see https://b.corp.google.com/issues/129843872#comment9 and https://b.corp.google.com/issues/129843872#comment12 Bug: 129843872 Test: 1. atest -v UserBackupSettingsActivityTest 2. atest -v BackupInactivePreferenceControllerTest 3a) backup not active for main and profile user. shows "isn't active" for both 3b) backup active for main only. shows backup settings for main user and "isn't active" for profile. 3c) backup active for both. shows backup settings for both. 3d) backup active for profile only is not possible (as profile backup is only active when main user backup is active). if we try to force set it, we get "isn't active" for both 3e) backup not active for secondary user. shows "isn't active" 3f) backup active for secondary user. shows backup settings. Change-Id: Icb87a047068d29eda560c45dfa4ae02bc991b1af
170 lines
5.9 KiB
Java
170 lines
5.9 KiB
Java
/*
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License
|
|
*/
|
|
|
|
package com.android.settings.backup;
|
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.anyInt;
|
|
import static org.mockito.ArgumentMatchers.isA;
|
|
import static org.mockito.Mockito.doReturn;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import android.app.Application;
|
|
import android.content.ComponentName;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
import androidx.fragment.app.FragmentManager;
|
|
import androidx.fragment.app.FragmentTransaction;
|
|
|
|
import org.junit.After;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.mockito.Mock;
|
|
import org.mockito.MockitoAnnotations;
|
|
import org.robolectric.Robolectric;
|
|
import org.robolectric.RobolectricTestRunner;
|
|
import org.robolectric.RuntimeEnvironment;
|
|
import org.robolectric.Shadows;
|
|
import org.robolectric.android.controller.ActivityController;
|
|
import org.robolectric.annotation.Config;
|
|
import org.robolectric.annotation.Implementation;
|
|
import org.robolectric.annotation.Implements;
|
|
import org.robolectric.annotation.Resetter;
|
|
import org.robolectric.shadows.ShadowPackageManager;
|
|
|
|
@RunWith(RobolectricTestRunner.class)
|
|
@Config(shadows = {UserBackupSettingsActivityTest.ShadowBackupSettingsHelper.class})
|
|
public class UserBackupSettingsActivityTest {
|
|
private ActivityController<UserBackupSettingsActivity> mActivityController;
|
|
private UserBackupSettingsActivity mActivity;
|
|
private Application mApplication;
|
|
private ShadowPackageManager mPackageManager;
|
|
private static boolean mIsBackupProvidedByOEM;
|
|
|
|
@Mock
|
|
private FragmentManager mFragmentManager;
|
|
|
|
@Mock
|
|
private FragmentTransaction mFragmentTransaction;
|
|
@Mock
|
|
private static Intent mIntent;
|
|
@Mock
|
|
private ComponentName mComponent;
|
|
|
|
@Before
|
|
public void setUp() {
|
|
MockitoAnnotations.initMocks(this);
|
|
|
|
mApplication = RuntimeEnvironment.application;
|
|
mActivityController = Robolectric.buildActivity(UserBackupSettingsActivity.class);
|
|
mActivity = mActivityController.get();
|
|
mPackageManager = Shadows.shadowOf(mApplication.getPackageManager());
|
|
when(mIntent.getComponent()).thenReturn(mComponent);
|
|
}
|
|
|
|
@After
|
|
public void resetShadows() {
|
|
ShadowBackupSettingsHelper.reset();
|
|
}
|
|
|
|
@Test
|
|
public void onCreate_launchActivity() {
|
|
mIsBackupProvidedByOEM = false;
|
|
|
|
// Testing the scenario when the activity is disabled
|
|
mApplication.getPackageManager().setComponentEnabledSetting(mComponent,
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
|
|
|
|
mActivityController.create();
|
|
|
|
// Verify that the component to launch was enabled.
|
|
final int flags = mPackageManager.getComponentEnabledSettingFlags(mComponent);
|
|
assertThat(flags & PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
|
|
.isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
|
|
|
|
// Verify that the intent returned by BackupSettingsHelper.getIntentForBackupSettings()
|
|
// was launched.
|
|
assertThat(Shadows.shadowOf(mApplication).getNextStartedActivity()).isEqualTo(mIntent);
|
|
}
|
|
|
|
@Test
|
|
public void onCreate_hasManufacturerIntent() {
|
|
mIsBackupProvidedByOEM = true;
|
|
|
|
// Fragments are tested separately, so mock out the manager.
|
|
mActivity.setFragmentManager(mFragmentManager);
|
|
doReturn(mFragmentTransaction).when(mFragmentTransaction).replace(anyInt(),
|
|
any(Fragment.class));
|
|
doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction();
|
|
|
|
mActivityController.create();
|
|
|
|
assertThat(Shadows.shadowOf(mApplication).getNextStartedActivity()).isNull();
|
|
verify(mFragmentTransaction).replace(anyInt(), isA(BackupSettingsFragment.class));
|
|
}
|
|
|
|
@Test
|
|
public void getNonIndexableKeys_whenBackupServiceActive() {
|
|
ShadowBackupSettingsHelper.isBackupServiceActive = true;
|
|
|
|
assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
|
|
mApplication, true)).isNotEmpty();
|
|
assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
|
|
mApplication)).isEmpty();
|
|
}
|
|
|
|
@Test
|
|
public void getNonIndexableKeys_whenBackupServiceNotActive() {
|
|
ShadowBackupSettingsHelper.isBackupServiceActive = false;
|
|
|
|
assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
|
|
mApplication, true)).isNotEmpty();
|
|
assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
|
|
mApplication)).contains("Backup");
|
|
}
|
|
|
|
@Implements(BackupSettingsHelper.class)
|
|
public static class ShadowBackupSettingsHelper {
|
|
static boolean isBackupServiceActive = true;
|
|
|
|
@Implementation
|
|
public boolean isBackupServiceActive() {
|
|
return isBackupServiceActive;
|
|
}
|
|
|
|
@Implementation
|
|
protected Intent getIntentForBackupSettings() {
|
|
return mIntent;
|
|
}
|
|
|
|
@Implementation
|
|
protected boolean isBackupProvidedByManufacturer() {
|
|
return mIsBackupProvidedByOEM;
|
|
}
|
|
|
|
@Resetter
|
|
public static void reset() {
|
|
isBackupServiceActive = true;
|
|
}
|
|
}
|
|
}
|