Home | History | Annotate | Download | only in storage
      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.os.SystemProperties;
     33 import android.provider.Settings;
     34 import android.support.v7.preference.Preference;
     35 import android.support.v7.preference.PreferenceScreen;
     36 
     37 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     38 import com.android.internal.os.RoSystemProperties;
     39 import com.android.settings.deletionhelper.ActivationWarningFragment;
     40 import com.android.settings.overlay.FeatureFactory;
     41 import com.android.settings.testutils.FakeFeatureFactory;
     42 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     43 import com.android.settings.widget.MasterSwitchPreference;
     44 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
     45 
     46 import org.junit.Before;
     47 import org.junit.Test;
     48 import org.junit.runner.RunWith;
     49 import org.mockito.Answers;
     50 import org.mockito.Mock;
     51 import org.mockito.MockitoAnnotations;
     52 import org.robolectric.RuntimeEnvironment;
     53 import org.robolectric.util.ReflectionHelpers;
     54 
     55 @RunWith(SettingsRobolectricTestRunner.class)
     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 
     70     @Before
     71     public void setUp() {
     72         MockitoAnnotations.initMocks(this);
     73         mContext = RuntimeEnvironment.application.getApplicationContext();
     74         final FeatureFactory factory = FeatureFactory.getFactory(mContext);
     75         final MetricsFeatureProvider metricsFeature = factory.getMetricsFeatureProvider();
     76 
     77         mController = new AutomaticStorageManagementSwitchPreferenceController(
     78                 mContext, metricsFeature, mFragmentManager);
     79         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
     80     }
     81 
     82     @Test
     83     public void isAvailable_shouldReturnTrue_forHighRamDevice() {
     84         assertThat(mController.isAvailable()).isTrue();
     85     }
     86 
     87     @Test
     88     public void isAvailable_shouldAlwaysReturnFalse_forLowRamDevice() {
     89         ReflectionHelpers.setStaticField(RoSystemProperties.class, "CONFIG_LOW_RAM", true);
     90         assertThat(mController.isAvailable()).isFalse();
     91         ReflectionHelpers.setStaticField(RoSystemProperties.class, "CONFIG_LOW_RAM", false);
     92     }
     93 
     94     @Test
     95     public void onResume_shouldReflectEnabledStatus() {
     96         mController.displayPreference(mScreen);
     97         final ContentResolver resolver = mContext.getContentResolver();
     98         Settings.Secure.putInt(resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 1);
     99 
    100         mController.onResume();
    101 
    102         verify(mPreference).setChecked(eq(true));
    103     }
    104 
    105     @Test
    106     public void onResume_shouldRegisterCallback() {
    107         mController.displayPreference(mScreen);
    108         mController.onResume();
    109 
    110         verify(mPreference).setOnPreferenceChangeListener(
    111                 any(Preference.OnPreferenceChangeListener.class));
    112     }
    113 
    114     @Test
    115     public void togglingShouldCauseMetricsEvent() {
    116         // FakeFeatureFactory uses mock contexts, so this test scaffolds itself rather than using
    117         // the instance variables.
    118         final FakeFeatureFactory factory = FakeFeatureFactory.setupForTest();
    119         final AutomaticStorageManagementSwitchPreferenceController controller =
    120                 new AutomaticStorageManagementSwitchPreferenceController(
    121                         mMockContext, factory.metricsFeatureProvider, mFragmentManager);
    122 
    123         controller.onSwitchToggled(true);
    124 
    125         verify(factory.metricsFeatureProvider, times(1)).action(
    126                 any(Context.class), eq(MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER), eq(true));
    127     }
    128 
    129     @Test
    130     public void togglingShouldUpdateSettingsSecure() {
    131         mController.onSwitchToggled(true);
    132 
    133         final ContentResolver resolver = mContext.getContentResolver();
    134         assertThat(Settings.Secure.getInt(
    135                 resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0)).isNotEqualTo(0);
    136     }
    137 
    138     @Test
    139     public void togglingOnShouldTriggerWarningFragment() {
    140         final FragmentTransaction transaction = mock(FragmentTransaction.class);
    141         when(mFragmentManager.beginTransaction()).thenReturn(transaction);
    142         SystemProperties.set(
    143                 AutomaticStorageManagementSwitchPreferenceController
    144                         .STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "false");
    145 
    146         mController.onSwitchToggled(true);
    147 
    148         verify(transaction).add(any(), eq(ActivationWarningFragment.TAG));
    149     }
    150 
    151     @Test
    152     public void togglingOffShouldTriggerWarningFragment() {
    153         final FragmentTransaction transaction = mock(FragmentTransaction.class);
    154         when(mFragmentManager.beginTransaction()).thenReturn(transaction);
    155 
    156         mController.onSwitchToggled(false);
    157 
    158         verify(transaction, never()).add(any(), eq(ActivationWarningFragment.TAG));
    159     }
    160 
    161 
    162     @Test
    163     public void togglingOnShouldNotTriggerWarningFragmentIfEnabledByDefault() {
    164         final FragmentTransaction transaction = mock(FragmentTransaction.class);
    165         when(mFragmentManager.beginTransaction()).thenReturn(transaction);
    166         SystemProperties.set(
    167             AutomaticStorageManagementSwitchPreferenceController
    168                         .STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "true");
    169 
    170         mController.onSwitchToggled(true);
    171 
    172         verify(transaction, never()).add(any(), eq(ActivationWarningFragment.TAG));
    173     }
    174 
    175     @Test
    176     public void togglingOnShouldTriggerWarningFragmentIfEnabledByDefaultAndDisabledByPolicy() {
    177         final FragmentTransaction transaction = mock(FragmentTransaction.class);
    178         when(mFragmentManager.beginTransaction()).thenReturn(transaction);
    179         SystemProperties.set(
    180                 AutomaticStorageManagementSwitchPreferenceController
    181                         .STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY,
    182                 "true");
    183         Settings.Secure.putInt(
    184                 mContext.getContentResolver(),
    185                 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_TURNED_OFF_BY_POLICY,
    186                 1);
    187 
    188         mController.onSwitchToggled(true);
    189 
    190         verify(transaction).add(any(), eq(ActivationWarningFragment.TAG));
    191     }
    192 }
    193