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
17import time
18
19from acts import asserts
20from acts import utils
21from acts.test_decorators import test_tracker_info
22from acts.test_utils.wifi import wifi_test_utils as wutils
23from acts.test_utils.wifi.aware import aware_const as aconsts
24from acts.test_utils.wifi.aware import aware_test_utils as autils
25from acts.test_utils.wifi.aware.AwareBaseTest import AwareBaseTest
26
27
28class AttachTest(AwareBaseTest):
29  def __init__(self, controllers):
30    AwareBaseTest.__init__(self, controllers)
31
32  @test_tracker_info(uuid="cdafd1e0-bcf5-4fe8-ae32-f55483db9925")
33  def test_attach(self):
34    """Functional test case / Attach test cases / attach
35
36    Validates that attaching to the Wi-Fi Aware service works (receive
37    the expected callback).
38    """
39    dut = self.android_devices[0]
40    dut.droid.wifiAwareAttach(False)
41    autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED)
42    autils.fail_on_event(dut, aconsts.EVENT_CB_ON_IDENTITY_CHANGED)
43
44  @test_tracker_info(uuid="82f2a8bc-a62b-49c2-ac8a-fe8460010ba2")
45  def test_attach_with_identity(self):
46    """Functional test case / Attach test cases / attach with identity callback
47
48    Validates that attaching to the Wi-Fi Aware service works (receive
49    the expected callbacks).
50    """
51    dut = self.android_devices[0]
52    dut.droid.wifiAwareAttach(True)
53    autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED)
54    autils.wait_for_event(dut, aconsts.EVENT_CB_ON_IDENTITY_CHANGED)
55
56  @test_tracker_info(uuid="d2714d14-f330-47d4-b8e9-ee4d5e5b7ea0")
57  def test_attach_multiple_sessions(self):
58    """Functional test case / Attach test cases / multiple attach sessions
59
60    Validates that when creating multiple attach sessions each can be
61    configured independently as to whether or not to receive an identity
62    callback.
63    """
64    dut = self.android_devices[0]
65
66    # Create 3 attach sessions: 2 without identity callback, 1 with
67    id1 = dut.droid.wifiAwareAttach(False, None, True)
68    time.sleep(10) # to make sure all calls and callbacks are done
69    id2 = dut.droid.wifiAwareAttach(True, None, True)
70    time.sleep(10) # to make sure all calls and callbacks are done
71    id3 = dut.droid.wifiAwareAttach(False, None, True)
72    dut.log.info('id1=%d, id2=%d, id3=%d', id1, id2, id3)
73
74    # Attach session 1: wait for attach, should not get identity
75    autils.wait_for_event(dut,
76                          autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED,
77                                                id1))
78    autils.fail_on_event(dut,
79                         autils.decorate_event(
80                             aconsts.EVENT_CB_ON_IDENTITY_CHANGED, id1))
81
82    # Attach session 2: wait for attach and for identity callback
83    autils.wait_for_event(dut,
84                          autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED,
85                                                id2))
86    autils.wait_for_event(dut,
87                          autils.decorate_event(
88                              aconsts.EVENT_CB_ON_IDENTITY_CHANGED, id2))
89
90    # Attach session 3: wait for attach, should not get identity
91    autils.wait_for_event(dut,
92                          autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED,
93                                                id3))
94    autils.fail_on_event(dut,
95                         autils.decorate_event(
96                             aconsts.EVENT_CB_ON_IDENTITY_CHANGED, id3))
97
98  @test_tracker_info(uuid="b8ea4d02-ae23-42a7-a85e-def52932c858")
99  def test_attach_with_no_wifi(self):
100    """Function test case / Attach test cases / attempt to attach with wifi off
101
102    Validates that if trying to attach with Wi-Fi disabled will receive the
103    expected failure callback. As a side-effect also validates that the
104    broadcast for Aware unavailable is received.
105    """
106    dut = self.android_devices[0]
107    wutils.wifi_toggle_state(dut, False)
108    autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_NOT_AVAILABLE)
109    dut.droid.wifiAwareAttach()
110    autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACH_FAILED)
111
112  @test_tracker_info(uuid="7dcc4530-c936-4447-9d22-a7c5b315e2ce")
113  def test_attach_with_doze(self):
114    """Function test case / Attach test cases / attempt to attach with doze on
115
116    Validates that if trying to attach with device in doze mode will receive the
117    expected failure callback. As a side-effect also validates that the
118    broadcast for Aware unavailable is received.
119    """
120    dut = self.android_devices[0]
121    asserts.assert_true(utils.enable_doze(dut), "Can't enable doze")
122    autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_NOT_AVAILABLE)
123    dut.droid.wifiAwareAttach()
124    autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACH_FAILED)
125    asserts.assert_true(utils.disable_doze(dut), "Can't disable doze")
126    autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_AVAILABLE)
127
128  @test_tracker_info(uuid="2574fd01-8974-4dd0-aeb8-a7194461140e")
129  def test_attach_with_location_off(self):
130    """Function test case / Attach test cases / attempt to attach with location
131    mode off.
132
133    Validates that if trying to attach with device location mode off will
134    receive the expected failure callback. As a side-effect also validates that
135    the broadcast for Aware unavailable is received.
136    """
137    dut = self.android_devices[0]
138    utils.set_location_service(dut, False)
139    autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_NOT_AVAILABLE)
140    dut.droid.wifiAwareAttach()
141    autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACH_FAILED)
142    utils.set_location_service(dut, True)
143    autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_AVAILABLE)
144
145  @test_tracker_info(uuid="7ffde8e7-a010-4b77-97f5-959f263b5249")
146  def test_attach_apm_toggle_attach_again(self):
147    """Validates that enabling Airplane mode while Aware is on resets it
148    correctly, and allows it to be re-enabled when Airplane mode is then
149    disabled."""
150    dut = self.android_devices[0]
151
152    # enable Aware (attach)
153    dut.droid.wifiAwareAttach()
154    autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED)
155
156    # enable airplane mode
157    utils.force_airplane_mode(dut, True)
158    autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_NOT_AVAILABLE)
159
160    # wait a few seconds and disable airplane mode
161    time.sleep(10)
162    utils.force_airplane_mode(dut, False)
163    autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_AVAILABLE)
164
165    # try enabling Aware again (attach)
166    dut.droid.wifiAwareAttach()
167    autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED)
168