xref: /aosp_15_r20/cts/apps/CameraITS/utils/zoom_capture_utils_tests.py (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1# Copyright 2023 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"""Tests for zoom_capture_utils."""
15
16
17import unittest
18
19import zoom_capture_utils
20
21
22_ARUCO_MARKER_CENTER = (320, 240)
23_FOCAL_LENGTH = 1
24_IMG_SIZE = (640, 480)
25_OFFSET_RTOL = 0.1
26_RADIUS_RTOL = 0.1
27_WRONG_ARUCO_OFFSET = 100
28
29
30def _generate_aruco_markers(center, side_length):
31  x, y = center
32  return (
33      (x - side_length // 2, y - side_length // 2),  # top left
34      (x + side_length // 2, y - side_length // 2),  # top right
35      (x + side_length // 2, y + side_length // 2),  # bottom right
36      (x - side_length // 2, y + side_length // 2),  # bottom left
37  )
38
39
40def _generate_valid_zoom_results():
41  return [
42      zoom_capture_utils.ZoomTestData(
43          result_zoom=1,
44          radius_tol=_RADIUS_RTOL,
45          offset_tol=_OFFSET_RTOL,
46          focal_length=_FOCAL_LENGTH,
47          aruco_corners=_generate_aruco_markers(_ARUCO_MARKER_CENTER, 10),
48          aruco_offset=10
49      ),
50      zoom_capture_utils.ZoomTestData(
51          result_zoom=2,
52          radius_tol=_RADIUS_RTOL,
53          offset_tol=_OFFSET_RTOL,
54          focal_length=_FOCAL_LENGTH,
55          aruco_corners=_generate_aruco_markers(_ARUCO_MARKER_CENTER, 20),
56          aruco_offset=20
57      ),
58      zoom_capture_utils.ZoomTestData(
59          result_zoom=3,
60          radius_tol=_RADIUS_RTOL,
61          offset_tol=_OFFSET_RTOL,
62          focal_length=_FOCAL_LENGTH,
63          aruco_corners=_generate_aruco_markers(_ARUCO_MARKER_CENTER, 30),
64          aruco_offset=30
65      ),
66      zoom_capture_utils.ZoomTestData(
67          result_zoom=4,
68          radius_tol=_RADIUS_RTOL,
69          offset_tol=_OFFSET_RTOL,
70          focal_length=_FOCAL_LENGTH,
71          aruco_corners=_generate_aruco_markers(_ARUCO_MARKER_CENTER, 40),
72          aruco_offset=40
73      ),
74  ]
75
76
77class ZoomCaptureUtilsTest(unittest.TestCase):
78  """Unit tests for this module."""
79
80  def setUp(self):
81    super().setUp()
82    self.zoom_results = _generate_valid_zoom_results()
83
84  def test_verify_zoom_results_enough_zoom_data(self):
85    self.assertTrue(
86        zoom_capture_utils.verify_zoom_results(
87            self.zoom_results, _IMG_SIZE, 4, 1
88        )
89    )
90
91  def test_verify_zoom_results_not_enough_zoom(self):
92    self.assertFalse(
93        zoom_capture_utils.verify_zoom_results(
94            self.zoom_results, _IMG_SIZE, 5, 1
95        )
96    )
97
98  def test_verify_zoom_results_wrong_zoom(self):
99    self.zoom_results[-1].result_zoom = 5
100    self.assertFalse(
101        zoom_capture_utils.verify_zoom_results(
102            self.zoom_results, _IMG_SIZE, 4, 1
103        )
104    )
105
106  def test_verify_zoom_results_wrong_offset(self):
107    self.zoom_results[-1].aruco_offset = _WRONG_ARUCO_OFFSET
108    self.assertFalse(
109        zoom_capture_utils.verify_zoom_results(
110            self.zoom_results, _IMG_SIZE, 4, 1
111        )
112    )
113
114
115if __name__ == '__main__':
116  unittest.main()
117