fractionally_sized_box_test.dart 3.14 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6 7
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
Hixie's avatar
Hixie committed
8 9

void main() {
10
  testWidgets('FractionallySizedBox', (WidgetTester tester) async {
11 12
    final GlobalKey inner = GlobalKey();
    await tester.pumpWidget(OverflowBox(
13 14 15 16
      minWidth: 0.0,
      maxWidth: 100.0,
      minHeight: 0.0,
      maxHeight: 100.0,
17
      alignment: const Alignment(-1.0, -1.0),
18 19
      child: Center(
        child: FractionallySizedBox(
20 21
          widthFactor: 0.5,
          heightFactor: 0.25,
22
          child: Container(
23
            key: inner
24 25 26
          ),
        ),
      ),
27
    ));
28
    final RenderBox box = inner.currentContext.findRenderObject() as RenderBox;
29
    expect(box.size, equals(const Size(50.0, 25.0)));
30
    expect(box.localToGlobal(Offset.zero), equals(const Offset(25.0, 37.5)));
Hixie's avatar
Hixie committed
31
  });
32 33 34 35 36 37 38 39 40 41 42 43

  testWidgets('FractionallySizedBox alignment', (WidgetTester tester) async {
    final GlobalKey inner = GlobalKey();
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.rtl,
      child: FractionallySizedBox(
        widthFactor: 0.5,
        heightFactor: 0.5,
        alignment: Alignment.topRight,
        child: Placeholder(key: inner),
      ),
    ));
44
    final RenderBox box = inner.currentContext.findRenderObject() as RenderBox;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    expect(box.size, equals(const Size(400.0, 300.0)));
    expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(800.0 - 400.0 / 2.0, 0.0 + 300.0 / 2.0)));
  });

  testWidgets('FractionallySizedBox alignment (direction-sensitive)', (WidgetTester tester) async {
    final GlobalKey inner = GlobalKey();
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.rtl,
      child: FractionallySizedBox(
        widthFactor: 0.5,
        heightFactor: 0.5,
        alignment: AlignmentDirectional.topEnd,
        child: Placeholder(key: inner),
      ),
    ));
60
    final RenderBox box = inner.currentContext.findRenderObject() as RenderBox;
61 62 63
    expect(box.size, equals(const Size(400.0, 300.0)));
    expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(0.0 + 400.0 / 2.0, 0.0 + 300.0 / 2.0)));
  });
Ian Hickson's avatar
Ian Hickson committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

  testWidgets('OverflowBox alignment with FractionallySizedBox', (WidgetTester tester) async {
    final GlobalKey inner = GlobalKey();
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.rtl,
      child: OverflowBox(
        minWidth: 0.0,
        maxWidth: 100.0,
        minHeight: 0.0,
        maxHeight: 100.0,
        alignment: const AlignmentDirectional(1.0, -1.0),
        child: Center(
          child: FractionallySizedBox(
            widthFactor: 0.5,
            heightFactor: 0.25,
            child: Container(
              key: inner
            ),
          ),
        ),
      ),
    ));
86
    final RenderBox box = inner.currentContext.findRenderObject() as RenderBox;
Ian Hickson's avatar
Ian Hickson committed
87 88 89
    expect(box.size, equals(const Size(50.0, 25.0)));
    expect(box.localToGlobal(Offset.zero), equals(const Offset(25.0, 37.5)));
  });
Hixie's avatar
Hixie committed
90
}