Skip to content

Commit 22ee8ee

Browse files
authored
Migrate existing embedder unit tests to use the fixture. (flutter#8296)
Also allows tests to specify their own embedder contexts.
1 parent 345ae7d commit 22ee8ee

File tree

9 files changed

+236
-186
lines changed

9 files changed

+236
-186
lines changed

shell/platform/embedder/BUILD.gn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ executable("embedder_unittests") {
7070
sources = [
7171
"tests/embedder_config_builder.cc",
7272
"tests/embedder_config_builder.h",
73+
"tests/embedder_context.cc",
74+
"tests/embedder_context.h",
7375
"tests/embedder_test.cc",
7476
"tests/embedder_test.h",
7577
"tests/embedder_unittests.cc",
@@ -78,7 +80,10 @@ executable("embedder_unittests") {
7880
deps = [
7981
":embedder",
8082
":fixtures",
83+
"$flutter_root/runtime",
8184
"$flutter_root/testing",
85+
"//third_party/skia",
86+
"//third_party/tonic",
8287
]
8388

8489
if (is_linux) {

shell/platform/embedder/embedder.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ extern const intptr_t kPlatformStrongDillSize;
5252

5353
static FlutterEngineResult LogEmbedderError(FlutterEngineResult code,
5454
const char* name,
55-
const char* function) {
55+
const char* function,
56+
const char* file,
57+
int line) {
5658
FML_LOG(ERROR) << "Returning error '" << name << "' (" << code
5759
<< ") from Flutter Embedder API call to '" << __FUNCTION__
58-
<< "'.";
60+
<< "'. Origin: " << file << ":" << line;
5961
return code;
6062
}
6163

62-
#define LOG_EMBEDDER_ERROR(code) LogEmbedderError(code, #code, __FUNCTION__)
64+
#define LOG_EMBEDDER_ERROR(code) \
65+
LogEmbedderError(code, #code, __FUNCTION__, __FILE__, __LINE__)
6366

6467
static bool IsOpenGLRendererConfigValid(const FlutterRendererConfig* config) {
6568
if (config->type != kOpenGL) {

shell/platform/embedder/tests/embedder_config_builder.cc

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
namespace shell {
88
namespace testing {
99

10-
EmbedderConfigBuilder::EmbedderConfigBuilder() {
10+
EmbedderConfigBuilder::EmbedderConfigBuilder(
11+
EmbedderContext& context,
12+
InitializationPreference preference)
13+
: context_(context) {
1114
project_args_.struct_size = sizeof(project_args_);
12-
1315
software_renderer_config_.struct_size = sizeof(FlutterSoftwareRendererConfig);
1416
software_renderer_config_.surface_present_callback =
1517
[](void*, const void*, size_t, size_t) { return true; };
18+
19+
if (preference == InitializationPreference::kInitialize) {
20+
SetSoftwareRendererConfig();
21+
SetAssetsPath();
22+
SetSnapshots();
23+
SetIsolateCreateCallbackHook();
24+
}
1625
}
1726

1827
EmbedderConfigBuilder::~EmbedderConfigBuilder() = default;
@@ -22,39 +31,41 @@ void EmbedderConfigBuilder::SetSoftwareRendererConfig() {
2231
renderer_config_.software = software_renderer_config_;
2332
}
2433

25-
void EmbedderConfigBuilder::SetAssetsPathFromFixture(
26-
const EmbedderTest* fixture) {
27-
assets_path_ = fixture->GetAssetsPath();
28-
project_args_.assets_path = assets_path_.c_str();
34+
void EmbedderConfigBuilder::SetAssetsPath() {
35+
project_args_.assets_path = context_.GetAssetsPath().c_str();
2936
}
3037

31-
void EmbedderConfigBuilder::SetSnapshotsFromFixture(
32-
const EmbedderTest* fixture) {
33-
if (auto mapping = fixture->GetVMSnapshotData()) {
38+
void EmbedderConfigBuilder::SetSnapshots() {
39+
if (auto mapping = context_.GetVMSnapshotData()) {
3440
project_args_.vm_snapshot_data = mapping->GetMapping();
3541
project_args_.vm_snapshot_data_size = mapping->GetSize();
3642
}
3743

38-
if (auto mapping = fixture->GetVMSnapshotInstructions()) {
44+
if (auto mapping = context_.GetVMSnapshotInstructions()) {
3945
project_args_.vm_snapshot_instructions = mapping->GetMapping();
4046
project_args_.vm_snapshot_instructions_size = mapping->GetSize();
4147
}
4248

43-
if (auto mapping = fixture->GetIsolateSnapshotData()) {
49+
if (auto mapping = context_.GetIsolateSnapshotData()) {
4450
project_args_.isolate_snapshot_data = mapping->GetMapping();
4551
project_args_.isolate_snapshot_data_size = mapping->GetSize();
4652
}
4753

48-
if (auto mapping = fixture->GetIsolateSnapshotInstructions()) {
54+
if (auto mapping = context_.GetIsolateSnapshotInstructions()) {
4955
project_args_.isolate_snapshot_instructions = mapping->GetMapping();
5056
project_args_.isolate_snapshot_instructions_size = mapping->GetSize();
5157
}
5258
}
5359

54-
UniqueEngine EmbedderConfigBuilder::LaunchEngine(void* user_data) const {
60+
void EmbedderConfigBuilder::SetIsolateCreateCallbackHook() {
61+
project_args_.root_isolate_create_callback =
62+
EmbedderContext::GetIsolateCreateCallbackHook();
63+
}
64+
65+
UniqueEngine EmbedderConfigBuilder::LaunchEngine() const {
5566
FlutterEngine engine = nullptr;
5667
auto result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &renderer_config_,
57-
&project_args_, user_data, &engine);
68+
&project_args_, &context_, &engine);
5869

5970
if (result != kSuccess) {
6071
return {};

shell/platform/embedder/tests/embedder_config_builder.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "flutter/fml/macros.h"
99
#include "flutter/fml/unique_object.h"
1010
#include "flutter/shell/platform/embedder/embedder.h"
11+
#include "flutter/shell/platform/embedder/tests/embedder_context.h"
1112
#include "flutter/shell/platform/embedder/tests/embedder_test.h"
1213

1314
namespace shell {
@@ -28,23 +29,32 @@ using UniqueEngine = fml::UniqueObject<FlutterEngine, UniqueEngineTraits>;
2829

2930
class EmbedderConfigBuilder {
3031
public:
31-
EmbedderConfigBuilder();
32+
enum class InitializationPreference {
33+
kInitialize,
34+
kNoInitialize,
35+
};
36+
37+
EmbedderConfigBuilder(EmbedderContext& context,
38+
InitializationPreference preference =
39+
InitializationPreference::kInitialize);
3240

3341
~EmbedderConfigBuilder();
3442

3543
void SetSoftwareRendererConfig();
3644

37-
void SetAssetsPathFromFixture(const EmbedderTest* fixture);
45+
void SetAssetsPath();
46+
47+
void SetSnapshots();
3848

39-
void SetSnapshotsFromFixture(const EmbedderTest* fixture);
49+
void SetIsolateCreateCallbackHook();
4050

41-
UniqueEngine LaunchEngine(void* user_data = nullptr) const;
51+
UniqueEngine LaunchEngine() const;
4252

4353
private:
54+
EmbedderContext& context_;
4455
FlutterProjectArgs project_args_ = {};
4556
FlutterRendererConfig renderer_config_ = {};
4657
FlutterSoftwareRendererConfig software_renderer_config_ = {};
47-
std::string assets_path_;
4858

4959
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderConfigBuilder);
5060
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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/embedder/tests/embedder_context.h"
6+
7+
#include "flutter/runtime/dart_vm.h"
8+
9+
namespace shell {
10+
namespace testing {
11+
12+
static std::unique_ptr<fml::Mapping> GetMapping(const fml::UniqueFD& directory,
13+
const char* path,
14+
bool executable) {
15+
fml::UniqueFD file = fml::OpenFile(directory, path, false /* create */,
16+
fml::FilePermission::kRead);
17+
if (!file.is_valid()) {
18+
return nullptr;
19+
}
20+
21+
using Prot = fml::FileMapping::Protection;
22+
std::unique_ptr<fml::FileMapping> mapping;
23+
if (executable) {
24+
mapping = std::make_unique<fml::FileMapping>(
25+
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
26+
} else {
27+
mapping = std::make_unique<fml::FileMapping>(
28+
file, std::initializer_list<Prot>{Prot::kRead});
29+
}
30+
31+
if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
32+
return nullptr;
33+
}
34+
35+
return mapping;
36+
}
37+
38+
EmbedderContext::EmbedderContext(std::string assets_path)
39+
: assets_path_(std::move(assets_path)) {
40+
auto assets_dir = fml::OpenDirectory(assets_path_.c_str(), false,
41+
fml::FilePermission::kRead);
42+
vm_snapshot_data_ = GetMapping(assets_dir, "vm_snapshot_data", false);
43+
isolate_snapshot_data_ =
44+
GetMapping(assets_dir, "isolate_snapshot_data", false);
45+
46+
if (blink::DartVM::IsRunningPrecompiledCode()) {
47+
vm_snapshot_instructions_ =
48+
GetMapping(assets_dir, "vm_snapshot_instr", true);
49+
isolate_snapshot_instructions_ =
50+
GetMapping(assets_dir, "isolate_snapshot_instr", true);
51+
}
52+
}
53+
54+
EmbedderContext::~EmbedderContext() = default;
55+
56+
const std::string& EmbedderContext::GetAssetsPath() const {
57+
return assets_path_;
58+
}
59+
60+
const fml::Mapping* EmbedderContext::GetVMSnapshotData() const {
61+
return vm_snapshot_data_.get();
62+
}
63+
64+
const fml::Mapping* EmbedderContext::GetVMSnapshotInstructions() const {
65+
return vm_snapshot_instructions_.get();
66+
}
67+
68+
const fml::Mapping* EmbedderContext::GetIsolateSnapshotData() const {
69+
return isolate_snapshot_data_.get();
70+
}
71+
72+
const fml::Mapping* EmbedderContext::GetIsolateSnapshotInstructions() const {
73+
return isolate_snapshot_instructions_.get();
74+
}
75+
76+
void EmbedderContext::AddIsolateCreateCallback(fml::closure closure) {
77+
if (closure) {
78+
isolate_create_callbacks_.push_back(closure);
79+
}
80+
}
81+
82+
VoidCallback EmbedderContext::GetIsolateCreateCallbackHook() {
83+
return [](void* user_data) {
84+
reinterpret_cast<EmbedderContext*>(user_data)->FireIsolateCreateCallbacks();
85+
};
86+
}
87+
88+
void EmbedderContext::FireIsolateCreateCallbacks() {
89+
for (auto closure : isolate_create_callbacks_) {
90+
closure();
91+
}
92+
}
93+
94+
} // namespace testing
95+
} // namespace shell
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_EMBEDDER_TESTS_EMBEDDER_CONTEXT_H_
6+
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_CONTEXT_H_
7+
8+
#include <memory>
9+
#include <string>
10+
#include <vector>
11+
12+
#include "flutter/fml/closure.h"
13+
#include "flutter/fml/macros.h"
14+
#include "flutter/fml/mapping.h"
15+
#include "flutter/shell/platform/embedder/embedder.h"
16+
17+
namespace shell {
18+
namespace testing {
19+
20+
class EmbedderContext {
21+
public:
22+
EmbedderContext(std::string assets_path = "");
23+
24+
~EmbedderContext();
25+
26+
const std::string& GetAssetsPath() const;
27+
28+
const fml::Mapping* GetVMSnapshotData() const;
29+
30+
const fml::Mapping* GetVMSnapshotInstructions() const;
31+
32+
const fml::Mapping* GetIsolateSnapshotData() const;
33+
34+
const fml::Mapping* GetIsolateSnapshotInstructions() const;
35+
36+
void AddIsolateCreateCallback(fml::closure closure);
37+
38+
private:
39+
// This allows the builder to access the hooks.
40+
friend class EmbedderConfigBuilder;
41+
42+
std::string assets_path_;
43+
std::unique_ptr<fml::Mapping> vm_snapshot_data_;
44+
std::unique_ptr<fml::Mapping> vm_snapshot_instructions_;
45+
std::unique_ptr<fml::Mapping> isolate_snapshot_data_;
46+
std::unique_ptr<fml::Mapping> isolate_snapshot_instructions_;
47+
std::vector<fml::closure> isolate_create_callbacks_;
48+
49+
static VoidCallback GetIsolateCreateCallbackHook();
50+
51+
void FireIsolateCreateCallbacks();
52+
53+
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderContext);
54+
};
55+
56+
} // namespace testing
57+
} // namespace shell
58+
59+
#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_CONTEXT_H_

0 commit comments

Comments
 (0)