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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2014 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:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:path/path.dart' as path;
import 'package:flutter_devicelab/framework/adb.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/task_result.dart';
import 'package:flutter_devicelab/framework/utils.dart';
void generateMain(Directory appDir, String sentinel) {
final String mainCode = '''
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_driver/driver_extension.dart';
class ReassembleListener extends StatefulWidget {
const ReassembleListener({Key key, this.child})
: super(key: key);
final Widget child;
@override
_ReassembleListenerState createState() => _ReassembleListenerState();
}
class _ReassembleListenerState extends State<ReassembleListener> {
@override
initState() {
super.initState();
print('$sentinel');
}
@override
void reassemble() {
super.reassemble();
print('$sentinel');
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}
void main() {
runApp(
ReassembleListener(
child: Text(
'Hello, word!',
textDirection: TextDirection.rtl,
)
)
);
}
''';
File(path.join(appDir.path, 'lib', 'fuchsia_main.dart'))
.writeAsStringSync(mainCode, flush: true);
}
void main() {
deviceOperatingSystem = DeviceOperatingSystem.fuchsia;
task(() async {
section('Checking environment variables');
if (Platform.environment['FUCHSIA_SSH_CONFIG'] == null &&
Platform.environment['FUCHSIA_BUILD_DIR'] == null) {
throw Exception('No FUCHSIA_SSH_CONFIG or FUCHSIA_BUILD_DIR set');
}
final String flutterBinary = path.join(flutterDirectory.path, 'bin', 'flutter');
section('Downloading Fuchsia SDK and flutter runner');
// Download the Fuchsia SDK.
final int precacheResult = await exec(
flutterBinary,
<String>[
'precache',
'--fuchsia',
'--flutter_runner',
]
);
if (precacheResult != 0) {
throw Exception('flutter precache failed with exit code $precacheResult');
}
final Directory fuchsiaToolDirectory =
Directory(path.join(flutterDirectory.path, 'bin', 'cache', 'artifacts', 'fuchsia', 'tools'));
if (!fuchsiaToolDirectory.existsSync()) {
throw Exception('Expected Fuchsia tool directory at ${fuchsiaToolDirectory.path}');
}
final Device device = await devices.workingDevice;
final Directory appDir = dir(path.join(
flutterDirectory.path,
'dev',
'integration_tests',
'ui',
));
await inDirectory(appDir, () async {
final Random random = Random();
final Map<String, Completer<void>> sentinelMessage = <String, Completer<void>>{
'sentinel-${random.nextInt(1<<32)}': Completer<void>(),
'sentinel-${random.nextInt(1<<32)}': Completer<void>(),
};
Process runProcess;
Process logsProcess;
try {
section('Creating lib/fuchsia_main.dart');
generateMain(appDir, sentinelMessage.keys.toList()[0]);
section('Launching `flutter run` in ${appDir.path}');
runProcess = await startProcess(
flutterBinary,
<String>[
'run',
'--suppress-analytics',
'-d', device.deviceId,
'-t', 'lib/fuchsia_main.dart',
],
isBot: false, // We just want to test the output, not have any debugging info.
);
logsProcess = await startProcess(
flutterBinary,
<String>['logs', '--suppress-analytics', '-d', device.deviceId],
isBot: false, // We just want to test the output, not have any debugging info.
);
Future<dynamic> eventOrExit(Future<void> event) {
return Future.any<dynamic>(<Future<dynamic>>[
event,
runProcess.exitCode,
logsProcess.exitCode,
]);
}
logsProcess.stdout
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen((String log) {
print('logs:stdout: $log');
for (final String sentinel in sentinelMessage.keys) {
if (log.contains(sentinel)) {
if (sentinelMessage[sentinel].isCompleted) {
throw Exception(
'Expected a single `$sentinel` message in the device log, but found more than one'
);
}
sentinelMessage[sentinel].complete();
break;
}
}
});
final Completer<void> hotReloadCompleter = Completer<void>();
final Completer<void> reloadedCompleter = Completer<void>();
final RegExp observatoryRegexp = RegExp('An Observatory debugger and profiler on .+ is available at');
runProcess.stdout
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen((String line) {
print('run:stdout: $line');
if (observatoryRegexp.hasMatch(line)) {
hotReloadCompleter.complete();
} else if (line.contains('Reloaded')) {
reloadedCompleter.complete();
}
});
final List<String> runStderr = <String>[];
runProcess.stderr
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen((String line) {
runStderr.add(line);
print('run:stderr: $line');
});
section('Waiting for hot reload availability');
await eventOrExit(hotReloadCompleter.future);
section('Waiting for Dart VM');
// Wait for the first message in the log from the Dart VM.
await eventOrExit(sentinelMessage.values.toList()[0].future);
// Change the dart file.
generateMain(appDir, sentinelMessage.keys.toList()[1]);
section('Hot reload');
runProcess.stdin.write('r');
runProcess.stdin.flush();
await eventOrExit(reloadedCompleter.future);
section('Waiting for Dart VM');
// Wait for the second message in the log from the Dart VM.
await eventOrExit(sentinelMessage.values.toList()[1].future);
section('Quitting flutter run');
runProcess.stdin.write('q');
runProcess.stdin.flush();
final int runExitCode = await runProcess.exitCode;
if (runExitCode != 0 || runStderr.isNotEmpty) {
throw Exception(
'flutter run exited with code $runExitCode and errors: ${runStderr.join('\n')}.'
);
}
} finally {
runProcess.kill();
logsProcess.kill();
File(path.join(appDir.path, 'lib', 'fuchsia_main.dart')).deleteSync();
}
for (final String sentinel in sentinelMessage.keys) {
if (!sentinelMessage[sentinel].isCompleted) {
throw Exception('Expected $sentinel in the device logs.');
}
}
});
return TaskResult.success(null);
});
}