Home | History | Annotate | Download | only in tether
      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.wifi.tether;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import android.content.Context;
     22 import android.provider.Settings;
     23 import android.support.v14.preference.SwitchPreference;
     24 
     25 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     26 
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 import org.mockito.MockitoAnnotations;
     31 import org.robolectric.RuntimeEnvironment;
     32 
     33 @RunWith(SettingsRobolectricTestRunner.class)
     34 public class WifiTetherAutoOffPreferenceControllerTest {
     35 
     36     private static final String KEY_PREF = "wifi_tether_auto_off";
     37     private static final int ON = 1;
     38     private static final int OFF = 0;
     39     private Context mContext;
     40     private WifiTetherAutoOffPreferenceController mController;
     41     private SwitchPreference mSwitchPreference;
     42 
     43     @Before
     44     public void setup() {
     45         MockitoAnnotations.initMocks(this);
     46 
     47         mContext = RuntimeEnvironment.application;
     48         mController = new WifiTetherAutoOffPreferenceController(mContext, KEY_PREF);
     49         mSwitchPreference = new SwitchPreference(mContext);
     50     }
     51 
     52     @Test
     53     public void testOnPreferenceChange_toggleOn_settingsOn() {
     54         mController.onPreferenceChange(null, true);
     55 
     56         assertThat(getAutoOffSetting()).isEqualTo(ON);
     57     }
     58 
     59     @Test
     60     public void testOnPreferenceChange_toggleOff_settingsOff() {
     61         mController.onPreferenceChange(null, false);
     62 
     63         assertThat(getAutoOffSetting()).isEqualTo(OFF);
     64     }
     65 
     66     @Test
     67     public void testUpdateState_settingsOn_toggleOn() {
     68         setAutoOffSetting(ON);
     69 
     70         mController.updateState(mSwitchPreference);
     71 
     72         assertThat(mSwitchPreference.isChecked()).isTrue();
     73     }
     74 
     75     @Test
     76     public void testUpdateState_settingsOff_toggleOff() {
     77         setAutoOffSetting(OFF);
     78 
     79         mController.updateState(mSwitchPreference);
     80 
     81         assertThat(mSwitchPreference.isChecked()).isFalse();
     82     }
     83 
     84     @Test
     85     public void testUpdateState_toggleDefaultOn() {
     86         mController.updateState(mSwitchPreference);
     87 
     88         assertThat(mSwitchPreference.isChecked()).isTrue();
     89     }
     90 
     91     private int getAutoOffSetting() {
     92         return Settings.Global.getInt(mContext.getContentResolver(),
     93                 Settings.Global.SOFT_AP_TIMEOUT_ENABLED, OFF);
     94     }
     95 
     96     private void setAutoOffSetting(int config) {
     97         Settings.Global.putInt(mContext.getContentResolver(),
     98                 Settings.Global.SOFT_AP_TIMEOUT_ENABLED, config);
     99     }
    100 }
    101