localized_fonts_test.dart 10.6 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 7
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])
8
library;
9

10 11
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
12
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
13 14 15 16
// 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() {
17
  testWidgetsWithLeakTracking(
18
    'Material2 - RichText TextSpan styles with different locales',
19 20 21
    (WidgetTester tester) async {

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

      await expectLater(
        find.byType(RichText),
58
        matchesGoldenFile('m2_localized_fonts.rich_text.styled_text_span.png'),
59
      );
60 61
    },
  );
62

63
  testWidgetsWithLeakTracking(
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 107 108
    'Material3 - RichText TextSpan styles with different locales',
    (WidgetTester tester) async {

      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData(useMaterial3: true),
          supportedLocales: const <Locale>[
            Locale('en', 'US'),
            Locale('ja'),
            Locale('zh'),
          ],
          home: Builder(
            builder: (BuildContext context) {
              const String character = '骨';
              final TextStyle style = Theme.of(context).textTheme.displayMedium!;
              return Scaffold(
                body: Container(
                  padding: const EdgeInsets.all(48.0),
                  alignment: Alignment.center,
                  child: RepaintBoundary(
                    // Expected result can be seen here:
                    // https://user-images.githubusercontent.com/1377460/40503473-faad6f34-5f42-11e8-972b-d83b727c9d0e.png
                    child: RichText(
                      text: TextSpan(
                        children: <TextSpan>[
                          TextSpan(text: character, style: style.copyWith(locale: const Locale('ja'))),
                          TextSpan(text: character, style: style.copyWith(locale: const Locale('zh'))),
                        ],
                      ),
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      );

      await expectLater(
        find.byType(RichText),
        matchesGoldenFile('m3_localized_fonts.rich_text.styled_text_span.png'),
      );
    },
  );

109
  testWidgetsWithLeakTracking(
110
    'Material2 - Text with locale-specific glyphs, ambient locale',
111 112
    (WidgetTester tester) async {
      await tester.pumpWidget(
113
        MaterialApp(
114
          theme: ThemeData(useMaterial3: false),
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.displayMedium!;
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
                        Localizations.override(
135 136
                          context: context,
                          locale: const Locale('ja'),
137
                          child: Text(character, style: style),
138
                        ),
139
                        Localizations.override(
140 141
                          context: context,
                          locale: const Locale('zh'),
142
                          child: Text(character, style: style),
143 144 145 146 147 148 149 150
                        ),
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
151
        ),
152 153 154 155
      );

      await expectLater(
        find.byType(Row),
156
        matchesGoldenFile('localized_fonts.text_ambient_locale.chars.png'),
157
      );
158 159
    },
  );
160

161
  testWidgetsWithLeakTracking(
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    'Material3 - Text with locale-specific glyphs, ambient locale',
    (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData(useMaterial3: true),
          supportedLocales: const <Locale>[
            Locale('en', 'US'),
            Locale('ja'),
            Locale('zh'),
          ],
          home: Builder(
            builder: (BuildContext context) {
              const String character = '骨';
              final TextStyle style = Theme.of(context).textTheme.displayMedium!;
              return Scaffold(
                body: Container(
                  padding: const EdgeInsets.all(48.0),
                  alignment: Alignment.center,
                  child: RepaintBoundary(
                    // Expected result can be seen here:
                    // https://user-images.githubusercontent.com/1377460/40503473-faad6f34-5f42-11e8-972b-d83b727c9d0e.png
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Localizations.override(
                          context: context,
                          locale: const Locale('ja'),
                          child: Text(character, style: style),
                        ),
                        Localizations.override(
                          context: context,
                          locale: const Locale('zh'),
                          child: Text(character, style: style),
                        ),
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      );

      await expectLater(
        find.byType(Row),
        matchesGoldenFile('m3_localized_fonts.text_ambient_locale.chars.png'),
      );
    },
  );

213
  testWidgetsWithLeakTracking(
214
    'Material2 - Text with locale-specific glyphs, explicit locale',
215 216
    (WidgetTester tester) async {
      await tester.pumpWidget(
217
        MaterialApp(
218
          theme: ThemeData(useMaterial3: false),
219
          supportedLocales: const <Locale>[
220 221 222
            Locale('en', 'US'),
            Locale('ja'),
            Locale('zh'),
223
          ],
224
          home: Builder(
225 226
            builder: (BuildContext context) {
              const String character = '骨';
227
              final TextStyle style = Theme.of(context).textTheme.displayMedium!;
228 229
              return Scaffold(
                body: Container(
230 231
                  padding: const EdgeInsets.all(48.0),
                  alignment: Alignment.center,
232
                  child: RepaintBoundary(
233 234
                    // Expected result can be seen here:
                    // https://user-images.githubusercontent.com/1377460/40503473-faad6f34-5f42-11e8-972b-d83b727c9d0e.png
235
                    child: Row(
236 237
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
238
                        Text(character, style: style, locale: const Locale('ja')),
239
                        Text(character, style: style, locale: const Locale('zh')),
240 241 242 243 244 245 246
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
247
        ),
248 249 250 251
      );

      await expectLater(
        find.byType(Row),
252
        matchesGoldenFile('m2_localized_fonts.text_explicit_locale.chars.png'),
253
      );
254 255
    },
  );
256

257
  testWidgetsWithLeakTracking(
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    'Material3 - Text with locale-specific glyphs, explicit locale',
    (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData(useMaterial3: true),
          supportedLocales: const <Locale>[
            Locale('en', 'US'),
            Locale('ja'),
            Locale('zh'),
          ],
          home: Builder(
            builder: (BuildContext context) {
              const String character = '骨';
              final TextStyle style = Theme.of(context).textTheme.displayMedium!;
              return Scaffold(
                body: Container(
                  padding: const EdgeInsets.all(48.0),
                  alignment: Alignment.center,
                  child: RepaintBoundary(
                    // Expected result can be seen here:
                    // https://user-images.githubusercontent.com/1377460/40503473-faad6f34-5f42-11e8-972b-d83b727c9d0e.png
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Text(character, style: style, locale: const Locale('ja')),
                        Text(character, style: style, locale: const Locale('zh')),
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      );

      await expectLater(
        find.byType(Row),
        matchesGoldenFile('m3_localized_fonts.text_explicit_locale.chars.png'),
      );
    },
  );
300
}