1#!/usr/bin/env python3
2# Lint as: python3
3"""
4Base class for setting up devices for CDM functionalities.
5"""
6
7from mobly import base_test
8from mobly import utils
9from mobly.controllers import android_device
10from time import sleep
11
12CDM_SNIPPET_PACKAGE = 'android.companion.cts.multidevice'
13
14BT_DISCOVERABLE_TIME = 15
15OPERATION_DELAY_TIME = 5
16
17class BaseTestClass(base_test.BaseTestClass):
18
19    def setup_class(self):
20        # Declare that two Android devices are needed.
21        self.primary, self.secondary = self.register_controller(
22            android_device, min_number=2)
23
24        def _setup_device(device):
25            device.load_snippet('cdm', CDM_SNIPPET_PACKAGE)
26            device.adb.shell('input keyevent KEYCODE_WAKEUP')
27            device.adb.shell('input keyevent KEYCODE_MENU')
28            device.adb.shell('input keyevent KEYCODE_HOME')
29
30            # Enable bluetooth and enable receivers
31            device.cdm.btEnable()
32
33            # Clean up existing associations
34            device.cdm.disassociateAll()
35
36            # Clear bluetooth bonds
37            self.clear_bonded_devices(device)
38
39
40        # Sets up devices in parallel to save time.
41        utils.concurrent_exec(
42            _setup_device,
43            ((self.primary,), (self.secondary,)),
44            max_workers=2,
45            raise_on_exception=True)
46
47    def teardown_test(self):
48        """Clean up tests"""
49        self.primary.cdm.disassociateAll()
50        self.secondary.cdm.disassociateAll()
51
52        self.clear_bonded_devices(self.primary)
53        self.clear_bonded_devices(self.secondary)
54
55    def clear_bonded_devices(self, ad: android_device.AndroidDevice):
56        """Remove bluetooth bonds"""
57        paired_devices = ad.cdm.btGetPairedDevices()
58        for device in paired_devices:
59            ad.cdm.btUnpairDevice(device['Address'])
60            sleep(OPERATION_DELAY_TIME)
61