OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #import "base/test/ios/wait_util.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #import "ios/testing/wait_util.h" |
11 #import "ios/web/public/navigation_manager.h" | 11 #import "ios/web/public/navigation_manager.h" |
12 #include "ios/web/public/web_state/web_state_interface_provider.h" | 12 #include "ios/web/public/web_state/web_state_interface_provider.h" |
13 #include "ios/web/public/web_ui_ios_data_source.h" | 13 #include "ios/web/public/web_ui_ios_data_source.h" |
14 #include "ios/web/public/webui/web_ui_ios_controller.h" | 14 #include "ios/web/public/webui/web_ui_ios_controller.h" |
15 #include "ios/web/public/webui/web_ui_ios_controller_factory.h" | 15 #include "ios/web/public/webui/web_ui_ios_controller_factory.h" |
16 #include "ios/web/test/grit/test_resources.h" | 16 #include "ios/web/test/grit/test_resources.h" |
17 #include "ios/web/test/mojo_test.mojom.h" | 17 #include "ios/web/test/mojo_test.mojom.h" |
18 #include "ios/web/test/test_url_constants.h" | 18 #include "ios/web/test/test_url_constants.h" |
19 #import "ios/web/test/web_int_test.h" | 19 #import "ios/web/test/web_int_test.h" |
20 #import "ios/web/web_state/ui/crw_web_controller.h" | 20 #import "ios/web/web_state/ui/crw_web_controller.h" |
21 #import "ios/web/web_state/web_state_impl.h" | 21 #import "ios/web/web_state/web_state_impl.h" |
22 #include "mojo/public/cpp/bindings/binding_set.h" | 22 #include "mojo/public/cpp/bindings/binding_set.h" |
23 #include "services/service_manager/public/cpp/bind_source_info.h" | 23 #include "services/service_manager/public/cpp/bind_source_info.h" |
24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
25 #include "url/scheme_host_port.h" | 25 #include "url/scheme_host_port.h" |
26 | 26 |
27 namespace web { | 27 namespace web { |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 // Hostname for test WebUI page. | 31 // Hostname for test WebUI page. |
32 const char kTestWebUIURLHost[] = "testwebui"; | 32 const char kTestWebUIURLHost[] = "testwebui"; |
33 | 33 |
| 34 // Timeout in seconds to wait for a sucessful message exchange between native |
| 35 // code and a web page using Mojo. |
| 36 const NSTimeInterval kMessageTimeout = 5.0; |
| 37 |
34 // UI handler class which communicates with test WebUI page as follows: | 38 // UI handler class which communicates with test WebUI page as follows: |
35 // - page sends "syn" message to |TestUIHandler| | 39 // - page sends "syn" message to |TestUIHandler| |
36 // - |TestUIHandler| replies with "ack" message | 40 // - |TestUIHandler| replies with "ack" message |
37 // - page replies back with "fin" | 41 // - page replies back with "fin" |
38 // | 42 // |
39 // Once "fin" is received |IsFinReceived()| call will return true, indicating | 43 // Once "fin" is received |IsFinReceived()| call will return true, indicating |
40 // that communication was successful. See test WebUI page code here: | 44 // that communication was successful. See test WebUI page code here: |
41 // ios/web/test/data/mojo_test.js | 45 // ios/web/test/data/mojo_test.js |
42 class TestUIHandler : public TestUIHandlerMojo { | 46 class TestUIHandler : public TestUIHandlerMojo { |
43 public: | 47 public: |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 | 129 |
126 private: | 130 private: |
127 // UI handler class which communicates with test WebUI page. | 131 // UI handler class which communicates with test WebUI page. |
128 TestUIHandler* ui_handler_; | 132 TestUIHandler* ui_handler_; |
129 }; | 133 }; |
130 } // namespace | 134 } // namespace |
131 | 135 |
132 // A test fixture for verifying mojo comminication for WebUI. | 136 // A test fixture for verifying mojo comminication for WebUI. |
133 class WebUIMojoTest : public WebIntTest { | 137 class WebUIMojoTest : public WebIntTest { |
134 protected: | 138 protected: |
135 WebUIMojoTest() : ui_handler_(new TestUIHandler()) { | 139 void SetUp() override { |
| 140 WebIntTest::SetUp(); |
| 141 ui_handler_ = base::MakeUnique<TestUIHandler>(); |
136 web::WebState::CreateParams params(GetBrowserState()); | 142 web::WebState::CreateParams params(GetBrowserState()); |
137 web_state_ = base::MakeUnique<web::WebStateImpl>(params); | 143 web_state_ = base::MakeUnique<web::WebStateImpl>(params); |
138 web_state_->GetNavigationManagerImpl().InitializeSession(); | 144 web_state_->GetNavigationManagerImpl().InitializeSession(); |
139 WebUIIOSControllerFactory::RegisterFactory( | 145 WebUIIOSControllerFactory::RegisterFactory( |
140 new TestWebUIControllerFactory(ui_handler_.get())); | 146 new TestWebUIControllerFactory(ui_handler_.get())); |
141 } | 147 } |
142 | 148 |
143 // Returns WebState which loads test WebUI page. | 149 // Returns WebState which loads test WebUI page. |
144 WebStateImpl* web_state() { return web_state_.get(); } | 150 WebStateImpl* web_state() { return web_state_.get(); } |
145 // Returns UI handler which communicates with WebUI page. | 151 // Returns UI handler which communicates with WebUI page. |
146 TestUIHandler* test_ui_handler() { return ui_handler_.get(); } | 152 TestUIHandler* test_ui_handler() { return ui_handler_.get(); } |
147 | 153 |
148 private: | 154 private: |
149 std::unique_ptr<WebStateImpl> web_state_; | 155 std::unique_ptr<WebStateImpl> web_state_; |
150 std::unique_ptr<TestUIHandler> ui_handler_; | 156 std::unique_ptr<TestUIHandler> ui_handler_; |
151 }; | 157 }; |
152 | 158 |
153 // Tests that JS can send messages to the native code and vice versa. | 159 // Tests that JS can send messages to the native code and vice versa. |
154 // TestUIHandler is used for communication and test suceeds only when | 160 // TestUIHandler is used for communication and test suceeds only when |
155 // |TestUIHandler| sucessfully receives "ack" message from WebUI page. | 161 // |TestUIHandler| sucessfully receives "ack" message from WebUI page. |
156 #if TARGET_IPHONE_SIMULATOR | 162 #if TARGET_IPHONE_SIMULATOR |
157 #define MAYBE_MessageExchange MessageExchange | 163 #define MAYBE_MessageExchange MessageExchange |
158 #else | 164 #else |
159 #define MAYBE_MessageExchange DISABLED_MessageExchange | 165 #define MAYBE_MessageExchange FLAKY_MessageExchange |
160 #endif | 166 #endif |
161 // TODO(crbug.com/720098): Enable this test on device. | 167 // TODO(crbug.com/720098): Enable this test on device. |
162 TEST_F(WebUIMojoTest, MAYBE_MessageExchange) { | 168 TEST_F(WebUIMojoTest, MAYBE_MessageExchange) { |
163 web_state()->SetWebUsageEnabled(true); | 169 web_state()->SetWebUsageEnabled(true); |
164 web_state()->GetView(); // WebState won't load URL without view. | 170 web_state()->GetView(); // WebState won't load URL without view. |
165 NavigationManager::WebLoadParams load_params(GURL( | 171 GURL url( |
166 url::SchemeHostPort(kTestWebUIScheme, kTestWebUIURLHost, 0).Serialize())); | 172 url::SchemeHostPort(kTestWebUIScheme, kTestWebUIURLHost, 0).Serialize()); |
| 173 NavigationManager::WebLoadParams load_params(url); |
167 web_state()->GetNavigationManager()->LoadURLWithParams(load_params); | 174 web_state()->GetNavigationManager()->LoadURLWithParams(load_params); |
168 | 175 |
169 // Wait until |TestUIHandler| receives "ack" message from WebUI page. | 176 // Wait until |TestUIHandler| receives "fin" message from WebUI page. |
170 base::test::ios::WaitUntilCondition(^{ | 177 bool fin_received = testing::WaitUntilConditionOrTimeout(kMessageTimeout, ^{ |
171 // Flush any pending tasks. Don't RunUntilIdle() because | 178 // Flush any pending tasks. Don't RunUntilIdle() because |
172 // RunUntilIdle() is incompatible with mojo::SimpleWatcher's | 179 // RunUntilIdle() is incompatible with mojo::SimpleWatcher's |
173 // automatic arming behavior, which Mojo JS still depends upon. | 180 // automatic arming behavior, which Mojo JS still depends upon. |
174 // | 181 // |
175 // TODO(crbug.com/701875): Introduce the full watcher API to JS and get rid | 182 // TODO(crbug.com/701875): Introduce the full watcher API to JS and get rid |
176 // of this hack. | 183 // of this hack. |
177 base::RunLoop loop; | 184 base::RunLoop loop; |
178 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 185 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
179 loop.QuitClosure()); | 186 loop.QuitClosure()); |
180 loop.Run(); | 187 loop.Run(); |
181 return test_ui_handler()->IsFinReceived(); | 188 return test_ui_handler()->IsFinReceived(); |
182 }); | 189 }); |
| 190 |
| 191 ASSERT_TRUE(fin_received); |
| 192 EXPECT_FALSE(web_state()->IsLoading()); |
| 193 EXPECT_EQ(url, web_state()->GetLastCommittedURL()); |
183 } | 194 } |
184 | 195 |
185 } // namespace web | 196 } // namespace web |
OLD | NEW |