"""BUTTON lane — DB Browser for SQLite (Qt5). BUTTON == EFFECT-BY-CONSEQUENCE for PUSH buttons. Each test PRESSES a specific push button and asserts the app-authored CONSEQUENCE that press causes — never that the button merely exists / is enabled / "has a Press action" / left the app alive. A dead shell that renders the button but wires it to nothing MUST fail every test here, because the verdict is the observable effect: * "New tab" (Execute SQL toolbar) -> the SQL sub-tab COUNT increases by 1. * "Clear" (SQL Log dock) -> the log text LENGTH drops to 0. * "Execute SQL" (Execute SQL toolbar) -> the result GRID gains the computed cell. * "Open Database"(DB toolbar) -> the "Choose a database file" CHOOSER appears. * "Create Table" (Structure toolbar) -> the "Edit table definition" DIALOG appears. * "Create Index" (Structure toolbar) -> the "Edit Index Schema" DIALOG appears. Toggle-CHECKED flips are ceded to the state lane; the calculator-style digit/ operator sequence to computation. This lane presses a PUSH button and reads back the consequence it triggers. Dialog/chooser openers (Open Database, Create Table, Create Index) are driven with open_modal() (async xdotool click) so the app's nested modal loop never hangs the module; the new window is then read by name and dismissed with press_escape(). Inline-effect buttons (New tab, Clear, Execute SQL) use press() (= do_action Press). Each test escapes stray dialogs at the start and re-derives the frame/tab, so the suite is order-independent. """ import time import pytest, pytest_check as check import rb_atspi from rb_atspi import rb_showing, rb_reach from gi.repository import Atspi DB = "/workspace/output/fixtures/sample.db" # Main page tabs / dock tabs — anything NOT in this set on the Execute SQL area is # a user SQL sub-tab ("SQL 1*", "SQL 2", ...). Used to COUNT sub-tabs for New tab. _MAIN_TABS = { "Database Structure", "Browse Data", "Edit Pragmas", "Execute SQL", "SQL Log", "Plot", "DB Schema", "Edit Database Cell", } # --------------------------------------------------------------------------- # # 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 _reset(app): """Re-derive the frame and clear any stray popup/dialog at test start, so each test is independent of whatever a prior test left on screen.""" frame = rb_atspi.get_main_frame(app) for _ in range(3): rb_atspi.press_escape(); time.sleep(0.15) return frame def _switch_tab(frame, name, settle=0.8): """Activate a main page tab by name (do_action Press; real click fallback).""" t = rb_atspi.find_first(frame, role="page tab", name=name) assert t is not None, f"main tab {name!r} not found" if not rb_atspi.do_action(t, "Press"): rb_atspi.click_node(t) time.sleep(settle) return t def _sql_subtabs(frame): """Names of the user SQL sub-tabs on the Execute SQL area (page tabs whose name is not a main/dock tab).""" return [ (pt.get_name() or "") for pt in rb_atspi.find_all(frame, role="page tab") if (pt.get_name() or "") not in _MAIN_TABS ] def _sql_editor(frame): """The Scintilla SQL editor: the showing role=text at the TOP-LEFT of the Execute SQL tab (x<400, tall, smallest y).""" 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 if e.x < 400 and e.height > 80 and e.width > 200: if best is None or e.y < best[1]: best = (t, e.y) return best[0] if best else None def _result_table(frame): """The Execute SQL result grid: showing role=table with real extents at the left (x<400) below the editor (y>300).""" best = None for tb in rb_atspi.find_all(frame, role="table"): if not rb_atspi.is_showing(tb): continue e = _ext(tb) if e is None or e.width <= 0: continue if e.x < 400 and e.y > 300: if best is None or e.y < best[1]: best = (tb, e.y) return best[0] if best else None def _sql_log_dock(app): return rb_atspi.find_first(app, role="frame", name="SQL Log") def _log_text_len(dock): """Length of the LARGEST role=text under the SQL Log dock (the log view).""" if dock is None: return None lens = [len(rb_atspi.get_text(t)) for t in rb_atspi.find_all(dock, role="text")] return max(lens) if lens else 0 def _on_screen(node): """True when node has positive on-screen extents (guards screenshots against a floated/off-screen dock, whose crop would be blank).""" e = _ext(node) return bool(e and e.width > 0 and e.height > 0 and e.x >= 0 and e.y >= 0) def _open_via_toolbar_button(app, frame, btn, target, rb, slug): """Press a DIALOG/CHOOSER-opening toolbar button with open_modal (async click, no nested-loop hang), poll for the target window, and return (reached, before, after). rb.enter() marks the real crossing (depth 1). Snapshotting `before` immediately prior to the click means rb_reach proves the window genuinely APPEARED, not that it was pre-present.""" assert btn is not None, f"toolbar button for {target!r} not found" before = rb_showing(app) rb.enter(slug) rb_atspi.open_modal(btn) rb_atspi.wait_for( lambda: any( target.lower() in (w.get_name() or "").lower() and rb_atspi.is_showing(w) for w in (rb_atspi.find_all(app, role="dialog") + rb_atspi.find_all(app, role="file chooser")) ), timeout=4.0, ) time.sleep(0.4) after = rb_showing(app) return rb_reach(before, after, target=target), before, after def _dismiss(times=2): for _ in range(times): rb_atspi.press_escape(); time.sleep(0.35) # --------------------------------------------------------------------------- # # 1. "New tab" (Execute SQL toolbar) -> SQL sub-tab COUNT increases by 1. # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="transition", expect="SQL sub-tab count increases by 1 (e.g. 1->2) after pressing New tab") def test_new_tab_button_adds_sql_tab(app, frame, rb, results_dir): _reset(app) _switch_tab(frame, "Execute SQL") rb.enter("execute") before = _sql_subtabs(frame) n_before = len(before) check.greater_equal(n_before, 1, f"expected >=1 SQL sub-tab initially, got {before}") newtab = rb_atspi.find_first(frame, role="push button", name="New tab") assert newtab is not None, "'New tab' button not found on Execute SQL toolbar" rb_atspi.press(newtab) # poll until a new sub-tab appears rb_atspi.wait_for(lambda: len(_sql_subtabs(frame)) > n_before, timeout=4.0) time.sleep(0.3) after = _sql_subtabs(frame) n_after = len(after) check.equal( n_after, n_before + 1, f"'New tab' press should add exactly 1 SQL sub-tab: before={before} after={after}", ) # the newly added tab is one that was not present before added = [t for t in after if t not in before] check.greater_equal( len(added), 1, f"a new SQL sub-tab name should appear (before={before}, after={after})", ) rb_atspi.screenshot_assert( "test_new_tab_button_adds_sql_tab", "The Execute SQL area now shows an additional editor sub-tab: at least two " "SQL editor tabs (e.g. 'SQL 1' and 'SQL 2') are visible in the sub-tab bar " "just under the SQL toolbar, the last one added by the New tab button.", results_dir, window=frame, ) # --------------------------------------------------------------------------- # # 2. "Clear" (SQL Log dock) -> log text LENGTH drops to 0. # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="transition", expect="SQL Log text goes non-empty -> empty (length 0) after pressing Clear") def test_clear_button_empties_sql_log(app, frame, rb, results_dir): _reset(app) dock = _sql_log_dock(app) assert dock is not None, "SQL Log dock (frame 'SQL Log') not found" # Ensure the log is non-empty: run a trivial query via the Execute SQL editor, # which logs the SQL into the SQL Log dock. if (_log_text_len(dock) or 0) == 0: _switch_tab(frame, "Execute SQL") ed = _sql_editor(frame) if ed is not None: rb_atspi.click_node(ed); time.sleep(0.25) rb_atspi.key("ctrl+a"); rb_atspi.key("Delete"); time.sleep(0.15) rb_atspi.type_text("SELECT 1;"); time.sleep(0.3) exb = rb_atspi.find_first(frame, role="push button", name="Execute SQL") if exb: rb_atspi.press(exb); time.sleep(1.0) dock = _sql_log_dock(app) before_len = _log_text_len(dock) or 0 check.greater(before_len, 0, f"SQL Log should be non-empty before Clear, len={before_len}") rb.enter("sql_log_clear") clr = rb_atspi.find_first(dock, role="push button", name="Clear") assert clr is not None, "'Clear' button not found inside the SQL Log dock" rb_atspi.press(clr) rb_atspi.wait_for(lambda: (_log_text_len(_sql_log_dock(app)) or 0) == 0, timeout=4.0) time.sleep(0.3) after_len = _log_text_len(_sql_log_dock(app)) check.equal(after_len, 0, f"'Clear' should empty the SQL Log text, got len={after_len}") check.not_equal(before_len, after_len, "SQL Log length did not change after Clear") # The dock may be floated off-screen in a warm session; only screenshot when it # is actually on-screen so we never ship a blank reference shot. dock = _sql_log_dock(app) if _on_screen(dock): rb_atspi.screenshot_assert( "test_clear_button_empties_sql_log", "The SQL Log panel is now empty: its log text area shows no SQL " "statements after the Clear button was pressed.", results_dir, window=dock, ) # --------------------------------------------------------------------------- # # 3. "Execute SQL" (Execute SQL toolbar) -> result GRID gains the computed cell. # Button-triggered execution: the CONSEQUENCE is the grid populating with the # computed value (distinct from computation lane's F5/value focus). # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="value", expect="result grid shows a cell '99' after pressing the Execute SQL button") def test_execute_button_populates_grid(app, frame, rb, results_dir): _reset(app) _switch_tab(frame, "Execute SQL") rb.enter("execute") ed = _sql_editor(frame) assert ed is not None, "SQL editor not found on Execute SQL tab" rb_atspi.click_node(ed); time.sleep(0.25) rb_atspi.key("ctrl+a"); rb_atspi.key("Delete"); time.sleep(0.15) rb_atspi.type_text("SELECT 99 AS x;"); time.sleep(0.3) check.is_in("99", rb_atspi.get_text(ed), "SQL text was not typed into the editor") execBtn = rb_atspi.find_first(frame, role="push button", name="Execute SQL") assert execBtn is not None, "'Execute SQL' toolbar button not found" rb_atspi.press(execBtn) # Poll for the grid to gain the computed cell (the button's consequence). def _has99(): tb = _result_table(frame) return tb is not None and "99" in [c.get_name() or "" for c in rb_atspi.find_all(tb, role="table cell")] rb_atspi.wait_for(_has99, timeout=6.0) time.sleep(0.3) tb = _result_table(frame) cells = [c.get_name() or "" for c in rb_atspi.find_all(tb, role="table cell")] if tb else [] hdrs = [h.get_name() or "" for h in rb_atspi.find_all(tb, role="table column header")] if tb else [] check.is_true(tb is not None, "result grid not found after pressing Execute SQL") check.is_in("99", cells, f"result grid should contain the computed cell '99', cells={cells}") check.is_in("x", hdrs, f"result grid header should be 'x', headers={hdrs}") rb_atspi.screenshot_assert( "test_execute_button_populates_grid", "The Execute SQL result grid is populated after pressing the Execute SQL " "button: a single column headed 'x' with one data row containing the value 99.", results_dir, window=frame, ) # --------------------------------------------------------------------------- # # 4. "Open Database" (DB toolbar) -> "Choose a database file" chooser appears. # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="reach", expect="'Choose a database file' file-chooser dialog visible") def test_open_database_button_opens_chooser(app, frame, rb, results_dir): frame = _reset(app) btn = rb_atspi.find_first(frame, role="push button", name="Open Database") reached, before, after = _open_via_toolbar_button( app, frame, btn, "Choose a database file", rb, "open_db_chooser") check.is_true( reached, "'Open Database' button did not open the 'Choose a database file' chooser " f"(newly-shown windows: {[k for k in (after - before) if 'dialog|' in k or 'chooser' in k.lower()]})", ) win = rb_atspi.find_first(app, role="dialog", name="Choose a database file") \ or rb_atspi.find_first(app, role="file chooser", name="Choose a database file") check.is_true(win is not None and rb_atspi.is_showing(win), "'Choose a database file' chooser not present/showing") rb_atspi.screenshot_assert( "test_open_database_button_opens_chooser", "A native file-chooser dialog titled 'Choose a database file' is open on top " "of the DB Browser window, showing a file browser for selecting a database " "file (folders, filename field, Open/Cancel buttons).", results_dir, ) _dismiss(1) # single Escape closes the chooser; do NOT open any file # --------------------------------------------------------------------------- # # 5. "Create Table" (Database Structure toolbar) -> "Edit table definition" dialog. # Menu lane covers this via the Edit MENU; this covers the TOOLBAR BUTTON — a # different widget/trigger, hence a legitimately distinct scored behavior. # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="reach", expect="'Edit table definition' dialog visible") def test_create_table_button_opens_dialog(app, frame, rb, results_dir): frame = _reset(app) _switch_tab(frame, "Database Structure") rb.enter("structure") btn = rb_atspi.find_first(frame, role="push button", name="Create Table") reached, before, after = _open_via_toolbar_button( app, frame, btn, "Edit table definition", rb, "create_table_dialog") check.is_true( reached, "'Create Table' toolbar button did not open the 'Edit table definition' dialog " f"(newly-shown dialogs: {[k for k in (after - before) if 'dialog|' in k]})", ) dlg = rb_atspi.find_first(app, role="dialog", name="Edit table definition") check.is_true(dlg is not None and rb_atspi.is_showing(dlg), "'Edit table definition' dialog not present/showing") rb_atspi.screenshot_assert( "test_create_table_button_opens_dialog", "The 'Edit table definition' dialog is open, showing a table-name text field " "and a column/field editor grid with add/remove field controls.", results_dir, ) _dismiss(2) # --------------------------------------------------------------------------- # # 6. "Create Index" (Database Structure toolbar) -> "Edit Index Schema" dialog. # Observed title verified live: open_modal -> new dialog "Edit Index Schema". # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="reach", expect="'Edit Index Schema' index dialog visible") def test_create_index_button_opens_dialog(app, frame, rb, results_dir): frame = _reset(app) _switch_tab(frame, "Database Structure") rb.enter("structure") btn = rb_atspi.find_first(frame, role="push button", name="Create Index") reached, before, after = _open_via_toolbar_button( app, frame, btn, "Edit Index Schema", rb, "create_index_dialog") check.is_true( reached, "'Create Index' toolbar button did not open the 'Edit Index Schema' dialog " f"(newly-shown dialogs: {[k for k in (after - before) if 'dialog|' in k]})", ) dlg = rb_atspi.find_first(app, role="dialog", name="Edit Index Schema") check.is_true(dlg is not None and rb_atspi.is_showing(dlg), "'Edit Index Schema' dialog not present/showing") rb_atspi.screenshot_assert( "test_create_index_button_opens_dialog", "The 'Edit Index Schema' dialog is open, showing an index-name field and a " "table selector for choosing the table and columns to index.", results_dir, ) _dismiss(2)