fitted_box_test.dart 15 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Adam Barth's avatar
Adam Barth committed
2 3 4 5 6 7 8 9 10
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

void main() {
  testWidgets('Can size according to aspect ratio', (WidgetTester tester) async {
11 12
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Adam Barth's avatar
Adam Barth committed
13 14

    await tester.pumpWidget(
15 16
      Center(
        child: Container(
Adam Barth's avatar
Adam Barth committed
17
          width: 200.0,
18
          child: FittedBox(
Adam Barth's avatar
Adam Barth committed
19
            key: outside,
20
            child: Container(
Adam Barth's avatar
Adam Barth committed
21 22 23
              key: inside,
              width: 100.0,
              height: 50.0,
24 25 26
            ),
          ),
        ),
27
      ),
Adam Barth's avatar
Adam Barth committed
28 29
    );

30
    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
Adam Barth's avatar
Adam Barth committed
31 32 33
    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 100.0);

34
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
Adam Barth's avatar
Adam Barth committed
35 36 37
    expect(insideBox.size.width, 100.0);
    expect(insideBox.size.height, 50.0);

38 39
    final Offset insidePoint = insideBox.localToGlobal(const Offset(100.0, 50.0));
    final Offset outsidePoint = outsideBox.localToGlobal(const Offset(200.0, 100.0));
Adam Barth's avatar
Adam Barth committed
40

41
    expect(outsidePoint, equals(const Offset(500.0, 350.0)));
Adam Barth's avatar
Adam Barth committed
42 43 44 45
    expect(insidePoint, equals(outsidePoint));
  });

  testWidgets('Can contain child', (WidgetTester tester) async {
46 47
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Adam Barth's avatar
Adam Barth committed
48 49

    await tester.pumpWidget(
50 51
      Center(
        child: Container(
Adam Barth's avatar
Adam Barth committed
52 53
          width: 200.0,
          height: 200.0,
54
          child: FittedBox(
Adam Barth's avatar
Adam Barth committed
55
            key: outside,
56
            child: Container(
Adam Barth's avatar
Adam Barth committed
57 58 59
              key: inside,
              width: 100.0,
              height: 50.0,
60 61 62
            ),
          ),
        ),
63
      ),
Adam Barth's avatar
Adam Barth committed
64 65
    );

66
    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
Adam Barth's avatar
Adam Barth committed
67 68 69
    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 200.0);

70
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
Adam Barth's avatar
Adam Barth committed
71 72 73
    expect(insideBox.size.width, 100.0);
    expect(insideBox.size.height, 50.0);

74 75
    final Offset insidePoint = insideBox.localToGlobal(const Offset(100.0, 0.0));
    final Offset outsidePoint = outsideBox.localToGlobal(const Offset(200.0, 50.0));
Adam Barth's avatar
Adam Barth committed
76 77 78 79 80

    expect(insidePoint, equals(outsidePoint));
  });

  testWidgets('Child can conver', (WidgetTester tester) async {
81 82
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Adam Barth's avatar
Adam Barth committed
83 84

    await tester.pumpWidget(
85 86
      Center(
        child: Container(
Adam Barth's avatar
Adam Barth committed
87 88
          width: 200.0,
          height: 200.0,
89
          child: FittedBox(
Adam Barth's avatar
Adam Barth committed
90
            key: outside,
91
            fit: BoxFit.cover,
92
            child: Container(
Adam Barth's avatar
Adam Barth committed
93 94 95
              key: inside,
              width: 100.0,
              height: 50.0,
96 97 98
            ),
          ),
        ),
99
      ),
Adam Barth's avatar
Adam Barth committed
100 101
    );

102
    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
Adam Barth's avatar
Adam Barth committed
103 104 105
    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 200.0);

106
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
Adam Barth's avatar
Adam Barth committed
107 108 109
    expect(insideBox.size.width, 100.0);
    expect(insideBox.size.height, 50.0);

110 111
    final Offset insidePoint = insideBox.localToGlobal(const Offset(50.0, 25.0));
    final Offset outsidePoint = outsideBox.localToGlobal(const Offset(100.0, 100.0));
Adam Barth's avatar
Adam Barth committed
112 113 114

    expect(insidePoint, equals(outsidePoint));
  });
