table_test.dart 9.89 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/rendering.dart';
6
import 'package:flutter_test/flutter_test.dart';
Hixie's avatar
Hixie committed
7

8
import 'mock_canvas.dart';
Hixie's avatar
Hixie committed
9 10 11
import 'rendering_tester.dart';

RenderBox sizedBox(double width, double height) {
12
  return RenderConstrainedBox(
13
    additionalConstraints: BoxConstraints.tight(Size(width, height)),
Hixie's avatar
Hixie committed
14 15 16 17 18 19
  );
}

void main() {
  test('Table control test; tight', () {
    RenderTable table;
20
    layout(table = RenderTable(textDirection: TextDirection.ltr));
Hixie's avatar
Hixie committed
21 22 23

    expect(table.size.width, equals(800.0));
    expect(table.size.height, equals(600.0));
24 25 26

    expect(table, hasAGoodToStringDeep);
    expect(
27
      table.toStringDeep(minLevel: DiagnosticLevel.info),
28 29 30 31 32 33 34 35 36 37 38 39 40
      equalsIgnoringHashCodes(
        'RenderTable#00000 NEEDS-PAINT\n'
        ' │ parentData: <none>\n'
        ' │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        ' │ size: Size(800.0, 600.0)\n'
        ' │ default column width: FlexColumnWidth(1.0)\n'
        ' │ table size: 0×0\n'
        ' │ column offsets: unknown\n'
        ' │ row offsets: []\n'
        ' │\n'
        ' └─table is empty\n',
      ),
    );
Hixie's avatar
Hixie committed
41 42 43 44
  });

  test('Table control test; loose', () {
    RenderTable table;
45
    layout(RenderPositionedBox(child: table = RenderTable(textDirection: TextDirection.ltr)));
Hixie's avatar
Hixie committed
46

47
    expect(table.size, equals(Size.zero));
Hixie's avatar
Hixie committed
48 49
  });

50 51 52 53 54 55 56 57
  test('Table control test: constrained flex columns', () {
    final RenderTable table = RenderTable(textDirection: TextDirection.ltr);
    final List<RenderBox> children = List<RenderBox>.generate(6, (_) => RenderPositionedBox());

    table.setFlatChildren(6, children);
    layout(table, constraints: const BoxConstraints.tightFor(width: 100.0));

    const double expectedWidth = 100.0 / 6;
58
    for (final RenderBox child in children) {
59 60 61 62
      expect(child.size.width, moreOrLessEquals(expectedWidth));
    }
  });

Hixie's avatar
Hixie committed
63 64
  test('Table test: combinations', () {
    RenderTable table;
65
    layout(RenderPositionedBox(child: table = RenderTable(
Hixie's avatar
Hixie committed
66 67 68
      columns: 5,
      rows: 5,
      defaultColumnWidth: const IntrinsicColumnWidth(),
69
      textDirection: TextDirection.ltr,
Hixie's avatar
Hixie committed
70
      defaultVerticalAlignment: TableCellVerticalAlignment.baseline,
71
      textBaseline: TextBaseline.alphabetic,
Hixie's avatar
Hixie committed
72 73
    )));

74
    expect(table.size, equals(Size.zero));
Hixie's avatar
Hixie committed
75 76 77 78 79

    table.setChild(2, 4, sizedBox(100.0, 200.0));

    pumpFrame();

80
    expect(table.size, equals(const Size(100.0, 200.0)));
Hixie's avatar
Hixie committed
81 82 83 84 85 86 87

    table.setChild(0, 0, sizedBox(10.0, 30.0));
    table.setChild(1, 0, sizedBox(20.0, 20.0));
    table.setChild(2, 0, sizedBox(30.0, 10.0));

    pumpFrame();

88
    expect(table.size, equals(const Size(130.0, 230.0)));
89 90 91

    expect(table, hasAGoodToStringDeep);
    expect(
92
      table.toStringDeep(minLevel: DiagnosticLevel.info),
93
      equalsIgnoringHashCodes(
94
        'RenderTable#00000 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE\n'
95 96 97
        ' │ parentData: offset=Offset(335.0, 185.0) (can use size)\n'
        ' │ constraints: BoxConstraints(0.0<=w<=800.0, 0.0<=h<=600.0)\n'
        ' │ size: Size(130.0, 230.0)\n'
98
        ' │ default column width: IntrinsicColumnWidth(flex: null)\n'
99
        ' │ table size: 5×5\n'
100 101
        ' │ column offsets: 0.0, 10.0, 30.0, 130.0, 130.0\n'
        ' │ row offsets: 0.0, 30.0, 30.0, 30.0, 30.0, 230.0\n'
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        ' │\n'
        ' ├─child (0, 0): RenderConstrainedBox#00000 relayoutBoundary=up2 NEEDS-PAINT\n'
        ' │   parentData: offset=Offset(0.0, 0.0); default vertical alignment\n'
        ' │     (can use size)\n'
        ' │   constraints: BoxConstraints(w=10.0, 0.0<=h<=Infinity)\n'
        ' │   size: Size(10.0, 30.0)\n'
        ' │   additionalConstraints: BoxConstraints(w=10.0, h=30.0)\n'
        ' │\n'
        ' ├─child (1, 0): RenderConstrainedBox#00000 relayoutBoundary=up2 NEEDS-PAINT\n'
        ' │   parentData: offset=Offset(10.0, 0.0); default vertical alignment\n'
        ' │     (can use size)\n'
        ' │   constraints: BoxConstraints(w=20.0, 0.0<=h<=Infinity)\n'
        ' │   size: Size(20.0, 20.0)\n'
        ' │   additionalConstraints: BoxConstraints(w=20.0, h=20.0)\n'
        ' │\n'
        ' ├─child (2, 0): RenderConstrainedBox#00000 relayoutBoundary=up2 NEEDS-PAINT\n'
        ' │   parentData: offset=Offset(30.0, 0.0); default vertical alignment\n'
        ' │     (can use size)\n'
        ' │   constraints: BoxConstraints(w=100.0, 0.0<=h<=Infinity)\n'
        ' │   size: Size(100.0, 10.0)\n'
        ' │   additionalConstraints: BoxConstraints(w=30.0, h=10.0)\n'
        ' │\n'
        ' ├─child (3, 0) is null\n'
        ' ├─child (4, 0) is null\n'
        ' ├─child (0, 1) is null\n'
        ' ├─child (1, 1) is null\n'
        ' ├─child (2, 1) is null\n'
        ' ├─child (3, 1) is null\n'
        ' ├─child (4, 1) is null\n'
        ' ├─child (0, 2) is null\n'
        ' ├─child (1, 2) is null\n'
        ' ├─child (2, 2) is null\n'
        ' ├─child (3, 2) is null\n'
        ' ├─child (4, 2) is null\n'
        ' ├─child (0, 3) is null\n'
        ' ├─child (1, 3) is null\n'
        ' ├─child (2, 3) is null\n'
        ' ├─child (3, 3) is null\n'
        ' ├─child (4, 3) is null\n'
        ' ├─child (0, 4) is null\n'
        ' ├─child (1, 4) is null\n'
        ' ├─child (2, 4): RenderConstrainedBox#00000 relayoutBoundary=up2 NEEDS-PAINT\n'
        ' │   parentData: offset=Offset(30.0, 30.0); default vertical alignment\n'
        ' │     (can use size)\n'
        ' │   constraints: BoxConstraints(w=100.0, 0.0<=h<=Infinity)\n'
        ' │   size: Size(100.0, 200.0)\n'
        ' │   additionalConstraints: BoxConstraints(w=100.0, h=200.0)\n'
        ' │\n'
        ' ├─child (3, 4) is null\n'
        ' └─child (4, 4) is null\n',
      ),
    );
Hixie's avatar
Hixie committed
154
  });
Hixie's avatar
Hixie committed
155 156 157 158

  test('Table test: removing cells', () {
    RenderTable table;
    RenderBox child;
159
    table = RenderTable(
Hixie's avatar
Hixie committed
160
      columns: 5,
161 162
      rows: 5,
      textDirection: TextDirection.ltr,
Hixie's avatar
Hixie committed
163 164 165 166 167 168 169 170 171
    );
    table.setChild(4, 4, child = sizedBox(10.0, 10.0));

    layout(table);

    expect(child.attached, isTrue);
    table.rows = 4;
    expect(child.attached, isFalse);
  });
Hixie's avatar
Hixie committed
172 173 174

  test('Table test: replacing cells', () {
    RenderTable table;
175 176 177 178
    final RenderBox child1 = RenderPositionedBox();
    final RenderBox child2 = RenderPositionedBox();
    final RenderBox child3 = RenderPositionedBox();
    table = RenderTable(textDirection: TextDirection.ltr);
179 180 181 182
    table.setFlatChildren(3, <RenderBox>[
      child1, RenderPositionedBox(), child2,
      RenderPositionedBox(), child3, RenderPositionedBox(),
    ]);
Hixie's avatar
Hixie committed
183 184
    expect(table.rows, equals(2));
    layout(table);
185 186 187 188
    table.setFlatChildren(3, <RenderBox>[
      RenderPositionedBox(), child1, RenderPositionedBox(),
      child2, RenderPositionedBox(), child3,
    ]);
189
    pumpFrame();
190 191 192 193
    table.setFlatChildren(3, <RenderBox>[
      RenderPositionedBox(), child1, RenderPositionedBox(),
      child2, RenderPositionedBox(), child3,
    ]);
194
    pumpFrame();
Hixie's avatar
Hixie committed
195 196 197
    expect(table.columns, equals(3));
    expect(table.rows, equals(2));
  });
198 199

  test('Table border painting', () {
200
    final RenderTable table = RenderTable(
201
      textDirection: TextDirection.rtl,
202
      border: TableBorder.all(),
203 204 205 206 207
    );
    layout(table);
    table.setFlatChildren(1, <RenderBox>[ ]);
    pumpFrame();
    expect(table, paints..path()..path()..path()..path());
208
    table.setFlatChildren(1, <RenderBox>[ RenderPositionedBox() ]);
209 210
    pumpFrame();
    expect(table, paints..path()..path()..path()..path());
211
    table.setFlatChildren(1, <RenderBox>[ RenderPositionedBox(), RenderPositionedBox() ]);
212 213
    pumpFrame();
    expect(table, paints..path()..path()..path()..path()..path());
214
    table.setFlatChildren(2, <RenderBox>[ RenderPositionedBox(), RenderPositionedBox() ]);
215 216
    pumpFrame();
    expect(table, paints..path()..path()..path()..path()..path());
217 218 219 220
    table.setFlatChildren(2, <RenderBox>[
      RenderPositionedBox(), RenderPositionedBox(),
      RenderPositionedBox(), RenderPositionedBox(),
    ]);
221 222
    pumpFrame();
    expect(table, paints..path()..path()..path()..path()..path()..path());
223 224 225 226
    table.setFlatChildren(3, <RenderBox>[
      RenderPositionedBox(), RenderPositionedBox(), RenderPositionedBox(),
      RenderPositionedBox(), RenderPositionedBox(), RenderPositionedBox(),
    ]);
227 228 229
    pumpFrame();
    expect(table, paints..path()..path()..path()..path()..path()..path());
  });
230 231 232 233 234 235 236 237 238 239 240 241 242

  test('Table flex sizing', () {
    const BoxConstraints cellConstraints =
        BoxConstraints.tightFor(width: 100, height: 100);
    final RenderTable table = RenderTable(
      textDirection: TextDirection.rtl,
      children: <List<RenderBox>>[
        List<RenderBox>.generate(
          7,
          (int _) => RenderConstrainedBox(additionalConstraints: cellConstraints),
        ),
      ],
      columnWidths: const <int, TableColumnWidth>{
243
        0: FlexColumnWidth(),
244 245 246 247 248 249 250 251 252 253 254 255
        1: FlexColumnWidth(0.123),
        2: FlexColumnWidth(0.123),
        3: FlexColumnWidth(0.123),
        4: FlexColumnWidth(0.123),
        5: FlexColumnWidth(0.123),
        6: FlexColumnWidth(0.123),
      },
    );

    layout(table, constraints: BoxConstraints.tight(const Size(800.0, 600.0)));
    expect(table.hasSize, true);
  });
256 257 258 259

  test('Table paints a borderRadius', () {
    final RenderTable table = RenderTable(
      textDirection: TextDirection.ltr,
260
      border: TableBorder.all(borderRadius: const BorderRadius.all(Radius.circular(8.0))),
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    );
    layout(table);
    table.setFlatChildren(2, <RenderBox>[
      RenderPositionedBox(), RenderPositionedBox(),
      RenderPositionedBox(), RenderPositionedBox(),
    ]);
    pumpFrame();
    expect(table, paints
      ..path()
      ..path()
      ..drrect(
        outer: RRect.fromLTRBR(0.0, 0.0, 800.0, 0.0, const Radius.circular(8.0)),
        inner: RRect.fromLTRBR(1.0, 1.0, 799.0, -1.0, const Radius.circular(7.0)),
      )
    );
  });

Hixie's avatar
Hixie committed
278
}