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 base_test 23from vts.runners.host import test_runner 24from vts.utils.python.controllers import android_device 25 26 27class SampleCameraV2Test(base_test.BaseTestClass): 28 """A sample testcase for the non-HIDL, conventional Camera HAL.""" 29 # Camera HAL version value (v2.1). 30 VERSION_2_1 = 0x201 31 VERSION_2_4 = 0x204 32 MAX_RETRIES = 5 33 34 def setUpClass(self): 35 self.dut = self.registerController(android_device)[0] 36 self.dut.hal.InitConventionalHal(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("module_api_version") 49 logging.info("version: %s", hex(version)) 50 if version != self.VERSION_2_1 and version != self.VERSION_2_4: 51 asserts.skip("HAL version %s is neither v2.1 nor v2.4" % version) 52 53 result = self.dut.hal.camera.get_number_of_cameras() 54 count = result.return_type.scalar_value.int32_t 55 logging.info("# of found cameras: %s", count) 56 asserts.assertTrue(count > 0, "no camera found") 57 for index in range(0, count): 58 arg = self.dut.hal.camera.camera_info_t(facing=0) 59 logging.info(self.dut.hal.camera.get_camera_info(index, arg)) 60 61 # uncomment when undefined function is handled gracefully. 62 # self.dut.hal.camera.init() 63 def camera_device_status_change(callbacks, camera_id, new_status): 64 self.call_count_camera_device_status_change += 1 65 logging.info("camera_device_status_change") 66 logging.info("camera_device_status_change: camera_id = %s", camera_id) 67 logging.info("camera_device_status_change: new_status = %s", new_status) 68 logging.info("camera_device_status_change: callbacks = %s", callbacks) 69 70 def torch_mode_status_change(callbacks, camera_id, new_status): 71 self.profiling.StopHostProfiling("callback_latency_torch_mode_status_change") 72 self.call_count_torch_mode_status_change += 1 73 logging.info("torch_mode_status_change") 74 logging.info("torch_mode_status_change: camera_id = %s", camera_id) 75 logging.info("torch_mode_status_change: new_status = %s", new_status) 76 logging.info("torch_mode_status_change: callbacks = %s", callbacks) 77 78 my_callback = self.dut.hal.camera.camera_module_callbacks_t( 79 camera_device_status_change, torch_mode_status_change) 80 self.dut.hal.camera.set_callbacks(my_callback) 81 self.profiling.StartHostProfiling("callback_latency_torch_mode_status_change") 82 self.dut.hal.camera.common.methods.open() # note args are skipped 83 retries = 0 84 while (self.call_count_torch_mode_status_change < 1 and 85 retries < self.MAX_RETRIES): 86 logging.info("waiting %s %s", 87 self.call_count_camera_device_status_change, 88 self.call_count_torch_mode_status_change) 89 time.sleep(1) 90 retries += 1 91 if self.call_count_torch_mode_status_change < 1: 92 # The above callback was not always called (~50% of chance). 93 logging.error("Callback not called within %s seconds", 94 self.MAX_RETRIES) 95 96 97if __name__ == "__main__": 98 test_runner.main() 99