• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.server.wifi;
18 
19 import static android.telephony.TelephonyManager.CALL_STATE_IDLE;
20 import static android.telephony.TelephonyManager.CALL_STATE_OFFHOOK;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.*;
28 
29 import android.content.Context;
30 import android.content.pm.ApplicationInfo;
31 import android.hardware.Sensor;
32 import android.hardware.SensorEvent;
33 import android.hardware.SensorEventListener;
34 import android.hardware.SystemSensorManager;
35 import android.net.wifi.WifiManager;
36 import android.os.Build;
37 import android.os.test.TestLooper;
38 import android.telephony.PhoneStateListener;
39 import android.telephony.TelephonyManager;
40 
41 import androidx.test.filters.SmallTest;
42 
43 import com.android.internal.R;
44 
45 import org.junit.After;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.mockito.ArgumentCaptor;
49 import org.mockito.InOrder;
50 import org.mockito.Mock;
51 import org.mockito.MockitoAnnotations;
52 
53 import java.lang.reflect.Constructor;
54 import java.lang.reflect.Field;
55 import java.util.ArrayList;
56 import java.util.List;
57 
58 /**
59  * unit tests for {@link com.android.server.wifi.SarManager}.
60  */
61 @SmallTest
62 public class SarManagerTest {
63     private static final String TAG = "WifiSarManagerTest";
64     private static final String OP_PACKAGE_NAME = "com.xxx";
65     private static final String SAR_SENSOR_NAME = "com.google.sensor.sar";
66 
67     private static final int SAR_SENSOR_EVENT_FREE_SPACE = 1;
68     private static final int SAR_SENSOR_EVENT_HAND       = 2;
69     private static final int SAR_SENSOR_EVENT_HEAD       = 3;
70     private static final int SAR_SENSOR_EVENT_BODY       = 4;
71 
enableDebugLogs()72     private void enableDebugLogs() {
73         mSarMgr.enableVerboseLogging(1);
74     }
75 
getMockResources()76     private MockResources getMockResources() {
77         MockResources resources = new MockResources();
78         return resources;
79     }
80 
81     private SarManager mSarMgr;
82     private TestLooper mLooper;
83     private MockResources mResources;
84     private PhoneStateListener mPhoneStateListener;
85     private List<Sensor> mSensorList;
86     private Sensor mSensor;
87     private SarInfo mSarInfo;
88 
89     @Mock private Context mContext;
90     @Mock SensorEventListener mSensorEventListener;
91     @Mock SystemSensorManager mSensorManager;
92     @Mock TelephonyManager mTelephonyManager;
93     @Mock private ApplicationInfo mMockApplInfo;
94     @Mock WifiNative mWifiNative;
95     @Mock WifiMetrics mWifiMetrics;
96 
97     @Before
setUp()98     public void setUp() throws Exception {
99         /* Ensure Looper exists */
100         mLooper = new TestLooper();
101 
102         MockitoAnnotations.initMocks(this);
103 
104         /* Default behavior is to return with success */
105         when(mWifiNative.selectTxPowerScenario(any(SarInfo.class))).thenReturn(true);
106 
107         mResources = getMockResources();
108 
109         when(mContext.getResources()).thenReturn(mResources);
110         mMockApplInfo.targetSdkVersion = Build.VERSION_CODES.P;
111         when(mContext.getApplicationInfo()).thenReturn(mMockApplInfo);
112         when(mContext.getOpPackageName()).thenReturn(OP_PACKAGE_NAME);
113     }
114 
115     @After
cleanUp()116     public void cleanUp() throws Exception {
117         mSarMgr = null;
118         mLooper = null;
119         mContext = null;
120         mResources = null;
121     }
122 
123     /**
124      * Helper function to capture SarInfo object
125      */
captureSarInfo(WifiNative wifiNative)126     private void captureSarInfo(WifiNative wifiNative) {
127         /* Capture the SensorEventListener */
128         ArgumentCaptor<SarInfo> sarInfoCaptor = ArgumentCaptor.forClass(SarInfo.class);
129         verify(wifiNative).selectTxPowerScenario(sarInfoCaptor.capture());
130         mSarInfo = sarInfoCaptor.getValue();
131         assertNotNull(mSarInfo);
132     }
133 
134     /**
135      * Helper function to create and prepare sensor info
136      */
prepareSensorInfo(boolean registerReturn)137     private void prepareSensorInfo(boolean registerReturn) {
138         /* Create a sensor object (note, this can not be mocked since it is a final class) */
139         Constructor<Sensor> constructor =
140                 (Constructor<Sensor>) Sensor.class.getDeclaredConstructors()[0];
141         constructor.setAccessible(true);
142 
143         try {
144             mSensor = constructor.newInstance();
145         } catch (Exception e) {
146             fail("Failed to create a sensor object");
147         }
148 
149         /* Now set the mStringType field with the proper field */
150         Field declaredField = null;
151         try {
152             declaredField = Sensor.class.getDeclaredField("mStringType");
153             declaredField.setAccessible(true);
154             declaredField.set(mSensor, SAR_SENSOR_NAME);
155         } catch (Exception e) {
156             fail("Could not set sensor string type");
157         }
158 
159         /* Prepare the sensor list */
160         mSensorList = new ArrayList<Sensor>();
161         mSensorList.add(mSensor);
162         when(mSensorManager.getSensorList(Sensor.TYPE_ALL)).thenReturn(mSensorList);
163         when(mSensorManager.registerListener(any(SensorEventListener.class), any(Sensor.class),
164                   anyInt())).thenReturn(registerReturn);
165     }
166 
167     /**
168      * Helper function to set configuration for SAR and create the SAR Manager
169      *
170      */
createSarManager(boolean isSarEnabled, boolean isSarSapEnabled, boolean isSarSensorEnabled)171     private void createSarManager(boolean isSarEnabled, boolean isSarSapEnabled,
172             boolean isSarSensorEnabled) {
173         mResources.setBoolean(
174                 R.bool.config_wifi_framework_enable_sar_tx_power_limit, isSarEnabled);
175         mResources.setBoolean(
176                 R.bool.config_wifi_framework_enable_soft_ap_sar_tx_power_limit,
177                 isSarSapEnabled);
178         mResources.setBoolean(
179                 R.bool.config_wifi_framework_enable_body_proximity_sar_tx_power_limit,
180                 isSarSensorEnabled);
181         mResources.setString(R.string.config_wifi_sar_sensor_type, SAR_SENSOR_NAME);
182 
183         /* Set the event id configs */
184         mResources.setInteger(R.integer.config_wifi_framework_sar_free_space_event_id,
185                 SAR_SENSOR_EVENT_FREE_SPACE);
186         mResources.setInteger(R.integer.config_wifi_framework_sar_near_hand_event_id,
187                 SAR_SENSOR_EVENT_HAND);
188         mResources.setInteger(R.integer.config_wifi_framework_sar_near_head_event_id,
189                 SAR_SENSOR_EVENT_HEAD);
190         mResources.setInteger(R.integer.config_wifi_framework_sar_near_body_event_id,
191                 SAR_SENSOR_EVENT_BODY);
192 
193         /* Prepare sensor info only if SarSensorEnabled */
194         if (isSarSensorEnabled) {
195             prepareSensorInfo(true);
196         }
197 
198         mSarMgr = new SarManager(mContext, mTelephonyManager, mLooper.getLooper(),
199                 mWifiNative, mSensorManager, mWifiMetrics);
200 
201         if (isSarEnabled) {
202             /* Capture the PhoneStateListener */
203             ArgumentCaptor<PhoneStateListener> phoneStateListenerCaptor =
204                     ArgumentCaptor.forClass(PhoneStateListener.class);
205             verify(mTelephonyManager).listen(phoneStateListenerCaptor.capture(),
206                     eq(PhoneStateListener.LISTEN_CALL_STATE));
207             mPhoneStateListener = phoneStateListenerCaptor.getValue();
208             assertNotNull(mPhoneStateListener);
209         }
210 
211         if (isSarSensorEnabled) {
212             /* Capture the SensorEventListener */
213             ArgumentCaptor<SensorEventListener> sensorEventListenerCaptor =
214                     ArgumentCaptor.forClass(SensorEventListener.class);
215             verify(mSensorManager).registerListener(sensorEventListenerCaptor.capture(),
216                     any(Sensor.class), anyInt());
217             mSensorEventListener = sensorEventListenerCaptor.getValue();
218             assertNotNull(mSensorEventListener);
219         }
220 
221         verify(mWifiMetrics, never()).incrementNumSarSensorRegistrationFailures();
222 
223         /* Enable logs from SarManager */
224         enableDebugLogs();
225     }
226 
227     /**
228      * Helper function to create SarManager with some error cases for sensor handling
229      */
createSarManagerSensorNegTest(String configSensorName, boolean addToConfigs, boolean sensorRegisterReturn)230     private void createSarManagerSensorNegTest(String configSensorName, boolean addToConfigs,
231             boolean sensorRegisterReturn) {
232         mResources.setBoolean(
233                 R.bool.config_wifi_framework_enable_sar_tx_power_limit, true);
234         mResources.setBoolean(
235                 R.bool.config_wifi_framework_enable_soft_ap_sar_tx_power_limit, true);
236         mResources.setBoolean(
237                 R.bool.config_wifi_framework_enable_body_proximity_sar_tx_power_limit, true);
238         if (addToConfigs) {
239             mResources.setString(R.string.config_wifi_sar_sensor_type, configSensorName);
240         }
241 
242         /* Set the event id configs */
243         mResources.setInteger(R.integer.config_wifi_framework_sar_free_space_event_id,
244                 SAR_SENSOR_EVENT_FREE_SPACE);
245         mResources.setInteger(R.integer.config_wifi_framework_sar_near_hand_event_id,
246                 SAR_SENSOR_EVENT_HAND);
247         mResources.setInteger(R.integer.config_wifi_framework_sar_near_head_event_id,
248                 SAR_SENSOR_EVENT_HEAD);
249         mResources.setInteger(R.integer.config_wifi_framework_sar_near_body_event_id,
250                 SAR_SENSOR_EVENT_BODY);
251 
252         prepareSensorInfo(sensorRegisterReturn);
253 
254         mSarMgr = new SarManager(mContext, mTelephonyManager, mLooper.getLooper(),
255                 mWifiNative, mSensorManager, mWifiMetrics);
256 
257         /* Capture the PhoneStateListener */
258         ArgumentCaptor<PhoneStateListener> phoneStateListenerCaptor =
259                 ArgumentCaptor.forClass(PhoneStateListener.class);
260         verify(mTelephonyManager).listen(phoneStateListenerCaptor.capture(),
261                 eq(PhoneStateListener.LISTEN_CALL_STATE));
262         mPhoneStateListener = phoneStateListenerCaptor.getValue();
263         assertNotNull(mPhoneStateListener);
264 
265         /* Enable logs from SarManager */
266         enableDebugLogs();
267     }
268 
269     /**
270      * Helper function to create and pass a sensor event
271      */
sendSensorEvent(int eventId)272     private void sendSensorEvent(int eventId) {
273         SensorEvent event;
274         Constructor<SensorEvent> constructor =
275                 (Constructor<SensorEvent>) SensorEvent.class.getDeclaredConstructors()[0];
276         constructor.setAccessible(true);
277 
278         try {
279             event = constructor.newInstance(1);
280             event.values[0] = (float) eventId;
281             mSensorEventListener.onSensorChanged(event);
282         } catch (Exception e) {
283             fail("Failed to create a Sensor Event");
284         }
285     }
286 
287     /**
288      * Test that we do register the telephony call state listener on devices which do support
289      * setting/resetting Tx power limit.
290      */
291     @Test
testSarMgr_enabledTxPowerScenario_registerPhone()292     public void testSarMgr_enabledTxPowerScenario_registerPhone() throws Exception {
293         createSarManager(true, false, false);
294         verify(mTelephonyManager).listen(any(), eq(PhoneStateListener.LISTEN_CALL_STATE));
295     }
296 
297     /**
298      * Test that we do not register the telephony call state listener on devices which
299      * do not support setting/resetting Tx power limit.
300      */
301     @Test
testSarMgr_disabledTxPowerScenario_registerPhone()302     public void testSarMgr_disabledTxPowerScenario_registerPhone() throws Exception {
303         createSarManager(false, false, false);
304         verify(mTelephonyManager, never()).listen(any(), anyInt());
305     }
306 
307     /**
308      * Test that for devices that support setting/resetting Tx Power limits, device sets the proper
309      * Tx power scenario upon receiving {@link TelephonyManager#CALL_STATE_OFFHOOK} when WiFi STA
310      * is enabled
311      * In this case Wifi is enabled first, then off-hook is detected
312      */
313     @Test
testSarMgr_enabledTxPowerScenario_wifiOn_offHook()314     public void testSarMgr_enabledTxPowerScenario_wifiOn_offHook() throws Exception {
315         createSarManager(true, false, false);
316 
317         InOrder inOrder = inOrder(mWifiNative);
318 
319         /* Enable WiFi State */
320         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
321         captureSarInfo(mWifiNative);
322 
323         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
324         assertFalse(mSarInfo.isVoiceCall);
325 
326         /* Set phone state to OFFHOOK */
327         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
328         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
329         assertTrue(mSarInfo.isVoiceCall);
330     }
331 
332     /**
333      * Test that for devices that support setting/resetting Tx Power limits, device sets the proper
334      * Tx power scenario upon receiving {@link TelephonyManager#CALL_STATE_OFFHOOK} when WiFi STA
335      * is enabled
336      * In this case off-hook event is detected first, then wifi is turned on
337      */
338     @Test
testSarMgr_enabledTxPowerScenario_offHook_wifiOn()339     public void testSarMgr_enabledTxPowerScenario_offHook_wifiOn() throws Exception {
340         createSarManager(true, false, false);
341 
342         InOrder inOrder = inOrder(mWifiNative);
343 
344         /* Set phone state to OFFHOOK */
345         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
346 
347         /* Enable WiFi State */
348         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
349         captureSarInfo(mWifiNative);
350 
351         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
352         assertTrue(mSarInfo.isVoiceCall);
353     }
354 
355     /**
356      * Test that for devices that support setting/resetting Tx Power limits, device sets the proper
357      * Tx power scenarios upon receiving {@link TelephonyManager#CALL_STATE_OFFHOOK} and
358      * {@link TelephonyManager#CALL_STATE_IDLE} when WiFi STA is enabled
359      */
360     @Test
testSarMgr_enabledTxPowerScenario_wifiOn_offHook_onHook()361     public void testSarMgr_enabledTxPowerScenario_wifiOn_offHook_onHook() throws Exception {
362         createSarManager(true, false, false);
363 
364         InOrder inOrder = inOrder(mWifiNative);
365 
366         /* Enable WiFi State */
367         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
368         captureSarInfo(mWifiNative);
369 
370         /* Now device should set tx power scenario to NORMAL */
371         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
372         assertFalse(mSarInfo.isVoiceCall);
373 
374         /* Set phone state to OFFHOOK */
375         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
376 
377         /* Device should set tx power scenario to Voice call */
378         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
379         assertTrue(mSarInfo.isVoiceCall);
380 
381         /* Set state back to ONHOOK */
382         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
383 
384         /* Device should set tx power scenario to NORMAL again */
385         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
386         assertFalse(mSarInfo.isVoiceCall);
387 
388         /* Disable WiFi State */
389         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_DISABLED);
390         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
391     }
392 
393     /**
394      * Test that for devices that support setting/resetting Tx Power limits, device does not
395      * sets the Tx power scenarios upon receiving {@link TelephonyManager#CALL_STATE_OFFHOOK} and
396      * {@link TelephonyManager#CALL_STATE_IDLE} when WiFi STA is disabled
397      */
398     @Test
testSarMgr_enabledTxPowerScenario_wifiOff_offHook_onHook()399     public void testSarMgr_enabledTxPowerScenario_wifiOff_offHook_onHook() throws Exception {
400         createSarManager(true, false, false);
401 
402         InOrder inOrder = inOrder(mWifiNative);
403 
404         /* Set phone state to OFFHOOK */
405         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
406 
407         /* Set state back to ONHOOK */
408         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
409 
410         /* Device should not set tx power scenario at all */
411         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
412     }
413 
414     /**
415      * Test that for devices supporting SAR the following scenario:
416      * - Wifi enabled
417      * - A call starts
418      * - Wifi disabled
419      * - Call ends
420      * - Wifi back on
421      */
422     @Test
testSarMgr_enabledSar_wifiOn_offHook_wifiOff_onHook()423     public void testSarMgr_enabledSar_wifiOn_offHook_wifiOff_onHook() throws Exception {
424         createSarManager(true, false, false);
425 
426         InOrder inOrder = inOrder(mWifiNative);
427 
428         /* Enable WiFi State */
429         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
430         captureSarInfo(mWifiNative);
431 
432         /* Now device should set tx power scenario to NORMAL */
433         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
434         assertFalse(mSarInfo.isVoiceCall);
435 
436         /* Set phone state to OFFHOOK */
437         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
438         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
439         assertTrue(mSarInfo.isVoiceCall);
440 
441         /* Disable WiFi State */
442         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_DISABLED);
443         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
444 
445         /* Set state back to ONHOOK */
446         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
447         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
448 
449         /* Enable WiFi State again */
450         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
451         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
452         assertFalse(mSarInfo.isVoiceCall);
453     }
454 
455     /**
456      * Test that for devices supporting SAR, Wifi disabled, a call starts, a call ends, Wifi
457      * enabled.
458      */
459     @Test
testSarMgr_enabledSar_wifiOff_offHook_onHook_wifiOn()460     public void testSarMgr_enabledSar_wifiOff_offHook_onHook_wifiOn() throws Exception {
461         createSarManager(true, false, false);
462 
463         InOrder inOrder = inOrder(mWifiNative);
464 
465         /* Set phone state to OFFHOOK */
466         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
467         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
468 
469         /* Set state back to ONHOOK */
470         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
471         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
472 
473         /* Enable WiFi State */
474         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
475         captureSarInfo(mWifiNative);
476 
477         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
478         assertFalse(mSarInfo.isVoiceCall);
479     }
480 
481     /**
482      * Test that for devices supporting SAR, Wifi disabled, a call starts, wifi on, wifi off,
483      * call ends.
484      */
485     @Test
testSarMgr_enabledSar_offHook_wifiOnOff_onHook()486     public void testSarMgr_enabledSar_offHook_wifiOnOff_onHook() throws Exception {
487         createSarManager(true, false, false);
488 
489         InOrder inOrder = inOrder(mWifiNative);
490 
491         /* Set phone state to OFFHOOK */
492         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
493         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
494 
495         /* Enable WiFi State */
496         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
497         captureSarInfo(mWifiNative);
498         assertNotNull(mSarInfo);
499 
500         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
501         assertTrue(mSarInfo.isVoiceCall);
502 
503         /* Disable WiFi State */
504         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_DISABLED);
505         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
506 
507         /* Set state back to ONHOOK */
508         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
509         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
510     }
511 
512     /**
513      * Test the error case for for devices supporting SAR, Wifi enabled, a call starts,
514      * call ends. With all of these cases, the call to set Tx power scenario fails.
515      */
516     @Test
testSarMgr_enabledSar_error_wifiOn_offOnHook()517     public void testSarMgr_enabledSar_error_wifiOn_offOnHook() throws Exception {
518         createSarManager(true, false, false);
519 
520         when(mWifiNative.selectTxPowerScenario(any(SarInfo.class))).thenReturn(false);
521         InOrder inOrder = inOrder(mWifiNative);
522 
523         /* Enable WiFi State */
524         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
525         captureSarInfo(mWifiNative);
526         assertNotNull(mSarInfo);
527 
528         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
529         assertFalse(mSarInfo.isVoiceCall);
530 
531         /* Set phone state to OFFHOOK */
532         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
533         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
534         assertTrue(mSarInfo.isVoiceCall);
535 
536         /* Set state back to ONHOOK */
537         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
538         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
539         assertFalse(mSarInfo.isVoiceCall);
540     }
541 
542     /**
543      * Test that for a device that has SAR enabled, with sar sensor enabled,
544      * wifi enabled, Then Tx power scenarios follow events from sensor for body/hand/head/none
545      */
546     @Test
testSarMgr_sarSensorOn_WifiOn_sensorEventsTriggered()547     public void testSarMgr_sarSensorOn_WifiOn_sensorEventsTriggered() throws Exception {
548         createSarManager(true, true, true);
549 
550         InOrder inOrder = inOrder(mWifiNative);
551 
552         /* Enable Wifi Client */
553         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
554         captureSarInfo(mWifiNative);
555 
556         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
557         assertEquals(SarInfo.SAR_SENSOR_FREE_SPACE, mSarInfo.sensorState);
558 
559         /* Sensor event */
560         sendSensorEvent(SAR_SENSOR_EVENT_BODY);
561         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
562         assertEquals(SarInfo.SAR_SENSOR_NEAR_BODY, mSarInfo.sensorState);
563         assertFalse(mSarInfo.isVoiceCall);
564 
565         /* Sensor event */
566         sendSensorEvent(SAR_SENSOR_EVENT_HEAD);
567         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
568         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
569         assertFalse(mSarInfo.isVoiceCall);
570 
571         /* Sensor event */
572         sendSensorEvent(SAR_SENSOR_EVENT_HAND);
573         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
574         assertEquals(SarInfo.SAR_SENSOR_NEAR_HAND, mSarInfo.sensorState);
575         assertFalse(mSarInfo.isVoiceCall);
576 
577         /* Sensor event */
578         sendSensorEvent(SAR_SENSOR_EVENT_FREE_SPACE);
579         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
580         assertEquals(SarInfo.SAR_SENSOR_FREE_SPACE, mSarInfo.sensorState);
581         assertFalse(mSarInfo.isVoiceCall);
582     }
583 
584     /**
585      * Test that for a device that has SAR enabled, with sar sensor enabled,
586      * wifi enabled, cellOn,
587      * then Tx power scenarios follow events from sensor for body/hand/head/none
588      */
589     @Test
testSarMgr_sarSensorOn_wifiOn_cellOn_sensorEventsTriggered()590     public void testSarMgr_sarSensorOn_wifiOn_cellOn_sensorEventsTriggered() throws Exception {
591         createSarManager(true, true, true);
592 
593         InOrder inOrder = inOrder(mWifiNative);
594 
595         /* Enable Wifi Client */
596         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
597         captureSarInfo(mWifiNative);
598 
599         /* Should get the an event with no calls */
600         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
601         assertEquals(SarInfo.SAR_SENSOR_FREE_SPACE, mSarInfo.sensorState);
602         assertFalse(mSarInfo.isVoiceCall);
603 
604         /* Start a Cell call */
605         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
606         inOrder.verify(mWifiNative).selectTxPowerScenario(any(SarInfo.class));
607         assertEquals(SarInfo.SAR_SENSOR_FREE_SPACE, mSarInfo.sensorState);
608         assertTrue(mSarInfo.isVoiceCall);
609 
610         /* Sensor event */
611         sendSensorEvent(SAR_SENSOR_EVENT_BODY);
612         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
613         assertEquals(SarInfo.SAR_SENSOR_NEAR_BODY, mSarInfo.sensorState);
614         assertTrue(mSarInfo.isVoiceCall);
615 
616         /* Sensor event */
617         sendSensorEvent(SAR_SENSOR_EVENT_HEAD);
618         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
619         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
620         assertTrue(mSarInfo.isVoiceCall);
621 
622         /* Sensor event */
623         sendSensorEvent(SAR_SENSOR_EVENT_HAND);
624         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
625         assertEquals(SarInfo.SAR_SENSOR_NEAR_HAND, mSarInfo.sensorState);
626         assertTrue(mSarInfo.isVoiceCall);
627 
628         /* Sensor event */
629         sendSensorEvent(SAR_SENSOR_EVENT_FREE_SPACE);
630         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
631         assertEquals(SarInfo.SAR_SENSOR_FREE_SPACE, mSarInfo.sensorState);
632         assertTrue(mSarInfo.isVoiceCall);
633     }
634 
635     /**
636      * Test that for a device that has SAR enabled, with sar sensor enabled,
637      * wifi enabled, device next to user head, a call has started and stopped,
638      * then Tx power scenarios should adjust properly
639      */
640     @Test
testSarMgr_sarSensorOn_wifiOn_onHead_cellOnOff()641     public void testSarMgr_sarSensorOn_wifiOn_onHead_cellOnOff() throws Exception {
642         createSarManager(true, true, true);
643 
644         InOrder inOrder = inOrder(mWifiNative);
645 
646         /* Enable Wifi Client */
647         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
648         captureSarInfo(mWifiNative);
649 
650         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
651         assertEquals(SarInfo.SAR_SENSOR_FREE_SPACE, mSarInfo.sensorState);
652         assertFalse(mSarInfo.isVoiceCall);
653 
654         /* Sensor event */
655         sendSensorEvent(SAR_SENSOR_EVENT_HEAD);
656         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
657         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
658         assertFalse(mSarInfo.isVoiceCall);
659 
660 
661         /* Start a Cell call */
662         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
663         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
664         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
665         assertTrue(mSarInfo.isVoiceCall);
666 
667         /* End a Cell call */
668         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
669         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
670         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
671         assertFalse(mSarInfo.isVoiceCall);
672     }
673 
674     /**
675      * Test that for a device that has SAR enabled, with sar sensor enabled,
676      * all wifi states disabled, when a sensor event is triggered no setting of Tx power scenario
677      * is initiated.
678      * Then when Wifi is enabled, Tx power setting will be initiated to reflect the sensor event.
679      */
680     @Test
testSarMgr_sarSensorOn_WifiOffOn_sensorEventTriggered()681     public void testSarMgr_sarSensorOn_WifiOffOn_sensorEventTriggered() throws Exception {
682         createSarManager(true, true, true);
683 
684         InOrder inOrder = inOrder(mWifiNative);
685 
686         /* Sensor event */
687         sendSensorEvent(SAR_SENSOR_EVENT_BODY);
688         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
689 
690         /* Enable Wifi Client */
691         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
692         captureSarInfo(mWifiNative);
693 
694         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
695         assertEquals(SarInfo.SAR_SENSOR_NEAR_BODY, mSarInfo.sensorState);
696         assertFalse(mSarInfo.isVoiceCall);
697     }
698 
699     /**
700      * Test the error case when SAR sensor name does not exist in configuration.
701      * In this case, SarManager should assume operation near head all the time.
702      */
703     @Test
testSarMgr_error_sar_name_does_not_exist()704     public void testSarMgr_error_sar_name_does_not_exist() throws Exception {
705         createSarManagerSensorNegTest(SAR_SENSOR_NAME, false, true);
706 
707         InOrder inOrder = inOrder(mWifiNative);
708 
709         verify(mSensorManager, never()).registerListener(any(SensorEventListener.class),
710                 any(Sensor.class), anyInt());
711         verify(mWifiMetrics).incrementNumSarSensorRegistrationFailures();
712 
713         /* Enable WiFi Client */
714         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
715         captureSarInfo(mWifiNative);
716 
717         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
718         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
719         assertFalse(mSarInfo.isVoiceCall);
720 
721         /* Start a Cell Call */
722         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
723         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
724         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
725         assertTrue(mSarInfo.isVoiceCall);
726 
727         /* End the call */
728         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
729         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
730         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
731         assertFalse(mSarInfo.isVoiceCall);
732     }
733 
734     /**
735      * Test the error case when SarManager uses the wrong sensor name in configuration.
736      * In this case, SarManager should assume operation near head all the time.
737      */
738     @Test
testSarMgr_error_sar_name_mismatch()739     public void testSarMgr_error_sar_name_mismatch() throws Exception {
740         createSarManagerSensorNegTest("wrong.sensor.name", true, true);
741 
742         InOrder inOrder = inOrder(mWifiNative);
743 
744         verify(mSensorManager, never()).registerListener(any(SensorEventListener.class),
745                 any(Sensor.class), anyInt());
746         verify(mWifiMetrics).incrementNumSarSensorRegistrationFailures();
747 
748         /* Enable WiFi Client */
749         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
750         captureSarInfo(mWifiNative);
751 
752         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
753         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
754         assertFalse(mSarInfo.isVoiceCall);
755 
756         /* Start a Cell Call */
757         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
758         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
759         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
760         assertTrue(mSarInfo.isVoiceCall);
761 
762         /* End the call */
763         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
764         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
765         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
766         assertFalse(mSarInfo.isVoiceCall);
767     }
768 
769     /**
770      * Test the error case when SarManager fails to register as a SensorEventListener.
771      * In this case, SarManager should assume operation near head all the time.
772      */
773     @Test
testSarMgr_error_sar_register_failure()774     public void testSarMgr_error_sar_register_failure() throws Exception {
775         createSarManagerSensorNegTest(SAR_SENSOR_NAME, true, false);
776 
777         verify(mSensorManager).registerListener(any(SensorEventListener.class),
778                 any(Sensor.class), anyInt());
779         verify(mWifiMetrics).incrementNumSarSensorRegistrationFailures();
780 
781         InOrder inOrder = inOrder(mWifiNative);
782 
783         /* Enable WiFi Client */
784         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
785         captureSarInfo(mWifiNative);
786 
787         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
788         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
789         assertFalse(mSarInfo.isVoiceCall);
790 
791         /* Start a Cell Call */
792         mPhoneStateListener.onCallStateChanged(CALL_STATE_OFFHOOK, "");
793         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
794         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
795         assertTrue(mSarInfo.isVoiceCall);
796 
797         /* End the call */
798         mPhoneStateListener.onCallStateChanged(CALL_STATE_IDLE, "");
799         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
800         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
801         assertFalse(mSarInfo.isVoiceCall);
802     }
803 
804     /**
805      * Test that Start of SoftAP for a device that does not have SAR enabled does not result in
806      * setting the Tx power scenario
807      */
808     @Test
testSarMgr_disabledTxPowerScenario_sapOn()809     public void testSarMgr_disabledTxPowerScenario_sapOn() throws Exception {
810         createSarManager(false, false, false);
811 
812         /* Enable WiFi SoftAP State */
813         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_ENABLED);
814 
815         verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
816     }
817 
818     /**
819      * Test that Start of SoftAP for a device that has SAR enabled, SAR sensor disabled.
820      */
821     @Test
testSarMgr_enabledTxPowerScenario_sapOn()822     public void testSarMgr_enabledTxPowerScenario_sapOn() throws Exception {
823         createSarManager(true, false, false);
824 
825         InOrder inOrder = inOrder(mWifiNative);
826 
827         /* Enable WiFi SoftAP State */
828         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_ENABLED);
829         captureSarInfo(mWifiNative);
830 
831         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
832         assertFalse(mSarInfo.isVoiceCall);
833         assertTrue(mSarInfo.isWifiSapEnabled);
834         assertFalse(mSarInfo.isWifiScanOnlyEnabled);
835     }
836 
837     /**
838      * Test that for a device that has SAR enabled, SAR sensor enabled, near head, and when
839      * wifi sta is enabled, turning on sap then turning it off.
840      */
841     @Test
testSarMgr_enabledTxPowerScenario_staOn_sapOnOff()842     public void testSarMgr_enabledTxPowerScenario_staOn_sapOnOff() throws Exception {
843         createSarManager(true, true, true);
844 
845         InOrder inOrder = inOrder(mWifiNative);
846 
847         /* Sensor event */
848         sendSensorEvent(SAR_SENSOR_EVENT_HEAD);
849         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
850 
851         /* Enable WiFi Client State */
852         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
853         captureSarInfo(mWifiNative);
854 
855         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
856         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
857         assertFalse(mSarInfo.isWifiSapEnabled);
858 
859         /* Enable WiFi SoftAP State */
860         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_ENABLED);
861         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
862         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
863         assertTrue(mSarInfo.isWifiSapEnabled);
864 
865         /* Disable Wifi SoftAP state */
866         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_DISABLED);
867         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
868         assertEquals(SarInfo.SAR_SENSOR_NEAR_HEAD, mSarInfo.sensorState);
869         assertFalse(mSarInfo.isWifiSapEnabled);
870     }
871 
872     /**
873      * Test that for a device that has SAR enabled, SAR sensor enabled, Near body, and when
874      * disabling wifi softAP while Wifi Sta is also disabled, no update to the HAL for Tx
875      * power scenario is issued.
876      * Then, when wifi client is enabled, the Tx Power scenario is set.
877      * This is to verify that no call to update tx power when all wifi modes are disabled.
878      */
879     @Test
testSarMgr_enabledTxPowerScenario_sapOnOff_staOffOn()880     public void testSarMgr_enabledTxPowerScenario_sapOnOff_staOffOn() throws Exception {
881         createSarManager(true, true, true);
882 
883         InOrder inOrder = inOrder(mWifiNative);
884 
885         /* Sensor event */
886         sendSensorEvent(SAR_SENSOR_EVENT_BODY);
887         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
888 
889         /* Enable WiFi softAP State */
890         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_ENABLED);
891         captureSarInfo(mWifiNative);
892 
893         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
894         assertEquals(SarInfo.SAR_SENSOR_NEAR_BODY, mSarInfo.sensorState);
895         assertTrue(mSarInfo.isWifiSapEnabled);
896 
897         /* Disable Wifi SoftAP state */
898         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_DISABLED);
899         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
900 
901         /* Enable WiFi Clinet State */
902         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
903         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
904         assertEquals(SarInfo.SAR_SENSOR_NEAR_BODY, mSarInfo.sensorState);
905         assertFalse(mSarInfo.isWifiSapEnabled);
906     }
907 
908     /**
909      * Test that for a device that has SAR enabled, when scan-only state is enabled with both SoftAP
910      * and Client states disabled, the SarInfo is reported with proper values.
911      */
912     @Test
testSarMgr_enabledTxPowerScenario_staOff_sapOff_scanOnlyOn()913     public void testSarMgr_enabledTxPowerScenario_staOff_sapOff_scanOnlyOn() throws Exception {
914         createSarManager(true, false, false);
915 
916         /* Enable Wifi ScanOnly State */
917         mSarMgr.setScanOnlyWifiState(WifiManager.WIFI_STATE_ENABLED);
918         captureSarInfo(mWifiNative);
919 
920         verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
921         assertFalse(mSarInfo.isWifiSapEnabled);
922         assertTrue(mSarInfo.isWifiScanOnlyEnabled);
923     }
924 
925     /**
926      * Test that for a device that has SAR enabled, when scan-only state is enabled, and then
927      * client state is enabled, no additional setting of Tx power scenario is initiated
928      */
929     @Test
testSarMgr_enabledTxPowerScenario_staOn_sapOff_scanOnlyOn()930     public void testSarMgr_enabledTxPowerScenario_staOn_sapOff_scanOnlyOn() throws Exception {
931         createSarManager(true, false, false);
932 
933         InOrder inOrder = inOrder(mWifiNative);
934 
935         /* Enable Wifi ScanOnly State */
936         mSarMgr.setScanOnlyWifiState(WifiManager.WIFI_STATE_ENABLED);
937         captureSarInfo(mWifiNative);
938 
939         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
940         assertFalse(mSarInfo.isWifiSapEnabled);
941         assertTrue(mSarInfo.isWifiScanOnlyEnabled);
942 
943         /* Now enable Client state */
944         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
945         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
946     }
947 
948     /**
949      * Test the success case for for devices supporting SAR, with no SAR sensor support,
950      * Wifi enabled, SoftAP enabled, wifi disabled, scan-only enabled, SoftAP disabled.
951      *
952      * SarManager should report these changes as they occur(only when changes occur to
953      * inputs affecting the SAR scenario).
954      */
955     @Test
testSarMgr_enabledTxPowerScenario_wifi_sap_scanOnly()956     public void testSarMgr_enabledTxPowerScenario_wifi_sap_scanOnly() throws Exception {
957         createSarManager(true, false, false);
958 
959         InOrder inOrder = inOrder(mWifiNative);
960 
961         /* Enable WiFi State */
962         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
963         captureSarInfo(mWifiNative);
964 
965         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
966         assertFalse(mSarInfo.isVoiceCall);
967         assertFalse(mSarInfo.isWifiSapEnabled);
968         assertFalse(mSarInfo.isWifiScanOnlyEnabled);
969 
970         /* Enable SoftAP state */
971         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_ENABLED);
972         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
973         assertFalse(mSarInfo.isVoiceCall);
974         assertTrue(mSarInfo.isWifiSapEnabled);
975         assertFalse(mSarInfo.isWifiScanOnlyEnabled);
976 
977         /* Disable WiFi State */
978         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_DISABLED);
979         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
980 
981         /* Enable ScanOnly state */
982         mSarMgr.setScanOnlyWifiState(WifiManager.WIFI_STATE_ENABLED);
983         inOrder.verify(mWifiNative, never()).selectTxPowerScenario(any(SarInfo.class));
984 
985         /* Disable SoftAP state */
986         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_DISABLED);
987         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
988         assertFalse(mSarInfo.isVoiceCall);
989         assertFalse(mSarInfo.isWifiSapEnabled);
990         assertTrue(mSarInfo.isWifiScanOnlyEnabled);
991     }
992 
993     /**
994      * Test the error case for devices supporting SAR, with no SAR sensor support,
995      * Wifi enabled, SoftAP enabled, wifi disabled, scan-only enabled, SoftAP disabled
996      * Throughout this test case, calls to the hal return with error.
997      */
998     @Test
testSarMgr_enabledTxPowerScenario_error_wifi_sap_scanOnly()999     public void testSarMgr_enabledTxPowerScenario_error_wifi_sap_scanOnly() throws Exception {
1000         createSarManager(true, false, false);
1001 
1002         when(mWifiNative.selectTxPowerScenario(any(SarInfo.class))).thenReturn(false);
1003         InOrder inOrder = inOrder(mWifiNative);
1004 
1005         /* Enable WiFi State */
1006         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_ENABLED);
1007         captureSarInfo(mWifiNative);
1008 
1009         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
1010         assertFalse(mSarInfo.isVoiceCall);
1011         assertFalse(mSarInfo.isWifiSapEnabled);
1012         assertFalse(mSarInfo.isWifiScanOnlyEnabled);
1013 
1014         /* Enable SoftAP state */
1015         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_ENABLED);
1016         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
1017         assertFalse(mSarInfo.isVoiceCall);
1018         assertTrue(mSarInfo.isWifiSapEnabled);
1019         assertFalse(mSarInfo.isWifiScanOnlyEnabled);
1020 
1021         /* Disable WiFi State, reporting should still happen */
1022         mSarMgr.setClientWifiState(WifiManager.WIFI_STATE_DISABLED);
1023         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
1024         assertFalse(mSarInfo.isVoiceCall);
1025         assertTrue(mSarInfo.isWifiSapEnabled);
1026         assertFalse(mSarInfo.isWifiScanOnlyEnabled);
1027         assertFalse(mSarInfo.isWifiClientEnabled);
1028 
1029         /* Enable ScanOnly state */
1030         mSarMgr.setScanOnlyWifiState(WifiManager.WIFI_STATE_ENABLED);
1031         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
1032         assertFalse(mSarInfo.isVoiceCall);
1033         assertTrue(mSarInfo.isWifiSapEnabled);
1034         assertTrue(mSarInfo.isWifiScanOnlyEnabled);
1035         assertFalse(mSarInfo.isWifiClientEnabled);
1036 
1037         /* Disable SoftAP state */
1038         mSarMgr.setSapWifiState(WifiManager.WIFI_AP_STATE_DISABLED);
1039         inOrder.verify(mWifiNative).selectTxPowerScenario(eq(mSarInfo));
1040         assertFalse(mSarInfo.isVoiceCall);
1041         assertFalse(mSarInfo.isWifiSapEnabled);
1042         assertTrue(mSarInfo.isWifiScanOnlyEnabled);
1043         assertFalse(mSarInfo.isWifiClientEnabled);
1044     }
1045 }
1046