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; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.IntentFilter; 22 import android.net.wifi.WifiManager; 23 import android.support.v7.preference.Preference; 24 import android.support.v7.preference.PreferenceScreen; 25 26 import com.android.settings.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 import com.android.settings.core.lifecycle.Lifecycle; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Answers; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.annotation.Config; 37 38 import static com.google.common.truth.Truth.assertThat; 39 import static org.mockito.Matchers.any; 40 import static org.mockito.Matchers.anyString; 41 import static org.mockito.Mockito.verify; 42 import static org.mockito.Mockito.when; 43 44 @RunWith(SettingsRobolectricTestRunner.class) 45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 46 public class WifiInfoPreferenceControllerTest { 47 48 @Mock 49 private Context mContext; 50 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 51 private WifiManager mWifiManager; 52 @Mock 53 private PreferenceScreen mScreen; 54 @Mock 55 private Preference mIpPreference; 56 @Mock 57 private Preference mMacPreference; 58 59 private Lifecycle mLifecycle; 60 private WifiInfoPreferenceController mController; 61 62 @Before setUp()63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mLifecycle = new Lifecycle(); 66 when(mContext.getSystemService(WifiManager.class)) 67 .thenReturn(mWifiManager); 68 when(mScreen.findPreference(anyString())) 69 .thenReturn(mMacPreference) 70 .thenReturn(mIpPreference); 71 mController = new WifiInfoPreferenceController(mContext, mLifecycle, mWifiManager); 72 } 73 74 @Test testIsAvailable_shouldAlwaysReturnTrue()75 public void testIsAvailable_shouldAlwaysReturnTrue() { 76 assertThat(mController.isAvailable()).isTrue(); 77 } 78 79 @Test getPreferenceKey_shouldReturnNull()80 public void getPreferenceKey_shouldReturnNull() { 81 assertThat(mController.getPreferenceKey()).isNull(); 82 } 83 84 @Test runThroughLifecycle_shouldInstallListenerOnResume()85 public void runThroughLifecycle_shouldInstallListenerOnResume() { 86 mLifecycle.onResume(); 87 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class)); 88 89 mLifecycle.onPause(); 90 verify(mContext).unregisterReceiver(any(BroadcastReceiver.class)); 91 } 92 93 @Test onResume_shouldUpdateWifiInfo()94 public void onResume_shouldUpdateWifiInfo() { 95 when(mWifiManager.getCurrentNetwork()).thenReturn(null); 96 97 mController.displayPreference(mScreen); 98 mController.onResume(); 99 100 verify(mMacPreference).setSummary(any()); 101 verify(mIpPreference).setSummary(any()); 102 } 103 } 104