1#!/usr/bin/env python3.4
2#
3#   Copyright 2018 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the 'License');
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an 'AS IS' BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import time
18from acts.test_decorators import test_tracker_info
19from acts.test_utils.power import PowerWiFiBaseTest as PWBT
20from acts.test_utils.wifi import wifi_test_utils as wutils
21
22UNLOCK_SCREEN = 'input keyevent 82'
23LOCATION_ON = 'settings put secure location_mode 3'
24
25
26class PowerWiFiscanTest(PWBT.PowerWiFiBaseTest):
27    def setup_class(self):
28        super().setup_class()
29        # Setup scan command
30        SINGLE_SHOT_SCAN = (
31            'nohup am instrument -w -r  -e min_scan_count \"700\"'
32            ' -e WifiScanTest-testWifiSingleShotScan %d'
33            ' -e class com.google.android.platform.powertests.'
34            'WifiScanTest#testWifiSingleShotScan'
35            ' com.google.android.platform.powertests/'
36            'androidx.test.runner.AndroidJUnitRunner > /dev/null &' %
37            (self.mon_duration + self.mon_offset + 10))
38        self.APK_SCAN_CMDS = {
39            'singleshot': SINGLE_SHOT_SCAN
40        }
41
42    def setup_test(self):
43        super().setup_test()
44        # Reset attenuation to minimum
45        self.set_attenuation([0, 0, 0, 0])
46        # Turn on location for WiFi Scans
47        self.dut.adb.shell(LOCATION_ON)
48
49    def scan_setup(self):
50        """Setup for scan based on the type of scan.
51
52        Trigger the desired scan.
53        """
54        self.log.info('Trigger {} scans'.format(self.test_configs.scan_mode))
55        if self.test_configs.scan_type == 'apk':
56            atten_setting = self.test_configs.wifi_band + '_' + self.test_configs.rssi
57            self.set_attenuation(self.atten_level[atten_setting])
58            self.dut.adb.shell_nb(
59                self.APK_SCAN_CMDS[self.test_configs.scan_mode])
60        else:
61            self.mon_info.offset = 0
62            if self.test_configs.scan_mode == 'pno':
63                self.log.info('Set attenuation to trigger PNO scan')
64                self.set_attenuation(self.atten_level['max_atten'])
65            elif self.test_configs.scan_mode == 'connectivity':
66                self.dut.droid.wakeUpNow()
67                self.log.info(
68                    'Now turn on screen to trigger connectivity scans')
69                self.dut.adb.shell(UNLOCK_SCREEN)
70            elif self.test_configs.scan_mode == 'roaming':
71                atten_setting = self.test_configs.wifi_band + '_roaming'
72                self.set_attenuation(self.atten_level[atten_setting])
73
74    def wifi_scan_test_func(self):
75
76        attrs = [
77            'screen_status', 'wifi_status', 'wifi_band', 'rssi', 'scan_type',
78            'scan_mode'
79        ]
80        indices = [2, 4, 6, 8, 10, 11]
81        self.decode_test_configs(attrs, indices)
82        if self.test_configs.wifi_status == 'Disconnected':
83            self.setup_ap_connection(
84                self.main_network[self.test_configs.wifi_band], connect=False)
85        elif self.test_configs.wifi_status == 'Connected':
86            self.setup_ap_connection(
87                self.main_network[self.test_configs.wifi_band])
88        else:
89            wutils.wifi_toggle_state(self.dut, True)
90        if self.test_configs.screen_status == 'OFF':
91            self.dut.droid.goToSleepNow()
92            self.dut.log.info('Screen is OFF')
93        time.sleep(2)
94        self.scan_setup()
95        self.measure_power_and_validate()
96
97    # Test cases
98    # Power.apk triggered singleshot scans
99    @test_tracker_info(uuid='e5539b01-e208-43c6-bebf-6f1e73d8d8cb')
100    def test_screen_OFF_WiFi_Disconnected_band_2g_RSSI_high_scan_apk_singleshot(
101            self):
102        self.wifi_scan_test_func()
103
104    @test_tracker_info(uuid='14c5a762-95bc-40ea-9fd4-27126df7d86c')
105    def test_screen_OFF_WiFi_Disconnected_band_2g_RSSI_low_scan_apk_singleshot(
106            self):
107        self.wifi_scan_test_func()
108
109    @test_tracker_info(uuid='a6506600-c567-43b5-9c25-86b505099b97')
110    def test_screen_OFF_WiFi_Disconnected_band_2g_RSSI_none_scan_apk_singleshot(
111            self):
112        self.wifi_scan_test_func()
113
114    @test_tracker_info(uuid='1a458248-1159-4c8e-a39f-92fc9e69c4dd')
115    def test_screen_OFF_WiFi_Disconnected_band_5g_RSSI_high_scan_apk_singleshot(
116            self):
117        self.wifi_scan_test_func()
118
119    @test_tracker_info(uuid='bd4da426-a621-4131-9f89-6e5a77f321d2')
120    def test_screen_OFF_WiFi_Disconnected_band_5g_RSSI_low_scan_apk_singleshot(
121            self):
122        self.wifi_scan_test_func()
123
124    @test_tracker_info(uuid='288b3add-8925-4803-81c0-53debf157ffc')
125    def test_screen_OFF_WiFi_Disconnected_band_5g_RSSI_none_scan_apk_singleshot(
126            self):
127        self.wifi_scan_test_func()
128
129    # Firmware/framework scans
130    @test_tracker_info(uuid='ff5ea952-ee31-4968-a190-82935ce7a8cb')
131    def test_screen_OFF_WiFi_ON_band_5g_RSSI_high_scan_system_connectivity(
132            self):
133        """WiFi disconected, turn on Screen to trigger connectivity scans.
134
135        """
136        self.wifi_scan_test_func()
137
138    @test_tracker_info(uuid='9a836e5b-8128-4dd2-8e96-e79177810bdd')
139    def test_screen_OFF_WiFi_Connected_band_2g_RSSI_high_scan_system_connectivity(
140            self):
141        """WiFi connected to 2g, turn on screen to trigger connectivity scans.
142
143        """
144        self.wifi_scan_test_func()
145
146    @test_tracker_info(uuid='51e3c4f1-742b-45af-afd5-ae3552a03272')
147    def test_screen_OFF_WiFi_Connected_band_2g_RSSI_high_scan_system_roaming(
148            self):
149        """WiFi connected to 2g, low RSSI to be below roaming threshold.
150
151        """
152        self.wifi_scan_test_func()
153
154    @test_tracker_info(uuid='a16ae337-326f-4d09-990f-42232c3c0dc4')
155    def test_screen_OFF_WiFi_Connected_band_2g_RSSI_high_scan_system_pno(self):
156        """WiFi connected to 2g, trigger pno scan.
157
158        """
159        self.wifi_scan_test_func()
160