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 static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.anyString; 24 import static org.mockito.Mockito.doNothing; 25 import static org.mockito.Mockito.doReturn; 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.verify; 29 30 import android.content.BroadcastReceiver; 31 import android.content.Context; 32 import android.content.Intent; 33 import android.content.IntentFilter; 34 import android.net.wifi.WifiManager; 35 36 import com.android.settings.R; 37 import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener; 38 import com.android.settingslib.wifi.WifiStatusTracker; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.RuntimeEnvironment; 47 48 @RunWith(RobolectricTestRunner.class) 49 public class WifiSummaryUpdaterTest { 50 @Mock private WifiStatusTracker mWifiTracker; 51 @Mock private SummaryListener mListener; 52 53 private Context mContext; 54 private WifiSummaryUpdater mSummaryUpdater; 55 56 @Before setUp()57 public void setUp() { 58 MockitoAnnotations.initMocks(this); 59 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 60 doReturn(mock(Intent.class)).when(mContext).registerReceiver(any(), any(), anyInt()); 61 doNothing().when(mContext).unregisterReceiver(any(BroadcastReceiver.class)); 62 63 mSummaryUpdater = new WifiSummaryUpdater(mContext, mListener, mWifiTracker); 64 } 65 66 @Test register_true_shouldRegisterListenerAndTracker()67 public void register_true_shouldRegisterListenerAndTracker() { 68 mSummaryUpdater.register(true); 69 70 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class), 71 anyInt()); 72 verify(mWifiTracker).setListening(true); 73 } 74 75 @Test register_true_shouldFetchInitialStateAndSendSummaryChange()76 public void register_true_shouldFetchInitialStateAndSendSummaryChange() { 77 mSummaryUpdater.register(true); 78 79 verify(mWifiTracker).fetchInitialState(); 80 verify(mListener).onSummaryChanged(anyString()); 81 } 82 83 @Test register_false_shouldUnregisterListenerAndTracker()84 public void register_false_shouldUnregisterListenerAndTracker() { 85 mSummaryUpdater.register(true); 86 mSummaryUpdater.register(false); 87 88 verify(mContext).unregisterReceiver(any(BroadcastReceiver.class)); 89 verify(mWifiTracker).setListening(false); 90 } 91 92 @Test onReceive_networkStateChanged_shouldSendSummaryChange()93 public void onReceive_networkStateChanged_shouldSendSummaryChange() { 94 mSummaryUpdater.register(true); 95 mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION)); 96 97 verify(mListener).onSummaryChanged(anyString()); 98 } 99 100 @Test onReceive_rssiChanged_shouldSendSummaryChange()101 public void onReceive_rssiChanged_shouldSendSummaryChange() { 102 mSummaryUpdater.register(true); 103 mContext.sendBroadcast(new Intent(WifiManager.RSSI_CHANGED_ACTION)); 104 105 verify(mListener).onSummaryChanged(anyString()); 106 } 107 108 @Test getSummary_wifiDisabled_shouldReturnDisabled()109 public void getSummary_wifiDisabled_shouldReturnDisabled() { 110 mWifiTracker.enabled = false; 111 112 assertThat(mSummaryUpdater.getSummary()) 113 .isEqualTo(mContext.getString(R.string.switch_off_text)); 114 } 115 116 @Test getSummary_wifiDisconnected_shouldReturnDisconnected()117 public void getSummary_wifiDisconnected_shouldReturnDisconnected() { 118 mWifiTracker.enabled = true; 119 mWifiTracker.connected = false; 120 121 assertThat(mSummaryUpdater.getSummary()) 122 .isEqualTo(mContext.getString(R.string.disconnected)); 123 } 124 125 @Test getSummary_wifiConnected_shouldReturnSsid()126 public void getSummary_wifiConnected_shouldReturnSsid() { 127 mWifiTracker.enabled = true; 128 mWifiTracker.connected = true; 129 mWifiTracker.ssid = "Test Ssid"; 130 131 assertThat(mSummaryUpdater.getSummary()).isEqualTo("Test Ssid"); 132 } 133 134 @Test getSummary_wifiConnected_withSpeedLabel_shouldReturnSsid_withSpeedLabel()135 public void getSummary_wifiConnected_withSpeedLabel_shouldReturnSsid_withSpeedLabel() { 136 mWifiTracker.enabled = true; 137 mWifiTracker.connected = true; 138 mWifiTracker.ssid = "Test Ssid"; 139 mWifiTracker.statusLabel = "Very Fast"; 140 141 assertThat(mSummaryUpdater.getSummary()).isEqualTo("Test Ssid / Very Fast"); 142 } 143 144 private class SummaryListener implements OnSummaryChangeListener { 145 private String summary; 146 147 @Override onSummaryChanged(String summary)148 public void onSummaryChanged(String summary) { 149 this.summary = summary; 150 } 151 } 152 } 153