Delete unused downloads page asset.
[chromium-blink-merge.git] / mandoline / tab / frame_apptest.cc
blob929ea2dbcf2c42c584f73a3aa1da853a2e6f69a8
1 // Copyright 2015 The Chromium 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.
5 #include "mandoline/tab/frame.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/test/test_timeouts.h"
13 #include "components/view_manager/public/cpp/view_observer.h"
14 #include "components/view_manager/public/cpp/view_tree_connection.h"
15 #include "components/view_manager/public/cpp/view_tree_delegate.h"
16 #include "components/view_manager/public/cpp/view_tree_host_factory.h"
17 #include "mandoline/tab/frame.h"
18 #include "mandoline/tab/frame_tree.h"
19 #include "mandoline/tab/frame_tree_delegate.h"
20 #include "mandoline/tab/frame_user_data.h"
21 #include "mandoline/tab/test_frame_tree_delegate.h"
22 #include "mojo/application/public/cpp/application_connection.h"
23 #include "mojo/application/public/cpp/application_delegate.h"
24 #include "mojo/application/public/cpp/application_impl.h"
25 #include "mojo/application/public/cpp/application_test_base.h"
26 #include "mojo/application/public/cpp/service_provider_impl.h"
28 using mojo::View;
29 using mojo::ViewTreeConnection;
31 namespace mandoline {
33 namespace {
35 base::RunLoop* current_run_loop = nullptr;
37 void TimeoutRunLoop(const base::Closure& timeout_task, bool* timeout) {
38 CHECK(current_run_loop);
39 *timeout = true;
40 timeout_task.Run();
43 bool DoRunLoopWithTimeout() {
44 if (current_run_loop != nullptr)
45 return false;
47 bool timeout = false;
48 base::RunLoop run_loop;
49 base::MessageLoop::current()->PostDelayedTask(
50 FROM_HERE, base::Bind(&TimeoutRunLoop, run_loop.QuitClosure(), &timeout),
51 TestTimeouts::action_timeout());
53 current_run_loop = &run_loop;
54 current_run_loop->Run();
55 current_run_loop = nullptr;
56 return !timeout;
59 void QuitRunLoop() {
60 current_run_loop->Quit();
61 current_run_loop = nullptr;
64 } // namespace
66 class FrameTest : public mojo::test::ApplicationTestBase,
67 public mojo::ApplicationDelegate,
68 public mojo::ViewTreeDelegate,
69 public mojo::InterfaceFactory<mojo::ViewTreeClient> {
70 public:
71 FrameTest() : most_recent_connection_(nullptr), window_manager_(nullptr) {}
73 ViewTreeConnection* most_recent_connection() {
74 return most_recent_connection_;
77 // ApplicationDelegate implementation.
78 bool ConfigureIncomingConnection(
79 mojo::ApplicationConnection* connection) override {
80 connection->AddService<mojo::ViewTreeClient>(this);
81 return true;
84 ViewTreeConnection* window_manager() { return window_manager_; }
86 // ApplicationTestBase:
87 ApplicationDelegate* GetApplicationDelegate() override { return this; }
89 // Overridden from ViewTreeDelegate:
90 void OnEmbed(View* root) override {
91 most_recent_connection_ = root->connection();
92 QuitRunLoop();
94 void OnConnectionLost(ViewTreeConnection* connection) override {}
96 private:
97 // Overridden from testing::Test:
98 void SetUp() override {
99 ApplicationTestBase::SetUp();
101 mojo::CreateSingleViewTreeHost(application_impl(), this, &host_);
103 ASSERT_TRUE(DoRunLoopWithTimeout());
104 std::swap(window_manager_, most_recent_connection_);
107 // Overridden from testing::Test:
108 void TearDown() override {
109 ApplicationTestBase::TearDown();
112 // Overridden from mojo::InterfaceFactory<mojo::ViewTreeClient>:
113 void Create(
114 mojo::ApplicationConnection* connection,
115 mojo::InterfaceRequest<mojo::ViewTreeClient> request) override {
116 mojo::ViewTreeConnection::Create(this, request.Pass());
119 mojo::ViewTreeHostPtr host_;
121 // Used to receive the most recent view manager loaded by an embed action.
122 ViewTreeConnection* most_recent_connection_;
123 // The View Manager connection held by the window manager (app running at the
124 // root view).
125 ViewTreeConnection* window_manager_;
127 MOJO_DISALLOW_COPY_AND_ASSIGN(FrameTest);
130 class TestFrameTreeClient : public FrameTreeClient {
131 public:
132 TestFrameTreeClient() : connect_count_(0) {}
133 ~TestFrameTreeClient() override {}
135 int connect_count() const { return connect_count_; }
137 mojo::Array<FrameDataPtr> connect_frames() { return connect_frames_.Pass(); }
139 mojo::Array<FrameDataPtr> adds() { return adds_.Pass(); }
141 // TestFrameTreeClient:
142 void OnConnect(FrameTreeServerPtr server,
143 uint32_t change_id,
144 mojo::Array<FrameDataPtr> frames) override {
145 connect_count_++;
146 connect_frames_ = frames.Pass();
147 server_ = server.Pass();
149 void OnFrameAdded(uint32_t change_id, FrameDataPtr frame) override {
150 adds_.push_back(frame.Pass());
152 void OnFrameRemoved(uint32_t change_id, uint32_t frame_id) override {}
153 void OnFrameClientPropertyChanged(uint32_t frame_id,
154 const mojo::String& name,
155 mojo::Array<uint8_t> new_data) override {}
156 void OnPostMessageEvent(uint32_t source_frame_id,
157 uint32_t target_frame_id,
158 HTMLMessageEventPtr event) override {}
159 void OnWillNavigate(uint32_t frame_id,
160 const OnWillNavigateCallback& callback) override {
161 callback.Run();
164 private:
165 int connect_count_;
166 mojo::Array<FrameDataPtr> connect_frames_;
167 FrameTreeServerPtr server_;
168 mojo::Array<FrameDataPtr> adds_;
170 DISALLOW_COPY_AND_ASSIGN(TestFrameTreeClient);
173 // Verifies the root gets a connect.
174 TEST_F(FrameTest, RootGetsConnect) {
175 TestFrameTreeDelegate tree_delegate;
176 TestFrameTreeClient root_client;
177 FrameTree tree(window_manager()->GetRoot(), &tree_delegate, &root_client,
178 nullptr, Frame::ClientPropertyMap());
179 ASSERT_EQ(1, root_client.connect_count());
180 mojo::Array<FrameDataPtr> frames = root_client.connect_frames();
181 ASSERT_EQ(1u, frames.size());
182 EXPECT_EQ(tree.root()->view()->id(), frames[0]->frame_id);
183 EXPECT_EQ(0u, frames[0]->parent_id);
186 // Verifies adding a child to the root.
187 TEST_F(FrameTest, SingleChild) {
188 TestFrameTreeDelegate tree_delegate;
189 TestFrameTreeClient root_client;
190 FrameTree tree(window_manager()->GetRoot(), &tree_delegate, &root_client,
191 nullptr, Frame::ClientPropertyMap());
193 View* child = window_manager()->CreateView();
194 EXPECT_EQ(nullptr, Frame::FindFirstFrameAncestor(child));
195 window_manager()->GetRoot()->AddChild(child);
196 EXPECT_EQ(tree.root(), Frame::FindFirstFrameAncestor(child));
198 TestFrameTreeClient child_client;
199 Frame* child_frame =
200 tree.CreateAndAddFrame(child, tree.root(), &child_client, nullptr);
201 EXPECT_EQ(tree.root(), child_frame->parent());
203 ASSERT_EQ(1, child_client.connect_count());
204 mojo::Array<FrameDataPtr> frames_in_child = child_client.connect_frames();
205 // We expect 2 frames. One for the root, one for the child.
206 ASSERT_EQ(2u, frames_in_child.size());
207 EXPECT_EQ(tree.root()->view()->id(), frames_in_child[0]->frame_id);
208 EXPECT_EQ(0u, frames_in_child[0]->parent_id);
209 EXPECT_EQ(child_frame->view()->id(), frames_in_child[1]->frame_id);
210 EXPECT_EQ(tree.root()->view()->id(), frames_in_child[1]->parent_id);
212 // We should have gotten notification of the add.
213 EXPECT_EQ(1u, root_client.adds().size());
216 } // namespace mandoline