Home | History | Annotate | Download | only in host
      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 
     18 import logging
     19 import threading
     20 import time
     21 
     22 from vts.runners.host import asserts
     23 from vts.runners.host import base_test
     24 from vts.runners.host import test_runner
     25 from vts.utils.python.controllers import android_device
     26 from vts.utils.python.precondition import precondition_utils
     27 
     28 
     29 class ContextHubCallback:
     30     def __init__(self, hub_id):
     31         self.hub_id = hub_id
     32         self.event = threading.Event()
     33 
     34     def wait_on_callback(timeout=None):
     35         """Wait on the next callback in this object to be invoked.
     36 
     37         Args:
     38             timeout: (fractional) seconds to wait before timing out, or None
     39 
     40         Returns:
     41             True when a callback was received, False if a timeout occurred
     42         """
     43         return self.event.wait(timeout)
     44 
     45     def prepare():
     46         # TODO: cleaner method of doing this, so that we clear --> call HAL
     47         # method --> wait for CB, all wrapped into one
     48         self.event.clear()
     49 
     50     def ignore_client_msg(self, msg):
     51         logging.debug("Ignoring client message from hubId %s: %s", self.hub_id, msg)
     52         self.event.set()
     53 
     54     def ignore_txn_result(self, txnId, result):
     55         logging.debug("Ignoring transaction result from hubId %s: %s", self.hub_id, msg)
     56         self.event.set()
     57 
     58     def ignore_hub_event(self, evt):
     59         logging.debug("Ignoring hub event from hubId %s: %s", self.hub_id, evt)
     60         self.event.set()
     61 
     62     def ignore_apps_info(self, appInfo):
     63         logging.debug("Ignoring app info data from hubId %s: %s", self.hub_id, appInfo)
     64         self.event.set()
     65 
     66 
     67 class ContexthubHidlTest(base_test.BaseTestClass):
     68     """A set of test cases for the context hub HIDL HAL"""
     69 
     70     def setUpClass(self):
     71         """Creates a mirror and turns on the framework-layer CONTEXTHUB service."""
     72         self.dut = self.registerController(android_device)[0]
     73 
     74         self.dut.shell.InvokeTerminal("one")
     75         self.dut.shell.one.Execute("setenforce 0")  # SELinux permissive mode
     76         if not precondition_utils.CanRunHidlHalTest(
     77             self, self.dut, self.dut.shell.one):
     78             self._skip_all_testcases = True
     79             return
     80 
     81         if self.profiling.enabled:
     82             self.profiling.EnableVTSProfiling(self.dut.shell.one)
     83 
     84         # Bring down the Android runtime so it doesn't interfere with the test
     85         #self.dut.shell.one.Execute("stop")
     86 
     87         self.dut.hal.InitHidlHal(
     88             target_type="contexthub",
     89             target_basepaths=self.dut.libPaths,
     90             target_version=1.0,
     91             target_package="android.hardware.contexthub",
     92             target_component_name="IContexthub",
     93             bits=int(self.abi_bitness))
     94 
     95         self.types = self.dut.hal.contexthub.GetHidlTypeInterface("types")
     96         logging.info("types: %s", self.types)
     97 
     98     def tearDownClass(self):
     99         # Restart the Android runtime
    100         #self.dut.shell.one.Execute("start")
    101         if not self._skip_all_testcases and self.profiling.enabled:
    102             self.profiling.ProcessAndUploadTraceData()
    103 
    104     def tearDown(self):
    105         """Process trace data.
    106         """
    107         if self.profiling.enabled:
    108             self.profiling.ProcessTraceDataForTestCase(self.dut)
    109             self.profiling.DisableVTSProfiling(self.dut.shell.one)
    110 
    111     def testContexthubBasic(self):
    112         logging.info("About to call gethubs!!!")
    113         hubs = self.dut.hal.contexthub.getHubs()
    114         logging.info("Got result: %s", hubs)
    115         #
    116         #hub_id = 0 # TODO: should get this from hub list
    117         #
    118         #cb = ContextHubCallback(hub_id)
    119         #client_callback = self.dut.hal.contexthub.GetHidlCallbackInterface(
    120         #    "IContexthubCallback",
    121         #    handleClientMsg=cb.ignore_client_msg,
    122         #    handleTxnResult=cb.ignore_txn_result,
    123         #    handleHubEvent=cb.ignore_hub_event,
    124         #    handleAppsInfo=cb.ignore_apps_info)
    125         #
    126         #logging.info("About to call registerCallback")
    127         #result = self.dut.hal.contexthub.registerCallback(hub_id,
    128         #                                                  client_callback)
    129         #logging.info("registerCallback returned result: %s", result)
    130         #
    131         #logging.info("About to call queryApps")
    132         #result = self.dut.hal.contexthub.queryApps(hub_id)
    133         #logging.info("queryApps returned result: %s", result)
    134         #
    135         #got_callback = cb.wait_on_callback(5)
    136         #logging.info("Wait on callback returned %s", got_callback)
    137 
    138 if __name__ == "__main__":
    139     test_runner.main()
    140