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 "components/web_view/frame.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 "components/web_view/frame.h"
18 #include "components/web_view/frame_tree.h"
19 #include "components/web_view/frame_tree_delegate.h"
20 #include "components/web_view/frame_user_data.h"
21 #include "components/web_view/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"
29 using mojo::ViewTreeConnection
;
35 base::RunLoop
* current_run_loop
= nullptr;
37 void TimeoutRunLoop(const base::Closure
& timeout_task
, bool* timeout
) {
38 CHECK(current_run_loop
);
43 bool DoRunLoopWithTimeout() {
44 if (current_run_loop
!= nullptr)
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;
60 current_run_loop
->Quit();
61 current_run_loop
= nullptr;
66 class FrameTest
: public mojo::test::ApplicationTestBase
,
67 public mojo::ApplicationDelegate
,
68 public mojo::ViewTreeDelegate
,
69 public mojo::InterfaceFactory
<mojo::ViewTreeClient
> {
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);
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();
94 void OnConnectionLost(ViewTreeConnection
* connection
) override
{}
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>:
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
125 ViewTreeConnection
* window_manager_
;
127 MOJO_DISALLOW_COPY_AND_ASSIGN(FrameTest
);
130 class TestFrameTreeClient
: public FrameTreeClient
{
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
,
145 ViewConnectType view_connect_type
,
146 mojo::Array
<FrameDataPtr
> frames
,
147 const OnConnectCallback
& callback
) override
{
149 connect_frames_
= frames
.Pass();
150 server_
= server
.Pass();
153 void OnFrameAdded(uint32_t change_id
, FrameDataPtr frame
) override
{
154 adds_
.push_back(frame
.Pass());
156 void OnFrameRemoved(uint32_t change_id
, uint32_t frame_id
) override
{}
157 void OnFrameClientPropertyChanged(uint32_t frame_id
,
158 const mojo::String
& name
,
159 mojo::Array
<uint8_t> new_data
) override
{}
160 void OnPostMessageEvent(uint32_t source_frame_id
,
161 uint32_t target_frame_id
,
162 HTMLMessageEventPtr event
) override
{}
163 void OnWillNavigate(uint32_t frame_id
) override
{}
167 mojo::Array
<FrameDataPtr
> connect_frames_
;
168 FrameTreeServerPtr server_
;
169 mojo::Array
<FrameDataPtr
> adds_
;
171 DISALLOW_COPY_AND_ASSIGN(TestFrameTreeClient
);
174 // Verifies the root gets a connect.
175 TEST_F(FrameTest
, RootGetsConnect
) {
176 TestFrameTreeDelegate tree_delegate
;
177 TestFrameTreeClient root_client
;
178 FrameTree
tree(0u, window_manager()->GetRoot(), &tree_delegate
, &root_client
,
179 nullptr, Frame::ClientPropertyMap());
180 ASSERT_EQ(1, root_client
.connect_count());
181 mojo::Array
<FrameDataPtr
> frames
= root_client
.connect_frames();
182 ASSERT_EQ(1u, frames
.size());
183 EXPECT_EQ(tree
.root()->view()->id(), frames
[0]->frame_id
);
184 EXPECT_EQ(0u, frames
[0]->parent_id
);
187 // Verifies adding a child to the root.
188 TEST_F(FrameTest
, SingleChild
) {
189 TestFrameTreeDelegate tree_delegate
;
190 TestFrameTreeClient root_client
;
191 FrameTree
tree(0u, window_manager()->GetRoot(), &tree_delegate
, &root_client
,
192 nullptr, Frame::ClientPropertyMap());
194 View
* child
= window_manager()->CreateView();
195 EXPECT_EQ(nullptr, Frame::FindFirstFrameAncestor(child
));
196 window_manager()->GetRoot()->AddChild(child
);
197 EXPECT_EQ(tree
.root(), Frame::FindFirstFrameAncestor(child
));
199 TestFrameTreeClient child_client
;
201 tree
.CreateAndAddFrame(child
, tree
.root(), 0u, &child_client
, nullptr);
202 EXPECT_EQ(tree
.root(), child_frame
->parent());
204 ASSERT_EQ(1, child_client
.connect_count());
205 mojo::Array
<FrameDataPtr
> frames_in_child
= child_client
.connect_frames();
206 // We expect 2 frames. One for the root, one for the child.
207 ASSERT_EQ(2u, frames_in_child
.size());
208 EXPECT_EQ(tree
.root()->view()->id(), frames_in_child
[0]->frame_id
);
209 EXPECT_EQ(0u, frames_in_child
[0]->parent_id
);
210 EXPECT_EQ(child_frame
->view()->id(), frames_in_child
[1]->frame_id
);
211 EXPECT_EQ(tree
.root()->view()->id(), frames_in_child
[1]->parent_id
);
213 // We should have gotten notification of the add.
214 EXPECT_EQ(1u, root_client
.adds().size());
217 } // namespace web_view