basics_test.dart 3.34 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 12
    await tester.pumpWidget(MaterialApp( // Creates the outer Localizations widget.
      home: ListView(
13
        children: <Widget>[
14
          const LocalizationTracker(key: ValueKey<String>('outer')),
15
          Localizations(
16 17
            locale: const Locale('zh', 'CN'),
            delegates: GlobalMaterialLocalizations.delegates,
18
            child: const LocalizationTracker(key: ValueKey<String>('inner')),
19 20 21 22 23
          ),
        ],
      ),
    ));

24
    final LocalizationTrackerState outerTracker = tester.state(find.byKey(const ValueKey<String>('outer'), skipOffstage: false));
25
    expect(outerTracker.captionFontSize, 12.0);
26
    final LocalizationTrackerState innerTracker = tester.state(find.byKey(const ValueKey<String>('inner'), skipOffstage: false));
27 28 29 30 31 32
    expect(innerTracker.captionFontSize, 13.0);
  });

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

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

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

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

  });
70 71 72 73 74 75
}

/// 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
76
  Future<DummyLocalizations> load(Locale locale) async => DummyLocalizations();
77

78 79 80
  @override
  bool isSupported(Locale locale) => true;

81 82 83 84
  @override
  bool shouldReload(_DummyLocalizationsDelegate old) => true;
}

85
class DummyLocalizations { }
86 87

class LocalizationTracker extends StatefulWidget {
88
  const LocalizationTracker({super.key});
89 90

  @override
91
  State<StatefulWidget> createState() => LocalizationTrackerState();
92 93 94
}

class LocalizationTrackerState extends State<LocalizationTracker> {
95
  late double captionFontSize;
96 97 98

  @override
  Widget build(BuildContext context) {
99
    captionFontSize = Theme.of(context).textTheme.caption!.fontSize!;
100
    return Container();
101 102
  }
}