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 org.mockito.Matchers.any; 20 import static org.mockito.Matchers.anyInt; 21 import static org.mockito.Matchers.isA; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.verify; 24 import static org.robolectric.Shadows.shadowOf; 25 26 import android.app.Application; 27 import android.app.Fragment; 28 import android.app.FragmentManager; 29 import android.app.FragmentTransaction; 30 import android.content.ComponentName; 31 import android.content.Intent; 32 import android.content.pm.PackageManager; 33 import android.os.UserHandle; 34 35 import com.android.settings.testutils.SettingsRobolectricTestRunner; 36 import com.android.settings.TestConfig; 37 import com.android.settings.search.SearchIndexableRaw; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.Robolectric; 46 import org.robolectric.RuntimeEnvironment; 47 import org.robolectric.annotation.Config; 48 import org.robolectric.annotation.Implementation; 49 import org.robolectric.annotation.Implements; 50 import org.robolectric.annotation.Resetter; 51 import org.robolectric.res.builder.RobolectricPackageManager; 52 import org.robolectric.util.ActivityController; 53 54 import static com.google.common.truth.Truth.assertThat; 55 56 import java.util.List; 57 58 59 @RunWith(SettingsRobolectricTestRunner.class) 60 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, 61 shadows = {BackupSettingsActivityTest.ShadowBackupSettingsHelper.class, 62 BackupSettingsActivityTest.ShadowUserHandle.class}) 63 public class BackupSettingsActivityTest { 64 private ActivityController<BackupSettingsActivity> mActivityController; 65 private BackupSettingsActivity mActivity; 66 private Application mApplication; 67 private RobolectricPackageManager mPackageManager; 68 private static boolean mIsBackupProvidedByOEM; 69 70 @Mock 71 private FragmentManager mFragmentManager; 72 73 @Mock 74 private FragmentTransaction mFragmentTransaction; 75 @Mock 76 private static Intent mIntent; 77 @Mock 78 private ComponentName mComponent; 79 80 @Before 81 public void setUp() { 82 MockitoAnnotations.initMocks(this); 83 84 mApplication = RuntimeEnvironment.application; 85 mActivityController = Robolectric.buildActivity(BackupSettingsActivity.class); 86 mActivity = mActivityController.get(); 87 mPackageManager = (RobolectricPackageManager) mApplication.getPackageManager(); 88 doReturn(mComponent).when(mIntent).getComponent(); 89 } 90 91 @Test 92 public void onCreate_launchActivity() { 93 mIsBackupProvidedByOEM = false; 94 95 // Testing the scenario when the activity is disabled 96 mPackageManager.setComponentEnabledSetting(mComponent, 97 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0); 98 99 mActivityController.create(); 100 101 // Verify that the component to launch was enabled. 102 assertThat(mPackageManager.getComponentState(mComponent).newState) 103 .isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED); 104 105 // Verify that the intent returned by BackupSettingsHelper.getIntentForBackupSettings() 106 // was launched. 107 assertThat(shadowOf(mApplication).getNextStartedActivity()).isEqualTo(mIntent); 108 } 109 110 @Test 111 public void onCreate_hasManufacturerIntent() { 112 mIsBackupProvidedByOEM = true; 113 114 // Fragments are tested separately, so mock out the manager. 115 mActivity.setFragmentManager(mFragmentManager); 116 doReturn(mFragmentTransaction).when(mFragmentTransaction).replace(anyInt(), 117 any(Fragment.class)); 118 doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction(); 119 120 mActivityController.create(); 121 122 assertThat(shadowOf(mApplication).getNextStartedActivity()).isNull(); 123 verify(mFragmentTransaction).replace(anyInt(), isA(BackupSettingsFragment.class)); 124 125 } 126 127 @Test 128 public void getNonIndexableKeys_SystemUser() { 129 final List<SearchIndexableRaw> indexableRaws = 130 BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex( 131 mApplication.getApplicationContext(), true); 132 final List<String> nonIndexableKeys = 133 BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 134 mApplication.getApplicationContext()); 135 136 assertThat(indexableRaws).isNotNull(); 137 assertThat(indexableRaws).isNotEmpty(); 138 assertThat(nonIndexableKeys).isEmpty(); 139 } 140 141 @Test 142 public void getNonIndexableKeys_NonSystemUser() { 143 ShadowUserHandle.setUid(1); // Non-SYSTEM user. 144 145 final List<SearchIndexableRaw> indexableRaws = 146 BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex( 147 mApplication.getApplicationContext(), true); 148 final List<String> nonIndexableKeys = 149 BackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 150 mApplication.getApplicationContext()); 151 152 assertThat(indexableRaws).isNotNull(); 153 assertThat(indexableRaws).isNotEmpty(); 154 assertThat(nonIndexableKeys).isNotEmpty(); 155 } 156 157 @After 158 public void resetShadows() { 159 ShadowUserHandle.reset(); 160 } 161 162 @Implements(BackupSettingsHelper.class) 163 public static class ShadowBackupSettingsHelper { 164 @Implementation 165 public Intent getIntentForBackupSettings() { 166 return mIntent; 167 } 168 169 @Implementation 170 public boolean isBackupProvidedByManufacturer() { 171 return mIsBackupProvidedByOEM; 172 } 173 } 174 175 @Implements(UserHandle.class) 176 public static class ShadowUserHandle { 177 private static int sUid = 0; // SYSTEM by default 178 179 public static void setUid(int uid) { 180 sUid = uid; 181 } 182 183 @Implementation 184 public static int myUserId() { 185 return sUid; 186 } 187 188 @Resetter 189 public static void reset() { 190 sUid = 0; 191 } 192 } 193 } 194