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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// 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.
// This test exercises the embedding of the native assets mapping in dill files.
// An initial dill file is created by `flutter assemble` and used for running
// the application. This dill must contain the mapping.
// When doing hot reload, this mapping must stay in place.
// When doing a hot restart, a new dill file is pushed. This dill file must also
// contain the native assets mapping.
// When doing a hot reload, this mapping must stay in place.
@Timeout(Duration(minutes: 10))
library;
import 'dart:io';
import 'package:file/file.dart';
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/os.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import '../src/common.dart';
import 'test_utils.dart' show ProcessResultMatcher, fileSystem, platform;
import 'transition_test_utils.dart';
final String hostOs = platform.operatingSystem;
final List<String> devices = <String>[
'flutter-tester',
hostOs,
];
final List<String> buildSubcommands = <String>[
hostOs,
if (hostOs == 'macos') 'ios',
'apk',
];
final List<String> add2appBuildSubcommands = <String>[
if (hostOs == 'macos') ...<String>[
'macos-framework',
'ios-framework',
],
];
/// The build modes to target for each flutter command that supports passing
/// a build mode.
///
/// The flow of compiling kernel as well as bundling dylibs can differ based on
/// build mode, so we should cover this.
const List<String> buildModes = <String>[
'debug',
'profile',
'release',
];
const String packageName = 'package_with_native_assets';
const String exampleAppName = '${packageName}_example';
void main() {
if (!platform.isMacOS && !platform.isLinux && !platform.isWindows) {
// TODO(dacoharkes): Implement Fuchsia. https://github.com/flutter/flutter/issues/129757
return;
}
setUpAll(() {
processManager.runSync(<String>[
flutterBin,
'config',
'--enable-native-assets',
]);
});
for (final String device in devices) {
for (final String buildMode in buildModes) {
if (device == 'flutter-tester' && buildMode != 'debug') {
continue;
}
final String hotReload = buildMode == 'debug' ? ' hot reload and hot restart' : '';
testWithoutContext('flutter run$hotReload with native assets $device $buildMode', () async {
await inTempDir((Directory tempDirectory) async {
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
final Directory exampleDirectory = packageDirectory.childDirectory('example');
final ProcessTestResult result = await runFlutter(
<String>[
'run',
'-d$device',
'--$buildMode',
],
exampleDirectory.path,
<Transition>[
Multiple(<Pattern>[
'Flutter run key commands.',
], handler: (String line) {
if (buildMode == 'debug') {
// Do a hot reload diff on the initial dill file.
return 'r';
} else {
// No hot reload and hot restart in release mode.
return 'q';
}
}),
if (buildMode == 'debug') ...<Transition>[
Barrier(
'Performing hot reload...'.padRight(progressMessageWidth),
logging: true,
),
Multiple(<Pattern>[
RegExp('Reloaded .*'),
], handler: (String line) {
// Do a hot restart, pushing a new complete dill file.
return 'R';
}),
Barrier('Performing hot restart...'.padRight(progressMessageWidth)),
Multiple(<Pattern>[
RegExp('Restarted application .*'),
], handler: (String line) {
// Do another hot reload, pushing a diff to the second dill file.
return 'r';
}),
Barrier(
'Performing hot reload...'.padRight(progressMessageWidth),
logging: true,
),
Multiple(<Pattern>[
RegExp('Reloaded .*'),
], handler: (String line) {
return 'q';
}),
],
const Barrier('Application finished.'),
],
logging: false,
);
if (result.exitCode != 0) {
throw Exception('flutter run failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
}
final String stdout = result.stdout.join('\n');
// Check that we did not fail to resolve the native function in the
// dynamic library.
expect(stdout, isNot(contains("Invalid argument(s): Couldn't resolve native function 'sum'")));
// And also check that we did not have any other exceptions that might
// shadow the exception we would have gotten.
expect(stdout, isNot(contains('EXCEPTION CAUGHT BY WIDGETS LIBRARY')));
if (device == 'macos') {
expectDylibIsBundledMacOS(exampleDirectory, buildMode);
} else if (device == 'linux') {
expectDylibIsBundledLinux(exampleDirectory, buildMode);
} else if (device == 'windows') {
expectDylibIsBundledWindows(exampleDirectory, buildMode);
}
if (device == hostOs) {
expectCCompilerIsConfigured(exampleDirectory);
}
});
});
}
}
testWithoutContext('flutter test with native assets', () async {
await inTempDir((Directory tempDirectory) async {
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
final ProcessTestResult result = await runFlutter(
<String>[
'test',
],
packageDirectory.path,
<Transition>[
Barrier(RegExp('.* All tests passed!')),
],
logging: false,
);
if (result.exitCode != 0) {
throw Exception('flutter test failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
}
});
});
for (final String buildSubcommand in buildSubcommands) {
for (final String buildMode in buildModes) {
testWithoutContext('flutter build $buildSubcommand with native assets $buildMode', () async {
await inTempDir((Directory tempDirectory) async {
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
final Directory exampleDirectory = packageDirectory.childDirectory('example');
final ProcessResult result = processManager.runSync(
<String>[
flutterBin,
'build',
buildSubcommand,
'--$buildMode',
if (buildSubcommand == 'ios') '--no-codesign',
],
workingDirectory: exampleDirectory.path,
);
if (result.exitCode != 0) {
throw Exception('flutter build failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
}
if (buildSubcommand == 'macos') {
expectDylibIsBundledMacOS(exampleDirectory, buildMode);
} else if (buildSubcommand == 'ios') {
expectDylibIsBundledIos(exampleDirectory, buildMode);
} else if (buildSubcommand == 'linux') {
expectDylibIsBundledLinux(exampleDirectory, buildMode);
} else if (buildSubcommand == 'windows') {
expectDylibIsBundledWindows(exampleDirectory, buildMode);
} else if (buildSubcommand == 'apk') {
expectDylibIsBundledAndroid(exampleDirectory, buildMode);
}
expectCCompilerIsConfigured(exampleDirectory);
});
});
}
// This could be an hermetic unit test if the native_assets_builder
// could mock process runs and file system.
// https://github.com/dart-lang/native/issues/90.
testWithoutContext('flutter build $buildSubcommand error on static libraries', () async {
await inTempDir((Directory tempDirectory) async {
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
final File buildDotDart = packageDirectory.childFile('build.dart');
final String buildDotDartContents = await buildDotDart.readAsString();
// Overrides the build to output static libraries.
final String buildDotDartContentsNew = buildDotDartContents.replaceFirst(
'final buildConfig = await BuildConfig.fromArgs(args);',
r'''
final buildConfig = await BuildConfig.fromArgs([
'-D${LinkModePreference.configKey}=${LinkModePreference.static}',
...args,
]);
''',
);
expect(buildDotDartContentsNew, isNot(buildDotDartContents));
await buildDotDart.writeAsString(buildDotDartContentsNew);
final Directory exampleDirectory = packageDirectory.childDirectory('example');
final ProcessResult result = processManager.runSync(
<String>[
flutterBin,
'build',
buildSubcommand,
if (buildSubcommand == 'ios') '--no-codesign',
if (buildSubcommand == 'windows') '-v' // Requires verbose mode for error.
],
workingDirectory: exampleDirectory.path,
);
expect(
(result.stdout as String) + (result.stderr as String),
contains('link mode set to static, but this is not yet supported'),
);
expect(result.exitCode, isNot(0));
});
});
}
for (final String add2appBuildSubcommand in add2appBuildSubcommands) {
testWithoutContext('flutter build $add2appBuildSubcommand with native assets', () async {
await inTempDir((Directory tempDirectory) async {
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
final Directory exampleDirectory = packageDirectory.childDirectory('example');
final ProcessResult result = processManager.runSync(
<String>[
flutterBin,
'build',
add2appBuildSubcommand,
],
workingDirectory: exampleDirectory.path,
);
if (result.exitCode != 0) {
throw Exception('flutter build failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
}
for (final String buildMode in buildModes) {
expectDylibIsBundledWithFrameworks(exampleDirectory, buildMode, add2appBuildSubcommand.replaceAll('-framework', ''));
}
expectCCompilerIsConfigured(exampleDirectory);
});
});
}
}
/// For `flutter build` we can't easily test whether running the app works.
/// Check that we have the dylibs in the app.
void expectDylibIsBundledMacOS(Directory appDirectory, String buildMode) {
final Directory appBundle = appDirectory.childDirectory('build/$hostOs/Build/Products/${buildMode.upperCaseFirst()}/$exampleAppName.app');
expect(appBundle, exists);
final Directory dylibsFolder = appBundle.childDirectory('Contents/Frameworks');
expect(dylibsFolder, exists);
final File dylib = dylibsFolder.childFile(OS.macOS.dylibFileName(packageName));
expect(dylib, exists);
}
void expectDylibIsBundledIos(Directory appDirectory, String buildMode) {
final Directory appBundle = appDirectory.childDirectory('build/ios/${buildMode.upperCaseFirst()}-iphoneos/Runner.app');
expect(appBundle, exists);
final Directory dylibsFolder = appBundle.childDirectory('Frameworks');
expect(dylibsFolder, exists);
final File dylib = dylibsFolder.childFile(OS.iOS.dylibFileName(packageName));
expect(dylib, exists);
}
/// Checks that dylibs are bundled.
///
/// Sample path: build/linux/x64/release/bundle/lib/libmy_package.so
void expectDylibIsBundledLinux(Directory appDirectory, String buildMode) {
// Linux does not support cross compilation, so always only check current architecture.
final String architecture = Architecture.current.dartPlatform;
final Directory appBundle = appDirectory
.childDirectory('build')
.childDirectory(hostOs)
.childDirectory(architecture)
.childDirectory(buildMode)
.childDirectory('bundle');
expect(appBundle, exists);
final Directory dylibsFolder = appBundle.childDirectory('lib');
expect(dylibsFolder, exists);
final File dylib = dylibsFolder.childFile(OS.linux.dylibFileName(packageName));
expect(dylib, exists);
}
/// Checks that dylibs are bundled.
///
/// Sample path: build\windows\x64\runner\Debug\my_package_example.exe
void expectDylibIsBundledWindows(Directory appDirectory, String buildMode) {
// Linux does not support cross compilation, so always only check current architecture.
final String architecture = Architecture.current.dartPlatform;
final Directory appBundle = appDirectory
.childDirectory('build')
.childDirectory(hostOs)
.childDirectory(architecture)
.childDirectory('runner')
.childDirectory(buildMode.upperCaseFirst());
expect(appBundle, exists);
final File dylib = appBundle.childFile(OS.windows.dylibFileName(packageName));
expect(dylib, exists);
}
void expectDylibIsBundledAndroid(Directory appDirectory, String buildMode) {
final File apk = appDirectory
.childDirectory('build')
.childDirectory('app')
.childDirectory('outputs')
.childDirectory('flutter-apk')
.childFile('app-$buildMode.apk');
expect(apk, exists);
final OperatingSystemUtils osUtils = OperatingSystemUtils(
fileSystem: fileSystem,
logger: BufferLogger.test(),
platform: platform,
processManager: processManager,
);
final Directory apkUnzipped = appDirectory.childDirectory('apk-unzipped');
apkUnzipped.createSync();
osUtils.unzip(apk, apkUnzipped);
final Directory lib = apkUnzipped.childDirectory('lib');
for (final String arch in <String>['arm64-v8a', 'armeabi-v7a', 'x86_64']) {
final Directory archDir = lib.childDirectory(arch);
expect(archDir, exists);
// The dylibs should be next to the flutter and app so.
expect(archDir.childFile('libflutter.so'), exists);
if (buildMode != 'debug') {
expect(archDir.childFile('libapp.so'), exists);
}
final File dylib = archDir.childFile(OS.android.dylibFileName(packageName));
expect(dylib, exists);
}
}
/// For `flutter build` we can't easily test whether running the app works.
/// Check that we have the dylibs in the app.
void expectDylibIsBundledWithFrameworks(Directory appDirectory, String buildMode, String os) {
final Directory frameworksFolder = appDirectory.childDirectory('build/$os/framework/${buildMode.upperCaseFirst()}');
expect(frameworksFolder, exists);
final File dylib = frameworksFolder.childFile(OS.macOS.dylibFileName(packageName));
expect(dylib, exists);
}
/// Check that the native assets are built with the C Compiler that Flutter uses.
///
/// This inspects the build configuration to see if the C compiler was configured.
void expectCCompilerIsConfigured(Directory appDirectory) {
final Directory nativeAssetsBuilderDir = appDirectory.childDirectory('.dart_tool/native_assets_builder/');
for (final Directory subDir in nativeAssetsBuilderDir.listSync().whereType<Directory>()) {
final File config = subDir.childFile('config.yaml');
expect(config, exists);
final String contents = config.readAsStringSync();
// Dry run does not pass compiler info.
if (contents.contains('dry_run: true')) {
continue;
}
expect(contents, contains('cc: '));
}
}
extension on String {
String upperCaseFirst() {
return replaceFirst(this[0], this[0].toUpperCase());
}
}
Future<Directory> createTestProject(String packageName, Directory tempDirectory) async {
final ProcessResult result = processManager.runSync(
<String>[
flutterBin,
'create',
'--no-pub',
'--template=package_ffi',
packageName,
],
workingDirectory: tempDirectory.path,
);
if (result.exitCode != 0) {
throw Exception(
'flutter create failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}',
);
}
final Directory packageDirectory = tempDirectory.childDirectory(packageName);
// No platform-specific boilerplate files.
expect(packageDirectory.childDirectory('android/'), isNot(exists));
expect(packageDirectory.childDirectory('ios/'), isNot(exists));
expect(packageDirectory.childDirectory('linux/'), isNot(exists));
expect(packageDirectory.childDirectory('macos/'), isNot(exists));
expect(packageDirectory.childDirectory('windows/'), isNot(exists));
await pinDependencies(packageDirectory.childFile('pubspec.yaml'));
await pinDependencies(
packageDirectory.childDirectory('example').childFile('pubspec.yaml'));
final ProcessResult result2 = await processManager.run(
<String>[
flutterBin,
'pub',
'get',
],
workingDirectory: packageDirectory.path,
);
expect(result2, const ProcessResultMatcher());
return packageDirectory;
}
Future<void> pinDependencies(File pubspecFile) async {
expect(pubspecFile, exists);
final String oldPubspec = await pubspecFile.readAsString();
final String newPubspec = oldPubspec.replaceAll(RegExp(r':\s*\^'), ': ');
expect(newPubspec, isNot(oldPubspec));
await pubspecFile.writeAsString(newPubspec);
}
Future<void> inTempDir(Future<void> Function(Directory tempDirectory) fun) async {
final Directory tempDirectory = fileSystem.directory(fileSystem.systemTempDirectory.createTempSync().resolveSymbolicLinksSync());
try {
await fun(tempDirectory);
} finally {
tryToDelete(tempDirectory);
}
}