baseline_test.dart 3.62 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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';
7 8 9 10

import 'rendering_tester.dart';

void main() {
11 12
  TestRenderingFlutterBinding.ensureInitialized();

Ian Hickson's avatar
Ian Hickson committed
13
  test('RenderBaseline', () {
14 15
    RenderBaseline parent;
    RenderSizedBox child;
16
    final RenderBox root = RenderPositionedBox(
17
      alignment: Alignment.topLeft,
18
      child: parent = RenderBaseline(
19 20
        baseline: 0.0,
        baselineType: TextBaseline.alphabetic,
21 22
        child: child = RenderSizedBox(const Size(100.0, 100.0)),
      ),
23
    );
24
    final BoxParentData childParentData = child.parentData! as BoxParentData;
25

26
    layout(root);
27 28
    expect(childParentData.offset.dx, equals(0.0));
    expect(childParentData.offset.dy, equals(-100.0));
29
    expect(parent.size, equals(const Size(100.0, 0.0)));
30 31

    parent.baseline = 25.0;
32
    pumpFrame();
33 34
    expect(childParentData.offset.dx, equals(0.0));
    expect(childParentData.offset.dy, equals(-75.0));
35
    expect(parent.size, equals(const Size(100.0, 25.0)));
36 37

    parent.baseline = 90.0;
38
    pumpFrame();
39 40
    expect(childParentData.offset.dx, equals(0.0));
    expect(childParentData.offset.dy, equals(-10.0));
41
    expect(parent.size, equals(const Size(100.0, 90.0)));
42 43

    parent.baseline = 100.0;
44
    pumpFrame();
45 46
    expect(childParentData.offset.dx, equals(0.0));
    expect(childParentData.offset.dy, equals(0.0));
47
    expect(parent.size, equals(const Size(100.0, 100.0)));
48 49

    parent.baseline = 110.0;
50
    pumpFrame();
51 52
    expect(childParentData.offset.dx, equals(0.0));
    expect(childParentData.offset.dy, equals(10.0));
53
    expect(parent.size, equals(const Size(100.0, 110.0)));
54
  });
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

  test('RenderFlex and RenderIgnoreBaseline (control test -- with baseline)', () {
    final RenderBox a, b;
    final RenderBox root = RenderFlex(
      crossAxisAlignment: CrossAxisAlignment.baseline,
      textBaseline: TextBaseline.alphabetic,
      textDirection: TextDirection.ltr,
      children: <RenderBox>[
        a = RenderParagraph(
          const TextSpan(text: 'a', style: TextStyle(fontSize: 128.0, fontFamily: 'FlutterTest')), // places baseline at y=96
          textDirection: TextDirection.ltr,
        ),
        b = RenderParagraph(
          const TextSpan(text: 'b', style: TextStyle(fontSize: 32.0, fontFamily: 'FlutterTest')), // 24 above baseline, 8 below baseline
          textDirection: TextDirection.ltr,
        ),
      ],
    );
    layout(root);

    final Offset aPos = a.localToGlobal(Offset.zero);
    final Offset bPos = b.localToGlobal(Offset.zero);
    expect(aPos.dy, 0.0);
    expect(bPos.dy, 96.0 - 24.0);
  });

  test('RenderFlex and RenderIgnoreBaseline (with ignored baseline)', () {
    final RenderBox a, b;
    final RenderBox root = RenderFlex(
      crossAxisAlignment: CrossAxisAlignment.baseline,
      textBaseline: TextBaseline.alphabetic,
      textDirection: TextDirection.ltr,
      children: <RenderBox>[
        RenderIgnoreBaseline(
          child: a = RenderParagraph(
            const TextSpan(text: 'a', style: TextStyle(fontSize: 128.0, fontFamily: 'FlutterTest')),
            textDirection: TextDirection.ltr,
          ),
        ),
        b = RenderParagraph(
          const TextSpan(text: 'b', style: TextStyle(fontSize: 32.0, fontFamily: 'FlutterTest')),
          textDirection: TextDirection.ltr,
        ),
      ],
    );
    layout(root);

    final Offset aPos = a.localToGlobal(Offset.zero);
    final Offset bPos = b.localToGlobal(Offset.zero);
    expect(aPos.dy, 0.0);
    expect(bPos.dy, 0.0);
  });
107
}