Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
proneon267 committed Nov 6, 2023
1 parent 392487a commit 2d59078
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 41 deletions.
108 changes: 84 additions & 24 deletions testbed/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ async def test_system_dpi_change(
):
# For restoring original behavior after completion of test.
original_values = dict()
# --------------------------------- Set up for testing ---------------------------------
# For toolbar
main_window.toolbar.add(app.cmd1, app.cmd2)
# For stack trace dialog
Expand All @@ -576,64 +577,123 @@ async def test_system_dpi_change(
on_result=on_result_handler,
)

# Setup Mock values for testing
original_values["update_scale"] = main_window._impl.update_scale
update_scale_mock = Mock()
monkeypatch.setattr(main_window._impl, "update_scale", update_scale_mock)
original_values["resize_content"] = main_window._impl.resize_content
resize_content_mock = Mock()
monkeypatch.setattr(main_window._impl, "resize_content", resize_content_mock)
original_values["dpi_scale"] = main_window._impl.dpi_scale
# ----------------------- Setup Mock values for testing -----------------------
# For main_window
original_values["main_window_update_scale"] = main_window._impl.update_scale
main_window_update_scale_mock = Mock()
monkeypatch.setattr(
main_window._impl, "update_scale", main_window_update_scale_mock
)
original_values["main_window_resize_content"] = main_window._impl.resize_content
main_window_resize_content_mock = Mock()
monkeypatch.setattr(
main_window._impl, "resize_content", main_window_resize_content_mock
)

window1 = toga.Window("Test Window 1")
window1.content = toga.Box()
window1_probe = window_probe(app, window1)
window1.show()
await window1_probe.wait_for_window("Extra windows added")

# For window1
original_values["window1_update_scale"] = window1._impl.update_scale
window1_update_scale_mock = Mock()
monkeypatch.setattr(window1._impl, "update_scale", window1_update_scale_mock)
original_values["window1_resize_content"] = window1._impl.resize_content
window1_resize_content_mock = Mock()
monkeypatch.setattr(
window1._impl, "resize_content", window1_resize_content_mock
)
original_values[
"window1_update_toolbar_font_scale"
] = window1._impl.update_toolbar_font_scale
window1_update_toolbar_font_scale_mock = Mock()
monkeypatch.setattr(
window1._impl,
"update_toolbar_font_scale",
window1_update_toolbar_font_scale_mock,
)
# -----------------------------------------------------------------------------
# Explicitly set the dpi_scale for testing
main_window._impl.dpi_scale = 1.5

window1._impl.dpi_scale = 1.5
# --------------------------------------------------------------------------------------
await main_window_probe.redraw(
"Triggering DPI change event for testing property changes"
)
app_probe.trigger_dpi_change_event()

# Test out properties which should change on dpi change
main_window._impl.update_scale.assert_called_once()
main_window._impl.update_scale.reset_mock()
window1._impl.update_scale.assert_called_once()
assert main_window_probe.has_toolbar()
app_probe.assert_main_window_toolbar_font_scale_updated()
assert not window1_probe.has_toolbar()
window1._impl.update_toolbar_font_scale.assert_not_called()
app_probe.assert_main_window_menubar_font_scale_updated()
assert not hasattr(window1._impl, "update_menubar_font_scale")
app_probe.assert_main_window_widgets_font_scale_updated()
main_window._impl.resize_content.assert_called_once()
main_window._impl.resize_content.reset_mock()
window1._impl.resize_content.assert_called_once()
app_probe.assert_main_window_stack_trace_dialog_scale_updated()
assert not hasattr(window1._impl, "current_stack_trace_dialog_impl")

# Test if widget.refresh is called once on each widget
for widget in main_window.widgets:
original_values[id(widget)] = widget.refresh
monkeypatch.setattr(widget, "refresh", Mock())
for window in app.windows:
for widget in window.widgets:
original_values[id(widget)] = widget.refresh
monkeypatch.setattr(widget, "refresh", Mock())

await main_window_probe.redraw(
"Triggering DPI change event for testing widget refresh calls"
)
app_probe.trigger_dpi_change_event()

for widget in main_window.widgets:
widget.refresh.assert_called_once()
for window in app.windows:
for widget in main_window.widgets:
widget.refresh.assert_called_once()

# Restore original state
for widget in main_window.widgets:
monkeypatch.setattr(widget, "refresh", original_values[id(widget)])
for window in app.windows:
for widget in window.widgets:
monkeypatch.setattr(widget, "refresh", original_values[id(widget)])
monkeypatch.setattr(
window1._impl,
"update_toolbar_font_scale",
original_values["window1_update_toolbar_font_scale"],
)
monkeypatch.setattr(
window1._impl, "resize_content", original_values["window1_resize_content"]
)
monkeypatch.setattr(
window1._impl, "update_scale", original_values["window1_update_scale"]
)
monkeypatch.setattr(
main_window._impl, "resize_content", original_values["resize_content"]
main_window._impl,
"resize_content",
original_values["main_window_resize_content"],
)
monkeypatch.setattr(
main_window._impl, "update_scale", original_values["update_scale"]
main_window._impl,
"update_scale",
original_values["main_window_update_scale"],
)

