OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <limits> | 5 #include <limits> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "grit/content_resources.h" | 25 #include "grit/content_resources.h" |
26 #include "mojo/common/test/test_utils.h" | 26 #include "mojo/common/test/test_utils.h" |
27 #include "mojo/public/cpp/bindings/allocation_scope.h" | 27 #include "mojo/public/cpp/bindings/allocation_scope.h" |
28 #include "mojo/public/cpp/bindings/remote_ptr.h" | 28 #include "mojo/public/cpp/bindings/remote_ptr.h" |
29 #include "mojo/public/js/bindings/constants.h" | 29 #include "mojo/public/js/bindings/constants.h" |
30 | 30 |
31 namespace content { | 31 namespace content { |
32 namespace { | 32 namespace { |
33 | 33 |
34 bool got_message = false; | 34 bool got_message = false; |
35 int message_count = 0; | |
36 | |
37 const int kExpectedMessageCount = 100; | |
38 | |
39 // Negative numbers with different values in each byte, the last of | |
40 // which can survive promotion to double and back. | |
41 const int8 kExpectedInt8Value = -65; | |
42 const int16 kExpectedInt16Value = -16961; | |
43 const int32 kExpectedInt32Value = -1145258561; | |
44 const int64 kExpectedInt64Value = -77263311946305LL; | |
45 | |
46 // Positive numbers with different values in each byte, the last of | |
47 // which can survive promotion to double and back. | |
48 const uint8 kExpectedUInt8Value = 65; | |
49 const uint16 kExpectedUInt16Value = 16961; | |
50 const uint32 kExpectedUInt32Value = 1145258561; | |
51 const uint64 kExpectedUInt64Value = 77263311946305LL; | |
52 | |
53 // Double/float values, including special case constants. | |
54 const double kExpectedDoubleVal = 3.14159265358979323846; | |
55 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); | |
56 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); | |
57 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); | |
58 const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); | |
59 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); | |
60 | |
61 // NaN has the property that it is not equal to itself. | |
62 #define EXPECT_NAN(x) EXPECT_NE(x, x) | |
63 | 35 |
64 // The bindings for the page are generated from a .mojom file. This code looks | 36 // The bindings for the page are generated from a .mojom file. This code looks |
65 // up the generated file from disk and returns it. | 37 // up the generated file from disk and returns it. |
66 bool GetResource(const std::string& id, | 38 bool GetResource(const std::string& id, |
67 const WebUIDataSource::GotDataCallback& callback) { | 39 const WebUIDataSource::GotDataCallback& callback) { |
68 // These are handled by the WebUIDataSource that AddMojoDataSource() creates. | 40 // These are handled by the WebUIDataSource that AddMojoDataSource() creates. |
69 if (id == mojo::kCodecModuleName || | 41 if (id == mojo::kCodecModuleName || |
70 id == mojo::kConnectionModuleName || | 42 id == mojo::kConnectionModuleName || |
71 id == mojo::kConnectorModuleName || | 43 id == mojo::kConnectorModuleName || |
72 id == mojo::kRouterModuleName) | 44 id == mojo::kRouterModuleName) |
(...skipping 17 matching lines...) Expand all Loading... |
90 run_loop_(run_loop) { | 62 run_loop_(run_loop) { |
91 } | 63 } |
92 | 64 |
93 virtual ~BrowserTargetImpl() {} | 65 virtual ~BrowserTargetImpl() {} |
94 | 66 |
95 // mojo::BrowserTarget overrides: | 67 // mojo::BrowserTarget overrides: |
96 virtual void PingResponse() OVERRIDE { | 68 virtual void PingResponse() OVERRIDE { |
97 NOTREACHED(); | 69 NOTREACHED(); |
98 } | 70 } |
99 | 71 |
100 virtual void EchoResponse(const mojo::EchoArgs& arg1, | |
101 const mojo::EchoArgs& arg2) OVERRIDE { | |
102 NOTREACHED(); | |
103 } | |
104 | |
105 protected: | 72 protected: |
106 mojo::RemotePtr<mojo::RendererTarget> client_; | 73 mojo::RemotePtr<mojo::RendererTarget> client_; |
107 base::RunLoop* run_loop_; | 74 base::RunLoop* run_loop_; |
108 | 75 |
109 private: | 76 private: |
110 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); | 77 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); |
111 }; | 78 }; |
112 | 79 |
113 class PingBrowserTargetImpl : public BrowserTargetImpl { | 80 class PingBrowserTargetImpl : public BrowserTargetImpl { |
114 public: | 81 public: |
115 PingBrowserTargetImpl(mojo::ScopedRendererTargetHandle handle, | 82 PingBrowserTargetImpl(mojo::ScopedRendererTargetHandle handle, |
116 base::RunLoop* run_loop) | 83 base::RunLoop* run_loop) |
117 : BrowserTargetImpl(handle, run_loop) { | 84 : BrowserTargetImpl(handle, run_loop) { |
118 client_->Ping(); | 85 client_->Ping(); |
119 } | 86 } |
120 | 87 |
121 virtual ~PingBrowserTargetImpl() {} | 88 virtual ~PingBrowserTargetImpl() {} |
122 | 89 |
123 // mojo::BrowserTarget overrides: | 90 // mojo::BrowserTarget overrides: |
124 // Quit the RunLoop when called. | 91 // Quit the RunLoop when called. |
125 virtual void PingResponse() OVERRIDE { | 92 virtual void PingResponse() OVERRIDE { |
126 got_message = true; | 93 got_message = true; |
127 run_loop_->Quit(); | 94 run_loop_->Quit(); |
128 } | 95 } |
129 | 96 |
130 private: | 97 private: |
131 DISALLOW_COPY_AND_ASSIGN(PingBrowserTargetImpl); | 98 DISALLOW_COPY_AND_ASSIGN(PingBrowserTargetImpl); |
132 }; | 99 }; |
133 | 100 |
134 class EchoBrowserTargetImpl : public BrowserTargetImpl { | |
135 public: | |
136 EchoBrowserTargetImpl(mojo::ScopedRendererTargetHandle handle, | |
137 base::RunLoop* run_loop) | |
138 : BrowserTargetImpl(handle, run_loop) { | |
139 mojo::AllocationScope scope; | |
140 mojo::EchoArgs::Builder builder; | |
141 builder.set_si64(kExpectedInt64Value); | |
142 builder.set_si32(kExpectedInt32Value); | |
143 builder.set_si16(kExpectedInt16Value); | |
144 builder.set_si8(kExpectedInt8Value); | |
145 builder.set_ui64(kExpectedUInt64Value); | |
146 builder.set_ui32(kExpectedUInt32Value); | |
147 builder.set_ui16(kExpectedUInt16Value); | |
148 builder.set_ui8(kExpectedUInt8Value); | |
149 builder.set_float_val(kExpectedFloatVal); | |
150 builder.set_float_inf(kExpectedFloatInf); | |
151 builder.set_float_nan(kExpectedFloatNan); | |
152 builder.set_double_val(kExpectedDoubleVal); | |
153 builder.set_double_inf(kExpectedDoubleInf); | |
154 builder.set_double_nan(kExpectedDoubleNan); | |
155 builder.set_name("coming"); | |
156 mojo::Array<mojo::String>::Builder string_array(3); | |
157 string_array[0] = "one"; | |
158 string_array[1] = "two"; | |
159 string_array[2] = "three"; | |
160 builder.set_string_array(string_array.Finish()); | |
161 client_->Echo(builder.Finish()); | |
162 } | |
163 | |
164 virtual ~EchoBrowserTargetImpl() {} | |
165 | |
166 // mojo::BrowserTarget overrides: | |
167 // Check the response, and quit the RunLoop after N calls. | |
168 virtual void EchoResponse(const mojo::EchoArgs& arg1, | |
169 const mojo::EchoArgs& arg2) OVERRIDE { | |
170 EXPECT_EQ(kExpectedInt64Value, arg1.si64()); | |
171 EXPECT_EQ(kExpectedInt32Value, arg1.si32()); | |
172 EXPECT_EQ(kExpectedInt16Value, arg1.si16()); | |
173 EXPECT_EQ(kExpectedInt8Value, arg1.si8()); | |
174 EXPECT_EQ(kExpectedUInt64Value, arg1.ui64()); | |
175 EXPECT_EQ(kExpectedUInt32Value, arg1.ui32()); | |
176 EXPECT_EQ(kExpectedUInt16Value, arg1.ui16()); | |
177 EXPECT_EQ(kExpectedUInt8Value, arg1.ui8()); | |
178 EXPECT_EQ(kExpectedFloatVal, arg1.float_val()); | |
179 EXPECT_EQ(kExpectedFloatInf, arg1.float_inf()); | |
180 EXPECT_NAN(arg1.float_nan()); | |
181 EXPECT_EQ(kExpectedDoubleVal, arg1.double_val()); | |
182 EXPECT_EQ(kExpectedDoubleInf, arg1.double_inf()); | |
183 EXPECT_NAN(arg1.double_nan()); | |
184 EXPECT_EQ(std::string("coming"), arg1.name().To<std::string>()); | |
185 EXPECT_EQ(std::string("one"), arg1.string_array()[0].To<std::string>()); | |
186 EXPECT_EQ(std::string("two"), arg1.string_array()[1].To<std::string>()); | |
187 EXPECT_EQ(std::string("three"), arg1.string_array()[2].To<std::string>()); | |
188 | |
189 EXPECT_EQ(-1, arg2.si64()); | |
190 EXPECT_EQ(-1, arg2.si32()); | |
191 EXPECT_EQ(-1, arg2.si16()); | |
192 EXPECT_EQ(-1, arg2.si8()); | |
193 EXPECT_EQ(std::string("going"), arg2.name().To<std::string>()); | |
194 | |
195 message_count += 1; | |
196 if (message_count == kExpectedMessageCount) | |
197 run_loop_->Quit(); | |
198 } | |
199 | |
200 private: | |
201 DISALLOW_COPY_AND_ASSIGN(EchoBrowserTargetImpl); | |
202 }; | |
203 | |
204 // WebUIController that sets up mojo bindings. | 101 // WebUIController that sets up mojo bindings. |
205 class TestWebUIController : public WebUIController { | 102 class TestWebUIController : public WebUIController { |
206 public: | 103 public: |
207 TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) | 104 TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) |
208 : WebUIController(web_ui), | 105 : WebUIController(web_ui), |
209 run_loop_(run_loop) { | 106 run_loop_(run_loop) { |
210 content::WebUIDataSource* data_source = | 107 content::WebUIDataSource* data_source = |
211 WebUIDataSource::AddMojoDataSource( | 108 WebUIDataSource::AddMojoDataSource( |
212 web_ui->GetWebContents()->GetBrowserContext()); | 109 web_ui->GetWebContents()->GetBrowserContext()); |
213 data_source->SetRequestFilter(base::Bind(&GetResource)); | 110 data_source->SetRequestFilter(base::Bind(&GetResource)); |
(...skipping 21 matching lines...) Expand all Loading... |
235 browser_target_.reset(new PingBrowserTargetImpl( | 132 browser_target_.reset(new PingBrowserTargetImpl( |
236 pipe.handle_to_peer.Pass(), run_loop_)); | 133 pipe.handle_to_peer.Pass(), run_loop_)); |
237 render_view_host->SetWebUIHandle( | 134 render_view_host->SetWebUIHandle( |
238 mojo::ScopedMessagePipeHandle(pipe.handle_to_self.release())); | 135 mojo::ScopedMessagePipeHandle(pipe.handle_to_self.release())); |
239 } | 136 } |
240 | 137 |
241 private: | 138 private: |
242 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController); | 139 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController); |
243 }; | 140 }; |
244 | 141 |
245 // TestWebUIController that additionally creates the echo test BrowserTarget | |
246 // implementation at the right time. | |
247 class EchoTestWebUIController : public TestWebUIController { | |
248 public: | |
249 EchoTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) | |
250 : TestWebUIController(web_ui, run_loop) { | |
251 } | |
252 | |
253 // WebUIController overrides: | |
254 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE { | |
255 mojo::InterfacePipe<mojo::BrowserTarget, mojo::RendererTarget> pipe; | |
256 browser_target_.reset(new EchoBrowserTargetImpl( | |
257 pipe.handle_to_peer.Pass(), run_loop_)); | |
258 render_view_host->SetWebUIHandle( | |
259 mojo::ScopedMessagePipeHandle(pipe.handle_to_self.release())); | |
260 } | |
261 | |
262 private: | |
263 DISALLOW_COPY_AND_ASSIGN(EchoTestWebUIController); | |
264 }; | |
265 | |
266 // WebUIControllerFactory that creates TestWebUIController. | 142 // WebUIControllerFactory that creates TestWebUIController. |
267 class TestWebUIControllerFactory : public WebUIControllerFactory { | 143 class TestWebUIControllerFactory : public WebUIControllerFactory { |
268 public: | 144 public: |
269 TestWebUIControllerFactory() : run_loop_(NULL) {} | 145 TestWebUIControllerFactory() : run_loop_(NULL) {} |
270 | 146 |
271 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | 147 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
272 | 148 |
273 virtual WebUIController* CreateWebUIControllerForURL( | 149 virtual WebUIController* CreateWebUIControllerForURL( |
274 WebUI* web_ui, const GURL& url) const OVERRIDE { | 150 WebUI* web_ui, const GURL& url) const OVERRIDE { |
275 if (url.query() == "ping") | 151 if (url.query() == "ping") |
276 return new PingTestWebUIController(web_ui, run_loop_); | 152 return new PingTestWebUIController(web_ui, run_loop_); |
277 if (url.query() == "echo") | |
278 return new EchoTestWebUIController(web_ui, run_loop_); | |
279 return NULL; | 153 return NULL; |
280 } | 154 } |
281 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context, | 155 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context, |
282 const GURL& url) const OVERRIDE { | 156 const GURL& url) const OVERRIDE { |
283 return reinterpret_cast<WebUI::TypeID>(1); | 157 return reinterpret_cast<WebUI::TypeID>(1); |
284 } | 158 } |
285 virtual bool UseWebUIForURL(BrowserContext* browser_context, | 159 virtual bool UseWebUIForURL(BrowserContext* browser_context, |
286 const GURL& url) const OVERRIDE { | 160 const GURL& url) const OVERRIDE { |
287 return true; | 161 return true; |
288 } | 162 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 ASSERT_TRUE(test_server()->Start()); | 208 ASSERT_TRUE(test_server()->Start()); |
335 base::RunLoop run_loop; | 209 base::RunLoop run_loop; |
336 factory()->set_run_loop(&run_loop); | 210 factory()->set_run_loop(&run_loop); |
337 GURL test_url(test_server()->GetURL("files/web_ui_mojo.html?ping")); | 211 GURL test_url(test_server()->GetURL("files/web_ui_mojo.html?ping")); |
338 NavigateToURL(shell(), test_url); | 212 NavigateToURL(shell(), test_url); |
339 // RunLoop is quit when message received from page. | 213 // RunLoop is quit when message received from page. |
340 run_loop.Run(); | 214 run_loop.Run(); |
341 EXPECT_TRUE(got_message); | 215 EXPECT_TRUE(got_message); |
342 } | 216 } |
343 | 217 |
344 // Loads a webui page that contains mojo bindings and verifies that | |
345 // parameters are passed back correctly from JavaScript. | |
346 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndEcho) { | |
347 // Currently there is no way to have a generated file included in the isolate | |
348 // files. If the bindings file doesn't exist assume we're on such a bot and | |
349 // pass. | |
350 // TODO(sky): remove this conditional when isolates support copying from gen. | |
351 const base::FilePath test_file_path( | |
352 mojo::test::GetFilePathForJSResource( | |
353 "content/test/data/web_ui_test_mojo_bindings.mojom")); | |
354 if (!base::PathExists(test_file_path)) { | |
355 LOG(WARNING) << " mojom binding file doesn't exist, assuming on isolate"; | |
356 return; | |
357 } | |
358 | |
359 message_count = 0; | |
360 ASSERT_TRUE(test_server()->Start()); | |
361 base::RunLoop run_loop; | |
362 factory()->set_run_loop(&run_loop); | |
363 GURL test_url(test_server()->GetURL("files/web_ui_mojo.html?echo")); | |
364 NavigateToURL(shell(), test_url); | |
365 // RunLoop is quit when response received from page. | |
366 run_loop.Run(); | |
367 EXPECT_EQ(kExpectedMessageCount, message_count); | |
368 } | |
369 | |
370 } // namespace | 218 } // namespace |
371 } // namespace content | 219 } // namespace content |
OLD | NEW |