1 /* 2 * Copyright (C) 2018 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 package com.android.settings.wifi.details; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.mockito.Mockito.doReturn; 20 import static org.mockito.Mockito.spy; 21 22 import android.content.Context; 23 import android.net.wifi.WifiConfiguration; 24 import android.support.v7.preference.DropDownPreference; 25 26 import com.android.settings.R; 27 import com.android.settings.testutils.SettingsRobolectricTestRunner; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.robolectric.RuntimeEnvironment; 34 35 @RunWith(SettingsRobolectricTestRunner.class) 36 public class WifiMeteredPreferenceControllerTest { 37 38 private static final int METERED_OVERRIDE_NONE = 0; 39 private static final int METERED_OVERRIDE_METERED = 1; 40 private static final int METERED_OVERRIDE_NOT_METERED = 2; 41 42 @Mock 43 private WifiConfiguration mWifiConfiguration; 44 45 private WifiMeteredPreferenceController mPreferenceController; 46 private Context mContext; 47 private DropDownPreference mDropDownPreference; 48 49 @Before 50 public void setUp() { 51 mContext = RuntimeEnvironment.application; 52 53 mPreferenceController = spy( 54 new WifiMeteredPreferenceController(mContext, mWifiConfiguration)); 55 mDropDownPreference = new DropDownPreference(mContext); 56 mDropDownPreference.setEntries(R.array.wifi_metered_entries); 57 mDropDownPreference.setEntryValues(R.array.wifi_metered_values); 58 } 59 60 @Test 61 public void testUpdateState_wifiMetered_setCorrectValue() { 62 doReturn(METERED_OVERRIDE_METERED).when(mPreferenceController).getMeteredOverride(); 63 64 mPreferenceController.updateState(mDropDownPreference); 65 66 assertThat(mDropDownPreference.getEntry()).isEqualTo("Treat as metered"); 67 } 68 69 @Test 70 public void testUpdateState_wifiNotMetered_setCorrectValue() { 71 doReturn(METERED_OVERRIDE_NOT_METERED).when(mPreferenceController).getMeteredOverride(); 72 73 mPreferenceController.updateState(mDropDownPreference); 74 75 assertThat(mDropDownPreference.getEntry()).isEqualTo("Treat as unmetered"); 76 } 77 78 @Test 79 public void testUpdateState_wifiAuto_setCorrectValue() { 80 doReturn(METERED_OVERRIDE_NONE).when(mPreferenceController).getMeteredOverride(); 81 82 mPreferenceController.updateState(mDropDownPreference); 83 84 assertThat(mDropDownPreference.getEntry()).isEqualTo("Detect automatically"); 85 } 86 87 @Test 88 public void testController_resilientToNullConfig() { 89 mPreferenceController = spy(new WifiMeteredPreferenceController(mContext, null)); 90 91 mPreferenceController.getMeteredOverride(); 92 mPreferenceController.onPreferenceChange(mDropDownPreference, new Integer(1)); 93 } 94 } 95