localized_fonts_test.dart 5.48 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 26
            builder: (BuildContext context) {
              const String character = '骨';
              final TextStyle style = Theme.of(context).textTheme.display3;
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
    skip: isBrowser, // TODO(yjbanov): implement goldens on the Web: https://github.com/flutter/flutter/issues/40297
56
  );
57 58 59 60 61

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

      await expectLater(
        find.byType(Row),
104
        matchesGoldenFile('localized_fonts.text_ambient_locale.chars.png'),
105
      );
106
    },
107
    skip: isBrowser, // TODO(yjbanov): implement goldens on the Web: https://github.com/flutter/flutter/issues/40297
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 123
            builder: (BuildContext context) {
              const String character = '骨';
              final TextStyle style = Theme.of(context).textTheme.display3;
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
    skip: isBrowser, // TODO(yjbanov): implement goldens on the Web: https://github.com/flutter/flutter/issues/40297
152
  );
153 154

}