fuchsia: Add FakeFlatland and tests (flutter/engine#28926)

This commit is contained in:
David Worsham
2021-09-30 20:27:30 -07:00
committed by GitHub
parent 21e40ce7df
commit bd89058626
5 changed files with 321 additions and 0 deletions

View File

@@ -470,6 +470,7 @@ executable("flutter_runner_unittests") {
"pointer_delegate_unittests.cc",
"runner_unittest.cc",
"tests/engine_unittests.cc",
"tests/fake_flatland_unittests.cc",
"tests/fake_session_unittests.cc",
"tests/flutter_runner_product_configuration_unittests.cc",
"tests/gfx_external_view_embedder_unittests.cc",

View File

@@ -0,0 +1,144 @@
// 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 "fakes/scenic/fake_flatland.h"
#include <lib/async-testing/test_loop.h>
#include <lib/async/dispatcher.h>
#include <string>
#include "flutter/fml/logging.h"
#include "fuchsia/ui/composition/cpp/fidl.h"
#include "gtest/gtest.h"
namespace flutter_runner::testing {
namespace {
std::string GetCurrentTestName() {
return ::testing::UnitTest::GetInstance()->current_test_info()->name();
}
} // namespace
class FakeFlatlandTest : public ::testing::Test {
protected:
FakeFlatlandTest() : flatland_subloop_(loop_.StartNewLoop()) {}
~FakeFlatlandTest() override = default;
async::TestLoop& loop() { return loop_; }
FakeFlatland& fake_flatland() { return fake_flatland_; }
fuchsia::ui::composition::FlatlandPtr ConnectFlatland() {
FML_CHECK(!fake_flatland_.is_bound());
auto flatland_handle =
fake_flatland_.Connect(flatland_subloop_->dispatcher());
return flatland_handle.Bind();
}
private:
// Primary loop and subloop for the FakeFlatland instance to process its
// messages. The subloop allocates it's own zx_port_t, allowing us to use a
// separate port for each end of the message channel, rather than sharing a
// single one. Dual ports allow messages and responses to be intermingled,
// which is how production code behaves; this improves test realism.
async::TestLoop loop_;
std::unique_ptr<async::LoopInterface> flatland_subloop_;
FakeFlatland fake_flatland_;
};
TEST_F(FakeFlatlandTest, Initialization) {
EXPECT_EQ(fake_flatland().debug_name(), "");
// Pump the loop one time; the flatland should retain its initial state.
loop().RunUntilIdle();
EXPECT_EQ(fake_flatland().debug_name(), "");
}
TEST_F(FakeFlatlandTest, DebugLabel) {
const std::string debug_label = GetCurrentTestName();
fuchsia::ui::composition::FlatlandPtr flatland = ConnectFlatland();
// Set the flatland's debug name. The `SetDebugName` hasn't been processed
// yet, so the flatland's view of the debug name is still empty.
flatland->SetDebugName(debug_label);
EXPECT_EQ(fake_flatland().debug_name(), "");
// Pump the loop; the contents of the initial `SetDebugName` should be
// processed.
loop().RunUntilIdle();
EXPECT_EQ(fake_flatland().debug_name(), debug_label);
}
TEST_F(FakeFlatlandTest, Present) {
fuchsia::ui::composition::FlatlandPtr flatland = ConnectFlatland();
// Fire an OnNextFrameBegin event and verify the test receives it.
constexpr uint64_t kOnNextFrameAdditionalPresentCredits = 10u;
uint64_t on_next_frame_additional_present_credits = 0u;
fuchsia::ui::composition::OnNextFrameBeginValues on_next_frame_begin_values;
on_next_frame_begin_values.set_additional_present_credits(
kOnNextFrameAdditionalPresentCredits);
flatland.events().OnNextFrameBegin =
[&on_next_frame_additional_present_credits](
auto on_next_frame_begin_values) {
static bool called_once = false;
EXPECT_FALSE(called_once);
on_next_frame_additional_present_credits =
on_next_frame_begin_values.additional_present_credits();
called_once = true;
};
fake_flatland().FireOnNextFrameBeginEvent(
std::move(on_next_frame_begin_values));
EXPECT_EQ(on_next_frame_additional_present_credits, 0u);
loop().RunUntilIdle();
EXPECT_EQ(on_next_frame_additional_present_credits,
kOnNextFrameAdditionalPresentCredits);
// Fire an OnFramePresented event and verify the test receives it.
constexpr uint64_t kOnFramePresentedNumPresentsAllowed = 20u;
uint64_t frame_presented_num_presents_allowed = 0u;
flatland.events().OnFramePresented =
[&frame_presented_num_presents_allowed](auto frame_presented_info) {
static bool called_once = false;
EXPECT_FALSE(called_once);
frame_presented_num_presents_allowed =
frame_presented_info.num_presents_allowed;
called_once = true;
};
fake_flatland().FireOnFramePresentedEvent(
fuchsia::scenic::scheduling::FramePresentedInfo{
.actual_presentation_time = 0,
.num_presents_allowed = kOnFramePresentedNumPresentsAllowed,
});
EXPECT_EQ(frame_presented_num_presents_allowed, 0u);
loop().RunUntilIdle();
EXPECT_EQ(frame_presented_num_presents_allowed,
kOnFramePresentedNumPresentsAllowed);
// Call Present and verify the fake handled it.
constexpr int64_t kPresentRequestedTime = 42;
int64_t present_requested_time = 0;
fuchsia::ui::composition::PresentArgs present_args;
present_args.set_requested_presentation_time(kPresentRequestedTime);
fake_flatland().SetPresentHandler(
[&present_requested_time](auto present_args) {
static bool called_once = false;
EXPECT_FALSE(called_once);
present_requested_time = present_args.requested_presentation_time();
called_once = true;
});
flatland->Present(std::move(present_args));
EXPECT_EQ(present_requested_time, 0);
loop().RunUntilIdle();
EXPECT_EQ(present_requested_time, kPresentRequestedTime);
}
} // namespace flutter_runner::testing

View File

@@ -8,6 +8,8 @@ source_set("scenic") {
testonly = true
sources = [
"fake_flatland.cc",
"fake_flatland.h",
"fake_resources.cc",
"fake_resources.h",
"fake_session.cc",

View File

@@ -0,0 +1,58 @@
// 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 "fake_flatland.h"
#include "flutter/fml/logging.h"
namespace flutter_runner::testing {
FakeFlatland::FakeFlatland()
: binding_(this), present_handler_([](auto args) {}) {}
fidl::InterfaceHandle<fuchsia::ui::composition::Flatland> FakeFlatland::Connect(
async_dispatcher_t* dispatcher) {
FML_CHECK(!binding_.is_bound());
fidl::InterfaceHandle<fuchsia::ui::composition::Flatland> flatland;
binding_.Bind(flatland.NewRequest(), dispatcher);
return flatland;
}
void FakeFlatland::Disconnect(fuchsia::ui::composition::FlatlandError error) {
binding_.events().OnError(std::move(error));
binding_.Unbind();
}
void FakeFlatland::SetPresentHandler(PresentHandler present_handler) {
present_handler_ =
present_handler ? std::move(present_handler) : [](auto args) {};
}
void FakeFlatland::FireOnNextFrameBeginEvent(
fuchsia::ui::composition::OnNextFrameBeginValues
on_next_frame_begin_values) {
binding_.events().OnNextFrameBegin(std::move(on_next_frame_begin_values));
}
void FakeFlatland::FireOnFramePresentedEvent(
fuchsia::scenic::scheduling::FramePresentedInfo frame_presented_info) {
binding_.events().OnFramePresented(std::move(frame_presented_info));
}
void FakeFlatland::NotImplemented_(const std::string& name) {
FML_LOG(FATAL) << "FakeFlatland does not implement " << name;
}
void FakeFlatland::Present(fuchsia::ui::composition::PresentArgs args) {
// TODO(fxb/85619): ApplyCommands()
present_handler_(std::move(args));
}
void FakeFlatland::SetDebugName(std::string debug_name) {
debug_name_ = std::move(debug_name);
}
} // namespace flutter_runner::testing

View File

@@ -0,0 +1,116 @@
// 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_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_H_
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_H_
#include <fuchsia/scenic/scheduling/cpp/fidl.h>
#include <fuchsia/ui/composition/cpp/fidl.h>
#include <fuchsia/ui/composition/cpp/fidl_test_base.h>
#include <lib/async/dispatcher.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fidl/cpp/interface_handle.h>
#include <string>
#include "flutter/fml/macros.h"
namespace flutter_runner::testing {
// A lightweight fake implementation of the scenic Flatland API. The fake has
// no side effects besides mutating its own internal state.
//
// The fake allows tests to do a few things that would be difficult using either
// a mock implementation or the real implementation:
// + It allows the user to hook `Present` invocations and respond with
// stubbed-out `FuturePresentationTimes`, but more crucially it mimics the
// real scenic behavior of only processing commands when a `Present` is
// invoked.
// TODO(fxb/85619): Implement the following bullet-points
// + It allows the user to inspect a snapshot of the scene graph at any moment
// in time, via the `SceneGraph()` accessor.
// + It stores the various flatland resources generated by commands into a
// std::unordered_map, and also correctly manages the resource lifetimes via
// reference counting. This allows a resource to stay alive if its parent
// still holds a reference to it, in the same way the real scenic
// implementation would.
// + The resources returned by `SceneGraph()` that the test uses for
// inspection are decoupled from the resources managed internally by the
// `FakeFlatland` itself -- they are a snapshot of the scene graph at that
// moment in time, with all snapshot state being cloned from the underlying
// scene graph state. This allows the `FakeFlatland` and test to naturally
// use `shared_ptr` for reference counting and mimic the real scenic behavior
// exactly, instead of an awkward index-based API.
//
// Error handling / flatland disconnection is still WIP. FakeFlatland will
// likely generate a CHECK in any place where the real scenic would disconnect
// the flatland or send a FlatlandError.
//
// Input is not handled.
//
// Rendering is not handled.
//
// Cross-flatland links between View and Viewport are not handled.
class FakeFlatland
: public fuchsia::ui::composition::testing::Flatland_TestBase {
public:
using PresentHandler =
std::function<void(fuchsia::ui::composition::PresentArgs)>;
FakeFlatland();
~FakeFlatland() override = default;
bool is_bound() const { return binding_.is_bound(); }
const std::string& debug_name() const { return debug_name_; }
// Bind this session's FIDL channels to the `dispatcher` and allow processing
// of incoming FIDL requests.
//
// This can only be called once.
fidl::InterfaceHandle<fuchsia::ui::composition::Flatland> Connect(
async_dispatcher_t* dispatcher = nullptr);
// Disconnect the session's FIDL channels with an error.
// TODO: Call this internally upon command error, instead of CHECK'ing.
void Disconnect(fuchsia::ui::composition::FlatlandError error);
// Set a handler for `Present`-related FIDL calls' return values.
void SetPresentHandler(PresentHandler present_handler);
// Fire an `OnNextFrameBegin` event. Call this first after a `Present` in
// order to give additional present tokens to the client and simulate scenic's
// normal event flow.
void FireOnNextFrameBeginEvent(
fuchsia::ui::composition::OnNextFrameBeginValues
on_next_frame_begin_values);
// Fire an `OnFramePresented` event. Call this second after a `Present` in
// order to inform the client of retured frames and simulate scenic's normal
// event flow.
void FireOnFramePresentedEvent(
fuchsia::scenic::scheduling::FramePresentedInfo frame_presented_info);
private:
// |fuchsia::ui::composition::testing::Flatland_TestBase|
void NotImplemented_(const std::string& name) override;
// |fuchsia::ui::composition::Flatland|
void Present(fuchsia::ui::composition::PresentArgs args) override;
// |fuchsia::ui::composition::Flatland|
void SetDebugName(std::string debug_name) override;
fidl::Binding<fuchsia::ui::composition::Flatland> binding_;
std::string debug_name_;
PresentHandler present_handler_;
FML_DISALLOW_COPY_AND_ASSIGN(FakeFlatland);
};
} // namespace flutter_runner::testing
#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_H_