"""STATE lane — DB Browser for SQLite (Qt5). STATE is the SOLE owner of CHECKED and of FOCUS. Every test reads the state BEFORE, drives the real user path, reads AFTER, asserts the flip is the verdict, pairs it with the app-authored CONSEQUENCE where one exists, then RESTORES so the suite is order-independent. Covered transitions (all verified live on the reference): * Edit-Pragmas checkbox CHECKED flips (Recursive Triggers False->True, Foreign Keys True->False) — round-tripped. * Edit-Database-Cell dock checkbox CHECKED flips (Word Wrap True->False, Autoformat False->True) — round-tripped. * View-menu dock toggles: item CHECKED flip True->False AND its consequence, the dock QDockWidget (role=frame) SHOWING flip (Plot, SQL Log) — restored. * Browse-Data table selector: the combo's selected-table NAME transitions departments->employees AND the app-authored "1 - N of N" row-count label changes 3->8 (selection/mode state; computation lane owns the cell values). * SQL editor keyboard FOCUS: clicking it lands focus (typed char round-trips into the editor text — the focus consequence a dead shell cannot fake). Nothing is read from source/constants; a pixel-perfect dead shell that never flips these states fails every test. """ import re import time import pytest, pytest_check as check import rb_atspi from gi.repository import Atspi DB = "/workspace/output/fixtures/sample.db" # --------------------------------------------------------------------------- # # Module fixture — ensure sample.db is open (cold baseline starts with no DB). # --------------------------------------------------------------------------- # @pytest.fixture(scope="module", autouse=True) def _load_db(app): rb_atspi.focus_app_window(app) frame = rb_atspi.get_main_frame(app) for _ in range(4): rb_atspi.press_escape(); time.sleep(0.15) if "sample.db" not in (frame.get_name() or ""): fm = rb_atspi.find_first(frame, role="menu item", name="File") if fm: rb_atspi.click_node(fm); time.sleep(0.4) op = rb_atspi.find_first(frame, role="menu item", name="Open Database...") if op: rb_atspi.open_modal(op); time.sleep(1.2) rb_atspi.type_text(DB); time.sleep(0.4) rb_atspi.key("Return"); time.sleep(1.5) yield # --------------------------------------------------------------------------- # # Helpers (all verified live on the running reference). # --------------------------------------------------------------------------- # def _ext(n): try: return n.get_extents(Atspi.CoordType.SCREEN) except Exception: return None def _forigin(frame): """Frame's top-left in SCREEN coords; locators are computed RELATIVE to this so they hold wherever the window manager places the window (a cold relaunch does not reuse the warm testgen instance's absolute position).""" e = _ext(frame) return (e.x, e.y) if e else (0, 0) def _reset(app): """Re-derive the frame and clear any stray popup/menu at test start.""" frame = rb_atspi.get_main_frame(app) for _ in range(2): rb_atspi.press_escape(); time.sleep(0.12) return frame def _select_tab(frame, name, settle=0.8): rb_atspi.focus_app_window(rb_atspi.get_app()) t = rb_atspi.find_first(frame, role="page tab", name=name) if t is not None: if not rb_atspi.do_action(t, "Press"): rb_atspi.click_node(t) time.sleep(settle) return t def _pragma_cb(frame, name): """A named pragma check box on the Edit Pragmas tab (unique real names).""" return rb_atspi.find_first(frame, role="check box", name=name) def _editcell_cb(frame, name): """A named check box in the always-present Edit Database Cell dock.""" for c in rb_atspi.find_all(frame, role="check box", name=name): if rb_atspi.is_showing(c): return c return rb_atspi.find_first(frame, role="check box", name=name) def _flip_checkbox(frame, finder, want): """Drive a check box to the boolean `want`, tolerantly. Re-fetches the live node (the tree hands back fresh nodes each walk), fires Toggle, and POLLS for the CHECKED state to actually reach `want` — falling back to a real click if the AT-SPI Toggle action was swallowed (the reference occasionally drops a Toggle fired immediately after a tab switch). Returns the observed after-state. """ node = finder() if node is None: return None if rb_atspi.is_checked(node) == want: return want rb_atspi.do_action(node, "Toggle") if rb_atspi.wait_for(lambda: rb_atspi.is_checked(finder()) == want, timeout=2.0, interval=0.2): return want # Toggle didn't land — raise/focus the window and use a real mouse click on the box. rb_atspi.focus_app_window(rb_atspi.get_app()); time.sleep(0.2) rb_atspi.click_node(finder()) rb_atspi.wait_for(lambda: rb_atspi.is_checked(finder()) == want, timeout=2.0, interval=0.2) return rb_atspi.is_checked(finder()) def _open_view(frame): vm = rb_atspi.find_first(frame, role="menu item", name="View") if vm is not None: rb_atspi.click_node(vm); time.sleep(0.45) return vm def _view_item(frame, name): return rb_atspi.find_first(frame, role="menu item", name=name) def _view_item_checked(frame, name): """Open the View menu and read the CHECKED state of `name` (the item only reports its checked state while the menu is open).""" _open_view(frame) it = _view_item(frame, name) return it, (rb_atspi.is_checked(it) if it is not None else None) def _dock_frame_showing(frame, name): """A dock QDockWidget is a role=frame carrying the dock title; SHOWING flips with the View-menu toggle (the app-authored consequence).""" for f in rb_atspi.find_all(frame, role="frame", name=name): return rb_atspi.is_showing(f) return None def _sql_editor(frame): """The Scintilla SQL editor: the showing role=text at the TOP-LEFT of the Execute SQL tab (frame-relative x<400, tall).""" fx, fy = _forigin(frame) best = None for t in rb_atspi.find_all(frame, role="text"): if not rb_atspi.is_showing(t): continue e = _ext(t) if e is None: continue rx, ry = e.x - fx, e.y - fy if rx < 400 and e.height > 80 and e.width > 200 and ry < 420: if best is None or e.y < best[1]: best = (t, e.y) return best[0] if best else None def _browse_combo(frame): """The Browse-Data table selector combo (frame-relative x<400, ry~90).""" fx, fy = _forigin(frame) for c in rb_atspi.find_all(frame, role="combo box"): if not rb_atspi.is_showing(c): continue e = _ext(c) if e is None: continue rx, ry = e.x - fx, e.y - fy if 0 < rx < 400 and 60 < ry < 240 and 80 < e.width < 400: return c return None def _rowcount_label(frame): for l in rb_atspi.find_all(frame, role="label"): n = l.get_name() or "" if re.match(r"^\s*\d+\s*-\s*\d+\s*of\s*\d+", n): return n.strip() return None def _select_browse_table(frame, target): """Select `target` in the Browse-Data combo via Qt type-ahead (the popup's list items report bogus (0,0) extents, so a center-click on an item misses). Open with a real click, type the name, Return. Returns the combo's reported table after.""" combo = _browse_combo(frame) if combo is None: return None if (combo.get_name() or "") == target: return target e = _ext(combo) if e is None: return None rb_atspi.xdo("mousemove", e.x + e.width // 2, e.y + e.height // 2, "click", "1") time.sleep(0.5) rb_atspi.type_text(target); time.sleep(0.4) rb_atspi.key("Return"); time.sleep(1.0) c = _browse_combo(frame) if c is not None and (c.get_name() or "") == target: return target # Retry once. rb_atspi.press_escape(); time.sleep(0.2) c = _browse_combo(frame) if c is None: return None e = _ext(c) rb_atspi.xdo("mousemove", e.x + e.width // 2, e.y + e.height // 2, "click", "1") time.sleep(0.5) rb_atspi.type_text(target); time.sleep(0.4) rb_atspi.key("Return"); time.sleep(1.0) c = _browse_combo(frame) return (c.get_name() or "") if c else None # =========================================================================== # # 1-2. Edit-Pragmas checkbox CHECKED flips (state lane owns CHECKED). # =========================================================================== # @pytest.mark.rb(jump_kind="transition", expect="Recursive Triggers CHECKED False->True") def test_pragma_recursive_triggers_toggle(app, rb, results_dir): frame = _reset(app) rb.enter("pragmas") _select_tab(frame, "Edit Pragmas", settle=0.8) cb = _pragma_cb(frame, "Recursive Triggers") assert cb is not None, "Recursive Triggers checkbox not found on Edit Pragmas tab" before = rb_atspi.is_checked(cb) check.is_false(before, f"Recursive Triggers should start UNchecked, got {before}") after = _flip_checkbox(frame, lambda: _pragma_cb(frame, "Recursive Triggers"), not before) check.is_true(after, f"Recursive Triggers should be CHECKED after toggle, got {after}") check.not_equal(before, after, "Recursive Triggers CHECKED did not flip") rb_atspi.screenshot_assert( "test_pragma_recursive_triggers_toggle", "Edit Pragmas tab: the 'Recursive Triggers' checkbox is now CHECKED " "(ticked), among the other pragma checkboxes (Foreign Keys, Secure " "Delete also ticked).", results_dir, window=frame, ) # Restore. restored = _flip_checkbox(frame, lambda: _pragma_cb(frame, "Recursive Triggers"), before) check.equal(restored, before, "Recursive Triggers not restored to original state") @pytest.mark.rb(jump_kind="transition", expect="Foreign Keys CHECKED True->False") def test_pragma_foreign_keys_toggle(app, rb): frame = _reset(app) rb.enter("pragmas") _select_tab(frame, "Edit Pragmas", settle=0.8) cb = _pragma_cb(frame, "Foreign Keys") assert cb is not None, "Foreign Keys checkbox not found on Edit Pragmas tab" before = rb_atspi.is_checked(cb) check.is_true(before, f"Foreign Keys should start CHECKED, got {before}") after = _flip_checkbox(frame, lambda: _pragma_cb(frame, "Foreign Keys"), not before) check.is_false(after, f"Foreign Keys should be UNchecked after toggle, got {after}") check.not_equal(before, after, "Foreign Keys CHECKED did not flip") # Restore. restored = _flip_checkbox(frame, lambda: _pragma_cb(frame, "Foreign Keys"), before) check.equal(restored, before, "Foreign Keys not restored to original state") # =========================================================================== # # 3-4. Edit-Database-Cell dock checkbox CHECKED flips. # =========================================================================== # @pytest.mark.rb(jump_kind="transition", expect="Word Wrap CHECKED True->False") def test_editcell_wordwrap_toggle(app, rb): frame = _reset(app) # Cross into a tab so depth>=1; the dock is always present regardless. rb.enter("pragmas") _select_tab(frame, "Edit Pragmas", settle=0.8) cb = _editcell_cb(frame, "Word Wrap") assert cb is not None, "Word Wrap checkbox not found in Edit Database Cell dock" before = rb_atspi.is_checked(cb) check.is_true(before, f"Word Wrap should start CHECKED, got {before}") after = _flip_checkbox(frame, lambda: _editcell_cb(frame, "Word Wrap"), not before) check.is_false(after, f"Word Wrap should be UNchecked after toggle, got {after}") check.not_equal(before, after, "Word Wrap CHECKED did not flip") # Restore. restored = _flip_checkbox(frame, lambda: _editcell_cb(frame, "Word Wrap"), before) check.equal(restored, before, "Word Wrap not restored to original state") @pytest.mark.rb(jump_kind="transition", expect="Autoformat CHECKED False->True") def test_editcell_autoformat_toggle(app, rb): frame = _reset(app) rb.enter("pragmas") _select_tab(frame, "Edit Pragmas", settle=0.8) cb = _editcell_cb(frame, "Autoformat") assert cb is not None, "Autoformat checkbox not found in Edit Database Cell dock" before = rb_atspi.is_checked(cb) check.is_false(before, f"Autoformat should start UNchecked, got {before}") after = _flip_checkbox(frame, lambda: _editcell_cb(frame, "Autoformat"), not before) check.is_true(after, f"Autoformat should be CHECKED after toggle, got {after}") check.not_equal(before, after, "Autoformat CHECKED did not flip") # Restore. restored = _flip_checkbox(frame, lambda: _editcell_cb(frame, "Autoformat"), before) check.equal(restored, before, "Autoformat not restored to original state") # =========================================================================== # # 5-6. View-menu dock toggles: item CHECKED flip AND dock SHOWING consequence. # =========================================================================== # @pytest.mark.rb( jump_kind="transition", expect="View>Plot CHECKED True->False and Plot dock hidden", ) def test_view_plot_dock_toggle(app, rb, results_dir): frame = _reset(app) rb.enter("viewmenu") dock_before = _dock_frame_showing(frame, "Plot") it, before = _view_item_checked(frame, "Plot") assert it is not None, "View>Plot menu item not found" check.is_true(before, f"View>Plot should start CHECKED, got {before}") check.is_true(dock_before, f"Plot dock should start visible, got {dock_before}") # Toggle from the open menu. rb_atspi.click_node(it); time.sleep(0.6) dock_after = _dock_frame_showing(frame, "Plot") _, after = _view_item_checked(frame, "Plot") rb_atspi.press_escape(); time.sleep(0.2) check.is_false(after, f"View>Plot should be UNchecked after toggle, got {after}") check.not_equal(before, after, "View>Plot CHECKED did not flip") # App-authored consequence: the Plot dock disappears. check.is_false(dock_after, f"Plot dock should be hidden after toggle, got {dock_after}") check.not_equal(dock_before, dock_after, "Plot dock SHOWING did not change") rb_atspi.screenshot_assert( "test_view_plot_dock_toggle", "The DB Browser main window with the Plot dock turned OFF: there is NO " "'Plot' panel or 'Plot' dock tab anywhere. The bottom docked tab bar shows " "only 'SQL Log' and 'DB Schema' tabs (no 'Plot' tab), and no plotting/chart " "axis panel is visible on the right.", results_dir, window=frame, ) # Restore: reopen menu, toggle back on. _open_view(frame) it_r = _view_item(frame, "Plot") rb_atspi.click_node(it_r); time.sleep(0.6) rb_atspi.press_escape(); time.sleep(0.2) _, restored = _view_item_checked(frame, "Plot") rb_atspi.press_escape(); time.sleep(0.2) check.equal(restored, before, "View>Plot CHECKED not restored") check.is_true(_dock_frame_showing(frame, "Plot"), "Plot dock not restored to visible") @pytest.mark.rb( jump_kind="transition", expect="View>SQL Log CHECKED True->False and SQL Log dock hidden", ) def test_view_sqllog_dock_toggle(app, rb): frame = _reset(app) rb.enter("viewmenu") dock_before = _dock_frame_showing(frame, "SQL Log") it, before = _view_item_checked(frame, "SQL Log") assert it is not None, "View>SQL Log menu item not found" check.is_true(before, f"View>SQL Log should start CHECKED, got {before}") check.is_true(dock_before, f"SQL Log dock should start visible, got {dock_before}") rb_atspi.click_node(it); time.sleep(0.6) dock_after = _dock_frame_showing(frame, "SQL Log") _, after = _view_item_checked(frame, "SQL Log") rb_atspi.press_escape(); time.sleep(0.2) check.is_false(after, f"View>SQL Log should be UNchecked after toggle, got {after}") check.not_equal(before, after, "View>SQL Log CHECKED did not flip") check.is_false(dock_after, f"SQL Log dock should be hidden after toggle, got {dock_after}") check.not_equal(dock_before, dock_after, "SQL Log dock SHOWING did not change") # Restore. _open_view(frame) it_r = _view_item(frame, "SQL Log") rb_atspi.click_node(it_r); time.sleep(0.6) rb_atspi.press_escape(); time.sleep(0.2) _, restored = _view_item_checked(frame, "SQL Log") rb_atspi.press_escape(); time.sleep(0.2) check.equal(restored, before, "View>SQL Log CHECKED not restored") check.is_true(_dock_frame_showing(frame, "SQL Log"), "SQL Log dock not restored to visible") # =========================================================================== # # 7. SQL editor keyboard FOCUS (state lane owns focus). # =========================================================================== # @pytest.mark.rb( jump_kind="transition", expect="SQL editor holds keyboard focus after click (typed char lands in editor)", ) def test_sql_editor_receives_focus(app, rb): frame = _reset(app) rb.enter("execute") _select_tab(frame, "Execute SQL", settle=0.8) ed = _sql_editor(frame) assert ed is not None, "SQL editor not found on Execute SQL tab" # Clear whatever is there so the readback is unambiguous. rb_atspi.click_node(ed); time.sleep(0.25) rb_atspi.key("ctrl+a"); rb_atspi.key("Delete"); time.sleep(0.2) empty = rb_atspi.get_text(_sql_editor(frame)) check.equal(empty, "", f"editor should be empty before focus proof, got {empty!r}") # The user path: click the editor to give it focus, then type. The focus # CONSEQUENCE a dead shell cannot fake: the keystroke lands in THIS widget's # text. Scintilla reports FOCUSED sticky-True, so the typed-char round-trip # is the discriminating verdict; is_focused is the additive AT-SPI check. rb_atspi.click_node(ed); time.sleep(0.25) focused = rb_atspi.is_focused(_sql_editor(frame)) rb_atspi.type_text("Z"); time.sleep(0.3) txt = rb_atspi.get_text(_sql_editor(frame)) check.equal(txt, "Z", f"typed char should appear in the focused SQL editor, got {txt!r}") check.is_true(focused, f"SQL editor should report FOCUSED after click, got {focused}") # Restore: clear the editor. rb_atspi.click_node(_sql_editor(frame)); time.sleep(0.2) rb_atspi.key("ctrl+a"); rb_atspi.key("Delete"); time.sleep(0.2) # =========================================================================== # # 8. Browse-Data selector: selection-state transition + row-count consequence. # (Computation lane owns the cell VALUES; STATE owns the selector state.) # =========================================================================== # @pytest.mark.rb( jump_kind="transition", expect="table selection departments->employees, row-count 3->8", ) def test_browse_table_combo_selection_state(app, rb, results_dir): frame = _reset(app) rb.enter("browse") _select_tab(frame, "Browse Data", settle=1.0) # Normalize to departments so the change is a real transition. _select_browse_table(frame, "departments"); time.sleep(0.4) combo = _browse_combo(frame) assert combo is not None, "Browse-Data table combo not found" name_before = combo.get_name() or "" rows_before = _rowcount_label(frame) check.equal(name_before, "departments", f"combo should show 'departments' before, got {name_before!r}") check.equal(rows_before, "1 - 3 of 3", f"row-count should be '1 - 3 of 3' before, got {rows_before!r}") # Select employees -> selector NAME transitions and row-count changes. name_after = _select_browse_table(frame, "employees") time.sleep(0.4) rows_after = _rowcount_label(frame) check.equal(name_after, "employees", f"combo selection should transition to 'employees', got {name_after!r}") check.not_equal(name_before, name_after, "table selector NAME did not change") check.equal(rows_after, "1 - 8 of 8", f"row-count should become '1 - 8 of 8' for employees, got {rows_after!r}") check.not_equal(rows_before, rows_after, "row-count label did not change on selection") rb_atspi.screenshot_assert( "test_browse_table_combo_selection_state", "Browse Data tab after selecting 'employees' in the table dropdown: the " "selector reads 'employees', the grid shows the 8-row employees table " "(id/name/department/salary/hired, Alice/Engineering/85000 first) and the " "row-count reads '1 - 8 of 8'.", results_dir, window=frame, ) # Restore selection to departments. _select_browse_table(frame, "departments"); time.sleep(0.4) check.equal((_browse_combo(frame).get_name() or ""), "departments", "selector not restored to departments")