• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.arch.lifecycle.Lifecycle.Event.ON_PAUSE;
20 import static android.arch.lifecycle.Lifecycle.Event.ON_RESUME;
21 import static com.google.common.truth.Truth.assertThat;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Matchers.anyString;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.arch.lifecycle.LifecycleOwner;
28 import android.content.BroadcastReceiver;
29 import android.content.Context;
30 import android.content.IntentFilter;
31 import android.net.wifi.WifiInfo;
32 import android.net.wifi.WifiManager;
33 import android.provider.Settings;
34 import android.support.v7.preference.Preference;
35 import android.support.v7.preference.PreferenceScreen;
36 
37 import com.android.settings.R;
38 import com.android.settings.testutils.SettingsRobolectricTestRunner;
39 import com.android.settingslib.core.lifecycle.Lifecycle;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Answers;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 
48 @RunWith(SettingsRobolectricTestRunner.class)
49 public class WifiInfoPreferenceControllerTest {
50 
51     @Mock
52     private Context mContext;
53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
54     private WifiManager mWifiManager;
55     @Mock
56     private PreferenceScreen mScreen;
57     @Mock
58     private Preference mIpPreference;
59     @Mock
60     private Preference mMacPreference;
61     @Mock
62     private WifiInfo mWifiInfo;
63 
64     private Lifecycle mLifecycle;
65     private LifecycleOwner mLifecycleOwner;
66     private WifiInfoPreferenceController mController;
67 
68     private static final String TEST_MAC_ADDRESS = "42:0a:23:43:ac:02";
69 
70     @Before
setUp()71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73         mLifecycleOwner = () -> mLifecycle;
74         mLifecycle = new Lifecycle(mLifecycleOwner);
75         when(mContext.getSystemService(WifiManager.class))
76                 .thenReturn(mWifiManager);
77         when(mScreen.findPreference(anyString()))
78                 .thenReturn(mMacPreference)
79                 .thenReturn(mIpPreference);
80         when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo);
81         mController = new WifiInfoPreferenceController(mContext, mLifecycle, mWifiManager);
82     }
83 
84     @Test
testIsAvailable_shouldAlwaysReturnTrue()85     public void testIsAvailable_shouldAlwaysReturnTrue() {
86         assertThat(mController.isAvailable()).isTrue();
87     }
88 
89     @Test
getPreferenceKey_shouldReturnNull()90     public void getPreferenceKey_shouldReturnNull() {
91         assertThat(mController.getPreferenceKey()).isNull();
92     }
93 
94     @Test
runThroughLifecycle_shouldInstallListenerOnResume()95     public void runThroughLifecycle_shouldInstallListenerOnResume() {
96         mLifecycle.handleLifecycleEvent(ON_RESUME);
97         verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
98 
99         mLifecycle.handleLifecycleEvent(ON_PAUSE);
100         verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
101     }
102 
103     @Test
onResume_shouldUpdateWifiInfo()104     public void onResume_shouldUpdateWifiInfo() {
105         when(mWifiManager.getCurrentNetwork()).thenReturn(null);
106         when(mWifiInfo.getMacAddress()).thenReturn(TEST_MAC_ADDRESS);
107 
108         mController.displayPreference(mScreen);
109         mController.onResume();
110 
111         verify(mMacPreference).setSummary(TEST_MAC_ADDRESS);
112         verify(mIpPreference).setSummary(any());
113     }
114 
115     @Test
testUpdateMacAddress()116     public void testUpdateMacAddress() {
117         when(mWifiManager.getCurrentNetwork()).thenReturn(null);
118         Settings.Global.putInt(mContext.getContentResolver(),
119                 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 1);
120         mController.displayPreference(mScreen);
121 
122         when(mWifiInfo.getMacAddress()).thenReturn(null);
123         mController.updateWifiInfo();
124         verify(mMacPreference).setSummary(R.string.status_unavailable);
125 
126         when(mWifiInfo.getMacAddress()).thenReturn(WifiInfo.DEFAULT_MAC_ADDRESS);
127         mController.updateWifiInfo();
128         verify(mMacPreference).setSummary(R.string.wifi_status_mac_randomized);
129 
130         when(mWifiInfo.getMacAddress()).thenReturn(TEST_MAC_ADDRESS);
131         mController.updateWifiInfo();
132         verify(mMacPreference).setSummary(TEST_MAC_ADDRESS);
133     }
134 }
135