1
+ # Copyright (C) 2024 The Android Open Source Project
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # https://source.android.com/docs/core/tests/tradefed/testing/through-suite/multi-devices-suites?hl=zh-tw
16
+
17
+ # Lint as: python3
18
+ """ACTS Wi-Fi Aware Attached test reimplemented in Mobly."""
19
+ import sys
20
+
21
+ from aware import aware_lib_utils as autils
22
+ from aware import constants
23
+ from mobly import asserts
24
+ from mobly import base_test
25
+ from mobly import records
26
+ from mobly import test_runner
27
+ from mobly import utils
28
+ from mobly .controllers import android_device
29
+
30
+
31
+ RUNTIME_PERMISSIONS = (
32
+ 'android.permission.ACCESS_FINE_LOCATION' ,
33
+ 'android.permission.ACCESS_COARSE_LOCATION' ,
34
+ 'android.permission.NEARBY_WIFI_DEVICES' ,
35
+ )
36
+ PACKAGE_NAME = constants .WIFI_AWARE_SNIPPET_PACKAGE_NAME
37
+
38
+
39
+ class WifiAwareAttachTest (base_test .BaseTestClass ):
40
+ """Wi-Fi Aware Attach test class."""
41
+
42
+ ads : list [android_device .AndroidDevice ]
43
+
44
+ def setup_class (self ):
45
+ # Register two Android devices.
46
+ self .ads = self .register_controller (android_device , min_number = 1 )
47
+
48
+ def setup_device (device : android_device .AndroidDevice ):
49
+ device .load_snippet ('wifi_aware_snippet' , PACKAGE_NAME )
50
+ for permission in RUNTIME_PERMISSIONS :
51
+ device .adb .shell (['pm' , 'grant' , PACKAGE_NAME , permission ])
52
+ asserts .abort_all_if (
53
+ not device .wifi_aware_snippet .wifiAwareIsAvailable (),
54
+ f'{ device } Wi-Fi Aware is not available.' ,
55
+ )
56
+
57
+ # Set up devices in parallel.
58
+ utils .concurrent_exec (
59
+ setup_device ,
60
+ param_list = [[ad ] for ad in self .ads ],
61
+ max_workers = 1 ,
62
+ raise_on_exception = True ,
63
+ )
64
+
65
+ def setup_test (self ):
66
+ for ad in self .ads :
67
+ autils .control_wifi (ad , True )
68
+ aware_avail = ad .wifi_aware_snippet .wifiAwareIsAvailable ()
69
+ if not aware_avail :
70
+ ad .log .info ('Aware not available. Waiting ...' )
71
+ state_handler = ad .wifi_aware_snippet .wifiAwareMonitorStateChange ()
72
+ state_handler .waitAndGet (constants .WifiAwareBroadcast .WIFI_AWARE_AVAILABLE )
73
+
74
+ def teardown_test (self ):
75
+ utils .concurrent_exec (
76
+ self ._teardown_test_on_device ,
77
+ param_list = [[ad ] for ad in self .ads ],
78
+ max_workers = 1 ,
79
+ raise_on_exception = True ,
80
+ )
81
+ utils .concurrent_exec (
82
+ lambda d : d .services .create_output_excerpts_all (self .current_test_info ),
83
+ param_list = [[ad ] for ad in self .ads ],
84
+ raise_on_exception = True ,
85
+ )
86
+
87
+ def _teardown_test_on_device (self , ad : android_device .AndroidDevice ) -> None :
88
+ ad .wifi_aware_snippet .wifiAwareCloseAllWifiAwareSession ()
89
+ ad .wifi_aware_snippet .wifiAwareMonitorStopStateChange ()
90
+ autils .set_airplane_mode (ad , False )
91
+ autils .control_wifi (ad , True )
92
+
93
+ def on_fail (self , record : records .TestResult ) -> None :
94
+ android_device .take_bug_reports (
95
+ self .ads , destination = self .current_test_info .output_path
96
+ )
97
+
98
+ def test_attach (self ) -> None :
99
+ """Basic attaching request.
100
+
101
+ Validates that attaching to the Wi-Fi Aware service works
102
+ without IdentityChanged callback.
103
+ """
104
+ dut = self .ads [0 ]
105
+ handler = dut .wifi_aware_snippet .wifiAwareAttached (False )
106
+ handler .waitAndGet (constants .AttachCallBackMethodType .ATTACHED )
107
+ autils .callback_no_response (
108
+ handler , constants .AttachCallBackMethodType .ID_CHANGED
109
+ )
110
+
111
+ def test_attach_with_identity (self ) -> None :
112
+ """Basic attaching request with extra callback.
113
+
114
+ Validates that attaching to the Wi-Fi Aware service works
115
+ with IdentityChanged callback.
116
+ """
117
+ dut = self .ads [0 ]
118
+ handler = dut .wifi_aware_snippet .wifiAwareAttached (True )
119
+ handler .waitAndGet (constants .AttachCallBackMethodType .ATTACHED )
120
+ handler .waitAndGet (constants .AttachCallBackMethodType .ID_CHANGED )
121
+
122
+ def test_attach_multiple_sessions (self ):
123
+ """Multiple attaching request.
124
+
125
+ Validates that attaching to the Wi-Fi Aware service works with
126
+ multiple request at same time.
127
+ """
128
+ dut = self .ads [0 ]
129
+ handler_1 = dut .wifi_aware_snippet .wifiAwareAttached (False )
130
+ handler_2 = dut .wifi_aware_snippet .wifiAwareAttached (True )
131
+ handler_3 = dut .wifi_aware_snippet .wifiAwareAttached (False )
132
+ handler_1 .waitAndGet (constants .AttachCallBackMethodType .ATTACHED )
133
+ autils .callback_no_response (
134
+ handler_1 , constants .AttachCallBackMethodType .ID_CHANGED , 10 , True
135
+ )
136
+ handler_2 .waitAndGet (constants .AttachCallBackMethodType .ATTACHED )
137
+ handler_2 .waitAndGet (constants .AttachCallBackMethodType .ID_CHANGED )
138
+ handler_3 .waitAndGet (constants .AttachCallBackMethodType .ATTACHED )
139
+ autils .callback_no_response (
140
+ handler_3 , constants .AttachCallBackMethodType .ID_CHANGED , 10 , True
141
+ )
142
+
143
+ def test_attach_with_no_wifi (self ):
144
+ """WiFi Aware attempt to attach with wifi off.
145
+
146
+ Validates that if trying to attach with Wi-Fi disabled will receive
147
+ the expected failure callback. As a side-effect also validates that the
148
+ broadcast for Aware unavailable is received.
149
+ """
150
+ dut = self .ads [0 ]
151
+ state_handler = dut .wifi_aware_snippet .wifiAwareMonitorStateChange ()
152
+ autils .control_wifi (dut , False )
153
+ state_handler .waitAndGet (constants .WifiAwareBroadcast .WIFI_AWARE_NOT_AVAILABLE )
154
+ attach_callback = dut .wifi_aware_snippet .wifiAwareAttached (True )
155
+ attach_callback .waitAndGet (constants .AttachCallBackMethodType .ATTACH_FAILED )
156
+ dut .wifi_aware_snippet .wifiAwareMonitorStopStateChange ()
157
+
158
+ def test_attach_with_location_off (self ):
159
+ """Function/Attach test cases/attempt to attach with location mode off.
160
+
161
+ Validates that if trying to attach with device location mode off will
162
+ receive the expected failure callback. As a side-effect also validates
163
+ that the broadcast for Aware unavailable is received.
164
+ """
165
+ dut = self .ads [0 ]
166
+ asserts .skip_if (
167
+ autils .check_android_os_version (
168
+ dut , constants .Operator .GREATER_EQUAL , constants .AndroidVersion .T
169
+ ),
170
+ 'From T build Aware will not be disabled due to location off' ,
171
+ )
172
+ state_handler = dut .wifi_aware_snippet .wifiAwareMonitorStateChange ()
173
+ dut .adb .shell ('settings put secure location_mode 0' )
174
+ state_handler .waitAndGet (constants .WifiAwareBroadcast .WIFI_AWARE_NOT_AVAILABLE )
175
+ attach_callback = dut .wifi_aware_snippet .wifiAwareAttached (True )
176
+ attach_callback .waitAndGet (constants .AttachCallBackMethodType .ATTACH_FAILED )
177
+ dut .adb .shell ('settings put secure location_mode 3' )
178
+ state_handler .waitAndGet (constants .WifiAwareBroadcast .WIFI_AWARE_AVAILABLE )
179
+ dut .wifi_aware_snippet .wifiAwareMonitorStopStateChange ()
180
+
181
+ def test_attach_apm_toggle_attach_again (self ):
182
+ """Function/Attach test cases/attempt to attach with airplane mode on.
183
+
184
+ Validates that enabling Airplane mode while Aware is on resets it
185
+ correctly, and allows it to be re-enabled when Airplane mode is then
186
+ disabled.
187
+ """
188
+ dut = self .ads [0 ]
189
+ asserts .skip_if (
190
+ not dut .is_adb_root ,
191
+ 'APM toggle needs Android device(s) with root permission' ,
192
+ )
193
+ state_handler = dut .wifi_aware_snippet .wifiAwareMonitorStateChange ()
194
+ attach_callback = dut .wifi_aware_snippet .wifiAwareAttached (True )
195
+ attach_callback .waitAndGet (constants .AttachCallBackMethodType .ATTACHED )
196
+ autils .set_airplane_mode (dut , True )
197
+ if autils .check_android_os_version (
198
+ dut , constants .Operator .GREATER_EQUAL , constants .AndroidVersion .T
199
+ ):
200
+ if not autils ._check_wifi_status (dut ):
201
+ return
202
+ else :
203
+ autils .control_wifi (dut , False )
204
+ autils .callback_no_response (
205
+ state_handler ,
206
+ constants .WifiAwareBroadcast .WIFI_AWARE_AVAILABLE ,
207
+ 10 ,
208
+ True ,)
209
+ state_handler .waitAndGet (constants .WifiAwareBroadcast .WIFI_AWARE_NOT_AVAILABLE )
210
+ autils .set_airplane_mode (dut , False )
211
+ state_handler .waitAndGet (constants .WifiAwareBroadcast .WIFI_AWARE_AVAILABLE )
212
+ attach_callback = dut .wifi_aware_snippet .wifiAwareAttached (True )
213
+ attach_callback .waitAndGet (constants .AttachCallBackMethodType .ATTACHED )
214
+ dut .wifi_aware_snippet .wifiAwareMonitorStopStateChange ()
215
+
216
+
217
+ if __name__ == '__main__' :
218
+ # Take test args
219
+ if '--' in sys .argv :
220
+ index = sys .argv .index ('--' )
221
+ sys .argv = sys .argv [:1 ] + sys .argv [index + 1 :]
222
+
223
+ test_runner .main ()
0 commit comments