From 4e4fb4608da95d198b0e796478462285ab974a3c Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Sat, 15 Dec 2018 13:59:58 -0800 Subject: [PATCH] Compile embedder unit test Dart to kernel (flutter/engine#7227) Compile embedder unit test Dart to kernel As of the migration to Dart 2, it has been necessary to compile Dart to kernel prior to execution. The embedder currently requires that the resulting kernel file be named `kernel_blob.bin` and be located at the root of the assets directory passed to the embedder API. This patch updates the test_fixtures build rule to perform a kernel compile using frontend_server, outputting `kernel_blob.bin` to `fixtures/test_target_name` directory, and updates the embedder unittests to specify the kernel file rather than the Dart source file. Since the kernel compiler requires a `main()` function to be defined, it also updates `simple_main.dart` from runtime_unittests to define `main()` rather than `simple_main()`. This also updates all existing sub-targets to be testonly. --- .../flutter/runtime/fixtures/simple_main.dart | 2 +- .../embedder/fixtures/simple_main.dart | 2 +- .../embedder/tests/embedder_unittests.cc | 7 +-- engine/src/flutter/testing/testing.gni | 44 ++++++++++++++----- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/engine/src/flutter/runtime/fixtures/simple_main.dart b/engine/src/flutter/runtime/fixtures/simple_main.dart index dc744d2777..55187d8642 100644 --- a/engine/src/flutter/runtime/fixtures/simple_main.dart +++ b/engine/src/flutter/runtime/fixtures/simple_main.dart @@ -2,6 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -void simple_main() { +void main() { print("Hello"); } diff --git a/engine/src/flutter/shell/platform/embedder/fixtures/simple_main.dart b/engine/src/flutter/shell/platform/embedder/fixtures/simple_main.dart index 05a37e9834..ab73b3a234 100644 --- a/engine/src/flutter/shell/platform/embedder/fixtures/simple_main.dart +++ b/engine/src/flutter/shell/platform/embedder/fixtures/simple_main.dart @@ -1 +1 @@ -void main() {} \ No newline at end of file +void main() {} diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc index 6cd3f4d330..266cf92e55 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc @@ -22,17 +22,14 @@ TEST(EmbedderTest, CanLaunchAndShutdownWithValidProjectArgs) { return false; }; - std::string main = - std::string(testing::GetFixturesPath()) + "/simple_main.dart"; - FlutterRendererConfig config = {}; config.type = FlutterRendererType::kSoftware; config.software = renderer; FlutterProjectArgs args = {}; args.struct_size = sizeof(FlutterProjectArgs); - args.assets_path = ""; - args.main_path = main.c_str(); + args.assets_path = testing::GetFixturesPath(); + args.main_path = ""; args.packages_path = ""; FlutterEngine engine = nullptr; diff --git a/engine/src/flutter/testing/testing.gni b/engine/src/flutter/testing/testing.gni index ad5badfe92..b0b49f02d6 100644 --- a/engine/src/flutter/testing/testing.gni +++ b/engine/src/flutter/testing/testing.gni @@ -2,18 +2,21 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//third_party/dart/build/dart/dart_action.gni") + +# Builds test fixtures for a unit test. +# +# Generates a directory structure containing an assets directory and the Dart +# code to execute compiled to kernel, emitted to assets/kernel_blob.bin. template("test_fixtures") { testonly = true assert(defined(invoker.fixtures), "Test fixtures must be specified.") + fixtures_location = "$target_gen_dir/$target_name/assets" + fixtures_location_file = "$target_gen_dir/$target_name/test_fixtures_location.cc" + fixtures_name_target_name = target_name + "_gen_fixtures_name" - fixtures_source_set_target_name = target_name + "_gen_fixtures_source_set" - fixtures_copy_target_name = target_name + "_copy_fixtures" - - fixtures_location = "$target_gen_dir/fixtures" - fixtures_location_file = "$target_gen_dir/test_fixtures_location.cc" - action(fixtures_name_target_name) { script = "$flutter_root/testing/build/gen_fixtures_location_symbol.py" @@ -29,7 +32,9 @@ template("test_fixtures") { ] } + fixtures_source_set_target_name = target_name + "_gen_fixtures_source_set" source_set(fixtures_source_set_target_name) { + testonly = true sources = [ fixtures_location_file, ] @@ -39,16 +44,33 @@ template("test_fixtures") { ] } - copy(fixtures_copy_target_name) { - sources = invoker.fixtures - outputs = [ - "$fixtures_location/{{source_file_part}}", + fixtures_kernel_target_name = target_name + "_kernel" + prebuilt_dart_action(fixtures_kernel_target_name) { + testonly = true + script = "$root_out_dir/frontend_server.dart.snapshot" + + fixture_paths = [] + foreach(fixture, invoker.fixtures) { + fixture_paths += [ rebase_path(fixture) ] + } + inputs = fixture_paths + outputs = ["$fixtures_location/kernel_blob.bin"] + + args = [ + "--sdk-root", rebase_path("$root_out_dir/flutter_patched_sdk"), + "--strong", + "--target", "flutter", + "--output-dill", rebase_path("$fixtures_location/kernel_blob.bin"), + ] + fixture_paths + deps = [ + "//third_party/dart/utils/kernel-service:frontend_server" ] } group(target_name) { + testonly = true deps = [ - ":$fixtures_copy_target_name", + ":$fixtures_kernel_target_name", ":$fixtures_source_set_target_name", ] }