table_test.dart 9.94 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
  );
}

void main() {
18 19
  TestRenderingFlutterBinding.ensureInitialized();

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

    expect(table.size.width, equals(800.0));
    expect(table.size.height, equals(600.0));
26 27 28

    expect(table, hasAGoodToStringDeep);
    expect(
29
      table.toStringDeep(minLevel: DiagnosticLevel.info),
30 31 32 33 34 35 36 37 38 39 40 41 42
      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
43 44 45 46
  });

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

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

52 53 54 55 56 57 58 59
  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;
60
    for (final RenderBox child in children) {
61 62 63 64
      expect(child.size.width, moreOrLessEquals(expectedWidth));
    }
  });

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

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

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

    pumpFrame();

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

    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();

90
    expect(table.size, equals(const Size(130.0, 230.0)));
91 92 93

    expect(table, hasAGoodToStringDeep);
    expect(
94
      table.toStringDeep(minLevel: DiagnosticLevel.info),
95
      equalsIgnoringHashCodes(
96
        'RenderTable#00000 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE\n'
97 98 99
        ' │ 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'
100
        ' │ default column width: IntrinsicColumnWidth(flex: null)\n'
101
        ' │ table size: 5×5\n'
102 103
        ' │ 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'
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 154 155
        ' │\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
156
  });
Hixie's avatar
Hixie committed
157 158 159 160

  test('Table test: removing cells', () {
    RenderTable table;
    RenderBox child;
161
    table = RenderTable(
Hixie's avatar
Hixie committed
162
      columns: 5,
163 164
      rows: 5,
      textDirection: TextDirection.ltr,
Hixie's avatar
Hixie committed
165 166 167 168 169 170 171 172 173
    );
    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
174 175 176

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

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

  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>{
245
        0: FlexColumnWidth(),
246 247 248 249 250 251 252 253 254 255 256 257
        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);
  });
258 259 260 261

  test('Table paints a borderRadius', () {
    final RenderTable table = RenderTable(
      textDirection: TextDirection.ltr,
262
      border: TableBorder.all(borderRadius: const BorderRadius.all(Radius.circular(8.0))),
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    );
    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
280
}