1#!/usr/bin/env python
2#
3# Copyright (C) 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#
17
18import logging
19import threading
20import time
21
22from vts.runners.host import asserts
23from vts.runners.host import base_test
24from vts.runners.host import test_runner
25
26
27class CodelabHostMultiHalTest(base_test.BaseTestClass):
28    """A simple testcase for testing multiple HALs."""
29
30    def setUpClass(self):
31        """Creates hal mirrors."""
32        self.dut = self.android_devices[0]
33
34        self.dut.hal.InitHidlHal(
35            target_type="light",
36            target_basepaths=self.dut.libPaths,
37            target_version=2.0,
38            target_package="android.hardware.light",
39            target_component_name="ILight",
40            bits=int(self.abi_bitness))
41
42        self.dut.hal.InitHidlHal(
43            target_type="thermal",
44            target_basepaths=self.dut.libPaths,
45            target_version=1.0,
46            target_package="android.hardware.thermal",
47            target_component_name="IThermal",
48            bits=int(self.abi_bitness))
49
50        self.light_types = self.dut.hal.light.GetHidlTypeInterface("types")
51        self.thermal_types = self.dut.hal.thermal.GetHidlTypeInterface("types")
52
53    def LightTest(self):
54        """A sample test for Light HAL."""
55        whiteState = {
56            "color": 0xFFFFFFFF,
57            "flashMode": self.light_types.Flash.TIMED,
58            "flashOnMs": 100,
59            "flashOffMs": 50,
60            "brightnessMode": self.light_types.Brightness.USER,
61        }
62        whiteState_pb = self.light_types.Py2Pb("LightState", whiteState)
63        status = self.dut.hal.light.setLight(self.light_types.Type.BACKLIGHT,
64                                             whiteState_pb)
65        asserts.assertEqual(status, self.light_types.Status.SUCCESS)
66
67    def ThermalTest(self):
68        """A sample test for Thermal HAL."""
69        status, cpuUsages = self.dut.hal.thermal.getCpuUsages()
70        asserts.assertEqual(status['code'],
71                            self.thermal_types.ThermalStatusCode.SUCCESS)
72
73    def testBase(self):
74        """A basic test case which tests APIs of two HALs."""
75        self.LightTest()
76        self.ThermalTest()
77
78    def testMutlThread(self):
79        """A basic test case which tests two HALs in parallel."""
80        def LightWorker():
81            for i in range(20):
82                logging.info("Light test round: %s", i)
83                self.LightTest()
84                time.sleep(1)
85            logging.info("Light test exiting.")
86
87        def ThermalWorker():
88            for i in range(10):
89                logging.info("Thermal test round: %s", i)
90                self.ThermalTest()
91                time.sleep(2)
92            logging.info("Thermal test exiting.")
93
94        t1 = threading.Thread(name='light_test', target=LightWorker)
95        t2 = threading.Thread(name='thermal_test', target=ThermalWorker)
96        t1.start()
97        t2.start()
98
99        t1.join()
100        t2.join()
101
102
103if __name__ == "__main__":
104    test_runner.main()
105