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