table_test.dart 8.17 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4 5
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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 12 13 14 15 16 17 18 19
import 'rendering_tester.dart';

RenderBox sizedBox(double width, double height) {
  return new RenderConstrainedBox(
    additionalConstraints: new BoxConstraints.tight(new Size(width, height))
  );
}

void main() {
  test('Table control test; tight', () {
    RenderTable table;
20
    layout(table = new 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(new RenderPositionedBox(child: table = new RenderTable(textDirection: TextDirection.ltr)));
Hixie's avatar
Hixie committed
46

47
    expect(table.size, equals(const Size(0.0, 0.0)));
Hixie's avatar
Hixie committed
48 49 50 51 52 53 54 55
  });

  test('Table test: combinations', () {
    RenderTable table;
    layout(new RenderPositionedBox(child: table = new RenderTable(
      columns: 5,
      rows: 5,
      defaultColumnWidth: const IntrinsicColumnWidth(),
56
      textDirection: TextDirection.ltr,
Hixie's avatar
Hixie committed
57
      defaultVerticalAlignment: TableCellVerticalAlignment.baseline,
58
      textBaseline: TextBaseline.alphabetic,
Hixie's avatar
Hixie committed
59 60
    )));

61
    expect(table.size, equals(const Size(0.0, 0.0)));
Hixie's avatar
Hixie committed
62 63 64 65 66

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

    pumpFrame();

67
    expect(table.size, equals(const Size(100.0, 200.0)));
Hixie's avatar
Hixie committed
68 69 70 71 72 73 74

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

75
    expect(table.size, equals(const Size(130.0, 230.0)));
76 77 78

    expect(table, hasAGoodToStringDeep);
    expect(
79
      table.toStringDeep(minLevel: DiagnosticLevel.info),
80 81 82 83 84
      equalsIgnoringHashCodes(
        'RenderTable#00000 relayoutBoundary=up1 NEEDS-PAINT\n'
        ' │ 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'
85
        ' │ default column width: IntrinsicColumnWidth(flex: null)\n'
86
        ' │ table size: 5×5\n'
87 88
        ' │ 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'
89 90 91 92 93 94 95 96 97 98 99 100 101 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
        ' │\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
141
  });
Hixie's avatar
Hixie committed
142 143 144 145 146 147

  test('Table test: removing cells', () {
    RenderTable table;
    RenderBox child;
    table = new RenderTable(
      columns: 5,
148 149
      rows: 5,
      textDirection: TextDirection.ltr,
Hixie's avatar
Hixie committed
150 151 152 153 154 155 156 157 158
    );
    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
159 160 161

  test('Table test: replacing cells', () {
    RenderTable table;
162 163 164
    final RenderBox child1 = new RenderPositionedBox();
    final RenderBox child2 = new RenderPositionedBox();
    final RenderBox child3 = new RenderPositionedBox();
165
    table = new RenderTable(textDirection: TextDirection.ltr);
Hixie's avatar
Hixie committed
166 167 168 169 170 171
    table.setFlatChildren(3, <RenderBox>[child1, new RenderPositionedBox(), child2,
                                         new RenderPositionedBox(), child3, new RenderPositionedBox()]);
    expect(table.rows, equals(2));
    layout(table);
    table.setFlatChildren(3, <RenderBox>[new RenderPositionedBox(), child1, new RenderPositionedBox(),
                                         child2, new RenderPositionedBox(), child3]);
172
    pumpFrame();
Hixie's avatar
Hixie committed
173 174
    table.setFlatChildren(3, <RenderBox>[new RenderPositionedBox(), child1, new RenderPositionedBox(),
                                         child2, new RenderPositionedBox(), child3]);
175
    pumpFrame();
Hixie's avatar
Hixie committed
176 177 178
    expect(table.columns, equals(3));
    expect(table.rows, equals(2));
  });
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

  test('Table border painting', () {
    final RenderTable table = new RenderTable(
      textDirection: TextDirection.rtl,
      border: new TableBorder.all(),
    );
    layout(table);
    table.setFlatChildren(1, <RenderBox>[ ]);
    pumpFrame();
    expect(table, paints..path()..path()..path()..path());
    table.setFlatChildren(1, <RenderBox>[ new RenderPositionedBox() ]);
    pumpFrame();
    expect(table, paints..path()..path()..path()..path());
    table.setFlatChildren(1, <RenderBox>[ new RenderPositionedBox(), new RenderPositionedBox() ]);
    pumpFrame();
    expect(table, paints..path()..path()..path()..path()..path());
    table.setFlatChildren(2, <RenderBox>[ new RenderPositionedBox(), new RenderPositionedBox() ]);
    pumpFrame();
    expect(table, paints..path()..path()..path()..path()..path());
    table.setFlatChildren(2, <RenderBox>[ new RenderPositionedBox(), new RenderPositionedBox(),
                                          new RenderPositionedBox(), new RenderPositionedBox() ]);
    pumpFrame();
    expect(table, paints..path()..path()..path()..path()..path()..path());
    table.setFlatChildren(3, <RenderBox>[ new RenderPositionedBox(), new RenderPositionedBox(), new RenderPositionedBox(),
                                          new RenderPositionedBox(), new RenderPositionedBox(), new RenderPositionedBox() ]);
    pumpFrame();
    expect(table, paints..path()..path()..path()..path()..path()..path());
  });
Hixie's avatar
Hixie committed
207
}