From 7e10548a2e52168c00c98c513e0f63b6357c892b Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 8 Apr 2019 15:36:07 -0700 Subject: [PATCH] Wire up support for Dart fixtures in shell_unittests. (flutter/engine#8497) --- .../ci/licenses_golden/licenses_flutter | 3 + engine/src/flutter/shell/common/BUILD.gn | 15 +- .../flutter/shell/common/fixtures/main.dart | 8 ++ engine/src/flutter/shell/common/shell_test.cc | 133 ++++++++++++++++++ engine/src/flutter/shell/common/shell_test.h | 50 +++++++ .../flutter/shell/common/shell_unittests.cc | 101 ++++++++----- 6 files changed, 269 insertions(+), 41 deletions(-) create mode 100644 engine/src/flutter/shell/common/fixtures/main.dart create mode 100644 engine/src/flutter/shell/common/shell_test.cc create mode 100644 engine/src/flutter/shell/common/shell_test.h diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 720b26d990..e5c04c4678 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -410,6 +410,7 @@ FILE: ../../../flutter/shell/common/animator.cc FILE: ../../../flutter/shell/common/animator.h FILE: ../../../flutter/shell/common/engine.cc FILE: ../../../flutter/shell/common/engine.h +FILE: ../../../flutter/shell/common/fixtures/main.dart FILE: ../../../flutter/shell/common/io_manager.cc FILE: ../../../flutter/shell/common/io_manager.h FILE: ../../../flutter/shell/common/isolate_configuration.cc @@ -425,6 +426,8 @@ FILE: ../../../flutter/shell/common/run_configuration.h FILE: ../../../flutter/shell/common/shell.cc FILE: ../../../flutter/shell/common/shell.h FILE: ../../../flutter/shell/common/shell_benchmarks.cc +FILE: ../../../flutter/shell/common/shell_test.cc +FILE: ../../../flutter/shell/common/shell_test.h FILE: ../../../flutter/shell/common/shell_unittests.cc FILE: ../../../flutter/shell/common/skia_event_tracer_impl.cc FILE: ../../../flutter/shell/common/skia_event_tracer_impl.h diff --git a/engine/src/flutter/shell/common/BUILD.gn b/engine/src/flutter/shell/common/BUILD.gn index c678388a10..c8366f7bed 100644 --- a/engine/src/flutter/shell/common/BUILD.gn +++ b/engine/src/flutter/shell/common/BUILD.gn @@ -128,7 +128,8 @@ template("shell_host_executable") { ":common", "$flutter_root/fml", "$flutter_root/lib/snapshot", - "//third_party/dart/runtime:libdart_jit", + "$flutter_root/runtime", + "$flutter_root/runtime:libdart", "//third_party/skia", "//third_party/tonic", ] @@ -145,15 +146,23 @@ shell_gpu_configuration("shell_unittests_gpu_configuration") { enable_gl = false } +test_fixtures("shell_unittests_fixtures") { + fixtures = [ "fixtures/main.dart" ] +} + shell_host_executable("shell_unittests") { sources = [ + "shell_test.cc", + "shell_test.h", "shell_unittests.cc", ] deps = [ + ":shell_unittests_fixtures", ":shell_unittests_gpu_configuration", - "$flutter_root/shell/", - "$flutter_root/testing", + "$flutter_root/common", + "$flutter_root/shell", + "$flutter_root/testing:dart", ] } diff --git a/engine/src/flutter/shell/common/fixtures/main.dart b/engine/src/flutter/shell/common/fixtures/main.dart new file mode 100644 index 0000000000..c896712d2d --- /dev/null +++ b/engine/src/flutter/shell/common/fixtures/main.dart @@ -0,0 +1,8 @@ +main() {} + +@pragma('vm:entry-point') +fixturesAreFunctionalMain() { + SayHiFromFixturesAreFunctionalMain(); +} + +void SayHiFromFixturesAreFunctionalMain() native "SayHiFromFixturesAreFunctionalMain"; \ No newline at end of file diff --git a/engine/src/flutter/shell/common/shell_test.cc b/engine/src/flutter/shell/common/shell_test.cc new file mode 100644 index 0000000000..c8aab60046 --- /dev/null +++ b/engine/src/flutter/shell/common/shell_test.cc @@ -0,0 +1,133 @@ +// 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. + +#define FML_USED_ON_EMBEDDER + +#include "flutter/shell/common/shell_test.h" + +#include "flutter/fml/mapping.h" +#include "flutter/runtime/dart_vm.h" +#include "flutter/testing/testing.h" + +namespace shell { +namespace testing { + +ShellTest::ShellTest() + : native_resolver_(std::make_shared<::testing::TestDartNativeResolver>()) {} + +ShellTest::~ShellTest() = default; + +static std::unique_ptr GetMapping(const fml::UniqueFD& directory, + const char* path, + bool executable) { + fml::UniqueFD file = fml::OpenFile(directory, path, false /* create */, + fml::FilePermission::kRead); + if (!file.is_valid()) { + return nullptr; + } + + using Prot = fml::FileMapping::Protection; + std::unique_ptr mapping; + if (executable) { + mapping = std::make_unique( + file, std::initializer_list{Prot::kRead, Prot::kExecute}); + } else { + mapping = std::make_unique( + file, std::initializer_list{Prot::kRead}); + } + + if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) { + return nullptr; + } + + return mapping; +} + +void ShellTest::SetSnapshotsAndAssets(blink::Settings& settings) { + if (!assets_dir_.is_valid()) { + return; + } + + settings.assets_dir = assets_dir_.get(); + + // In JIT execution, all snapshots are present within the binary itself and + // don't need to be explicitly suppiled by the embedder. + if (blink::DartVM::IsRunningPrecompiledCode()) { + settings.vm_snapshot_data = [this]() { + return GetMapping(assets_dir_, "vm_snapshot_data", false); + }; + + settings.isolate_snapshot_data = [this]() { + return GetMapping(assets_dir_, "isolate_snapshot_data", false); + }; + + if (blink::DartVM::IsRunningPrecompiledCode()) { + settings.vm_snapshot_instr = [this]() { + return GetMapping(assets_dir_, "vm_snapshot_instr", true); + }; + + settings.isolate_snapshot_instr = [this]() { + return GetMapping(assets_dir_, "isolate_snapshot_instr", true); + }; + } + } else { + settings.application_kernels = [this]() { + std::vector> kernel_mappings; + kernel_mappings.emplace_back( + GetMapping(assets_dir_, "kernel_blob.bin", false)); + return kernel_mappings; + }; + } +} + +blink::Settings ShellTest::CreateSettingsForFixture() { + blink::Settings settings; + settings.task_observer_add = [](intptr_t key, fml::closure handler) { + fml::MessageLoop::GetCurrent().AddTaskObserver(key, handler); + }; + settings.task_observer_remove = [](intptr_t key) { + fml::MessageLoop::GetCurrent().RemoveTaskObserver(key); + }; + settings.root_isolate_create_callback = [this]() { + native_resolver_->SetNativeResolverForIsolate(); + }; + SetSnapshotsAndAssets(settings); + return settings; +} + +blink::TaskRunners ShellTest::GetTaskRunnersForFixture() { + return { + "test", + thread_host_->platform_thread->GetTaskRunner(), // platform + thread_host_->gpu_thread->GetTaskRunner(), // gpu + thread_host_->ui_thread->GetTaskRunner(), // ui + thread_host_->io_thread->GetTaskRunner() // io + }; +} + +// |testing::ThreadTest| +void ShellTest::SetUp() { + ThreadTest::SetUp(); + assets_dir_ = fml::OpenDirectory(::testing::GetFixturesPath(), false, + fml::FilePermission::kRead); + thread_host_ = std::make_unique( + "io.flutter.test." + ::testing::GetCurrentTestName() + ".", + ThreadHost::Type::Platform | ThreadHost::Type::IO | ThreadHost::Type::UI | + ThreadHost::Type::GPU); +} + +// |testing::ThreadTest| +void ShellTest::TearDown() { + ThreadTest::TearDown(); + assets_dir_.reset(); + thread_host_.reset(); +} + +void ShellTest::AddNativeCallback(std::string name, + Dart_NativeFunction callback) { + native_resolver_->AddNativeCallback(std::move(name), callback); +} + +} // namespace testing +} // namespace shell diff --git a/engine/src/flutter/shell/common/shell_test.h b/engine/src/flutter/shell/common/shell_test.h new file mode 100644 index 0000000000..4dcd9f8dbf --- /dev/null +++ b/engine/src/flutter/shell/common/shell_test.h @@ -0,0 +1,50 @@ +// 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. + +#ifndef FLUTTER_SHELL_COMMON_SHELL_TEST_H_ +#define FLUTTER_SHELL_COMMON_SHELL_TEST_H_ + +#include + +#include "flutter/common/settings.h" +#include "flutter/fml/macros.h" +#include "flutter/shell/common/run_configuration.h" +#include "flutter/shell/common/thread_host.h" +#include "flutter/testing/test_dart_native_resolver.h" +#include "flutter/testing/thread_test.h" + +namespace shell { +namespace testing { + +class ShellTest : public ::testing::ThreadTest { + public: + ShellTest(); + + ~ShellTest(); + + blink::Settings CreateSettingsForFixture(); + + blink::TaskRunners GetTaskRunnersForFixture(); + + void AddNativeCallback(std::string name, Dart_NativeFunction callback); + + protected: + // |testing::ThreadTest| + void SetUp() override; + + // |testing::ThreadTest| + void TearDown() override; + + private: + fml::UniqueFD assets_dir_; + std::shared_ptr<::testing::TestDartNativeResolver> native_resolver_; + std::unique_ptr thread_host_; + + void SetSnapshotsAndAssets(blink::Settings& settings); +}; + +} // namespace testing +} // namespace shell + +#endif // FLUTTER_SHELL_COMMON_SHELL_TEST_H_ diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 85c6b7962a..793df24a41 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -8,21 +8,20 @@ #include #include +#include "flutter/fml/make_copyable.h" #include "flutter/fml/message_loop.h" #include "flutter/fml/synchronization/waitable_event.h" #include "flutter/shell/common/platform_view.h" #include "flutter/shell/common/rasterizer.h" #include "flutter/shell/common/shell.h" +#include "flutter/shell/common/shell_test.h" #include "flutter/shell/common/thread_host.h" #include "flutter/shell/gpu/gpu_surface_software.h" +#include "flutter/testing/testing.h" #include "gtest/gtest.h" -#define CURRENT_TEST_NAME \ - std::string { \ - ::testing::UnitTest::GetInstance()->current_test_info()->name() \ - } - namespace shell { +namespace testing { class TestPlatformView : public PlatformView, public GPUSurfaceSoftwareDelegate { @@ -84,10 +83,8 @@ static bool ValidateShell(Shell* shell) { return true; } -TEST(ShellTest, InitializeWithInvalidThreads) { - blink::Settings settings = {}; - settings.task_observer_add = [](intptr_t, fml::closure) {}; - settings.task_observer_remove = [](intptr_t) {}; +TEST_F(ShellTest, InitializeWithInvalidThreads) { + blink::Settings settings = CreateSettingsForFixture(); blink::TaskRunners task_runners("test", nullptr, nullptr, nullptr, nullptr); auto shell = Shell::Create( std::move(task_runners), settings, @@ -101,13 +98,12 @@ TEST(ShellTest, InitializeWithInvalidThreads) { ASSERT_FALSE(shell); } -TEST(ShellTest, InitializeWithDifferentThreads) { - blink::Settings settings = {}; - settings.task_observer_add = [](intptr_t, fml::closure) {}; - settings.task_observer_remove = [](intptr_t) {}; - ThreadHost thread_host("io.flutter.test." + CURRENT_TEST_NAME + ".", - ThreadHost::Type::Platform | ThreadHost::Type::GPU | - ThreadHost::Type::IO | ThreadHost::Type::UI); +TEST_F(ShellTest, InitializeWithDifferentThreads) { + blink::Settings settings = CreateSettingsForFixture(); + ThreadHost thread_host( + "io.flutter.test." + ::testing::GetCurrentTestName() + ".", + ThreadHost::Type::Platform | ThreadHost::Type::GPU | + ThreadHost::Type::IO | ThreadHost::Type::UI); blink::TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(), thread_host.gpu_thread->GetTaskRunner(), @@ -125,12 +121,11 @@ TEST(ShellTest, InitializeWithDifferentThreads) { ASSERT_TRUE(ValidateShell(shell.get())); } -TEST(ShellTest, InitializeWithSingleThread) { - blink::Settings settings = {}; - settings.task_observer_add = [](intptr_t, fml::closure) {}; - settings.task_observer_remove = [](intptr_t) {}; - ThreadHost thread_host("io.flutter.test." + CURRENT_TEST_NAME + ".", - ThreadHost::Type::Platform); +TEST_F(ShellTest, InitializeWithSingleThread) { + blink::Settings settings = CreateSettingsForFixture(); + ThreadHost thread_host( + "io.flutter.test." + ::testing::GetCurrentTestName() + ".", + ThreadHost::Type::Platform); auto task_runner = thread_host.platform_thread->GetTaskRunner(); blink::TaskRunners task_runners("test", task_runner, task_runner, task_runner, task_runner); @@ -146,10 +141,8 @@ TEST(ShellTest, InitializeWithSingleThread) { ASSERT_TRUE(ValidateShell(shell.get())); } -TEST(ShellTest, InitializeWithSingleThreadWhichIsTheCallingThread) { - blink::Settings settings = {}; - settings.task_observer_add = [](intptr_t, fml::closure) {}; - settings.task_observer_remove = [](intptr_t) {}; +TEST_F(ShellTest, InitializeWithSingleThreadWhichIsTheCallingThread) { + blink::Settings settings = CreateSettingsForFixture(); fml::MessageLoop::EnsureInitializedForCurrentThread(); auto task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner(); blink::TaskRunners task_runners("test", task_runner, task_runner, task_runner, @@ -166,12 +159,11 @@ TEST(ShellTest, InitializeWithSingleThreadWhichIsTheCallingThread) { ASSERT_TRUE(ValidateShell(shell.get())); } -TEST(ShellTest, InitializeWithMultipleThreadButCallingThreadAsPlatformThread) { - blink::Settings settings = {}; - settings.task_observer_add = [](intptr_t, fml::closure) {}; - settings.task_observer_remove = [](intptr_t) {}; +TEST_F(ShellTest, + InitializeWithMultipleThreadButCallingThreadAsPlatformThread) { + blink::Settings settings = CreateSettingsForFixture(); ThreadHost thread_host( - "io.flutter.test." + CURRENT_TEST_NAME + ".", + "io.flutter.test." + ::testing::GetCurrentTestName() + ".", ThreadHost::Type::GPU | ThreadHost::Type::IO | ThreadHost::Type::UI); fml::MessageLoop::EnsureInitializedForCurrentThread(); blink::TaskRunners task_runners( @@ -191,14 +183,10 @@ TEST(ShellTest, InitializeWithMultipleThreadButCallingThreadAsPlatformThread) { ASSERT_TRUE(ValidateShell(shell.get())); } -// Reported in Bug: Engine deadlocks when gpu and platforms threads are the same -// #21398 (https://github.com/flutter/flutter/issues/21398) -TEST(ShellTest, InitializeWithGPUAndPlatformThreadsTheSame) { - blink::Settings settings = {}; - settings.task_observer_add = [](intptr_t, fml::closure) {}; - settings.task_observer_remove = [](intptr_t) {}; +TEST_F(ShellTest, InitializeWithGPUAndPlatformThreadsTheSame) { + blink::Settings settings = CreateSettingsForFixture(); ThreadHost thread_host( - "io.flutter.test." + CURRENT_TEST_NAME + ".", + "io.flutter.test." + ::testing::GetCurrentTestName() + ".", ThreadHost::Type::Platform | ThreadHost::Type::IO | ThreadHost::Type::UI); blink::TaskRunners task_runners( "test", @@ -219,4 +207,41 @@ TEST(ShellTest, InitializeWithGPUAndPlatformThreadsTheSame) { ASSERT_TRUE(ValidateShell(shell.get())); } +TEST_F(ShellTest, FixturesAreFunctional) { + const auto settings = CreateSettingsForFixture(); + auto shell = Shell::Create( + GetTaskRunnersForFixture(), settings, + [](Shell& shell) { + return std::make_unique(shell, + shell.GetTaskRunners()); + }, + [](Shell& shell) { + return std::make_unique(shell.GetTaskRunners()); + }); + ASSERT_TRUE(ValidateShell(shell.get())); + + auto configuration = RunConfiguration::InferFromSettings(settings); + ASSERT_TRUE(configuration.IsValid()); + configuration.SetEntrypoint("fixturesAreFunctionalMain"); + + fml::AutoResetWaitableEvent main_latch; + AddNativeCallback( + "SayHiFromFixturesAreFunctionalMain", + CREATE_NATIVE_ENTRY([&main_latch](auto args) { main_latch.Signal(); })); + + fml::AutoResetWaitableEvent latch; + fml::TaskRunner::RunNowOrPostTask( + shell->GetTaskRunners().GetUITaskRunner(), + fml::MakeCopyable([&latch, config = std::move(configuration), + engine = shell->GetEngine()]() mutable { + ASSERT_TRUE(engine); + ASSERT_EQ(engine->Run(std::move(config)), Engine::RunStatus::Success); + latch.Signal(); + })); + + latch.Wait(); + main_latch.Wait(); +} + +} // namespace testing } // namespace shell