Home | History | Annotate | Download | only in development
      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.development;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Mockito.verify;
     21 import static org.mockito.Mockito.when;
     22 
     23 import android.content.Context;
     24 import android.provider.Settings;
     25 import android.support.v14.preference.SwitchPreference;
     26 import android.support.v7.preference.PreferenceScreen;
     27 
     28 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     29 
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 import org.mockito.Mock;
     34 import org.mockito.MockitoAnnotations;
     35 import org.robolectric.RuntimeEnvironment;
     36 
     37 @RunWith(SettingsRobolectricTestRunner.class)
     38 public class MobileDataAlwaysOnPreferenceControllerTest {
     39 
     40     @Mock
     41     private SwitchPreference mPreference;
     42     @Mock
     43     private PreferenceScreen mPreferenceScreen;
     44 
     45     private Context mContext;
     46     private MobileDataAlwaysOnPreferenceController mController;
     47 
     48     @Before
     49     public void setup() {
     50         MockitoAnnotations.initMocks(this);
     51         mContext = RuntimeEnvironment.application;
     52         mController = new MobileDataAlwaysOnPreferenceController(mContext);
     53         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
     54             .thenReturn(mPreference);
     55         mController.displayPreference(mPreferenceScreen);
     56     }
     57 
     58     @Test
     59     public void onPreferenceChanged_turnOnPreference_shouldEnableMobileDataAlwaysOn() {
     60         mController.onPreferenceChange(mPreference, true /* new value */);
     61 
     62         final int mode = Settings.System.getInt(mContext.getContentResolver(),
     63                 Settings.Global.MOBILE_DATA_ALWAYS_ON, -1 /* default */);
     64 
     65         assertThat(mode).isEqualTo(MobileDataAlwaysOnPreferenceController.SETTING_VALUE_ON);
     66     }
     67 
     68     @Test
     69     public void onPreferenceChanged_turnOffPreference_shouldDisableMobileDataAlwaysOn() {
     70         mController.onPreferenceChange(mPreference, false /* new value */);
     71 
     72         final int mode = Settings.System.getInt(mContext.getContentResolver(),
     73                 Settings.Global.MOBILE_DATA_ALWAYS_ON, -1 /* default */);
     74 
     75         assertThat(mode).isEqualTo(MobileDataAlwaysOnPreferenceController.SETTING_VALUE_OFF);
     76     }
     77 
     78     @Test
     79     public void updateState_settingEnabled_preferenceShouldBeChecked() {
     80         Settings.System.putInt(mContext.getContentResolver(), Settings.Global.MOBILE_DATA_ALWAYS_ON,
     81                 MobileDataAlwaysOnPreferenceController.SETTING_VALUE_ON);
     82         mController.updateState(mPreference);
     83 
     84         verify(mPreference).setChecked(true);
     85     }
     86 
     87     @Test
     88     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
     89         Settings.System.putInt(mContext.getContentResolver(), Settings.Global.MOBILE_DATA_ALWAYS_ON,
     90                 MobileDataAlwaysOnPreferenceController.SETTING_VALUE_OFF);
     91         mController.updateState(mPreference);
     92 
     93         verify(mPreference).setChecked(false);
     94     }
     95 
     96     @Test
     97     public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeDisabled() {
     98         mController.onDeveloperOptionsSwitchDisabled();
     99         final int mode = Settings.System.getInt(mContext.getContentResolver(),
    100                 Settings.Global.MOBILE_DATA_ALWAYS_ON, -1 /* default */);
    101 
    102         assertThat(mode).isEqualTo(MobileDataAlwaysOnPreferenceController.SETTING_VALUE_OFF);
    103         verify(mPreference).setEnabled(false);
    104         verify(mPreference).setChecked(false);
    105     }
    106 }
    107