Skip to content

Commit

Permalink
Added tests as per review: 9
Browse files Browse the repository at this point in the history
  • Loading branch information
proneon267 committed Jul 6, 2024
1 parent 2b6d347 commit 6b3e883
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 4 deletions.
6 changes: 3 additions & 3 deletions cocoa/src/toga_cocoa/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,9 @@ def enter_presentation_mode(self, screen_window_dict):
window.content._impl.native.enterFullScreenMode(
screen._impl.native, withOptions=opts
)
# Going full screen causes the window content to be re-homed
# in a NSFullScreenWindow; teach the new parent window
# about its Toga representations.
# Going presentation mode causes the window content to be re-homed
# in a NSFullScreenWindow; teach the new parent window about its
# Toga representations.
window.content._impl.native.window._impl = window._impl
window.content._impl.native.window.interface = window
window.content.refresh()
Expand Down
6 changes: 5 additions & 1 deletion core/src/toga/constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def __str__(self) -> str:


class WindowState(Enum):
"""The possible window states of an app."""
"""The possible window states of an app.
Note: Changing window state while the app is in presentation mode will cause
the app to exit presentation mode, and the new window state will be set.
"""

NORMAL = 0
"""The ``NORMAL`` state represents the default state of the window or app when it is
Expand Down
79 changes: 79 additions & 0 deletions core/tests/app/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytest

import toga
from toga.constants import WindowState
from toga_dummy.utils import (
EventLog,
assert_action_not_performed,
Expand Down Expand Up @@ -599,6 +600,84 @@ def test_presentation_mode_no_op(event_loop):
assert not app.is_presentation_mode


@pytest.mark.parametrize(
"new_window_state",
[
WindowState.MINIMIZED,
WindowState.MAXIMIZED,
WindowState.FULLSCREEN,
],
)
def test_presentation_mode_exit_on_window_state_change(event_loop, new_window_state):
"""Changing window state exits presentation mode and sets the new state."""
app = toga.App(formal_name="Test App", app_id="org.example.test")
extra_window = toga.Window()

# Enter presentation mode
app.enter_presentation_mode([app.main_window])
assert_action_performed_with(
app,
"enter presentation mode",
screen_window_dict={app.screens[0]: app.main_window},
)

assert app.is_presentation_mode
assert app.main_window.state == WindowState.PRESENTATION
assert extra_window.state != WindowState.PRESENTATION

# Changing window state of main_window should make the app exit presentation mode.
app.main_window.state = new_window_state
assert_action_performed_with(
app.main_window,
f"set window state to {new_window_state}",
state=new_window_state,
)

assert not app.is_presentation_mode
assert_action_performed(
app,
"exit presentation mode",
)
assert app.main_window.state != WindowState.PRESENTATION
assert app.main_window.state == new_window_state

# Reset window states
app.main_window.state = WindowState.NORMAL
extra_window.state = WindowState.NORMAL

# Enter presentation mode again
app.enter_presentation_mode([app.main_window])
assert_action_performed_with(
app,
"enter presentation mode",
screen_window_dict={app.screens[0]: app.main_window},
)

assert app.is_presentation_mode
assert app.main_window.state == WindowState.PRESENTATION
assert extra_window.state != WindowState.PRESENTATION

# Changing window state of extra window should make the app exit presentation mode.
extra_window.state = new_window_state
assert_action_performed_with(
extra_window,
f"set window state to {new_window_state}",
state=new_window_state,
)

assert not app.is_presentation_mode
assert_action_performed(
app,
"exit presentation mode",
)
assert app.main_window.state != WindowState.PRESENTATION
assert extra_window.state == new_window_state

# Reset window states
app.main_window.state = WindowState.NORMAL
extra_window.state = WindowState.NORMAL


def test_show_hide_cursor(app):
"""The app cursor can be shown and hidden."""
app.hide_cursor()
Expand Down
3 changes: 3 additions & 0 deletions dummy/src/toga_dummy/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def get_window_state(self):
return self._get_value("state", WindowState.NORMAL)

def set_window_state(self, state):
if self.interface.app.is_presentation_mode and state != WindowState.NORMAL:
self.interface.app.exit_presentation_mode()
self.set_window_state(state)
self._action(f"set window state to {state}", state=state)
self._set_value("state", state)

Expand Down
83 changes: 83 additions & 0 deletions testbed/tests/app/test_desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ async def test_presentation_mode_with_excess_windows_list(app, app_probe):
try:
window_information_list = list()
excess_windows_list = list()
# Generate an additional window compared to the number of screens present.
for i in range(len(app.screens) + 1):
window = toga.MainWindow(
title=f"Test Window {i}",
Expand Down Expand Up @@ -336,6 +337,88 @@ async def test_presentation_mode_with_excess_windows_list(app, app_probe):
window.close()


@pytest.mark.parametrize(
"new_window_state",
[
WindowState.MINIMIZED,
WindowState.MAXIMIZED,
WindowState.FULLSCREEN,
],
)
async def test_presentation_mode_exit_on_window_state_change(
app, app_probe, main_window, main_window_probe, new_window_state
):
"""Changing window state exits presentation mode and sets the new state."""
try:
main_window.toolbar.add(app.cmd1)
extra_window = toga.MainWindow(
title="Extra Window", position=(150, 150), size=(200, 200)
)
extra_window.content = toga.Box(style=Pack(background_color=CORNFLOWERBLUE))
extra_window_probe = window_probe(app, extra_window)
extra_window.show()
# Add delay for gtk to show the windows
await app_probe.redraw("Extra window is shown", delay=0.5)

# Enter presentation mode
app.enter_presentation_mode([main_window])
# Add delay for gtk to show the windows
await app_probe.redraw("App is in presentation mode", delay=0.5)

assert app.is_presentation_mode
assert main_window_probe.get_window_state() == WindowState.PRESENTATION
assert extra_window_probe.get_window_state() != WindowState.PRESENTATION

# Changing window state of main window should make the app exit presentation mode.
main_window.state = new_window_state
# Add delay for gtk to show the windows
await app_probe.redraw(
"App is not in presentation mode" f"\nMain Window is in {new_window_state}",
delay=0.5,
)

assert not app.is_presentation_mode
assert main_window_probe.get_window_state != WindowState.PRESENTATION
assert main_window_probe.get_window_state() == new_window_state

# Reset window states
main_window.state = WindowState.NORMAL
extra_window.state = WindowState.NORMAL
# Add delay for gtk to show the windows
await app_probe.redraw("All windows are in WindowState.NORMAL", delay=0.5)

# Enter presentation mode again
app.enter_presentation_mode([main_window])
# Add delay for gtk to show the windows
await app_probe.redraw("App is in presentation mode", delay=0.5)
assert app.is_presentation_mode
assert main_window_probe.get_window_state() == WindowState.PRESENTATION
assert extra_window_probe.get_window_state() != WindowState.PRESENTATION

# Changing window state of extra window should make the app exit presentation mode.
extra_window.state = new_window_state
# Add delay for gtk to show the windows
await app_probe.redraw(
"App is not in presentation mode"
f"\nExtra Window is in {new_window_state}",
delay=0.5,
)

assert not app.is_presentation_mode
assert main_window_probe.get_window_state() != WindowState.PRESENTATION
assert extra_window_probe.get_window_state() == new_window_state

# Reset window states
main_window.state = WindowState.NORMAL
extra_window.state = WindowState.NORMAL
# Add delay for gtk to show the windows
await app_probe.redraw("All windows are in WindowState.NORMAL", delay=0.5)

finally:
main_window.toolbar.clear()
extra_window.close()


async def test_show_hide_cursor(app, app_probe):
"""The app cursor can be hidden and shown"""
assert app_probe.is_cursor_visible
Expand Down

0 comments on commit 6b3e883

Please sign in to comment.