OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/string16.h" | 15 #include "base/string16.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
18 #include "base/time.h" | 18 #include "base/time.h" |
19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
20 #include "chrome/browser/autofill/autofill_profile.h" | 20 #include "chrome/browser/autofill/autofill_profile.h" |
21 #include "chrome/browser/autofill/credit_card.h" | 21 #include "chrome/browser/autofill/credit_card.h" |
22 #include "chrome/browser/intents/default_web_intent_service.h" | |
23 #include "chrome/browser/webdata/autofill_change.h" | 22 #include "chrome/browser/webdata/autofill_change.h" |
24 #include "chrome/browser/webdata/autofill_entry.h" | 23 #include "chrome/browser/webdata/autofill_entry.h" |
25 #include "chrome/browser/webdata/web_data_service.h" | 24 #include "chrome/browser/webdata/web_data_service.h" |
26 #include "chrome/browser/webdata/web_data_service_test_util.h" | 25 #include "chrome/browser/webdata/web_data_service_test_util.h" |
27 #include "chrome/browser/webdata/web_intents_table.h" | |
28 #include "chrome/common/chrome_notification_types.h" | 26 #include "chrome/common/chrome_notification_types.h" |
29 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
30 #include "chrome/common/form_field_data.h" | 28 #include "chrome/common/form_field_data.h" |
31 #include "chrome/test/base/thread_observer_helper.h" | 29 #include "chrome/test/base/thread_observer_helper.h" |
32 #include "content/public/browser/notification_details.h" | 30 #include "content/public/browser/notification_details.h" |
33 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
34 #include "content/public/test/test_browser_thread.h" | 32 #include "content/public/test/test_browser_thread.h" |
35 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
37 | 35 |
38 using base::Time; | 36 using base::Time; |
39 using base::TimeDelta; | 37 using base::TimeDelta; |
40 using base::WaitableEvent; | 38 using base::WaitableEvent; |
41 using content::BrowserThread; | 39 using content::BrowserThread; |
42 using testing::_; | 40 using testing::_; |
43 using testing::DoDefault; | 41 using testing::DoDefault; |
44 using testing::ElementsAreArray; | 42 using testing::ElementsAreArray; |
45 using testing::Pointee; | 43 using testing::Pointee; |
46 using testing::Property; | 44 using testing::Property; |
47 using webkit_glue::WebIntentServiceData; | |
48 | 45 |
49 typedef std::vector<AutofillChange> AutofillChangeList; | 46 typedef std::vector<AutofillChange> AutofillChangeList; |
50 | 47 |
51 static const int kWebDataServiceTimeoutSeconds = 8; | 48 static const int kWebDataServiceTimeoutSeconds = 8; |
52 | 49 |
53 ACTION_P(SignalEvent, event) { | 50 ACTION_P(SignalEvent, event) { |
54 event->Signal(); | 51 event->Signal(); |
55 } | 52 } |
56 | 53 |
57 class AutofillDBThreadObserverHelper : public DBThreadObserverHelper { | 54 class AutofillDBThreadObserverHelper : public DBThreadObserverHelper { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 string16 name1_; | 142 string16 name1_; |
146 string16 name2_; | 143 string16 name2_; |
147 string16 value1_; | 144 string16 value1_; |
148 string16 value2_; | 145 string16 value2_; |
149 int unique_id1_, unique_id2_; | 146 int unique_id1_, unique_id2_; |
150 const TimeDelta test_timeout_; | 147 const TimeDelta test_timeout_; |
151 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper_; | 148 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper_; |
152 WaitableEvent done_event_; | 149 WaitableEvent done_event_; |
153 }; | 150 }; |
154 | 151 |
155 // Run the current message loop. OnWebDataServiceRequestDone will invoke | |
156 // MessageLoop::Quit on completion, so this call will finish at that point. | |
157 static void WaitUntilCalled() { | |
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
159 MessageLoop::current()->Run(); | |
160 } | |
161 | |
162 // Simple consumer for WebIntents service data. Stores the result data and | |
163 // quits UI message loop when callback is invoked. | |
164 class WebIntentsConsumer : public WebDataServiceConsumer { | |
165 public: | |
166 virtual void OnWebDataServiceRequestDone( | |
167 WebDataService::Handle h, | |
168 const WDTypedResult* result) OVERRIDE { | |
169 services_.clear(); | |
170 if (result) { | |
171 DCHECK(result->GetType() == WEB_INTENTS_RESULT); | |
172 services_ = static_cast< | |
173 const WDResult<std::vector<WebIntentServiceData> >*>(result)-> | |
174 GetValue(); | |
175 } | |
176 | |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
178 MessageLoop::current()->Quit(); | |
179 } | |
180 | |
181 // Result data from completion callback. | |
182 std::vector<WebIntentServiceData> services_; | |
183 }; | |
184 | |
185 // Simple consumer for WebIntents defaults data. Stores the result data and | |
186 // quits UI message loop when callback is invoked. | |
187 class WebIntentsDefaultsConsumer : public WebDataServiceConsumer { | |
188 public: | |
189 virtual void OnWebDataServiceRequestDone( | |
190 WebDataService::Handle h, | |
191 const WDTypedResult* result) OVERRIDE { | |
192 services_.clear(); | |
193 if (result) { | |
194 DCHECK(result->GetType() == WEB_INTENTS_DEFAULTS_RESULT); | |
195 services_ = static_cast< | |
196 const WDResult<std::vector<DefaultWebIntentService> >*>(result)-> | |
197 GetValue(); | |
198 } | |
199 | |
200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
201 MessageLoop::current()->Quit(); | |
202 } | |
203 | |
204 // Result data from completion callback. | |
205 std::vector<DefaultWebIntentService> services_; | |
206 }; | |
207 | |
208 // Simple consumer for Keywords data. Stores the result data and quits UI | 152 // Simple consumer for Keywords data. Stores the result data and quits UI |
209 // message loop when callback is invoked. | 153 // message loop when callback is invoked. |
210 class KeywordsConsumer : public WebDataServiceConsumer { | 154 class KeywordsConsumer : public WebDataServiceConsumer { |
211 public: | 155 public: |
212 KeywordsConsumer() : load_succeeded(false) {} | 156 KeywordsConsumer() : load_succeeded(false) {} |
213 | 157 |
214 virtual void OnWebDataServiceRequestDone( | 158 virtual void OnWebDataServiceRequestDone( |
215 WebDataService::Handle h, | 159 WebDataService::Handle h, |
216 const WDTypedResult* result) OVERRIDE { | 160 const WDTypedResult* result) OVERRIDE { |
217 if (result) { | 161 if (result) { |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 ASSERT_EQ(0U, profile_consumer2.result().size()); | 591 ASSERT_EQ(0U, profile_consumer2.result().size()); |
648 | 592 |
649 // Check that the credit card was removed. | 593 // Check that the credit card was removed. |
650 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > | 594 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > |
651 card_consumer2; | 595 card_consumer2; |
652 handle2 = wds_->GetCreditCards(&card_consumer2); | 596 handle2 = wds_->GetCreditCards(&card_consumer2); |
653 MessageLoop::current()->Run(); | 597 MessageLoop::current()->Run(); |
654 EXPECT_EQ(handle2, card_consumer2.handle()); | 598 EXPECT_EQ(handle2, card_consumer2.handle()); |
655 ASSERT_EQ(0U, card_consumer2.result().size()); | 599 ASSERT_EQ(0U, card_consumer2.result().size()); |
656 } | 600 } |
657 | |
658 TEST_F(WebDataServiceTest, WebIntents) { | |
659 WebIntentsConsumer consumer; | |
660 | |
661 wds_->GetWebIntentServicesForAction(ASCIIToUTF16("share"), &consumer); | |
662 WaitUntilCalled(); | |
663 EXPECT_EQ(0U, consumer.services_.size()); | |
664 | |
665 WebIntentServiceData service; | |
666 service.service_url = GURL("http://google.com"); | |
667 service.action = ASCIIToUTF16("share1"); | |
668 service.type = ASCIIToUTF16("image/*"); | |
669 wds_->AddWebIntentService(service); | |
670 | |
671 service.action = ASCIIToUTF16("share"); | |
672 service.type = ASCIIToUTF16("image/*"); | |
673 wds_->AddWebIntentService(service); | |
674 | |
675 service.type = ASCIIToUTF16("video/*"); | |
676 wds_->AddWebIntentService(service); | |
677 | |
678 wds_->GetWebIntentServicesForAction(ASCIIToUTF16("share"), &consumer); | |
679 WaitUntilCalled(); | |
680 ASSERT_EQ(2U, consumer.services_.size()); | |
681 | |
682 if (consumer.services_[0].type != ASCIIToUTF16("image/*")) | |
683 std::swap(consumer.services_[0], consumer.services_[1]); | |
684 | |
685 EXPECT_EQ(service.service_url, consumer.services_[0].service_url); | |
686 EXPECT_EQ(service.action, consumer.services_[0].action); | |
687 EXPECT_EQ(ASCIIToUTF16("image/*"), consumer.services_[0].type); | |
688 EXPECT_EQ(service.service_url, consumer.services_[1].service_url); | |
689 EXPECT_EQ(service.action, consumer.services_[1].action); | |
690 EXPECT_EQ(service.type, consumer.services_[1].type); | |
691 | |
692 service.type = ASCIIToUTF16("image/*"); | |
693 wds_->RemoveWebIntentService(service); | |
694 | |
695 wds_->GetWebIntentServicesForAction(ASCIIToUTF16("share"), &consumer); | |
696 WaitUntilCalled(); | |
697 ASSERT_EQ(1U, consumer.services_.size()); | |
698 | |
699 service.type = ASCIIToUTF16("video/*"); | |
700 EXPECT_EQ(service.service_url, consumer.services_[0].service_url); | |
701 EXPECT_EQ(service.action, consumer.services_[0].action); | |
702 EXPECT_EQ(service.type, consumer.services_[0].type); | |
703 } | |
704 | |
705 TEST_F(WebDataServiceTest, WebIntentsForURL) { | |
706 WebIntentsConsumer consumer; | |
707 | |
708 WebIntentServiceData service; | |
709 service.service_url = GURL("http://google.com"); | |
710 service.action = ASCIIToUTF16("share1"); | |
711 service.type = ASCIIToUTF16("image/*"); | |
712 wds_->AddWebIntentService(service); | |
713 | |
714 service.action = ASCIIToUTF16("share"); | |
715 service.type = ASCIIToUTF16("image/*"); | |
716 wds_->AddWebIntentService(service); | |
717 | |
718 wds_->GetWebIntentServicesForURL( | |
719 UTF8ToUTF16(service.service_url.spec()), | |
720 &consumer); | |
721 WaitUntilCalled(); | |
722 ASSERT_EQ(2U, consumer.services_.size()); | |
723 EXPECT_EQ(service, consumer.services_[0]); | |
724 service.action = ASCIIToUTF16("share1"); | |
725 EXPECT_EQ(service, consumer.services_[1]); | |
726 } | |
727 | |
728 TEST_F(WebDataServiceTest, WebIntentsGetAll) { | |
729 WebIntentsConsumer consumer; | |
730 | |
731 WebIntentServiceData service; | |
732 service.service_url = GURL("http://google.com"); | |
733 service.action = ASCIIToUTF16("share"); | |
734 service.type = ASCIIToUTF16("image/*"); | |
735 wds_->AddWebIntentService(service); | |
736 | |
737 service.action = ASCIIToUTF16("edit"); | |
738 wds_->AddWebIntentService(service); | |
739 | |
740 wds_->GetAllWebIntentServices(&consumer); | |
741 WaitUntilCalled(); | |
742 ASSERT_EQ(2U, consumer.services_.size()); | |
743 | |
744 if (consumer.services_[0].action != ASCIIToUTF16("edit")) | |
745 std::swap(consumer.services_[0],consumer.services_[1]); | |
746 | |
747 EXPECT_EQ(service, consumer.services_[0]); | |
748 service.action = ASCIIToUTF16("share"); | |
749 EXPECT_EQ(service, consumer.services_[1]); | |
750 } | |
751 | |
752 TEST_F(WebDataServiceTest, WebIntentsDefaultsTest) { | |
753 WebIntentsDefaultsConsumer consumer; | |
754 | |
755 wds_->GetDefaultWebIntentServicesForAction(ASCIIToUTF16("share"), &consumer); | |
756 WaitUntilCalled(); | |
757 EXPECT_EQ(0U, consumer.services_.size()); | |
758 | |
759 DefaultWebIntentService default_service; | |
760 default_service.action = ASCIIToUTF16("share"); | |
761 default_service.type = ASCIIToUTF16("type"); | |
762 default_service.user_date = 1; | |
763 default_service.suppression = 4; | |
764 default_service.service_url = "service_url"; | |
765 wds_->AddDefaultWebIntentService(default_service); | |
766 | |
767 default_service.action = ASCIIToUTF16("share2"); | |
768 default_service.service_url = "service_url_2"; | |
769 wds_->AddDefaultWebIntentService(default_service); | |
770 | |
771 wds_->GetDefaultWebIntentServicesForAction(ASCIIToUTF16("share"), &consumer); | |
772 WaitUntilCalled(); | |
773 ASSERT_EQ(1U, consumer.services_.size()); | |
774 EXPECT_EQ("service_url", consumer.services_[0].service_url); | |
775 | |
776 wds_->GetAllDefaultWebIntentServices(&consumer); | |
777 WaitUntilCalled(); | |
778 EXPECT_EQ(2U, consumer.services_.size()); | |
779 | |
780 default_service.action = ASCIIToUTF16("share"); | |
781 wds_->RemoveDefaultWebIntentService(default_service); | |
782 | |
783 wds_->GetDefaultWebIntentServicesForAction(ASCIIToUTF16("share"), &consumer); | |
784 WaitUntilCalled(); | |
785 EXPECT_EQ(0U, consumer.services_.size()); | |
786 | |
787 wds_->GetDefaultWebIntentServicesForAction(ASCIIToUTF16("share2"), &consumer); | |
788 WaitUntilCalled(); | |
789 ASSERT_EQ(1U, consumer.services_.size()); | |
790 EXPECT_EQ("service_url_2", consumer.services_[0].service_url); | |
791 | |
792 wds_->GetAllDefaultWebIntentServices(&consumer); | |
793 WaitUntilCalled(); | |
794 ASSERT_EQ(1U, consumer.services_.size()); | |
795 EXPECT_EQ("service_url_2", consumer.services_[0].service_url); | |
796 } | |
797 | |
798 TEST_F(WebDataServiceTest, WebIntentsRemoveDefaultByServiceURL) { | |
799 WebIntentsDefaultsConsumer consumer; | |
800 | |
801 GURL service_url_0("http://pandawaddle.com/observe"); | |
802 GURL service_url_1("http://kittysnicker.com/mock"); | |
803 | |
804 DefaultWebIntentService s0; | |
805 s0.action = ASCIIToUTF16("share"); | |
806 s0.type = ASCIIToUTF16("type"); | |
807 s0.user_date = 1; | |
808 s0.suppression = 4; | |
809 s0.service_url = service_url_0.spec(); | |
810 wds_->AddDefaultWebIntentService(s0); | |
811 | |
812 DefaultWebIntentService s1; | |
813 s1.action = ASCIIToUTF16("share2"); | |
814 s1.type = ASCIIToUTF16("type"); | |
815 s1.user_date = 1; | |
816 s1.suppression = 4; | |
817 s1.service_url = service_url_1.spec(); | |
818 wds_->AddDefaultWebIntentService(s1); | |
819 | |
820 wds_->GetAllDefaultWebIntentServices(&consumer); | |
821 WaitUntilCalled(); | |
822 ASSERT_EQ(2U, consumer.services_.size()); | |
823 | |
824 wds_->RemoveWebIntentServiceDefaults(service_url_0); | |
825 MessageLoop::current()->RunUntilIdle(); | |
826 | |
827 wds_->GetAllDefaultWebIntentServices(&consumer); | |
828 WaitUntilCalled(); | |
829 ASSERT_EQ(1U, consumer.services_.size()); | |
830 EXPECT_EQ(service_url_1.spec(), consumer.services_[0].service_url); | |
831 } | |
OLD | NEW |