From b73e827969798917a7eb0b1e2ec240f35c3b7e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sharma?= <737941+loic-sharma@users.noreply.github.com> Date: Fri, 23 Feb 2024 15:23:29 -0800 Subject: [PATCH] [Windows] Add helper for headless integration tests (flutter/engine#50885) We'll add more headless mode tests as part of https://github.com/flutter/flutter/issues/143765. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../windows/flutter_windows_unittests.cc | 4 +-- .../testing/windows_test_config_builder.cc | 27 ++++++++++++++++--- .../testing/windows_test_config_builder.h | 12 +++++++-- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc index e012254d97..a35bfae0f6 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc @@ -116,10 +116,8 @@ TEST_F(WindowsTest, LaunchCustomEntrypointInEngineRunInvocation) { TEST_F(WindowsTest, LaunchHeadlessEngine) { auto& context = GetContext(); WindowsConfigBuilder builder(context); - EnginePtr engine{builder.InitializeEngine()}; + EnginePtr engine{builder.RunHeadless()}; ASSERT_NE(engine, nullptr); - - ASSERT_TRUE(FlutterDesktopEngineRun(engine.get(), nullptr)); } // Verify that accessibility features are initialized when a view is created. diff --git a/engine/src/flutter/shell/platform/windows/testing/windows_test_config_builder.cc b/engine/src/flutter/shell/platform/windows/testing/windows_test_config_builder.cc index 35376e241f..f39b4db643 100644 --- a/engine/src/flutter/shell/platform/windows/testing/windows_test_config_builder.cc +++ b/engine/src/flutter/shell/platform/windows/testing/windows_test_config_builder.cc @@ -68,7 +68,28 @@ FlutterDesktopEngineProperties WindowsConfigBuilder::GetEngineProperties() EnginePtr WindowsConfigBuilder::InitializeEngine() const { FlutterDesktopEngineProperties engine_properties = GetEngineProperties(); - return EnginePtr(FlutterDesktopEngineCreate(&engine_properties)); + return EnginePtr{FlutterDesktopEngineCreate(&engine_properties)}; +} + +EnginePtr WindowsConfigBuilder::RunHeadless() const { + InitializeCOM(); + + EnginePtr engine = InitializeEngine(); + if (!engine) { + return {}; + } + + // Register native functions. + FlutterWindowsEngine* windows_engine = + reinterpret_cast(engine.get()); + windows_engine->SetRootIsolateCreateCallback( + context_.GetRootIsolateCallback()); + + if (!FlutterDesktopEngineRun(engine.get(), /* entry_point */ nullptr)) { + return {}; + } + + return engine; } ViewControllerPtr WindowsConfigBuilder::Run() const { @@ -87,8 +108,8 @@ ViewControllerPtr WindowsConfigBuilder::Run() const { int width = 600; int height = 400; - ViewControllerPtr controller( - FlutterDesktopViewControllerCreate(width, height, engine.release())); + ViewControllerPtr controller{ + FlutterDesktopViewControllerCreate(width, height, engine.release())}; if (!controller) { return {}; } diff --git a/engine/src/flutter/shell/platform/windows/testing/windows_test_config_builder.h b/engine/src/flutter/shell/platform/windows/testing/windows_test_config_builder.h index 7ec9a3352b..f634c86f53 100644 --- a/engine/src/flutter/shell/platform/windows/testing/windows_test_config_builder.h +++ b/engine/src/flutter/shell/platform/windows/testing/windows_test_config_builder.h @@ -64,8 +64,16 @@ class WindowsConfigBuilder { // Returns a configured and initialized engine. EnginePtr InitializeEngine() const; - // Returns a configured and initialized view controller running the default - // Dart entrypoint. + // Returns a configured and initialized engine that runs the configured Dart + // entrypoint. + // + // Returns null on failure. + EnginePtr RunHeadless() const; + + // Returns a configured and initialized view controller that runs the + // configured Dart entrypoint. + // + // Returns null on failure. ViewControllerPtr Run() const; private: