def test_push_dialog() -> None: # GIVEN mainloop = mock.Mock(name="MainLoop") mainloop.screen._palette = {} screen = ScreenManagerImpl(mainloop) act1 = ActivityStub("Main Activity") screen.push_activity(act1) # WHEN dialog = screen.push_dialog("Dialog Title", urwid.Text("Dialog content")) dialog.add_button("Nevermind", lambda: False) dialog.add_button("Close", lambda: True) # THEN view = _render(mainloop.widget) assert "Dialog Title" in view assert "Dialog content" in view assert "Close" in view assert "Nevermind" in view # WHEN # Click the default button (Nevermind) _keypress(mainloop.widget, ["enter"]) # THEN # Dialog did not close assert "Dialog Title" in _render(mainloop.widget) # WHEN # Click the "close" button _keypress(mainloop.widget, ["right", "enter"]) # THEN # Dialog closed assert "Dialog Title" not in _render(mainloop.widget)
def test_push_activity() -> None: mainloop = mock.Mock(name="MainLoop") screen = ScreenManagerImpl(mainloop) act = ActivityStub("Stub Activity") # WHEN screen.push_activity(act) # THEN act.activated == 1 view = _render(mainloop.widget) assert "Stub Activity" in view
async def test_spawn_task() -> None: # GIVEN mainloop = mock.Mock(name="MainLoop") mainloop.screen._palette = {} screen = ScreenManagerImpl(mainloop) act1 = ActivityStub("Main Activity") screen.push_activity(act1) log: List[str] = [] async def infinite_loop() -> None: log.append("started") while True: try: await asyncio.sleep(0.1) except asyncio.CancelledError: log.append("cancelled") raise # WHEN screen.spawn_task(act1, infinite_loop()) await asyncio.sleep(0) # THEN assert log == ["started"] # WHEN screen.pop_activity() await asyncio.sleep(0) # THEN assert log == ["started", "cancelled"]
def run(argv) -> None: args = parser.parse_args(argv) palette = [ ("text", "white", "black", ""), ("edit", "black", "yellow", ""), ("btn focus", "white", "dark red", ""), ("btn normal", "white", "dark blue", ""), ("li normal", "light cyan", "black", ""), ("li focus", "white", "dark red", ""), ("pg normal", "white", "black", "standout"), ("pg inverted", "black", "light gray", "standout"), ("pg complete", "white", "dark magenta"), ("pg smooth", "dark magenta", "black"), ("screen header", "white", "brown", "standout"), ("screen header divider", "black", "brown", ""), ("bg", "light gray", "black", ""), ("success message", "light green", "black", ""), ("success banner", "white", "dark green", ""), ("error message", "light red", "black", ""), ("error banner", "yellow", "dark red", ""), ("progress", "light magenta", "black", ""), ("remark", "dark gray", "black", ""), ("highlight", "yellow", "black", ""), ("topbar", "black", "white", ""), # Indicator color scheme ("ind normal", "black", "white", ""), ("ind error", "dark red", "white", ""), ("ind warning", "brown", "white", ""), ("ind good", "dark green", "white", ""), # Light color scheme (for modals and popups) ("bg light", "black", "white", ""), ("li normal light", "dark blue", "white", ""), ("success message light", "dark green", "white", ""), ("error message light", "dark red", "white", ""), ("pg normal light", "white", "black", "standout"), ] asyncioloop = asyncio.get_event_loop() evl = urwid.AsyncioEventLoop(loop=asyncioloop) urwidloop = urwid.MainLoop( None, palette=palette, event_loop=evl, input_filter=debounce_esc, pop_ups=True, ) screen = ScreenManagerImpl(urwidloop) shell = OpenvarioShellImpl(screen, config=args.config, rootfs=args.sim) shell.extensions.load_all(shell) shell.apps.install_new_apps() asyncioloop.call_soon(startui, (shell, args.run)) try: urwidloop.run() except KeyboardInterrupt: pass # Clear the screen on exit print("\033[H\033[J", end="")
def test_set_indicator_ordering() -> None: # GIVEN mainloop = mock.Mock(urwid.MainLoop) screen = ScreenManagerImpl(mainloop) screen.set_indicator("3", "Three", protocol.IndicatorLocation.RIGHT, 3) screen.set_indicator("1", "One", protocol.IndicatorLocation.RIGHT, 1) screen.set_indicator("2", ["T", "w", "o"], protocol.IndicatorLocation.RIGHT, 2) # WHEN view = _render(mainloop.widget) # THEN assert "One Two Three" in view
def test_pop_activity() -> None: # GIVEN # Create a basic palette and an urwid main loop - we are messing with the internal # implementation of palette handling. urwid_mock = UrwidMock() palette = [ ("text", "white", "black", ""), ] asyncioloop = asyncio.get_event_loop() evl = urwid.AsyncioEventLoop(loop=asyncioloop) mainloop = urwid.MainLoop(None, palette=palette, event_loop=evl) screen = ScreenManagerImpl(mainloop) act1 = ActivityStub("Activity One") act2 = ActivityStub("Activity Two") screen.push_activity(act1) screen.push_activity(act2) assert "Activity Two" in urwid_mock.render(mainloop.widget) # WHEN screen.pop_activity() # THEN assert "Activity One" in urwid_mock.render(mainloop.widget) assert act2.activated == 1 assert act2.destroyed == 1 assert act2.shown == 1 assert act2.hidden == 1 assert act1.activated == 1 assert act1.shown == 2 assert act1.hidden == 1
def test_set_indicator_simple() -> None: mainloop = mock.Mock(urwid.MainLoop) screen = ScreenManagerImpl(mainloop) screen.set_indicator("test", "Hello World", protocol.IndicatorLocation.LEFT, 0) view = _render(mainloop.widget) assert "Hello World" in view screen.remove_indicator("test") view = _render(mainloop.widget) assert "Hello World" not in view
def test_push_modal() -> None: mainloop = mock.Mock(name="MainLoop") mainloop.screen._palette = {} screen = ScreenManagerImpl(mainloop) act1 = ActivityStub("Activity One") screen.push_activity(act1) # WHEN screen.push_modal( ActivityStub("Modal Activity"), protocol.ModalOptions(align="center", width=20, valign="top", height=3), ) # THEN # Modal activity does not obscure the main view view = _render(mainloop.widget) assert "Modal Activity" in view assert "Activity One" in view
def test_pop_activity() -> None: mainloop = mock.Mock(name="MainLoop") mainloop.screen._palette = {} screen = ScreenManagerImpl(mainloop) act1 = ActivityStub("Activity One") act2 = ActivityStub("Activity Two") screen.push_activity(act1) screen.push_activity(act2) assert "Activity Two" in _render(mainloop.widget) # WHEN screen.pop_activity() # THEN assert "Activity One" in _render(mainloop.widget) assert act2.activated == 1 assert act2.destroyed == 1 assert act2.shown == 1 assert act2.hidden == 1 assert act1.activated == 1 assert act1.shown == 2 assert act1.hidden == 1