1#!/usr/bin/python3.4
2#
3#   Copyright 2017 - 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
17from acts import asserts
18from acts import utils
19from acts.test_decorators import test_tracker_info
20from acts.test_utils.wifi.rtt import rtt_const as rconsts
21from acts.test_utils.wifi.rtt import rtt_test_utils as rutils
22from acts.test_utils.wifi.rtt.RttBaseTest import RttBaseTest
23from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest
24
25
26class RttDisableTest(WifiBaseTest, RttBaseTest):
27  """Test class for RTT ranging enable/disable flows."""
28
29  MODE_DISABLE_WIFI = 0
30  MODE_ENABLE_DOZE = 1
31  MODE_DISABLE_LOCATIONING = 2
32
33  def __init__(self, controllers):
34    WifiBaseTest.__init__(self, controllers)
35    RttBaseTest.__init__(self, controllers)
36    if "AccessPoint" in self.user_params:
37      self.legacy_configure_ap_and_start()
38
39  def run_disable_rtt(self, disable_mode):
40    """Validate the RTT disabled flows: whether by disabling Wi-Fi or entering
41    doze mode.
42
43    Args:
44      disable_mode: The particular mechanism in which RTT is disabled. One of
45                    the MODE_* constants.
46    """
47    dut = self.android_devices[0]
48
49    # validate start-up conditions
50    asserts.assert_true(dut.droid.wifiIsRttAvailable(), "RTT is not available")
51
52    # scan to get some APs to be used later
53    all_aps = rutils.select_best_scan_results(rutils.scan_networks(dut),
54                                              select_count=1)
55    asserts.assert_true(len(all_aps) > 0, "Need at least one visible AP!")
56
57    # disable RTT and validate broadcast & API
58    if disable_mode == self.MODE_DISABLE_WIFI:
59      # disabling Wi-Fi is not sufficient: since scan mode (and hence RTT) will
60      # remain enabled - we need to disable the Wi-Fi chip aka Airplane Mode
61      asserts.assert_true(utils.force_airplane_mode(dut, True),
62                          "Can not turn on airplane mode on: %s" % dut.serial)
63    elif disable_mode == self.MODE_ENABLE_DOZE:
64      asserts.assert_true(utils.enable_doze(dut), "Can't enable doze")
65    elif disable_mode == self.MODE_DISABLE_LOCATIONING:
66      utils.set_location_service(dut, False)
67
68    rutils.wait_for_event(dut, rconsts.BROADCAST_WIFI_RTT_NOT_AVAILABLE)
69    asserts.assert_false(dut.droid.wifiIsRttAvailable(), "RTT is available")
70
71    # request a range and validate error
72    id = dut.droid.wifiRttStartRangingToAccessPoints(all_aps[0:1])
73    event = rutils.wait_for_event(dut, rutils.decorate_event(
74        rconsts.EVENT_CB_RANGING_ON_FAIL, id))
75    asserts.assert_equal(event["data"][rconsts.EVENT_CB_RANGING_KEY_STATUS],
76                         rconsts.RANGING_FAIL_CODE_RTT_NOT_AVAILABLE,
77                         "Invalid error code")
78
79    # enable RTT and validate broadcast & API
80    if disable_mode == self.MODE_DISABLE_WIFI:
81      asserts.assert_true(utils.force_airplane_mode(dut, False),
82                          "Can not turn off airplane mode on: %s" % dut.serial)
83    elif disable_mode == self.MODE_ENABLE_DOZE:
84      asserts.assert_true(utils.disable_doze(dut), "Can't disable doze")
85    elif disable_mode == self.MODE_DISABLE_LOCATIONING:
86      utils.set_location_service(dut, True)
87
88    rutils.wait_for_event(dut, rconsts.BROADCAST_WIFI_RTT_AVAILABLE)
89    asserts.assert_true(dut.droid.wifiIsRttAvailable(), "RTT is not available")
90
91  ############################################################################
92
93  @test_tracker_info(uuid="498c49ab-a188-4612-998d-c47b35ff285e")
94  def test_disable_wifi(self):
95    """Validate that getting expected broadcast when Wi-Fi is disabled and that
96    any range requests are rejected."""
97    self.run_disable_rtt(self.MODE_DISABLE_WIFI)
98
99  @test_tracker_info(uuid="f71f731f-4aaf-402b-8595-db94b625b544")
100  def test_enable_doze(self):
101    """Validate that getting expected broadcast when RTT is disabled due to doze
102    mode and that any range requests are rejected."""
103    self.run_disable_rtt(self.MODE_ENABLE_DOZE)
104
105  @test_tracker_info(uuid="6a1c83a8-9eaf-49db-b547-5131cba0eafe")
106  def test_disable_location(self):
107    """Validate that getting expected broadcast when locationing is disabled and
108    that any range requests are rejected."""
109    self.run_disable_rtt(self.MODE_DISABLE_LOCATIONING)
110