1import unittest
2
3import numpy as np
4
5from compare_scroll import compare
6
7
8class ScrollTests(unittest.TestCase):
9 def test_overlap_ignores_blank_rows_and_survives_a_changed_caret(self):
10 page = np.full((120, 20, 3), 255, dtype=np.uint8)
11 for y in range(0, len(page), 3):
12 page[y, 2:8] = [y, 40, 80]
13 before, after = page[:90].copy(), page[30:].copy()
14 after[20:25, 12] = 0
15 result = compare(before, after, 20)
16 self.assertEqual(result['scroll_y'], 30)
17 self.assertFalse(result['ambiguous'])
18 self.assertGreater(result['candidates'][0]['foreground_equal_fraction'], 0.95)
19
20 def test_repeated_foreground_has_no_unique_offset(self):
21 image = np.full((40, 20, 3), 255, dtype=np.uint8)
22 image[::4, 2:8] = 0
23 result = compare(image, image, 20)
24 self.assertTrue(result['ambiguous'])
25 self.assertIsNone(result['scroll_y'])
26
27 def test_blank_or_unrelated_images_cannot_establish_scroll(self):
28 white = np.full((40, 20, 3), 255, dtype=np.uint8)
29 with self.assertRaises(ValueError):
30 compare(white, white, 20)
31 before, after = white.copy(), white.copy()
32 before[:, 2:8] = [30, 40, 80]
33 after[:, 2:8] = [31, 40, 80]
34 with self.assertRaises(ValueError):
35 compare(before, after, 20)
36 with self.assertRaises(ValueError):
37 compare(before, after[:-1], 20)
38
39
40if __name__ == '__main__':
41 unittest.main()