1#!/usr/bin/env python
2#
3# Copyright (C) 2016 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 time
20
21from vts.runners.host import asserts
22from vts.runners.host import test_runner
23from vts.testcases.template.hal_hidl_host_test import hal_hidl_host_test
24
25TVCEC_V1_0_HAL = "android.hardware.tv.cec@1.0::IHdmiCec"
26
27class TvCecHidlTest(hal_hidl_host_test.HalHidlHostTest):
28    """Host testcase class for the TV HDMI_CEC HIDL HAL."""
29
30    TEST_HAL_SERVICES = {TVCEC_V1_0_HAL}
31    def setUpClass(self):
32        """Creates a mirror and init tv hdmi cec hal service."""
33        super(TvCecHidlTest, self).setUpClass()
34
35        self.dut.hal.InitHidlHal(
36            target_type="tv_cec",
37            target_basepaths=self.dut.libPaths,
38            target_version=1.0,
39            target_package="android.hardware.tv.cec",
40            target_component_name="IHdmiCec",
41            hw_binder_service_name=self.getHalServiceName(TVCEC_V1_0_HAL),
42            bits=int(self.abi_bitness))
43
44        time.sleep(1) # Wait for hal to be ready
45
46        self.vtypes = self.dut.hal.tv_cec.GetHidlTypeInterface("types")
47        logging.info("tv_cec types: %s", self.vtypes)
48
49    def testClearAndAddLogicalAddress(self):
50        """A simple test case which sets logical address and clears it."""
51        self.dut.hal.tv_cec.clearLogicalAddress()
52        result = self.dut.hal.tv_cec.addLogicalAddress(
53                self.vtypes.CecLogicalAddress.PLAYBACK_3)
54        asserts.assertEqual(self.vtypes.Result.SUCCESS, result)
55        logging.info("addLogicalAddress result: %s", result)
56
57    def testGetPhysicalAddress(self):
58        """A simple test case which queries the physical address."""
59        status, paddr = self.dut.hal.tv_cec.getPhysicalAddress()
60        asserts.assertEqual(self.vtypes.Result.SUCCESS, status)
61        logging.info("getPhysicalAddress status: %s, paddr: %s", status, paddr)
62
63    def testSendRandomMessage(self):
64        """A test case which sends a random message."""
65        cec_message = {
66            "initiator": self.vtypes.CecLogicalAddress.TV,
67            "destination": self.vtypes.CecLogicalAddress.PLAYBACK_1,
68            "body": [1, 2, 3]
69        }
70        message = self.vtypes.Py2Pb("CecMessage", cec_message)
71        logging.info("message: %s", message)
72        result = self.dut.hal.tv_cec.sendMessage(message)
73        logging.info("sendMessage result: %s", result)
74
75    def testGetCecVersion1(self):
76        """A simple test case which queries the cec version."""
77        version = self.dut.hal.tv_cec.getCecVersion()
78        logging.info("getCecVersion version: %s", version)
79
80    def testGetVendorId(self):
81        """A simple test case which queries vendor id."""
82        vendor_id = self.dut.hal.tv_cec.getVendorId()
83        asserts.assertEqual(0, 0xff000000 & vendor_id)
84        logging.info("getVendorId vendor_id: %s", vendor_id)
85
86    def testGetPortInfo(self):
87        """A simple test case which queries port information."""
88        port_infos = self.dut.hal.tv_cec.getPortInfo()
89        logging.info("getPortInfo port_infos: %s", port_infos)
90
91    def testSetOption(self):
92        """A simple test case which changes CEC options."""
93        self.dut.hal.tv_cec.setOption(self.vtypes.OptionKey.WAKEUP, True)
94        self.dut.hal.tv_cec.setOption(self.vtypes.OptionKey.ENABLE_CEC, True)
95        self.dut.hal.tv_cec.setOption(
96                self.vtypes.OptionKey.SYSTEM_CEC_CONTROL, True)
97
98    def testSetLanguage(self):
99        """A simple test case which updates language information."""
100        self.dut.hal.tv_cec.setLanguage("eng")
101
102    def testEnableAudioReturnChannel(self):
103        """Checks whether Audio Return Channel can be enabled."""
104        port_infos = self.dut.hal.tv_cec.getPortInfo()
105        for port_info in port_infos:
106            if "portId" in port_info and port_info.get("arcSupported"):
107                self.dut.hal.tv_cec.enableAudioReturnChannel(
108                        port_info["portId"], True)
109
110    def testIsConnected(self):
111        """A simple test case which queries the connected status."""
112        status = self.dut.hal.tv_cec.isConnected()
113        logging.info("isConnected status: %s", status)
114
115if __name__ == "__main__":
116    test_runner.main()
117