• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.settings.backup;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Matchers.anyInt;
22 import static org.mockito.Matchers.isA;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 import static org.robolectric.Shadows.shadowOf;
27 
28 import android.app.Application;
29 import android.app.Fragment;
30 import android.app.FragmentManager;
31 import android.app.FragmentTransaction;
32 import android.content.ComponentName;
33 import android.content.Intent;
34 import android.content.pm.PackageManager;
35 import android.os.UserHandle;
36 
37 import com.android.settings.search.SearchIndexableRaw;
38 import com.android.settings.testutils.SettingsRobolectricTestRunner;
39 
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.Robolectric;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.Shadows;
49 import org.robolectric.android.controller.ActivityController;
50 import org.robolectric.annotation.Config;
51 import org.robolectric.annotation.Implementation;
52 import org.robolectric.annotation.Implements;
53 import org.robolectric.annotation.Resetter;
54 import org.robolectric.shadows.ShadowPackageManager;
55 
56 import java.util.List;
57 
58 @RunWith(SettingsRobolectricTestRunner.class)
59 @Config(shadows = {BackupSettingsActivityTest.ShadowBackupSettingsHelper.class,
60                 BackupSettingsActivityTest.ShadowUserHandle.class})
61 public class BackupSettingsActivityTest {
62     private ActivityController<BackupSettingsActivity> mActivityController;
63     private BackupSettingsActivity mActivity;
64     private Application mApplication;
65     private ShadowPackageManager mPackageManager;
66     private static boolean mIsBackupProvidedByOEM;
67 
68     @Mock
69     private FragmentManager mFragmentManager;
70 
71     @Mock
72     private FragmentTransaction mFragmentTransaction;
73     @Mock
74     private static Intent mIntent;
75     @Mock
76     private ComponentName mComponent;
77 
78     @Before
setUp()79     public void setUp() {
80         MockitoAnnotations.initMocks(this);
81 
82         mApplication = RuntimeEnvironment.application;
83         mActivityController = Robolectric.buildActivity(BackupSettingsActivity.class);
84         mActivity = mActivityController.get();
85         mPackageManager = Shadows.shadowOf(mApplication.getPackageManager());
86         when(mIntent.getComponent()).thenReturn(mComponent);
87     }
88 
89     @After
resetShadows()90     public void resetShadows() {
91         ShadowUserHandle.reset();
92     }
93 
94     @Test
onCreate_launchActivity()95     public void onCreate_launchActivity() {
96         mIsBackupProvidedByOEM = false;
97 
98         // Testing the scenario when the activity is disabled
99         mApplication.getPackageManager().setComponentEnabledSetting(mComponent,
100                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
101 
102         mActivityController.create();
103 
104         // Verify that the component to launch was enabled.
105         final int flags = mPackageManager.getComponentEnabledSettingFlags(mComponent);
106         assertThat(flags & PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
107             .isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
108 
109         // Verify that the intent returned by BackupSettingsHelper.getIntentForBackupSettings()
110         // was launched.
111         assertThat(shadowOf(mApplication).getNextStartedActivity()).isEqualTo(mIntent);
112     }
113 
114     @Test
onCreate_hasManufacturerIntent()115     public void onCreate_hasManufacturerIntent() {
116         mIsBackupProvidedByOEM = true;
117 
118         // Fragments are tested separately, so mock out the manager.
119         mActivity.setFragmentManager(mFragmentManager);
120         doReturn(mFragmentTransaction).when(mFragmentTransaction).replace(anyInt(),
121                 any(Fragment.class));
122         doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction();
123 
124         mActivityController.create();
125 
126         assertThat(shadowOf(mApplication).getNextStartedActivity()).isNull();
127         verify(mFragmentTransaction).replace(anyInt(), isA(BackupSettingsFragment.class));
128     }
129 
130     @Test
getNonIndexableKeys_SystemUser()131     public void getNonIndexableKeys_SystemUser() {
132         assertThat(BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
133                 mApplication, true)).isNotEmpty();
134         assertThat(BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
135                 mApplication)).isEmpty();
136     }
137 
138     @Test
getNonIndexableKeys_NonSystemUser()139     public void getNonIndexableKeys_NonSystemUser() {
140         ShadowUserHandle.setUid(1); // Non-SYSTEM user.
141 
142         final List<SearchIndexableRaw> indexableRaws =
143                 BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
144                         mApplication, true);
145         final List<String> nonIndexableKeys =
146                 BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
147                         mApplication);
148 
149         assertThat(indexableRaws).isNotNull();
150         assertThat(indexableRaws).isNotEmpty();
151         assertThat(nonIndexableKeys).isNotEmpty();
152     }
153 
154     @Implements(BackupSettingsHelper.class)
155     public static class ShadowBackupSettingsHelper {
156         @Implementation
getIntentForBackupSettings()157         public Intent getIntentForBackupSettings() {
158             return mIntent;
159         }
160 
161         @Implementation
isBackupProvidedByManufacturer()162         public boolean isBackupProvidedByManufacturer() {
163             return mIsBackupProvidedByOEM;
164         }
165     }
166 
167     @Implements(UserHandle.class)
168     public static class ShadowUserHandle {
169         private static int sUid = 0; // SYSTEM by default
170 
setUid(int uid)171         public static void setUid(int uid) {
172             sUid = uid;
173         }
174 
175         @Implementation
myUserId()176         public static int myUserId() {
177             return sUid;
178         }
179 
180         @Resetter
reset()181         public static void reset() {
182             sUid = 0;
183         }
184     }
185 }
186