Phase 4: app-level tests; fix tab-label GTK critical + Gdk version warning
This commit is contained in:
1 parent
2a99acbd61
commit
c22ff1059f
3 files changed
+103
-7
No files matched your search
+7
-4
@@ -434,17 +434,20 @@ class Application:
|
||||
if self.code_window.is_visible():
|
||||
self.code_window.hide()
|
||||
else:
|
||||
self.refresh_code_view()
|
||||
self.refresh_code_view(force=True)
|
||||
self.code_window.show()
|
||||
self._sync_code_menu_item()
|
||||
if not self.code_window.is_visible():
|
||||
self.set_status("Code window hidden — Ctrl+E to show it again")
|
||||
|
||||
def refresh_code_view(self):
|
||||
def refresh_code_view(self, force=False):
|
||||
"""Regenerate into the Code window's generated tab — unless
|
||||
that tab holds unsaved hand edits, which are kept (C)."""
|
||||
that tab holds unsaved hand edits, which are kept (C).
|
||||
Skipped when there is no generated tab and the window is
|
||||
hidden, unless *force* (opening the window creates the tab).
|
||||
"""
|
||||
if self.code_window.generated_view is None \
|
||||
and not self.code_window.is_visible():
|
||||
and not self.code_window.is_visible() and not force:
|
||||
return
|
||||
self.code_window.show_generated(
|
||||
self._generate_text(), self.generated_path)
|
||||
|
||||
@@ -27,8 +27,10 @@ import re
|
||||
import gi
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
gi.require_version("Gdk", "3.0")
|
||||
gi.require_version("PangoCairo", "1.0")
|
||||
|
||||
from gi.repository import Gdk, GLib, Gtk, Pango # noqa: E402
|
||||
from gi.repository import Gdk, GLib, Gtk, Pango, PangoCairo # noqa: E402
|
||||
|
||||
# Built-in themes, copied from the C CodeTheme::builtin() values
|
||||
# (src/theme.cpp). Monokai is the default, matching the C code view.
|
||||
@@ -509,7 +511,7 @@ class CodeView(Gtk.Box):
|
||||
cr.set_source_rgb(*fg)
|
||||
cr.move_to((self.FOLD_COLUMN - mw) / 2,
|
||||
y + (rect.height - mh) / 2)
|
||||
cr.show_layout(layout)
|
||||
PangoCairo.show_layout(cr, layout)
|
||||
# Line number, right-aligned before the divider.
|
||||
text = str(line + 1)
|
||||
layout = gutter.create_pango_layout(text)
|
||||
@@ -517,7 +519,7 @@ class CodeView(Gtk.Box):
|
||||
tw, th = layout.get_pixel_size()
|
||||
cr.set_source_rgb(*num_rgb)
|
||||
cr.move_to(alloc.width - 4 - tw, y + (rect.height - th) / 2)
|
||||
cr.show_layout(layout)
|
||||
PangoCairo.show_layout(cr, layout)
|
||||
# Divider between gutter and code.
|
||||
cr.set_source_rgb(*fg)
|
||||
cr.set_line_width(1.0)
|
||||
@@ -633,6 +635,10 @@ class CodeWindow:
|
||||
return hbox
|
||||
|
||||
def _set_tab_label(self, view, title):
|
||||
# Not a page yet (new_tab sets the text before appending):
|
||||
# get_tab_label() would hit a GTK assertion, so check first.
|
||||
if self.notebook.page_num(view) < 0:
|
||||
return
|
||||
tab = self.notebook.get_tab_label(view)
|
||||
if tab is None:
|
||||
return
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
"""Tests for lightgui.app wiring (Phase 4): Code window toggle,
|
||||
snapshot dirty tracking, Generate/Build paths.
|
||||
|
||||
Needs a display; skipped when Gtk cannot initialise (run under
|
||||
Xvfb, :99). Windows are destroyed in tearDown; Gtk.main is never
|
||||
started.
|
||||
"""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
try:
|
||||
import gi
|
||||
gi.require_version("Gtk", "3.0")
|
||||
from gi.repository import Gtk
|
||||
_GTK_OK = Gtk.init_check()[0]
|
||||
except Exception:
|
||||
Gtk = None
|
||||
_GTK_OK = False
|
||||
|
||||
if _GTK_OK:
|
||||
from lightgui.app import Application
|
||||
|
||||
|
||||
class AppTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
if not _GTK_OK:
|
||||
self.skipTest("needs a display (run under Xvfb)")
|
||||
self.app = Application()
|
||||
while Gtk.events_pending():
|
||||
Gtk.main_iteration()
|
||||
|
||||
def tearDown(self):
|
||||
if _GTK_OK and getattr(self, "app", None) is not None:
|
||||
self.app.code_window.window.destroy()
|
||||
self.app.inspector.window.destroy()
|
||||
self.app.window.destroy()
|
||||
while Gtk.events_pending():
|
||||
Gtk.main_iteration()
|
||||
|
||||
def test_toggle_code_window_creates_generated_tab(self):
|
||||
self.assertIsNone(self.app.code_window.generated_view)
|
||||
self.assertFalse(self.app.code_window.is_visible())
|
||||
self.app.toggle_code_window()
|
||||
self.assertTrue(self.app.code_window.is_visible())
|
||||
view = self.app.code_window.generated_view
|
||||
self.assertIsNotNone(view)
|
||||
self.assertIn("Generated by LightGUI", view.get_text())
|
||||
self.assertIn("with 0 widget(s)", view.get_text())
|
||||
self.app.toggle_code_window()
|
||||
self.assertFalse(self.app.code_window.is_visible())
|
||||
|
||||
def test_menu_bar_lives_in_code_window(self):
|
||||
# C placement: the menu bar is packed into the Code window,
|
||||
# not the main designer window.
|
||||
self.assertIs(self.app.menubar.get_parent(),
|
||||
self.app.code_window.vbox)
|
||||
self.assertIsNot(self.app.menubar.get_parent(),
|
||||
self.app.window)
|
||||
|
||||
def test_place_widget_marks_dirty_then_save_clears(self):
|
||||
self.assertFalse(self.app.project_file.dirty)
|
||||
self.app.canvas.state.place("GtkButton", 10, 10, 150, 36)
|
||||
self.assertTrue(self.app.project_file.dirty)
|
||||
self.assertIn("*", self.app.statusbar.get_text())
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
path = os.path.join(folder, "app.fdj")
|
||||
self.app.project_file.save_as(path)
|
||||
self.app._saved_snapshot = self.app.project.save_text()
|
||||
self.app.project_file.dirty = False
|
||||
self.assertFalse(self.app.project_file.dirty)
|
||||
# Selecting a widget does not dirty the project.
|
||||
self.app.canvas.state.select(
|
||||
self.app.project.widgets[0].id)
|
||||
self.assertFalse(self.app.project_file.dirty)
|
||||
|
||||
def test_ensure_generated_writes_runnable_script(self):
|
||||
self.app.canvas.state.place("GtkButton", 10, 10, 150, 36)
|
||||
path = self.app._ensure_generated()
|
||||
self.assertTrue(os.path.isfile(path))
|
||||
self.assertTrue(path.endswith(".py"))
|
||||
compile(open(path).read(), path, "exec")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user