From 09894ec50bd55f6b27d879d1c366c01a3bd6b357 Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Tue, 8 Dec 2015 16:05:53 -0800 Subject: [PATCH] Add a test to stocks for changing the locale I had to add a setLocale method to WidgetTester and split the code in FlutterBinding which handled locale changes to allow me to dispatch a locale change w/o actually changing what the c++ code reports as the locale. Also added the test to Travis. @abarth @jason-simmons --- examples/stocks/pubspec.yaml | 3 +++ examples/stocks/tests/locale_test.dart | 23 +++++++++++++++++++ .../flutter/lib/src/rendering/binding.dart | 8 +++++-- .../flutter_test/lib/src/widget_tester.dart | 6 +++++ travis/test.sh | 8 +++++-- 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 examples/stocks/tests/locale_test.dart diff --git a/examples/stocks/pubspec.yaml b/examples/stocks/pubspec.yaml index d633a460083..146dac10c2a 100644 --- a/examples/stocks/pubspec.yaml +++ b/examples/stocks/pubspec.yaml @@ -4,3 +4,6 @@ dependencies: path: ../../packages/flutter intl: '>=0.12.4+2 <0.13.0' +dev_dependencies: + flutter_test: + path: ../../packages/flutter_test diff --git a/examples/stocks/tests/locale_test.dart b/examples/stocks/tests/locale_test.dart new file mode 100644 index 00000000000..6c093134149 --- /dev/null +++ b/examples/stocks/tests/locale_test.dart @@ -0,0 +1,23 @@ + +import 'package:test/test.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter/material.dart'; +import 'package:stocks/main.dart' as stocks; +import 'package:stocks/stock_data.dart' as stock_data; + +void main() { + stock_data.StockDataFetcher.actuallyFetchData = false; + + test("Test changing locale", () { + testWidgets((WidgetTester tester) { + stocks.main(); + tester.pump(const Duration(seconds: 1)); // Unclear why duration is required. + + Element tab = tester.findText('MARKET'); + expect(tab, isNotNull); + tester.setLocale("es", "US"); + tester.pump(); + expect(tab.widget.data, equals("MERCADO")); + }); + }); +} diff --git a/packages/flutter/lib/src/rendering/binding.dart b/packages/flutter/lib/src/rendering/binding.dart index 34e6554eb79..23d3b02f961 100644 --- a/packages/flutter/lib/src/rendering/binding.dart +++ b/packages/flutter/lib/src/rendering/binding.dart @@ -29,7 +29,7 @@ class _PointerState { _pointerCount += 1; _pointer = _pointerCount; } - + bool get down => _down; bool _down = false; void setDown() { @@ -247,8 +247,12 @@ class FlutterBinding extends HitTestTarget { } void _handleLocaleChanged() { + dispatchLocaleChanged(ui.window.locale); + } + + void dispatchLocaleChanged(ui.Locale locale) { for (BindingObserver observer in _observers) - observer.didChangeLocale(ui.window.locale); + observer.didChangeLocale(locale); } void _handlePersistentFrameCallback(Duration timeStamp) { diff --git a/packages/flutter_test/lib/src/widget_tester.dart b/packages/flutter_test/lib/src/widget_tester.dart index 612a94400bf..d71b8e826b4 100644 --- a/packages/flutter_test/lib/src/widget_tester.dart +++ b/packages/flutter_test/lib/src/widget_tester.dart @@ -32,6 +32,12 @@ class WidgetTester { pump(duration); } + void setLocale(String languageCode, String countryCode) { + ui.Locale locale = new ui.Locale(languageCode, countryCode); + FlutterBinding.instance.dispatchLocaleChanged(locale); + async.flushMicrotasks(); + } + void pump([ Duration duration ]) { if (duration != null) async.elapse(duration); diff --git a/travis/test.sh b/travis/test.sh index 471a80daafe..f331e6324d0 100755 --- a/travis/test.sh +++ b/travis/test.sh @@ -1,11 +1,13 @@ #!/bin/bash set -ex +export PATH="$PWD/bin:$PATH" + # analyze all the Dart code in the repo -./bin/flutter analyze --flutter-repo --no-current-directory --no-current-package --congratulate +flutter analyze --flutter-repo --no-current-directory --no-current-package --congratulate # flutter package tests -./bin/flutter test --flutter-repo +flutter test --flutter-repo (cd packages/cassowary; pub run test -j1) # (cd packages/flutter_sprites; ) # No tests to run. @@ -15,6 +17,8 @@ set -ex # (cd packages/playfair; ) # No tests to run. # (cd packages/updater; ) # No tests to run. +(cd examples/stocks; flutter test) + if [ $TRAVIS_PULL_REQUEST = "false" ]; then if [ $TRAVIS_BRANCH = "master" ]; then (cd packages/flutter; dartdoc)