basics_test.dart 3.59 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
// 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_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Nested Localizations', (WidgetTester tester) async {
11
    await tester.pumpWidget(MaterialApp( // Creates the outer Localizations widget.
12
      theme: ThemeData(useMaterial3: true),
13
      home: ListView(
14
        children: <Widget>[
15
          const LocalizationTracker(key: ValueKey<String>('outer')),
16
          Localizations(
17 18
            locale: const Locale('zh', 'CN'),
            delegates: GlobalMaterialLocalizations.delegates,
19
            child: const LocalizationTracker(key: ValueKey<String>('inner')),
20 21 22 23
          ),
        ],
      ),
    ));
24 25
    // Most localized aspects of the TextTheme text styles are the same for the default US local and
    // for Chinese for Material3. The baselines for all text styles differ.
26
    final LocalizationTrackerState outerTracker = tester.state(find.byKey(const ValueKey<String>('outer'), skipOffstage: false));
27
    expect(outerTracker.textBaseline, TextBaseline.alphabetic);
28
    final LocalizationTrackerState innerTracker = tester.state(find.byKey(const ValueKey<String>('inner'), skipOffstage: false));
29
    expect(innerTracker.textBaseline, TextBaseline.ideographic);
30 31 32 33 34
  });

  testWidgets('Localizations is compatible with ChangeNotifier.dispose() called during didChangeDependencies', (WidgetTester tester) async {
    // PageView calls ScrollPosition.dispose() during didChangeDependencies.
    await tester.pumpWidget(
35
      MaterialApp(
36
        supportedLocales: const <Locale>[
37 38
          Locale('en', 'US'),
          Locale('es', 'ES'),
39 40
        ],
        localizationsDelegates: <LocalizationsDelegate<dynamic>>[
41
          _DummyLocalizationsDelegate(),
42
          ...GlobalMaterialLocalizations.delegates,
43
        ],
44
        home: PageView(),
45 46 47 48 49
      )
    );

    await tester.binding.setLocale('es', 'US');
    await tester.pump();
50
    await tester.pumpWidget(Container());
51
  });
52

53
  testWidgets('Locale without countryCode', (WidgetTester tester) async {
54 55
    // Regression test for https://github.com/flutter/flutter/pull/16782
    await tester.pumpWidget(
56
      MaterialApp(
57
        localizationsDelegates: GlobalMaterialLocalizations.delegates,
58
        supportedLocales: const <Locale>[
59 60
          Locale('es', 'ES'),
          Locale('zh'),
61
        ],
62
        home: Container(),
63 64 65
      )
    );

66
    await tester.binding.setLocale('zh', '');
67 68 69 70 71
    await tester.pump();
    await tester.binding.setLocale('es', 'US');
    await tester.pump();

  });
72 73 74 75 76 77
}

/// A localizations delegate that does not contain any useful data, and is only
/// used to trigger didChangeDependencies upon locale change.
class _DummyLocalizationsDelegate extends LocalizationsDelegate<DummyLocalizations> {
  @override
78
  Future<DummyLocalizations> load(Locale locale) async => DummyLocalizations();
79

80 81 82
  @override
  bool isSupported(Locale locale) => true;

83 84 85 86
  @override
  bool shouldReload(_DummyLocalizationsDelegate old) => true;
}

87
class DummyLocalizations { }
88 89

class LocalizationTracker extends StatefulWidget {
90
  const LocalizationTracker({super.key});
91 92

  @override
93
  State<StatefulWidget> createState() => LocalizationTrackerState();
94 95 96
}

class LocalizationTrackerState extends State<LocalizationTracker> {
97
  late TextBaseline textBaseline;
98 99 100

  @override
  Widget build(BuildContext context) {
101
    textBaseline = Theme.of(context).textTheme.bodySmall!.textBaseline!;
102
    return Container();
103 104
  }
}