1# Copyright (C) 2022 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"""Base for all Nearby Mainline Fast Pair end-to-end test cases.""" 16 17from typing import List, Tuple 18 19from mobly import base_test 20from mobly import signals 21from mobly.controllers import android_device 22 23from test_helper import constants 24from test_helper import fast_pair_provider_simulator 25from test_helper import fast_pair_seeker 26 27# Abbreviations for common use type. 28AndroidDevice = android_device.AndroidDevice 29FastPairProviderSimulator = fast_pair_provider_simulator.FastPairProviderSimulator 30FastPairSeeker = fast_pair_seeker.FastPairSeeker 31REQUIRED_BUILD_FINGERPRINT = constants.FAST_PAIR_PROVIDER_SIMULATOR_BUILD_FINGERPRINT 32 33 34class FastPairBaseTest(base_test.BaseTestClass): 35 """Base class for all Nearby Mainline Fast Pair end-to-end classes to inherit.""" 36 37 _duts: List[AndroidDevice] 38 _provider: FastPairProviderSimulator 39 _seeker: FastPairSeeker 40 41 def setup_class(self) -> None: 42 super().setup_class() 43 self._duts = self.register_controller(android_device, min_number=2) 44 45 provider_ad, seeker_ad = self._check_devices_supported() 46 self._provider = FastPairProviderSimulator(provider_ad) 47 self._seeker = FastPairSeeker(seeker_ad) 48 self._provider.load_snippet() 49 self._seeker.load_snippet() 50 51 def setup_test(self) -> None: 52 super().setup_test() 53 self._provider.setup_provider_simulator(constants.SETUP_TIMEOUT_SEC) 54 55 def teardown_test(self) -> None: 56 super().teardown_test() 57 # Create per-test excepts of logcat. 58 for dut in self._duts: 59 dut.services.create_output_excerpts_all(self.current_test_info) 60 61 def _check_devices_supported(self) -> Tuple[AndroidDevice, AndroidDevice]: 62 # Assume the 1st phone is provider, the 2nd one is seeker. 63 provider_ad, seeker_ad = self._duts[:2] 64 65 for ad in self._duts: 66 if ad.build_info['build_fingerprint'] == REQUIRED_BUILD_FINGERPRINT: 67 if ad != provider_ad: 68 provider_ad, seeker_ad = seeker_ad, provider_ad 69 break 70 else: 71 raise signals.TestAbortClass( 72 f'None of phones has custom ROM ({REQUIRED_BUILD_FINGERPRINT}) for Fast Pair ' 73 f'provider simulator. Skip all the test cases!') 74 75 return provider_ad, seeker_ad 76