• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2#   Copyright 2014 - 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
16import time
17
18import acts.base_test
19import acts.test_utils.wifi.wifi_test_utils as wutils
20
21from acts import asserts
22
23WifiEnums = wutils.WifiEnums
24WifiEventNames = wutils.WifiEventNames
25
26class WifiPnoTest(acts.base_test.BaseTestClass):
27
28    def __init__(self, controllers):
29        acts.base_test.BaseTestClass.__init__(self, controllers)
30        self.tests = (
31            "test_simple_pno_connection",
32            "test_pno_connection_with_multiple_saved_networks",
33        )
34
35    def setup_class(self):
36        self.dut = self.android_devices[0]
37        wutils.wifi_test_device_init(self.dut)
38        req_params = (
39            "attn_vals",
40            "pno_network_a",
41            "pno_network_b",
42            "pno_interval"
43        )
44        self.unpack_userparams(req_params)
45        self.attn_a = self.attenuators[0]
46        self.attn_b = self.attenuators[1]
47        self.set_attns("default")
48
49    def setup_test(self):
50        self.dut.droid.wifiStartTrackingStateChange()
51        self.dut.droid.wakeLockAcquireBright()
52        self.dut.droid.wakeUpNow()
53        wutils.reset_wifi(self.dut)
54        self.dut.ed.clear_all_events()
55
56    def teardown_test(self):
57        self.dut.droid.wifiStopTrackingStateChange()
58        self.dut.droid.wakeLockRelease()
59        self.dut.droid.goToSleepNow()
60        wutils.reset_wifi(self.dut)
61        self.dut.ed.clear_all_events()
62        self.set_attns("default")
63
64    def on_fail(self, test_name, begin_time):
65        self.dut.take_bug_report(test_name, begin_time)
66        self.dut.cat_adb_log(test_name, begin_time)
67
68    """Helper Functions"""
69    def set_attns(self, attn_val_name):
70        """Sets attenuation values on attenuators used in this test.
71
72        Args:
73            attn_val_name: Name of the attenuation value pair to use.
74        """
75        msg = "Set attenuation values to %s" % self.attn_vals[attn_val_name]
76        self.log.info(msg)
77        try:
78            self.attn_a.set_atten(self.attn_vals[attn_val_name][0])
79            self.attn_b.set_atten(self.attn_vals[attn_val_name][1])
80        except:
81            msg = "Failed to set attenuation values %s." % attn_val_name
82            self.log.error(msg)
83            raise
84
85    def trigger_pno_and_assert_connect(self, attn_val_name, expected_con):
86        """Sets attenuators to disconnect current connection and power-off the
87        screen to trigger PNO. Validate that the DUT connected to the new SSID
88        as expected after PNO.
89
90        Args:
91            attn_val_name: Name of the attenuation value pair to use.
92            expected_con: The expected info of the network to we expect the DUT
93                to roam to.
94        """
95        connection_info = self.dut.droid.wifiGetConnectionInfo()
96        self.log.info("Triggering PNO connect from %s to %s" %
97            (connection_info[WifiEnums.SSID_KEY],
98                expected_con[WifiEnums.SSID_KEY]))
99        self.dut.droid.goToSleepNow()
100        self.set_attns(attn_val_name)
101        self.log.info("Wait %ss for PNO to trigger." % self.pno_interval)
102        time.sleep(self.pno_interval)
103        try:
104            self.dut.droid.wakeLockAcquireBright()
105            self.dut.droid.wakeUpNow()
106            expected_ssid = expected_con[WifiEnums.SSID_KEY]
107            verify_con = { WifiEnums.SSID_KEY : expected_ssid }
108            wutils.verify_wifi_connection_info(self.dut, verify_con)
109            self.log.info("Connected to %s successfully after PNO" %
110                expected_ssid)
111        finally:
112            self.dut.droid.wifiLockRelease()
113            self.dut.droid.goToSleepNow()
114
115    def add_dummy_networks(self, num_networks):
116        """Add some dummy networks to the device.
117
118        Args:
119            num_networks: Number of networks to add.
120        """
121        ssid_name_base = "pno_dummy_network_"
122        for i in range(0, num_networks) :
123            network = {}
124            network[WifiEnums.SSID_KEY] = ssid_name_base + str(i)
125            network[WifiEnums.PWD_KEY] = "pno_dummy";
126            asserts.assert_true(self.dut.droid.wifiAddNetwork(network) != -1,
127                "Add network %r failed" % network)
128
129    """ Tests Begin """
130    def test_simple_pno_connection(self):
131        """Test PNO triggered autoconnect to a network.
132
133        Steps:
134        1. Save 2 valid network configurations (a & b) in the device.
135        2. Attenuate network b.
136        3. Connect the device to network a.
137        4. Switch off the screen on the device.
138        5. Attenuate network a and remove attenuation on network b and wait for
139           a few seconds to trigger PNO.
140        6. Check the device connected to network b automatically.
141        7. Switch off the screen on the device.
142        8. Attenuate network b and remove attenuation on network a and wait for
143           a few seconds to trigger PNO.
144        9. Check the device connected to network a automatically.
145        """
146        asserts.assert_true(
147            self.dut.droid.wifiAddNetwork(self.pno_network_a) != -1,
148            "Add network %r failed" % self.pno_network_a)
149        asserts.assert_true(
150            self.dut.droid.wifiAddNetwork(self.pno_network_b) != -1,
151            "Add network %r failed" % self.pno_network_b)
152        self.set_attns("a_on_b_off")
153        wutils.wifi_connect(self.dut, self.pno_network_a),
154        self.trigger_pno_and_assert_connect("b_on_a_off", self.pno_network_b)
155        self.trigger_pno_and_assert_connect("a_on_b_off", self.pno_network_a)
156
157    def test_pno_connection_with_multiple_saved_networks(self):
158        """Test PNO triggered autoconnect to a network when there are more
159        than 16 networks saved in the device.
160
161        16 is the max list size of PNO watch list for most devices. The device
162        should automatically pick the 16 latest added networks in the list.
163        So add 16 dummy networks and then add 2 valid networks.
164
165        Steps:
166        1. Save 16 dummy network configurations in the device.
167        2. Run the simple pno test.
168        """
169        self.add_dummy_networks(16)
170        self.test_simple_pno_connection()
171    """ Tests End """
172