Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test, fix invalid user-provided startup() method #2047

Merged
merged 6 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/src/toga_android/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def create(self):
# the app's `.native` is the listener's native Java class.
self._listener = TogaApp(self)
# Call user code to populate the main window
self.interface.startup()
self.interface._startup()

def open_document(self, fileURL):
print("Can't open document %s (yet)" % fileURL)
Expand Down
1 change: 1 addition & 0 deletions changes/2047.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Verify that ``app.startup`` creates a ``MainWindow``
freakboy3742 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion cocoa/src/toga_cocoa/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def create(self):
self._create_app_commands()

# Call user code to populate the main window
self.interface.startup()
self.interface._startup()

# Create the lookup table of menu items,
# then force the creation of the menus.
Expand Down
17 changes: 17 additions & 0 deletions core/src/toga/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,19 @@ def startup(self):

self.main_window.show()

def _startup(self):
"""Since startup can and is overridden by users, verify
they did the right thing in their code
"""
freakboy3742 marked this conversation as resolved.
Show resolved Hide resolved
self.startup()
self._verify_startup()

def _verify_startup(self):
if not isinstance(self.main_window, MainWindow):
raise ValueError(
"user-defined startup() did not instaniate self.main_window"
)

def about(self):
"""Display the About dialog for the app.

Expand Down Expand Up @@ -683,6 +696,10 @@ def __init__(
def _create_impl(self):
return self.factory.DocumentApp(interface=self)

def _verify_startup(self):
"""The class 'startup()' method has no restrictions"""
freakboy3742 marked this conversation as resolved.
Show resolved Hide resolved
pass

@property
def documents(self):
"""Return the list of documents associated with this app.
Expand Down
14 changes: 14 additions & 0 deletions core/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ async def test_handler(sender):
)


class BadAppTests(TestCase):
def test_override_startup(self):
class BadApp(toga.App):
# issue 760. A bogus startup function should raise
def startup(self):
# This is bogus, it does not create the main window
pass

msg = "did not instaniate"
app = BadApp(app_name="pytest", formal_name="pytest", app_id="org.beeware")
with self.assertRaisesRegex(ValueError, msg):
app.main_loop()


class DocumentAppTests(TestCase):
def setUp(self):
super().setUp()
Expand Down
2 changes: 2 additions & 0 deletions dummy/src/toga_dummy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ def __init__(self, interface):

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

@not_required_on("mobile")
def create_menus(self):
self._action("create menus")

def main_loop(self):
self._action("main loop")
self.create()

def set_main_window(self, window):
self._set_value("main_window", window)
Expand Down
2 changes: 1 addition & 1 deletion gtk/src/toga_gtk/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def gtk_startup(self, data=None):
)
self._create_app_commands()

self.interface.startup()
self.interface._startup()

# Create the lookup table of menu items,
# then force the creation of the menus.
Expand Down
2 changes: 1 addition & 1 deletion iOS/src/toga_iOS/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, interface):

def create(self):
"""Calls the startup method on the interface."""
self.interface.startup()
self.interface._startup()

def open_document(self, fileURL):
"""Add a new document to this app."""
Expand Down
2 changes: 1 addition & 1 deletion web/src/toga_web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def create(self):
self.create_menus()

# Call user code to populate the main window
self.interface.startup()
self.interface._startup()

def _create_submenu(self, group, items):
submenu = create_element(
Expand Down
2 changes: 1 addition & 1 deletion winforms/src/toga_winforms/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def create(self):
self._create_app_commands()

# Call user code to populate the main window
self.interface.startup()
self.interface._startup()
self.create_menus()
self.interface.main_window._impl.set_app(self)

Expand Down
Loading