OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/file_util.h" |
| 6 #include "base/scoped_temp_dir.h" |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/intents/web_intents_registry.h" |
| 9 #include "chrome/browser/webdata/web_data_service.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 class WebIntentsRegistryTest : public testing::Test { |
| 13 public: |
| 14 WebIntentsRegistryTest() |
| 15 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 16 db_thread_(BrowserThread::DB) {} |
| 17 |
| 18 protected: |
| 19 virtual void SetUp() { |
| 20 db_thread_.Start(); |
| 21 wds_ = new WebDataService(); |
| 22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 23 wds_->Init(temp_dir_.path()); |
| 24 registry_.Initialize(wds_); |
| 25 } |
| 26 |
| 27 virtual void TearDown() { |
| 28 if (wds_.get()) |
| 29 wds_->Shutdown(); |
| 30 |
| 31 db_thread_.Stop(); |
| 32 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 33 MessageLoop::current()->Run(); |
| 34 } |
| 35 |
| 36 MessageLoopForUI message_loop_; |
| 37 BrowserThread ui_thread_; |
| 38 BrowserThread db_thread_; |
| 39 scoped_refptr<WebDataService> wds_; |
| 40 WebIntentsRegistry registry_; |
| 41 ScopedTempDir temp_dir_; |
| 42 }; |
| 43 |
| 44 // Simple consumer for WebIntentsRegistry notifications. Stores result data and |
| 45 // terminates UI thread when callback is invoked. |
| 46 class TestConsumer: public WebIntentsRegistry::Consumer { |
| 47 public: |
| 48 virtual void OnIntentsQueryDone(WebIntentsRegistry::QueryID id, |
| 49 const std::vector<WebIntentData>& intents) { |
| 50 DCHECK(id == expected_id_); |
| 51 intents_ = intents; |
| 52 |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 54 MessageLoop::current()->Quit(); |
| 55 } |
| 56 |
| 57 // Wait for the UI message loop to terminate - happens when OnIntesQueryDone |
| 58 // is invoked. |
| 59 void WaitForData() { |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 61 MessageLoop::current()->Run(); |
| 62 } |
| 63 |
| 64 WebIntentsRegistry::QueryID expected_id_; // QueryID callback is tied to. |
| 65 std::vector<WebIntentData> intents_; // Result data from callback. |
| 66 }; |
| 67 |
| 68 TEST_F(WebIntentsRegistryTest, BasicTests) { |
| 69 WebIntentData intent; |
| 70 intent.service_url = GURL("http://google.com"); |
| 71 intent.action = ASCIIToUTF16("share"); |
| 72 intent.type = ASCIIToUTF16("image/*"); |
| 73 |
| 74 registry_.RegisterIntentProvider(intent); |
| 75 |
| 76 intent.type = ASCIIToUTF16("video/*"); |
| 77 registry_.RegisterIntentProvider(intent); |
| 78 |
| 79 intent.action = ASCIIToUTF16("search"); |
| 80 registry_.RegisterIntentProvider(intent); |
| 81 |
| 82 TestConsumer consumer; |
| 83 consumer.expected_id_ = registry_.GetIntentProviders(ASCIIToUTF16("share"), |
| 84 &consumer); |
| 85 consumer.WaitForData(); |
| 86 EXPECT_EQ(2U, consumer.intents_.size()); |
| 87 |
| 88 consumer.expected_id_ = registry_.GetIntentProviders(ASCIIToUTF16("search"), |
| 89 &consumer); |
| 90 consumer.WaitForData(); |
| 91 EXPECT_EQ(1U, consumer.intents_.size()); |
| 92 |
| 93 intent.action = ASCIIToUTF16("share"); |
| 94 intent.type = ASCIIToUTF16("image/*"); |
| 95 registry_.UnregisterIntentProvider(intent); |
| 96 |
| 97 consumer.expected_id_ = registry_.GetIntentProviders(ASCIIToUTF16("share"), |
| 98 &consumer); |
| 99 consumer.WaitForData(); |
| 100 EXPECT_EQ(1U, consumer.intents_.size()); |
| 101 } |
OLD | NEW |