WifiControllerTest.java revision f4267b6840dbc7f430638c35c5448187b6e83846
1/*
2 * Copyright (C) 2016 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
17package com.android.server.wifi;
18
19import static com.android.server.wifi.WifiController.CMD_AP_STOPPED;
20import static com.android.server.wifi.WifiController.CMD_EMERGENCY_CALL_STATE_CHANGED;
21import static com.android.server.wifi.WifiController.CMD_EMERGENCY_MODE_CHANGED;
22import static com.android.server.wifi.WifiController.CMD_SET_AP;
23import static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED;
24
25import static org.junit.Assert.assertEquals;
26import static org.mockito.Matchers.*;
27import static org.mockito.Mockito.*;
28
29import android.content.ContentResolver;
30import android.content.Context;
31import android.os.test.TestLooper;
32import android.test.suitebuilder.annotation.SmallTest;
33import android.util.Log;
34
35import com.android.internal.util.IState;
36import com.android.internal.util.StateMachine;
37
38import org.junit.After;
39import org.junit.Before;
40import org.junit.Test;
41import org.mockito.InOrder;
42import org.mockito.Mock;
43import org.mockito.MockitoAnnotations;
44
45import java.io.ByteArrayOutputStream;
46import java.io.PrintWriter;
47import java.lang.reflect.Method;
48
49/**
50 * Test WifiController for changes in and out of ECM and SoftAP modes.
51 */
52@SmallTest
53public class WifiControllerTest {
54
55    private static final String TAG = "WifiControllerTest";
56
57    private void dumpState() {
58        ByteArrayOutputStream stream = new ByteArrayOutputStream();
59        PrintWriter writer = new PrintWriter(stream);
60        mWifiController.dump(null, writer, null);
61        writer.flush();
62        Log.d(TAG, "WifiStateMachine state -" + stream.toString());
63    }
64
65    private IState getCurrentState() throws Exception {
66        Method method = StateMachine.class.getDeclaredMethod("getCurrentState");
67        method.setAccessible(true);
68        return (IState) method.invoke(mWifiController);
69    }
70
71    private void initializeSettingsStore() throws Exception {
72        when(mSettingsStore.isAirplaneModeOn()).thenReturn(false);
73        when(mSettingsStore.isWifiToggleEnabled()).thenReturn(false);
74        when(mSettingsStore.isScanAlwaysAvailable()).thenReturn(true);
75    }
76
77    TestLooper mLooper;
78    @Mock Context mContext;
79    @Mock WifiServiceImpl mService;
80    @Mock FrameworkFacade mFacade;
81    @Mock WifiSettingsStore mSettingsStore;
82    @Mock WifiStateMachine mWifiStateMachine;
83    @Mock WifiServiceImpl.LockList mLockList;
84
85    WifiController mWifiController;
86
87    @Before
88    public void setUp() throws Exception {
89        MockitoAnnotations.initMocks(this);
90
91        mLooper = new TestLooper();
92
93        initializeSettingsStore();
94
95        when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
96
97        mWifiController = new WifiController(mContext, mWifiStateMachine,
98                mSettingsStore, mLockList, mLooper.getLooper(), mFacade);
99
100        mWifiController.start();
101        mLooper.dispatchAll();
102    }
103
104    @After
105    public void cleanUp() {
106        mLooper.dispatchAll();
107    }
108
109    @Test
110    public void enableWifi() throws Exception {
111        assertEquals("StaDisabledWithScanState", getCurrentState().getName());
112
113        when(mSettingsStore.isWifiToggleEnabled()).thenReturn(true);
114        mWifiController.sendMessage(CMD_WIFI_TOGGLED);
115        mLooper.dispatchAll();
116        assertEquals("DeviceActiveState", getCurrentState().getName());
117
118        when(mSettingsStore.isWifiToggleEnabled()).thenReturn(false);
119        mWifiController.sendMessage(CMD_WIFI_TOGGLED);
120        mLooper.dispatchAll();
121        assertEquals("StaDisabledWithScanState", getCurrentState().getName());
122
123        when(mSettingsStore.isWifiToggleEnabled()).thenReturn(true);
124        mWifiController.sendMessage(CMD_WIFI_TOGGLED);
125        mLooper.dispatchAll();
126        assertEquals("DeviceActiveState", getCurrentState().getName());
127    }
128
129    @Test
130    public void testEcmOn() throws Exception {
131        enableWifi();
132
133        // Test with WifiDisableInECBM turned on:
134        when(mFacade.getConfigWiFiDisableInECBM(mContext)).thenReturn(true);
135        doTestEcm(true);
136    }
137
138    @Test
139    public void testEcmOff() throws Exception {
140        enableWifi();
141
142        // Test with WifiDisableInECBM turned off
143        when(mFacade.getConfigWiFiDisableInECBM(mContext)).thenReturn(false);
144        doTestEcm(false);
145    }
146
147    private void assertInEcm(boolean ecmEnabled) throws Exception {
148        if (ecmEnabled) {
149            assertEquals("EcmState", getCurrentState().getName());
150        } else {
151            assertEquals("DeviceActiveState", getCurrentState().getName());
152        }
153    }
154
155
156    private void doTestEcm(boolean ecmEnabled) throws Exception {
157
158        // test ecm changed
159        mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, 1);
160        mLooper.dispatchAll();
161        assertInEcm(ecmEnabled);
162
163        mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, 0);
164        mLooper.dispatchAll();
165        assertEquals("DeviceActiveState", getCurrentState().getName());
166
167        // test call state changed
168        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 1);
169        mLooper.dispatchAll();
170        assertInEcm(ecmEnabled);
171
172        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 0);
173        mLooper.dispatchAll();
174        assertEquals("DeviceActiveState", getCurrentState().getName());
175
176
177        // test both changed (variation 1 - the good case)
178        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 1);
179        mLooper.dispatchAll();
180        assertInEcm(ecmEnabled);
181
182        mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, 1);
183        mLooper.dispatchAll();
184        assertInEcm(ecmEnabled);
185
186        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 0);
187        mLooper.dispatchAll();
188        assertInEcm(ecmEnabled);
189
190        mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, 0);
191        mLooper.dispatchAll();
192        assertEquals("DeviceActiveState", getCurrentState().getName());
193
194        // test both changed (variation 2 - emergency call in ecm)
195        mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, 1);
196        mLooper.dispatchAll();
197        assertInEcm(ecmEnabled);
198
199        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 1);
200        mLooper.dispatchAll();
201        assertInEcm(ecmEnabled);
202
203        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 0);
204        mLooper.dispatchAll();
205        assertInEcm(ecmEnabled);
206
207        mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, 0);
208        mLooper.dispatchAll();
209        assertEquals("DeviceActiveState", getCurrentState().getName());
210
211        // test both changed (variation 3 - not so good order of events)
212        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 1);
213        mLooper.dispatchAll();
214        assertInEcm(ecmEnabled);
215
216        mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, 1);
217        mLooper.dispatchAll();
218        assertInEcm(ecmEnabled);
219
220        mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, 0);
221        mLooper.dispatchAll();
222        assertInEcm(ecmEnabled);
223
224        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 0);
225        mLooper.dispatchAll();
226        assertEquals("DeviceActiveState", getCurrentState().getName());
227
228        // test that Wifi toggle doesn't exit Ecm
229        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 1);
230        mLooper.dispatchAll();
231        assertInEcm(ecmEnabled);
232
233        when(mSettingsStore.isWifiToggleEnabled()).thenReturn(true);
234        mWifiController.sendMessage(CMD_WIFI_TOGGLED);
235        mLooper.dispatchAll();
236        assertInEcm(ecmEnabled);
237
238        mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, 0);
239        mLooper.dispatchAll();
240        assertEquals("DeviceActiveState", getCurrentState().getName());
241    }
242
243    /**
244     * When AP mode is enabled and wifi was previously in AP mode, we should return to
245     * DeviceActiveState after the AP is disabled.
246     * Enter DeviceActiveState, activate AP mode, disable AP mode.
247     * <p>
248     * Expected: AP should successfully start and exit, then return to DeviceActiveState.
249     */
250    @Test
251    public void testReturnToDeviceActiveStateAfterAPModeShutdown() throws Exception {
252        enableWifi();
253        assertEquals("DeviceActiveState", getCurrentState().getName());
254
255        mWifiController.obtainMessage(CMD_SET_AP, 1, 0).sendToTarget();
256        mLooper.dispatchAll();
257        assertEquals("ApEnabledState", getCurrentState().getName());
258
259        when(mSettingsStore.getWifiSavedState()).thenReturn(1);
260        mWifiController.obtainMessage(CMD_AP_STOPPED).sendToTarget();
261        mLooper.dispatchAll();
262
263        InOrder inOrder = inOrder(mWifiStateMachine);
264        inOrder.verify(mWifiStateMachine).setSupplicantRunning(true);
265        inOrder.verify(mWifiStateMachine).setOperationalMode(WifiStateMachine.CONNECT_MODE);
266        inOrder.verify(mWifiStateMachine).setDriverStart(true);
267        assertEquals("DeviceActiveState", getCurrentState().getName());
268    }
269}
270