1"""Local hotspot tests."""
2
3import time
4
5
6
7from mobly import asserts
8from mobly import test_runner
9from utils import android_base_test
10
11SSID = 'SSID'
12# Number of seconds for the receiver to restore wifi state.
13RESTORE_TIME = 20
14# The time period that SSID needs to be found.
15SCAN_TIMEOUT = 30
16
17
18class 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
70if __name__ == '__main__':
71  test_runner.main()
72