default_text_style_test.dart 3.72 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8
import 'dart:ui' as ui show TextHeightBehavior;

9 10
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
11
import 'package:flutter/painting.dart';
12 13 14

void main() {
  testWidgets('DefaultTextStyle changes propagate to Text', (WidgetTester tester) async {
15 16
    const Text textWidget = Text('Hello', textDirection: TextDirection.ltr);
    const TextStyle s1 = TextStyle(
17 18 19 20 21 22 23
      fontSize: 10.0,
      fontWeight: FontWeight.w800,
      height: 123.0,
    );

    await tester.pumpWidget(const DefaultTextStyle(
      style: s1,
Ian Hickson's avatar
Ian Hickson committed
24
      child: textWidget,
25 26 27 28 29 30 31 32 33 34 35 36
    ));

    RichText text = tester.firstWidget(find.byType(RichText));
    expect(text, isNotNull);
    expect(text.text.style, s1);

    await tester.pumpWidget(const DefaultTextStyle(
      style: s1,
      textAlign: TextAlign.justify,
      softWrap: false,
      overflow: TextOverflow.fade,
      maxLines: 3,
37
      child: textWidget,
38 39 40 41 42 43 44 45 46 47
    ));

    text = tester.firstWidget(find.byType(RichText));
    expect(text, isNotNull);
    expect(text.text.style, s1);
    expect(text.textAlign, TextAlign.justify);
    expect(text.softWrap, false);
    expect(text.overflow, TextOverflow.fade);
    expect(text.maxLines, 3);
  });
48 49

  testWidgets('AnimatedDefaultTextStyle changes propagate to Text', (WidgetTester tester) async {
50 51
    const Text textWidget = Text('Hello', textDirection: TextDirection.ltr);
    const TextStyle s1 = TextStyle(
52 53 54 55
      fontSize: 10.0,
      fontWeight: FontWeight.w800,
      height: 123.0,
    );
56
    const TextStyle s2 = TextStyle(
57 58 59 60 61 62 63 64
      fontSize: 20.0,
      fontWeight: FontWeight.w200,
      height: 1.0,
    );

    await tester.pumpWidget(const AnimatedDefaultTextStyle(
      style: s1,
      child: textWidget,
65
      duration: Duration(milliseconds: 1000),
66 67 68 69 70 71 72 73 74
    ));

    final RichText text1 = tester.firstWidget(find.byType(RichText));
    expect(text1, isNotNull);
    expect(text1.text.style, s1);
    expect(text1.textAlign, TextAlign.start);
    expect(text1.softWrap, isTrue);
    expect(text1.overflow, TextOverflow.clip);
    expect(text1.maxLines, isNull);
75 76
    expect(text1.textWidthBasis, TextWidthBasis.parent);
    expect(text1.textHeightBehavior, isNull);
77 78 79 80 81 82 83

    await tester.pumpWidget(const AnimatedDefaultTextStyle(
      style: s2,
      textAlign: TextAlign.justify,
      softWrap: false,
      overflow: TextOverflow.fade,
      maxLines: 3,
84 85
      textWidthBasis: TextWidthBasis.longestLine,
      textHeightBehavior: ui.TextHeightBehavior(applyHeightToFirstAscent: false),
86
      child: textWidget,
87
      duration: Duration(milliseconds: 1000),
88 89 90 91 92 93 94 95 96
    ));

    final RichText text2 = tester.firstWidget(find.byType(RichText));
    expect(text2, isNotNull);
    expect(text2.text.style, s1); // animation hasn't started yet
    expect(text2.textAlign, TextAlign.justify);
    expect(text2.softWrap, false);
    expect(text2.overflow, TextOverflow.fade);
    expect(text2.maxLines, 3);
97 98
    expect(text2.textWidthBasis, TextWidthBasis.longestLine);
    expect(text2.textHeightBehavior, const ui.TextHeightBehavior(applyHeightToFirstAscent: false));
99 100 101 102 103 104 105 106 107 108

    await tester.pump(const Duration(milliseconds: 1000));

    final RichText text3 = tester.firstWidget(find.byType(RichText));
    expect(text3, isNotNull);
    expect(text3.text.style, s2); // animation has now finished
    expect(text3.textAlign, TextAlign.justify);
    expect(text3.softWrap, false);
    expect(text3.overflow, TextOverflow.fade);
    expect(text3.maxLines, 3);
109 110
    expect(text2.textWidthBasis, TextWidthBasis.longestLine);
    expect(text2.textHeightBehavior, const ui.TextHeightBehavior(applyHeightToFirstAscent: false));
111
  });
112
}