[flutter_test] new WidgetTester API based on finder objects (#3288)

This commit is contained in:
Yegor
2016-04-13 23:40:15 -07:00
parent a5a34d9704
commit f3a4f722c4
67 changed files with 1671 additions and 1122 deletions

View File

@@ -13,7 +13,7 @@ void main() {
hello_world.main(); // builds the app and schedules a frame but doesn't trigger one
tester.pump(); // triggers a frame
expect(tester.findText('Hello, world!'), isNotNull);
expect(tester, hasWidget(find.text('Hello, world!')));
});
});
}

View File

@@ -16,45 +16,45 @@ void main() {
tester.pump(); // triggers a frame
// Try loading Weather demo
tester.tap(tester.findText('Demos'));
tester.tap(find.text('Demos'));
tester.pump();
tester.pump(const Duration(seconds: 1)); // wait til it's really opened
tester.tap(tester.findText('Weather'));
tester.tap(find.text('Weather'));
tester.pump();
tester.pump(const Duration(seconds: 1)); // wait til it's really opened
// Go back
Element backButton = tester.findElement((Element element) {
Finder backButton = find.byElement((Element element) {
Widget widget = element.widget;
if (widget is Tooltip)
return widget.message == 'Back';
return false;
});
expect(backButton, isNotNull);
expect(tester, hasWidget(backButton));
tester.tap(backButton);
tester.pump(); // start going back
tester.pump(const Duration(seconds: 1)); // wait til it's finished
// Open menu
Element navigationMenu = tester.findElement((Element element) {
Finder navigationMenu = find.byElement((Element element) {
Widget widget = element.widget;
if (widget is Tooltip)
return widget.message == 'Open navigation menu';
return false;
});
expect(navigationMenu, isNotNull);
expect(tester, hasWidget(navigationMenu));
tester.tap(navigationMenu);
tester.pump(); // start opening menu
tester.pump(const Duration(seconds: 1)); // wait til it's really opened
// switch theme
tester.tap(tester.findText('Dark'));
tester.tap(find.text('Dark'));
tester.pump();
tester.pump(const Duration(seconds: 1)); // wait til it's changed
// switch theme
tester.tap(tester.findText('Light'));
tester.tap(find.text('Light'));
tester.pump();
tester.pump(const Duration(seconds: 1)); // wait til it's changed
});

View File

@@ -38,7 +38,7 @@ Element findElementOfExactWidgetTypeGoingUp(Element node, Type targetType) {
final RegExp materialIconAssetNameColorExtractor = new RegExp(r'[^/]+/ic_.+_(white|black)_[0-9]+dp\.png');
void checkIconColor(WidgetTester tester, String label, Color color) {
void checkIconColor(ElementTreeTester tester, String label, Color color) {
// The icon is going to be in the same merged semantics box as the text
// regardless of how the menu item is represented, so this is a good
// way to find the menu item. I hope.
@@ -59,11 +59,11 @@ void main() {
tester.pump(); // triggers a frame
// sanity check
expect(tester.findText('MARKET'), isNotNull);
expect(tester.findText('Help & Feedback'), isNull);
expect(tester, hasWidget(find.text('MARKET')));
expect(tester, doesNotHaveWidget(find.text('Help & Feedback')));
tester.pump(new Duration(seconds: 2));
expect(tester.findText('MARKET'), isNotNull);
expect(tester.findText('Help & Feedback'), isNull);
expect(tester, hasWidget(find.text('MARKET')));
expect(tester, doesNotHaveWidget(find.text('Help & Feedback')));
// drag the drawer out
Point left = new Point(0.0, ui.window.size.height / 2.0);
@@ -74,24 +74,24 @@ void main() {
tester.pump();
gesture.up();
tester.pump();
expect(tester.findText('MARKET'), isNotNull);
expect(tester.findText('Help & Feedback'), isNotNull);
expect(tester, hasWidget(find.text('MARKET')));
expect(tester, hasWidget(find.text('Help & Feedback')));
// check the colour of the icon - light mode
checkIconColor(tester, 'Stock List', Colors.purple[500]); // theme primary color
checkIconColor(tester, 'Account Balance', Colors.black45); // enabled
checkIconColor(tester, 'Help & Feedback', Colors.black26); // disabled
checkIconColor(tester.elementTreeTester, 'Stock List', Colors.purple[500]); // theme primary color
checkIconColor(tester.elementTreeTester, 'Account Balance', Colors.black45); // enabled
checkIconColor(tester.elementTreeTester, 'Help & Feedback', Colors.black26); // disabled
// switch to dark mode
tester.tap(tester.findText('Pessimistic'));
tester.tap(find.text('Pessimistic'));
tester.pump(); // get the tap and send the notification that the theme has changed
tester.pump(); // start the theme transition
tester.pump(const Duration(seconds: 5)); // end the transition
// check the colour of the icon - dark mode
checkIconColor(tester, 'Stock List', Colors.redAccent[200]); // theme accent color
checkIconColor(tester, 'Account Balance', Colors.white); // enabled
checkIconColor(tester, 'Help & Feedback', Colors.white30); // disabled
checkIconColor(tester.elementTreeTester, 'Stock List', Colors.redAccent[200]); // theme accent color
checkIconColor(tester.elementTreeTester, 'Account Balance', Colors.white); // enabled
checkIconColor(tester.elementTreeTester, 'Help & Feedback', Colors.white30); // disabled
});
});

View File

@@ -2,7 +2,6 @@
// 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';
import 'package:stocks/main.dart' as stocks;
import 'package:stocks/stock_data.dart' as stock_data;
@@ -14,15 +13,13 @@ void main() {
test("Test changing locale", () {
testWidgets((WidgetTester tester) {
stocks.main();
tester.async.flushMicrotasks(); // see https://github.com/flutter/flutter/issues/1865
tester.flushMicrotasks(); // see https://github.com/flutter/flutter/issues/1865
tester.pump();
Element tab = tester.findText('MARKET');
expect(tab, isNotNull);
expect(tester, hasWidget(find.text('MARKET')));
tester.setLocale("es", "US");
tester.pump();
Text text = tab.widget;
expect(text.data, equals("MERCADO"));
expect(tester, hasWidget(find.text('MERCADO')));
// TODO(abarth): We're leaking an animation. We should track down the leak
// and plug it rather than waiting for the animation to end here.