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 unittest
17import unittest.mock
18
19import numpy
20
21import image_processing_utils
22import its_session_utils
23
24
25# intensity values of Y-plane array from ITS testing
26_EMPIRICAL_TABLET_OFF_BRIGHTNESS = 0.11
27_PLACEHOLDER_SCENE = 'its_session_utils_unittest_scene'
28_TEST_IMG_W = 640
29_TEST_IMG_H = 480
30
31
32def _generate_test_image(brightness):
33  """Creates a Y plane array with pixel values of brightness.
34
35  Args:
36    brightness: float between [0.0, 1.0]
37
38  Returns:
39    test_image: Y plane array with elements of value brightness.
40  """
41  test_image = numpy.zeros((_TEST_IMG_W, _TEST_IMG_H, 1), dtype=float)
42  test_image.fill(brightness)
43  return test_image
44
45
46class ItsSessionUtilsTests(unittest.TestCase):
47  """Run a suite of unit tests on this module."""
48
49  def setUp(self):
50    super().setUp()
51    self.test_image_zero = _generate_test_image(0.0)
52    self.test_image_half = _generate_test_image(0.5)
53    self.test_image_one = _generate_test_image(1.0)
54    self.addCleanup(unittest.mock.patch.stopall)
55    unittest.mock.patch.object(
56        image_processing_utils, 'write_image', autospec=True).start()
57
58  def test_validate_lighting_state_on_at_zero(self):
59    with self.assertRaises(AssertionError):
60      its_session_utils.validate_lighting(
61          self.test_image_zero, _PLACEHOLDER_SCENE)
62
63  def test_validate_lighting_state_on_at_half(self):
64    its_session_utils.validate_lighting(
65        self.test_image_half, _PLACEHOLDER_SCENE)
66
67  def test_validate_lighting_state_on_at_one(self):
68    its_session_utils.validate_lighting(
69        self.test_image_one, _PLACEHOLDER_SCENE)
70
71  def test_validate_lighting_state_off_at_zero(self):
72    its_session_utils.validate_lighting(
73        self.test_image_zero, _PLACEHOLDER_SCENE, state='OFF')
74
75  def test_validate_lighting_state_off_at_half(self):
76    with self.assertRaises(AssertionError):
77      its_session_utils.validate_lighting(
78          self.test_image_half, _PLACEHOLDER_SCENE, state='OFF')
79
80  def test_validate_lighting_state_off_at_one(self):
81    with self.assertRaises(AssertionError):
82      its_session_utils.validate_lighting(
83          self.test_image_one, _PLACEHOLDER_SCENE, state='OFF')
84
85  def test_validate_lighting_state_off_tablet_off_handles_noise(self):
86    test_image_tablet_off = _generate_test_image(
87        _EMPIRICAL_TABLET_OFF_BRIGHTNESS)
88    its_session_utils.validate_lighting(test_image_tablet_off,
89                                        _PLACEHOLDER_SCENE,
90                                        state='OFF',
91                                        tablet_state='OFF')
92
93
94if __name__ == '__main__':
95  unittest.main()
96