|
| 1 | +// |
| 2 | +// DataGridAccessibilityMountTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import AppKit |
| 7 | +@testable import TablePro |
| 8 | +import TableProTestSupport |
| 9 | +import Testing |
| 10 | + |
| 11 | +@MainActor |
| 12 | +private final class FlippedPage: NSView { |
| 13 | + override var isFlipped: Bool { true } |
| 14 | +} |
| 15 | + |
| 16 | +// A fitsRows grid on a page that scrolls, placed by hand the way the Definition page places it. |
| 17 | +@MainActor |
| 18 | +private final class FittedGridOnPage { |
| 19 | + let grid: HostedDataGrid |
| 20 | + private let page = NSScrollView(frame: NSRect(x: 0, y: 0, width: 800, height: 400)) |
| 21 | + private let document = FlippedPage(frame: .zero) |
| 22 | + |
| 23 | + init(rows: TableRows, top: CGFloat, height: CGFloat) { |
| 24 | + var configuration = DataGridConfiguration() |
| 25 | + configuration.fitsRows = true |
| 26 | + grid = HostedDataGrid(rows: rows, configuration: configuration) |
| 27 | + page.hasVerticalScroller = true |
| 28 | + page.documentView = document |
| 29 | + grid.window.contentView = page |
| 30 | + let gridView = grid.controller.view |
| 31 | + gridView.removeFromSuperview() |
| 32 | + gridView.autoresizingMask = [] |
| 33 | + document.addSubview(gridView) |
| 34 | + place(top: top, height: height) |
| 35 | + // HostedDataGrid first laid the grid out in a plain window; a reload starts it over where it now sits. |
| 36 | + grid.model.reloadRows() |
| 37 | + grid.layout() |
| 38 | + } |
| 39 | + |
| 40 | + var fittedHeight: CGFloat { grid.controller.measuredFittingHeight } |
| 41 | + |
| 42 | + func place(top: CGFloat, height: CGFloat) { |
| 43 | + document.frame = NSRect(x: 0, y: 0, width: page.bounds.width, height: max(page.bounds.height, top + height)) |
| 44 | + grid.controller.view.frame = NSRect(x: 0, y: top, width: page.bounds.width, height: height) |
| 45 | + grid.layout() |
| 46 | + } |
| 47 | + |
| 48 | + // What an assistive client does on its first look: down to each cell's children, which prepares the cell's row. |
| 49 | + func readEveryRow() { |
| 50 | + let rows = grid.main.accessibilityAttributeValue(.children) as? [NSObject] ?? [] |
| 51 | + for row in rows { |
| 52 | + let cells = row.accessibilityAttributeValue(.children) as? [NSObject] ?? [] |
| 53 | + for cell in cells { |
| 54 | + _ = cell.accessibilityAttributeValue(.children) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + var preparedRows: IndexSet { |
| 60 | + IndexSet((0..<grid.main.numberOfRows).filter { grid.main.rowView(atRow: $0, makeIfNecessary: false) != nil }) |
| 61 | + } |
| 62 | + |
| 63 | + var mountedRows: IndexSet { |
| 64 | + IndexSet((0..<grid.main.numberOfRows).filter { |
| 65 | + grid.main.view(atColumn: 0, row: $0, makeIfNecessary: false) is DataGridAccessibilityCellView |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + var visibleRows: IndexSet { |
| 70 | + let range = grid.main.rows(in: grid.main.visibleRect) |
| 71 | + return IndexSet(integersIn: range.location..<(range.location + range.length)) |
| 72 | + } |
| 73 | + |
| 74 | + func close() { |
| 75 | + grid.close() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +extension GlobalStateSuites { |
| 80 | + @MainActor |
| 81 | + struct DataGridAccessibilityMountTests { |
| 82 | + private let everyRow = IndexSet(integersIn: 0..<5) |
| 83 | + |
| 84 | + // A fitted grid mounts at height 0 and grows once it has measured its rows. A client that reads it in |
| 85 | + // between prepares every row while none is on screen, so none got a stand-in, and growing is no scroll. |
| 86 | + @Test("A fitted grid read before it grows publishes every row once it has grown") |
| 87 | + func rowsReadBeforeTheGridGrowsMount() async throws { |
| 88 | + let wasActive = DataGridAccessibility.isActive |
| 89 | + DataGridAccessibility.isActive = true |
| 90 | + defer { DataGridAccessibility.isActive = wasActive } |
| 91 | + let page = FittedGridOnPage(rows: GridFixtureRows.people, top: 40, height: 0) |
| 92 | + defer { page.close() } |
| 93 | + |
| 94 | + page.readEveryRow() |
| 95 | + try #require(page.preparedRows == everyRow) |
| 96 | + try #require(page.mountedRows.isEmpty) |
| 97 | + |
| 98 | + page.place(top: 40, height: page.fittedHeight) |
| 99 | + #expect(page.visibleRows == everyRow) |
| 100 | + |
| 101 | + try await waitUntil { page.mountedRows == everyRow } |
| 102 | + } |
| 103 | + |
| 104 | + // The page moves a grid when a section above it collapses, which neither scrolls nor resizes the grid. |
| 105 | + @Test("A grid moved onto the page publishes its rows to the next read") |
| 106 | + func rowsMovedOntoThePageMountOnTheNextRead() async throws { |
| 107 | + let wasActive = DataGridAccessibility.isActive |
| 108 | + DataGridAccessibility.isActive = true |
| 109 | + defer { DataGridAccessibility.isActive = wasActive } |
| 110 | + let page = FittedGridOnPage(rows: GridFixtureRows.people, top: 40, height: 0) |
| 111 | + defer { page.close() } |
| 112 | + let height = page.fittedHeight |
| 113 | + page.place(top: 1_000, height: height) |
| 114 | + page.readEveryRow() |
| 115 | + try #require(page.visibleRows.isEmpty) |
| 116 | + |
| 117 | + page.place(top: 40, height: height) |
| 118 | + try #require(page.visibleRows == everyRow) |
| 119 | + page.readEveryRow() |
| 120 | + |
| 121 | + try await waitUntil { page.mountedRows == everyRow } |
| 122 | + } |
| 123 | + |
| 124 | + @Test("Only the rows on the page get a stand-in, however many a client reads") |
| 125 | + func onlyRowsOnThePageMount() async throws { |
| 126 | + let wasActive = DataGridAccessibility.isActive |
| 127 | + DataGridAccessibility.isActive = true |
| 128 | + defer { DataGridAccessibility.isActive = wasActive } |
| 129 | + let page = FittedGridOnPage(rows: GridFixtureRows.wide(rows: 80, columns: 3), top: 0, height: 0) |
| 130 | + defer { page.close() } |
| 131 | + page.place(top: 0, height: page.fittedHeight) |
| 132 | + let onPage = page.visibleRows |
| 133 | + try #require(!onPage.isEmpty && onPage.count < 80) |
| 134 | + |
| 135 | + page.readEveryRow() |
| 136 | + try await waitUntil { page.mountedRows == onPage } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments