"""BUTTON lane for LabPlot2 (org.kde.labplot2): push-button EFFECT-BY-CONSEQUENCE. Every test presses a REAL toolbar ``push button`` and asserts the specific app-authored CONSEQUENCE that press causes -- measured BEFORE -> press -> AFTER, then REVERSED (Ctrl+Z) so the delta itself is the verdict and the project is left as it was found. A dead shell that draws the same toolbar but wires no handlers fails every test (no row appears / no row-or-column count changes). Two consequence families are owned here: 1. ADD-OBJECT toolbar buttons (role "push button", toolbar band y~=95, distinct from the same-named menu-bar items at y~=69) -> the Project Explorer ``tree`` gains a specifically-named 4-cell row: * "Note" -> a "Note" row (tree table-cell count +4) * "Workbook" -> a "Workbook" row (+4) * "Folder" -> a "Folder" row (+4) * "Spreadsheet" -> a "Spreadsheet 1" row PLUS its two default child Column rows (+12 = 3 new rows x 4 cells) -- a DISTINCT compound consequence that a single-row clone cannot fake. These buttons are INLINE (no modal), so ``press()`` is correct. The structure lane already owns Worksheet + Matrix (single-row +4) and the Note MDI-subwindow delta; this lane deliberately picks DIFFERENT object types (Note/Workbook/Folder/Spreadsheet) and frames each verdict as "a row named X appears" (the button's own consequence) -- plus the Spreadsheet compound +12 delta which no other lane asserts. 2. INSERT ROW/COLUMN toolbar buttons act on the ACTIVE spreadsheet (a cell must be selected first): * "Insert Row Below" -> the spreadsheet's ``table row header`` count rises by one (100 -> 101). * "Insert Column Left" -> the count of ``{Double}`` ``table column header`` cells rises by one (2 -> 3; a new "3 {Double} [Y]" header appears). Undo restores the original dimension. Verified on the running reference (live probe on this build): - toolbar add buttons Note/Workbook/Folder each grow the tree 16 -> 20; the new row's Name cell reads "Note"/"Workbook"/"Folder"; Ctrl+Z -> back to 16. - Spreadsheet button grows the tree 16 -> 28 (a "Spreadsheet 1" row + 2 Column child rows); Ctrl+Z -> 16. - The spreadsheet grid ``table`` is exposed only when its MDI sub-window is ACTIVE; a module fixture double-clicks the "Spreadsheet" tree row to activate it (wait for role "table" with 100 row headers, 2 {Double} column headers). - Insert Row Below: 100 -> 101 row headers (undo -> 100). - Insert Column Left: 2 -> 3 {Double} column headers (undo -> 2). Deliberately NOT here: CHECKED dock toggles (state lane), the cell VALUE round-trip (computation lane), dialog identity/title text (menu/text lanes), and file/modal/destructive buttons (New/Open/Save/Print/Export/Quit/Delete/Remove). """ from __future__ import annotations import time import pytest import pytest_check as check import rb_atspi # --------------------------------------------------------------------------- # # Local workflow helpers (built ONLY on rb_atspi primitives -- no per-app kit) # --------------------------------------------------------------------------- # def _tree(frame): """The Project Explorer -- the single role=tree left dock.""" return rb_atspi.find_first(frame, role="tree") def _tree_cells(frame): tree = _tree(frame) return rb_atspi.find_all(tree, role="table cell") if tree is not None else [] def _tree_cell_count(frame): """SHAPE metric: number of ``table cell`` leaves (rows x 4 columns).""" return len(_tree_cells(frame)) def _tree_row_names(frame): return [c.get_name() or "" for c in _tree_cells(frame)] def _toolbar_add_button(frame, name): """The TOOLBAR add-object push button of the given name. Disambiguated from the same-named menu-bar entry (role 'menu item', y~=69) by role='push button' + a live 'Press' action. Prefer one whose on-screen extent sits in the toolbar band (y ~90..160); AT-SPI can momentarily report a stale extent during a window relayout, so fall back to the first showing pressable candidate rather than over-filtering on the band.""" cands = [ b for b in rb_atspi.find_all(frame, role="push button", name=name) if rb_atspi.is_showing(b) and "Press" in rb_atspi.action_names(b) ] for b in cands: try: e = b.get_extents(rb_atspi.Atspi.CoordType.SCREEN) if e.width > 0 and e.height > 0 and 80 <= e.y <= 160: return b except Exception: continue return cands[0] if cands else None def _showing_named_action(root, name): """Return the visible toolbar/overflow representation of ``name``. Qt exposes a QAction as a ``push button`` while it fits in the toolbar and as a ``menu item`` after the toolbar extension popup is opened. Requiring SHOWING plus a real action avoids accidentally selecting the permanently hidden copy in the normal Spreadsheet menu. """ # A Qt popup is a transient top-level accessible. Depending on the AT-SPI # bridge version it is not necessarily attached below the cached application # object, so also search the desktop root after opening an overflow/context menu. roots = (root, rb_atspi._desktop()) for search_root in roots: for role in ("push button", "menu item"): for node in rb_atspi.find_all(search_root, role=role, name=name): if rb_atspi.is_showing(node) and rb_atspi.action_names(node): return node return None def _toolbar_extension_buttons(frame): """Find Qt's right-edge ``>>`` toolbar extension button. Its accessible name is empty on some Qt builds, so locate it structurally: it is a small, visible, pressable button inside a tool bar, furthest to the right. A width cap prevents a normal labelled action from being mistaken for the extension. """ candidates = [] for toolbar in rb_atspi.find_all( frame, role="tool bar", name="Spreadsheet Toolbar" ): for role in ("push button", "toggle button"): for node in rb_atspi.find_all(toolbar, role=role): if not rb_atspi.is_showing(node) or not rb_atspi.action_names(node): continue # Ordinary toolbar actions can also be narrow. Qt's extension # button is anonymous; never click a named action while probing # for the overflow menu because that mutates the spreadsheet. if (node.get_name() or "").strip(): continue try: ext = node.get_extents(rb_atspi.Atspi.CoordType.SCREEN) except Exception: continue if 0 < ext.width <= 48 and ext.height > 0: candidates.append((ext.x, node)) return [node for _x, node in sorted(candidates, key=lambda item: item[0], reverse=True)] def _press_toolbar_action(app, frame, name): """Press a toolbar QAction whether inline or inside Qt's overflow popup.""" direct = _toolbar_add_button(frame, name) if direct is not None: # Use a real click here. Qt reports ``Press`` successfully for some # QToolButtons even when the backing QAction is in the overflow layout, # but no triggered() signal is emitted. rb_atspi.click_node(direct) return True # At 1280px the spreadsheet's trailing column actions do not fit. Open the # actual toolbar extension and press the QAction representation that becomes # visible in that popup; this remains the toolbar user path, not a menu-bar # substitute. for extension in _toolbar_extension_buttons(frame): if rb_atspi.click_node(extension) is None: continue rb_atspi.wait_for( lambda: _showing_named_action(app, name) is not None, timeout=2.0, interval=0.1, ) action = _showing_named_action(app, name) if action is not None: _press_button(action) return True rb_atspi.press_escape() # Qt's toolbar-extension button is not exposed as an Accessible on all # builds. It is nevertheless a real widget at the right edge of the # Spreadsheet Toolbar, so open it with a physical click and then resolve # the now-visible QAction by name. for toolbar in rb_atspi.find_all(frame, role="tool bar", name="Spreadsheet Toolbar"): if not rb_atspi.is_showing(toolbar): continue try: ext = toolbar.get_extents(rb_atspi.Atspi.CoordType.SCREEN) except Exception: continue if ext.width <= 16 or ext.height <= 0: continue click_points = [(ext.x + ext.width - 8, ext.y + ext.height // 2)] # KXMLGUI lays out the extension widget at the right edge of the main # window, outside the toolbar's own AT-SPI extent. This is the layout # used by the 1920x1080 eval desktop. try: frame_ext = frame.get_extents(rb_atspi.Atspi.CoordType.SCREEN) click_points.append( (frame_ext.x + frame_ext.width - 8, ext.y + ext.height // 2) ) except Exception: pass for x, y in dict.fromkeys(click_points): rb_atspi.xdo("mousemove", x, y, "click", "1") rb_atspi.wait_for( lambda: _showing_named_action(app, name) is not None, timeout=2.0, interval=0.1, ) action = _showing_named_action(app, name) if action is not None: _press_button(action) return True rb_atspi.press_escape() return False def _press_column_context_action(app, frame, name): """Open a spreadsheet column-header context menu and activate ``name``. LabPlot owns the insert-column actions in ``m_columnMenu``. They are not members of the top-level Spreadsheet menu, so that menu is not a valid fallback when a narrow desktop moves the toolbar action into overflow. """ columns = _data_columns(frame) if not columns: return False # Select the complete column first. Shift+F10 then asks the focused table # view to open its column context menu and is more reliable under Xvfb than # synthesizing a right click directly on a QHeaderView accessible. if rb_atspi.click_node(columns[0]) is None: return False rb_atspi.key("shift+F10") if rb_atspi.wait_for( lambda: _showing_named_action(app, name) is not None, timeout=3.0, interval=0.1, ): _press_button(_showing_named_action(app, name)) return True rb_atspi.press_escape() if rb_atspi.click_node(columns[0], button=3) is None: return False if not rb_atspi.wait_for( lambda: _showing_named_action(app, name) is not None, timeout=3.0, interval=0.1, ): rb_atspi.press_escape() return False _press_button(_showing_named_action(app, name)) return True def _press_button(btn): """Fire an INLINE toolbar button. ``press()`` (AT-SPI 'Press' action) is right for these -- they mutate the model in place, they do NOT open a modal. Fall back to a real xdotool click if the action didn't register.""" if not rb_atspi.press(btn): rb_atspi.click_node(btn) def _undo(app, rounds=1): """Reverse the last model change(s) so the test is order-independent and leaves the project as found. The tree/spreadsheet delta (and its reversal) is the verdict, not this call.""" for _ in range(rounds): rb_atspi.key("ctrl+z") time.sleep(0.5) def _add_object_consequence(app, rb, frame, obj_name, expect_rows, results_dir, test_name, shoot_desc=None): """Shared add-object consequence: press the toolbar button, assert the tree gained ``expect_rows`` new 4-cell rows AND a row named ``obj_name`` appeared that was not the trigger, then UNDO and assert the count restores. ``before`` is measured immediately BEFORE the press (never assume launch state -- a prior test may have mutated the project).""" btn = _toolbar_add_button(frame, obj_name) assert btn is not None, f"toolbar '{obj_name}' push button not found/showing" before = _tree_cell_count(frame) assert before > 0, "Project Explorer tree exposes no cells (precondition failed)" before_names = _tree_row_names(frame) prior_count = before_names.count(obj_name) want = expect_rows * 4 rb.enter(f"add {obj_name}") # a real tree-shape crossing -> measured depth 1 _press_button(btn) rb_atspi.wait_for(lambda: _tree_cell_count(frame) >= before + want, timeout=6.0) after = _tree_cell_count(frame) after_names = _tree_row_names(frame) # CONSEQUENCE verdict: the tree grew by exactly the object's row footprint... check.greater(after, before, f"{obj_name}: pressing the button added no tree rows " f"({before}->{after}) -- no consequence") check.equal(after - before, want, f"{obj_name}: expected +{want} tree cells ({expect_rows} row(s)), " f"got {after - before}") # ...and a NEW row whose Name cell reads the object type appeared. check.greater(after_names.count(obj_name), prior_count, f"{obj_name}: no new '{obj_name}' row appeared after the press " f"(names={after_names})") if shoot_desc: rb_atspi.screenshot_assert(test_name, shoot_desc, results_dir, window=frame) # Reverse it: undo removes the added row(s) and RESTORES the original shape. _undo(app, rounds=1) rb_atspi.wait_for(lambda: _tree_cell_count(frame) <= before, timeout=6.0) restored = _tree_cell_count(frame) check.equal(restored, before, f"{obj_name}: undo did not restore the tree cell count " f"({before} -> {after} -> {restored})") # --------------------------------------------------------------------------- # # Spreadsheet-MDI activation (needed by the insert-row/column tests) # --------------------------------------------------------------------------- # def _sheet(frame): """The spreadsheet data grid = the role=table whose column headers name a ``{Double}`` column (distinguishes it from the Project Explorer tree). Exposed on AT-SPI only while the Spreadsheet MDI sub-window is ACTIVE.""" for t in rb_atspi.find_all(frame, role="table"): hdrs = [h.get_name() or "" for h in rb_atspi.find_all(t, role="table column header")] if any("{Double}" in h for h in hdrs): return t return None def _row_headers(frame): s = _sheet(frame) return rb_atspi.find_all(s, role="table row header") if s is not None else [] def _data_columns(frame): s = _sheet(frame) if s is None: return [] return [h for h in rb_atspi.find_all(s, role="table column header") if "{Double}" in (h.get_name() or "")] def _activate_spreadsheet(frame): """Bring the Spreadsheet MDI sub-window forward so its grid ``table`` is exposed: double-click the 'Spreadsheet' row in the Project Explorer tree, then wait for the table to appear. Returns the sheet node (or None).""" if _sheet(frame) is not None: return _sheet(frame) tree = _tree(frame) target = None if tree is not None: for c in rb_atspi.find_all(tree, role="table cell"): if (c.get_name() or "") == "Spreadsheet" and rb_atspi.is_showing(c): target = c break if target is not None: ctr = rb_atspi.center(target) if ctr is not None: rb_atspi.xdo("mousemove", ctr[0], ctr[1], "click", "1") time.sleep(0.3) rb_atspi.xdo("mousemove", ctr[0], ctr[1], "click", "1", "click", "1") rb_atspi.wait_for(lambda: _sheet(frame) is not None, timeout=6.0) return _sheet(frame) def _select_first_cell(frame): """Real-click the first data cell so the insert buttons have a target row/ column (they act relative to the current selection).""" s = _sheet(frame) if s is None: return False cells = rb_atspi.find_all(s, role="table cell") if not cells: return False rb_atspi.click_node(cells[0]) time.sleep(0.4) return True @pytest.fixture() def sheet(app, frame): """Per-test: ensure the Spreadsheet MDI is active (grid ``table`` exposed) and a data cell is selected, so the insert-row/column buttons have a live target. Skips the test if the spreadsheet grid can't be brought forward.""" s = _activate_spreadsheet(frame) if s is None: pytest.skip("spreadsheet grid 'table' could not be activated on AT-SPI") _select_first_cell(frame) return _sheet(frame) # --------------------------------------------------------------------------- # # ADD-OBJECT push buttons -> a specifically-named Project Explorer row appears # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="transition", expect="pressing the Note toolbar button adds a 'Note' row to the " "Project Explorer tree (table-cell count +4); undo restores it") def test_note_button_adds_note_row(app, rb, frame, results_dir): """The toolbar 'Note' push button adds a Note object -> a new 4-cell row named 'Note' appears in the Project Explorer tree (count +4). Ctrl+Z removes it and restores the count. The named-row consequence (not mere existence) is the verdict.""" _add_object_consequence( app, rb, frame, "Note", 1, results_dir, "test_note_button_adds_note_row", shoot_desc=( "LabPlot Project Explorer (left dock): the tree now lists a 'Note' " "entry in the Name column (Type 'Note') as a new row below the " "existing 'Spreadsheet' entry -- the project gained a Note object."), ) @pytest.mark.rb(jump_kind="transition", expect="pressing the Workbook toolbar button adds a 'Workbook' row to " "the Project Explorer tree (table-cell count +4); undo restores it") def test_workbook_button_adds_workbook_row(app, rb, frame, results_dir): """The toolbar 'Workbook' push button adds a Workbook object -> a new 4-cell row named 'Workbook' appears in the tree (count +4). Ctrl+Z restores the count. A different object type from Note -- discriminates a shell that only wires one add path.""" _add_object_consequence( app, rb, frame, "Workbook", 1, results_dir, "test_workbook_button_adds_workbook_row", ) @pytest.mark.rb(jump_kind="transition", expect="pressing the Folder toolbar button adds a 'Folder' row to the " "Project Explorer tree (table-cell count +4); undo restores it") def test_folder_button_adds_folder_row(app, rb, frame, results_dir): """The toolbar 'Folder' push button adds a Folder object -> a new 4-cell row named 'Folder' appears in the tree (count +4). Ctrl+Z restores the count.""" _add_object_consequence( app, rb, frame, "Folder", 1, results_dir, "test_folder_button_adds_folder_row", ) @pytest.mark.rb(jump_kind="value", expect="pressing the Spreadsheet toolbar button adds a Spreadsheet whose " "two default child 'Column' rows appear (table-cell count +12, " "'Column' Type rows 2 -> 4); undo restores it") def test_spreadsheet_button_adds_spreadsheet_with_columns(app, rb, frame, results_dir): """DISTINCT compound consequence, asserted as a VALUE: the toolbar 'Spreadsheet' push button adds a Spreadsheet object that carries its two DEFAULT child columns. So the tree gains THREE rows (a Spreadsheet row + two 'Column'-typed rows) = +12 table cells, and the number of rows whose Type reads 'Column' rises from 2 to 4. That the button authors exactly two new 'Column' children (not zero, not one) is a specific content value a single-row or empty clone cannot reproduce. Ctrl+Z restores the tree.""" btn = _toolbar_add_button(frame, "Spreadsheet") assert btn is not None, "toolbar 'Spreadsheet' push button not found/showing" before = _tree_cell_count(frame) assert before > 0, "Project Explorer tree exposes no cells (precondition failed)" before_names = _tree_row_names(frame) before_columns = before_names.count("Column") # Type-column value; 2 on launch rb.enter("add Spreadsheet") # a real tree-shape crossing -> measured depth 1 _press_button(btn) rb_atspi.wait_for(lambda: _tree_cell_count(frame) >= before + 12, timeout=6.0) after = _tree_cell_count(frame) after_names = _tree_row_names(frame) after_columns = after_names.count("Column") # VALUE verdict: the new Spreadsheet brought exactly its two default child # columns (Type 'Column' rows 2 -> 4) and the compound +12 footprint. check.equal(after_columns - before_columns, 2, f"Spreadsheet button did not author two new 'Column' child rows " f"({before_columns} -> {after_columns}); names={after_names}") check.equal(after - before, 12, f"Spreadsheet: expected +12 tree cells (Spreadsheet + 2 Columns), " f"got {after - before}") check.greater(after_names.count("Spreadsheet"), before_names.count("Spreadsheet"), "no new 'Spreadsheet' Name row appeared after the press") _undo(app, rounds=1) rb_atspi.wait_for(lambda: _tree_cell_count(frame) <= before, timeout=6.0) restored = _tree_cell_count(frame) check.equal(restored, before, f"Spreadsheet: undo did not restore the tree cell count " f"({before} -> {after} -> {restored})") # --------------------------------------------------------------------------- # # INSERT ROW / COLUMN push buttons -> spreadsheet dimension change # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="transition", expect="Insert Row Below adds one spreadsheet row (row-header count " "100 -> 101); undo restores it") def test_insert_row_below_grows_spreadsheet(app, rb, frame, sheet, results_dir): """With the Spreadsheet MDI active and a cell selected, pressing the toolbar 'Insert Row Below' push button inserts one row -> the spreadsheet's ``table row header`` count rises by exactly one (100 -> 101; a new numbered row header appears). Ctrl+Z restores the count. The row-count DELTA is the verdict, not that the button exists.""" btn = _toolbar_add_button(frame, "Insert Row Below") assert btn is not None, "toolbar 'Insert Row Below' push button not found/showing" before = len(_row_headers(frame)) assert before > 0, "spreadsheet exposes no row headers (precondition failed)" # Two real surface crossings: the module fixture brought the Spreadsheet MDI # sub-window forward (crossing 1) and selected a data cell inside it, so the # insert acts on that in-sheet selection (crossing 2) -> measured depth 2. rb.enter("activate Spreadsheet MDI sub-window") rb.enter("select a cell inside the sheet, then insert") _press_button(btn) rb_atspi.wait_for(lambda: len(_row_headers(frame)) > before, timeout=6.0) after = len(_row_headers(frame)) check.greater(after, before, f"Insert Row Below: row-header count did not grow ({before}->{after})") check.equal(after - before, 1, f"Insert Row Below: expected exactly one new row, got {after - before}") _undo(app, rounds=1) rb_atspi.wait_for(lambda: len(_row_headers(frame)) <= before, timeout=6.0) restored = len(_row_headers(frame)) check.equal(restored, before, f"Insert Row Below: undo did not restore the row count " f"({before} -> {after} -> {restored})") @pytest.mark.rb(jump_kind="value", expect="Insert Column Left adds a data column whose new header text reads " "'3 {Double} [Y]' (Double count 2 -> 3); undo restores it") def test_insert_column_left_adds_data_column(app, rb, frame, sheet, results_dir): """With the Spreadsheet MDI active and a cell selected, pressing the toolbar 'Insert Column Left' push button inserts a data column. The app AUTHORS a specific new column header: the ``{Double}`` header count rises 2 -> 3 and a header whose text reads '3 {Double} [Y]' appears (the next column index, still a Double / Y-role). That exact header VALUE -- not merely one more header -- is the verdict; a shell that adds a blank/wrong-typed column fails it. Ctrl+Z restores the column set.""" def _hdr_texts(): s = _sheet(frame) return [h.get_name() or "" for h in rb_atspi.find_all(s, role="table column header")] \ if s is not None else [] before = len(_data_columns(frame)) before_hdrs = _hdr_texts() assert before >= 2, f"spreadsheet has fewer than 2 data columns ({before})" # Two real surface crossings (see insert-row test): the fixture activated the # Spreadsheet MDI (crossing 1) and selected a data cell within it (crossing 2). rb.enter("activate Spreadsheet MDI sub-window") rb.enter("select a cell inside the sheet, then insert column") toolbar_pressed = _press_toolbar_action(app, frame, "Insert Column Left") changed = toolbar_pressed and rb_atspi.wait_for( lambda: len(_data_columns(frame)) > before, timeout=6.0 ) if not changed: assert _press_column_context_action(app, frame, "Insert Column Left"), ( "Insert Column Left action not found in the toolbar/overflow or the " "column-header context menu" ) rb_atspi.wait_for(lambda: len(_data_columns(frame)) > before, timeout=6.0) after = len(_data_columns(frame)) after_hdrs = _hdr_texts() # VALUE verdict: the specific app-authored header text appeared, and the Double # column count grew by exactly one. check.is_true("3 {Double} [Y]" in after_hdrs, f"Insert Column Left: expected a '3 {{Double}} [Y]' header to " f"appear; headers were {before_hdrs} -> {after_hdrs}") check.equal(after - before, 1, f"Insert Column Left: expected exactly one new Double column, " f"got {after - before}") rb_atspi.screenshot_assert( "test_insert_column_left_adds_data_column", "LabPlot spreadsheet view (center MDI): the data grid now shows THREE " "numbered '{Double}' column headers across the top instead of two -- a " "newly inserted data column has appeared to the left of the original " "columns.", results_dir, window=frame, ) _undo(app, rounds=1) rb_atspi.wait_for(lambda: len(_data_columns(frame)) <= before, timeout=6.0) restored = len(_data_columns(frame)) check.equal(restored, before, f"Insert Column Left: undo did not restore the column count " f"({before} -> {after} -> {restored})")