forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.dart
219 lines (188 loc) · 7 KB
/
utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'dart:io' as io;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
final TextTheme textTheme = Typography.material2018()
.black
.merge(const TextTheme(bodyMedium: TextStyle(fontSize: 12.0)));
Iterable<Widget> selfAndDescendantWidgetsOf(Finder start, WidgetTester tester) {
final Element startElement = tester.element(start);
final Iterable<Widget> descendants =
collectAllElementsFrom(startElement, skipOffstage: false)
.map((Element e) => e.widget);
return <Widget>[
startElement.widget,
...descendants,
];
}
void expectWidgetTypes(Iterable<Widget> widgets, List<Type> expected) {
final 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 (final Widget widget in widgets) {
if (widget is RichText) {
final TextSpan span = widget.text as TextSpan;
final String text = _extractTextFromTextSpan(span);
expect(text, equals(strings[currentString]));
currentString += 1;
}
}
}
String _extractTextFromTextSpan(TextSpan span) {
String text = span.text ?? '';
if (span.children != null) {
for (final TextSpan child in span.children! as Iterable<TextSpan>) {
text += _extractTextFromTextSpan(child);
}
}
return text;
}
// Check the font style and weight of the text span.
void expectTextSpanStyle(
TextSpan textSpan, FontStyle? style, FontWeight weight) {
// Verify a text style is set
expect(textSpan.style, isNotNull, reason: 'text span text style is null');
// Font style check
if (style == null) {
expect(textSpan.style!.fontStyle, isNull, reason: 'font style is not null');
} else {
expect(textSpan.style!.fontStyle, isNotNull, reason: 'font style is null');
expect(
textSpan.style!.fontStyle == style,
isTrue,
reason: 'font style is not $style',
);
}
// Font weight check
expect(textSpan.style, isNotNull, reason: 'font style is null');
expect(
textSpan.style!.fontWeight == weight,
isTrue,
reason: 'font weight is not $weight',
);
}
@immutable
class MarkdownLink {
const MarkdownLink(this.text, this.destination, [this.title = '']);
final String text;
final String? destination;
final String title;
@override
bool operator ==(Object other) =>
other is MarkdownLink &&
other.text == text &&
other.destination == destination &&
other.title == title;
@override
int get hashCode => '$text$destination$title'.hashCode;
@override
String toString() {
return '[$text]($destination "$title")';
}
}
/// Verify a valid link structure has been created. This routine checks for the
/// link text and the associated [TapGestureRecognizer] on the text span.
void expectValidLink(String linkText) {
final Finder richTextFinder = find.byType(RichText);
expect(richTextFinder, findsOneWidget);
final RichText richText = richTextFinder.evaluate().first.widget as RichText;
// Verify the link text.
expect(richText.text, isNotNull);
expect(richText.text, isA<TextSpan>());
// Verify the link text is a onTap gesture recognizer.
final TextSpan textSpan = richText.text as TextSpan;
expectLinkTextSpan(textSpan, linkText);
}
void expectLinkTextSpan(TextSpan textSpan, String linkText) {
expect(textSpan.children, isNull);
expect(textSpan.toPlainText(), linkText);
expect(textSpan.recognizer, isNotNull);
expect(textSpan.recognizer, isA<TapGestureRecognizer>());
final TapGestureRecognizer? tapRecognizer =
textSpan.recognizer as TapGestureRecognizer?;
expect(tapRecognizer?.onTap, isNotNull);
// Execute the onTap callback handler.
tapRecognizer!.onTap!();
}
void expectInvalidLink(String linkText) {
final Finder richTextFinder = find.byType(RichText);
expect(richTextFinder, findsOneWidget);
final RichText richText = richTextFinder.evaluate().first.widget as RichText;
expect(richText.text, isNotNull);
expect(richText.text, isA<TextSpan>());
final String text = richText.text.toPlainText();
expect(text, linkText);
final TextSpan textSpan = richText.text as TextSpan;
expect(textSpan.recognizer, isNull);
}
void expectTableSize(int rows, int columns) {
final Finder tableFinder = find.byType(Table);
expect(tableFinder, findsOneWidget);
final Table table = tableFinder.evaluate().first.widget as Table;
expect(table.children.length, rows);
for (int index = 0; index < rows; index++) {
expect(_ambiguate(table.children[index].children)!.length, columns);
}
}
void expectLinkTap(MarkdownLink? actual, MarkdownLink expected) {
expect(actual, equals(expected),
reason:
'incorrect link tap results, actual: $actual expected: $expected');
}
String dumpRenderView() {
return WidgetsBinding.instance.renderViewElement!.toStringDeep().replaceAll(
RegExp(r'SliverChildListDelegate#\d+', multiLine: true),
'SliverChildListDelegate',
);
}
/// Wraps a widget with a left-to-right [Directionality] for tests.
Widget boilerplate(Widget child) {
return Directionality(
textDirection: TextDirection.ltr,
child: child,
);
}
class TestAssetBundle extends CachingAssetBundle {
@override
Future<ByteData> load(String key) async {
if (key == 'AssetManifest.json') {
const String manifest = r'{"assets/logo.png":["assets/logo.png"]}';
final ByteData asset =
ByteData.view(utf8.encoder.convert(manifest).buffer);
return Future<ByteData>.value(asset);
} else if (key == 'AssetManifest.bin') {
final ByteData manifest = const StandardMessageCodec().encodeMessage(
<String, List<Object>>{'assets/logo.png': <Object>[]})!;
return Future<ByteData>.value(manifest);
} else if (key == 'assets/logo.png') {
// The root directory tests are run from is different for 'flutter test'
// verses 'flutter test test/*_test.dart'. Adjust the root directory
// to access the assets directory.
final io.Directory rootDirectory =
io.Directory.current.path.endsWith('${io.Platform.pathSeparator}test')
? io.Directory.current.parent
: io.Directory.current;
final io.File file =
io.File('${rootDirectory.path}/test/assets/images/logo.png');
final ByteData asset = ByteData.view(file.readAsBytesSync().buffer);
if (asset == null) {
throw FlutterError('Unable to load asset: $key');
}
return asset;
} else {
throw ArgumentError('Unknown asset key: $key');
}
}
}
/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;