basics_test.dart 3.4 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 41
          GlobalMaterialLocalizations.delegate,
        ],
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 52 53

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

    await tester.binding.setLocale('zh', null);
    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 87 88 89
  @override
  bool shouldReload(_DummyLocalizationsDelegate old) => true;
}

class DummyLocalizations {}

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

  @override
93
  State<StatefulWidget> createState() => LocalizationTrackerState();
94 95 96 97 98 99 100 101
}

class LocalizationTrackerState extends State<LocalizationTracker> {
  double captionFontSize;

  @override
  Widget build(BuildContext context) {
    captionFontSize = Theme.of(context).textTheme.caption.fontSize;
102
    return Container();
103 104
  }
}