text_golden_test.dart 15.3 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 The Chromium 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 'dart:io' show Platform;

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

void main() {
  testWidgets('Centered text', (WidgetTester tester) async {
    await tester.pumpWidget(
14 15 16
      Center(
        child: RepaintBoundary(
          child: Container(
17 18 19
            width: 200.0,
            height: 100.0,
            decoration: const BoxDecoration(
20
              color: Color(0xff00ff00),
21 22 23 24
            ),
            child: const Text('Hello',
              textDirection: TextDirection.ltr,
              textAlign: TextAlign.center,
25
              style: TextStyle(color: Color(0xffff0000)),
26 27 28 29 30 31 32 33 34 35 36 37
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byType(Container),
      matchesGoldenFile('text_golden.Centered.png'),
    );

    await tester.pumpWidget(
38 39 40
      Center(
        child: RepaintBoundary(
          child: Container(
41 42 43
            width: 200.0,
            height: 100.0,
            decoration: const BoxDecoration(
44
              color: Color(0xff00ff00),
45 46 47 48
            ),
            child: const Text('Hello world how are you today',
              textDirection: TextDirection.ltr,
              textAlign: TextAlign.center,
49
              style: TextStyle(color: Color(0xffff0000)),
50 51 52 53 54 55 56 57 58 59
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byType(Container),
      matchesGoldenFile('text_golden.Centered.wrap.png'),
    );
60
  }, skip: !Platform.isLinux);
61 62 63


  testWidgets('Text Foreground', (WidgetTester tester) async {
64 65 66
    const Color black = Color(0xFF000000);
    const Color red = Color(0xFFFF0000);
    const Color blue = Color(0xFF0000FF);
67
    final Shader linearGradient = const LinearGradient(
68
      colors: <Color>[red, blue],
69
    ).createShader(Rect.fromLTWH(0.0, 0.0, 50.0, 20.0));
70 71

    await tester.pumpWidget(
72
      Align(
73
        alignment: Alignment.topLeft,
74 75
        child: RepaintBoundary(
          child: Text('Hello',
76
            textDirection: TextDirection.ltr,
77 78
            style: TextStyle(
              foreground: Paint()
79 80 81 82 83 84 85 86 87 88 89 90 91 92
                ..color = black
                ..shader = linearGradient
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byType(RepaintBoundary),
      matchesGoldenFile('text_golden.Foreground.gradient.png'),
    );

    await tester.pumpWidget(
93
      Align(
94
        alignment: Alignment.topLeft,
95 96
        child: RepaintBoundary(
          child: Text('Hello',
97
            textDirection: TextDirection.ltr,
98 99
            style: TextStyle(
              foreground: Paint()
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
                ..color = black
                ..style = PaintingStyle.stroke
                ..strokeWidth = 2.0
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byType(RepaintBoundary),
      matchesGoldenFile('text_golden.Foreground.stroke.png'),
    );

    await tester.pumpWidget(
115
      Align(
116
        alignment: Alignment.topLeft,
117 118
        child: RepaintBoundary(
          child: Text('Hello',
119
            textDirection: TextDirection.ltr,
120 121
            style: TextStyle(
              foreground: Paint()
122 123 124 125 126 127 128 129 130 131 132 133 134 135
                ..color = black
                ..style = PaintingStyle.stroke
                ..strokeWidth = 2.0
                ..shader = linearGradient
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byType(RepaintBoundary),
      matchesGoldenFile('text_golden.Foreground.stroke_and_gradient.png'),
    );
136
  }, skip: !Platform.isLinux);
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  // TODO(garyq): This test requires an update when the background
  // drawing from the beginning of the line bug is fixed. The current
  // tested version is not completely correct.
  testWidgets('Text Background', (WidgetTester tester) async {
    const Color red = Colors.red;
    const Color blue = Colors.blue;
    const Color translucentGreen = Color(0x5000F000);
    const Color translucentDarkRed = Color(0x500F0000);
    await tester.pumpWidget(
      Align(
        alignment: Alignment.topLeft,
        child: RepaintBoundary(
          child: Container(
            width: 200.0,
            height: 100.0,
            decoration: const BoxDecoration(
              color: Colors.green,
            ),
            child: RichText(
              textDirection: TextDirection.ltr,
              text: TextSpan(
                text: 'text1 ',
                style: TextStyle(
                  color: translucentGreen,
                  background: Paint()
163
                    ..color = red.withOpacity(0.5),
164 165 166 167 168 169 170
                ),
                children: <TextSpan>[
                  TextSpan(
                    text: 'text2',
                    style: TextStyle(
                      color: translucentDarkRed,
                      background: Paint()
171 172
                        ..color = blue.withOpacity(0.5),
                    ),
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byType(RepaintBoundary),
      matchesGoldenFile('text_golden.Background.png'),
    );
  }, skip: !Platform.isLinux);

188 189
  testWidgets('Text Fade', (WidgetTester tester) async {
    await tester.pumpWidget(
190 191
        MaterialApp(
          home: Scaffold(
192
            backgroundColor: Colors.transparent,
193
            body: RepaintBoundary(
194
              child: Center(
195
                child: Container(
196 197 198
                  width: 200.0,
                  height: 200.0,
                  color: Colors.green,
199 200
                  child: Center(
                    child: Container(
201 202 203 204
                      width: 100.0,
                      color: Colors.blue,
                      child: const Text(
                        'Pp PPp PPPp PPPPp PPPPpp PPPPppp PPPPppppp ',
205
                        style: TextStyle(color: Colors.black),
206 207 208 209 210 211 212 213
                        maxLines: 3,
                        overflow: TextOverflow.fade,
                      ),
                    ),
                  ),
                ),
              ),
            ),
214
          ),
215 216 217 218 219 220 221
        )
    );

    await expectLater(
      find.byType(RepaintBoundary).first,
      matchesGoldenFile('text_golden.Fade.1.png'),
    );
222
  }, skip: !Platform.isLinux);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

  testWidgets('Default Strut text', (WidgetTester tester) async {
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          child: Container(
            width: 200.0,
            height: 100.0,
            decoration: const BoxDecoration(
              color: Color(0xff00ff00),
            ),
            child: const Text('Hello\nLine 2\nLine 3',
              textDirection: TextDirection.ltr,
              style: TextStyle(),
              strutStyle: StrutStyle(),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(Container),
      matchesGoldenFile('text_golden.StrutDefault.png'),
    );
247 248
  }, skip: true); // Should only be on linux (skip: !Platform.isLinux).
                  // Disabled for now until font inconsistency is resolved.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

  testWidgets('Strut text 1', (WidgetTester tester) async {
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          child: Container(
            width: 200.0,
            height: 100.0,
            decoration: const BoxDecoration(
              color: Color(0xff00ff00),
            ),
            child: const Text('Hello\nLine2\nLine3',
              textDirection: TextDirection.ltr,
              style: TextStyle(),
              strutStyle: StrutStyle(
                height: 1.5,
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(Container),
273
      matchesGoldenFile('text_golden.Strut.1.1.png'),
274
    );
275 276
  }, skip: true); // Should only be on linux (skip: !Platform.isLinux).
                  // Disabled for now until font inconsistency is resolved.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

  testWidgets('Strut text 2', (WidgetTester tester) async {
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          child: Container(
            width: 200.0,
            height: 100.0,
            decoration: const BoxDecoration(
              color: Color(0xff00ff00),
            ),
            child: const Text('Hello\nLine 2\nLine 3',
              textDirection: TextDirection.ltr,
              style: TextStyle(),
              strutStyle: StrutStyle(
                height: 1.5,
                fontSize: 14,
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(Container),
302
      matchesGoldenFile('text_golden.Strut.2.1.png'),
303
    );
304 305
  }, skip: true); // Should only be on linux (skip: !Platform.isLinux).
                  // Disabled for now until font inconsistency is resolved.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

  testWidgets('Strut text rich', (WidgetTester tester) async {
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          child: Container(
            width: 200.0,
            height: 150.0,
            decoration: const BoxDecoration(
              color: Color(0xff00ff00),
            ),
            child: const Text.rich(
              TextSpan(
                text: 'Hello\n',
                style: TextStyle(
                  color: Colors.red,
322
                  fontSize: 30,
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
                ),
                children: <TextSpan>[
                  TextSpan(
                    text: 'Second line!\n',
                    style: TextStyle(
                      fontSize: 5,
                      color: Colors.blue,
                    ),
                  ),
                  TextSpan(
                    text: 'Third line!\n',
                    style: TextStyle(
                      fontSize: 25,
                      color: Colors.white,
                    ),
                  ),
                ],
              ),
              textDirection: TextDirection.ltr,
              strutStyle: StrutStyle(
                fontSize: 14,
                height: 1.1,
                leading: 0.1,
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(Container),
354
      matchesGoldenFile('text_golden.Strut.3.1.png'),
355
    );
356 357
  }, skip: true); // Should only be on linux (skip: !Platform.isLinux).
                  // Disabled for now until font inconsistency is resolved.
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

  testWidgets('Strut text font fallback', (WidgetTester tester) async {
    // Font Fallback
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          child: Container(
            width: 200.0,
            height: 100.0,
            decoration: const BoxDecoration(
              color: Color(0xff00ff00),
            ),
            child: const Text('Hello\nLine 2\nLine 3',
              textDirection: TextDirection.ltr,
              style: TextStyle(),
              strutStyle: StrutStyle(
                fontFamily: 'FakeFont 1',
                fontFamilyFallback: <String>[
                  'FakeFont 2',
                  'EvilFont 3',
                  'Nice Font 4',
379
                  'ahem',
380 381 382 383 384 385 386 387 388 389
                ],
                fontSize: 14,
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(Container),
390
      matchesGoldenFile('text_golden.Strut.4.1.png'),
391
    );
392 393
  }, skip: true); // Should only be on linux (skip: !Platform.isLinux).
                  // Disabled for now until font inconsistency is resolved.
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

  testWidgets('Strut text rich forceStrutHeight', (WidgetTester tester) async {
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          child: Container(
            width: 200.0,
            height: 100.0,
            decoration: const BoxDecoration(
              color: Color(0xff00ff00),
            ),
            child: const Text.rich(
              TextSpan(
                text: 'Hello\n',
                style: TextStyle(
                  color: Colors.red,
410
                  fontSize: 30,
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
                ),
                children: <TextSpan>[
                  TextSpan(
                    text: 'Second line!\n',
                    style: TextStyle(
                      fontSize: 9,
                      color: Colors.blue,
                    ),
                  ),
                  TextSpan(
                    text: 'Third line!\n',
                    style: TextStyle(
                      fontSize: 27,
                      color: Colors.white,
                    ),
                  ),
                ],
              ),
              textDirection: TextDirection.ltr,
              strutStyle: StrutStyle(
                fontSize: 14,
                height: 1.1,
                forceStrutHeight: true,
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(Container),
442
      matchesGoldenFile('text_golden.StrutForce.1.1.png'),
443
    );
444 445
  }, skip: true); // Should only be on linux (skip: !Platform.isLinux).
                  // Disabled for now until font inconsistency is resolved.
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

  testWidgets('Decoration thickness', (WidgetTester tester) async {
    final TextDecoration allDecorations = TextDecoration.combine(
      <TextDecoration>[
        TextDecoration.underline,
        TextDecoration.overline,
        TextDecoration.lineThrough,
      ]
    );

    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          child: Container(
            width: 300.0,
            height: 100.0,
            decoration: const BoxDecoration(
              color: Color(0xff00ff00),
            ),
            child: Text(
              'Hello, wor!\nabcd.',
              style: TextStyle(
                fontSize: 25,
                decoration: allDecorations,
                decorationColor: Colors.blue,
                decorationStyle: TextDecorationStyle.dashed,
              ),
              textDirection: TextDirection.rtl,
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(Container),
      matchesGoldenFile('text_golden.Decoration.1.0.png'),
    );
  }, skip: !Platform.isLinux); // Coretext uses different thicknesses for decoration

  testWidgets('Decoration thickness', (WidgetTester tester) async {
    final TextDecoration allDecorations = TextDecoration.combine(
      <TextDecoration>[
        TextDecoration.underline,
        TextDecoration.overline,
        TextDecoration.lineThrough,
      ]
    );

    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          child: Container(
            width: 300.0,
            height: 100.0,
            decoration: const BoxDecoration(
              color: Color(0xff00ff00),
            ),
            child: Text(
              'Hello, wor!\nabcd.',
              style: TextStyle(
                fontSize: 25,
                decoration: allDecorations,
                decorationColor: Colors.blue,
                decorationStyle: TextDecorationStyle.wavy,
                decorationThickness: 4,
              ),
              textDirection: TextDirection.rtl,
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(Container),
      matchesGoldenFile('text_golden.DecorationThickness.1.0.png'),
    );
  }, skip: !Platform.isLinux); // Coretext uses different thicknesses for decoration
523
}