def test_refresher_view(self): text_cb = Mock() text_cb.return_value = "Test" monochrome_cb = Mock() monochrome_cb.return_value = Image.new("1", (128, 64)) color_cb = Mock() color_cb.return_value = Image.new("RGB", (128, 64)) view = RefresherView(text_cb, monochrome_cb, color_cb) i = get_mock_input() o = get_mock_output() r = Refresher(view, i, o) r.to_foreground() r.refresh() assert(text_cb.called) assert(o.display_data.called) assert(o.display_data.call_count == 2) o = get_mock_graphical_output() r = Refresher(view, i, o) r.to_foreground() r.refresh() assert(monochrome_cb.called) assert(o.display_image.called) assert(o.display_image.call_count == 2) o.type.append("color") r = Refresher(view, i, o) r.to_foreground() r.refresh() assert(color_cb.called) assert(o.display_image.called) assert(o.display_image.call_count == 4)
def test_update_keymap(self): """Tests whether the Refresher updates the keymap correctly.""" i = get_mock_input() o = get_mock_output() r = Refresher(lambda: "Hello", i, o, name=r_name, refresh_interval=0.1) r.refresh = lambda *args, **kwargs: None # We need to patch "process_callback" because otherwise the keymap callbacks # are wrapped and we can't test equivalence with patch.object(r, 'process_callback', side_effect=lambda keymap:keymap) as p: keymap1 = {"KEY_LEFT": lambda:1} r.update_keymap(keymap1) assert(r.keymap == keymap1) keymap2 = {"KEY_RIGHT": lambda:2} r.update_keymap(keymap2) keymap2.update(keymap1) assert(r.keymap == keymap2)
def test_keymap_restore_on_resume(self): """Tests whether the Refresher re-sets the keymap upon resume.""" i = get_mock_input() o = get_mock_output() r = Refresher(lambda: "Hello", i, o, name=r_name, refresh_interval=0.1) r.refresh = lambda *args, **kwargs: None r.to_foreground() assert i.set_keymap.called assert i.set_keymap.call_count == 1 assert i.set_keymap.call_args[0][0] == r.keymap r.pause() assert i.set_keymap.call_count == 1 #paused, so count shouldn't change i.set_keymap(None) assert i.set_keymap.call_args[0][0] != r.keymap r.resume() assert i.set_keymap.call_count == 3 #one explicitly done in the test right beforehand assert i.set_keymap.call_args[0][0] == r.keymap
def test_left_key_exits(self): r = Refresher(lambda: "Hello", get_mock_input(), get_mock_output(), name=r_name) r.refresh = lambda *args, **kwargs: None # This test doesn't actually test whether the Refresher exits # It only tests whether the in_foreground attribute is set # Any ideas? Maybe use some kind of "timeout" library? def scenario(): r.keymap["KEY_LEFT"]() assert not r.in_foreground # If the test fails, either the assert will trigger a test failure, # or the idle loop will just run indefinitely # The exception thrown should protect from the latter raise KeyboardInterrupt with patch.object(r, 'idle_loop', side_effect=scenario) as p: try: r.activate() except KeyboardInterrupt: pass #Test succeeded