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 its_session_utils.""" 15 16import logging 17import unittest 18 19import numpy 20 21import its_session_utils 22 23 24class ItsSessionUtilsTests(unittest.TestCase): 25 """Run a suite of unit tests on this module.""" 26 27 _BRIGHTNESS_CHECKS = (0.0, 28 its_session_utils._VALIDATE_LIGHTING_THRESH-0.01, 29 its_session_utils._VALIDATE_LIGHTING_THRESH, 30 its_session_utils._VALIDATE_LIGHTING_THRESH+0.01, 31 1.0) 32 _TEST_IMG_W = 640 33 _TEST_IMG_H = 480 34 35 def _generate_test_image(self, brightness): 36 """Creates a Y plane array with pixel values of brightness. 37 38 Args: 39 brightness: float between [0.0, 1.0] 40 41 Returns: 42 Y plane array with elements of value brightness 43 """ 44 test_image = numpy.zeros((self._TEST_IMG_W, self._TEST_IMG_H, 1), 45 dtype=float) 46 test_image.fill(brightness) 47 return test_image 48 49 def test_validate_lighting(self): 50 """Tests validate_lighting() works correctly.""" 51 # Run with different brightnesses to validate. 52 for brightness in self._BRIGHTNESS_CHECKS: 53 logging.debug('Testing validate_lighting with brightness %.1f', 54 brightness) 55 test_image = self._generate_test_image(brightness) 56 print(f'Testing brightness: {brightness}') 57 if brightness <= its_session_utils._VALIDATE_LIGHTING_THRESH: 58 self.assertRaises( 59 AssertionError, its_session_utils.validate_lighting, 60 test_image, 'unittest') 61 else: 62 self.assertTrue(its_session_utils.validate_lighting( 63 test_image, 'unittest'), f'image value {brightness} should PASS') 64 65 66if __name__ == '__main__': 67 unittest.main() 68