Unverified Commit 11660159 authored by chunhtai's avatar chunhtai Committed by GitHub

Enable widget load assets in its own package in test (#35991)

parent a8c4da22
// Copyright 2019 The Chromium 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 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Rendering Error', (WidgetTester tester) async {
// Assets can load with its package name.
await tester.pumpWidget(
Image.asset('icon/test.png',
width: 54,
height: 54,
fit: BoxFit.none,
package: 'flutter_automated_tests'
),
);
});
}
This diff was suppressed by a .gitattributes entry.
......@@ -64,5 +64,6 @@ dependencies:
flutter:
uses-material-design: true
assets:
- icon/
# PUBSPEC CHECKSUM: 4625
......@@ -811,15 +811,23 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
final String assetFolderPath = Platform.environment['UNIT_TEST_ASSETS'];
_ensureInitialized(assetFolderPath);
final String prefix = 'packages/${Platform.environment['APP_NAME']}/';
if (_allowedAssetKeys.isNotEmpty) {
defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData message) {
final String key = utf8.decode(message.buffer.asUint8List());
if (_allowedAssetKeys.contains(key)) {
final File asset = File(path.join(assetFolderPath, key));
final Uint8List encoded = Uint8List.fromList(asset.readAsBytesSync());
return Future<ByteData>.value(encoded.buffer.asByteData());
String key = utf8.decode(message.buffer.asUint8List());
if (!_allowedAssetKeys.contains(key)) {
// For tests in package, it will load assets with its own package prefix.
// In this case, we do a best-effort look up.
if (!key.startsWith(prefix))
return null;
key = key.replaceFirst(prefix, '');
if (!_allowedAssetKeys.contains(key))
return null;
}
return null;
final File asset = File(path.join(assetFolderPath, key));
final Uint8List encoded = Uint8List.fromList(asset.readAsBytesSync());
return Future<ByteData>.value(encoded.buffer.asByteData());
});
}
}
......
......@@ -866,6 +866,7 @@ class FlutterPlatform extends PlatformPlugin {
'FLUTTER_TEST': flutterTest,
'FONTCONFIG_FILE': _fontConfigFile.path,
'SERVER_PORT': serverPort.toString(),
'APP_NAME': flutterProject?.manifest?.appName ?? '',
};
if (buildTestAssets) {
environment['UNIT_TEST_ASSETS'] = fs.path.join(
......
......@@ -65,6 +65,11 @@ void main() {
return _testFile('print_user_created_ancestor_no_flag', automatedTestsDirectory, flutterTestDirectory);
}, skip: io.Platform.isWindows); // TODO(chunhtai): Dart on Windows has trouble with unicode characters in output (#35425).
testUsingContext('can load assets within its own package', () async {
Cache.flutterRoot = '../..';
return _testFile('package_assets', automatedTestsDirectory, flutterTestDirectory, exitCode: isZero);
}, skip: io.Platform.isWindows);
testUsingContext('run a test when its name matches a regexp', () async {
Cache.flutterRoot = '../..';
final ProcessResult result = await _runFlutterTest('filtering', automatedTestsDirectory, flutterTestDirectory,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment