| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/auto_reset.h" | 5 #include "base/auto_reset.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "components/html_viewer/public/interfaces/test_html_viewer.mojom.h" | 14 #include "components/html_viewer/public/interfaces/test_html_viewer.mojom.h" |
| 15 #include "components/mus/public/cpp/tests/view_manager_test_base.h" | 15 #include "components/mus/public/cpp/tests/mandoline_ui_services_test_base.h" |
| 16 #include "components/mus/public/cpp/view.h" | 16 #include "components/mus/public/cpp/view.h" |
| 17 #include "components/mus/public/cpp/view_tree_connection.h" | 17 #include "components/mus/public/cpp/view_tree_connection.h" |
| 18 #include "components/web_view/frame.h" | 18 #include "components/web_view/frame.h" |
| 19 #include "components/web_view/frame_connection.h" | 19 #include "components/web_view/frame_connection.h" |
| 20 #include "components/web_view/frame_tree.h" | 20 #include "components/web_view/frame_tree.h" |
| 21 #include "components/web_view/public/interfaces/frame_tree.mojom.h" | 21 #include "components/web_view/public/interfaces/frame_tree.mojom.h" |
| 22 #include "components/web_view/test_frame_tree_delegate.h" | 22 #include "components/web_view/test_frame_tree_delegate.h" |
| 23 #include "mojo/application/public/cpp/application_impl.h" | 23 #include "mojo/application/public/cpp/application_impl.h" |
| 24 #include "net/test/spawned_test_server/spawned_test_server.h" | 24 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 25 #include "third_party/mojo_services/src/accessibility/public/interfaces/accessib
ility.mojom.h" | 25 #include "third_party/mojo_services/src/accessibility/public/interfaces/accessib
ility.mojom.h" |
| 26 | 26 |
| 27 using mus::MandolineUIServicesTestBase; |
| 27 using web_view::Frame; | 28 using web_view::Frame; |
| 28 using web_view::FrameConnection; | 29 using web_view::FrameConnection; |
| 29 using web_view::FrameTree; | 30 using web_view::FrameTree; |
| 30 using web_view::FrameTreeClient; | 31 using web_view::FrameTreeClient; |
| 31 using web_view::FrameTreeDelegate; | 32 using web_view::FrameTreeDelegate; |
| 32 | 33 |
| 33 namespace mojo { | 34 namespace mojo { |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 const char kAddFrameWithEmptyPageScript[] = | 38 const char kAddFrameWithEmptyPageScript[] = |
| 38 "var iframe = document.createElement(\"iframe\");" | 39 "var iframe = document.createElement(\"iframe\");" |
| 39 "iframe.src = \"http://127.0.0.1:%u/files/empty_page.html\";" | 40 "iframe.src = \"http://127.0.0.1:%u/files/empty_page.html\";" |
| 40 "document.body.appendChild(iframe);"; | 41 "document.body.appendChild(iframe);"; |
| 41 | 42 |
| 42 void OnGotContentHandlerForRoot(bool* got_callback) { | 43 void OnGotContentHandlerForRoot(bool* got_callback) { |
| 43 *got_callback = true; | 44 *got_callback = true; |
| 44 ignore_result(ViewManagerTestBase::QuitRunLoop()); | 45 ignore_result(MandolineUIServicesTestBase::QuitRunLoop()); |
| 45 } | 46 } |
| 46 | 47 |
| 47 mojo::ApplicationConnection* ApplicationConnectionForFrame(Frame* frame) { | 48 mojo::ApplicationConnection* ApplicationConnectionForFrame(Frame* frame) { |
| 48 return static_cast<FrameConnection*>(frame->user_data()) | 49 return static_cast<FrameConnection*>(frame->user_data()) |
| 49 ->application_connection(); | 50 ->application_connection(); |
| 50 } | 51 } |
| 51 | 52 |
| 52 std::string GetFrameText(ApplicationConnection* connection) { | 53 std::string GetFrameText(ApplicationConnection* connection) { |
| 53 html_viewer::TestHTMLViewerPtr test_html_viewer; | 54 html_viewer::TestHTMLViewerPtr test_html_viewer; |
| 54 connection->ConnectToService(&test_html_viewer); | 55 connection->ConnectToService(&test_html_viewer); |
| 55 std::string result; | 56 std::string result; |
| 56 test_html_viewer->GetContentAsText([&result](const String& mojo_string) { | 57 test_html_viewer->GetContentAsText([&result](const String& mojo_string) { |
| 57 result = mojo_string; | 58 result = mojo_string; |
| 58 ASSERT_TRUE(ViewManagerTestBase::QuitRunLoop()); | 59 ASSERT_TRUE(MandolineUIServicesTestBase::QuitRunLoop()); |
| 59 }); | 60 }); |
| 60 if (!ViewManagerTestBase::DoRunLoopWithTimeout()) | 61 if (!MandolineUIServicesTestBase::DoRunLoopWithTimeout()) |
| 61 ADD_FAILURE() << "Timed out waiting for execute to complete"; | 62 ADD_FAILURE() << "Timed out waiting for execute to complete"; |
| 62 // test_html_viewer.WaitForIncomingResponse(); | 63 // test_html_viewer.WaitForIncomingResponse(); |
| 63 return result; | 64 return result; |
| 64 } | 65 } |
| 65 | 66 |
| 66 scoped_ptr<base::Value> ExecuteScript(ApplicationConnection* connection, | 67 scoped_ptr<base::Value> ExecuteScript(ApplicationConnection* connection, |
| 67 const std::string& script) { | 68 const std::string& script) { |
| 68 html_viewer::TestHTMLViewerPtr test_html_viewer; | 69 html_viewer::TestHTMLViewerPtr test_html_viewer; |
| 69 connection->ConnectToService(&test_html_viewer); | 70 connection->ConnectToService(&test_html_viewer); |
| 70 scoped_ptr<base::Value> result; | 71 scoped_ptr<base::Value> result; |
| 71 test_html_viewer->ExecuteScript(script, [&result](const String& json_string) { | 72 test_html_viewer->ExecuteScript(script, [&result](const String& json_string) { |
| 72 result = base::JSONReader::Read(json_string.To<std::string>()); | 73 result = base::JSONReader::Read(json_string.To<std::string>()); |
| 73 ASSERT_TRUE(ViewManagerTestBase::QuitRunLoop()); | 74 ASSERT_TRUE(MandolineUIServicesTestBase::QuitRunLoop()); |
| 74 }); | 75 }); |
| 75 if (!ViewManagerTestBase::DoRunLoopWithTimeout()) | 76 if (!MandolineUIServicesTestBase::DoRunLoopWithTimeout()) |
| 76 ADD_FAILURE() << "Timed out waiting for execute to complete"; | 77 ADD_FAILURE() << "Timed out waiting for execute to complete"; |
| 77 return result.Pass(); | 78 return result.Pass(); |
| 78 } | 79 } |
| 79 | 80 |
| 80 // FrameTreeDelegate that can block waiting for navigation to start. | 81 // FrameTreeDelegate that can block waiting for navigation to start. |
| 81 class TestFrameTreeDelegateImpl : public web_view::TestFrameTreeDelegate { | 82 class TestFrameTreeDelegateImpl : public web_view::TestFrameTreeDelegate { |
| 82 public: | 83 public: |
| 83 explicit TestFrameTreeDelegateImpl(mojo::ApplicationImpl* app) | 84 explicit TestFrameTreeDelegateImpl(mojo::ApplicationImpl* app) |
| 84 : TestFrameTreeDelegate(app), | 85 : TestFrameTreeDelegate(app), |
| 85 frame_tree_(nullptr), | 86 frame_tree_(nullptr), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 97 // navigation has already occurred. In other words, take care when using this, | 98 // navigation has already occurred. In other words, take care when using this, |
| 98 // you may need to clear_got_navigate() before calling this. | 99 // you may need to clear_got_navigate() before calling this. |
| 99 bool WaitForNavigateFrame() { | 100 bool WaitForNavigateFrame() { |
| 100 if (waiting_for_navigate_) | 101 if (waiting_for_navigate_) |
| 101 return false; | 102 return false; |
| 102 | 103 |
| 103 if (got_navigate_) | 104 if (got_navigate_) |
| 104 return true; | 105 return true; |
| 105 | 106 |
| 106 base::AutoReset<bool> resetter(&waiting_for_navigate_, true); | 107 base::AutoReset<bool> resetter(&waiting_for_navigate_, true); |
| 107 return ViewManagerTestBase::DoRunLoopWithTimeout() && got_navigate_; | 108 return MandolineUIServicesTestBase::DoRunLoopWithTimeout() && got_navigate_; |
| 108 } | 109 } |
| 109 | 110 |
| 110 // TestFrameTreeDelegate: | 111 // TestFrameTreeDelegate: |
| 111 void CanNavigateFrame(Frame* target, | 112 void CanNavigateFrame(Frame* target, |
| 112 mojo::URLRequestPtr request, | 113 mojo::URLRequestPtr request, |
| 113 const CanNavigateFrameCallback& callback) override { | 114 const CanNavigateFrameCallback& callback) override { |
| 114 FrameConnection::CreateConnectionForCanNavigateFrame( | 115 FrameConnection::CreateConnectionForCanNavigateFrame( |
| 115 app(), target, request.Pass(), callback); | 116 app(), target, request.Pass(), callback); |
| 116 } | 117 } |
| 117 | 118 |
| 118 void DidStartNavigation(Frame* frame) override { | 119 void DidStartNavigation(Frame* frame) override { |
| 119 got_navigate_ = true; | 120 got_navigate_ = true; |
| 120 | 121 |
| 121 if (waiting_for_navigate_) | 122 if (waiting_for_navigate_) |
| 122 ignore_result(ViewManagerTestBase::QuitRunLoop()); | 123 ignore_result(MandolineUIServicesTestBase::QuitRunLoop()); |
| 123 } | 124 } |
| 124 | 125 |
| 125 private: | 126 private: |
| 126 FrameTree* frame_tree_; | 127 FrameTree* frame_tree_; |
| 127 bool waiting_for_navigate_; | 128 bool waiting_for_navigate_; |
| 128 bool got_navigate_; | 129 bool got_navigate_; |
| 129 | 130 |
| 130 DISALLOW_COPY_AND_ASSIGN(TestFrameTreeDelegateImpl); | 131 DISALLOW_COPY_AND_ASSIGN(TestFrameTreeDelegateImpl); |
| 131 }; | 132 }; |
| 132 | 133 |
| 133 } // namespace | 134 } // namespace |
| 134 | 135 |
| 135 class HTMLFrameTest : public ViewManagerTestBase { | 136 class HTMLFrameTest : public MandolineUIServicesTestBase { |
| 136 public: | 137 public: |
| 137 HTMLFrameTest() {} | 138 HTMLFrameTest() {} |
| 138 ~HTMLFrameTest() override {} | 139 ~HTMLFrameTest() override {} |
| 139 | 140 |
| 140 protected: | 141 protected: |
| 141 // Creates the frame tree showing an empty page at the root and adds (via | 142 // Creates the frame tree showing an empty page at the root and adds (via |
| 142 // script) a frame showing the same empty page. | 143 // script) a frame showing the same empty page. |
| 143 Frame* LoadEmptyPageAndCreateFrame() { | 144 Frame* LoadEmptyPageAndCreateFrame() { |
| 144 View* embed_view = window_manager()->CreateView(); | 145 mus::View* embed_view = window_manager()->CreateView(); |
| 145 frame_tree_delegate_.reset( | 146 frame_tree_delegate_.reset( |
| 146 new TestFrameTreeDelegateImpl(application_impl())); | 147 new TestFrameTreeDelegateImpl(application_impl())); |
| 147 FrameConnection* root_connection = | 148 FrameConnection* root_connection = |
| 148 InitFrameTree(embed_view, "http://127.0.0.1:%u/files/empty_page2.html"); | 149 InitFrameTree(embed_view, "http://127.0.0.1:%u/files/empty_page2.html"); |
| 149 if (!root_connection) { | 150 if (!root_connection) { |
| 150 ADD_FAILURE() << "unable to establish root connection"; | 151 ADD_FAILURE() << "unable to establish root connection"; |
| 151 return nullptr; | 152 return nullptr; |
| 152 } | 153 } |
| 153 const std::string frame_text = | 154 const std::string frame_text = |
| 154 GetFrameText(root_connection->application_connection()); | 155 GetFrameText(root_connection->application_connection()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 const uint16_t assigned_port = http_server_->host_port_pair().port(); | 188 const uint16_t assigned_port = http_server_->host_port_pair().port(); |
| 188 return base::StringPrintf(string.c_str(), assigned_port); | 189 return base::StringPrintf(string.c_str(), assigned_port); |
| 189 } | 190 } |
| 190 | 191 |
| 191 mojo::URLRequestPtr BuildRequestForURL(const std::string& url_string) { | 192 mojo::URLRequestPtr BuildRequestForURL(const std::string& url_string) { |
| 192 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 193 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 193 request->url = mojo::String::From(AddPortToString(url_string)); | 194 request->url = mojo::String::From(AddPortToString(url_string)); |
| 194 return request.Pass(); | 195 return request.Pass(); |
| 195 } | 196 } |
| 196 | 197 |
| 197 FrameConnection* InitFrameTree(View* view, const std::string& url_string) { | 198 FrameConnection* InitFrameTree(mus::View* view, |
| 199 const std::string& url_string) { |
| 198 frame_tree_delegate_.reset( | 200 frame_tree_delegate_.reset( |
| 199 new TestFrameTreeDelegateImpl(application_impl())); | 201 new TestFrameTreeDelegateImpl(application_impl())); |
| 200 scoped_ptr<FrameConnection> frame_connection(new FrameConnection); | 202 scoped_ptr<FrameConnection> frame_connection(new FrameConnection); |
| 201 bool got_callback = false; | 203 bool got_callback = false; |
| 202 frame_connection->Init( | 204 frame_connection->Init( |
| 203 application_impl(), BuildRequestForURL(url_string), | 205 application_impl(), BuildRequestForURL(url_string), |
| 204 base::Bind(&OnGotContentHandlerForRoot, &got_callback)); | 206 base::Bind(&OnGotContentHandlerForRoot, &got_callback)); |
| 205 ignore_result(ViewManagerTestBase::DoRunLoopWithTimeout()); | 207 ignore_result(MandolineUIServicesTestBase::DoRunLoopWithTimeout()); |
| 206 if (!got_callback) | 208 if (!got_callback) |
| 207 return nullptr; | 209 return nullptr; |
| 208 FrameConnection* result = frame_connection.get(); | 210 FrameConnection* result = frame_connection.get(); |
| 209 FrameTreeClient* frame_tree_client = frame_connection->frame_tree_client(); | 211 FrameTreeClient* frame_tree_client = frame_connection->frame_tree_client(); |
| 210 ViewTreeClientPtr tree_client = frame_connection->GetViewTreeClient(); | 212 ViewTreeClientPtr tree_client = frame_connection->GetViewTreeClient(); |
| 211 frame_tree_.reset( | 213 frame_tree_.reset( |
| 212 new FrameTree(result->GetContentHandlerID(), view, tree_client.Pass(), | 214 new FrameTree(result->GetContentHandlerID(), view, tree_client.Pass(), |
| 213 frame_tree_delegate_.get(), frame_tree_client, | 215 frame_tree_delegate_.get(), frame_tree_client, |
| 214 frame_connection.Pass(), Frame::ClientPropertyMap())); | 216 frame_connection.Pass(), Frame::ClientPropertyMap())); |
| 215 frame_tree_delegate_->set_frame_tree(frame_tree_.get()); | 217 frame_tree_delegate_->set_frame_tree(frame_tree_.get()); |
| 216 return result; | 218 return result; |
| 217 } | 219 } |
| 218 | 220 |
| 219 bool WaitForNavigateFrame() { | 221 bool WaitForNavigateFrame() { |
| 220 if (frame_tree_delegate_->waiting_for_navigate()) | 222 if (frame_tree_delegate_->waiting_for_navigate()) |
| 221 return false; | 223 return false; |
| 222 | 224 |
| 223 frame_tree_delegate_->clear_got_navigate(); | 225 frame_tree_delegate_->clear_got_navigate(); |
| 224 return frame_tree_delegate_->WaitForNavigateFrame(); | 226 return frame_tree_delegate_->WaitForNavigateFrame(); |
| 225 } | 227 } |
| 226 | 228 |
| 227 // ViewManagerTest: | 229 // ViewManagerTest: |
| 228 void SetUp() override { | 230 void SetUp() override { |
| 229 ViewManagerTestBase::SetUp(); | 231 MandolineUIServicesTestBase::SetUp(); |
| 230 | 232 |
| 231 // Start a test server. | 233 // Start a test server. |
| 232 http_server_.reset(new net::SpawnedTestServer( | 234 http_server_.reset(new net::SpawnedTestServer( |
| 233 net::SpawnedTestServer::TYPE_HTTP, net::SpawnedTestServer::kLocalhost, | 235 net::SpawnedTestServer::TYPE_HTTP, net::SpawnedTestServer::kLocalhost, |
| 234 base::FilePath(FILE_PATH_LITERAL("components/test/data/html_viewer")))); | 236 base::FilePath(FILE_PATH_LITERAL("components/test/data/html_viewer")))); |
| 235 ASSERT_TRUE(http_server_->Start()); | 237 ASSERT_TRUE(http_server_->Start()); |
| 236 } | 238 } |
| 237 void TearDown() override { | 239 void TearDown() override { |
| 238 frame_tree_.reset(); | 240 frame_tree_.reset(); |
| 239 http_server_.reset(); | 241 http_server_.reset(); |
| 240 ViewManagerTestBase::TearDown(); | 242 MandolineUIServicesTestBase::TearDown(); |
| 241 } | 243 } |
| 242 | 244 |
| 243 scoped_ptr<net::SpawnedTestServer> http_server_; | 245 scoped_ptr<net::SpawnedTestServer> http_server_; |
| 244 scoped_ptr<FrameTree> frame_tree_; | 246 scoped_ptr<FrameTree> frame_tree_; |
| 245 | 247 |
| 246 scoped_ptr<TestFrameTreeDelegateImpl> frame_tree_delegate_; | 248 scoped_ptr<TestFrameTreeDelegateImpl> frame_tree_delegate_; |
| 247 | 249 |
| 248 private: | 250 private: |
| 249 DISALLOW_COPY_AND_ASSIGN(HTMLFrameTest); | 251 DISALLOW_COPY_AND_ASSIGN(HTMLFrameTest); |
| 250 }; | 252 }; |
| 251 | 253 |
| 252 TEST_F(HTMLFrameTest, PageWithSingleFrame) { | 254 TEST_F(HTMLFrameTest, PageWithSingleFrame) { |
| 253 View* embed_view = window_manager()->CreateView(); | 255 mus::View* embed_view = window_manager()->CreateView(); |
| 254 | 256 |
| 255 FrameConnection* root_connection = InitFrameTree( | 257 FrameConnection* root_connection = InitFrameTree( |
| 256 embed_view, "http://127.0.0.1:%u/files/page_with_single_frame.html"); | 258 embed_view, "http://127.0.0.1:%u/files/page_with_single_frame.html"); |
| 257 ASSERT_TRUE(root_connection); | 259 ASSERT_TRUE(root_connection); |
| 258 | 260 |
| 259 ASSERT_EQ("Page with single frame", | 261 ASSERT_EQ("Page with single frame", |
| 260 GetFrameText(root_connection->application_connection())); | 262 GetFrameText(root_connection->application_connection())); |
| 261 | 263 |
| 262 // page_with_single_frame contains a child frame. The child frame should | 264 // page_with_single_frame contains a child frame. The child frame should |
| 263 // create a new View and Frame. | 265 // create a new View and Frame. |
| 264 if (frame_tree_->root()->children().empty() || | 266 if (frame_tree_->root()->children().empty() || |
| 265 !frame_tree_->root()->children().back()->user_data()) { | 267 !frame_tree_->root()->children().back()->user_data()) { |
| 266 ASSERT_TRUE(WaitForNavigateFrame()); | 268 ASSERT_TRUE(WaitForNavigateFrame()); |
| 267 } | 269 } |
| 268 | 270 |
| 269 ASSERT_EQ(1u, embed_view->children().size()); | 271 ASSERT_EQ(1u, embed_view->children().size()); |
| 270 Frame* child_frame = | 272 Frame* child_frame = |
| 271 frame_tree_->root()->FindFrame(embed_view->children()[0]->id()); | 273 frame_tree_->root()->FindFrame(embed_view->children()[0]->id()); |
| 272 ASSERT_TRUE(child_frame); | 274 ASSERT_TRUE(child_frame); |
| 273 | 275 |
| 274 ASSERT_EQ("child", | 276 ASSERT_EQ("child", |
| 275 GetFrameText(static_cast<FrameConnection*>(child_frame->user_data()) | 277 GetFrameText(static_cast<FrameConnection*>(child_frame->user_data()) |
| 276 ->application_connection())); | 278 ->application_connection())); |
| 277 } | 279 } |
| 278 | 280 |
| 279 // Creates two frames. The parent navigates the child frame by way of changing | 281 // Creates two frames. The parent navigates the child frame by way of changing |
| 280 // the location of the child frame. | 282 // the location of the child frame. |
| 281 TEST_F(HTMLFrameTest, ChangeLocationOfChildFrame) { | 283 TEST_F(HTMLFrameTest, ChangeLocationOfChildFrame) { |
| 282 View* embed_view = window_manager()->CreateView(); | 284 mus::View* embed_view = window_manager()->CreateView(); |
| 283 | 285 |
| 284 ASSERT_TRUE(InitFrameTree( | 286 ASSERT_TRUE(InitFrameTree( |
| 285 embed_view, "http://127.0.0.1:%u/files/page_with_single_frame.html")); | 287 embed_view, "http://127.0.0.1:%u/files/page_with_single_frame.html")); |
| 286 | 288 |
| 287 // page_with_single_frame contains a child frame. The child frame should | 289 // page_with_single_frame contains a child frame. The child frame should |
| 288 // create a new View and Frame. | 290 // create a new View and Frame. |
| 289 if (frame_tree_->root()->children().empty() || | 291 if (frame_tree_->root()->children().empty() || |
| 290 !frame_tree_->root()->children().back()->user_data()) { | 292 !frame_tree_->root()->children().back()->user_data()) { |
| 291 ASSERT_TRUE(WaitForNavigateFrame()); | 293 ASSERT_TRUE(WaitForNavigateFrame()); |
| 292 } | 294 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 script_value_as_list->GetString(0u, &message_in_child); | 470 script_value_as_list->GetString(0u, &message_in_child); |
| 469 } | 471 } |
| 470 } | 472 } |
| 471 } while (message_in_child != "hello from parent" && | 473 } while (message_in_child != "hello from parent" && |
| 472 base::TimeTicks::Now() - start_time < | 474 base::TimeTicks::Now() - start_time < |
| 473 TestTimeouts::action_timeout()); | 475 TestTimeouts::action_timeout()); |
| 474 EXPECT_EQ("hello from parent", message_in_child); | 476 EXPECT_EQ("hello from parent", message_in_child); |
| 475 } | 477 } |
| 476 | 478 |
| 477 } // namespace mojo | 479 } // namespace mojo |
| OLD | NEW |