rich_text_test.dart 7.03 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
9
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
10 11

void main() {
12
  testWidgetsWithLeakTracking('RichText with recognizers without handlers does not throw', (WidgetTester tester) async {
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: RichText(
          text: TextSpan(text: 'root', children: <InlineSpan>[
            TextSpan(text: 'one', recognizer: TapGestureRecognizer()),
            TextSpan(text: 'two', recognizer: LongPressGestureRecognizer()),
            TextSpan(text: 'three', recognizer: DoubleTapGestureRecognizer()),
          ]),
        ),
      ),
    );

    expect(tester.getSemantics(find.byType(RichText)), matchesSemantics(
      children: <Matcher>[
        matchesSemantics(
          label: 'root',
        ),
        matchesSemantics(
          label: 'one',
        ),
        matchesSemantics(
          label: 'two',
        ),
        matchesSemantics(
          label: 'three',
        ),
      ],
    ));
  });
43

44
  testWidgetsWithLeakTracking('TextSpan Locale works', (WidgetTester tester) async {
45 46 47 48 49 50 51 52 53 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
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: RichText(
          text: TextSpan(
            text: 'root',
            locale: const Locale('es', 'MX'),
            children: <InlineSpan>[
              TextSpan(text: 'one', recognizer: TapGestureRecognizer()),
              const WidgetSpan(
                child: SizedBox(),
              ),
              TextSpan(text: 'three', recognizer: DoubleTapGestureRecognizer()),
            ]
          ),
        ),
      ),
    );
    expect(tester.getSemantics(find.byType(RichText)), matchesSemantics(
      children: <Matcher>[
        matchesSemantics(
          attributedLabel: AttributedString(
            'root',
            attributes: <StringAttribute>[
              LocaleStringAttribute(range: const TextRange(start: 0, end: 4), locale: const Locale('es', 'MX')),
            ]
          ),
        ),
        matchesSemantics(
          attributedLabel: AttributedString(
            'one',
            attributes: <StringAttribute>[
              LocaleStringAttribute(range: const TextRange(start: 0, end: 3), locale: const Locale('es', 'MX')),
            ]
          ),
        ),
        matchesSemantics(
          attributedLabel: AttributedString(
            'three',
            attributes: <StringAttribute>[
              LocaleStringAttribute(range: const TextRange(start: 0, end: 5), locale: const Locale('es', 'MX')),
            ]
          ),
        ),
      ],
    ));
  });

93
  testWidgetsWithLeakTracking('TextSpan spellOut works', (WidgetTester tester) async {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: RichText(
          text: TextSpan(
              text: 'root',
              spellOut: true,
              children: <InlineSpan>[
                TextSpan(text: 'one', recognizer: TapGestureRecognizer()),
                const WidgetSpan(
                  child: SizedBox(),
                ),
                TextSpan(text: 'three', recognizer: DoubleTapGestureRecognizer()),
              ]
          ),
        ),
      ),
    );
    expect(tester.getSemantics(find.byType(RichText)), matchesSemantics(
      children: <Matcher>[
        matchesSemantics(
          attributedLabel: AttributedString(
              'root',
              attributes: <StringAttribute>[
                SpellOutStringAttribute(range: const TextRange(start: 0, end: 4)),
              ]
          ),
        ),
        matchesSemantics(
          attributedLabel: AttributedString(
              'one',
              attributes: <StringAttribute>[
                SpellOutStringAttribute(range: const TextRange(start: 0, end: 3)),
              ]
          ),
        ),
        matchesSemantics(
          attributedLabel: AttributedString(
              'three',
              attributes: <StringAttribute>[
                SpellOutStringAttribute(range: const TextRange(start: 0, end: 5)),
              ]
          ),
        ),
      ],
    ));
  });

142
  testWidgetsWithLeakTracking('WidgetSpan calculate correct intrinsic heights', (WidgetTester tester) async {
143 144 145 146
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
147
          child: ColoredBox(
148 149 150
            color: Colors.green,
            child: IntrinsicHeight(
              child: RichText(
151
                text: const TextSpan(
152
                  children: <InlineSpan>[
153
                    TextSpan(text: 'Start\n', style: TextStyle(height: 1.0, fontSize: 16)),
154 155
                    WidgetSpan(
                      child: Row(
156
                        children: <Widget>[
157
                          SizedBox(height: 16, width: 16),
158 159 160
                        ],
                      ),
                    ),
161
                    TextSpan(text: 'End', style: TextStyle(height: 1.0, fontSize: 16)),
162 163 164 165 166 167 168 169 170 171 172
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );

    expect(tester.getSize(find.byType(IntrinsicHeight)).height, 3 * 16);
  });
173

174
  testWidgetsWithLeakTracking('RichText implements debugFillProperties', (WidgetTester tester) async {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    RichText(
      text: const TextSpan(text: 'rich text'),
      textAlign: TextAlign.center,
      textDirection: TextDirection.rtl,
      softWrap: false,
      overflow: TextOverflow.ellipsis,
      textScaleFactor: 1.3,
      maxLines: 1,
      locale: const Locale('zh', 'HK'),
      strutStyle: const StrutStyle(
        fontSize: 16,
      ),
      textWidthBasis: TextWidthBasis.longestLine,
      textHeightBehavior: const TextHeightBehavior(applyHeightToFirstAscent: false),
    ).debugFillProperties(builder);

    final List<String> description = builder.properties
      .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
      .map((DiagnosticsNode node) => node.toString())
      .toList();

197
    expect(description, unorderedMatches(<Matcher>[
198 199 200 201
      contains('textAlign: center'),
      contains('textDirection: rtl'),
      contains('softWrap: no wrapping except at line break characters'),
      contains('overflow: ellipsis'),
202
      contains('textScaler: linear (1.3x)'),
203 204 205 206
      contains('maxLines: 1'),
      contains('textWidthBasis: longestLine'),
      contains('text: "rich text"'),
      contains('locale: zh_HK'),
207
      allOf(startsWith('strutStyle: StrutStyle('), contains('size: 16.0')),
208 209 210 211 212 213
      allOf(
        startsWith('textHeightBehavior: TextHeightBehavior('),
        contains('applyHeightToFirstAscent: false'),
        contains('applyHeightToLastDescent: true'),
      ),
    ]));
214
  });
215
}