1#!/usr/bin/env python3.5 2# 3# Copyright 2019 - 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. 16import time 17from collections import namedtuple 18 19from acts import asserts 20from acts import signals 21from acts import utils 22from acts.base_test import BaseTestClass 23from acts_contrib.test_utils.gnss import gnss_test_utils as gutils 24from acts_contrib.test_utils.wifi import wifi_test_utils as wutils 25 26BACKGROUND_LOCATION_PERMISSION = 'android.permission.ACCESS_BACKGROUND_LOCATION' 27APP_CLEAN_UP_TIME = 10 28 29class LocationPlatinumTest(BaseTestClass): 30 """Location Platinum Tests""" 31 32 def setup_class(self): 33 super().setup_class() 34 self.ad = self.android_devices[0] 35 req_params = [ 36 # A { SSID, password } dictionary. Password is optional. 37 'wifi_network', 38 # A [latitude, longitude] list to identify test location. 39 'test_location', 40 # Cold Start Criteria, a int to define the criteria. 41 'cs_criteria', 42 # Warm Start Criteria, a int to define the criteria. 43 'ws_criteria', 44 # Hot Start Criteria, a int to define the criteria. 45 'hs_criteria', 46 # NetworkLocationProvide Criteria, a int to define the criteria. 47 'nlp_criteria' 48 ] 49 self.unpack_userparams(req_param_names=req_params) 50 51 # Init test types Cold Start, Warm Start and Hot Start. 52 test_type = namedtuple('Type', ['command', 'criteria']) 53 self.test_types = { 54 'cs': test_type('Cold Start', self.cs_criteria), 55 'ws': test_type('Warm Start', self.ws_criteria), 56 'hs': test_type('Hot Start', self.hs_criteria) 57 } 58 self._init(self.ad) 59 60 def _init(self, ad): 61 gutils.enable_gnss_verbose_logging(ad) 62 if gutils.check_chipset_vendor_by_qualcomm(ad): 63 gutils.disable_xtra_throttle(ad) 64 65 def setup_test(self): 66 """Prepare device with mobile data, wifi and gps ready for test """ 67 gutils.check_location_service(self.ad) 68 if not self.ad.droid.wifiCheckState(): 69 wutils.wifi_toggle_state(self.ad, True) 70 gutils.connect_to_wifi_network(self.ad, self.wifi_network) 71 72 def get_and_verify_ttff(self, mode): 73 """Retrieve ttff with designate mode. 74 75 Args: 76 mode: A string for identify gnss test mode. 77 """ 78 if mode in self.test_types: 79 test_type = self.test_types.get(mode) 80 else: 81 raise signals.TestFailure('Unrecognized mode %s' % mode) 82 83 gutils.process_gnss_by_gtw_gpstool(self.ad, 84 self.test_types['cs'].criteria) 85 begin_time = gutils.get_current_epoch_time() 86 gutils.start_ttff_by_gtw_gpstool( 87 self.ad, ttff_mode=mode, iteration=1, aid_data=True) 88 ttff_data = gutils.process_ttff_by_gtw_gpstool(self.ad, begin_time, 89 self.test_location) 90 result = gutils.check_ttff_data( 91 self.ad, 92 ttff_data, 93 ttff_mode=test_type.command, 94 criteria=test_type.criteria) 95 asserts.assert_true( 96 result, 97 '%s TTFF fails to reach designated criteria' % test_type.command) 98 99 # Test cases 100 def test_gnss_cs_ttff(self): 101 """ 102 1. Send intent to GPSTool for cold start test. 103 2. Retrieve ttff and validate with target criteria. 104 """ 105 self.get_and_verify_ttff('cs') 106 107 def test_gnss_ws_ttff(self): 108 """ 109 1. Send intent to GPSTool for warm start test. 110 2. Retrieve ttff and validate with target criteria. 111 """ 112 self.get_and_verify_ttff('ws') 113 114 def test_gnss_hs_ttff(self): 115 """ 116 1. Send intent to GPSTool for hot start test. 117 2. Retrieve ttff and validate with target criteria. 118 """ 119 self.get_and_verify_ttff('hs') 120 121 def test_nlp_by_wifi(self): 122 """ 123 1. Disable mobile data. 124 2. Send intent to GPSTool for NLP. 125 3. Retrieve response time and validate with criteria. 126 """ 127 gutils.set_mobile_data(self.ad, False) 128 asserts.assert_true( 129 gutils.check_network_location( 130 self.ad, 1, 'wifi', self.nlp_criteria), 131 'Fail to get NLP from wifi') 132 133 def test_nlp_by_cell(self): 134 """ 135 1. Disable wifi. 136 2. Send intent to GPSTool for NLP. 137 3. Retrieve response time and validate with criteria. 138 """ 139 wutils.wifi_toggle_state(self.ad, False) 140 asserts.assert_true( 141 gutils.check_network_location( 142 self.ad, 1, 'cell', self.nlp_criteria), 143 'Fail to get NLP from cell') 144 145 def test_toggle_location_setting_off_on(self): 146 """ 147 1. Toggle location setting off on. 148 2. Open Google Map and ask for location. 149 3. Validate there are location fix in logcat. 150 """ 151 self.ad.adb.shell('settings put secure location_mode 0') 152 self.ad.adb.shell('settings put secure location_mode 3') 153 gutils.launch_google_map(self.ad) 154 asserts.assert_true( 155 gutils.check_location_api(self.ad, retries=1), 156 'DUT failed to receive location fix') 157 158 def test_location_setting_off(self): 159 """ 160 1. Toggle location setting off. 161 2. Open Google Map and ask for location. 162 3. Validate there is no location fix in logcat. 163 """ 164 self.ad.adb.shell('settings put secure location_mode 0') 165 gutils.launch_google_map(self.ad) 166 asserts.assert_false( 167 gutils.check_location_api(self.ad, retries=1), 168 'DUT Still receive location fix') 169 170 def test_toggle_location_permission_off_on(self): 171 """ 172 1. Toggle Google Map location permission off on. 173 2. Open Google Map and ask for location. 174 3. Validate there are location fix in logcat. 175 """ 176 gutils.grant_location_permission(self.ad, False) 177 gutils.grant_location_permission(self.ad, True) 178 gutils.launch_google_map(self.ad) 179 asserts.assert_true( 180 gutils.check_location_api(self.ad, retries=1), 181 'DUT fail to receive location fix') 182 183 def test_location_permission_off(self): 184 """ 185 1. Toggle Google Map location permission off. 186 2. Open Google Map and ask for location. 187 3. Validate there is no location fix in logcat. 188 """ 189 gutils.grant_location_permission(self.ad, False) 190 gutils.launch_google_map(self.ad) 191 asserts.assert_false( 192 gutils.check_location_api(self.ad, retries=1), 193 'DUT still receive location fix') 194 195 def test_location_only_in_use_state(self): 196 """ 197 1. Revoke ACCESS_BACKGROUBND_LOCATION permission on GPSTool. 198 2. Open GPSTool for tracking. 199 3. Validate there are location fix in logcat. 200 4. Turn GPSTool from foreground to background by press home. 201 5. Wait 60 seconds for app clean up. 202 6. Validate GPSTool is skipping for location update in logcat. 203 """ 204 self.ad.log.info('Revoke background permission') 205 self.ad.adb.shell('pm revoke com.android.gpstool %s' % 206 BACKGROUND_LOCATION_PERMISSION) 207 gutils.start_gnss_by_gtw_gpstool(self.ad, True) 208 asserts.assert_true( 209 gutils.check_location_api(self.ad, retries=1), 210 'APP failed to receive location fix in foreground') 211 self.ad.log.info('Trun GPSTool from foreground to background') 212 self.ad.adb.shell('input keyevent 3') 213 self.ad.log.info('Wait %d seconds for app clean up' % APP_CLEAN_UP_TIME) 214 time.sleep(APP_CLEAN_UP_TIME) 215 asserts.assert_false( 216 gutils.check_location_api(self.ad, retries=1), 217 'DUT still receive location fix') 218