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: | |
37 // ios/web/test/data/mojo_test.js | |
38 class TestUIHandler : public TestUIHandlerMojo { | |
39 public: | |
40 TestUIHandler() : fin_received_(false) {} | |
41 ~TestUIHandler() override {} | |
42 | |
43 // Returns true if "fin" has been received. | |
44 bool IsFinReceived() { return fin_received_; } | |
45 | |
46 // TestUIHandlerMojo overrides. | |
47 void HandleJsMessage(const mojo::String& message, TestPagePtr page) override { | |
48 if (message.get() == "syn") { | |
49 // Received "syn" message from WebUI page, send "ack" as reply. | |
Ken Rockot(use gerrit already)
2016/05/26 21:11:51
Can you add some logic to ensure that syn is recei
Eugene But (OOO till 7-30)
2016/05/26 21:52:05
Done.
| |
50 NativeMessageResultMojoPtr result(NativeMessageResultMojo::New()); | |
51 result->message = mojo::String::From("ack"); | |
52 page->HandleNativeMessage(std::move(result)); | |
53 } else if (message.get() == "fin") { | |
54 // Received "fin" from the WebUI page in response to "ack". | |
Ken Rockot(use gerrit already)
2016/05/26 21:11:51
Similarly can you also check that fin_received_ is
Eugene But (OOO till 7-30)
2016/05/26 21:52:05
Done.
| |
55 fin_received_ = true; | |
56 } else { | |
57 NOTREACHED(); | |
58 } | |
59 } | |
60 | |
61 private: | |
62 // |true| if "fin" has been received. | |
63 bool fin_received_; | |
64 }; | |
65 | |
66 // Mojo interface factory for TestUIHandler. | |
67 class TestUIHandlerFactory : public shell::InterfaceFactory<TestUIHandlerMojo> { | |
Ken Rockot(use gerrit already)
2016/05/26 21:11:51
nit: I would just merge this into TestUIHandler. I
Eugene But (OOO till 7-30)
2016/05/26 21:52:05
Done.
| |
68 public: | |
69 // Constructs factory which will create |ui_handler| object. | |
70 explicit TestUIHandlerFactory(TestUIHandler* ui_handler) | |
71 : ui_handler_(ui_handler) {} | |
72 | |
73 private: | |
74 // shell::InterfaceFactory overrides. | |
75 void Create(shell::Connection* connection, | |
76 mojo::InterfaceRequest<TestUIHandlerMojo> request) override { | |
77 bindings_.AddBinding(ui_handler_, std::move(request)); | |
78 } | |
79 | |
80 // Bindings required by shell::InterfaceFactory. | |
81 mojo::BindingSet<TestUIHandlerMojo> bindings_; | |
82 // Object created by this factory. | |
83 TestUIHandler* ui_handler_; | |
84 }; | |
85 | |
86 // Controller for test WebUI. | |
87 class TestUI : public WebUIIOSController { | |
88 public: | |
89 // Constructs controller from |web_ui| and |ui_handler| which will communicate | |
90 // with test WebUI page. | |
91 TestUI(WebUIIOS* web_ui, TestUIHandler* ui_handler) | |
92 : WebUIIOSController(web_ui), | |
93 handler_factory_(new TestUIHandlerFactory(ui_handler)) { | |
94 web::WebUIIOSDataSource* source = | |
95 web::WebUIIOSDataSource::Create(kTestWebUIURLHost); | |
96 | |
97 source->AddResourcePath("mojo_test.js", IDR_MOJO_TEST_JS); | |
98 source->AddResourcePath("ios/web/test/mojo_test.mojom", | |
99 IDR_MOJO_TEST_MOJO_JS); | |
100 source->SetDefaultResource(IDR_MOJO_TEST_HTML); | |
101 | |
102 web::WebState* web_state = web_ui->GetWebState(); | |
103 web::WebUIIOSDataSource::Add(web_state->GetBrowserState(), source); | |
104 | |
105 web_state->GetMojoInterfaceRegistry()->AddInterface(handler_factory_.get()); | |
106 } | |
107 | |
108 private: | |
109 // Mojo interface factory for TestUIHandler. | |
110 std::unique_ptr<TestUIHandlerFactory> handler_factory_; | |
111 }; | |
112 | |
113 // Factory that creates TestUI controller. | |
114 class TestWebUIControllerFactory : public WebUIIOSControllerFactory { | |
115 public: | |
116 // Constructs a controller factory which will eventually create |ui_handler|. | |
117 explicit TestWebUIControllerFactory(TestUIHandler* ui_handler) | |
118 : ui_handler_(ui_handler) {} | |
119 | |
120 // WebUIIOSControllerFactory overrides. | |
121 WebUIIOSController* CreateWebUIIOSControllerForURL( | |
122 WebUIIOS* web_ui, | |
123 const GURL& url) const override { | |
124 DCHECK_EQ(url.scheme(), kTestWebUIScheme); | |
125 DCHECK_EQ(url.host(), kTestWebUIURLHost); | |
126 return new TestUI(web_ui, ui_handler_); | |
127 } | |
128 | |
129 private: | |
130 // UI handler class which communicates with test WebUI page. | |
131 TestUIHandler* ui_handler_; | |
132 }; | |
133 } // namespace | |
134 | |
135 // A test fixture for verifying mojo comminication for WebUI. | |
136 class WebUIMojoTest : public WebIntTest { | |
137 protected: | |
138 WebUIMojoTest() | |
139 : web_state_(new WebStateImpl(GetBrowserState())), | |
140 ui_handler_(new TestUIHandler()) { | |
141 web_state_->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, 0); | |
142 WebUIIOSControllerFactory::RegisterFactory( | |
143 new TestWebUIControllerFactory(ui_handler_.get())); | |
144 } | |
145 | |
146 // Returns WebState which loads test WebUI page. | |
147 WebStateImpl* web_state() { return web_state_.get(); } | |
148 // Returns UI handler which communicates with WebUI page. | |
149 TestUIHandler* test_ui_handler() { return ui_handler_.get(); } | |
150 | |
151 private: | |
152 std::unique_ptr<WebStateImpl> web_state_; | |
153 std::unique_ptr<TestUIHandler> ui_handler_; | |
154 }; | |
155 | |
156 // Tests that JS can send messages to the native code and vice versa. | |
157 // TestUIHandler is used for communication and test suceeds only when | |
158 // |TestUIHandler| sucessfully receives "ack" message from WebUI page. | |
159 TEST_F(WebUIMojoTest, MessageExchange) { | |
160 web_state()->SetWebUsageEnabled(true); | |
161 web_state()->GetWebController().useMojoForWebUI = YES; | |
162 web_state()->GetView(); // WebState won't load URL without view. | |
163 NavigationManager::WebLoadParams load_params(GURL( | |
164 url::SchemeHostPort(kTestWebUIScheme, kTestWebUIURLHost, 0).Serialize())); | |
165 web_state()->GetNavigationManager()->LoadURLWithParams(load_params); | |
166 | |
167 // Wait until |TestUIHandler| receives "ack" message from WebUI page. | |
168 base::test::ios::WaitUntilCondition(^{ | |
169 base::RunLoop().RunUntilIdle(); | |
170 return test_ui_handler()->IsFinReceived(); | |
171 }); | |
172 } | |
173 | |
174 } // namespace web | |
OLD | NEW |