2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.deviceinfo.storage;
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;
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;
54 @RunWith(SettingsRobolectricTestRunner.class)
55 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
56 public class AutomaticStorageManagementSwitchPreferenceControllerTest {
59 private PreferenceScreen mScreen;
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;
67 private Context mContext;
68 private AutomaticStorageManagementSwitchPreferenceController mController;
69 private MetricsFeatureProvider mMetricsFeature;
73 MockitoAnnotations.initMocks(this);
74 mContext = RuntimeEnvironment.application.getApplicationContext();
75 FeatureFactory factory = FeatureFactory.getFactory(mContext);
76 mMetricsFeature = factory.getMetricsFeatureProvider();
78 mController = new AutomaticStorageManagementSwitchPreferenceController(
79 mContext, mMetricsFeature, mFragmentManager);
80 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
84 public void isAvailable_shouldReturnTrue_forHighRamDevice() {
85 assertThat(mController.isAvailable()).isTrue();
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();
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);
102 mController.onResume();
104 verify(mPreference).setChecked(eq(true));
108 public void onResume_shouldRegisterCallback() {
109 mController.displayPreference(mScreen);
110 mController.onResume();
112 verify(mPreference).setOnPreferenceChangeListener(
113 any(Preference.OnPreferenceChangeListener.class));
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);
127 controller.onSwitchToggled(true);
129 verify(factory.metricsFeatureProvider, times(1)).action(
130 any(Context.class), eq(MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER), eq(true));
134 public void togglingShouldUpdateSettingsSecure() {
135 mController.onSwitchToggled(true);
137 ContentResolver resolver = mContext.getContentResolver();
138 assertThat(Settings.Secure.getInt(
139 resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0)).isNotEqualTo(0);
143 public void togglingOnShouldTriggerWarningFragment() {
144 FragmentTransaction transaction = mock(FragmentTransaction.class);
145 when (mFragmentManager.beginTransaction()).thenReturn(transaction);
147 mController.onSwitchToggled(true);
149 verify(transaction).add(any(), eq(ActivationWarningFragment.TAG));
153 public void togglingOffShouldTriggerWarningFragment() {
154 FragmentTransaction transaction = mock(FragmentTransaction.class);
155 when (mFragmentManager.beginTransaction()).thenReturn(transaction);
157 mController.onSwitchToggled(false);
159 verify(transaction, never()).add(any(), eq(ActivationWarningFragment.TAG));
163 @Config(shadows = {SettingsShadowSystemProperties.class})
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");
172 mController.onSwitchToggled(true);
174 verify(transaction, never()).add(any(), eq(ActivationWarningFragment.TAG));
177 @Config(shadows = {SettingsShadowSystemProperties.class})
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,
186 Settings.Secure.putInt(
187 mContext.getContentResolver(),
188 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_TURNED_OFF_BY_POLICY,
191 mController.onSwitchToggled(true);
193 verify(transaction).add(any(), eq(ActivationWarningFragment.TAG));