sized_box_test.dart 4.49 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7 8
// Copyright 2016 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
9
  testWidgets('SizedBox constructors', (WidgetTester tester) async {
10
    const SizedBox a = SizedBox();
11 12 13
    expect(a.width, isNull);
    expect(a.height, isNull);

14
    const SizedBox b = SizedBox(width: 10.0);
15 16 17
    expect(b.width, 10.0);
    expect(b.height, isNull);

18
    const SizedBox c = SizedBox(width: 10.0, height: 20.0);
19 20 21
    expect(c.width, 10.0);
    expect(c.height, 20.0);

22
    final SizedBox d = SizedBox.fromSize();
23 24 25
    expect(d.width, isNull);
    expect(d.height, isNull);

26
    final SizedBox e = SizedBox.fromSize(size: const Size(1.0, 2.0));
27 28 29
    expect(e.width, 1.0);
    expect(e.height, 2.0);

30
    const SizedBox f = SizedBox.expand();
31 32
    expect(f.width, double.infinity);
    expect(f.height, double.infinity);
33 34 35 36

    const SizedBox g = SizedBox.shrink();
    expect(g.width, 0.0);
    expect(g.height, 0.0);
37 38
  });

Ian Hickson's avatar
Ian Hickson committed
39
  testWidgets('SizedBox - no child', (WidgetTester tester) async {
40
    final GlobalKey patient = GlobalKey();
Ian Hickson's avatar
Ian Hickson committed
41 42

    await tester.pumpWidget(
43 44
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
45 46 47 48 49 50 51
          key: patient,
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(0.0, 0.0)));

    await tester.pumpWidget(
52 53
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
54 55 56 57 58 59 60 61
          key: patient,
          height: 0.0,
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(0.0, 0.0)));

    await tester.pumpWidget(
62 63
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
64 65 66 67 68 69 70 71 72
          key: patient,
          width: 0.0,
          height: 0.0,
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(0.0, 0.0)));

    await tester.pumpWidget(
73 74
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
75 76 77 78 79 80 81 82 83
          key: patient,
          width: 100.0,
          height: 100.0,
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(100.0, 100.0)));

    await tester.pumpWidget(
84 85
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
86 87 88 89 90 91 92 93 94
          key: patient,
          width: 1000.0,
          height: 1000.0,
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(800.0, 600.0)));

    await tester.pumpWidget(
95 96
      Center(
        child: SizedBox.expand(
Ian Hickson's avatar
Ian Hickson committed
97 98 99 100 101
          key: patient,
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(800.0, 600.0)));
102 103

    await tester.pumpWidget(
104 105
      Center(
        child: SizedBox.shrink(
106 107 108 109 110
          key: patient,
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(0.0, 0.0)));
Ian Hickson's avatar
Ian Hickson committed
111 112 113
  });

  testWidgets('SizedBox - container child', (WidgetTester tester) async {
114
    final GlobalKey patient = GlobalKey();
Ian Hickson's avatar
Ian Hickson committed
115 116

    await tester.pumpWidget(
117 118
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
119
          key: patient,
120
          child: Container(),
Ian Hickson's avatar
Ian Hickson committed
121 122 123 124 125 126
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(800.0, 600.0)));

    await tester.pumpWidget(
127 128
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
129 130
          key: patient,
          height: 0.0,
131
          child: Container(),
Ian Hickson's avatar
Ian Hickson committed
132 133 134 135 136 137
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(800.0, 0.0)));

    await tester.pumpWidget(
138 139
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
140 141 142
          key: patient,
          width: 0.0,
          height: 0.0,
143
          child: Container(),
Ian Hickson's avatar
Ian Hickson committed
144 145 146 147 148 149
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(0.0, 0.0)));

    await tester.pumpWidget(
150 151
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
152 153 154
          key: patient,
          width: 100.0,
          height: 100.0,
155
          child: Container(),
Ian Hickson's avatar
Ian Hickson committed
156 157 158 159 160 161
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(100.0, 100.0)));

    await tester.pumpWidget(
162 163
      Center(
        child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
164 165 166
          key: patient,
          width: 1000.0,
          height: 1000.0,
167
          child: Container(),
Ian Hickson's avatar
Ian Hickson committed
168 169 170 171 172 173
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(800.0, 600.0)));

    await tester.pumpWidget(
174 175
      Center(
        child: SizedBox.expand(
Ian Hickson's avatar
Ian Hickson committed
176
          key: patient,
177
          child: Container(),
Ian Hickson's avatar
Ian Hickson committed
178 179 180 181
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(800.0, 600.0)));
182 183

    await tester.pumpWidget(
184 185
      Center(
        child: SizedBox.shrink(
186
          key: patient,
187
          child: Container(),
188 189 190 191
        )
      )
    );
    expect(patient.currentContext.size, equals(const Size(0.0, 0.0)));
Ian Hickson's avatar
Ian Hickson committed
192 193
  });
}