localized_fonts_test.dart 5.14 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

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

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

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

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

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

}