Flutter Engine
The Flutter Engine
fake_flatland_unittests.cc
Go to the documentation of this file.
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
6
7#include <lib/async-testing/test_loop.h>
8#include <lib/async/dispatcher.h>
9
10#include <string>
11
12#include "flutter/fml/logging.h"
13#include "fuchsia/ui/composition/cpp/fidl.h"
14#include "gtest/gtest.h"
15
17namespace {
18
19std::string GetCurrentTestName() {
20 return ::testing::UnitTest::GetInstance()->current_test_info()->name();
21}
22
23} // namespace
24
25class FakeFlatlandTest : public ::testing::Test {
26 protected:
27 FakeFlatlandTest() : flatland_subloop_(loop_.StartNewLoop()) {}
28 ~FakeFlatlandTest() override = default;
29
30 async::TestLoop& loop() { return loop_; }
31
32 FakeFlatland& fake_flatland() { return fake_flatland_; }
33
34 fuchsia::ui::composition::FlatlandPtr ConnectFlatland() {
35 FML_CHECK(!fake_flatland_.is_flatland_connected());
36
37 auto flatland_handle =
38 fake_flatland_.ConnectFlatland(flatland_subloop_->dispatcher());
39 return flatland_handle.Bind();
40 }
41
42 private:
43 // Primary loop and subloop for the FakeFlatland instance to process its
44 // messages. The subloop allocates it's own zx_port_t, allowing us to use a
45 // separate port for each end of the message channel, rather than sharing a
46 // single one. Dual ports allow messages and responses to be intermingled,
47 // which is how production code behaves; this improves test realism.
48 async::TestLoop loop_;
49 std::unique_ptr<async::LoopInterface> flatland_subloop_;
50
51 FakeFlatland fake_flatland_;
52};
53
54TEST_F(FakeFlatlandTest, Initialization) {
55 EXPECT_EQ(fake_flatland().debug_name(), "");
56
57 // Pump the loop one time; the flatland should retain its initial state.
58 loop().RunUntilIdle();
59 EXPECT_EQ(fake_flatland().debug_name(), "");
60}
61
63 const std::string debug_label = GetCurrentTestName();
64 fuchsia::ui::composition::FlatlandPtr flatland = ConnectFlatland();
65
66 // Set the flatland's debug name. The `SetDebugName` hasn't been processed
67 // yet, so the flatland's view of the debug name is still empty.
68 flatland->SetDebugName(debug_label);
69 EXPECT_EQ(fake_flatland().debug_name(), "");
70
71 // Pump the loop; the contents of the initial `SetDebugName` should be
72 // processed.
73 loop().RunUntilIdle();
74 EXPECT_EQ(fake_flatland().debug_name(), debug_label);
75}
76
78 fuchsia::ui::composition::FlatlandPtr flatland = ConnectFlatland();
79
80 // Fire an OnNextFrameBegin event and verify the test receives it.
81 constexpr uint64_t kOnNextFrameAdditionalPresentCredits = 10u;
82 uint64_t on_next_frame_additional_present_credits = 0u;
83 fuchsia::ui::composition::OnNextFrameBeginValues on_next_frame_begin_values;
84 on_next_frame_begin_values.set_additional_present_credits(
85 kOnNextFrameAdditionalPresentCredits);
86 flatland.events().OnNextFrameBegin =
87 [&on_next_frame_additional_present_credits](
88 auto on_next_frame_begin_values) {
89 static bool called_once = false;
90 EXPECT_FALSE(called_once);
91
92 on_next_frame_additional_present_credits =
93 on_next_frame_begin_values.additional_present_credits();
94 called_once = true;
95 };
96 fake_flatland().FireOnNextFrameBeginEvent(
97 std::move(on_next_frame_begin_values));
98 EXPECT_EQ(on_next_frame_additional_present_credits, 0u);
99 loop().RunUntilIdle();
100 EXPECT_EQ(on_next_frame_additional_present_credits,
101 kOnNextFrameAdditionalPresentCredits);
102
103 // Fire an OnFramePresented event and verify the test receives it.
104 constexpr uint64_t kOnFramePresentedNumPresentsAllowed = 20u;
105 uint64_t frame_presented_num_presents_allowed = 0u;
106 flatland.events().OnFramePresented =
107 [&frame_presented_num_presents_allowed](auto frame_presented_info) {
108 static bool called_once = false;
109 EXPECT_FALSE(called_once);
110
111 frame_presented_num_presents_allowed =
112 frame_presented_info.num_presents_allowed;
113 called_once = true;
114 };
115 fake_flatland().FireOnFramePresentedEvent(
116 fuchsia::scenic::scheduling::FramePresentedInfo{
117 .actual_presentation_time = 0,
118 .num_presents_allowed = kOnFramePresentedNumPresentsAllowed,
119 });
120 EXPECT_EQ(frame_presented_num_presents_allowed, 0u);
121 loop().RunUntilIdle();
122 EXPECT_EQ(frame_presented_num_presents_allowed,
123 kOnFramePresentedNumPresentsAllowed);
124
125 // Call Present and verify the fake handled it.
126 constexpr int64_t kPresentRequestedTime = 42;
127 int64_t present_requested_time = 0;
128 fuchsia::ui::composition::PresentArgs present_args;
129 present_args.set_requested_presentation_time(kPresentRequestedTime);
130 fake_flatland().SetPresentHandler(
131 [&present_requested_time](auto present_args) {
132 static bool called_once = false;
133 EXPECT_FALSE(called_once);
134
135 present_requested_time = present_args.requested_presentation_time();
136 called_once = true;
137 });
138 flatland->Present(std::move(present_args));
139 EXPECT_EQ(present_requested_time, 0);
140 loop().RunUntilIdle();
141 EXPECT_EQ(present_requested_time, kPresentRequestedTime);
142}
143
144} // namespace flutter_runner::testing
fuchsia::ui::composition::FlatlandPtr ConnectFlatland()
fuchsia::ui::composition::FlatlandHandle ConnectFlatland(async_dispatcher_t *dispatcher=nullptr)
#define FML_CHECK(condition)
Definition: logging.h:85
std::string GetCurrentTestName()
Gets the name of the currently running test. This is useful in generating logs or assets based on tes...
Definition: testing.cc:15
TEST_F(FocusDelegateTest, WatchCallbackSeries)