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 image_fov_utils.""" 15 16import math 17import unittest 18 19import image_fov_utils 20 21 22class ImageFovUtilsTest(unittest.TestCase): 23 """Unit tests for this module.""" 24 25 def test_calc_expected_circle_image_ratio(self): 26 """Unit test for calc_expected_circle_image_ratio. 27 28 Test by using 5% area circle in VGA cropped to nHD format 29 """ 30 ref_fov = {'w': 640, 'h': 480, 'percent': 5} 31 # nHD format cut down 32 img_w, img_h = 640, 360 33 nhd = image_fov_utils.calc_expected_circle_image_ratio( 34 ref_fov, img_w, img_h) 35 self.assertTrue(math.isclose(nhd, 5*480/360, abs_tol=0.01)) 36 37 def test_check_ar(self): 38 """Unit test for aspect ratio check.""" 39 # Circle true 40 circle = {'w': 1, 'h': 1} 41 ar_gt = 1.0 42 w, h = 640, 480 43 e_msg_stem = 'check_ar_true' 44 e_msg = image_fov_utils.check_ar(circle, ar_gt, w, h, e_msg_stem) 45 self.assertIsNone(e_msg) 46 47 # Circle false 48 circle = {'w': 2, 'h': 1} 49 e_msg_stem = 'check_ar_false' 50 e_msg = image_fov_utils.check_ar(circle, ar_gt, w, h, e_msg_stem) 51 self.assertIn('check_ar_false', e_msg) 52 53 def test_check_crop(self): 54 """Unit test for crop check.""" 55 # Crop true 56 circle = {'w': 100, 'h': 100, 'x_offset': 1, 'y_offset': 1} 57 cc_gt = {'hori': 1.0, 'vert': 1.0} 58 w, h = 640, 480 59 e_msg_stem = 'check_crop_true' 60 crop_thresh_factor = 1 61 e_msg = image_fov_utils.check_crop(circle, cc_gt, w, h, 62 e_msg_stem, crop_thresh_factor) 63 self.assertIsNone(e_msg) 64 65 # Crop false 66 circle = {'w': 100, 'h': 100, 'x_offset': 2, 'y_offset': 1} 67 e_msg_stem = 'check_crop_false' 68 e_msg = image_fov_utils.check_crop(circle, cc_gt, w, h, 69 e_msg_stem, crop_thresh_factor) 70 self.assertIn('check_crop_false', e_msg) 71 72if __name__ == '__main__': 73 unittest.main() 74 75