mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

* Refactor widget test framework Instead of: ```dart test("Card Collection smoke test", () { testWidgets((WidgetTester tester) { ``` ...you now say: ```dart testWidgets("Card Collection smoke test", (WidgetTester tester) { ``` Instead of: ```dart expect(tester, hasWidget(find.text('hello'))); ``` ...you now say: ```dart expect(find.text('hello'), findsOneWidget); ``` Instead of the previous API (exists, widgets, widget, stateOf, elementOf, etc), you now have the following comprehensive API. All these are functions that take a Finder, except the all* properties. * `any()` - true if anything matches, c.f. `Iterable.any` * `allWidgets` - all the widgets in the tree * `widget()` - the one and only widget that matches the finder * `firstWidget()` - the first widget that matches the finder * `allElements` - all the elements in the tree * `element()` - the one and only element that matches the finder * `firstElement()` - the first element that matches the finder * `allStates` - all the `State`s in the tree * `state()` - the one and only state that matches the finder * `firstState()` - the first state that matches the finder * `allRenderObjects` - all the render objects in the tree * `renderObject()` - the one and only render object that matches the finder * `firstRenderObject()` - the first render object that matches the finder There's also `layers' which returns the list of current layers. `tap`, `fling`, getCenter, getSize, etc, take Finders, like the APIs above, and expect there to only be one matching widget. The finders are: * `find.text(String text)` * `find.widgetWithText(Type widgetType, String text)` * `find.byKey(Key key)` * `find.byType(Type type)` * `find.byElementType(Type type)` * `find.byConfig(Widget config)` * `find.byWidgetPredicate(WidgetPredicate predicate)` * `find.byElementPredicate(ElementPredicate predicate)` The matchers (for `expect`) are: * `findsNothing` * `findsWidgets` * `findsOneWidget` * `findsNWidgets(n)` * `isOnStage` * `isOffStage` * `isInCard` * `isNotInCard` Benchmarks now use benchmarkWidgets instead of testWidgets. Also, for those of you using mockers, `serviceMocker` now automatically handles the binding initialization. This patch also: * changes how tests are run so that we can more easily swap the logic out for a "real" mode instead of FakeAsync. * introduces CachingIterable. * changes how flutter_driver interacts with the widget tree to use the aforementioned new API rather than ElementTreeTester, which is gone. * removes ElementTreeTester. * changes the semantics of a test for scrollables because we couldn't convince ourselves that the old semantics made sense; it only worked before because flushing the microtasks after every event was broken. * fixes the flushing of microtasks after every event. * Reindent the tests * Fix review comments
138 lines
4.5 KiB
Dart
138 lines
4.5 KiB
Dart
import 'package:flutter_markdown/flutter_markdown.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:flutter/gestures.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:test/test.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
void main() {
|
|
testWidgets('Simple string', (WidgetTester tester) {
|
|
tester.pumpWidget(new MarkdownBody(data: 'Hello'));
|
|
|
|
Iterable<Widget> widgets = tester.allWidgets;
|
|
_expectWidgetTypes(widgets, <Type>[MarkdownBody, Column, Container, Padding, RichText]);
|
|
_expectTextStrings(widgets, <String>['Hello']);
|
|
});
|
|
|
|
testWidgets('Header', (WidgetTester tester) {
|
|
tester.pumpWidget(new MarkdownBody(data: '# Header'));
|
|
|
|
Iterable<Widget> widgets = tester.allWidgets;
|
|
_expectWidgetTypes(widgets, <Type>[MarkdownBody, Column, Container, Padding, RichText]);
|
|
_expectTextStrings(widgets, <String>['Header']);
|
|
});
|
|
|
|
testWidgets('Empty string', (WidgetTester tester) {
|
|
tester.pumpWidget(new MarkdownBody(data: ''));
|
|
|
|
Iterable<Widget> widgets = tester.allWidgets;
|
|
_expectWidgetTypes(widgets, <Type>[MarkdownBody, Column]);
|
|
});
|
|
|
|
testWidgets('Ordered list', (WidgetTester tester) {
|
|
tester.pumpWidget(new MarkdownBody(data: '1. Item 1\n1. Item 2\n2. Item 3'));
|
|
|
|
Iterable<Widget> widgets = tester.allWidgets;
|
|
_expectTextStrings(widgets, <String>[
|
|
'1.',
|
|
'Item 1',
|
|
'2.',
|
|
'Item 2',
|
|
'3.',
|
|
'Item 3']
|
|
);
|
|
});
|
|
|
|
testWidgets('Unordered list', (WidgetTester tester) {
|
|
tester.pumpWidget(new MarkdownBody(data: '- Item 1\n- Item 2\n- Item 3'));
|
|
|
|
Iterable<Widget> widgets = tester.allWidgets;
|
|
_expectTextStrings(widgets, <String>[
|
|
'•',
|
|
'Item 1',
|
|
'•',
|
|
'Item 2',
|
|
'•',
|
|
'Item 3']
|
|
);
|
|
});
|
|
|
|
testWidgets('Scrollable wrapping', (WidgetTester tester) {
|
|
tester.pumpWidget(new Markdown(data: ''));
|
|
|
|
List<Widget> widgets = tester.allWidgets.toList();
|
|
_expectWidgetTypes(widgets.take(2), <Type>[
|
|
Markdown,
|
|
ScrollableViewport,
|
|
]);
|
|
_expectWidgetTypes(widgets.reversed.take(3).toList().reversed, <Type>[
|
|
Padding,
|
|
MarkdownBody,
|
|
Column
|
|
]);
|
|
});
|
|
|
|
testWidgets('Links', (WidgetTester tester) {
|
|
tester.pumpWidget(new Markdown(data: '[Link Text](href)'));
|
|
|
|
RichText textWidget = tester.allWidgets.firstWhere((Widget widget) => widget is RichText);
|
|
TextSpan span = textWidget.text;
|
|
|
|
expect(span.children[0].recognizer.runtimeType, equals(TapGestureRecognizer));
|
|
});
|
|
|
|
testWidgets('Changing config - data', (WidgetTester tester) {
|
|
tester.pumpWidget(new Markdown(data: 'Data1'));
|
|
_expectTextStrings(tester.allWidgets, <String>['Data1']);
|
|
|
|
String stateBefore = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
|
tester.pumpWidget(new Markdown(data: 'Data1'));
|
|
String stateAfter = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
|
expect(stateBefore, equals(stateAfter));
|
|
|
|
tester.pumpWidget(new Markdown(data: 'Data2'));
|
|
_expectTextStrings(tester.allWidgets, <String>['Data2']);
|
|
});
|
|
|
|
testWidgets('Changing config - style', (WidgetTester tester) {
|
|
ThemeData theme = new ThemeData.light();
|
|
|
|
MarkdownStyle style1 = new MarkdownStyle.defaultFromTheme(theme);
|
|
MarkdownStyle style2 = new MarkdownStyle.largeFromTheme(theme);
|
|
|
|
tester.pumpWidget(new Markdown(data: 'Test', markdownStyle: style1));
|
|
|
|
String stateBefore = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
|
tester.pumpWidget(new Markdown(data: 'Test', markdownStyle: style2));
|
|
String stateAfter = WidgetsBinding.instance.renderViewElement.toStringDeep();
|
|
expect(stateBefore, isNot(stateAfter));
|
|
});
|
|
}
|
|
|
|
void _expectWidgetTypes(Iterable<Widget> widgets, List<Type> expected) {
|
|
List<Type> actual = widgets.map((Widget w) => w.runtimeType).toList();
|
|
expect(actual, expected);
|
|
}
|
|
|
|
void _expectTextStrings(Iterable<Widget> widgets, List<String> strings) {
|
|
int currentString = 0;
|
|
for (Widget widget in widgets) {
|
|
if (widget is RichText) {
|
|
TextSpan span = widget.text;
|
|
String text = _extractTextFromTextSpan(span);
|
|
expect(text, equals(strings[currentString]));
|
|
currentString += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
String _extractTextFromTextSpan(TextSpan span) {
|
|
String text = span.text ?? '';
|
|
if (span.children != null) {
|
|
for (TextSpan child in span.children) {
|
|
text += _extractTextFromTextSpan(child);
|
|
}
|
|
}
|
|
return text;
|
|
}
|