test_timer.py (9407B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\control\test\test_timer.py 2 from datetime import datetime 3 from eve.client.script.ui.control.timer import CountdownTimer 4 from gametime import SEC 5 from mock import Mock 6 from testsuites.testcases import ClientTestCase 7 8 class TestTimer(ClientTestCase): 9 10 def test_calculate_seconds_left_can_show_more_than_1_day(self): 11 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 12 time_to_countdown_to = datetime(day=23, month=11, year=2024, hour=17, minute=0, second=0) 13 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 14 timer._get_now = lambda : time_now 15 actual_seconds_left = timer._calculate_seconds_left() 16 expected_seconds_left = (86400 + 3600 + 900) * SEC 17 self.assertEqual(actual_seconds_left, expected_seconds_left) 18 19 def test_calculate_seconds_left_can_show_between_1_hour_and_1_day(self): 20 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 21 time_to_countdown_to = datetime(day=22, month=11, year=2024, hour=17, minute=0, second=0) 22 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 23 timer._get_now = lambda : time_now 24 actual_seconds_left = timer._calculate_seconds_left() 25 expected_seconds_left = (3600 + 900) * SEC 26 self.assertEqual(actual_seconds_left, expected_seconds_left) 27 28 def test_calculate_seconds_left_can_show_between_1_minute_and_1_hour(self): 29 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 30 time_to_countdown_to = datetime(day=22, month=11, year=2024, hour=16, minute=0, second=0) 31 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 32 timer._get_now = lambda : time_now 33 actual_seconds_left = timer._calculate_seconds_left() 34 expected_seconds_left = 900 * SEC 35 self.assertEqual(actual_seconds_left, expected_seconds_left) 36 37 def test_calculate_seconds_left_can_show_between_1_second_and_1_minute(self): 38 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 39 time_to_countdown_to = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=10) 40 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 41 timer._get_now = lambda : time_now 42 actual_seconds_left = timer._calculate_seconds_left() 43 expected_seconds_left = 10 * SEC 44 self.assertEqual(actual_seconds_left, expected_seconds_left) 45 46 def test_calculate_seconds_left_supports_negatives(self): 47 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 48 time_to_countdown_to = datetime(day=22, month=11, year=2024, hour=15, minute=44, second=0) 49 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 50 timer._get_now = lambda : time_now 51 actual_seconds_left = timer._calculate_seconds_left() 52 expected_seconds_left = -60 * SEC 53 self.assertEqual(actual_seconds_left, expected_seconds_left) 54 55 def test_timer_updates_stop_timer_if_fetching_returns_none(self): 56 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 57 time_to_countdown_to = None 58 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 59 timer._get_now = lambda : time_now 60 timer.stop_timer = Mock(wraps=timer.stop_timer) 61 timer._update() 62 timer.stop_timer.assert_called_once_with() 63 64 def test_timer_updates_stop_timer_if_fetching_returns_negative_over_one_year(self): 65 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 66 time_to_countdown_to = datetime(day=22, month=11, year=2022, hour=15, minute=45, second=0) 67 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 68 timer._get_now = lambda : time_now 69 timer.stop_timer = Mock(wraps=timer.stop_timer) 70 timer._update() 71 timer.stop_timer.assert_called_once_with() 72 73 def test_timer_stops_timer_if_fetching_returns_negative_between_two_minutes_and_one_year(self): 74 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 75 time_to_countdown_to = datetime(day=22, month=10, year=2024, hour=15, minute=45, second=0) 76 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 77 timer._get_now = lambda : time_now 78 timer.stop_timer = Mock(wraps=timer.stop_timer) 79 timer._update() 80 timer.stop_timer.assert_called_once_with() 81 82 def test_timer_does_not_stop_timer_if_fetching_returns_negative_under_two_minutes(self): 83 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=44, second=0) 84 time_to_countdown_to = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 85 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 86 timer._get_now = lambda : time_now 87 timer.stop_timer = Mock(wraps=timer.stop_timer) 88 timer._update() 89 self.assertFalse(timer.stop_timer.called) 90 91 def test_timer_interval_is_increased_if_fetching_returns_negative_under_two_minutes(self): 92 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 93 time_to_countdown_to = datetime(day=22, month=11, year=2024, hour=15, minute=44, second=0) 94 timer = CountdownTimer(fetch_timestamp_to_countdown_to=lambda : time_to_countdown_to) 95 timer._get_now = lambda : time_now 96 timer._increase_timer_interval = Mock(wraps=timer._increase_timer_interval) 97 timer.stop_timer = Mock(wraps=timer.stop_timer) 98 timer._update() 99 timer._increase_timer_interval.assert_called_once_with() 100 self.assertFalse(timer.stop_timer.called) 101 102 def test_fetching_attempts_do_not_exceed_maximum_when_fetching_returns_negative_under_two_minutes(self): 103 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 104 time_to_countdown_to = datetime(day=22, month=11, year=2024, hour=15, minute=44, second=0) 105 fetch_timestamp_to_countdown_to = Mock(return_value=time_to_countdown_to) 106 timer = CountdownTimer(fetch_timestamp_to_countdown_to=fetch_timestamp_to_countdown_to, msecs_between_updates=1, max_fetching_attempts=5) 107 timer._get_now = lambda : time_now 108 timer._reset_timestamp_to_countdown_to = Mock(wraps=timer._reset_timestamp_to_countdown_to) 109 timer._increase_timer_interval = Mock(wraps=timer._increase_timer_interval) 110 self.is_timer_running = True 111 112 def stop_timer(*args, **kwargs): 113 self.is_timer_running = False 114 115 timer.stop_timer = stop_timer 116 while self.is_timer_running: 117 timer._update() 118 119 self.assertEqual(fetch_timestamp_to_countdown_to.call_count, 5) 120 self.assertEqual(timer._increase_timer_interval.call_count, 5) 121 self.assertEqual(timer._increase_timer_interval.call_count, 5) 122 123 def test_fetching_attempts_only_fetch_once_when_fetching_returns_positive(self): 124 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 125 time_to_countdown_to = datetime(day=22, month=11, year=2024, hour=15, minute=46, second=0) 126 fetch_timestamp_to_countdown_to = Mock(return_value=time_to_countdown_to) 127 timer = CountdownTimer(fetch_timestamp_to_countdown_to=fetch_timestamp_to_countdown_to, msecs_between_updates=1, max_fetching_attempts=5) 128 timer._get_now = lambda : time_now 129 timer._reset_timestamp_to_countdown_to = Mock(wraps=timer._reset_timestamp_to_countdown_to) 130 timer._increase_timer_interval = Mock(wraps=timer._increase_timer_interval) 131 timer._update() 132 timer._update() 133 self.assertEqual(fetch_timestamp_to_countdown_to.call_count, 1) 134 self.assertEqual(timer._reset_timestamp_to_countdown_to.call_count, 0) 135 self.assertEqual(timer._increase_timer_interval.call_count, 0) 136 137 def test_fetching_attempts_only_fetch_once_when_fetching_returns_negative_over_two_minutes(self): 138 time_now = datetime(day=22, month=11, year=2024, hour=15, minute=48, second=0) 139 time_to_countdown_to = datetime(day=22, month=11, year=2024, hour=15, minute=45, second=0) 140 fetch_timestamp_to_countdown_to = Mock(return_value=time_to_countdown_to) 141 timer = CountdownTimer(fetch_timestamp_to_countdown_to=fetch_timestamp_to_countdown_to, msecs_between_updates=1, max_fetching_attempts=5) 142 timer._get_now = lambda : time_now 143 timer._reset_timestamp_to_countdown_to = Mock(wraps=timer._reset_timestamp_to_countdown_to) 144 timer._increase_timer_interval = Mock(wraps=timer._increase_timer_interval) 145 self.is_timer_running = True 146 147 def stop_timer(*args, **kwargs): 148 self.is_timer_running = False 149 150 timer.stop_timer = stop_timer 151 while self.is_timer_running: 152 timer._update() 153 154 self.assertEqual(fetch_timestamp_to_countdown_to.call_count, 1) 155 self.assertEqual(timer._reset_timestamp_to_countdown_to.call_count, 0) 156 self.assertEqual(timer._increase_timer_interval.call_count, 0)