• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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#  Licensed under the Apache License, Version 2.0 (the "License");
16#  you may not use this file except in compliance with the License.
17#  You may obtain a copy of the License at
18#
19#       http://www.apache.org/licenses/LICENSE-2.0
20#
21#  Unless required by applicable law or agreed to in writing, software
22#  distributed under the License is distributed on an "AS IS" BASIS,
23#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24#  See the License for the specific language governing permissions and
25#  limitations under the License.
26
27# Lint as: python3
28"""CTS Tests that verify NFC HCE features.
29
30These tests require two phones, one acting as a card emulator and the other
31acting as an NFC reader. The two phones should be placed back to back.
32"""
33
34import sys
35import logging
36
37from mobly import asserts
38from mobly import base_test
39from mobly import test_runner
40from mobly import utils
41from mobly.controllers import android_device
42from mobly.snippet import errors
43
44# Timeout to give the NFC service time to perform async actions such as
45# discover tags.
46_NFC_TIMEOUT_SEC = 10
47_NFC_TECH_A_POLLING_ON = (0x1 #NfcAdapter.FLAG_READER_NFC_A
48                          | 0x10 #NfcAdapter.FLAG_READER_NFC_BARCODE
49                          | 0x80 #NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
50                          )
51_NFC_TECH_A_POLLING_OFF = (0x10 #NfcAdapter.FLAG_READER_NFC_BARCODE
52                           | 0x80 #NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
53                           )
54_NFC_TECH_A_LISTEN_ON = 0x1 #NfcAdapter.FLAG_LISTEN_NFC_PASSIVE_A
55_NFC_TECH_F_LISTEN_ON = 0x4 #NfcAdapter.FLAG_LISTEN_NFC_PASSIVE_F
56_NFC_LISTEN_OFF = 0x0 #NfcAdapter.FLAG_LISTEN_DISABLE
57
58class CtsNfcHceMultiDeviceTestCases(base_test.BaseTestClass):
59    def setup_class(self):
60        """
61        Sets up class by registering two devices, enabling nfc on them,
62        and loading snippets.
63        """
64        self.emulator, self.reader = self.register_controller(android_device,
65                                                              min_number=2)[:2]
66        self.reader.load_snippet('nfc_reader',
67                                 'com.android.nfc.reader')
68        self.emulator.load_snippet('nfc_emulator',
69                                   'com.android.nfc.emulator')
70
71        self.reader.adb.shell(['svc', 'nfc', 'enable'])
72        self.emulator.adb.shell(['svc', 'nfc', 'enable'])
73
74        self.reader.debug_tag = 'reader'
75        self.emulator.debug_tag = 'emulator'
76
77    def setup_test(self):
78        """
79        Turns emulator/reader screen on and unlocks between tests as some tests will
80        turn the screen off.
81        """
82        self.emulator.nfc_emulator.logInfo("*** TEST START: " + self.current_test_info.name + " ***")
83        self.reader.nfc_reader.logInfo("*** TEST START: " + self.current_test_info.name + " ***")
84        asserts.skip_if(
85            not self.emulator.nfc_emulator.isNfcSupported() or
86            not self.emulator.nfc_emulator.isNfcHceSupported(),
87            f"NFC is not supported on {self.emulator}",
88        )
89        asserts.skip_if(
90            not self.reader.nfc_reader.isNfcSupported(),
91            f"NFC is not supported on {self.reader}"
92        )
93
94        self.emulator.nfc_emulator.turnScreenOn()
95        self.emulator.nfc_emulator.pressMenu()
96        self.reader.nfc_reader.turnScreenOn()
97        self.reader.nfc_reader.pressMenu()
98
99    def on_fail(self, record):
100        test_name = record.test_name
101        self.emulator.take_bug_report(
102            test_name=self.emulator.debug_tag + "_" + test_name,
103            destination=self.current_test_info.output_path,
104        )
105        self.reader.take_bug_report(
106            test_name=self.reader.debug_tag + "_" + test_name,
107            destination=self.current_test_info.output_path,
108        )
109
110
111    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
112    def test_single_non_payment_service(self):
113        """Tests successful APDU exchange between non-payment service and
114        reader.
115
116        Test Steps:
117        1. Start emulator activity and set up non-payment HCE Service.
118        2. Set callback handler on emulator for when a TestPass event is
119        received.
120        3. Start reader activity, which should trigger APDU exchange between
121        reader and emulator.
122
123        Verifies:
124        1. Verifies a successful APDU exchange between the emulator and
125        Transport Service after
126        _NFC_TIMEOUT_SEC.
127        """
128        self.emulator.nfc_emulator.startSingleNonPaymentEmulatorActivity()
129
130        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
131            'ApduSuccess')
132        self.reader.nfc_reader.startSingleNonPaymentReaderActivity()
133        test_pass_event = test_pass_handler.waitAndGet('ApduSuccess',
134                                                       _NFC_TIMEOUT_SEC)
135
136        asserts.assert_is_not_none(test_pass_event,
137                                   'ApduSuccess event was not received.')
138
139    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2", "9.1/C-0-1"})
140    def test_single_payment_service(self):
141        """Tests successful APDU exchange between payment service and
142        reader.
143
144        Test Steps:
145        1. Set callback handler on emulator for when the instrumentation app is
146        set to default wallet app.
147        2. Start emulator activity and wait for the role to be set.
148        2. Set callback handler on emulator for when a TestPass event is
149        received.
150        3. Start reader activity, which should trigger APDU exchange between
151        reader and emulator.
152
153        Verifies:
154        1. Verifies emulator device sets the instrumentation emulator app to the
155        default wallet app.
156        2. Verifies a successful APDU exchange between the emulator and
157        Transport Service after _NFC_TIMEOUT_SEC.
158        """
159        # Wait for instrumentation app to hold onto wallet role before starting
160        # reader
161        role_held_handler = self.emulator.nfc_emulator.asyncWaitForRoleHeld(
162            'RoleHeld')
163        self.emulator.nfc_emulator.startSinglePaymentEmulatorActivity()
164        role_held_handler.waitAndGet('RoleHeld', _NFC_TIMEOUT_SEC)
165        self.emulator.nfc_emulator.waitForService()
166
167        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
168            'ApduSuccess')
169        self.reader.nfc_reader.startSinglePaymentReaderActivity()
170        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
171
172    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2", "9.1/C-0-1"})
173    def test_dual_payment_service(self):
174        """Tests successful APDU exchange between a payment service and
175        reader when two payment services are set up on the emulator.
176
177        Test Steps:
178        1. Set callback handler on emulator for when the instrumentation app is
179        set to default wallet app.
180        2. Start emulator activity and wait for the role to be set.
181        2. Set callback handler on emulator for when a TestPass event is
182        received.
183        3. Start reader activity, which should trigger APDU exchange between
184        reader and emulator.
185
186        Verifies:
187        1. Verifies a successful APDU exchange between the emulator and the
188        payment service.
189        """
190        role_held_handler = self.emulator.nfc_emulator.asyncWaitForRoleHeld(
191            'RoleHeld')
192        self.emulator.nfc_emulator.startDualPaymentEmulatorActivity()
193        role_held_handler.waitAndGet('RoleHeld', _NFC_TIMEOUT_SEC)
194        self.emulator.nfc_emulator.waitForService()
195
196        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
197            'ApduSuccess')
198        self.reader.nfc_reader.startDualPaymentReaderActivity()
199        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
200
201    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2", "9.1/C-0-1"})
202    def test_foreground_payment_emulator(self):
203        """Tests successful APDU exchange between non-default payment service and
204        reader when the foreground app sets a preference for the non-default
205        service.
206
207        Test Steps:
208        1. Set callback handler on emulator for when the instrumentation app is
209        set to default wallet app.
210        2. Start emulator activity and wait for the role to be set.
211        2. Set callback handler on emulator for when a TestPass event is
212        received.
213        3. Start reader activity, which should trigger APDU exchange between
214        reader and emulator.
215
216        Verifies:
217        1. Verifies a successful APDU exchange between the emulator and the
218        preferred service.
219        """
220        role_held_handler = self.emulator.nfc_emulator.asyncWaitForRoleHeld(
221            'RoleHeld')
222        self.emulator.nfc_emulator.startForegroundPaymentEmulatorActivity()
223        role_held_handler.waitAndGet('RoleHeld', _NFC_TIMEOUT_SEC)
224        self.emulator.nfc_emulator.waitForService()
225
226        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
227            'ApduSuccess')
228        self.reader.nfc_reader.startForegroundPaymentReaderActivity()
229        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
230
231    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
232    def test_dynamic_aid_emulator(self):
233        """Tests successful APDU exchange between payment service and reader
234        when the payment service has registered dynamic AIDs.
235
236        Test Steps:
237        1. Set callback handler on emulator for when the instrumentation app is
238        set to default wallet app.
239        2. Start emulator activity and wait for the role to be set.
240        2. Set callback handler on emulator for when a TestPass event is
241        received.
242        3. Start reader activity, which should trigger APDU exchange between
243        reader and emulator.
244
245        Verifies:
246        1. Verifies a successful APDU exchange between the emulator and the
247        payment service with dynamic AIDs.
248        """
249        role_held_handler = self.emulator.nfc_emulator.asyncWaitForRoleHeld(
250            'RoleHeld')
251        self.emulator.nfc_emulator.startDynamicAidEmulatorActivity()
252        role_held_handler.waitAndGet('RoleHeld', _NFC_TIMEOUT_SEC)
253        self.emulator.nfc_emulator.waitForService()
254
255        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
256            'ApduSuccess')
257        self.reader.nfc_reader.startDynamicAidReaderActivity()
258        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
259
260    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2", "9.1/C-0-1"})
261    def test_payment_prefix_emulator(self):
262        """Tests successful APDU exchange between payment service and reader
263        when the payment service has statically registered prefix AIDs.
264
265        Test Steps:
266        1. Set callback handler on emulator for when the instrumentation app is
267        set to default wallet app.
268        2. Start emulator activity and wait for the role to be set.
269        2. Set callback handler on emulator for when a TestPass event is
270        received.
271        3. Start reader activity, which should trigger APDU exchange between
272        reader and emulator.
273
274        Verifies:
275        1. Verifies a successful APDU exchange between the emulator and the
276        payment service with prefix AIDs.
277        """
278        role_held_handler = self.emulator.nfc_emulator.asyncWaitForRoleHeld(
279            'RoleHeld')
280        self.emulator.nfc_emulator.startPrefixPaymentEmulatorActivity()
281        role_held_handler.waitAndGet('RoleHeld', _NFC_TIMEOUT_SEC)
282        self.emulator.nfc_emulator.waitForService()
283
284        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
285            'ApduSuccess')
286        self.reader.nfc_reader.startPrefixPaymentReaderActivity()
287        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
288
289    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2", "9.1/C-0-1"})
290    def test_prefix_payment_emulator_2(self):
291        """Tests successful APDU exchange between payment service and reader
292        when the payment service has statically registered prefix AIDs.
293        Identical to the test above, except PrefixPaymentService2 is set up
294        first in the emulator activity.
295
296        Test Steps:
297        1. Set callback handler on emulator for when the instrumentation app is
298        set to default wallet app.
299        2. Start emulator activity and wait for the role to be set.
300        2. Set callback handler on emulator for when a TestPass event is
301        received.
302        3. Start reader activity, which should trigger APDU exchange between
303        reader and emulator.
304
305        Verifies:
306        1. Verifies a successful APDU exchange between the emulator and the
307        payment service with prefix AIDs.
308        """
309        role_held_handler = self.emulator.nfc_emulator.asyncWaitForRoleHeld(
310            'RoleHeld')
311        self.emulator.nfc_emulator.startPrefixPaymentEmulator2Activity()
312        role_held_handler.waitAndGet('RoleHeld', _NFC_TIMEOUT_SEC)
313        self.emulator.nfc_emulator.waitForService()
314
315        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
316            'ApduSuccess')
317        self.reader.nfc_reader.startPrefixPaymentReader2Activity()
318        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
319
320    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
321    def test_other_prefix(self):
322        """Tests successful APDU exchange when the emulator dynamically
323        registers prefix AIDs for a non-payment service.
324
325        Test steps:
326        1. Start emulator activity.
327        2. Set callback handler on emulator for when ApduSuccess event is
328        received.
329        3. Start reader activity, which should trigger APDU exchange between
330        reader and emulator.
331
332        Verifies:
333        1. Verifies successful APDU sequence exchange.
334
335        """
336        self.emulator.nfc_emulator.startDualNonPaymentPrefixEmulatorActivity()
337        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
338            'ApduSuccess')
339        self.reader.nfc_reader.startDualNonPaymentPrefixReaderActivity()
340        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
341
342    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
343    def test_offhost_service(self):
344        """Tests successful APDU exchange between offhost service and reader.
345
346        Test Steps:
347        1. Start emulator activity.
348        2. Set callback handler for when reader TestPass event is received.
349        3. Start reader activity, which should trigger APDU exchange between
350        reader and emulator.
351
352        Verifies:
353        1. Verifies a successful APDU exchange inside the reader.
354        We cannot verify the APDUs in the emulator since we don't have access to the secure element.
355        """
356        self.emulator.nfc_emulator.startOffHostEmulatorActivity(False)
357        test_pass_handler = self.reader.nfc_reader.asyncWaitForTestPass('ApduSuccess')
358        self.reader.nfc_reader.startOffHostReaderActivity()
359        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
360
361    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
362    def test_on_and_offhost_service(self):
363        """Tests successful APDU exchange between when reader selects both an on-host and off-host
364        service.
365
366        Test Steps:
367        1. Start emulator activity.
368        2. Set callback handler for when reader TestPass event is received.
369        3. Start reader activity, which should trigger APDU exchange between
370        reader and emulator.
371
372        Verifies:
373        1. Verifies a successful APDU exchange inside the reader.
374        We cannot verify the APDUs in the emulator since we don't have access to the secure element.
375        """
376        self.emulator.nfc_emulator.startOnAndOffHostEmulatorActivity()
377        test_pass_handler = self.reader.nfc_reader.asyncWaitForTestPass('ApduSuccess')
378        self.reader.nfc_reader.startOnAndOffHostReaderActivity()
379        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
380
381    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
382    def test_dual_non_payment(self):
383        """Tests successful APDU exchange between transport service and reader
384        when two non-payment services are enabled.
385
386        Test Steps:
387        1. Start emulator activity which sets up TransportService2 and
388        AccessService.
389        2. Set callback handler on emulator for when a TestPass event is
390        received.
391        3. Start reader activity, which should trigger APDU exchange between
392        reader and emulator.
393
394        Verifies:
395        1. Verifies a successful APDU exchange between the emulator and the
396        transport service.
397        """
398        self.emulator.nfc_emulator.startDualNonPaymentEmulatorActivity()
399        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
400            'ApduSuccess')
401        self.reader.nfc_reader.startDualNonPaymentReaderActivity()
402        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
403
404    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
405    def test_foreground_non_payment(self):
406        """Tests successful APDU exchange between non-payment service and
407          reader when the foreground app sets a preference for the
408          non-default service.
409
410          Test Steps:
411          1. Start emulator activity which sets up TransportService1 and
412          TransportService2
413          2. Set callback handler on emulator for when a TestPass event is
414          received.
415          3. Start reader activity, which should trigger APDU exchange between
416          reader and non-default service.
417
418          Verifies:
419          1. Verifies a successful APDU exchange between the emulator and the
420          transport service.
421          """
422        self.emulator.nfc_emulator.startForegroundNonPaymentEmulatorActivity()
423        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
424            'ApduSuccess')
425        self.reader.nfc_reader.startForegroundNonPaymentReaderActivity()
426        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
427
428    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
429    def test_throughput(self):
430        """Tests that APDU sequence exchange occurs with under 60ms per APDU.
431
432         Test Steps:
433         1. Start emulator activity.
434         2. Set callback handler on emulator for when a TestPass event is
435         received.
436         3. Start reader activity, which should trigger APDU exchange between
437         reader and non-default service.
438
439         Verifies:
440         1. Verifies a successful APDU exchange between the emulator and the
441         transport service with the duration averaging under 60 ms per single
442         exchange.
443         """
444        self.emulator.nfc_emulator.startThroughputEmulatorActivity()
445        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
446            'ApduUnderThreshold')
447        self.reader.nfc_reader.startThroughputReaderActivity()
448        test_pass_handler.waitAndGet('ApduUnderThreshold', _NFC_TIMEOUT_SEC)
449
450    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
451    def test_tap_50_times(self):
452        """Tests that 50 consecutive APDU exchanges are successful.
453
454        Test Steps:
455         1. Start emulator activity.
456         2. Perform the following sequence 50 times:
457            a. Set callback handler on emulator for when a TestPass event is
458            received.
459            b. Start reader activity.
460            c. Wait for successful APDU exchange.
461            d. Close reader activity.
462
463         Verifies:
464         1. Verifies 50 ApduSuccess events are received in a row.
465         """
466        self.emulator.nfc_emulator.startTapTestEmulatorActivity()
467        for i in range(50):
468            test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
469                'ApduSuccess'
470            )
471            self.reader.nfc_reader.startTapTestReaderActivity()
472            test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
473            self.reader.nfc_reader.closeActivity()
474
475    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
476    def test_large_num_aids(self):
477        """Tests that a long APDU sequence (256 commands/responses) is
478        successfully exchanged.
479
480        Test Steps:
481         1. Start emulator activity.
482         2. Set callback handler on emulator for when a TestPass event is
483         received.
484         3. Start reader activity.
485         4. Wait for successful APDU exchange.
486
487         Verifies:
488         1. Verifies successful APDU exchange.
489         """
490        # This test requires a larger timeout due to large number of AIDs
491        large_timeout = 60
492        self.emulator.nfc_emulator.startLargeNumAidsEmulatorActivity()
493        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
494            'ApduSuccess'
495        )
496        self.reader.nfc_reader.startLargeNumAidsReaderActivity()
497        test_pass_handler.waitAndGet('ApduSuccess', large_timeout)
498
499    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
500    def test_screen_off_payment(self):
501        """Tests that APDU exchange occurs when device screen is off.
502
503        Test Steps:
504        1. Set callback handler on emulator for when the instrumentation app is
505        set to default wallet app.
506        2. Start emulator activity and wait for the role to be set.
507        3. Set callback handler for when screen is off.
508        4. Turn emulator screen off and wait for event.
509        5. Set callback handler on emulator for when a TestPass event is
510         received.
511        6. Start reader activity, which should trigger successful APDU exchange.
512        7. Wait for successful APDU exchange.
513
514        Verifies:
515        1. Verifies default wallet app is set.
516        2. Verifies screen is turned off on the emulator.
517        3. Verifies successful APDU exchange with emulator screen off.
518        """
519        role_held_handler = self.emulator.nfc_emulator.asyncWaitForRoleHeld(
520            'RoleHeld'
521        )
522        self.emulator.nfc_emulator.startScreenOffPaymentEmulatorActivity()
523        role_held_handler.waitAndGet('RoleHeld', _NFC_TIMEOUT_SEC)
524        self.emulator.nfc_emulator.waitForService()
525
526        screen_off_handler = self.emulator.nfc_emulator.asyncWaitForScreenOff(
527            'ScreenOff')
528        self.emulator.nfc_emulator.turnScreenOff()
529        screen_off_handler.waitAndGet('ScreenOff', _NFC_TIMEOUT_SEC)
530
531        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
532            'ApduSuccess'
533        )
534        self.reader.nfc_reader.startScreenOffPaymentReaderActivity()
535        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
536
537    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
538    def test_conflicting_non_payment(self):
539        """ This test registers two non-payment services with conflicting AIDs,
540        selects a service to use, and ensures the selected service exchanges
541        an APDU sequence with the reader.
542
543        Test Steps:
544        1. Start emulator.
545        2. Start reader.
546        3. Select a service on the emulator device from the list of services.
547        4. Disable polling on the reader.
548        5. Set a callback handler on the emulator for a successful APDU
549        exchange.
550        6. Re-enable polling on the reader, which should trigger the APDU
551        exchange with the selected service.
552
553        Verifies:
554        1. Verifies APDU exchange is successful between the reader and the
555        selected service.
556        """
557        self.emulator.nfc_emulator.startConflictingNonPaymentEmulatorActivity()
558        self.reader.nfc_reader.startConflictingNonPaymentReaderActivity()
559        self.emulator.nfc_emulator.selectItem()
560        self.reader.nfc_reader.setPollTech(_NFC_TECH_A_POLLING_OFF)
561        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
562            'ApduSuccess'
563        )
564        self.reader.nfc_reader.setPollTech(_NFC_TECH_A_POLLING_ON)
565        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
566
567    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
568    def test_conflicting_non_payment_prefix(self):
569        """ This test registers two non-payment services with conflicting
570        prefix AIDs, selects a service to use, and ensures the selected
571        service exchanges an APDU sequence with the reader.
572
573        Test Steps:
574        1. Start emulator.
575        2. Start reader.
576        3. Select a service on the emulator device from the list of services.
577        4. Disable polling on the reader.
578        5. Set a callback handler on the emulator for a successful APDU
579        exchange.
580        6. Re-enable polling on the reader, which should trigger the APDU
581        exchange with the selected service.
582
583        Verifies:
584        1. Verifies APDU exchange is successful between the reader and the
585        selected service.
586        """
587        (self.emulator.nfc_emulator
588         .startConflictingNonPaymentPrefixEmulatorActivity())
589        self.reader.nfc_reader.startConflictingNonPaymentPrefixReaderActivity()
590        self.emulator.nfc_emulator.selectItem()
591        self.reader.nfc_reader.setPollTech(_NFC_TECH_A_POLLING_OFF)
592        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
593            'ApduSuccess'
594        )
595        self.reader.nfc_reader.setPollTech(_NFC_TECH_A_POLLING_ON)
596        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
597
598    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
599    def test_protocol_params(self):
600        """ Tests that the Nfc-A and ISO-DEP protocol parameters are being
601        set correctly.
602
603        Test Steps:
604        1. Start emulator.
605        2. Start callback handler on reader for when a TestPass event is
606        received.
607        3. Start reader.
608        4. Wait for success event to be sent.
609
610        Verifies:
611        1. Verifies Nfc-A and ISO-DEP protocol parameters are set correctly.
612        """
613        self.emulator.nfc_emulator.startProtocolParamsEmulatorActivity()
614        test_pass_handler = self.reader.nfc_reader.asyncWaitForTestPass(
615            'TestPass')
616        self.reader.nfc_reader.startProtocolParamsReaderActivity()
617        test_pass_handler.waitAndGet('TestPass', _NFC_TIMEOUT_SEC)
618
619    #@CddTest(requirements = {"7.4.4/C-2-2", "7.4.4/C-1-2"})
620    def test_screen_on_only_off_host_service(self):
621        """
622        Test Steps:
623        1. Start emulator and turn screen off.
624        2. Start callback handler on reader for when a TestPass event is
625        received.
626        3. Start reader activity, which should trigger callback handler.
627        4. Ensure expected APDU is received.
628        5. Close reader and turn screen off on the emulator.
629
630        Verifies:
631        1. Verifies correct APDU response when screen is off.
632        2. Verifies correct APDU response between reader and off-host service
633        when screen is on.
634        """
635        #Tests APDU exchange with screen off.
636        self.emulator.nfc_emulator.startScreenOnOnlyOffHostEmulatorActivity()
637        self.emulator.nfc_emulator.turnScreenOff()
638        screen_off_handler = self.emulator.nfc_emulator.asyncWaitForScreenOff(
639            'ScreenOff')
640        screen_off_handler.waitAndGet('ScreenOff', _NFC_TIMEOUT_SEC)
641        test_pass_handler = (
642            self.reader.nfc_reader.asyncWaitForTestPass(
643                'ApduSuccessScreenOff'))
644        self.reader.nfc_reader.startScreenOnOnlyOffHostReaderActivity()
645        test_pass_handler.waitAndGet('ApduSuccessScreenOff', _NFC_TIMEOUT_SEC)
646        self.reader.nfc_reader.closeActivity()
647
648        #Tests APDU exchange with screen on.
649        screen_on_handler = self.emulator.nfc_emulator.asyncWaitForScreenOn(
650            'ScreenOn')
651        self.emulator.nfc_emulator.pressMenu()
652        screen_on_handler.waitAndGet('ScreenOn', _NFC_TIMEOUT_SEC)
653        test_pass_handler = self.reader.nfc_reader.asyncWaitForTestPass(
654            'ApduSuccessScreenOn')
655        self.reader.nfc_reader.startScreenOnOnlyOffHostReaderActivity()
656
657        test_pass_handler.waitAndGet('ApduSuccessScreenOn', _NFC_TIMEOUT_SEC)
658
659    def test_single_payment_service_toggle_nfc_off_on(self):
660        """Tests successful APDU exchange between payment service and
661        reader.
662
663        Test Steps:
664        1. Set callback handler on emulator for when the instrumentation app is
665        set to default wallet app.
666        2. Start emulator activity and wait for the role to be set.
667        3. Toggle NFC off and back on the emulator.
668        4. Set callback handler on emulator for when a TestPass event is
669        received.
670        5. Start reader activity, which should trigger APDU exchange between
671        reader and emulator.
672
673        Verifies:
674        1. Verifies emulator device sets the instrumentation emulator app to the
675        default wallet app.
676        2. Verifies a successful APDU exchange between the emulator and
677        Transport Service after _NFC_TIMEOUT_SEC after toggling NFC off and on.
678        """
679        # Wait for instrumentation app to hold onto wallet role before starting
680        # reader
681        role_held_handler = self.emulator.nfc_emulator.asyncWaitForRoleHeld(
682            'RoleHeld')
683        self.emulator.nfc_emulator.startSinglePaymentEmulatorActivity()
684        role_held_handler.waitAndGet('RoleHeld', _NFC_TIMEOUT_SEC)
685        self.emulator.nfc_emulator.waitForService()
686
687        self.emulator.nfc_emulator.setNfcState(False)
688        self.emulator.nfc_emulator.setNfcState(True)
689
690        test_pass_handler = self.emulator.nfc_emulator.asyncWaitForTestPass(
691            'ApduSuccess')
692        self.reader.nfc_reader.startSinglePaymentReaderActivity()
693        test_pass_handler.waitAndGet('ApduSuccess', _NFC_TIMEOUT_SEC)
694
695    def teardown_test(self):
696        self.emulator.nfc_emulator.closeActivity()
697        self.reader.nfc_reader.closeActivity()
698        utils.concurrent_exec(lambda d: d.services.create_output_excerpts_all(
699            self.current_test_info),
700                              param_list=[[self.emulator], [self.reader]],
701                              raise_on_exception=True)
702        self.emulator.nfc_emulator.logInfo("*** TEST END: " + self.current_test_info.name + " ***")
703        self.reader.nfc_reader.logInfo("*** TEST END: " + self.current_test_info.name + " ***")
704
705
706if __name__ == '__main__':
707    # Take test args
708    if '--' in sys.argv:
709        index = sys.argv.index('--')
710        sys.argv = sys.argv[:1] + sys.argv[index + 1:]
711    test_runner.main()
712