gtk: Add the xfstestsdb gtk command

This command doesn't do much at the moment, it simply sets up the Gtk
Application and then exits. The actual UI will be built up over the next
several patches.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-07-28 14:06:08 -04:00
parent bea49c5eae
commit 62bf603ca4
7 changed files with 134 additions and 1 deletions

2
tests/gtk/__init__.py Normal file
View File

@ -0,0 +1,2 @@
# Copyright 2023 (c) Anna Schumaker.
"""Test package for the xfstestsdb gtk application."""

19
tests/gtk/test_gsetup.py Normal file
View File

@ -0,0 +1,19 @@
# Copyright 2023 (c) Anna Schumaker.
"""Tests our Gtk Setup."""
import gi
import unittest
import xfstestsdb.gtk.gsetup
class TestGSetup(unittest.TestCase):
"""Test our Gtk setup."""
def test_require_version(self):
"""Check that we have called gi.require_version()."""
self.assertEqual(gi.get_required_version("Adw"), "1")
def test_constants(self):
"""Check that constants are configured correctly."""
self.assertEqual(xfstestsdb.gtk.gsetup.DEBUG_STR, "-debug")
self.assertEqual(xfstestsdb.gtk.gsetup.APPLICATION_ID,
"com.nowheycreamery.xfstestsdb.gtk-debug")

61
tests/test_gtk.py Normal file
View File

@ -0,0 +1,61 @@
# Copyright 2023 (c) Anna Schumaker.
"""Tests the `xfstestsdb gtk` command."""
import unittest
import unittest.mock
import xfstestsdb.gtk
from gi.repository import Adw
class TestApplication(unittest.TestCase):
"""Tests the Gtk Application."""
def setUp(self):
"""Set up common variables."""
self.application = xfstestsdb.gtk.Application()
def test_init(self):
"""Check that the Gtk Application was set up properly."""
self.assertIsInstance(self.application, Adw.Application)
self.assertEqual(self.application.get_application_id(),
xfstestsdb.gtk.gsetup.APPLICATION_ID)
@unittest.mock.patch("gi.repository.Adw.Application.do_startup")
def test_startup(self, mock_startup: unittest.mock.Mock):
"""Check that startup sets up our application instance correctly."""
self.application.emit("startup")
mock_startup.assert_called_with(self.application)
@unittest.mock.patch("gi.repository.Adw.Application.do_activate")
def test_activate(self, mock_activate: unittest.mock.Mock):
"""Check that activating our application works correctly."""
self.application.emit("activate")
mock_activate.assert_called_with(self.application)
@unittest.mock.patch("gi.repository.Adw.Application.do_shutdown")
def test_shutdown(self, mock_shutdown: unittest.mock.Mock):
"""Check that shutting down application cleans up global state."""
self.application.emit("shutdown")
mock_shutdown.assert_called_with(self.application)
class TestGtk(unittest.TestCase):
"""Tests the `xfstestsdb gtk` command."""
def setUp(self):
"""Set up common variables."""
self.xfstestsdb = xfstestsdb.Command()
self.gtk = self.xfstestsdb.commands["gtk"]
def test_init(self):
"""Check that the gtk command was set up properly."""
self.assertIsInstance(self.gtk, xfstestsdb.commands.Command)
self.assertIsInstance(self.gtk, xfstestsdb.gtk.Command)
self.assertEqual(self.xfstestsdb.subparser.choices["gtk"],
self.gtk.parser)
@unittest.mock.patch("xfstestsdb.gtk.Application")
def test_command(self, mock_app: unittest.mock.Mock):
"""Check that running `xfstestsdb gtk` creates an Application."""
self.xfstestsdb.run(["gtk"])
mock_app.assert_called()
mock_app.return_value.run.assert_called_with([])

View File

@ -32,7 +32,7 @@ class TestXfstestsdb(unittest.TestCase):
self.xfstestsdb.run([])
self.assertRegex(mock_stdout.getvalue(),
r"^usage: pytest \[\-h\] \[\-\-version\]"
r"\s+\{.*?} ...$")
r"\s+\{.*?}\s+...$")
def test_run(self):
"""Test running the xfstestsdb."""

View File

@ -4,6 +4,7 @@ import argparse
from . import sqlite
from . import delete
from . import gc
from . import gtk
from . import list
from . import new
from . import rename
@ -30,6 +31,7 @@ class Command:
self.sql = sqlite.Connection()
self.commands = {"delete": delete.Command(self.subparser, self.sql),
"gc": gc.Command(self.subparser, self.sql),
"gtk": gtk.Command(self.subparser, self.sql),
"list": list.Command(self.subparser, self.sql),
"new": new.Command(self.subparser, self.sql),
"rename": rename.Command(self.subparser, self.sql),

View File

@ -0,0 +1,41 @@
# Copyright 2023 (c) Anna Schumaker.
"""The `xfstestsdb gtk` command."""
import argparse
from . import gsetup
from gi.repository import Adw
from .. import commands
from .. import sqlite
class Application(Adw.Application):
"""Our Adw.Application for displaying xfstests results."""
def __init__(self):
"""Initialize the application."""
super().__init__(application_id=gsetup.APPLICATION_ID)
def do_startup(self) -> None:
"""Handle the Adw.Application::startup signal."""
Adw.Application.do_startup(self)
def do_activate(self) -> None:
"""Handle the Adw.Application::activate signal."""
Adw.Application.do_activate(self)
def do_shutdown(self) -> None:
"""Handle the Adw.Application::shutdown signal."""
Adw.Application.do_shutdown(self)
class Command(commands.Command):
"""The `xfstestsdb gtk` command."""
def __init__(self, subparser: argparse.Action,
sql: sqlite.Connection) -> None:
"""Set up the gtk command."""
super().__init__(subparser, sql, "gtk", help="show a gtk-based ui")
def do_command(self, args: argparse.Namespace) -> None:
"""Run the Gtk UI."""
app_args = []
Application().run(app_args)

8
xfstestsdb/gtk/gsetup.py Normal file
View File

@ -0,0 +1,8 @@
# Copyright 2023 (c) Anna Schumaker.
"""Early setup of Gtk modules to avoid errors later."""
import gi
gi.require_version("Adw", "1")
DEBUG_STR = "-debug" if __debug__ else ""
APPLICATION_ID = f"com.nowheycreamery.xfstestsdb.gtk{DEBUG_STR}"