Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate existing embedder unit tests to use the fixture. #8296

Merged
merged 1 commit into from Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions shell/platform/embedder/BUILD.gn
Expand Up @@ -70,6 +70,8 @@ executable("embedder_unittests") {
sources = [
"tests/embedder_config_builder.cc",
"tests/embedder_config_builder.h",
"tests/embedder_context.cc",
"tests/embedder_context.h",
"tests/embedder_test.cc",
"tests/embedder_test.h",
"tests/embedder_unittests.cc",
Expand All @@ -78,7 +80,10 @@ executable("embedder_unittests") {
deps = [
":embedder",
":fixtures",
"$flutter_root/runtime",
"$flutter_root/testing",
"//third_party/skia",
"//third_party/tonic",
]

if (is_linux) {
Expand Down
9 changes: 6 additions & 3 deletions shell/platform/embedder/embedder.cc
Expand Up @@ -52,14 +52,17 @@ extern const intptr_t kPlatformStrongDillSize;

static FlutterEngineResult LogEmbedderError(FlutterEngineResult code,
const char* name,
const char* function) {
const char* function,
const char* file,
int line) {
FML_LOG(ERROR) << "Returning error '" << name << "' (" << code
<< ") from Flutter Embedder API call to '" << __FUNCTION__
<< "'.";
<< "'. Origin: " << file << ":" << line;
return code;
}

#define LOG_EMBEDDER_ERROR(code) LogEmbedderError(code, #code, __FUNCTION__)
#define LOG_EMBEDDER_ERROR(code) \
LogEmbedderError(code, #code, __FUNCTION__, __FILE__, __LINE__)

static bool IsOpenGLRendererConfigValid(const FlutterRendererConfig* config) {
if (config->type != kOpenGL) {
Expand Down
39 changes: 25 additions & 14 deletions shell/platform/embedder/tests/embedder_config_builder.cc
Expand Up @@ -7,12 +7,21 @@
namespace shell {
namespace testing {

EmbedderConfigBuilder::EmbedderConfigBuilder() {
EmbedderConfigBuilder::EmbedderConfigBuilder(
EmbedderContext& context,
InitializationPreference preference)
: context_(context) {
project_args_.struct_size = sizeof(project_args_);

software_renderer_config_.struct_size = sizeof(FlutterSoftwareRendererConfig);
software_renderer_config_.surface_present_callback =
[](void*, const void*, size_t, size_t) { return true; };

if (preference == InitializationPreference::kInitialize) {
SetSoftwareRendererConfig();
SetAssetsPath();
SetSnapshots();
SetIsolateCreateCallbackHook();
}
}

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

void EmbedderConfigBuilder::SetAssetsPathFromFixture(
const EmbedderTest* fixture) {
assets_path_ = fixture->GetAssetsPath();
project_args_.assets_path = assets_path_.c_str();
void EmbedderConfigBuilder::SetAssetsPath() {
project_args_.assets_path = context_.GetAssetsPath().c_str();
}

void EmbedderConfigBuilder::SetSnapshotsFromFixture(
const EmbedderTest* fixture) {
if (auto mapping = fixture->GetVMSnapshotData()) {
void EmbedderConfigBuilder::SetSnapshots() {
if (auto mapping = context_.GetVMSnapshotData()) {
project_args_.vm_snapshot_data = mapping->GetMapping();
project_args_.vm_snapshot_data_size = mapping->GetSize();
}

if (auto mapping = fixture->GetVMSnapshotInstructions()) {
if (auto mapping = context_.GetVMSnapshotInstructions()) {
project_args_.vm_snapshot_instructions = mapping->GetMapping();
project_args_.vm_snapshot_instructions_size = mapping->GetSize();
}

if (auto mapping = fixture->GetIsolateSnapshotData()) {
if (auto mapping = context_.GetIsolateSnapshotData()) {
project_args_.isolate_snapshot_data = mapping->GetMapping();
project_args_.isolate_snapshot_data_size = mapping->GetSize();
}

if (auto mapping = fixture->GetIsolateSnapshotInstructions()) {
if (auto mapping = context_.GetIsolateSnapshotInstructions()) {
project_args_.isolate_snapshot_instructions = mapping->GetMapping();
project_args_.isolate_snapshot_instructions_size = mapping->GetSize();
}
}

UniqueEngine EmbedderConfigBuilder::LaunchEngine(void* user_data) const {
void EmbedderConfigBuilder::SetIsolateCreateCallbackHook() {
project_args_.root_isolate_create_callback =
EmbedderContext::GetIsolateCreateCallbackHook();
}

UniqueEngine EmbedderConfigBuilder::LaunchEngine() const {
FlutterEngine engine = nullptr;
auto result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &renderer_config_,
&project_args_, user_data, &engine);
&project_args_, &context_, &engine);

if (result != kSuccess) {
return {};
Expand Down
20 changes: 15 additions & 5 deletions shell/platform/embedder/tests/embedder_config_builder.h
Expand Up @@ -8,6 +8,7 @@
#include "flutter/fml/macros.h"
#include "flutter/fml/unique_object.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/embedder/tests/embedder_context.h"
#include "flutter/shell/platform/embedder/tests/embedder_test.h"

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

class EmbedderConfigBuilder {
public:
EmbedderConfigBuilder();
enum class InitializationPreference {
kInitialize,
kNoInitialize,
};

EmbedderConfigBuilder(EmbedderContext& context,
InitializationPreference preference =
InitializationPreference::kInitialize);

~EmbedderConfigBuilder();

void SetSoftwareRendererConfig();

void SetAssetsPathFromFixture(const EmbedderTest* fixture);
void SetAssetsPath();

void SetSnapshots();

void SetSnapshotsFromFixture(const EmbedderTest* fixture);
void SetIsolateCreateCallbackHook();

UniqueEngine LaunchEngine(void* user_data = nullptr) const;
UniqueEngine LaunchEngine() const;

private:
EmbedderContext& context_;
FlutterProjectArgs project_args_ = {};
FlutterRendererConfig renderer_config_ = {};
FlutterSoftwareRendererConfig software_renderer_config_ = {};
std::string assets_path_;

FML_DISALLOW_COPY_AND_ASSIGN(EmbedderConfigBuilder);
};
Expand Down
95 changes: 95 additions & 0 deletions shell/platform/embedder/tests/embedder_context.cc
@@ -0,0 +1,95 @@
// 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.

#include "flutter/shell/platform/embedder/tests/embedder_context.h"

#include "flutter/runtime/dart_vm.h"

namespace shell {
namespace testing {

static std::unique_ptr<fml::Mapping> 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<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
}

if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}

return mapping;
}

EmbedderContext::EmbedderContext(std::string assets_path)
: assets_path_(std::move(assets_path)) {
auto assets_dir = fml::OpenDirectory(assets_path_.c_str(), false,
fml::FilePermission::kRead);
vm_snapshot_data_ = GetMapping(assets_dir, "vm_snapshot_data", false);
isolate_snapshot_data_ =
GetMapping(assets_dir, "isolate_snapshot_data", false);

if (blink::DartVM::IsRunningPrecompiledCode()) {
vm_snapshot_instructions_ =
GetMapping(assets_dir, "vm_snapshot_instr", true);
isolate_snapshot_instructions_ =
GetMapping(assets_dir, "isolate_snapshot_instr", true);
}
}

EmbedderContext::~EmbedderContext() = default;

const std::string& EmbedderContext::GetAssetsPath() const {
return assets_path_;
}

const fml::Mapping* EmbedderContext::GetVMSnapshotData() const {
return vm_snapshot_data_.get();
}

const fml::Mapping* EmbedderContext::GetVMSnapshotInstructions() const {
return vm_snapshot_instructions_.get();
}

const fml::Mapping* EmbedderContext::GetIsolateSnapshotData() const {
return isolate_snapshot_data_.get();
}

const fml::Mapping* EmbedderContext::GetIsolateSnapshotInstructions() const {
return isolate_snapshot_instructions_.get();
}

void EmbedderContext::AddIsolateCreateCallback(fml::closure closure) {
if (closure) {
isolate_create_callbacks_.push_back(closure);
}
}

VoidCallback EmbedderContext::GetIsolateCreateCallbackHook() {
return [](void* user_data) {
reinterpret_cast<EmbedderContext*>(user_data)->FireIsolateCreateCallbacks();
};
}

void EmbedderContext::FireIsolateCreateCallbacks() {
for (auto closure : isolate_create_callbacks_) {
closure();
}
}

} // namespace testing
} // namespace shell
59 changes: 59 additions & 0 deletions shell/platform/embedder/tests/embedder_context.h
@@ -0,0 +1,59 @@
// 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_PLATFORM_EMBEDDER_TESTS_EMBEDDER_CONTEXT_H_
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_CONTEXT_H_

#include <memory>
#include <string>
#include <vector>

#include "flutter/fml/closure.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/mapping.h"
#include "flutter/shell/platform/embedder/embedder.h"

namespace shell {
namespace testing {

class EmbedderContext {
public:
EmbedderContext(std::string assets_path = "");

~EmbedderContext();

const std::string& GetAssetsPath() const;

const fml::Mapping* GetVMSnapshotData() const;

const fml::Mapping* GetVMSnapshotInstructions() const;

const fml::Mapping* GetIsolateSnapshotData() const;

const fml::Mapping* GetIsolateSnapshotInstructions() const;

void AddIsolateCreateCallback(fml::closure closure);

private:
// This allows the builder to access the hooks.
friend class EmbedderConfigBuilder;

std::string assets_path_;
std::unique_ptr<fml::Mapping> vm_snapshot_data_;
std::unique_ptr<fml::Mapping> vm_snapshot_instructions_;
std::unique_ptr<fml::Mapping> isolate_snapshot_data_;
std::unique_ptr<fml::Mapping> isolate_snapshot_instructions_;
std::vector<fml::closure> isolate_create_callbacks_;

static VoidCallback GetIsolateCreateCallbackHook();

void FireIsolateCreateCallbacks();

FML_DISALLOW_COPY_AND_ASSIGN(EmbedderContext);
};

} // namespace testing
} // namespace shell

#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_CONTEXT_H_