Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: chrome/browser/intents/web_intents_registry_unittest.cc

Issue 12225076: Delete most web intents code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "base/bind.h"
6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/json/json_file_value_serializer.h"
9 #include "base/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/extensions/test_extension_service.h"
14 #include "chrome/browser/intents/default_web_intent_service.h"
15 #include "chrome/browser/intents/web_intents_registry.h"
16 #include "chrome/browser/webdata/web_data_service.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/extensions/extension.h"
19 #include "chrome/common/extensions/extension_manifest_constants.h"
20 #include "chrome/common/extensions/extension_set.h"
21 #include "chrome/common/extensions/manifest.h"
22 #include "chrome/common/extensions/manifest_handler.h"
23 #include "chrome/common/extensions/web_intents_handler.h"
24 #include "content/public/test/test_browser_thread.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 using content::BrowserThread;
29 using extensions::Extension;
30 using extensions::Manifest;
31 using webkit_glue::WebIntentServiceData;
32
33 class MockExtensionService: public TestExtensionService {
34 public:
35 virtual ~MockExtensionService() {}
36 MOCK_CONST_METHOD0(extensions, const ExtensionSet*());
37 MOCK_CONST_METHOD2(GetExtensionById,
38 const Extension*(const std::string&, bool));
39 MOCK_CONST_METHOD1(GetInstalledExtension,
40 const Extension*(const std::string& id));
41 };
42
43 namespace {
44
45 // TODO(groby): Unify loading functions with extension_manifest_unittest code.
46 DictionaryValue* LoadManifestFile(const base::FilePath& path,
47 std::string* error) {
48 EXPECT_TRUE(file_util::PathExists(path));
49 JSONFileValueSerializer serializer(path);
50 return static_cast<DictionaryValue*>(serializer.Deserialize(NULL, error));
51 }
52
53 scoped_refptr<Extension> LoadExtensionWithLocation(
54 const std::string& name,
55 Manifest::Location location,
56 std::string* error) {
57 base::FilePath path;
58 PathService::Get(chrome::DIR_TEST_DATA, &path);
59 path = path.AppendASCII("extensions")
60 .AppendASCII("manifest_tests")
61 .AppendASCII(name.c_str());
62 scoped_ptr<DictionaryValue> value(LoadManifestFile(path, error));
63 if (!value.get())
64 return NULL;
65 return Extension::Create(path.DirName(),
66 location,
67 *value,
68 Extension::NO_FLAGS,
69 Extension::GenerateIdForPath(path),
70 error);
71 }
72
73 scoped_refptr<Extension> LoadExtension(const std::string& name,
74 std::string* error) {
75 return LoadExtensionWithLocation(name, Manifest::INTERNAL, error);
76 }
77
78 scoped_refptr<Extension> LoadAndExpectSuccess(const std::string& name) {
79 std::string error;
80 scoped_refptr<Extension> extension = LoadExtension(name, &error);
81 EXPECT_TRUE(extension) << name;
82 EXPECT_EQ("", error) << name;
83 return extension;
84 }
85
86 } // namespace
87
88 class WebIntentsRegistryTest : public testing::Test {
89 public:
90 WebIntentsRegistryTest()
91 : ui_thread_(BrowserThread::UI, &message_loop_),
92 db_thread_(BrowserThread::DB) {}
93
94 protected:
95 virtual void SetUp() {
96 db_thread_.Start();
97 wds_ = new WebDataService();
98 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
99 wds_->Init(temp_dir_.path());
100 registry_.Initialize(wds_, &extension_service_);
101 EXPECT_CALL(extension_service_, extensions()).
102 WillRepeatedly(testing::Return(&extensions_));
103 EXPECT_CALL(extension_service_, GetExtensionById(testing::_, testing::_)).
104 WillRepeatedly(
105 testing::Invoke(this, &WebIntentsRegistryTest::GetExtensionById));
106 extensions::ManifestHandler::Register(
107 extension_manifest_keys::kIntents,
108 make_linked_ptr(new extensions::WebIntentsHandler));
109 }
110
111 virtual void TearDown() {
112 // Clear all references to wds to force it destruction.
113 wds_->ShutdownOnUIThread();
114 wds_ = NULL;
115
116 // Schedule another task on the DB thread to notify us that it's safe to
117 // carry on with the test.
118 base::WaitableEvent done(false, false);
119 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
120 base::Bind(&base::WaitableEvent::Signal,
121 base::Unretained(&done)));
122 done.Wait();
123 db_thread_.Stop();
124 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
125 MessageLoop::current()->Run();
126 }
127
128 const Extension* GetExtensionById(const std::string& extension_id,
129 testing::Unused) {
130 for (ExtensionSet::const_iterator iter = extensions_.begin();
131 iter != extensions_.end(); ++iter) {
132 if ((*iter)->id() == extension_id)
133 return &**iter;
134 }
135
136 return NULL;
137 }
138
139 MessageLoopForUI message_loop_;
140 content::TestBrowserThread ui_thread_;
141 content::TestBrowserThread db_thread_;
142 scoped_refptr<WebDataService> wds_;
143 MockExtensionService extension_service_;
144 ExtensionSet extensions_;
145 WebIntentsRegistry registry_;
146 base::ScopedTempDir temp_dir_;
147 };
148
149 // Base consumer for WebIntentsRegistry results.
150 class TestConsumer {
151 public:
152 // Wait for the UI message loop to terminate - happens when OnIntesQueryDone
153 // is invoked.
154 void WaitForData() {
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
156 MessageLoop::current()->Run();
157 }
158 };
159
160 // Consumer of service lists. Stores result data and
161 // terminates UI thread when callback is invoked.
162 class ServiceListConsumer : public TestConsumer {
163 public:
164 void Accept(
165 const std::vector<webkit_glue::WebIntentServiceData>& services) {
166 services_ = services;
167
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
169 MessageLoop::current()->Quit();
170 }
171
172 bool ResultsContain(const webkit_glue::WebIntentServiceData& service) {
173 for (size_t i = 0; i < services_.size(); ++i) {
174 if (services_[i] == service)
175 return true;
176 }
177 return false;
178 }
179
180 // Result data from callback.
181 std::vector<webkit_glue::WebIntentServiceData> services_;
182 };
183
184 // Consume or defaultservice lists. Stores result data and
185 // terminates UI thread when callback is invoked.
186 class DefaultServiceListConsumer : public TestConsumer {
187 public:
188 void Accept(const std::vector<DefaultWebIntentService>& services) {
189 services_ = services;
190
191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
192 MessageLoop::current()->Quit();
193 }
194
195 bool ResultsContain(const DefaultWebIntentService& service) {
196 for (size_t i = 0; i < services_.size(); ++i) {
197 if (services_[i] == service)
198 return true;
199 }
200 return false;
201 }
202
203 // Result data from callback.
204 std::vector<DefaultWebIntentService> services_;
205 };
206
207 // Consumer of a default service. Stores result data and
208 // terminates UI thread when callback is invoked.
209 class DefaultServiceConsumer : public TestConsumer {
210 public:
211 void Accept(
212 const DefaultWebIntentService& default_service) {
213 service_ = default_service;
214
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
216 MessageLoop::current()->Quit();
217 }
218
219 // Result default data from callback.
220 DefaultWebIntentService service_;
221 };
222
223 TEST_F(WebIntentsRegistryTest, BasicTests) {
224 webkit_glue::WebIntentServiceData service;
225 service.service_url = GURL("http://google.com");
226 service.action = ASCIIToUTF16("share");
227 service.type = ASCIIToUTF16("image/*");
228 service.title = ASCIIToUTF16("Google's Sharing Service");
229
230 registry_.RegisterIntentService(service);
231
232 service.type = ASCIIToUTF16("video/*");
233 service.title = ASCIIToUTF16("Second Service");
234 registry_.RegisterIntentService(service);
235
236 service.action = ASCIIToUTF16("search");
237 registry_.RegisterIntentService(service);
238
239 ServiceListConsumer consumer;
240 registry_.GetIntentServices(ASCIIToUTF16("share"), ASCIIToUTF16("*"),
241 base::Bind(&ServiceListConsumer::Accept,
242 base::Unretained(&consumer)));
243 consumer.WaitForData();
244 EXPECT_EQ(2U, consumer.services_.size());
245
246 registry_.GetIntentServices(ASCIIToUTF16("search"), ASCIIToUTF16("*"),
247 base::Bind(&ServiceListConsumer::Accept,
248 base::Unretained(&consumer)));
249 consumer.WaitForData();
250 EXPECT_EQ(1U, consumer.services_.size());
251
252 service.action = ASCIIToUTF16("share");
253 service.type = ASCIIToUTF16("image/*");
254 registry_.UnregisterIntentService(service);
255
256 registry_.GetIntentServices(ASCIIToUTF16("share"), ASCIIToUTF16("*"),
257 base::Bind(&ServiceListConsumer::Accept,
258 base::Unretained(&consumer)));
259 consumer.WaitForData();
260 EXPECT_EQ(1U, consumer.services_.size());
261 }
262
263 TEST_F(WebIntentsRegistryTest, GetIntentServicesForExtensionFilter) {
264 scoped_refptr<Extension> share_extension(
265 LoadAndExpectSuccess("intent_valid.json"));
266 scoped_refptr<Extension> edit_extension(
267 LoadAndExpectSuccess("intent_valid_2.json"));
268 extensions_.Insert(share_extension);
269 extensions_.Insert(edit_extension);
270 ASSERT_EQ(2U, extensions_.size());
271
272 WebIntentsRegistry::IntentServiceList services;
273 registry_.GetIntentServicesForExtensionFilter(
274 ASCIIToUTF16("http://webintents.org/edit"),
275 ASCIIToUTF16("image/*"),
276 edit_extension->id(),
277 &services);
278 ASSERT_EQ(1U, services.size());
279
280 EXPECT_EQ(edit_extension->url().spec() + "services/edit",
281 services[0].service_url.spec());
282 }
283
284 TEST_F(WebIntentsRegistryTest, GetAllIntents) {
285 webkit_glue::WebIntentServiceData service;
286 service.service_url = GURL("http://google.com");
287 service.action = ASCIIToUTF16("share");
288 service.type = ASCIIToUTF16("image/*");
289 service.title = ASCIIToUTF16("Google's Sharing Service");
290 registry_.RegisterIntentService(service);
291
292 service.action = ASCIIToUTF16("search");
293 registry_.RegisterIntentService(service);
294
295 ServiceListConsumer consumer;
296 registry_.GetAllIntentServices(base::Bind(&ServiceListConsumer::Accept,
297 base::Unretained(&consumer)));
298 consumer.WaitForData();
299 ASSERT_EQ(2U, consumer.services_.size());
300
301 if (consumer.services_[0].action != ASCIIToUTF16("share"))
302 std::swap(consumer.services_[0], consumer.services_[1]);
303
304 service.action = ASCIIToUTF16("share");
305 EXPECT_EQ(service, consumer.services_[0]);
306
307 service.action = ASCIIToUTF16("search");
308 EXPECT_EQ(service, consumer.services_[1]);
309 }
310
311 TEST_F(WebIntentsRegistryTest, GetExtensionIntents) {
312 extensions_.Insert(LoadAndExpectSuccess("intent_valid.json"));
313 extensions_.Insert(LoadAndExpectSuccess("intent_valid_2.json"));
314 ASSERT_EQ(2U, extensions_.size());
315
316 ServiceListConsumer consumer;
317 registry_.GetAllIntentServices(base::Bind(&ServiceListConsumer::Accept,
318 base::Unretained(&consumer)));
319 consumer.WaitForData();
320 ASSERT_EQ(2U, consumer.services_.size());
321 }
322
323 TEST_F(WebIntentsRegistryTest, GetSomeExtensionIntents) {
324 extensions_.Insert(LoadAndExpectSuccess("intent_valid.json"));
325 extensions_.Insert(LoadAndExpectSuccess("intent_valid_2.json"));
326 ASSERT_EQ(2U, extensions_.size());
327
328 ServiceListConsumer consumer;
329 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/edit"),
330 ASCIIToUTF16("*"),
331 base::Bind(&ServiceListConsumer::Accept,
332 base::Unretained(&consumer)));
333 consumer.WaitForData();
334 ASSERT_EQ(1U, consumer.services_.size());
335 }
336
337 TEST_F(WebIntentsRegistryTest, GetIntentsFromMixedSources) {
338 extensions_.Insert(LoadAndExpectSuccess("intent_valid.json"));
339 extensions_.Insert(LoadAndExpectSuccess("intent_valid_2.json"));
340 ASSERT_EQ(2U, extensions_.size());
341
342 webkit_glue::WebIntentServiceData service;
343 service.service_url = GURL("http://somewhere.com/intent/edit.html");
344 service.action = ASCIIToUTF16("http://webintents.org/edit");
345 service.type = ASCIIToUTF16("image/*");
346 service.title = ASCIIToUTF16("Image Editing Service");
347 registry_.RegisterIntentService(service);
348
349 ServiceListConsumer consumer;
350 registry_.GetIntentServices(
351 ASCIIToUTF16("http://webintents.org/edit"), ASCIIToUTF16("*"),
352 base::Bind(&ServiceListConsumer::Accept,
353 base::Unretained(&consumer)));
354 consumer.WaitForData();
355 ASSERT_EQ(2U, consumer.services_.size());
356
357 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/share"),
358 ASCIIToUTF16("*"),
359 base::Bind(&ServiceListConsumer::Accept,
360 base::Unretained(&consumer)));
361 consumer.WaitForData();
362 ASSERT_EQ(1U, consumer.services_.size());
363 }
364
365 TEST_F(WebIntentsRegistryTest, GetIntentsWithMimeAndLiteralMatching) {
366 WebIntentServiceData services[] = {
367 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
368 ASCIIToUTF16("image/*"),
369 string16(),
370 GURL("http://elsewhere.com/intent/share.html"),
371 ASCIIToUTF16("Image Sharing Service")),
372 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
373 ASCIIToUTF16("image/jpeg"),
374 string16(),
375 GURL("http://somewhere.com/intent/share.html"),
376 ASCIIToUTF16("Specific Image Editing Service")),
377 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
378 ASCIIToUTF16("text/uri-list"),
379 string16(),
380 GURL("http://somewhere.com/intent/share.html"),
381 ASCIIToUTF16("Text Link Sharing Service")),
382 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
383 ASCIIToUTF16("text/plain"),
384 string16(),
385 GURL("http://somewhere2.com/intent/share.html"),
386 ASCIIToUTF16("Text Sharing Service")),
387 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
388 ASCIIToUTF16("elsewhere"),
389 string16(),
390 GURL("http://elsewhere.com/intent/share.html"),
391 ASCIIToUTF16("Text Sharing Service")),
392 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
393 ASCIIToUTF16("somewhere"),
394 string16(),
395 GURL("http://somewhere.com/intent/share.html"),
396 ASCIIToUTF16("Text Sharing Service")),
397 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
398 ASCIIToUTF16("nota/*"),
399 string16(),
400 GURL("http://somewhere.com/intent/share.html"),
401 ASCIIToUTF16("Text Sharing Service")),
402 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
403 ASCIIToUTF16("*nomime"),
404 string16(),
405 GURL("http://somewhere.com/intent/share.html"),
406 ASCIIToUTF16("Text Sharing Service")),
407 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
408 ASCIIToUTF16("*/nomime"),
409 string16(),
410 GURL("http://somewhere.com/intent/share.html"),
411 ASCIIToUTF16("Text Sharing Service")),
412 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
413 ASCIIToUTF16("*/*nomime"),
414 string16(),
415 GURL("http://somewhere.com/intent/share.html"),
416 ASCIIToUTF16("Text Sharing Service")),
417 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
418 ASCIIToUTF16("*/*/nomime"),
419 string16(),
420 GURL("http://somewhere.com/intent/share.html"),
421 ASCIIToUTF16("Text Sharing Service")),
422 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
423 ASCIIToUTF16("nomime/*"),
424 string16(),
425 GURL("http://somewhere.com/intent/share.html"),
426 ASCIIToUTF16("Text Sharing Service")),
427 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
428 ASCIIToUTF16("x-type/*"),
429 string16(),
430 GURL("http://somewhere.com/intent/share.html"),
431 ASCIIToUTF16("Text Sharing Service")),
432 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
433 ASCIIToUTF16("x-/*"), // actually a string literal
434 string16(),
435 GURL("http://somewhere.com/intent/share.html"),
436 ASCIIToUTF16("Text Sharing Service"))
437 };
438 registry_.RegisterIntentService(services[0]);
439 registry_.RegisterIntentService(services[1]);
440 registry_.RegisterIntentService(services[2]);
441 registry_.RegisterIntentService(services[3]);
442 registry_.RegisterIntentService(services[4]);
443 registry_.RegisterIntentService(services[5]);
444 registry_.RegisterIntentService(services[6]);
445 registry_.RegisterIntentService(services[7]);
446 registry_.RegisterIntentService(services[8]);
447 registry_.RegisterIntentService(services[9]);
448 registry_.RegisterIntentService(services[10]);
449 registry_.RegisterIntentService(services[11]);
450 registry_.RegisterIntentService(services[12]);
451 registry_.RegisterIntentService(services[13]);
452
453 ServiceListConsumer consumer;
454
455 // Test specific match on both sides.
456 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/share"),
457 ASCIIToUTF16("text/uri-list"),
458 base::Bind(&ServiceListConsumer::Accept,
459 base::Unretained(&consumer)));
460 consumer.WaitForData();
461 ASSERT_EQ(1U, consumer.services_.size());
462 EXPECT_EQ(services[2], consumer.services_[0]);
463
464 // Test specific query, wildcard registration.
465 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/share"),
466 ASCIIToUTF16("image/png"),
467 base::Bind(&ServiceListConsumer::Accept,
468 base::Unretained(&consumer)));
469 consumer.WaitForData();
470 ASSERT_EQ(1U, consumer.services_.size());
471 EXPECT_EQ(services[0], consumer.services_[0]);
472
473 // Test wildcard query, specific registration.
474 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/share"),
475 ASCIIToUTF16("text/*"),
476 base::Bind(&ServiceListConsumer::Accept,
477 base::Unretained(&consumer)));
478 consumer.WaitForData();
479 ASSERT_EQ(2U, consumer.services_.size());
480 EXPECT_EQ(services[2], consumer.services_[0]);
481 EXPECT_EQ(services[3], consumer.services_[1]);
482
483 // Test wildcard query, wildcard registration.
484 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/share"),
485 ASCIIToUTF16("image/*"),
486 base::Bind(&ServiceListConsumer::Accept,
487 base::Unretained(&consumer)));
488 consumer.WaitForData();
489 ASSERT_EQ(2U, consumer.services_.size());
490 EXPECT_EQ(services[0], consumer.services_[0]);
491 EXPECT_EQ(services[1], consumer.services_[1]);
492
493 // Test "catch-all" query.
494 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/share"),
495 ASCIIToUTF16("*"),
496 base::Bind(&ServiceListConsumer::Accept,
497 base::Unretained(&consumer)));
498 consumer.WaitForData();
499 ASSERT_EQ(5U, consumer.services_.size());
500 EXPECT_TRUE(consumer.ResultsContain(services[0]));
501 EXPECT_TRUE(consumer.ResultsContain(services[1]));
502 EXPECT_TRUE(consumer.ResultsContain(services[2]));
503 EXPECT_TRUE(consumer.ResultsContain(services[3]));
504 EXPECT_TRUE(consumer.ResultsContain(services[12]));
505
506 // Test retrieve string literal match.
507 registry_.GetIntentServices(
508 ASCIIToUTF16("http://webintents.org/share"), ASCIIToUTF16("elsewhere"),
509 base::Bind(&ServiceListConsumer::Accept,
510 base::Unretained(&consumer)));
511 consumer.WaitForData();
512 ASSERT_EQ(1U, consumer.services_.size());
513 EXPECT_EQ(services[4], consumer.services_[0]);
514
515 // Test retrieve MIME-looking type but actually isn't
516 // doesn't wildcard match.
517 registry_.GetIntentServices(
518 ASCIIToUTF16("http://webintents.org/share"),
519 ASCIIToUTF16("nota/mimetype"),
520 base::Bind(&ServiceListConsumer::Accept,
521 base::Unretained(&consumer)));
522 consumer.WaitForData();
523 ASSERT_EQ(0U, consumer.services_.size());
524
525 // Also a MIME-ish type that actually isn't.
526 registry_.GetIntentServices(
527 ASCIIToUTF16("http://webintents.org/share"),
528 ASCIIToUTF16("x-/mimetype"),
529 base::Bind(&ServiceListConsumer::Accept,
530 base::Unretained(&consumer)));
531 consumer.WaitForData();
532 ASSERT_EQ(0U, consumer.services_.size());
533
534 // Extension MIME type will match wildcard.
535 registry_.GetIntentServices(
536 ASCIIToUTF16("http://webintents.org/share"),
537 ASCIIToUTF16("x-type/mimetype"),
538 base::Bind(&ServiceListConsumer::Accept,
539 base::Unretained(&consumer)));
540 consumer.WaitForData();
541 ASSERT_EQ(1U, consumer.services_.size());
542 }
543
544 TEST_F(WebIntentsRegistryTest, TestGetAllDefaultIntentServices) {
545 DefaultWebIntentService s0;
546 s0.action = ASCIIToUTF16("share");
547 s0.type = ASCIIToUTF16("text/*");
548 // Values here are just dummies to test for preservation.
549 s0.user_date = 1;
550 s0.suppression = 4;
551 s0.service_url = "service_url";
552 registry_.RegisterDefaultIntentService(s0);
553
554 DefaultWebIntentService s1;
555 s1.action = ASCIIToUTF16("pick");
556 s1.type = ASCIIToUTF16("image/*");
557 // Values here are just dummies to test for preservation.
558 s1.user_date = 1;
559 s1.suppression = 4;
560 s1.service_url = "service_url";
561 registry_.RegisterDefaultIntentService(s1);
562
563 DefaultWebIntentService s2;
564 s2.action = ASCIIToUTF16("save");
565 s2.type = ASCIIToUTF16("application/*");
566 // Values here are just dummies to test for preservation.
567 s2.user_date = 1;
568 s2.suppression = 4;
569 s2.service_url = "service_url";
570 registry_.RegisterDefaultIntentService(s2);
571
572 DefaultServiceListConsumer consumer;
573
574 registry_.GetAllDefaultIntentServices(
575 base::Bind(&DefaultServiceListConsumer::Accept,
576 base::Unretained(&consumer)));
577
578 consumer.WaitForData();
579
580 EXPECT_TRUE(consumer.ResultsContain(s0));
581 EXPECT_TRUE(consumer.ResultsContain(s1));
582 EXPECT_TRUE(consumer.ResultsContain(s2));
583 }
584
585 TEST_F(WebIntentsRegistryTest, TestGetDefaults) {
586 // Ignore QO-default related calls.
587 EXPECT_CALL(extension_service_, GetInstalledExtension(testing::_)).
588 WillRepeatedly(testing::ReturnNull());
589
590 DefaultWebIntentService default_service;
591 default_service.action = ASCIIToUTF16("share");
592 default_service.type = ASCIIToUTF16("text/*");
593 // Values here are just dummies to test for preservation.
594 default_service.user_date = 1;
595 default_service.suppression = 4;
596 default_service.service_url = "service_url";
597 registry_.RegisterDefaultIntentService(default_service);
598
599 DefaultServiceConsumer consumer;
600
601 // Test we can retrieve default entries by action.
602 registry_.GetDefaultIntentService(
603 ASCIIToUTF16("share"), ASCIIToUTF16("text/plain"),
604 GURL("http://www.google.com/"),
605 base::Bind(&DefaultServiceConsumer::Accept,
606 base::Unretained(&consumer)));
607
608 consumer.WaitForData();
609
610 EXPECT_EQ("service_url", consumer.service_.service_url);
611 EXPECT_EQ(1, consumer.service_.user_date);
612 EXPECT_EQ(4, consumer.service_.suppression);
613
614 // Can get for wildcard.
615 consumer.service_ = DefaultWebIntentService();
616 registry_.GetDefaultIntentService(
617 ASCIIToUTF16("share"),
618 ASCIIToUTF16("text/*"),
619 GURL("http://www.google.com/"),
620 base::Bind(&DefaultServiceConsumer::Accept,
621 base::Unretained(&consumer)));
622 consumer.WaitForData();
623 EXPECT_EQ("service_url", consumer.service_.service_url);
624 EXPECT_EQ(1, consumer.service_.user_date);
625 EXPECT_EQ(4, consumer.service_.suppression);
626
627 // Test that no action match means we don't retrieve any
628 // default entries.
629 consumer.service_ = DefaultWebIntentService();
630 ASSERT_EQ("", consumer.service_.service_url);
631 registry_.GetDefaultIntentService(
632 ASCIIToUTF16("no-share"), ASCIIToUTF16("text/plain"),
633 GURL("http://www.google.com/"),
634 base::Bind(&DefaultServiceConsumer::Accept,
635 base::Unretained(&consumer)));
636
637 consumer.WaitForData();
638
639 EXPECT_EQ("", consumer.service_.service_url);
640
641 // Test that no type match means we don't retrieve any
642 // default entries (they get filtered out).
643 consumer.service_ = DefaultWebIntentService();
644 ASSERT_EQ("", consumer.service_.service_url);
645 registry_.GetDefaultIntentService(
646 ASCIIToUTF16("share"), ASCIIToUTF16("image/plain"),
647 GURL("http://www.google.com/"),
648 base::Bind(&DefaultServiceConsumer::Accept,
649 base::Unretained(&consumer)));
650
651 consumer.WaitForData();
652
653 EXPECT_EQ("", consumer.service_.service_url);
654
655 // Check that a string-literal type won't match.
656 consumer.service_ = DefaultWebIntentService();
657 ASSERT_EQ("", consumer.service_.service_url);
658 registry_.GetDefaultIntentService(
659 ASCIIToUTF16("share"),
660 ASCIIToUTF16("literal"),
661 GURL("http://www.google.com/"),
662 base::Bind(&DefaultServiceConsumer::Accept,
663 base::Unretained(&consumer)));
664
665 consumer.WaitForData();
666
667 EXPECT_EQ("", consumer.service_.service_url);
668 }
669
670 // Verify that collapsing equivalent intents works properly.
671 TEST_F(WebIntentsRegistryTest, CollapseIntents) {
672 WebIntentsRegistry::IntentServiceList services;
673
674 // Add two intents with identical |service_url|, |title|, and |action|.
675 services.push_back(
676 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
677 ASCIIToUTF16("image/png"),
678 string16(),
679 GURL("http://somewhere.com/intent/share.html"),
680 ASCIIToUTF16("Image Sharing Service")));
681 services.push_back(
682 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
683 ASCIIToUTF16("image/jpg"),
684 string16(),
685 GURL("http://somewhere.com/intent/share.html"),
686 ASCIIToUTF16("Image Sharing Service")));
687 // Service that differs in disposition.
688 services.push_back(
689 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
690 ASCIIToUTF16("image/png"),
691 string16(),
692 GURL("http://somewhere.com/intent/share.html"),
693 ASCIIToUTF16("Image Sharing Service")));
694 ASSERT_EQ(WebIntentServiceData::DISPOSITION_WINDOW,
695 services.back().disposition);
696 services.back().disposition = WebIntentServiceData::DISPOSITION_INLINE;
697 // Service that differs in title.
698 services.push_back(
699 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
700 ASCIIToUTF16("image/png"),
701 string16(),
702 GURL("http://somewhere.com/intent/share.html"),
703 ASCIIToUTF16("Sharing Service")));
704 // Service that differs in |action|.
705 services.push_back(
706 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share-old"),
707 ASCIIToUTF16("image/png"),
708 string16(),
709 GURL("http://somewhere.com/intent/share.html"),
710 ASCIIToUTF16("Image Sharing Service")));
711 // Service that differs in |service_url|.
712 services.push_back(
713 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
714 ASCIIToUTF16("image/png"),
715 string16(),
716 GURL("http://zoo.com/share.html"),
717 ASCIIToUTF16("Image Sharing Service")));
718
719 // Only the first two services should be collapsed.
720 registry_.CollapseIntents(&services);
721 ASSERT_EQ(5UL, services.size());
722
723 // Joined services have their mime types combined
724 EXPECT_EQ(ASCIIToUTF16("image/png,image/jpg"), services[0].type);
725
726 // Verify the remaining services via distinguishing characteristics.
727 EXPECT_EQ(WebIntentServiceData::DISPOSITION_INLINE, services[1].disposition);
728 EXPECT_EQ(ASCIIToUTF16("Sharing Service"), services[2].title);
729 EXPECT_EQ(ASCIIToUTF16("http://webintents.org/share-old"),
730 services[3].action);
731 EXPECT_EQ(GURL("http://zoo.com/share.html").spec(),
732 services[4].service_url.spec());
733 }
734
735 // Verify that GetIntentServices collapses equivalent intents.
736 TEST_F(WebIntentsRegistryTest, GetIntentsCollapsesEquivalentIntents) {
737 WebIntentServiceData services[] = {
738 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
739 ASCIIToUTF16("image/png"),
740 string16(),
741 GURL("http://somewhere.com/intent/share.html"),
742 ASCIIToUTF16("Image Sharing Service")),
743 WebIntentServiceData(ASCIIToUTF16("http://webintents.org/share"),
744 ASCIIToUTF16("image/jpg"),
745 string16(),
746 GURL("http://somewhere.com/intent/share.html"),
747 ASCIIToUTF16("Image Sharing Service"))
748 };
749 registry_.RegisterIntentService(services[0]);
750 registry_.RegisterIntentService(services[1]);
751
752 ServiceListConsumer consumer;
753 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/share"),
754 ASCIIToUTF16("image/*"),
755 base::Bind(&ServiceListConsumer::Accept,
756 base::Unretained(&consumer)));
757
758 consumer.WaitForData();
759 ASSERT_EQ(1U, consumer.services_.size());
760 EXPECT_EQ(ASCIIToUTF16("image/png,image/jpg"), consumer.services_[0].type);
761 }
762
763 TEST_F(WebIntentsRegistryTest, UnregisterDefaultIntentServicesForServiceURL) {
764
765 const GURL service_url_0("http://jibfest.com/dozer");
766 const GURL service_url_1("http://kittyfizzer.com/fizz");
767
768 DefaultWebIntentService s0;
769 s0.action = ASCIIToUTF16("share");
770 s0.type = ASCIIToUTF16("text/*");
771 // Values here are just dummies to test for preservation.
772 s0.user_date = 1;
773 s0.suppression = 4;
774 s0.service_url = service_url_0.spec();
775 registry_.RegisterDefaultIntentService(s0);
776
777 DefaultWebIntentService s1;
778 s1.action = ASCIIToUTF16("whack");
779 s1.type = ASCIIToUTF16("text/*");
780 // Values here are just dummies to test for preservation.
781 s1.user_date = 1;
782 s1.suppression = 4;
783 s1.service_url = service_url_1.spec();
784 registry_.RegisterDefaultIntentService(s1);
785
786 DefaultServiceListConsumer consumer;
787
788 registry_.GetAllDefaultIntentServices(
789 base::Bind(&DefaultServiceListConsumer::Accept,
790 base::Unretained(&consumer)));
791
792 consumer.WaitForData();
793
794 ASSERT_EQ(2U, consumer.services_.size());
795
796 registry_.UnregisterServiceDefaults(service_url_0);
797 MessageLoop::current()->RunUntilIdle();
798
799 registry_.GetAllDefaultIntentServices(
800 base::Bind(&DefaultServiceListConsumer::Accept,
801 base::Unretained(&consumer)));
802
803 consumer.WaitForData();
804
805 ASSERT_EQ(1U, consumer.services_.size());
806 EXPECT_EQ(service_url_1.spec(), consumer.services_[0].service_url);
807 }
OLDNEW
« no previous file with comments | « chrome/browser/intents/web_intents_registry_factory.cc ('k') | chrome/browser/intents/web_intents_reporting.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698