|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | +import 'package:two_dimensional_scrollables/two_dimensional_scrollables.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + group('TableView pinned extent warnings', () { |
| 12 | + testWidgets('Warns when pinned columns exceed viewport width', ( |
| 13 | + WidgetTester tester, |
| 14 | + ) async { |
| 15 | + // Regression test for https://github.com/flutter/flutter/issues/136833 |
| 16 | + final log = <String>[]; |
| 17 | + final DebugPrintCallback oldDebugPrint = debugPrint; |
| 18 | + debugPrint = (String? message, {int? wrapWidth}) { |
| 19 | + log.add(message!); |
| 20 | + }; |
| 21 | + |
| 22 | + await tester.pumpWidget( |
| 23 | + MaterialApp( |
| 24 | + home: Scaffold( |
| 25 | + body: SizedBox( |
| 26 | + width: 200, |
| 27 | + height: 400, |
| 28 | + child: TableView.builder( |
| 29 | + columnCount: 5, |
| 30 | + rowCount: 5, |
| 31 | + pinnedColumnCount: 3, |
| 32 | + columnBuilder: (int index) => |
| 33 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 34 | + rowBuilder: (int index) => |
| 35 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 36 | + cellBuilder: (BuildContext context, TableVicinity vicinity) => |
| 37 | + const TableViewCell(child: SizedBox.shrink()), |
| 38 | + ), |
| 39 | + ), |
| 40 | + ), |
| 41 | + ), |
| 42 | + ); |
| 43 | + |
| 44 | + // Pinned columns extent = 300 (3 * 100), viewport width = 200. |
| 45 | + // A warning is expected because the pinned columns are wider than the |
| 46 | + // viewport, meaning even the pinned content cannot be fully displayed. |
| 47 | + expect( |
| 48 | + log, |
| 49 | + contains( |
| 50 | + matches( |
| 51 | + r'TableView has pinned columns with a total width of 300(\.0)?, which exceeds the viewport width of 200(\.0)?', |
| 52 | + ), |
| 53 | + ), |
| 54 | + ); |
| 55 | + debugPrint = oldDebugPrint; |
| 56 | + }); |
| 57 | + |
| 58 | + testWidgets('Warns when pinned rows exceed viewport height', ( |
| 59 | + WidgetTester tester, |
| 60 | + ) async { |
| 61 | + // Regression test for https://github.com/flutter/flutter/issues/136833 |
| 62 | + final log = <String>[]; |
| 63 | + final DebugPrintCallback oldDebugPrint = debugPrint; |
| 64 | + debugPrint = (String? message, {int? wrapWidth}) { |
| 65 | + log.add(message!); |
| 66 | + }; |
| 67 | + |
| 68 | + await tester.pumpWidget( |
| 69 | + MaterialApp( |
| 70 | + home: Scaffold( |
| 71 | + body: SizedBox( |
| 72 | + width: 400, |
| 73 | + height: 200, |
| 74 | + child: TableView.builder( |
| 75 | + columnCount: 5, |
| 76 | + rowCount: 5, |
| 77 | + pinnedRowCount: 3, |
| 78 | + columnBuilder: (int index) => |
| 79 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 80 | + rowBuilder: (int index) => |
| 81 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 82 | + cellBuilder: (BuildContext context, TableVicinity vicinity) => |
| 83 | + const TableViewCell(child: SizedBox.shrink()), |
| 84 | + ), |
| 85 | + ), |
| 86 | + ), |
| 87 | + ), |
| 88 | + ); |
| 89 | + |
| 90 | + // Pinned rows extent = 300 (3 * 100), viewport height = 200. |
| 91 | + // A warning is expected because the pinned rows are taller than the |
| 92 | + // viewport, meaning even the pinned content cannot be fully displayed. |
| 93 | + expect( |
| 94 | + log, |
| 95 | + contains( |
| 96 | + matches( |
| 97 | + r'TableView has pinned rows with a total height of 300(\.0)?, which exceeds the viewport height of 200(\.0)?', |
| 98 | + ), |
| 99 | + ), |
| 100 | + ); |
| 101 | + debugPrint = oldDebugPrint; |
| 102 | + }); |
| 103 | + |
| 104 | + testWidgets( |
| 105 | + 'Warns when pinned columns fully consume viewport width and there are unpinned columns', |
| 106 | + (WidgetTester tester) async { |
| 107 | + // Regression test for https://github.com/flutter/flutter/issues/136833 |
| 108 | + final log = <String>[]; |
| 109 | + final DebugPrintCallback oldDebugPrint = debugPrint; |
| 110 | + debugPrint = (String? message, {int? wrapWidth}) { |
| 111 | + log.add(message!); |
| 112 | + }; |
| 113 | + |
| 114 | + await tester.pumpWidget( |
| 115 | + MaterialApp( |
| 116 | + home: Scaffold( |
| 117 | + body: SizedBox( |
| 118 | + width: 200, |
| 119 | + height: 400, |
| 120 | + child: TableView.builder( |
| 121 | + columnCount: 3, |
| 122 | + rowCount: 5, |
| 123 | + pinnedColumnCount: 2, |
| 124 | + columnBuilder: (int index) => |
| 125 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 126 | + rowBuilder: (int index) => |
| 127 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 128 | + cellBuilder: (BuildContext context, TableVicinity vicinity) => |
| 129 | + const TableViewCell(child: SizedBox.shrink()), |
| 130 | + ), |
| 131 | + ), |
| 132 | + ), |
| 133 | + ), |
| 134 | + ); |
| 135 | + |
| 136 | + // Pinned columns extent = 200 (2 * 100), viewport width = 200. |
| 137 | + // There is 1 unpinned column (columnCount: 3, pinnedColumnCount: 2). |
| 138 | + // Since the pinned columns take up the entire viewport width, the |
| 139 | + // unpinned column will never be visible during scrolling. |
| 140 | + expect( |
| 141 | + log, |
| 142 | + contains( |
| 143 | + 'TableView has pinned columns that fully consume the viewport width. Unpinned columns will not be visible.', |
| 144 | + ), |
| 145 | + ); |
| 146 | + debugPrint = oldDebugPrint; |
| 147 | + }, |
| 148 | + ); |
| 149 | + |
| 150 | + testWidgets( |
| 151 | + 'Warns when pinned rows fully consume viewport height and there are unpinned rows', |
| 152 | + (WidgetTester tester) async { |
| 153 | + // Regression test for https://github.com/flutter/flutter/issues/136833 |
| 154 | + final log = <String>[]; |
| 155 | + final DebugPrintCallback oldDebugPrint = debugPrint; |
| 156 | + debugPrint = (String? message, {int? wrapWidth}) { |
| 157 | + log.add(message!); |
| 158 | + }; |
| 159 | + |
| 160 | + await tester.pumpWidget( |
| 161 | + MaterialApp( |
| 162 | + home: Scaffold( |
| 163 | + body: SizedBox( |
| 164 | + width: 400, |
| 165 | + height: 200, |
| 166 | + child: TableView.builder( |
| 167 | + columnCount: 5, |
| 168 | + rowCount: 3, |
| 169 | + pinnedRowCount: 2, |
| 170 | + columnBuilder: (int index) => |
| 171 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 172 | + rowBuilder: (int index) => |
| 173 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 174 | + cellBuilder: (BuildContext context, TableVicinity vicinity) => |
| 175 | + const TableViewCell(child: SizedBox.shrink()), |
| 176 | + ), |
| 177 | + ), |
| 178 | + ), |
| 179 | + ), |
| 180 | + ); |
| 181 | + |
| 182 | + // Pinned rows extent = 200 (2 * 100), viewport height = 200. |
| 183 | + // There is 1 unpinned row (rowCount: 3, pinnedRowCount: 2). |
| 184 | + // Since the pinned rows take up the entire viewport height, the |
| 185 | + // unpinned row will never be visible during scrolling. |
| 186 | + expect( |
| 187 | + log, |
| 188 | + contains( |
| 189 | + 'TableView has pinned rows that fully consume the viewport height. Unpinned rows will not be visible.', |
| 190 | + ), |
| 191 | + ); |
| 192 | + debugPrint = oldDebugPrint; |
| 193 | + }, |
| 194 | + ); |
| 195 | + |
| 196 | + testWidgets( |
| 197 | + 'Does not warn when all columns are pinned even if they consume viewport', |
| 198 | + (WidgetTester tester) async { |
| 199 | + // Regression test for https://github.com/flutter/flutter/issues/136833 |
| 200 | + final log = <String>[]; |
| 201 | + final DebugPrintCallback oldDebugPrint = debugPrint; |
| 202 | + debugPrint = (String? message, {int? wrapWidth}) { |
| 203 | + log.add(message!); |
| 204 | + }; |
| 205 | + |
| 206 | + await tester.pumpWidget( |
| 207 | + MaterialApp( |
| 208 | + home: Scaffold( |
| 209 | + body: SizedBox( |
| 210 | + width: 200, |
| 211 | + height: 400, |
| 212 | + child: TableView.builder( |
| 213 | + columnCount: 2, |
| 214 | + rowCount: 5, |
| 215 | + pinnedColumnCount: 2, |
| 216 | + columnBuilder: (int index) => |
| 217 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 218 | + rowBuilder: (int index) => |
| 219 | + const TableSpan(extent: FixedTableSpanExtent(100)), |
| 220 | + cellBuilder: (BuildContext context, TableVicinity vicinity) => |
| 221 | + const TableViewCell(child: SizedBox.shrink()), |
| 222 | + ), |
| 223 | + ), |
| 224 | + ), |
| 225 | + ), |
| 226 | + ); |
| 227 | + |
| 228 | + // Pinned columns extent = 200 (2 * 100), viewport width = 200. |
| 229 | + // Although the pinned columns fully consume the viewport width, |
| 230 | + // ALL columns are pinned (columnCount: 2, pinnedColumnCount: 2). |
| 231 | + // Since there are no unpinned columns, no warning is issued about |
| 232 | + // unpinned columns being hidden. |
| 233 | + expect( |
| 234 | + log, |
| 235 | + isNot(contains(contains('Unpinned columns will not be visible'))), |
| 236 | + ); |
| 237 | + debugPrint = oldDebugPrint; |
| 238 | + }, |
| 239 | + ); |
| 240 | + }); |
| 241 | +} |
0 commit comments