localized_fonts_test.dart 5.16 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 9 10 11 12 13 14 15 16 17 18
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

// TODO(hansmuller): when https://github.com/flutter/flutter/issues/17700
// is fixed, these tests should be updated to use a real font (not Ahem).

void main() {
  testWidgets(
    'RichText TextSpan styles with different locales',
    (WidgetTester tester) async {

      await tester.pumpWidget(
19
        MaterialApp(
20
          supportedLocales: const <Locale>[
21 22 23
            Locale('en', 'US'),
            Locale('ja'),
            Locale('zh'),
24
          ],
25
          home: Builder(
26 27
            builder: (BuildContext context) {
              const String character = '骨';
28
              final TextStyle style = Theme.of(context).textTheme.headline2;
29 30
              return Scaffold(
                body: Container(
31 32
                  padding: const EdgeInsets.all(48.0),
                  alignment: Alignment.center,
33
                  child: RepaintBoundary(
34 35
                    // Expected result can be seen here:
                    // https://user-images.githubusercontent.com/1377460/40503473-faad6f34-5f42-11e8-972b-d83b727c9d0e.png
36 37
                    child: RichText(
                      text: TextSpan(
38
                        children: <TextSpan>[
39 40
                          TextSpan(text: character, style: style.copyWith(locale: const Locale('ja'))),
                          TextSpan(text: character, style: style.copyWith(locale: const Locale('zh'))),
41 42 43 44 45 46 47 48
                        ],
                      ),
                    ),
                  ),
                ),
              );
            },
          ),
49
        ),
50 51 52 53
      );

      await expectLater(
        find.byType(RichText),
54
        matchesGoldenFile('localized_fonts.rich_text.styled_text_span.png'),
55
      );
56 57
    },
  );
58 59 60 61 62

  testWidgets(
    'Text with locale-specific glyphs, ambient locale',
    (WidgetTester tester) async {
      await tester.pumpWidget(
63
        MaterialApp(
64
          supportedLocales: const <Locale>[
65 66 67
            Locale('en', 'US'),
            Locale('ja'),
            Locale('zh'),
68
          ],
69
          home: Builder(
70 71
            builder: (BuildContext context) {
              const String character = '骨';
72
              final TextStyle style = Theme.of(context).textTheme.headline2;
73 74
              return Scaffold(
                body: Container(
75 76
                  padding: const EdgeInsets.all(48.0),
                  alignment: Alignment.center,
77
                  child: RepaintBoundary(
78 79
                    // Expected result can be seen here:
                    // https://user-images.githubusercontent.com/1377460/40503473-faad6f34-5f42-11e8-972b-d83b727c9d0e.png
80
                    child: Row(
81 82
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
83
                        Localizations.override(
84 85
                          context: context,
                          locale: const Locale('ja'),
86
                          child: Text(character, style: style),
87
                        ),
88
                        Localizations.override(
89 90
                          context: context,
                          locale: const Locale('zh'),
91
                          child: Text(character, style: style),
92 93 94 95 96 97 98 99
                        ),
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
100
        ),
101 102 103 104
      );

      await expectLater(
        find.byType(Row),
105
        matchesGoldenFile('localized_fonts.text_ambient_locale.chars.png'),
106
      );
107 108
    },
  );
109 110 111 112 113

  testWidgets(
    'Text with locale-specific glyphs, explicit locale',
    (WidgetTester tester) async {
      await tester.pumpWidget(
114
        MaterialApp(
115
          supportedLocales: const <Locale>[
116 117 118
            Locale('en', 'US'),
            Locale('ja'),
            Locale('zh'),
119
          ],
120
          home: Builder(
121 122
            builder: (BuildContext context) {
              const String character = '骨';
123
              final TextStyle style = Theme.of(context).textTheme.headline2;
124 125
              return Scaffold(
                body: Container(
126 127
                  padding: const EdgeInsets.all(48.0),
                  alignment: Alignment.center,
128
                  child: RepaintBoundary(
129 130
                    // Expected result can be seen here:
                    // https://user-images.githubusercontent.com/1377460/40503473-faad6f34-5f42-11e8-972b-d83b727c9d0e.png
131
                    child: Row(
132 133
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
134
                        Text(character, style: style, locale: const Locale('ja')),
135
                        Text(character, style: style, locale: const Locale('zh')),
136 137 138 139 140 141 142
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
143
        ),
144 145 146 147
      );

      await expectLater(
        find.byType(Row),
148
        matchesGoldenFile('localized_fonts.text_explicit_locale.chars.png'),
149
      );
150 151
    },
  );
152 153

}