BackupSettingsActivityTest.java revision 9a5220e7c3c9730b3cb362dab898e5f8ae8d6d5b
1f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov/*
2f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * Copyright (C) 2017 The Android Open Source Project
3f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov *
4f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * Licensed under the Apache License, Version 2.0 (the "License");
5f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * you may not use this file except in compliance with the License.
6f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * You may obtain a copy of the License at
7f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov *
8f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov *      http://www.apache.org/licenses/LICENSE-2.0
9f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov *
10f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * Unless required by applicable law or agreed to in writing, software
11f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * distributed under the License is distributed on an "AS IS" BASIS,
12f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * See the License for the specific language governing permissions and
14f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * limitations under the License
15f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov */
16f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
17f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovpackage com.android.settings.backup;
18f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
19f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Matchers.any;
20f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Matchers.anyInt;
21f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Matchers.isA;
22f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Mockito.doReturn;
23f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Mockito.verify;
24f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.robolectric.Shadows.shadowOf;
25f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
26f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.app.Application;
27f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.app.Fragment;
28f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.app.FragmentManager;
29f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.app.FragmentTransaction;
30f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.content.ComponentName;
31f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.content.Intent;
32f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.content.pm.PackageManager;
339a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippovimport android.os.UserHandle;
34f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
35f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport com.android.settings.SettingsRobolectricTestRunner;
36f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport com.android.settings.TestConfig;
379a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippovimport com.android.settings.search.SearchIndexableRaw;
38f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
399a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippovimport org.junit.After;
40f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.junit.Before;
41f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.junit.Test;
42f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.junit.runner.RunWith;
43f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.mockito.Mock;
44f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.mockito.MockitoAnnotations;
45f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.Robolectric;
46f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.RuntimeEnvironment;
47f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.annotation.Config;
48f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.annotation.Implementation;
49f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.annotation.Implements;
509a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippovimport org.robolectric.annotation.Resetter;
51f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.res.builder.RobolectricPackageManager;
52f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.util.ActivityController;
53f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.shadows.ShadowActivity;
54f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
55f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static com.google.common.truth.Truth.assertThat;
56f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
579a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippovimport java.util.List;
589a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
59f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
60f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov@RunWith(SettingsRobolectricTestRunner.class)
61f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
629a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        shadows = {BackupSettingsActivityTest.ShadowBackupSettingsHelper.class,
639a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov                BackupSettingsActivityTest.ShadowUserHandle.class})
64f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovpublic class BackupSettingsActivityTest {
65f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private ActivityController<BackupSettingsActivity> mActivityController;
66f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private BackupSettingsActivity mActivity;
67f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private Application mApplication;
68f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private RobolectricPackageManager mPackageManager;
69f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private static boolean mIsBackupProvidedByOEM;
70f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
71f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Mock
72f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private FragmentManager mFragmentManager;
73f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
74f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Mock
75f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private FragmentTransaction mFragmentTransaction;
76f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Mock
77f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private static Intent mIntent;
78f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Mock
79f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private ComponentName mComponent;
80f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
81f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Before
82f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    public void setUp() {
83f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        MockitoAnnotations.initMocks(this);
84f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
85f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mApplication = RuntimeEnvironment.application;
86f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivityController = Robolectric.buildActivity(BackupSettingsActivity.class);
87f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivity = mActivityController.get();
88f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mPackageManager = (RobolectricPackageManager) mApplication.getPackageManager();
89f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        doReturn(mComponent).when(mIntent).getComponent();
90f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    }
91f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
92f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Test
93f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    public void onCreate_launchActivity() {
94f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mIsBackupProvidedByOEM = false;
95f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
96f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // Testing the scenario when the activity is disabled
97f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mPackageManager.setComponentEnabledSetting(mComponent,
98f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
99f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
100f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivityController.create();
101f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
102f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // Verify that the component to launch was enabled.
103f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        assertThat(mPackageManager.getComponentState(mComponent).newState)
104f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov                .isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
105f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
106f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // Verify that the intent returned by BackupSettingsHelper.getIntentForBackupSettings()
107f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // was launched.
108f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        assertThat(shadowOf(mApplication).getNextStartedActivity()).isEqualTo(mIntent);
109f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    }
110f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
111f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Test
112f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    public void onCreate_hasManufacturerIntent() {
113f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mIsBackupProvidedByOEM = true;
114f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
115f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // Fragments are tested separately, so mock out the manager.
116f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivity.setFragmentManager(mFragmentManager);
117f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        doReturn(mFragmentTransaction).when(mFragmentTransaction).replace(anyInt(),
118f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov                any(Fragment.class));
119f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction();
120f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
121f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivityController.create();
122f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
123f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        assertThat(shadowOf(mApplication).getNextStartedActivity()).isNull();
124f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        verify(mFragmentTransaction).replace(anyInt(), isA(BackupSettingsFragment.class));
125f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
126f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    }
127f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
1289a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    @Test
1299a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    public void getNonIndexableKeys_SystemUser() {
1309a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        final List<SearchIndexableRaw> indexableRaws =
1319a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov                BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
1329a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov                        mApplication.getApplicationContext(), true);
1339a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        final List<String> nonIndexableKeys =
1349a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov                BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
1359a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov                        mApplication.getApplicationContext());
1369a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
1379a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        assertThat(indexableRaws).isNotNull();
1389a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        assertThat(indexableRaws).isNotEmpty();
1399a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        assertThat(nonIndexableKeys).isEmpty();
1409a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    }
1419a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
1429a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    @Test
1439a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    public void getNonIndexableKeys_NonSystemUser() {
1449a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        ShadowUserHandle.setUid(1); // Non-SYSTEM user.
1459a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
1469a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        final List<SearchIndexableRaw> indexableRaws =
1479a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov                BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
1489a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov                        mApplication.getApplicationContext(), true);
1499a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        final List<String> nonIndexableKeys =
1509a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov                BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
1519a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov                        mApplication.getApplicationContext());
1529a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
1539a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        assertThat(indexableRaws).isNotNull();
1549a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        assertThat(indexableRaws).isNotEmpty();
1559a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        assertThat(nonIndexableKeys).isNotEmpty();
1569a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    }
1579a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
1589a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    @After
1599a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    public void resetShadows() {
1609a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        ShadowUserHandle.reset();
1619a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    }
1629a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
163f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Implements(BackupSettingsHelper.class)
164f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    public static class ShadowBackupSettingsHelper {
165f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        @Implementation
166f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        public Intent getIntentForBackupSettings() {
167f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov            return mIntent;
168f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        }
169f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
170f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        @Implementation
171f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        public boolean isBackupProvidedByManufacturer() {
172f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov            return mIsBackupProvidedByOEM;
173f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        }
174f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    }
1759a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
1769a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    @Implements(UserHandle.class)
1779a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    public static class ShadowUserHandle {
1789a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        private static int sUid = 0; // SYSTEM by default
1799a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
1809a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        public static void setUid(int uid) {
1819a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov            sUid = uid;
1829a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        }
1839a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
1849a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        @Implementation
1859a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        public static int myUserId() {
1869a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov            return sUid;
1879a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        }
1889a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov
1899a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        @Resetter
1909a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        public static void reset() {
1919a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov            sUid = 0;
1929a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov        }
1939a5220e7c3c9730b3cb362dab898e5f8ae8d6d5bAnton Philippov    }
194f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov}
195