1#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import copy
17import logging
18
19from vts.runners.host import base_test
20from vts.testcases.template.param_test import param_test
21from vts.utils.python.controllers import android_device
22from vts.utils.python.hal import hal_service_name_utils
23from vts.utils.python.precondition import precondition_utils
24
25
26class HalHidlHostTest(param_test.ParamTestClass):
27    """Base class to run a host-driver hidl hal test.
28
29    Attributes:
30        dut: AndroidDevice, the device under test as config
31        shell: ShellMirrorObject, shell mirror
32        TEST_HAL_SERVICES: a set of hal services accessed in the test.
33    """
34    TEST_HAL_SERVICES = set()
35
36    # @Override
37    def initParams(self):
38        """Get the service combination according to the registered test HAL."""
39        self.dut = self.android_devices[0]
40        self.shell = self.dut.shell
41        service_instance_combinations = self._GetServiceInstanceCombinations()
42        self.params = service_instance_combinations
43
44    # @Override
45    def setUpClass(self):
46        """Basic setup process for host-side hidl hal tests.
47
48        Test precondition check, prepare for profiling and coverage measurement
49        if enabled.
50        """
51        # Testability check.
52        if not precondition_utils.CanRunHidlHalTest(
53                self, self.dut, self.shell, self.run_as_compliance_test):
54            self.skipAllTests("precondition check for hidl hal tests didn't pass")
55            return
56
57        # Initialization for coverage measurement.
58        if self.coverage.enabled and self.coverage.global_coverage:
59            self.coverage.InitializeDeviceCoverage(self.dut)
60            if self.TEST_HAL_SERVICES:
61                self.coverage.SetHalNames(self.TEST_HAL_SERVICES)
62                self.coverage.SetCoverageReportFilePrefix(self.test_module_name + self.abi_bitness)
63
64        # Enable profiling.
65        if self.profiling.enabled:
66            self.profiling.EnableVTSProfiling(self.shell)
67
68    # @Override
69    def tearDownClass(self):
70        """Basic cleanup process for host-side hidl hal tests.
71
72        If profiling is enabled for the test, collect the profiling data
73        and disable profiling after the test is done.
74        If coverage is enabled for the test, collect the coverage data and
75        upload it to dashboard.
76        """
77        if self.isSkipAllTests():
78            return
79
80        if self.coverage.enabled and self.coverage.global_coverage:
81            self.coverage.SetCoverageData(dut=self.dut, isGlobal=True)
82
83        if self.profiling.enabled:
84            self.profiling.ProcessAndUploadTraceData()
85
86    # @Override
87    def setUp(self):
88        """Setup process for each test case."""
89        if self.profiling.enabled:
90            self.profiling.EnableVTSProfiling(self.shell)
91
92    # @Override
93    def tearDown(self):
94        """Cleanup process for each test case."""
95        if self.profiling.enabled:
96            self.profiling.ProcessTraceDataForTestCase(self.dut)
97            self.profiling.DisableVTSProfiling(self.shell)
98
99    # @Override
100    def getParamTag(self, param):
101        """Concatenate names for all services passed as the param test tag.
102
103        Args:
104            param: a list of service instances. e.g [s1/n1, s2/n2]
105
106        Returns:
107            a string of concatenated service names. e.g. n1/n2
108        """
109        names = map(lambda instance: instance.split("/")[1], param)
110        return "({})".format(",".join(names))
111
112    def getHalServiceName(self, hal_service):
113        """Get corresponding name for hal_service from the current parameter.
114
115        The current parameter should be a list of service instances with the
116        format [hal_service/name], e.g [s1/n1, s2/n2]
117
118        Args:
119            hal_service: string, hal@version e.g. foo@1.0
120
121        Returns:
122            Name for hal_service, "default" if could not find the hal_service in
123            the list of service instances.
124        """
125        for instance in self.cur_param:
126            service, name = instance.split("/")
127            if service == hal_service:
128                return str(name)
129        # In case could not find the name for given hal_service, fall back to
130        # use the "default" name.
131        logging.warning(
132            "Could not find the service name for %s, using default name instead",
133            hal_service)
134        return "default"
135
136    def _GetServiceInstanceCombinations(self):
137        """Create combinations of instances for registered HAL services.
138
139        Returns:
140            A list of instance combination for registered HAL.
141        """
142        registered_services = copy.copy(self.TEST_HAL_SERVICES)
143        service_instances = {}
144
145        for service in registered_services:
146            testable, service_names = hal_service_name_utils.GetHalServiceName(
147                self.shell, service, self.abi_bitness,
148                self.run_as_compliance_test)
149            if not testable:
150                self.skipAllTests("Hal: %s is not testable, "
151                                  "skip all tests." % service)
152                return []
153            if service_names:
154                service_instances[service] = service_names
155            else:
156                self.skipAllTests("No service name found for: %s, "
157                                  "skip all tests." % service)
158                return []
159        logging.info("registered service instances: %s", service_instances)
160
161        return hal_service_name_utils.GetServiceInstancesCombinations(
162                registered_services, service_instances)
163