fractionally_sized_box_test.dart 3.09 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.

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

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

  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),
      ),
    ));
43
    final RenderBox box = inner.currentContext!.findRenderObject()! as RenderBox;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    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),
      ),
    ));
59
    final RenderBox box = inner.currentContext!.findRenderObject()! as RenderBox;
60 61 62
    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
63 64 65 66 67 68 69 70 71 72

  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,
73
        alignment: AlignmentDirectional.topEnd,
Ian Hickson's avatar
Ian Hickson committed
74 75 76 77 78
        child: Center(
          child: FractionallySizedBox(
            widthFactor: 0.5,
            heightFactor: 0.25,
            child: Container(
79
              key: inner,
Ian Hickson's avatar
Ian Hickson committed
80 81 82 83 84
            ),
          ),
        ),
      ),
    ));
85
    final RenderBox box = inner.currentContext!.findRenderObject()! as RenderBox;
Ian Hickson's avatar
Ian Hickson committed
86 87 88
    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
89
}