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 setup_class(self):
34        super().setup_class()
35        if "AccessPoint" in self.user_params:
36            self.legacy_configure_ap_and_start()
37
38    def run_disable_rtt(self, disable_mode):
39        """Validate the RTT disabled flows: whether by disabling Wi-Fi or entering
40    doze mode.
41
42    Args:
43      disable_mode: The particular mechanism in which RTT is disabled. One of
44                    the MODE_* constants.
45    """
46        dut = self.android_devices[0]
47
48        # validate start-up conditions
49        asserts.assert_true(dut.droid.wifiIsRttAvailable(),
50                            "RTT is not available")
51
52        # scan to get some APs to be used later
53        all_aps = rutils.select_best_scan_results(
54            rutils.scan_networks(dut), 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(
62                utils.force_airplane_mode(dut, True),
63                "Can not turn on airplane mode on: %s" % dut.serial)
64        elif disable_mode == self.MODE_ENABLE_DOZE:
65            asserts.assert_true(utils.enable_doze(dut), "Can't enable doze")
66        elif disable_mode == self.MODE_DISABLE_LOCATIONING:
67            utils.set_location_service(dut, False)
68
69        rutils.wait_for_event(dut, rconsts.BROADCAST_WIFI_RTT_NOT_AVAILABLE)
70        asserts.assert_false(dut.droid.wifiIsRttAvailable(),
71                             "RTT is available")
72
73        # request a range and validate error
74        id = dut.droid.wifiRttStartRangingToAccessPoints(all_aps[0:1])
75        event = rutils.wait_for_event(
76            dut, rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_FAIL, id))
77        asserts.assert_equal(
78            event["data"][rconsts.EVENT_CB_RANGING_KEY_STATUS],
79            rconsts.RANGING_FAIL_CODE_RTT_NOT_AVAILABLE, "Invalid error code")
80
81        # enable RTT and validate broadcast & API
82        if disable_mode == self.MODE_DISABLE_WIFI:
83            asserts.assert_true(
84                utils.force_airplane_mode(dut, False),
85                "Can not turn off airplane mode on: %s" % dut.serial)
86        elif disable_mode == self.MODE_ENABLE_DOZE:
87            asserts.assert_true(utils.disable_doze(dut), "Can't disable doze")
88        elif disable_mode == self.MODE_DISABLE_LOCATIONING:
89            utils.set_location_service(dut, True)
90
91        rutils.wait_for_event(dut, rconsts.BROADCAST_WIFI_RTT_AVAILABLE)
92        asserts.assert_true(dut.droid.wifiIsRttAvailable(),
93                            "RTT is not available")
94
95    ############################################################################
96
97    @test_tracker_info(uuid="498c49ab-a188-4612-998d-c47b35ff285e")
98    def test_disable_wifi(self):
99        """Validate that getting expected broadcast when Wi-Fi is disabled and that
100    any range requests are rejected."""
101        self.run_disable_rtt(self.MODE_DISABLE_WIFI)
102
103    @test_tracker_info(uuid="f71f731f-4aaf-402b-8595-db94b625b544")
104    def test_enable_doze(self):
105        """Validate that getting expected broadcast when RTT is disabled due to doze
106    mode and that any range requests are rejected."""
107        self.run_disable_rtt(self.MODE_ENABLE_DOZE)
108
109    @test_tracker_info(uuid="6a1c83a8-9eaf-49db-b547-5131cba0eafe")
110    def test_disable_location(self):
111        """Validate that getting expected broadcast when locationing is disabled and
112    that any range requests are rejected."""
113        self.run_disable_rtt(self.MODE_DISABLE_LOCATIONING)
114