| 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 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #import "ios/testing/wait_util.h" | 10 #import "ios/testing/wait_util.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 private: | 130 private: |
| 131 // UI handler class which communicates with test WebUI page. | 131 // UI handler class which communicates with test WebUI page. |
| 132 TestUIHandler* ui_handler_; | 132 TestUIHandler* ui_handler_; |
| 133 }; | 133 }; |
| 134 } // namespace | 134 } // namespace |
| 135 | 135 |
| 136 // A test fixture for verifying mojo comminication for WebUI. | 136 // A test fixture for verifying mojo comminication for WebUI. |
| 137 class WebUIMojoTest : public WebIntTest { | 137 class WebUIMojoTest : public WebIntTest { |
| 138 protected: | 138 protected: |
| 139 void SetUp() override { | 139 void SetUp() override { |
| 140 @autoreleasepool { | 140 WebIntTest::SetUp(); |
| 141 WebIntTest::SetUp(); | 141 ui_handler_ = base::MakeUnique<TestUIHandler>(); |
| 142 ui_handler_ = base::MakeUnique<TestUIHandler>(); | 142 web::WebState::CreateParams params(GetBrowserState()); |
| 143 web::WebState::CreateParams params(GetBrowserState()); | 143 web_state_ = base::MakeUnique<web::WebStateImpl>(params); |
| 144 web_state_ = base::MakeUnique<web::WebStateImpl>(params); | 144 web_state_->GetNavigationManagerImpl().InitializeSession(); |
| 145 web_state_->GetNavigationManagerImpl().InitializeSession(); | 145 WebUIIOSControllerFactory::RegisterFactory( |
| 146 WebUIIOSControllerFactory::RegisterFactory( | 146 new TestWebUIControllerFactory(ui_handler_.get())); |
| 147 new TestWebUIControllerFactory(ui_handler_.get())); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void TearDown() override { | |
| 152 @autoreleasepool { | |
| 153 web_state_.reset(); | |
| 154 WebIntTest::TearDown(); | |
| 155 } | |
| 156 } | 147 } |
| 157 | 148 |
| 158 // Returns WebState which loads test WebUI page. | 149 // Returns WebState which loads test WebUI page. |
| 159 WebStateImpl* web_state() { return web_state_.get(); } | 150 WebStateImpl* web_state() { return web_state_.get(); } |
| 160 // Returns UI handler which communicates with WebUI page. | 151 // Returns UI handler which communicates with WebUI page. |
| 161 TestUIHandler* test_ui_handler() { return ui_handler_.get(); } | 152 TestUIHandler* test_ui_handler() { return ui_handler_.get(); } |
| 162 | 153 |
| 163 private: | 154 private: |
| 164 std::unique_ptr<WebStateImpl> web_state_; | 155 std::unique_ptr<WebStateImpl> web_state_; |
| 165 std::unique_ptr<TestUIHandler> ui_handler_; | 156 std::unique_ptr<TestUIHandler> ui_handler_; |
| 166 }; | 157 }; |
| 167 | 158 |
| 168 // 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. |
| 169 // TestUIHandler is used for communication and test suceeds only when | 160 // TestUIHandler is used for communication and test suceeds only when |
| 170 // |TestUIHandler| sucessfully receives "ack" message from WebUI page. | 161 // |TestUIHandler| sucessfully receives "ack" message from WebUI page. |
| 171 #if TARGET_IPHONE_SIMULATOR | 162 #if TARGET_IPHONE_SIMULATOR |
| 172 #define MAYBE_MessageExchange MessageExchange | 163 #define MAYBE_MessageExchange MessageExchange |
| 173 #else | 164 #else |
| 174 #define MAYBE_MessageExchange FLAKY_MessageExchange | 165 #define MAYBE_MessageExchange FLAKY_MessageExchange |
| 175 #endif | 166 #endif |
| 176 // TODO(crbug.com/720098): Enable this test on device. | 167 // TODO(crbug.com/720098): Enable this test on device. |
| 177 TEST_F(WebUIMojoTest, MAYBE_MessageExchange) { | 168 TEST_F(WebUIMojoTest, MAYBE_MessageExchange) { |
| 178 @autoreleasepool { | 169 web_state()->SetWebUsageEnabled(true); |
| 179 web_state()->SetWebUsageEnabled(true); | 170 web_state()->GetView(); // WebState won't load URL without view. |
| 180 web_state()->GetView(); // WebState won't load URL without view. | 171 GURL url( |
| 181 GURL url(url::SchemeHostPort(kTestWebUIScheme, kTestWebUIURLHost, 0) | 172 url::SchemeHostPort(kTestWebUIScheme, kTestWebUIURLHost, 0).Serialize()); |
| 182 .Serialize()); | 173 NavigationManager::WebLoadParams load_params(url); |
| 183 NavigationManager::WebLoadParams load_params(url); | 174 web_state()->GetNavigationManager()->LoadURLWithParams(load_params); |
| 184 web_state()->GetNavigationManager()->LoadURLWithParams(load_params); | |
| 185 | 175 |
| 186 // Wait until |TestUIHandler| receives "fin" message from WebUI page. | 176 // Wait until |TestUIHandler| receives "fin" message from WebUI page. |
| 187 bool fin_received = testing::WaitUntilConditionOrTimeout(kMessageTimeout, ^{ | 177 bool fin_received = testing::WaitUntilConditionOrTimeout(kMessageTimeout, ^{ |
| 188 // Flush any pending tasks. Don't RunUntilIdle() because | 178 // Flush any pending tasks. Don't RunUntilIdle() because |
| 189 // RunUntilIdle() is incompatible with mojo::SimpleWatcher's | 179 // RunUntilIdle() is incompatible with mojo::SimpleWatcher's |
| 190 // automatic arming behavior, which Mojo JS still depends upon. | 180 // automatic arming behavior, which Mojo JS still depends upon. |
| 191 // | 181 // |
| 192 // TODO(crbug.com/701875): Introduce the full watcher API to JS and get | 182 // TODO(crbug.com/701875): Introduce the full watcher API to JS and get rid |
| 193 // rid of this hack. | 183 // of this hack. |
| 194 base::RunLoop loop; | 184 base::RunLoop loop; |
| 195 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 185 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 196 loop.QuitClosure()); | 186 loop.QuitClosure()); |
| 197 loop.Run(); | 187 loop.Run(); |
| 198 return test_ui_handler()->IsFinReceived(); | 188 return test_ui_handler()->IsFinReceived(); |
| 199 }); | 189 }); |
| 200 | 190 |
| 201 ASSERT_TRUE(fin_received); | 191 ASSERT_TRUE(fin_received); |
| 202 EXPECT_FALSE(web_state()->IsLoading()); | 192 EXPECT_FALSE(web_state()->IsLoading()); |
| 203 EXPECT_EQ(url, web_state()->GetLastCommittedURL()); | 193 EXPECT_EQ(url, web_state()->GetLastCommittedURL()); |
| 204 } | |
| 205 } | 194 } |
| 206 | 195 |
| 207 } // namespace web | 196 } // namespace web |
| OLD | NEW |