# When dpi_scale is None then calculates dpi_scale should be equal to
# When dpi_scale is None then calculated dpi_scale should be equal to
# dpi scale of Primary Screen
main_window._impl.dpi_scale = None
app_probe.assert_dpi_scale_equal_to_primary_screen_dpi_scale()
for window in app.windows:
window._impl.dpi_scale = None
app_probe.assert_dpi_scale_equal_to_primary_screen_dpi_scale(window)

# Restore original state
for window in app.windows:
window._impl.dpi_scale = 1.0
await main_window_probe.redraw(
"Triggering DPI change event for restoring original state"
)

app_probe.trigger_dpi_change_event()
await main_window_probe.close_stack_trace_dialog(dialog_result._impl, True)
app.main_window.toolbar.clear()
main_window.toolbar.clear()
window1.close()
16 changes: 2 additions & 14 deletions winforms/src/toga_winforms/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class App(Scalable):
windll.user32.SetProcessDpiAwarenessContext.restype = c_bool
windll.user32.SetProcessDpiAwarenessContext.argtypes = [c_void_p]
# SetProcessDpiAwarenessContext returns False on Failure
if not windll.user32.SetProcessDpiAwarenessContext(-4):
if not windll.user32.SetProcessDpiAwarenessContext(-4): # pragma: no cover
print("WARNING: Failed to set the DPI Awareness mode for the app.")
else:
else: # pragma: no cover
print(
"WARNING: Your Windows version doesn't support DPI Awareness setting. "
"We recommend you upgrade to at least Windows 10 Build 1703."
Expand Down Expand Up @@ -362,31 +362,19 @@ def hide_cursor(self):
self._cursor_visible = False

def winforms_DisplaySettingsChanged(self, sender, event):
# Print statements added only for testing, will be removed
# in final code cleanup.
for window in self.interface.windows:
window._impl.update_scale(
screen=WinForms.Screen.FromControl(window._impl.native)
)
if window._impl.toolbar_native is not None:
print("About to update toolbar font scale...")
window._impl.update_toolbar_font_scale()
print("Done...")
if isinstance(window._impl, MainWindow):
print("About to update menubar font scale...")
window._impl.update_menubar_font_scale()
print("Done...")
for widget in window.widgets:
print("About to update menubar font scale...")
widget.refresh()
print("Done...")
print("About to resize window content...")
window._impl.resize_content()
print("Done...")
if hasattr(window._impl, "current_stack_trace_dialog_impl"):
print("About to resize stack trace dialog...")
window._impl.current_stack_trace_dialog_impl.resize_content()
print("Done...")


class DocumentApp(App): # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion winforms/src/toga_winforms/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def remove_child(self, child):

def refresh(self):
# Update the scaling of the font
if hasattr(self, "original_font"):
if hasattr(self, "original_font"): # pragma: no branch
self.native.Font = WinFont(
self.original_font.FontFamily,
self.scale_font(self.original_font.Size),
Expand Down
8 changes: 6 additions & 2 deletions winforms/tests_backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def activate_menu_minimize(self):
def keystroke(self, combination):
pytest.xfail("Not applicable to this backend")

# ------------------- Functions specific to test_system_dpi_change -------------------

def trigger_dpi_change_event(self):
self.app._impl.winforms_DisplaySettingsChanged(None, None)

Expand Down Expand Up @@ -232,7 +234,7 @@ def assert_main_window_stack_trace_dialog_scale_updated(self):
stack_trace_dialog_impl.original_control_bounds[control].Height
)

def assert_dpi_scale_equal_to_primary_screen_dpi_scale(self):
def assert_dpi_scale_equal_to_primary_screen_dpi_scale(self, window):
screen = WinScreen.PrimaryScreen
screen_rect = wintypes.RECT(
screen.Bounds.Left,
Expand All @@ -247,4 +249,6 @@ def assert_dpi_scale_equal_to_primary_screen_dpi_scale(self):
pScale = wintypes.UINT()
windll.shcore.GetScaleFactorForMonitor(c_void_p(hMonitor), byref(pScale))

assert self.main_window._impl.dpi_scale == pScale.value / 100
assert window._impl.dpi_scale == pScale.value / 100

# ------------------------------------------------------------------------------------

0 comments on commit 2d59078

Please sign in to comment.