Ian Hickson's avatar
Ian Hickson committed
115 116

  testWidgets('FittedBox with no child', (WidgetTester tester) async {
117
    final Key key = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
118
    await tester.pumpWidget(
119 120
      Center(
        child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
121 122 123 124 125 126 127 128 129 130 131 132
          key: key,
          fit: BoxFit.cover,
        ),
      ),
    );

    final RenderBox box = tester.firstRenderObject(find.byKey(key));
    expect(box.size.width, 0.0);
    expect(box.size.height, 0.0);
  });

  testWidgets('Child can be aligned multiple ways in a row', (WidgetTester tester) async {
133 134
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
135 136 137 138

    { // align RTL

      await tester.pumpWidget(
139
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
140
          textDirection: TextDirection.rtl,
141 142
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
143 144
              width: 100.0,
              height: 100.0,
145
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
146 147 148
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.bottomEnd,
149
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
                  key: inside,
                  width: 10.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 10.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(0.0, 90.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(10.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(10.0, 100.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }

    { // change direction

      await tester.pumpWidget(
180
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
181
          textDirection: TextDirection.ltr,
182 183
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
184 185
              width: 100.0,
              height: 100.0,
186
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
187 188 189
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.bottomEnd,
190
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                  key: inside,
                  width: 10.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 10.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(90.0, 90.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(10.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(100.0, 100.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }

    { // change alignment

      await tester.pumpWidget(
221
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
222
          textDirection: TextDirection.ltr,
223 224
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
225 226
              width: 100.0,
              height: 100.0,
227
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
228 229 230
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.center,
231
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                  key: inside,
                  width: 10.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 10.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(45.0, 45.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(10.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(55.0, 55.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }

    { // change size

      await tester.pumpWidget(
262
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
263
          textDirection: TextDirection.ltr,
264 265
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
266 267
              width: 100.0,
              height: 100.0,
268
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
269 270 271
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.center,
272
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                  key: inside,
                  width: 30.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 30.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(35.0, 45.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(30.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(65.0, 55.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }

    { // change fit

      await tester.pumpWidget(
303
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
304
          textDirection: TextDirection.ltr,
305 306
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
307 308
              width: 100.0,
              height: 100.0,
309
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
310 311 312
                key: outside,
                fit: BoxFit.fill,
                alignment: AlignmentDirectional.center,
313
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
                  key: inside,
                  width: 30.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 30.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(30.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(100.0, 100.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }
  });
341 342 343 344

  testWidgets('FittedBox layers - contain', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Center(
345
        child: SizedBox(
346 347
          width: 100.0,
          height: 10.0,
348
          child: FittedBox(
349
            fit: BoxFit.contain,
350
            child: SizedBox(
351 352
              width: 50.0,
              height: 50.0,
353 354
              child: RepaintBoundary(
                child: Placeholder(),
355 356 357 358 359 360 361 362 363 364 365 366
              ),
            ),
          ),
        ),
      ),
    );
    expect(getLayers(), <Type>[TransformLayer, TransformLayer, OffsetLayer]);
  });

  testWidgets('FittedBox layers - cover - horizontal', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Center(
367
        child: SizedBox(
368 369
          width: 100.0,
          height: 10.0,
370
          child: FittedBox(
371
            fit: BoxFit.cover,
372
            child: SizedBox(
373 374
              width: 10.0,
              height: 50.0,
375 376
              child: RepaintBoundary(
                child: Placeholder(),
377 378 379 380 381 382 383 384 385 386 387 388
              ),
            ),
          ),
        ),
      ),
    );
    expect(getLayers(), <Type>[TransformLayer, ClipRectLayer, TransformLayer, OffsetLayer]);
  });

  testWidgets('FittedBox layers - cover - vertical', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Center(
389
        child: SizedBox(
390 391
          width: 10.0,
          height: 100.0,
392
          child: FittedBox(
393
            fit: BoxFit.cover,
394
            child: SizedBox(
395 396
              width: 50.0,
              height: 10.0,
397 398
              child: RepaintBoundary(
                child: Placeholder(),
399 400 401 402 403 404 405 406 407 408 409
              ),
            ),
          ),
        ),
      ),
    );
    expect(getLayers(), <Type>[TransformLayer, ClipRectLayer, TransformLayer, OffsetLayer]);
  });

  testWidgets('FittedBox layers - none - clip', (WidgetTester tester) async {
    final List<double> values = <double>[10.0, 50.0, 100.0];
410 411 412 413
    for (final double a in values) {
      for (final double b in values) {
        for (final double c in values) {
          for (final double d in values) {
414
            await tester.pumpWidget(
415 416
              Center(
                child: SizedBox(
417 418
                  width: a,
                  height: b,
419
                  child: FittedBox(
420
                    fit: BoxFit.none,
421
                    child: SizedBox(
422 423 424
                      width: c,
                      height: d,
                      child: const RepaintBoundary(
425
                        child: Placeholder(),
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
                      ),
                    ),
                  ),
                ),
              ),
            );
            if (a < c || b < d) {
              expect(getLayers(), <Type>[TransformLayer, ClipRectLayer, OffsetLayer]);
            } else {
              expect(getLayers(), <Type>[TransformLayer, OffsetLayer]);
            }
          }
        }
      }
    }
  });
Emmanuel Garcia's avatar
Emmanuel Garcia committed
442 443

  testWidgets('Big child into small fitted box - hit testing', (WidgetTester tester) async {
444
    final GlobalKey key1 = GlobalKey();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
445 446
    bool _pointerDown = false;
    await tester.pumpWidget(
447 448
      Center(
        child: SizedBox(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
449 450
          width: 100.0,
          height: 100.0,
451
          child: FittedBox(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
452 453
            fit: BoxFit.contain,
            alignment: FractionalOffset.center,
454
            child: SizedBox(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
455 456
              width: 1000.0,
              height: 1000.0,
457
              child: Listener(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
458 459 460
                onPointerDown: (PointerDownEvent event) {
                  _pointerDown = true;
                },
461
                child: Container(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
462 463
                  key: key1,
                  color: const Color(0xFF000000),
464 465 466 467 468
                ),
              ),
            ),
          ),
        ),
469
      ),
Emmanuel Garcia's avatar
Emmanuel Garcia committed
470 471 472 473 474
    );
    expect(_pointerDown, isFalse);
    await tester.tap(find.byKey(key1));
    expect(_pointerDown, isTrue);
  });
475 476 477 478 479 480
}

List<Type> getLayers() {
  final List<Type> layers = <Type>[];
  Layer layer = RendererBinding.instance.renderView.debugLayer;
  while (layer is ContainerLayer) {
481
    final ContainerLayer container = layer as ContainerLayer;
482 483 484 485 486
    layers.add(container.runtimeType);
    expect(container.firstChild, same(container.lastChild));
    layer = container.firstChild;
  }
  return layers;
Adam Barth's avatar
Adam Barth committed
487
}