Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <memory> | |
| 6 | |
| 7 #include "base/run_loop.h" | |
| 8 #import "base/test/ios/wait_util.h" | |
| 9 #include "ios/public/provider/web/web_ui_ios_controller.h" | |
| 10 #include "ios/public/provider/web/web_ui_ios_controller_factory.h" | |
| 11 #import "ios/web/public/navigation_manager.h" | |
| 12 #include "ios/web/public/web_ui_ios_data_source.h" | |
| 13 #include "ios/web/test/grit/test_resources.h" | |
| 14 #include "ios/web/test/mojo_test.mojom.h" | |
| 15 #include "ios/web/test/test_url_constants.h" | |
| 16 #import "ios/web/test/web_int_test.h" | |
| 17 #import "ios/web/web_state/web_state_impl.h" | |
| 18 #include "mojo/public/cpp/bindings/binding_set.h" | |
| 19 #include "services/shell/public/cpp/interface_registry.h" | |
| 20 #include "url/gurl.h" | |
| 21 #include "url/scheme_host_port.h" | |
| 22 | |
| 23 namespace web { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Hostname for test WebUI page. | |
| 28 const char kTestWebUIURLHost[] = "testwebui"; | |
| 29 | |
| 30 // UI handler class which communicates with test WebUI page as follows: | |
| 31 // - page sends "syn" message to |TestUIHandler| | |
| 32 // - |TestUIHandler| replies with "ack" message | |
| 33 // - page replies back with "fin" | |
| 34 // | |
| 35 // Once "fin" is received |IsFinReceived()| call will return true, indicating | |
| 36 // that communication was successfull. See test WebUI page for more details: | |
|
Jackie Quinn
2016/05/27 01:03:41
s/successfull/successful
Eugene But (OOO till 7-30)
2016/05/27 15:29:00
Done.
| |
| 37 // ios/web/test/data/mojo_test.js | |
|
Jackie Quinn
2016/05/27 01:03:41
You've got a circular reference here, since mojo_t
Eugene But (OOO till 7-30)
2016/05/27 15:29:00
Done.
| |
| 38 class TestUIHandler : public TestUIHandlerMojo, | |
| 39 public shell::InterfaceFactory<TestUIHandlerMojo> { | |
| 40 public: | |
| 41 TestUIHandler() {} | |
| 42 ~TestUIHandler() override {} | |
| 43 | |
| 44 // Returns true if "fin" has been received. | |
| 45 bool IsFinReceived() { return fin_received_; } | |
| 46 | |
| 47 // TestUIHandlerMojo overrides. | |
| 48 void HandleJsMessage(const mojo::String& message, TestPagePtr page) override { | |
| 49 if (message.get() == "syn") { | |
| 50 // Received "syn" message from WebUI page, send "ack" as reply. | |
| 51 DCHECK(!syn_received_); | |
| 52 DCHECK(!fin_received_); | |
| 53 syn_received_ = true; | |
| 54 NativeMessageResultMojoPtr result(NativeMessageResultMojo::New()); | |
| 55 result->message = mojo::String::From("ack"); | |
| 56 page->HandleNativeMessage(std::move(result)); | |
| 57 } else if (message.get() == "fin") { | |
| 58 // Received "fin" from the WebUI page in response to "ack". | |
| 59 DCHECK(syn_received_); | |
| 60 DCHECK(!fin_received_); | |
| 61 fin_received_ = true; | |
| 62 } else { | |
| 63 NOTREACHED(); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 // shell::InterfaceFactory overrides. | |
| 69 void Create(shell::Connection* connection, | |
| 70 mojo::InterfaceRequest<TestUIHandlerMojo> request) override { | |
| 71 bindings_.AddBinding(this, std::move(request)); | |
| 72 } | |
| 73 | |
| 74 mojo::BindingSet<TestUIHandlerMojo> bindings_; | |
| 75 // |true| if "syn" has been received. | |
| 76 bool syn_received_ = false; | |
| 77 // |true| if "fin" has been received. | |
| 78 bool fin_received_ = false; | |
| 79 }; | |
| 80 | |
| 81 // Controller for test WebUI. | |
| 82 class TestUI : public WebUIIOSController { | |
| 83 public: | |
| 84 // Constructs controller from |web_ui| and |ui_handler| which will communicate | |
| 85 // with test WebUI page. | |
| 86 TestUI(WebUIIOS* web_ui, TestUIHandler* ui_handler) | |
| 87 : WebUIIOSController(web_ui) { | |
| 88 web::WebUIIOSDataSource* source = | |
| 89 web::WebUIIOSDataSource::Create(kTestWebUIURLHost); | |
| 90 | |
| 91 source->AddResourcePath("mojo_test.js", IDR_MOJO_TEST_JS); | |
| 92 source->AddResourcePath("ios/web/test/mojo_test.mojom", | |
| 93 IDR_MOJO_TEST_MOJO_JS); | |
| 94 source->SetDefaultResource(IDR_MOJO_TEST_HTML); | |
| 95 | |
| 96 web::WebState* web_state = web_ui->GetWebState(); | |
| 97 web::WebUIIOSDataSource::Add(web_state->GetBrowserState(), source); | |
| 98 | |
| 99 web_state->GetMojoInterfaceRegistry()->AddInterface(ui_handler); | |
| 100 } | |
| 101 }; | |
| 102 | |
| 103 // Factory that creates TestUI controller. | |
| 104 class TestWebUIControllerFactory : public WebUIIOSControllerFactory { | |
| 105 public: | |
| 106 // Constructs a controller factory which will eventually create |ui_handler|. | |
| 107 explicit TestWebUIControllerFactory(TestUIHandler* ui_handler) | |
| 108 : ui_handler_(ui_handler) {} | |
| 109 | |
| 110 // WebUIIOSControllerFactory overrides. | |
| 111 WebUIIOSController* CreateWebUIIOSControllerForURL( | |
| 112 WebUIIOS* web_ui, | |
| 113 const GURL& url) const override { | |
| 114 DCHECK_EQ(url.scheme(), kTestWebUIScheme); | |
| 115 DCHECK_EQ(url.host(), kTestWebUIURLHost); | |
| 116 return new TestUI(web_ui, ui_handler_); | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 // UI handler class which communicates with test WebUI page. | |
| 121 TestUIHandler* ui_handler_; | |
| 122 }; | |
| 123 } // namespace | |
| 124 | |
| 125 // A test fixture for verifying mojo comminication for WebUI. | |
| 126 class WebUIMojoTest : public WebIntTest { | |
| 127 protected: | |
| 128 WebUIMojoTest() | |
| 129 : web_state_(new WebStateImpl(GetBrowserState())), | |
| 130 ui_handler_(new TestUIHandler()) { | |
| 131 web_state_->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, 0); | |
| 132 WebUIIOSControllerFactory::RegisterFactory( | |
| 133 new TestWebUIControllerFactory(ui_handler_.get())); | |
| 134 } | |
| 135 | |
| 136 // Returns WebState which loads test WebUI page. | |
| 137 WebStateImpl* web_state() { return web_state_.get(); } | |
| 138 // Returns UI handler which communicates with WebUI page. | |
| 139 TestUIHandler* test_ui_handler() { return ui_handler_.get(); } | |
| 140 | |
| 141 private: | |
| 142 std::unique_ptr<WebStateImpl> web_state_; | |
| 143 std::unique_ptr<TestUIHandler> ui_handler_; | |
| 144 }; | |
| 145 | |
| 146 // Tests that JS can send messages to the native code and vice versa. | |
| 147 // TestUIHandler is used for communication and test suceeds only when | |
| 148 // |TestUIHandler| sucessfully receives "ack" message from WebUI page. | |
| 149 TEST_F(WebUIMojoTest, MessageExchange) { | |
| 150 web_state()->SetWebUsageEnabled(true); | |
| 151 web_state()->GetWebController().useMojoForWebUI = YES; | |
| 152 web_state()->GetView(); // WebState won't load URL without view. | |
| 153 NavigationManager::WebLoadParams load_params(GURL( | |
| 154 url::SchemeHostPort(kTestWebUIScheme, kTestWebUIURLHost, 0).Serialize())); | |
| 155 web_state()->GetNavigationManager()->LoadURLWithParams(load_params); | |
| 156 | |
| 157 // Wait until |TestUIHandler| receives "ack" message from WebUI page. | |
| 158 base::test::ios::WaitUntilCondition(^{ | |
| 159 base::RunLoop().RunUntilIdle(); | |
| 160 return test_ui_handler()->IsFinReceived(); | |
| 161 }); | |
| 162 } | |
| 163 | |
| 164 } // namespace web | |
| OLD | NEW |