Home | History | Annotate | Download | only in deletionhelper
      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.deletionhelper;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import static org.mockito.Matchers.any;
     22 import static org.mockito.Matchers.eq;
     23 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
     24 import static org.mockito.Mockito.mock;
     25 import static org.mockito.Mockito.never;
     26 import static org.mockito.Mockito.spy;
     27 import static org.mockito.Mockito.verify;
     28 
     29 import android.app.Fragment;
     30 import android.app.FragmentManager;
     31 import android.content.Context;
     32 import android.provider.Settings;
     33 import android.support.v7.preference.Preference;
     34 
     35 import com.android.internal.logging.nano.MetricsProto;
     36 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     37 import com.android.settings.TestConfig;
     38 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
     39 import com.android.settings.overlay.FeatureFactory;
     40 import com.android.settings.testutils.FakeFeatureFactory;
     41 import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
     42 import com.android.settings.widget.SwitchBar;
     43 
     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 @RunWith(SettingsRobolectricTestRunner.class)
     54 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     55 public class AutomaticStorageManagerSwitchBarControllerTest {
     56     private Context mContext;
     57     private SwitchBar mSwitchBar;
     58     private MetricsFeatureProvider mMetricsFeatureProvider;
     59     private Preference mPreference;
     60 
     61     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     62     private FragmentManager mFragmentManager;
     63 
     64     private AutomaticStorageManagerSwitchBarController mController;
     65 
     66     @Before
     67     public void setUp() {
     68         MockitoAnnotations.initMocks(this);
     69 
     70         mContext = spy(RuntimeEnvironment.application);
     71         mSwitchBar = new SwitchBar(mContext);
     72 
     73         Context fakeContextForFakeProvider = mock(Context.class, RETURNS_DEEP_STUBS);
     74         FakeFeatureFactory.setupForTest(fakeContextForFakeProvider);
     75         FeatureFactory featureFactory = FakeFeatureFactory.getFactory(fakeContextForFakeProvider);
     76         mMetricsFeatureProvider = featureFactory.getMetricsFeatureProvider();
     77         mPreference = new Preference(mContext);
     78 
     79         mController =
     80                 new AutomaticStorageManagerSwitchBarController(
     81                         mContext,
     82                         mSwitchBar,
     83                         mMetricsFeatureProvider,
     84                         mPreference,
     85                         mFragmentManager);
     86     }
     87 
     88     @Test
     89     public void onSwitchChanged_false_recordsAMetric() {
     90         mController.onSwitchChanged(null, false);
     91 
     92         verify(mMetricsFeatureProvider)
     93                 .action(
     94                         eq(mContext),
     95                         eq(MetricsProto.MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER),
     96                         eq(false));
     97     }
     98 
     99     @Test
    100     public void onSwitchChanged_true_recordsAMetric() {
    101         mController.onSwitchChanged(null, true);
    102 
    103         verify(mMetricsFeatureProvider)
    104                 .action(
    105                         eq(mContext),
    106                         eq(MetricsProto.MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER),
    107                         eq(true));
    108     }
    109 
    110     @Test
    111     public void onSwitchChanged_showWarningFragmentIfNotEnabledByDefault() {
    112         mController.onSwitchChanged(null, true);
    113 
    114         verify(mFragmentManager.beginTransaction())
    115                 .add(any(Fragment.class), eq(ActivationWarningFragment.TAG));
    116     }
    117 
    118     @Config(shadows = {SettingsShadowSystemProperties.class})
    119     @Test
    120     public void onSwitchChange_doNotShowWarningFragmentIfEnabledByDefault() {
    121         SettingsShadowSystemProperties.set("ro.storage_manager.enabled", "true");
    122 
    123         mController.onSwitchChanged(null, true);
    124 
    125         verify(mFragmentManager.beginTransaction(), never())
    126                 .add(any(Fragment.class), eq(ActivationWarningFragment.TAG));
    127     }
    128 
    129     @Test
    130     public void initializeSwitchOnConstruction() {
    131         Settings.Secure.putInt(
    132                 mContext.getContentResolver(),
    133                 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED,
    134                 1);
    135 
    136         mController =
    137                 new AutomaticStorageManagerSwitchBarController(
    138                         mContext,
    139                         mSwitchBar,
    140                         mMetricsFeatureProvider,
    141                         mPreference,
    142                         mFragmentManager);
    143 
    144         assertThat(mSwitchBar.isChecked()).isTrue();
    145     }
    146 
    147     @Test
    148     public void initializingSwitchDoesNotTriggerView() {
    149         Settings.Secure.putInt(
    150                 mContext.getContentResolver(),
    151                 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED,
    152                 1);
    153 
    154         mController =
    155                 new AutomaticStorageManagerSwitchBarController(
    156                         mContext,
    157                         mSwitchBar,
    158                         mMetricsFeatureProvider,
    159                         mPreference,
    160                         mFragmentManager);
    161 
    162         verify(mFragmentManager.beginTransaction(), never())
    163                 .add(any(Fragment.class), eq(ActivationWarningFragment.TAG));
    164     }
    165 }
    166