Skip to content

Commit

Permalink
Add test coverage of new core app classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Nov 20, 2023
1 parent 39316be commit 4389855
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 12 deletions.
16 changes: 14 additions & 2 deletions core/src/toga/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ def _create_impl(self):
self.factory.WindowlessApp(interface=self)


class DocumentApp(App):
class DocumentApp(AbstractBaseApp):
def __init__(
self,
formal_name: str | None = None,
Expand All @@ -978,6 +978,19 @@ def __init__(
:param document_types: Initial :any:`document_types` mapping.
"""
######################################################################
# 2023-10: Backwards compatibility
######################################################################
if id is not None:
warn(
"App.id is deprecated and will be ignored. Use app_id instead",
DeprecationWarning,
stacklevel=2,
)
######################################################################
# End Backwards compatibility
######################################################################

if document_types is None:
raise ValueError("A document must manage at least one document type.")

Expand All @@ -995,7 +1008,6 @@ def __init__(
description=description,
startup=startup,
on_exit=on_exit,
id=id,
)

def _create_impl(self):
Expand Down
9 changes: 9 additions & 0 deletions core/tests/app/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def test_create(

metadata_mock.assert_called_once_with(expected_app_name)

# A normal app was created.
assert_action_performed(app, "create App")


@pytest.mark.parametrize(
"kwargs, exc_type, message",
Expand Down Expand Up @@ -289,6 +292,9 @@ def test_app_metadata(monkeypatch):
assert app.home_page == "https://example.com/test-app"
assert app.description == "A test app"

# A normal app was created.
assert_action_performed(app, "create App")


def test_explicit_app_metadata(monkeypatch):
"""App metadata can be provided explicitly, overriding module-level metadata"""
Expand Down Expand Up @@ -327,6 +333,9 @@ def test_explicit_app_metadata(monkeypatch):

assert app.on_exit._raw == on_exit_handler

# A normal app was created.
assert_action_performed(app, "create App")


@pytest.mark.parametrize("construct", [True, False])
def test_icon_construction(construct):
Expand Down
20 changes: 20 additions & 0 deletions core/tests/app/test_documentapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,23 @@ async def _do_close():

assert not asyncio.get_event_loop().run_until_complete(_do_close())
app.exit.assert_not_called()


def test_deprecated_args(monkeypatch):
"""Deprecated arguments to the DocumentApp constructor raise warnings."""
monkeypatch.setattr(sys, "argv", ["app-exe"])

# Range is deprecated
with pytest.warns(
DeprecationWarning,
match="App.id is deprecated and will be ignored. Use app_id instead",
):
app = toga.DocumentApp(
"Test App",
"org.beeware.document-app",
id="my-app",
document_types={"foobar": ExampleDocument},
)

assert app._impl.interface == app
assert_action_performed(app, "create DocumentApp")
29 changes: 29 additions & 0 deletions core/tests/app/test_simpleapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from unittest.mock import Mock

import toga
from toga_dummy.utils import assert_action_performed


def test_simple_app():
"""A SimpleApp could be created"""
on_exit_handler = Mock()

app = toga.SimpleApp(
formal_name="Test App",
app_id="org.example.test-app",
author="Jane Developer",
version="1.2.3",
home_page="https://example.com/test-app",
description="A test app",
on_exit=on_exit_handler,
)

assert app.author == "Jane Developer"
assert app.version == "1.2.3"
assert app.home_page == "https://example.com/test-app"
assert app.description == "A test app"

assert app.on_exit._raw == on_exit_handler

# A simple app was created.
assert_action_performed(app, "create Simple App")
29 changes: 29 additions & 0 deletions core/tests/app/test_windowlessapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from unittest.mock import Mock

import toga
from toga_dummy.utils import assert_action_performed


def test_windowless_app():
"""A WindowlessApp could be created"""
on_exit_handler = Mock()

app = toga.WindowlessApp(
formal_name="Test App",
app_id="org.example.test-app",
author="Jane Developer",
version="1.2.3",
home_page="https://example.com/test-app",
description="A test app",
on_exit=on_exit_handler,
)

assert app.author == "Jane Developer"
assert app.version == "1.2.3"
assert app.home_page == "https://example.com/test-app"
assert app.description == "A test app"

assert app.on_exit._raw == on_exit_handler

# A windowless app was created.
assert_action_performed(app, "create Windowless App")
32 changes: 23 additions & 9 deletions dummy/src/toga_dummy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MainWindow(Window):
pass


class App(LoggedObject):
class BaseApp(LoggedObject):
def __init__(self, interface):
super().__init__()
self.interface = interface
Expand All @@ -19,20 +19,13 @@ def __init__(self, interface):
self.loop = asyncio.get_event_loop()
self.create()

def create(self):
self._action("create App")
self.interface._startup()

def create_menus(self):
self._action("create App menus")

def main_loop(self):
print("Starting app using Dummy backend.")
self._action("main loop")

def set_main_window(self, window):
self._action("set_main_window", window=window)

def show_about_dialog(self):
self._action("show_about_dialog")

Expand Down Expand Up @@ -68,7 +61,28 @@ def simulate_exit(self):
self.interface.on_exit()


class DocumentApp(App):
class SimpleApp(BaseApp):
def create(self):
self._action("create Simple App")
self.interface._startup()

def set_main_window(self, window):
self._action("set_main_window", window=window)


class App(SimpleApp):
def create(self):
self._action("create App")
self.interface._startup()


class WindowlessApp(BaseApp):
def create(self):
self._action("create Windowless App")
self.interface._startup()


class DocumentApp(BaseApp):
def create(self):
self._action("create DocumentApp")
self.interface._startup()
Expand Down
4 changes: 3 additions & 1 deletion dummy/src/toga_dummy/factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import dialogs
from .app import App, DocumentApp, MainWindow
from .app import App, DocumentApp, MainWindow, SimpleApp, WindowlessApp
from .command import Command
from .documents import Document
from .fonts import Font
Expand Down Expand Up @@ -42,6 +42,8 @@ def not_implemented(feature):
"not_implemented",
"App",
"DocumentApp",
"SimpleApp",
"WindowlessApp",
"MainWindow",
"Command",
"Document",
Expand Down

0 comments on commit 4389855

Please sign in to comment.