OSDN Git Service

Merge "Fix search indexing for encryption_and_credential page" into oc-dr1-dev
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / deviceinfo / storage / AutomaticStorageManagementSwitchPreferenceControllerTest.java
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.deviceinfo.storage;
18
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Matchers.eq;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import android.app.FragmentManager;
29 import android.app.FragmentTransaction;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.provider.Settings;
33 import android.support.v7.preference.Preference;
34 import android.support.v7.preference.PreferenceScreen;
35 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
36 import com.android.settings.TestConfig;
37 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
38 import com.android.settings.deletionhelper.ActivationWarningFragment;
39 import com.android.settings.overlay.FeatureFactory;
40 import com.android.settings.testutils.FakeFeatureFactory;
41 import com.android.settings.testutils.SettingsRobolectricTestRunner;
42 import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
43 import com.android.settings.widget.MasterSwitchPreference;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Answers;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50 import org.robolectric.RuntimeEnvironment;
51 import org.robolectric.annotation.Config;
52
53
54 @RunWith(SettingsRobolectricTestRunner.class)
55 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
56 public class AutomaticStorageManagementSwitchPreferenceControllerTest {
57
58     @Mock
59     private PreferenceScreen mScreen;
60     @Mock
61     private MasterSwitchPreference mPreference;
62     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
63     private Context mMockContext;
64     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
65     private FragmentManager mFragmentManager;
66
67     private Context mContext;
68     private AutomaticStorageManagementSwitchPreferenceController mController;
69     private MetricsFeatureProvider mMetricsFeature;
70
71     @Before
72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74         mContext = RuntimeEnvironment.application.getApplicationContext();
75         FeatureFactory factory = FeatureFactory.getFactory(mContext);
76         mMetricsFeature = factory.getMetricsFeatureProvider();
77
78         mController = new AutomaticStorageManagementSwitchPreferenceController(
79                 mContext, mMetricsFeature, mFragmentManager);
80         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
81     }
82
83     @Test
84     public void isAvailable_shouldReturnTrue_forHighRamDevice() {
85         assertThat(mController.isAvailable()).isTrue();
86     }
87
88     @Test
89     @Config(shadows = {SettingsShadowSystemProperties.class})
90     public void isAvailable_shouldAlwaysReturnFalse_forLowRamDevice() {
91         SettingsShadowSystemProperties.set("ro.config.low_ram", "true");
92         assertThat(mController.isAvailable()).isFalse();
93         SettingsShadowSystemProperties.clear();
94     }
95
96     @Test
97     public void onResume_shouldReflectEnabledStatus() {
98         mController.displayPreference(mScreen);
99         ContentResolver resolver = mContext.getContentResolver();
100         Settings.Secure.putInt(resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 1);
101
102         mController.onResume();
103
104         verify(mPreference).setChecked(eq(true));
105     }
106
107     @Test
108     public void onResume_shouldRegisterCallback() {
109         mController.displayPreference(mScreen);
110         mController.onResume();
111
112         verify(mPreference).setOnPreferenceChangeListener(
113                 any(Preference.OnPreferenceChangeListener.class));
114     }
115
116     @Test
117     public void togglingShouldCauseMetricsEvent() {
118         // FakeFeatureFactory uses mock contexts, so this test scaffolds itself rather than using
119         // the instance variables.
120         FakeFeatureFactory.setupForTest(mMockContext);
121         FakeFeatureFactory factory =
122                 (FakeFeatureFactory) FakeFeatureFactory.getFactory(mMockContext);
123         AutomaticStorageManagementSwitchPreferenceController controller =
124                 new AutomaticStorageManagementSwitchPreferenceController(
125                         mMockContext, factory.metricsFeatureProvider, mFragmentManager);
126
127         controller.onSwitchToggled(true);
128
129         verify(factory.metricsFeatureProvider, times(1)).action(
130                 any(Context.class), eq(MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER), eq(true));
131     }
132
133     @Test
134     public void togglingShouldUpdateSettingsSecure() {
135         mController.onSwitchToggled(true);
136
137         ContentResolver resolver = mContext.getContentResolver();
138         assertThat(Settings.Secure.getInt(
139                 resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0)).isNotEqualTo(0);
140     }
141
142     @Test
143     public void togglingOnShouldTriggerWarningFragment() {
144         FragmentTransaction transaction = mock(FragmentTransaction.class);
145         when (mFragmentManager.beginTransaction()).thenReturn(transaction);
146
147         mController.onSwitchToggled(true);
148
149         verify(transaction).add(any(), eq(ActivationWarningFragment.TAG));
150     }
151
152     @Test
153     public void togglingOffShouldTriggerWarningFragment() {
154         FragmentTransaction transaction = mock(FragmentTransaction.class);
155         when (mFragmentManager.beginTransaction()).thenReturn(transaction);
156
157         mController.onSwitchToggled(false);
158
159         verify(transaction, never()).add(any(), eq(ActivationWarningFragment.TAG));
160     }
161
162
163     @Config(shadows = {SettingsShadowSystemProperties.class})
164     @Test
165     public void togglingOnShouldNotTriggerWarningFragmentIfEnabledByDefault() {
166         FragmentTransaction transaction = mock(FragmentTransaction.class);
167         when (mFragmentManager.beginTransaction()).thenReturn(transaction);
168         SettingsShadowSystemProperties.set(
169                 AutomaticStorageManagementSwitchPreferenceController
170                         .STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "true");
171
172         mController.onSwitchToggled(true);
173
174         verify(transaction, never()).add(any(), eq(ActivationWarningFragment.TAG));
175     }
176
177     @Config(shadows = {SettingsShadowSystemProperties.class})
178     @Test
179     public void togglingOnShouldTriggerWarningFragmentIfEnabledByDefaultAndDisabledByPolicy() {
180         FragmentTransaction transaction = mock(FragmentTransaction.class);
181         when(mFragmentManager.beginTransaction()).thenReturn(transaction);
182         SettingsShadowSystemProperties.set(
183                 AutomaticStorageManagementSwitchPreferenceController
184                         .STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY,
185                 "true");
186         Settings.Secure.putInt(
187                 mContext.getContentResolver(),
188                 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_TURNED_OFF_BY_POLICY,
189                 1);
190
191         mController.onSwitchToggled(true);
192
193         verify(transaction).add(any(), eq(ActivationWarningFragment.TAG));
194     }
195 }