Home | History | Annotate | Download | only in 2_1
      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 time
     20 
     21 from vts.runners.host import asserts
     22 from vts.runners.host import base_test
     23 from vts.runners.host import test_runner
     24 
     25 
     26 class SampleCameraV2Test(base_test.BaseTestClass):
     27     """A sample testcase for the non-HIDL, conventional Camera HAL."""
     28     # Camera HAL version value (v2.1).
     29     VERSION_2_1 = 0x201
     30     VERSION_2_4 = 0x204
     31     MAX_RETRIES = 5
     32 
     33     def setUpClass(self):
     34         self.dut = self.android_devices[0]
     35         self.dut.hal.InitConventionalHal(
     36             target_type="camera",
     37             target_version=2.1,
     38             target_basepaths=["/system/lib/hw"],
     39             bits=32,
     40             target_package="hal.conventional.camera")
     41 
     42     def setUp(self):
     43         self.call_count_camera_device_status_change = 0
     44         self.call_count_torch_mode_status_change = 0
     45 
     46     def testCameraNormal(self):
     47         """A simple testcase which just emulates a normal usage pattern."""
     48         version = self.dut.hal.camera.common.GetAttributeValue(
     49             "module_api_version")
     50         logging.info("version: %s", hex(version))
     51         if version != self.VERSION_2_1 and version != self.VERSION_2_4:
     52             asserts.skip("HAL version %s is neither v2.1 nor v2.4" % version)
     53 
     54         result = self.dut.hal.camera.get_number_of_cameras()
     55         count = result.return_type.scalar_value.int32_t
     56         logging.info("# of found cameras: %s", count)
     57         asserts.assertTrue(count > 0, "no camera found")
     58         for index in range(0, count):
     59             arg = self.dut.hal.camera.camera_info_t(facing=0)
     60             logging.info(self.dut.hal.camera.get_camera_info(index, arg))
     61 
     62         # uncomment when undefined function is handled gracefully.
     63         # self.dut.hal.camera.init()
     64         def camera_device_status_change(callbacks, camera_id, new_status):
     65             self.call_count_camera_device_status_change += 1
     66             logging.info("camera_device_status_change")
     67             logging.info("camera_device_status_change: camera_id = %s",
     68                          camera_id)
     69             logging.info("camera_device_status_change: new_status = %s",
     70                          new_status)
     71             logging.info("camera_device_status_change: callbacks = %s",
     72                          callbacks)
     73 
     74         def torch_mode_status_change(callbacks, camera_id, new_status):
     75             self.profiling.StopHostProfiling(
     76                 "callback_latency_torch_mode_status_change")
     77             self.call_count_torch_mode_status_change += 1
     78             logging.info("torch_mode_status_change")
     79             logging.info("torch_mode_status_change: camera_id = %s", camera_id)
     80             logging.info("torch_mode_status_change: new_status = %s",
     81                          new_status)
     82             logging.info("torch_mode_status_change: callbacks = %s", callbacks)
     83 
     84         my_callback = self.dut.hal.camera.camera_module_callbacks_t(
     85             camera_device_status_change, torch_mode_status_change)
     86         self.dut.hal.camera.set_callbacks(my_callback)
     87         self.profiling.StartHostProfiling(
     88             "callback_latency_torch_mode_status_change")
     89         self.dut.hal.camera.common.methods.open()  # note args are skipped
     90         retries = 0
     91         while (self.call_count_torch_mode_status_change < 1 and
     92                retries < self.MAX_RETRIES):
     93             logging.info("waiting %s %s",
     94                          self.call_count_camera_device_status_change,
     95                          self.call_count_torch_mode_status_change)
     96             time.sleep(1)
     97             retries += 1
     98         if self.call_count_torch_mode_status_change < 1:
     99             # The above callback was not always called (~50% of chance).
    100             logging.error("Callback not called within %s seconds",
    101                           self.MAX_RETRIES)
    102 
    103 
    104 if __name__ == "__main__":
    105     test_runner.main()
    106