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 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.ArgumentMatchers.isA;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.Application;
29 import android.content.ComponentName;
30 import android.content.Intent;
31 import android.content.pm.PackageManager;
32 
33 import androidx.fragment.app.Fragment;
34 import androidx.fragment.app.FragmentManager;
35 import androidx.fragment.app.FragmentTransaction;
36 
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.Robolectric;
44 import org.robolectric.RobolectricTestRunner;
45 import org.robolectric.RuntimeEnvironment;
46 import org.robolectric.Shadows;
47 import org.robolectric.android.controller.ActivityController;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.annotation.Implementation;
50 import org.robolectric.annotation.Implements;
51 import org.robolectric.annotation.Resetter;
52 import org.robolectric.shadows.ShadowPackageManager;
53 
54 @RunWith(RobolectricTestRunner.class)
55 @Config(shadows = {UserBackupSettingsActivityTest.ShadowBackupSettingsHelper.class})
56 public class UserBackupSettingsActivityTest {
57     private ActivityController<UserBackupSettingsActivity> mActivityController;
58     private UserBackupSettingsActivity mActivity;
59     private Application mApplication;
60     private ShadowPackageManager mPackageManager;
61     private static boolean mIsBackupProvidedByOEM;
62 
63     @Mock
64     private FragmentManager mFragmentManager;
65 
66     @Mock
67     private FragmentTransaction mFragmentTransaction;
68     @Mock
69     private static Intent mIntent;
70     @Mock
71     private ComponentName mComponent;
72 
73     @Before
setUp()74     public void setUp() {
75         MockitoAnnotations.initMocks(this);
76 
77         mApplication = RuntimeEnvironment.application;
78         mActivityController = Robolectric.buildActivity(UserBackupSettingsActivity.class);
79         mActivity = mActivityController.get();
80         mPackageManager = Shadows.shadowOf(mApplication.getPackageManager());
81         when(mIntent.getComponent()).thenReturn(mComponent);
82     }
83 
84     @After
resetShadows()85     public void resetShadows() {
86         ShadowBackupSettingsHelper.reset();
87     }
88 
89     @Test
onCreate_launchActivity()90     public void onCreate_launchActivity() {
91         mIsBackupProvidedByOEM = false;
92 
93         // Testing the scenario when the activity is disabled
94         mApplication.getPackageManager().setComponentEnabledSetting(mComponent,
95                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
96 
97         mActivityController.create();
98 
99         // Verify that the component to launch was enabled.
100         final int flags = mPackageManager.getComponentEnabledSettingFlags(mComponent);
101         assertThat(flags & PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
102             .isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
103 
104         // Verify that the intent returned by BackupSettingsHelper.getIntentForBackupSettings()
105         // was launched.
106         assertThat(Shadows.shadowOf(mApplication).getNextStartedActivity()).isEqualTo(mIntent);
107     }
108 
109     @Test
onCreate_hasManufacturerIntent()110     public void onCreate_hasManufacturerIntent() {
111         mIsBackupProvidedByOEM = true;
112 
113         // Fragments are tested separately, so mock out the manager.
114         mActivity.setFragmentManager(mFragmentManager);
115         doReturn(mFragmentTransaction).when(mFragmentTransaction).replace(anyInt(),
116                 any(Fragment.class));
117         doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction();
118 
119         mActivityController.create();
120 
121         assertThat(Shadows.shadowOf(mApplication).getNextStartedActivity()).isNull();
122         verify(mFragmentTransaction).replace(anyInt(), isA(BackupSettingsFragment.class));
123     }
124 
125     @Test
getNonIndexableKeys_whenBackupServiceActive()126     public void getNonIndexableKeys_whenBackupServiceActive() {
127         ShadowBackupSettingsHelper.isBackupServiceActive = true;
128 
129         assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
130                 mApplication, true)).isNotEmpty();
131         assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
132                 mApplication)).isEmpty();
133     }
134 
135     @Test
getNonIndexableKeys_whenBackupServiceNotActive()136     public void getNonIndexableKeys_whenBackupServiceNotActive() {
137         ShadowBackupSettingsHelper.isBackupServiceActive = false;
138 
139         assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(
140             mApplication, true)).isNotEmpty();
141         assertThat(UserBackupSettingsActivity.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
142             mApplication)).contains("Backup");
143     }
144 
145     @Implements(BackupSettingsHelper.class)
146     public static class ShadowBackupSettingsHelper {
147         static boolean isBackupServiceActive = true;
148 
149         @Implementation
isBackupServiceActive()150         public boolean isBackupServiceActive() {
151             return isBackupServiceActive;
152         }
153 
154         @Implementation
getIntentForBackupSettings()155         protected Intent getIntentForBackupSettings() {
156             return mIntent;
157         }
158 
159         @Implementation
isBackupProvidedByManufacturer()160         protected boolean isBackupProvidedByManufacturer() {
161             return mIsBackupProvidedByOEM;
162         }
163 
164         @Resetter
reset()165         public static void reset() {
166             isBackupServiceActive = true;
167         }
168     }
169 }
170