1# Copyright 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Verifies that flash is fired when lighting conditions are dark."""
15
16
17import logging
18import os.path
19import pathlib
20
21import cv2
22from mobly import test_runner
23import numpy as np
24
25import its_base_test
26import camera_properties_utils
27import image_processing_utils
28import its_session_utils
29import lighting_control_utils
30import opencv_processing_utils
31import ui_interaction_utils
32
33_JETPACK_CAMERA_APP_PACKAGE_NAME = 'com.google.jetpackcamera'
34_MEAN_DELTA_ATOL = 15  # mean used for reflective charts
35_PATCH_H = 0.25  # center 25%
36_PATCH_W = 0.25
37_PATCH_X = 0.5 - _PATCH_W/2
38_PATCH_Y = 0.5 - _PATCH_H/2
39_TEST_NAME = os.path.splitext(os.path.basename(__file__))[0]
40
41
42class AutoFlashTest(its_base_test.UiAutomatorItsBaseTest):
43  """Test that flash is fired when lighting conditions are dark using JCA."""
44
45  def setup_class(self):
46    super().setup_class()
47    self.ui_app = _JETPACK_CAMERA_APP_PACKAGE_NAME
48
49  def teardown_test(self):
50    ui_interaction_utils.force_stop_app(self.dut, self.ui_app)
51
52  def test_auto_flash(self):
53    with its_session_utils.ItsSession(
54        device_id=self.dut.serial,
55        camera_id=self.camera_id,
56        hidden_physical_id=self.hidden_physical_id) as cam:
57      props = cam.get_camera_properties()
58      props = cam.override_with_hidden_physical_camera_props(props)
59      test_name = os.path.join(self.log_path, _TEST_NAME)
60
61      # close camera after props retrieved, so that ItsTestActivity can open it
62      cam.close_camera()
63
64      # check SKIP conditions
65      first_api_level = its_session_utils.get_first_api_level(self.dut.serial)
66      facing_front = (props['android.lens.facing'] ==
67                      camera_properties_utils.LENS_FACING['FRONT'])
68      should_run_front = (
69          facing_front and
70          first_api_level >= its_session_utils.ANDROID15_API_LEVEL
71      )
72      should_run_rear = (
73          camera_properties_utils.flash(props) and
74          first_api_level >= its_session_utils.ANDROID13_API_LEVEL
75      )
76      camera_properties_utils.skip_unless(should_run_front or should_run_rear)
77
78      # establish connection with lighting controller
79      arduino_serial_port = lighting_control_utils.lighting_control(
80          self.lighting_cntl, self.lighting_ch)
81
82      # turn OFF lights to darken scene
83      lighting_control_utils.set_lighting_state(
84          arduino_serial_port, self.lighting_ch, 'OFF')
85
86      # take capture with no flash as baseline
87      path = pathlib.Path(
88          cam.do_jca_capture(
89              self.dut,
90              self.log_path,
91              flash='OFF',
92              facing=props['android.lens.facing'],
93          )
94      )
95      no_flash_capture_path = path.with_name(
96          f'{path.stem}_no_flash{path.suffix}'
97      )
98      os.rename(path, no_flash_capture_path)
99      cv2_no_flash_image = cv2.imread(str(no_flash_capture_path))
100      y = opencv_processing_utils.convert_to_y(cv2_no_flash_image, 'BGR')
101      # Add a color channel dimension for interoperability
102      y = np.expand_dims(y, axis=2)
103      patch = image_processing_utils.get_image_patch(
104          y, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H
105      )
106      no_flash_mean = image_processing_utils.compute_image_means(patch)[0]
107      image_processing_utils.write_image(y, f'{test_name}_no_flash_Y.jpg')
108      logging.debug('No flash frames Y mean: %.4f', no_flash_mean)
109
110      # take capture with auto flash enabled
111      logging.debug('Taking capture with auto flash enabled.')
112      path = pathlib.Path(
113          cam.do_jca_capture(
114              self.dut,
115              self.log_path,
116              flash='AUTO',
117              facing=props['android.lens.facing']
118          )
119      )
120      auto_flash_capture_path = path.with_name(
121          f'{path.stem}_auto_flash{path.suffix}'
122      )
123      os.rename(path, auto_flash_capture_path)
124      cv2_auto_flash_image = cv2.imread(str(auto_flash_capture_path))
125      y = opencv_processing_utils.convert_to_y(cv2_auto_flash_image, 'BGR')
126      # Add a color channel dimension for interoperability
127      y = np.expand_dims(y, axis=2)
128      patch = image_processing_utils.get_image_patch(
129          y, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H
130      )
131      flash_mean = image_processing_utils.compute_image_means(patch)[0]
132      image_processing_utils.write_image(y, f'{test_name}_auto_flash_Y.jpg')
133      logging.debug('Flash frames Y mean: %.4f', flash_mean)
134
135      # confirm correct behavior
136      mean_delta = flash_mean - no_flash_mean
137      if mean_delta <= _MEAN_DELTA_ATOL:
138        raise AssertionError(f'mean FLASH-OFF: {mean_delta:.3f}, '
139                             f'ATOL: {_MEAN_DELTA_ATOL}')
140
141      # turn lights back ON
142      lighting_control_utils.set_lighting_state(
143          arduino_serial_port, self.lighting_ch, 'ON')
144
145if __name__ == '__main__':
146  test_runner.main()
147