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