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.android.settings.development.WifiConnectedMacRandomizationPreferenceController.SETTING_VALUE_OFF;
     20 import static com.android.settings.development.WifiConnectedMacRandomizationPreferenceController.SETTING_VALUE_ON;
     21 import static com.google.common.truth.Truth.assertThat;
     22 import static org.mockito.Mockito.when;
     23 
     24 import android.content.Context;
     25 import android.provider.Settings;
     26 import android.support.v14.preference.SwitchPreference;
     27 import android.support.v7.preference.PreferenceScreen;
     28 
     29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     30 
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 import org.mockito.Mock;
     35 import org.mockito.MockitoAnnotations;
     36 import org.robolectric.RuntimeEnvironment;
     37 import org.robolectric.annotation.Config;
     38 
     39 @RunWith(SettingsRobolectricTestRunner.class)
     40 public class WifiConnectedMacRandomizationPreferenceControllerTest {
     41 
     42     @Mock
     43     private PreferenceScreen mPreferenceScreen;
     44 
     45     private Context mContext;
     46     private SwitchPreference mPreference;
     47     private WifiConnectedMacRandomizationPreferenceController mController;
     48 
     49     @Before
     50     public void setup() {
     51         MockitoAnnotations.initMocks(this);
     52         mContext = RuntimeEnvironment.application;
     53         mController = new WifiConnectedMacRandomizationPreferenceController(mContext);
     54         mPreference = new SwitchPreference(mContext);
     55         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
     56             .thenReturn(mPreference);
     57         mController.displayPreference(mPreferenceScreen);
     58     }
     59 
     60     @Test
     61     public void isAvailable_trueSupportFlag_shouldReturnTrue() {
     62         assertThat(mController.isAvailable()).isTrue();
     63     }
     64 
     65     @Test
     66     @Config(qualifiers = "mcc999")
     67     public void isAvailable_falseSupportFlag_shouldReturnFalse() {
     68         assertThat(mController.isAvailable()).isFalse();
     69     }
     70 
     71     @Test
     72     public void onPreferenceChange_settingEnabled_shouldEnableConnectedMacRandomization() {
     73         mController.onPreferenceChange(mPreference, true /* new value */);
     74 
     75         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
     76                 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, -1 /* default */);
     77 
     78         assertThat(mode).isEqualTo(SETTING_VALUE_ON);
     79     }
     80 
     81     @Test
     82     public void onPreferenceChange_settingDisabled_shouldDisableConnectedMacRandomization() {
     83         mController.onPreferenceChange(mPreference, false /* new value */);
     84 
     85         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
     86                 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, -1 /* default */);
     87 
     88         assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
     89     }
     90 
     91     @Test
     92     public void updateState_settingEnabled_shouldEnablePreference() {
     93         Settings.Global.putInt(mContext.getContentResolver(),
     94                 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, SETTING_VALUE_ON);
     95         mController.updateState(mPreference);
     96 
     97         assertThat(mPreference.isChecked()).isTrue();
     98     }
     99 
    100     @Test
    101     public void updateState_settingDisabled_shouldDisablePreference() {
    102         Settings.Global.putInt(mContext.getContentResolver(),
    103                 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, SETTING_VALUE_OFF);
    104         mController.updateState(mPreference);
    105 
    106         assertThat(mPreference.isChecked()).isFalse();
    107     }
    108 
    109     @Test
    110     public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
    111         mController.onDeveloperOptionsSwitchDisabled();
    112 
    113         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
    114                 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, -1 /* default */);
    115 
    116         assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
    117         assertThat(mPreference.isChecked()).isFalse();
    118         assertThat(mPreference.isEnabled()).isFalse();
    119     }
    120 }
    121