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
async def test_mainmenu_autostart_timeout(ovshell: testing.OpenVarioShellStub, nosleep: None) -> None: # GIVEN urwid_mock = UrwidMock() app = MockApp() ovshell.apps.stub_add_app("mockapp", app, MockExtension()) ovshell.settings.set("ovshell.autostart_app", "mockapp") ovshell.settings.set("ovshell.autostart_timeout", 3) act = MainMenuActivity(ovshell) w = act.create() # WHEN act.activate() await asyncio.sleep(0) await asyncio.sleep(0) # THEN rendered = urwid_mock.render(w) assert "Starting Mock App in" in rendered assert "Press any button to cancel" in rendered # Let the countdown finish assert act.autostart_countdown_task is not None await act.autostart_countdown_task rendered = urwid_mock.render(w) assert "Press any button to cancel" not in rendered assert app.launched
def test_switch_orientation(self, ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() step = setupapp.OrientationWizardStep(ovshell) root = Path(ovshell.os.path("//")) root.joinpath("boot").mkdir() root.joinpath("sys", "class", "graphics", "fbcon").mkdir(parents=True) with open(root / "boot" / "config.uEnv", "w") as f: f.write("rotation=0") rendered = urwid_mock.render(step) assert "Skip" in rendered assert "Landscape" in rendered assert "Portrait" in rendered # WHEN urwid_mock.keypress(step, ["down", "down", "enter"]) # THEN with open(ovshell.os.path("//sys/class/graphics/fbcon/rotate_all"), "r") as f: writtenrot = f.read() assert writtenrot == "3" setting = ovshell.settings.getstrict("core.screen_orientation", str) assert setting == "1"
async def test_restore_cancel(ovshell: testing.OpenVarioShellStub) -> None: urwid_mock = UrwidMock() rsync = RsyncRunnerStub() rsync.progress = [ RsyncStatusLine(0, 0, "0 KB/s", "00:01:00", None), RsyncStatusLine(2000, 23, "15 KB/s", "01:28:20", None), RsyncStatusLine(12000, 85, "12 KB/s", "03:29:12", None), ] act = RestoreActivity(ovshell, rsync, "/backup_src", "/backup_dest") ovshell.screen.push_activity(act) w = act.create() act.activate() await asyncio.sleep(0) assert "0% | 0.0 B | 0 KB/s | 00:01:00" in urwid_mock.render(w) await asyncio.sleep(0) # press the cancel button assert "Cancel" in urwid_mock.render(w) urwid_mock.keypress(w, ["enter"]) await asyncio.sleep(0) assert "Restore was cancelled." in urwid_mock.render(w) # close the activity assert "Close" in urwid_mock.render(w) urwid_mock.keypress(w, ["enter"]) assert ovshell.screen.stub_top_activity() is None
async def test_activity_fs_mounting( activity_testbed: LogDownloaderActivityTestbed, ) -> None: urwid_mock = UrwidMock() w = activity_testbed.activity.create() activity_testbed.activity.activate() await asyncio.sleep(0) assert "Please insert USB storage" in urwid_mock.render(w) activity_testbed.mountwatcher.stub_device_in() assert "Mounting USB storage..." in urwid_mock.render(w) activity_testbed.mountwatcher.stub_device_out() assert "Please insert USB storage" in urwid_mock.render(w)
def test_activity_initial_view(ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() act = CheckForUpdatesActivity(ovshell, OpkgToolsStub()) # WHEN wdg = act.create() view = urwid_mock.render(wdg) # THEN assert "System update" in view
def test_set_indicator_simple() -> None: urwid_mock = UrwidMock() mainloop = mock.Mock(urwid.MainLoop) screen = ScreenManagerImpl(mainloop) screen.set_indicator("test", "Hello World", api.IndicatorLocation.LEFT, 0) view = urwid_mock.render(mainloop.widget) assert "Hello World" in view screen.remove_indicator("test") view = urwid_mock.render(mainloop.widget) assert "Hello World" not in view
def test_push_activity() -> None: urwid_mock = UrwidMock() mainloop = mock.Mock(name="MainLoop") screen = ScreenManagerImpl(mainloop) act = ActivityStub("Stub Activity") # WHEN screen.push_activity(act) # THEN act.activated == 1 view = urwid_mock.render(mainloop.widget) assert "Stub Activity" in view
def test_mainmenu_start_initial(ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() act = MainMenuActivity(ovshell) # WHEN w = act.create() # THEN rendered = urwid_mock.render(w) assert "Main Menu" in rendered assert "Applications" in rendered assert "Settings" in rendered
async def test_restore_failure(ovshell: testing.OpenVarioShellStub) -> None: urwid_mock = UrwidMock() rsync = RsyncRunnerStub() rsync.progress = [ RsyncStatusLine(0, 0, "0 KB/s", "00:01:00", None), RsyncFailedException(255, "Expected failure"), ] act = RestoreActivity(ovshell, rsync, "/backup_src", "/backup_dest") ovshell.screen.push_activity(act) w = act.create() act.activate() await asyncio.sleep(0) assert "0% | 0.0 B | 0 KB/s | 00:01:00" in urwid_mock.render(w) await asyncio.sleep(0) # rsync should fail at this point assert "Restore has failed (error code: 255)." in urwid_mock.render(w) assert "Expected failure" in urwid_mock.render(w) # close the activity assert "Close" in urwid_mock.render(w) urwid_mock.keypress(w, ["enter"]) assert ovshell.screen.stub_top_activity() is None
async def test_backup_act_create(ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() rsync = RsyncRunnerStub() act = BackupActivity(ovshell, rsync, "/backup_src", "/backup_dest") # WHEN w = act.create() # THEN rendered = urwid_mock.render(w) assert "Backup" in rendered assert "Copying files from Openvario to USB stick..." in rendered assert "Cancel" in rendered
async def test_activity_no_updates(ovshell: testing.OpenVarioShellStub) -> None: urwid_mock = UrwidMock() act = CheckForUpdatesActivity(ovshell, OpkgToolsStub()) ovshell.screen.push_activity(act) wdg = act.create() # First render starts the command view = urwid_mock.render(wdg) assert "System update" in view assert "Checking for updates..." in view # Second render shows the output await asyncio.sleep(0.1) view = urwid_mock.render(wdg) assert "Checking for updates..." in view # Fourth render renders the finished command view = urwid_mock.render(wdg) await ovshell.screen.stub_wait_for_tasks(act) view = urwid_mock.render(wdg) assert "No updates found" in view assert "Exit" in view urwid_mock.keypress(wdg, ["enter"]) assert ovshell.screen.stub_top_activity() is None
async def _until( wdg: urwid.Widget, predicate: Callable[[str], bool], timeout: float = 0.05 ) -> str: urwid_mock = UrwidMock() step = 0.01 view = urwid_mock.render(wdg) time = timeout while not predicate(view): await asyncio.sleep(step) time -= step if time < 0: raise TimeoutError() view = urwid_mock.render(wdg) return view
def test_set_indicator_ordering() -> None: # GIVEN urwid_mock = UrwidMock() mainloop = mock.Mock(urwid.MainLoop) screen = ScreenManagerImpl(mainloop) screen.set_indicator("3", "Three", api.IndicatorLocation.RIGHT, 3) screen.set_indicator("1", "One", api.IndicatorLocation.RIGHT, 1) screen.set_indicator("2", ["T", "w", "o"], api.IndicatorLocation.RIGHT, 2) # WHEN view = urwid_mock.render(mainloop.widget) # THEN assert "One Two Three" in view
async def test_calibrate(self, ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() step = setupapp.CalibrateTouchWizardStep(ovshell) urwid.connect_signal(step, "next", self._completed) # WHEN urwid_mock.keypress(step, ["right", "enter"]) topact = ovshell.screen.stub_top_activity() assert isinstance(topact, setupapp.CommandRunnerActivity) topact.create() topact.activate() await ovshell.screen.stub_wait_for_tasks(topact) assert self.completed is True
async def test_success(self, ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() act = setupapp.CommandRunnerActivity(ovshell, "Test", "Running test", "cmd", []) ovshell.screen.push_activity(act) w = act.create() assert "Test" in urwid_mock.render(w) assert "Running test" in urwid_mock.render(w) # WHEN act.activate() await ovshell.screen.stub_wait_for_tasks(act) # THEN assert ovshell.screen.stub_top_activity() is None
async def test_activity_backup(ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() mountwatcher = AutomountWatcherStub() rsync = RsyncRunnerStub() backupdir = BackupDirectoryStub() act = BackupRestoreMainActivity(ovshell, mountwatcher, rsync, backupdir) w = act.create() act.activate() mountwatcher.stub_mount() # WHEN # Press the default button (Backup) assert "Backup" in urwid_mock.render(w) urwid_mock.keypress(w, ["enter"]) # THEN pact = ovshell.screen.stub_top_activity() assert isinstance(pact, BackupActivity)
def test_PackageSelectionWidget_select_some( ovshell: testing.OpenVarioShellStub, ) -> None: # GIVEN urwid_mock = UrwidMock() packages = [ UpgradablePackage("package-one", "1", "1.1"), UpgradablePackage("package-two", "2.4", "4.3"), ] wdg = PackageSelectionWidget(packages, ovshell.screen) # WHEN # select and deselect package-one, then select package-two urwid_mock.keypress(wdg, ["down", "enter", "enter", "down", "enter"]) # THEN view = urwid_mock.render(wdg) assert "[ ] package-one" in view assert "[X] package-two" in view assert wdg.selected == {"package-two"}
def test_push_modal() -> None: urwid_mock = UrwidMock() 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"), api.ModalOptions(align="center", width=20, valign="top", height=3), ) # THEN # Modal activity does not obscure the main view view = urwid_mock.render(mainloop.widget) assert "Modal Activity" in view assert "Activity One" in view
async def test_activity_list_files( activity_testbed: LogDownloaderActivityTestbed, ) -> None: # GIVEN urwid_mock = UrwidMock() activity_testbed.downloader.stub_files = [ FileInfo("two.igc", ".igc", size=20000, mtime=0, downloaded=False) ] w = activity_testbed.activity.create() activity_testbed.activity.activate() # Let mount watcher detect start await asyncio.sleep(0) assert activity_testbed.mountwatcher.stub_running activity_testbed.mountwatcher.stub_mount() # WHEN rendered = urwid_mock.render(w) # THEN assert "two.igc" in rendered
async def test_PackageSelectionWidget_nothing_selected( ovshell: testing.OpenVarioShellStub, ) -> None: # GIVEN urwid_mock = UrwidMock() packages = [ UpgradablePackage("package-one", "1", "1.1"), UpgradablePackage("package-two", "2.4", "4.3"), ] wdg = PackageSelectionWidget(packages, ovshell.screen) assert "Upgrade" in urwid_mock.render(wdg) # WHEN # Press "Upgrade" button without selecting anything urwid_mock.keypress(wdg, ["right", "enter"]) # THEN dialog = ovshell.screen.stub_dialog() assert dialog is not None assert dialog.title == "Nothing to upgrade" assert dialog.content.text == "Please select one or more packages to upgrade."
async def test_activity_mounting(ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() mountwatcher = AutomountWatcherStub() rsync = RsyncRunnerStub() backupdir = BackupDirectoryStub() act = BackupRestoreMainActivity(ovshell, mountwatcher, rsync, backupdir) w = act.create() act.activate() act.show() await asyncio.sleep(0) assert "Please insert USB storage" in urwid_mock.render(w) # WHEN mountwatcher.stub_mount() await asyncio.sleep(0) # THEN assert "This app allows to copy files to and from USB stick" in urwid_mock.render(w) assert "No files to restore." in urwid_mock.render(w) # WHEN backupdir.backed_up_files = ["file_one", "file_two"] act.show() assert "file_one" in urwid_mock.render(w) assert "file_two" in urwid_mock.render(w)
def test_mainmenu_exit(ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() act = MainMenuActivity(ovshell) w = act.create() # WHEN urwid_mock.keypress(w, ["esc"]) # THEN quitdialog = ovshell.screen.stub_dialog() assert quitdialog is not None assert quitdialog.title == "Shut Down?" # Perform the shut down closed = quitdialog.stub_press_button("Shut Down") assert not closed finalact = ovshell.screen.stub_top_activity() assert finalact is not None rendered = urwid_mock.render(finalact.create()) assert "Openvario shutting down..." in rendered assert "OS: Shut down" in ovshell.get_stub_log()
async def test_failure(self, ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() act = setupapp.CommandRunnerActivity(ovshell, "Test", "Running test", "cmd", []) act.on_failure(self._completed) ovshell.screen.push_activity(act) act.create() ovshell.os.stub_expect_run(2, stderr=b"Error happened") # WHEN act.activate() await ovshell.screen.stub_wait_for_tasks(act) # THEN dialog = ovshell.screen.stub_dialog() assert dialog is not None assert dialog.title == "Test" assert "Error happened" in urwid_mock.render(dialog.content) closed = dialog.stub_press_button("Close") assert closed assert self.completed
def setup_activity(self, ovshell: testing.OpenVarioShellStub) -> None: self.ovshell = ovshell self.manager = ConnmanManagerStub() self.activity = ConnmanManagerActivity(ovshell, self.manager) self.urwid = UrwidMock() self.wifi_tech = ConnmanTechnology( path="/tech-path", name="WiFi", type="wifi", connected=False, powered=False, ) self.svc = ConnmanService( path="/svc-path", auto_connect=False, favorite=False, name="Service One", security=["wps"], state=ConnmanServiceState.IDLE, strength=32, type="wifi", )
async def test_activity_change_settings( activity_testbed: LogDownloaderActivityTestbed, ) -> None: # GIVEN urwid_mock = UrwidMock() # Set initial settings activity_testbed.ovshell.settings.set("fileman.download_logs.filter", { "new": True, "igc": True, "nmea": True }) w = activity_testbed.activity.create() activity_testbed.activity.activate() activity_testbed.mountwatcher.stub_mount() await asyncio.sleep(0) # WHEN # Turn off all settings urwid_mock.keypress(w, ["enter", "right", "enter", "right", "enter"]) # THEN filt = activity_testbed.ovshell.settings.get( "fileman.download_logs.filter", dict) assert filt == {"new": False, "igc": False, "nmea": False}
async def test_wizard_diplay(self, ovshell: testing.OpenVarioShellStub) -> None: # GIVEN urwid_mock = UrwidMock() sys_info_stub = SystemInfoStub() sys_info_stub.pkg_versions = {"xcsoar-testing": "7.0.0-preview15"} act = aboutapp.AboutActivity(ovshell) act.sys_info = sys_info_stub w = act.create() act.activate() rendered = urwid_mock.render(w) assert "About Openvario" in rendered assert "..." in rendered # versions are not populated initially # WHEN # wait for all versions to be fetched await ovshell.screen.stub_wait_for_tasks(act) rendered = urwid_mock.render(w) assert "..." not in rendered assert "N/A" in rendered assert "7.0.0-preview15" in rendered assert "00000 (stub)" in rendered
def test_push_dialog() -> None: # GIVEN urwid_mock = UrwidMock() 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 = urwid_mock.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) urwid_mock.keypress(mainloop.widget, ["enter"]) # THEN # Dialog did not close assert "Dialog Title" in urwid_mock.render(mainloop.widget) # WHEN # Click the "close" button urwid_mock.keypress(mainloop.widget, ["right", "enter"]) # THEN # Dialog closed assert "Dialog Title" not in urwid_mock.render(mainloop.widget)
async def test_password_input(self, ovshell: testing.OpenVarioShellStub) -> None: urwid_mock = UrwidMock() fields = {"Passphrase": {"Type": "psk", "Requirement": "mandatory"}} act = ConnmanInputActivity(ovshell.screen, self.service, fields) ovshell.screen.push_activity(act) wdg = act.create() rendered = urwid_mock.render(wdg) assert "Sample Service" in rendered assert "Passphrase" in rendered assert "Confirm" in rendered # Enter a password urwid_mock.keypress(wdg, ["5", "e", "c", "r", "e", "t"]) urwid_mock.keypress(wdg, ["down", "enter"]) res = await act.done assert res == {"Passphrase": "5ecret"}
async def test_activity_download( activity_testbed: LogDownloaderActivityTestbed, ) -> None: # GIVEN urwid_mock = UrwidMock() activity_testbed.downloader.stub_files = [ FileInfo("two.igc", ".igc", size=20000, mtime=0, downloaded=False) ] w = activity_testbed.activity.create() activity_testbed.activity.activate() activity_testbed.mountwatcher.stub_mount() # WHEN urwid_mock.keypress(w, ["down", "enter"]) # THEN await asyncio.sleep(0) assert "0 %" in urwid_mock.render(w) await asyncio.sleep(0) assert "50 %" in urwid_mock.render(w) await asyncio.sleep(0) assert "100 %" in urwid_mock.render(w) await asyncio.sleep(0) assert "Done" in urwid_mock.render(w)