OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "mojo/application/public/cpp/application_connection.h" | 16 #include "mojo/application/public/cpp/application_connection.h" |
17 #include "mojo/application/public/cpp/application_delegate.h" | 17 #include "mojo/application/public/cpp/application_delegate.h" |
18 #include "mojo/application/public/cpp/application_impl.h" | 18 #include "mojo/application/public/cpp/application_impl.h" |
19 #include "mojo/application/public/cpp/interface_factory.h" | 19 #include "mojo/application/public/cpp/interface_factory.h" |
20 #include "mojo/application/public/interfaces/content_handler.mojom.h" | 20 #include "mojo/application/public/interfaces/content_handler.mojom.h" |
21 #include "mojo/application/public/interfaces/service_provider.mojom.h" | 21 #include "mojo/application/public/interfaces/service_provider.mojom.h" |
22 #include "mojo/package_manager/package_manager_impl.h" | |
23 #include "mojo/public/cpp/bindings/strong_binding.h" | 22 #include "mojo/public/cpp/bindings/strong_binding.h" |
24 #include "mojo/shell/application_loader.h" | 23 #include "mojo/shell/application_loader.h" |
25 #include "mojo/shell/application_manager.h" | 24 #include "mojo/shell/application_manager.h" |
26 #include "mojo/shell/connect_util.h" | 25 #include "mojo/shell/connect_util.h" |
27 #include "mojo/shell/fetcher.h" | 26 #include "mojo/shell/fetcher.h" |
| 27 #include "mojo/shell/package_manager/package_manager_impl.h" |
28 #include "mojo/shell/test_package_manager.h" | 28 #include "mojo/shell/test_package_manager.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
30 | 30 |
31 namespace mojo { | 31 namespace mojo { |
32 namespace package_manager { | 32 namespace shell { |
33 namespace test { | 33 namespace test { |
34 namespace { | 34 namespace { |
35 | 35 |
36 const char kTestMimeType[] = "test/mime-type"; | 36 const char kTestMimeType[] = "test/mime-type"; |
37 | 37 |
38 class TestFetcher : public shell::Fetcher { | 38 class TestFetcher : public Fetcher { |
39 public: | 39 public: |
40 TestFetcher(const FetchCallback& fetch_callback, | 40 TestFetcher(const FetchCallback& fetch_callback, |
41 const GURL& url, | 41 const GURL& url, |
42 const std::string& mime_type) | 42 const std::string& mime_type) |
43 : Fetcher(fetch_callback), url_(url), mime_type_(mime_type) { | 43 : Fetcher(fetch_callback), url_(url), mime_type_(mime_type) { |
44 loader_callback_.Run(make_scoped_ptr(this)); | 44 loader_callback_.Run(make_scoped_ptr(this)); |
45 } | 45 } |
46 ~TestFetcher() override {} | 46 ~TestFetcher() override {} |
47 | 47 |
48 // Fetcher: | 48 // Fetcher: |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 destruct_callback.Run(); | 87 destruct_callback.Run(); |
88 } | 88 } |
89 | 89 |
90 private: | 90 private: |
91 StrongBinding<ContentHandler> binding_; | 91 StrongBinding<ContentHandler> binding_; |
92 ScopedVector<ApplicationImpl> apps_; | 92 ScopedVector<ApplicationImpl> apps_; |
93 | 93 |
94 DISALLOW_COPY_AND_ASSIGN(TestContentHandler); | 94 DISALLOW_COPY_AND_ASSIGN(TestContentHandler); |
95 }; | 95 }; |
96 | 96 |
97 class TestApplicationLoader : public shell::ApplicationLoader, | 97 class TestApplicationLoader : public ApplicationLoader, |
98 public ApplicationDelegate, | 98 public ApplicationDelegate, |
99 public InterfaceFactory<ContentHandler> { | 99 public InterfaceFactory<ContentHandler> { |
100 public: | 100 public: |
101 TestApplicationLoader() : num_loads_(0) {} | 101 TestApplicationLoader() : num_loads_(0) {} |
102 ~TestApplicationLoader() override {} | 102 ~TestApplicationLoader() override {} |
103 | 103 |
104 int num_loads() const { return num_loads_; } | 104 int num_loads() const { return num_loads_; } |
105 const GURL& last_requestor_url() const { return last_requestor_url_; } | 105 const GURL& last_requestor_url() const { return last_requestor_url_; } |
106 | 106 |
107 private: | 107 private: |
(...skipping 16 matching lines...) Expand all Loading... |
124 new TestContentHandler(connection, std::move(request)); | 124 new TestContentHandler(connection, std::move(request)); |
125 } | 125 } |
126 | 126 |
127 scoped_ptr<ApplicationImpl> test_app_; | 127 scoped_ptr<ApplicationImpl> test_app_; |
128 int num_loads_; | 128 int num_loads_; |
129 GURL last_requestor_url_; | 129 GURL last_requestor_url_; |
130 | 130 |
131 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); | 131 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); |
132 }; | 132 }; |
133 | 133 |
134 class TestPackageManager : public PackageManagerImpl { | 134 class TestPackageManagerImpl : public PackageManagerImpl { |
135 public: | 135 public: |
136 explicit TestPackageManager(const base::FilePath& package_path) | 136 explicit TestPackageManagerImpl(const base::FilePath& package_path) |
137 : PackageManagerImpl(package_path, nullptr), | 137 : PackageManagerImpl(package_path, nullptr), |
138 mime_type_(kTestMimeType) {} | 138 mime_type_(kTestMimeType) {} |
139 ~TestPackageManager() override {} | 139 ~TestPackageManagerImpl() override {} |
140 | 140 |
141 void set_mime_type(const std::string& mime_type) { | 141 void set_mime_type(const std::string& mime_type) { |
142 mime_type_ = mime_type; | 142 mime_type_ = mime_type; |
143 } | 143 } |
144 | 144 |
145 // PackageManagerImpl: | 145 // PackageManagerImpl: |
146 void FetchRequest( | 146 void FetchRequest( |
147 URLRequestPtr request, | 147 URLRequestPtr request, |
148 const shell::Fetcher::FetchCallback& loader_callback) override { | 148 const Fetcher::FetchCallback& loader_callback) override { |
149 new TestFetcher(loader_callback, GURL(request->url), mime_type_); | 149 new TestFetcher(loader_callback, GURL(request->url), mime_type_); |
150 } | 150 } |
151 | 151 |
152 private: | 152 private: |
153 std::string mime_type_; | 153 std::string mime_type_; |
154 | 154 |
155 DISALLOW_COPY_AND_ASSIGN(TestPackageManager); | 155 DISALLOW_COPY_AND_ASSIGN(TestPackageManagerImpl); |
156 }; | 156 }; |
157 | 157 |
158 } // namespace | 158 } // namespace |
159 | 159 |
160 class ContentHandlerTest : public testing::Test { | 160 class ContentHandlerTest : public testing::Test { |
161 public: | 161 public: |
162 ContentHandlerTest() | 162 ContentHandlerTest() |
163 : content_handler_url_("http://test.content.handler"), | 163 : content_handler_url_("http://test.content.handler"), |
164 requestor_url_("http://requestor.url") {} | 164 requestor_url_("http://requestor.url") {} |
165 ~ContentHandlerTest() override {} | 165 ~ContentHandlerTest() override {} |
166 | 166 |
167 void SetUp() override { | 167 void SetUp() override { |
168 base::FilePath shell_dir; | 168 base::FilePath shell_dir; |
169 PathService::Get(base::DIR_MODULE, &shell_dir); | 169 PathService::Get(base::DIR_MODULE, &shell_dir); |
170 test_package_manager_ = new TestPackageManager(shell_dir); | 170 test_package_manager_ = new TestPackageManagerImpl(shell_dir); |
171 test_package_manager_->RegisterContentHandler(kTestMimeType, | 171 test_package_manager_->RegisterContentHandler(kTestMimeType, |
172 content_handler_url_); | 172 content_handler_url_); |
173 application_manager_.reset(new shell::ApplicationManager( | 173 application_manager_.reset(new ApplicationManager( |
174 make_scoped_ptr(test_package_manager_))); | 174 make_scoped_ptr(test_package_manager_))); |
175 } | 175 } |
176 | 176 |
177 void TearDown() override { | 177 void TearDown() override { |
178 test_package_manager_ = nullptr; | 178 test_package_manager_ = nullptr; |
179 application_manager_.reset(); | 179 application_manager_.reset(); |
180 } | 180 } |
181 | 181 |
182 protected: | 182 protected: |
183 const GURL content_handler_url_; | 183 const GURL content_handler_url_; |
184 const GURL requestor_url_; | 184 const GURL requestor_url_; |
185 | 185 |
186 base::MessageLoop loop_; | 186 base::MessageLoop loop_; |
187 scoped_ptr<shell::ApplicationManager> application_manager_; | 187 scoped_ptr<ApplicationManager> application_manager_; |
188 // Owned by ApplicationManager. | 188 // Owned by ApplicationManager. |
189 TestPackageManager* test_package_manager_; | 189 TestPackageManagerImpl* test_package_manager_; |
190 | 190 |
191 DISALLOW_COPY_AND_ASSIGN(ContentHandlerTest); | 191 DISALLOW_COPY_AND_ASSIGN(ContentHandlerTest); |
192 }; | 192 }; |
193 | 193 |
194 TEST_F(ContentHandlerTest, ContentHandlerConnectionGetsRequestorURL) { | 194 TEST_F(ContentHandlerTest, ContentHandlerConnectionGetsRequestorURL) { |
195 TestApplicationLoader* loader = new TestApplicationLoader; | 195 TestApplicationLoader* loader = new TestApplicationLoader; |
196 application_manager_->SetLoaderForURL( | 196 application_manager_->SetLoaderForURL( |
197 scoped_ptr<shell::ApplicationLoader>(loader), | 197 scoped_ptr<ApplicationLoader>(loader), |
198 content_handler_url_); | 198 content_handler_url_); |
199 | 199 |
200 bool called = false; | 200 bool called = false; |
201 scoped_ptr<shell::ConnectToApplicationParams> params( | 201 scoped_ptr<ConnectToApplicationParams> params(new ConnectToApplicationParams); |
202 new shell::ConnectToApplicationParams); | 202 params->set_source(Identity(requestor_url_)); |
203 params->set_source(shell::Identity(requestor_url_)); | |
204 params->SetTargetURL(GURL("test:test")); | 203 params->SetTargetURL(GURL("test:test")); |
205 params->set_on_application_end( | 204 params->set_on_application_end( |
206 base::Bind(&QuitClosure, base::Unretained(&called))); | 205 base::Bind(&QuitClosure, base::Unretained(&called))); |
207 application_manager_->ConnectToApplication(std::move(params)); | 206 application_manager_->ConnectToApplication(std::move(params)); |
208 loop_.Run(); | 207 loop_.Run(); |
209 EXPECT_TRUE(called); | 208 EXPECT_TRUE(called); |
210 | 209 |
211 ASSERT_EQ(1, loader->num_loads()); | 210 ASSERT_EQ(1, loader->num_loads()); |
212 EXPECT_EQ(requestor_url_, loader->last_requestor_url()); | 211 EXPECT_EQ(requestor_url_, loader->last_requestor_url()); |
213 } | 212 } |
214 | 213 |
215 TEST_F(ContentHandlerTest, | 214 TEST_F(ContentHandlerTest, |
216 MultipleConnectionsToContentHandlerGetSameContentHandlerId) { | 215 MultipleConnectionsToContentHandlerGetSameContentHandlerId) { |
217 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; | 216 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; |
218 application_manager_->SetLoaderForURL( | 217 application_manager_->SetLoaderForURL( |
219 scoped_ptr<shell::ApplicationLoader>(content_handler_loader), | 218 scoped_ptr<ApplicationLoader>(content_handler_loader), |
220 content_handler_url_); | 219 content_handler_url_); |
221 | 220 |
222 uint32_t content_handler_id; | 221 uint32_t content_handler_id; |
223 { | 222 { |
224 base::RunLoop run_loop; | 223 base::RunLoop run_loop; |
225 scoped_ptr<shell::ConnectToApplicationParams> params( | 224 scoped_ptr<ConnectToApplicationParams> params( |
226 new shell::ConnectToApplicationParams); | 225 new ConnectToApplicationParams); |
227 params->set_source(shell::Identity(requestor_url_)); | 226 params->set_source(Identity(requestor_url_)); |
228 params->SetTargetURL(GURL("test:test")); | 227 params->SetTargetURL(GURL("test:test")); |
229 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) { | 228 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) { |
230 content_handler_id = t; | 229 content_handler_id = t; |
231 run_loop.Quit(); | 230 run_loop.Quit(); |
232 }); | 231 }); |
233 application_manager_->ConnectToApplication(std::move(params)); | 232 application_manager_->ConnectToApplication(std::move(params)); |
234 run_loop.Run(); | 233 run_loop.Run(); |
235 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id); | 234 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id); |
236 } | 235 } |
237 | 236 |
238 uint32_t content_handler_id2; | 237 uint32_t content_handler_id2; |
239 { | 238 { |
240 base::RunLoop run_loop; | 239 base::RunLoop run_loop; |
241 scoped_ptr<shell::ConnectToApplicationParams> params( | 240 scoped_ptr<ConnectToApplicationParams> params( |
242 new shell::ConnectToApplicationParams); | 241 new ConnectToApplicationParams); |
243 params->set_source(shell::Identity(requestor_url_)); | 242 params->set_source(Identity(requestor_url_)); |
244 params->SetTargetURL(GURL("test:test")); | 243 params->SetTargetURL(GURL("test:test")); |
245 params->set_connect_callback([&content_handler_id2, &run_loop](uint32_t t) { | 244 params->set_connect_callback([&content_handler_id2, &run_loop](uint32_t t) { |
246 content_handler_id2 = t; | 245 content_handler_id2 = t; |
247 run_loop.Quit(); | 246 run_loop.Quit(); |
248 }); | 247 }); |
249 application_manager_->ConnectToApplication(std::move(params)); | 248 application_manager_->ConnectToApplication(std::move(params)); |
250 run_loop.Run(); | 249 run_loop.Run(); |
251 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2); | 250 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2); |
252 } | 251 } |
253 EXPECT_EQ(content_handler_id, content_handler_id2); | 252 EXPECT_EQ(content_handler_id, content_handler_id2); |
254 } | 253 } |
255 | 254 |
256 TEST_F(ContentHandlerTest, DifferedContentHandlersGetDifferentIDs) { | 255 TEST_F(ContentHandlerTest, DifferedContentHandlersGetDifferentIDs) { |
257 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; | 256 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; |
258 application_manager_->SetLoaderForURL( | 257 application_manager_->SetLoaderForURL( |
259 scoped_ptr<shell::ApplicationLoader>(content_handler_loader), | 258 scoped_ptr<ApplicationLoader>(content_handler_loader), |
260 content_handler_url_); | 259 content_handler_url_); |
261 | 260 |
262 uint32_t content_handler_id; | 261 uint32_t content_handler_id; |
263 { | 262 { |
264 base::RunLoop run_loop; | 263 base::RunLoop run_loop; |
265 scoped_ptr<shell::ConnectToApplicationParams> params( | 264 scoped_ptr<ConnectToApplicationParams> params( |
266 new shell::ConnectToApplicationParams); | 265 new ConnectToApplicationParams); |
267 params->set_source(shell::Identity(requestor_url_)); | 266 params->set_source(Identity(requestor_url_)); |
268 params->SetTargetURL(GURL("test:test")); | 267 params->SetTargetURL(GURL("test:test")); |
269 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) { | 268 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) { |
270 content_handler_id = t; | 269 content_handler_id = t; |
271 run_loop.Quit(); | 270 run_loop.Quit(); |
272 }); | 271 }); |
273 application_manager_->ConnectToApplication(std::move(params)); | 272 application_manager_->ConnectToApplication(std::move(params)); |
274 run_loop.Run(); | 273 run_loop.Run(); |
275 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id); | 274 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id); |
276 } | 275 } |
277 | 276 |
278 const std::string mime_type2 = "test/mime-type2"; | 277 const std::string mime_type2 = "test/mime-type2"; |
279 const GURL content_handler_url2("http://test.content.handler2"); | 278 const GURL content_handler_url2("http://test.content.handler2"); |
280 test_package_manager_->set_mime_type(mime_type2); | 279 test_package_manager_->set_mime_type(mime_type2); |
281 test_package_manager_->RegisterContentHandler(mime_type2, | 280 test_package_manager_->RegisterContentHandler(mime_type2, |
282 content_handler_url2); | 281 content_handler_url2); |
283 | 282 |
284 TestApplicationLoader* content_handler_loader2 = new TestApplicationLoader; | 283 TestApplicationLoader* content_handler_loader2 = new TestApplicationLoader; |
285 application_manager_->SetLoaderForURL( | 284 application_manager_->SetLoaderForURL( |
286 scoped_ptr<shell::ApplicationLoader>(content_handler_loader2), | 285 scoped_ptr<ApplicationLoader>(content_handler_loader2), |
287 content_handler_url2); | 286 content_handler_url2); |
288 | 287 |
289 uint32_t content_handler_id2; | 288 uint32_t content_handler_id2; |
290 { | 289 { |
291 base::RunLoop run_loop; | 290 base::RunLoop run_loop; |
292 scoped_ptr<shell::ConnectToApplicationParams> params( | 291 scoped_ptr<ConnectToApplicationParams> params( |
293 new shell::ConnectToApplicationParams); | 292 new ConnectToApplicationParams); |
294 params->set_source(shell::Identity(requestor_url_)); | 293 params->set_source(Identity(requestor_url_)); |
295 params->SetTargetURL(GURL("test2:test2")); | 294 params->SetTargetURL(GURL("test2:test2")); |
296 params->set_connect_callback([&content_handler_id2, &run_loop](uint32_t t) { | 295 params->set_connect_callback([&content_handler_id2, &run_loop](uint32_t t) { |
297 content_handler_id2 = t; | 296 content_handler_id2 = t; |
298 run_loop.Quit(); | 297 run_loop.Quit(); |
299 }); | 298 }); |
300 application_manager_->ConnectToApplication(std::move(params)); | 299 application_manager_->ConnectToApplication(std::move(params)); |
301 run_loop.Run(); | 300 run_loop.Run(); |
302 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2); | 301 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2); |
303 } | 302 } |
304 EXPECT_NE(content_handler_id, content_handler_id2); | 303 EXPECT_NE(content_handler_id, content_handler_id2); |
305 } | 304 } |
306 | 305 |
307 TEST_F(ContentHandlerTest, | 306 TEST_F(ContentHandlerTest, |
308 ConnectWithNoContentHandlerGetsInvalidContentHandlerId) { | 307 ConnectWithNoContentHandlerGetsInvalidContentHandlerId) { |
309 application_manager_->SetLoaderForURL( | 308 application_manager_->SetLoaderForURL( |
310 scoped_ptr<shell::ApplicationLoader>(new TestApplicationLoader), | 309 scoped_ptr<ApplicationLoader>(new TestApplicationLoader), |
311 GURL("test:test")); | 310 GURL("test:test")); |
312 | 311 |
313 uint32_t content_handler_id = 1u; | 312 uint32_t content_handler_id = 1u; |
314 scoped_ptr<shell::ConnectToApplicationParams> params( | 313 scoped_ptr<ConnectToApplicationParams> params( |
315 new shell::ConnectToApplicationParams); | 314 new ConnectToApplicationParams); |
316 params->SetTargetURL(GURL("test:test")); | 315 params->SetTargetURL(GURL("test:test")); |
317 params->set_connect_callback( | 316 params->set_connect_callback( |
318 [&content_handler_id](uint32_t t) { content_handler_id = t; }); | 317 [&content_handler_id](uint32_t t) { content_handler_id = t; }); |
319 application_manager_->ConnectToApplication(std::move(params)); | 318 application_manager_->ConnectToApplication(std::move(params)); |
320 EXPECT_EQ(0u, content_handler_id); | 319 EXPECT_EQ(0u, content_handler_id); |
321 } | 320 } |
322 | 321 |
323 } // namespace test | 322 } // namespace test |
324 } // namespace package_manager | 323 } // namespace package_manager |
325 } // namespace mojo | 324 } // namespace mojo |
OLD | NEW |