WENTAO WANG | b4da3a5 | 2017-11-02 14:37:56 -0700 | [diff] [blame] | 1 | """Local hotspot tests.""" |
| 2 | |
| 3 | import time |
| 4 | |
| 5 | |
| 6 | |
| 7 | from mobly import asserts |
Yuexi Ma | b4da9e0 | 2017-12-22 02:51:25 -0800 | [diff] [blame^] | 8 | from mobly import test_runner |
WENTAO WANG | b4da3a5 | 2017-11-02 14:37:56 -0700 | [diff] [blame] | 9 | from utils import android_base_test |
| 10 | |
| 11 | SSID = 'SSID' |
| 12 | # Number of seconds for the receiver to restore wifi state. |
| 13 | RESTORE_TIME = 20 |
| 14 | # The time period that SSID needs to be found. |
| 15 | SCAN_TIMEOUT = 30 |
| 16 | |
| 17 | |
| 18 | class LocalHotspotTest(android_base_test.AndroidBaseTest): |
| 19 | """Local hotspot tests.""" |
| 20 | |
| 21 | def setup_class(self): |
| 22 | super(LocalHotspotTest, self).setup_class() |
| 23 | self.station = self.dut_a |
| 24 | self.ap = self.dut_b |
| 25 | # The device used to discover Bluetooth devices and send messages. |
| 26 | # Sets the tag that represents this device in logs. |
| 27 | self.station.debug_tag = 'station' |
| 28 | # The device that is expected to be discovered and receive messages. |
| 29 | self.ap.debug_tag = 'ap' |
| 30 | |
| 31 | def setup_test(self): |
| 32 | # Make sure wifi is on. |
| 33 | self.station.android.wifiEnable() |
| 34 | self.ap.android.wifiEnable() |
| 35 | |
| 36 | def test_local_hotspot_process(self): |
| 37 | """Test for basic local hotspot process flow. |
| 38 | |
| 39 | Steps: |
| 40 | 1. Ap sets up a local hotspot and retrieves the credentials. |
| 41 | 2. Station connects to the hotspot. |
| 42 | |
| 43 | Verifies: |
| 44 | Station can connect to the local hotspot created by ap. |
| 45 | """ |
| 46 | wifi_on_before = self.ap.android.wifiIsEnabled() |
| 47 | start_localhotspot_callback = self.ap.android.startLocalHotspot() |
| 48 | start_result = start_localhotspot_callback.waitAndGet('onStarted', 30) |
| 49 | local_hotspot_info = start_result.data |
| 50 | self.ap.log.info('Local hotspot started') |
| 51 | network_found = self.station.android.wifiScanAndFindSsid( |
| 52 | local_hotspot_info[SSID], SCAN_TIMEOUT) |
| 53 | asserts.assert_true(network_found, 'Network is not found within 30 seconds') |
| 54 | self.station.android.wifiConnectByUpdate(local_hotspot_info[SSID], |
| 55 | local_hotspot_info['Password']) |
| 56 | self.station.log.info('Connected to the network %s.' |
| 57 | % local_hotspot_info[SSID]) |
| 58 | self.ap.android.stopLocalHotspot() |
| 59 | time.sleep(RESTORE_TIME) |
| 60 | wifi_on_after = self.ap.android.wifiIsEnabled() |
| 61 | asserts.assert_equal(wifi_on_before, wifi_on_after) |
| 62 | self.ap.log.info('Wifi state restored') |
| 63 | |
| 64 | def teardown_test(self): |
| 65 | # Turn wifi off on both devices after test finishes. |
| 66 | self.station.android.wifiDisable() |
| 67 | self.ap.android.wifiDisable() |
| 68 | |
| 69 | |
| 70 | if __name__ == '__main__': |
Yuexi Ma | b4da9e0 | 2017-12-22 02:51:25 -0800 | [diff] [blame^] | 71 | test_runner.main() |