Skip to content

Commit 1ba19b2

Browse files
committed
[Windows] Add engine builder to simplify tests (flutter#38546)
* [Windows] Add engine builder to simplify tests * Format
1 parent a810404 commit 1ba19b2

7 files changed

+151
-68
lines changed

shell/platform/windows/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ executable("flutter_windows_unittests") {
199199
"testing/engine_modifier.h",
200200
"testing/flutter_window_test.cc",
201201
"testing/flutter_window_test.h",
202+
"testing/flutter_windows_engine_builder.cc",
203+
"testing/flutter_windows_engine_builder.h",
202204
"testing/mock_direct_manipulation.h",
203205
"testing/mock_gl_functions.h",
204206
"testing/mock_text_input_manager.cc",

shell/platform/windows/flutter_windows_engine_unittests.cc

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
99
#include "flutter/shell/platform/windows/flutter_windows_view.h"
1010
#include "flutter/shell/platform/windows/testing/engine_modifier.h"
11+
#include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
1112
#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
1213
#include "flutter/shell/platform/windows/testing/test_keyboard.h"
1314
#include "flutter/shell/platform/windows/testing/windows_test.h"
@@ -21,34 +22,14 @@
2122
namespace flutter {
2223
namespace testing {
2324

24-
namespace {
25-
26-
// Returns an engine instance configured with dummy project path values.
27-
std::unique_ptr<FlutterWindowsEngine> GetTestEngine() {
28-
FlutterDesktopEngineProperties properties = {};
29-
properties.assets_path = L"C:\\foo\\flutter_assets";
30-
properties.icu_data_path = L"C:\\foo\\icudtl.dat";
31-
properties.aot_library_path = L"C:\\foo\\aot.so";
32-
33-
std::vector<const char*> test_arguments = {"arg1", "arg2"};
34-
properties.dart_entrypoint_argc = test_arguments.size();
35-
properties.dart_entrypoint_argv = test_arguments.data();
36-
37-
FlutterProjectBundle project(properties);
38-
auto engine = std::make_unique<FlutterWindowsEngine>(project);
39-
40-
EngineModifier modifier(engine.get());
41-
// Force the non-AOT path unless overridden by the test.
42-
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
43-
44-
return engine;
45-
}
46-
} // namespace
47-
4825
class FlutterWindowsEngineTest : public WindowsTest {};
4926

5027
TEST_F(FlutterWindowsEngineTest, RunDoesExpectedInitialization) {
51-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
28+
FlutterWindowsEngineBuilder builder{GetContext()};
29+
builder.AddDartEntrypointArgument("arg1");
30+
builder.AddDartEntrypointArgument("arg2");
31+
32+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
5233
EngineModifier modifier(engine.get());
5334

5435
// The engine should be run with expected configuration values.
@@ -67,8 +48,8 @@ TEST_F(FlutterWindowsEngineTest, RunDoesExpectedInitialization) {
6748
EXPECT_EQ(config->type, kOpenGL);
6849
EXPECT_EQ(user_data, engine_instance);
6950
// Spot-check arguments.
70-
EXPECT_STREQ(args->assets_path, "C:\\foo\\flutter_assets");
71-
EXPECT_STREQ(args->icu_data_path, "C:\\foo\\icudtl.dat");
51+
EXPECT_NE(args->assets_path, nullptr);
52+
EXPECT_NE(args->icu_data_path, nullptr);
7253
EXPECT_EQ(args->dart_entrypoint_argc, 2U);
7354
EXPECT_EQ(strcmp(args->dart_entrypoint_argv[0], "arg1"), 0);
7455
EXPECT_EQ(strcmp(args->dart_entrypoint_argv[1], "arg2"), 0);
@@ -159,7 +140,8 @@ TEST_F(FlutterWindowsEngineTest, RunDoesExpectedInitialization) {
159140
}
160141

161142
TEST_F(FlutterWindowsEngineTest, ConfiguresFrameVsync) {
162-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
143+
FlutterWindowsEngineBuilder builder{GetContext()};
144+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
163145
EngineModifier modifier(engine.get());
164146
bool on_vsync_called = false;
165147

@@ -185,7 +167,8 @@ TEST_F(FlutterWindowsEngineTest, ConfiguresFrameVsync) {
185167
}
186168

187169
TEST_F(FlutterWindowsEngineTest, RunWithoutANGLEUsesSoftware) {
188-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
170+
FlutterWindowsEngineBuilder builder{GetContext()};
171+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
189172
EngineModifier modifier(engine.get());
190173

191174
modifier.embedder_api().NotifyDisplayUpdate =
@@ -237,7 +220,8 @@ TEST_F(FlutterWindowsEngineTest, RunWithoutANGLEUsesSoftware) {
237220
}
238221

239222
TEST_F(FlutterWindowsEngineTest, SendPlatformMessageWithoutResponse) {
240-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
223+
FlutterWindowsEngineBuilder builder{GetContext()};
224+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
241225
EngineModifier modifier(engine.get());
242226

243227
const char* channel = "test";
@@ -263,14 +247,10 @@ TEST_F(FlutterWindowsEngineTest, SendPlatformMessageWithoutResponse) {
263247
}
264248

265249
TEST_F(FlutterWindowsEngineTest, PlatformMessageRoundTrip) {
266-
FlutterDesktopEngineProperties properties = {};
267-
properties.assets_path = GetContext().GetAssetsPath().c_str();
268-
properties.icu_data_path = GetContext().GetIcuDataPath().c_str();
269-
properties.dart_entrypoint = "hiPlatformChannels";
270-
271-
FlutterProjectBundle project(properties);
272-
auto engine = std::make_unique<FlutterWindowsEngine>(project);
250+
FlutterWindowsEngineBuilder builder{GetContext()};
251+
builder.SetDartEntrypoint("hiPlatformChannels");
273252

253+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
274254
EngineModifier modifier(engine.get());
275255
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
276256

@@ -312,13 +292,10 @@ TEST_F(FlutterWindowsEngineTest, PlatformMessageRoundTrip) {
312292
}
313293

314294
TEST_F(FlutterWindowsEngineTest, PlatformMessageRespondOnDifferentThread) {
315-
FlutterDesktopEngineProperties properties = {};
316-
properties.assets_path = GetContext().GetAssetsPath().c_str();
317-
properties.icu_data_path = GetContext().GetIcuDataPath().c_str();
318-
properties.dart_entrypoint = "hiPlatformChannels";
295+
FlutterWindowsEngineBuilder builder{GetContext()};
296+
builder.SetDartEntrypoint("hiPlatformChannels");
319297

320-
FlutterProjectBundle project(properties);
321-
auto engine = std::make_unique<FlutterWindowsEngine>(project);
298+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
322299

323300
EngineModifier modifier(engine.get());
324301
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
@@ -366,7 +343,8 @@ TEST_F(FlutterWindowsEngineTest, PlatformMessageRespondOnDifferentThread) {
366343
}
367344

368345
TEST_F(FlutterWindowsEngineTest, SendPlatformMessageWithResponse) {
369-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
346+
FlutterWindowsEngineBuilder builder{GetContext()};
347+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
370348
EngineModifier modifier(engine.get());
371349

372350
const char* channel = "test";
@@ -424,7 +402,8 @@ TEST_F(FlutterWindowsEngineTest, SendPlatformMessageWithResponse) {
424402
}
425403

426404
TEST_F(FlutterWindowsEngineTest, DispatchSemanticsAction) {
427-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
405+
FlutterWindowsEngineBuilder builder{GetContext()};
406+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
428407
EngineModifier modifier(engine.get());
429408

430409
bool called = false;
@@ -469,7 +448,8 @@ TEST_F(FlutterWindowsEngineTest, SetsThreadPriority) {
469448
}
470449

471450
TEST_F(FlutterWindowsEngineTest, AddPluginRegistrarDestructionCallback) {
472-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
451+
FlutterWindowsEngineBuilder builder{GetContext()};
452+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
473453
EngineModifier modifier(engine.get());
474454

475455
MockEmbedderApiForKeyboard(modifier,
@@ -499,7 +479,8 @@ TEST_F(FlutterWindowsEngineTest, AddPluginRegistrarDestructionCallback) {
499479
}
500480

501481
TEST_F(FlutterWindowsEngineTest, ScheduleFrame) {
502-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
482+
FlutterWindowsEngineBuilder builder{GetContext()};
483+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
503484
EngineModifier modifier(engine.get());
504485

505486
bool called = false;
@@ -514,7 +495,8 @@ TEST_F(FlutterWindowsEngineTest, ScheduleFrame) {
514495
}
515496

516497
TEST_F(FlutterWindowsEngineTest, SetNextFrameCallback) {
517-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
498+
FlutterWindowsEngineBuilder builder{GetContext()};
499+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
518500
EngineModifier modifier(engine.get());
519501

520502
bool called = false;
@@ -529,14 +511,16 @@ TEST_F(FlutterWindowsEngineTest, SetNextFrameCallback) {
529511
}
530512

531513
TEST_F(FlutterWindowsEngineTest, GetExecutableName) {
532-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
514+
FlutterWindowsEngineBuilder builder{GetContext()};
515+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
533516
EXPECT_EQ(engine->GetExecutableName(), "flutter_windows_unittests.exe");
534517
}
535518

536519
// Ensure that after setting or resetting the high contrast feature,
537520
// the corresponding status flag can be retrieved from the engine.
538521
TEST_F(FlutterWindowsEngineTest, UpdateHighContrastFeature) {
539-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
522+
FlutterWindowsEngineBuilder builder{GetContext()};
523+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
540524
EngineModifier modifier(engine.get());
541525

542526
bool called = false;
@@ -561,7 +545,8 @@ TEST_F(FlutterWindowsEngineTest, UpdateHighContrastFeature) {
561545
}
562546

563547
TEST_F(FlutterWindowsEngineTest, PostRasterThreadTask) {
564-
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
548+
FlutterWindowsEngineBuilder builder{GetContext()};
549+
std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
565550
EngineModifier modifier(engine.get());
566551

567552
modifier.embedder_api().PostRenderThreadTask = MOCK_ENGINE_PROC(
@@ -586,20 +571,16 @@ class MockFlutterWindowsView : public FlutterWindowsView {
586571
};
587572

588573
TEST_F(FlutterWindowsEngineTest, AlertPlatformMessage) {
589-
FlutterDesktopEngineProperties properties = {};
590-
properties.assets_path = GetContext().GetAssetsPath().c_str();
591-
properties.icu_data_path = GetContext().GetIcuDataPath().c_str();
592-
properties.dart_entrypoint = "alertPlatformChannel";
593-
594-
FlutterProjectBundle project(properties);
574+
FlutterWindowsEngineBuilder builder{GetContext()};
575+
builder.SetDartEntrypoint("alertPlatformChannel");
595576

596577
auto window_binding_handler =
597578
std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
598579
AccessibilityRootNode* root_node = AccessibilityRootNode::Create();
599580
ON_CALL(*window_binding_handler, GetAccessibilityRootNode)
600581
.WillByDefault(::testing::Return(root_node));
601582
MockFlutterWindowsView view(std::move(window_binding_handler));
602-
view.SetEngine(std::make_unique<FlutterWindowsEngine>(project));
583+
view.SetEngine(builder.Build());
603584
FlutterWindowsEngine* engine = view.GetEngine();
604585

605586
EngineModifier modifier(engine);

shell/platform/windows/platform_handler_unittests.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
#include "flutter/shell/platform/common/json_method_codec.h"
1010
#include "flutter/shell/platform/windows/flutter_windows_view.h"
11+
#include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
1112
#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
1213
#include "flutter/shell/platform/windows/testing/test_binary_messenger.h"
14+
#include "flutter/shell/platform/windows/testing/windows_test.h"
1315
#include "gmock/gmock.h"
1416
#include "gtest/gtest.h"
1517
#include "rapidjson/document.h"
@@ -95,26 +97,22 @@ std::string SimulatePlatformMessage(TestBinaryMessenger* messenger,
9597

9698
} // namespace
9799

98-
class PlatformHandlerTest : public ::testing::Test {
100+
class PlatformHandlerTest : public WindowsTest {
99101
protected:
100102
FlutterWindowsEngine* engine() { return engine_.get(); }
101103

102104
void use_headless_engine() {
103-
// Set properties required to create the engine.
104-
FlutterDesktopEngineProperties properties = {};
105-
properties.assets_path = L"C:\\foo\\flutter_assets";
106-
properties.icu_data_path = L"C:\\foo\\icudtl.dat";
107-
properties.aot_library_path = L"C:\\foo\\aot.so";
108-
FlutterProjectBundle project(properties);
109-
110-
engine_ = std::make_unique<FlutterWindowsEngine>(project);
105+
FlutterWindowsEngineBuilder builder{GetContext()};
106+
107+
engine_ = builder.Build();
111108
}
112109

113110
void use_engine_with_view() {
114-
use_headless_engine();
111+
FlutterWindowsEngineBuilder builder{GetContext()};
115112

116113
auto window = std::make_unique<NiceMock<MockWindowBindingHandler>>();
117114
view_ = std::make_unique<FlutterWindowsView>(std::move(window));
115+
engine_ = builder.Build();
118116

119117
engine_->SetView(view_.get());
120118
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
6+
7+
namespace flutter {
8+
namespace testing {
9+
10+
FlutterWindowsEngineBuilder::FlutterWindowsEngineBuilder(
11+
WindowsTestContext& context)
12+
: context_(context) {
13+
properties_.assets_path = context.GetAssetsPath().c_str();
14+
properties_.icu_data_path = context.GetIcuDataPath().c_str();
15+
properties_.aot_library_path = context.GetAotLibraryPath().c_str();
16+
}
17+
18+
FlutterWindowsEngineBuilder::~FlutterWindowsEngineBuilder() = default;
19+
20+
void FlutterWindowsEngineBuilder::SetDartEntrypoint(std::string entrypoint) {
21+
dart_entrypoint_ = std::move(entrypoint);
22+
properties_.dart_entrypoint = dart_entrypoint_.c_str();
23+
}
24+
25+
void FlutterWindowsEngineBuilder::AddDartEntrypointArgument(std::string arg) {
26+
dart_entrypoint_arguments_.emplace_back(std::move(arg));
27+
}
28+
29+
std::unique_ptr<FlutterWindowsEngine> FlutterWindowsEngineBuilder::Build() {
30+
std::vector<const char*> dart_args;
31+
dart_args.reserve(dart_entrypoint_arguments_.size());
32+
33+
for (const auto& arg : dart_entrypoint_arguments_) {
34+
dart_args.push_back(arg.c_str());
35+
}
36+
37+
if (!dart_args.empty()) {
38+
properties_.dart_entrypoint_argv = dart_args.data();
39+
properties_.dart_entrypoint_argc = dart_args.size();
40+
} else {
41+
properties_.dart_entrypoint_argv = nullptr;
42+
properties_.dart_entrypoint_argc = 0;
43+
}
44+
45+
FlutterProjectBundle project(properties_);
46+
47+
return std::make_unique<FlutterWindowsEngine>(project);
48+
}
49+
50+
} // namespace testing
51+
} // namespace flutter
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_FLUTTER_WINDOWS_ENGINE_BUILDER_H_
6+
#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_FLUTTER_WINDOWS_ENGINE_BUILDER_H_
7+
8+
#include <memory>
9+
10+
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
11+
#include "flutter/shell/platform/windows/public/flutter_windows.h"
12+
#include "flutter/shell/platform/windows/testing/windows_test_context.h"
13+
14+
namespace flutter {
15+
namespace testing {
16+
17+
class FlutterWindowsEngineBuilder {
18+
public:
19+
explicit FlutterWindowsEngineBuilder(WindowsTestContext& context);
20+
~FlutterWindowsEngineBuilder();
21+
22+
void SetDartEntrypoint(std::string entrypoint);
23+
24+
void AddDartEntrypointArgument(std::string arg);
25+
26+
std::unique_ptr<FlutterWindowsEngine> Build();
27+
28+
// Prevent copying.
29+
FlutterWindowsEngineBuilder(FlutterWindowsEngineBuilder const&) = delete;
30+
FlutterWindowsEngineBuilder& operator=(FlutterWindowsEngineBuilder const&) =
31+
delete;
32+
33+
private:
34+
WindowsTestContext& context_;
35+
FlutterDesktopEngineProperties properties_ = {};
36+
std::string dart_entrypoint_;
37+
std::vector<std::string> dart_entrypoint_arguments_;
38+
};
39+
40+
} // namespace testing
41+
} // namespace flutter
42+
43+
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_FLUTTER_WINDOWS_ENGINE_BUILDER_H_

shell/platform/windows/testing/windows_test_context.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const std::wstring& WindowsTestContext::GetIcuDataPath() const {
3131
return icu_data_path_;
3232
}
3333

34+
const std::wstring& WindowsTestContext::GetAotLibraryPath() const {
35+
return aot_library_path_;
36+
}
37+
3438
void WindowsTestContext::AddNativeFunction(std::string_view name,
3539
Dart_NativeFunction function) {
3640
native_resolver_->AddNativeCallback(std::string{name}, function);

0 commit comments

Comments
 (0)