Split out embedder_unittests test cases into GL and non-GL tests (flutter/engine#21386)

This commit is contained in:
George Wright
2020-09-24 17:02:01 -07:00
committed by GitHub
parent b5ca962a5c
commit 235133f27f
5 changed files with 3434 additions and 3321 deletions

View File

@@ -156,6 +156,8 @@ if (current_toolchain == host_toolchain) {
"tests/embedder_test_context_software.cc",
"tests/embedder_test_context_software.h",
"tests/embedder_unittests.cc",
"tests/embedder_unittests_gl.cc",
"tests/embedder_unittests_util.cc",
]
deps = [

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,189 @@
// 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.
// FLUTTER_NOLINT
#define FML_USED_ON_EMBEDDER
#include "flutter/shell/platform/embedder/tests/embedder_unittests_util.h"
namespace flutter {
namespace testing {
sk_sp<SkSurface> CreateRenderSurface(const FlutterLayer& layer,
GrDirectContext* context) {
const auto image_info =
SkImageInfo::MakeN32Premul(layer.size.width, layer.size.height);
auto surface = context ? SkSurface::MakeRenderTarget(
context, // context
SkBudgeted::kNo, // budgeted
image_info, // image info
1, // sample count
kTopLeft_GrSurfaceOrigin, // surface origin
nullptr, // surface properties
false // mipmaps
)
: SkSurface::MakeRaster(image_info);
FML_CHECK(surface != nullptr);
return surface;
}
bool RasterImagesAreSame(sk_sp<SkImage> a, sk_sp<SkImage> b) {
FML_CHECK(!a->isTextureBacked());
FML_CHECK(!b->isTextureBacked());
if (!a || !b) {
return false;
}
SkPixmap pixmapA;
SkPixmap pixmapB;
if (!a->peekPixels(&pixmapA)) {
FML_LOG(ERROR) << "Could not peek pixels of image A.";
return false;
}
if (!b->peekPixels(&pixmapB)) {
FML_LOG(ERROR) << "Could not peek pixels of image B.";
return false;
}
const auto sizeA = pixmapA.rowBytes() * pixmapA.height();
const auto sizeB = pixmapB.rowBytes() * pixmapB.height();
if (sizeA != sizeB) {
FML_LOG(ERROR) << "Pixmap sizes were inconsistent.";
return false;
}
return ::memcmp(pixmapA.addr(), pixmapB.addr(), sizeA) == 0;
}
bool WriteImageToDisk(const fml::UniqueFD& directory,
const std::string& name,
sk_sp<SkImage> image) {
if (!image) {
return false;
}
auto data = image->encodeToData(SkEncodedImageFormat::kPNG, 100);
if (!data) {
return false;
}
fml::NonOwnedMapping mapping(static_cast<const uint8_t*>(data->data()),
data->size());
return WriteAtomically(directory, name.c_str(), mapping);
}
bool ImageMatchesFixture(const std::string& fixture_file_name,
sk_sp<SkImage> scene_image) {
fml::FileMapping fixture_image_mapping(OpenFixture(fixture_file_name));
FML_CHECK(fixture_image_mapping.GetSize() != 0u)
<< "Could not find fixture: " << fixture_file_name;
auto encoded_image = SkData::MakeWithoutCopy(
fixture_image_mapping.GetMapping(), fixture_image_mapping.GetSize());
auto fixture_image =
SkImage::MakeFromEncoded(std::move(encoded_image))->makeRasterImage();
FML_CHECK(fixture_image) << "Could not create image from fixture: "
<< fixture_file_name;
auto scene_image_subset = scene_image->makeSubset(
SkIRect::MakeWH(fixture_image->width(), fixture_image->height()));
FML_CHECK(scene_image_subset)
<< "Could not create image subset for fixture comparison: "
<< scene_image_subset;
const auto images_are_same =
RasterImagesAreSame(scene_image_subset, fixture_image);
// If the images are not the same, this predicate is going to indicate test
// failure. Dump both the actual image and the expectation to disk to the
// test author can figure out what went wrong.
if (!images_are_same) {
const auto fixtures_path = GetFixturesPath();
const auto actual_file_name = "actual_" + fixture_file_name;
const auto expect_file_name = "expectation_" + fixture_file_name;
auto fixtures_fd = OpenFixturesDirectory();
FML_CHECK(
WriteImageToDisk(fixtures_fd, actual_file_name, scene_image_subset))
<< "Could not write file to disk: " << actual_file_name;
FML_CHECK(WriteImageToDisk(fixtures_fd, expect_file_name, fixture_image))
<< "Could not write file to disk: " << expect_file_name;
FML_LOG(ERROR) << "Image did not match expectation." << std::endl
<< "Expected:"
<< fml::paths::JoinPaths({fixtures_path, expect_file_name})
<< std::endl
<< "Got:"
<< fml::paths::JoinPaths({fixtures_path, actual_file_name})
<< std::endl;
}
return images_are_same;
}
bool ImageMatchesFixture(const std::string& fixture_file_name,
std::future<sk_sp<SkImage>>& scene_image) {
return ImageMatchesFixture(fixture_file_name, scene_image.get());
}
void FilterMutationsByType(
const FlutterPlatformViewMutation** mutations,
size_t count,
FlutterPlatformViewMutationType type,
std::function<void(const FlutterPlatformViewMutation& mutation)> handler) {
if (mutations == nullptr) {
return;
}
for (size_t i = 0; i < count; ++i) {
const FlutterPlatformViewMutation* mutation = mutations[i];
if (mutation->type != type) {
continue;
}
handler(*mutation);
}
}
void FilterMutationsByType(
const FlutterPlatformView* view,
FlutterPlatformViewMutationType type,
std::function<void(const FlutterPlatformViewMutation& mutation)> handler) {
return FilterMutationsByType(view->mutations, view->mutations_count, type,
handler);
}
SkMatrix GetTotalMutationTransformationMatrix(
const FlutterPlatformViewMutation** mutations,
size_t count) {
SkMatrix collected;
FilterMutationsByType(
mutations, count, kFlutterPlatformViewMutationTypeTransformation,
[&](const auto& mutation) {
collected.preConcat(SkMatrixMake(mutation.transformation));
});
return collected;
}
SkMatrix GetTotalMutationTransformationMatrix(const FlutterPlatformView* view) {
return GetTotalMutationTransformationMatrix(view->mutations,
view->mutations_count);
}
} // namespace testing
} // namespace flutter

View File

@@ -0,0 +1,111 @@
// 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_UNITTESTS_UTIL_H_
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_UNITTESTS_UTIL_H_
#define FML_USED_ON_EMBEDDER
#include <future>
#include "embedder.h"
#include "flutter/fml/mapping.h"
#include "flutter/fml/message_loop.h"
#include "flutter/fml/paths.h"
#include "flutter/shell/platform/embedder/tests/embedder_assertions.h"
#include "flutter/shell/platform/embedder/tests/embedder_config_builder.h"
#include "flutter/shell/platform/embedder/tests/embedder_test.h"
#include "third_party/skia/include/core/SkSurface.h"
namespace flutter {
namespace testing {
sk_sp<SkSurface> CreateRenderSurface(const FlutterLayer& layer,
GrDirectContext* context);
bool RasterImagesAreSame(sk_sp<SkImage> a, sk_sp<SkImage> b);
bool WriteImageToDisk(const fml::UniqueFD& directory,
const std::string& name,
sk_sp<SkImage> image);
bool ImageMatchesFixture(const std::string& fixture_file_name,
sk_sp<SkImage> scene_image);
bool ImageMatchesFixture(const std::string& fixture_file_name,
std::future<sk_sp<SkImage>>& scene_image);
void FilterMutationsByType(
const FlutterPlatformViewMutation** mutations,
size_t count,
FlutterPlatformViewMutationType type,
std::function<void(const FlutterPlatformViewMutation& mutation)> handler);
void FilterMutationsByType(
const FlutterPlatformView* view,
FlutterPlatformViewMutationType type,
std::function<void(const FlutterPlatformViewMutation& mutation)> handler);
SkMatrix GetTotalMutationTransformationMatrix(
const FlutterPlatformViewMutation** mutations,
size_t count);
SkMatrix GetTotalMutationTransformationMatrix(const FlutterPlatformView* view);
//------------------------------------------------------------------------------
/// @brief A task runner that we expect the embedder to provide but whose
/// implementation is a real FML task runner.
///
class EmbedderTestTaskRunner {
public:
using TaskExpiryCallback = std::function<void(FlutterTask)>;
EmbedderTestTaskRunner(fml::RefPtr<fml::TaskRunner> real_task_runner,
TaskExpiryCallback on_task_expired)
: identifier_(++sEmbedderTaskRunnerIdentifiers),
real_task_runner_(real_task_runner),
on_task_expired_(on_task_expired) {
FML_CHECK(real_task_runner_);
FML_CHECK(on_task_expired_);
task_runner_description_.struct_size = sizeof(FlutterTaskRunnerDescription);
task_runner_description_.user_data = this;
task_runner_description_.runs_task_on_current_thread_callback =
[](void* user_data) -> bool {
return reinterpret_cast<EmbedderTestTaskRunner*>(user_data)
->real_task_runner_->RunsTasksOnCurrentThread();
};
task_runner_description_.post_task_callback = [](FlutterTask task,
uint64_t target_time_nanos,
void* user_data) -> void {
auto thiz = reinterpret_cast<EmbedderTestTaskRunner*>(user_data);
auto target_time = fml::TimePoint::FromEpochDelta(
fml::TimeDelta::FromNanoseconds(target_time_nanos));
auto on_task_expired = thiz->on_task_expired_;
auto invoke_task = [task, on_task_expired]() { on_task_expired(task); };
auto real_task_runner = thiz->real_task_runner_;
real_task_runner->PostTaskForTime(invoke_task, target_time);
};
task_runner_description_.identifier = identifier_;
}
const FlutterTaskRunnerDescription& GetFlutterTaskRunnerDescription() {
return task_runner_description_;
}
private:
static std::atomic_size_t sEmbedderTaskRunnerIdentifiers;
const size_t identifier_;
fml::RefPtr<fml::TaskRunner> real_task_runner_;
TaskExpiryCallback on_task_expired_;
FlutterTaskRunnerDescription task_runner_description_ = {};
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestTaskRunner);
};
} // namespace testing
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_UNITTESTS_UTIL_H_