OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
12 #include "base/string16.h" | 12 #include "base/string16.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/time.h" |
14 #include "chrome/browser/chrome_thread.h" | 15 #include "chrome/browser/chrome_thread.h" |
15 #include "chrome/browser/webdata/autofill_change.h" | 16 #include "chrome/browser/webdata/autofill_change.h" |
16 #include "chrome/browser/webdata/autofill_entry.h" | 17 #include "chrome/browser/webdata/autofill_entry.h" |
17 #include "chrome/browser/webdata/web_data_service.h" | 18 #include "chrome/browser/webdata/web_data_service.h" |
18 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
19 #include "chrome/common/notification_details.h" | 20 #include "chrome/common/notification_details.h" |
20 #include "chrome/common/notification_observer_mock.h" | 21 #include "chrome/common/notification_observer_mock.h" |
21 #include "chrome/common/notification_registrar.h" | 22 #include "chrome/common/notification_registrar.h" |
22 #include "chrome/common/notification_service.h" | 23 #include "chrome/common/notification_service.h" |
23 #include "chrome/common/notification_type.h" | 24 #include "chrome/common/notification_type.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "webkit/glue/form_field.h" | 27 #include "webkit/glue/form_field.h" |
27 | 28 |
| 29 using base::Time; |
| 30 using base::TimeDelta; |
28 using testing::_; | 31 using testing::_; |
29 using testing::ElementsAreArray; | 32 using testing::ElementsAreArray; |
30 using testing::Pointee; | 33 using testing::Pointee; |
31 using testing::Property; | 34 using testing::Property; |
32 | 35 |
33 typedef std::vector<AutofillChange> AutofillChangeList; | 36 typedef std::vector<AutofillChange> AutofillChangeList; |
34 | 37 |
35 ACTION(QuitUIMessageLoop) { | 38 ACTION(QuitUIMessageLoop) { |
36 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 39 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
37 MessageLoop::current()->Quit(); | 40 MessageLoop::current()->Quit(); |
(...skipping 22 matching lines...) Expand all Loading... |
60 const std::vector<string16>& values() { return values_; } | 63 const std::vector<string16>& values() { return values_; } |
61 | 64 |
62 private: | 65 private: |
63 WebDataService::Handle handle_; | 66 WebDataService::Handle handle_; |
64 std::vector<string16> values_; | 67 std::vector<string16> values_; |
65 DISALLOW_COPY_AND_ASSIGN(AutofillWebDataServiceConsumer); | 68 DISALLOW_COPY_AND_ASSIGN(AutofillWebDataServiceConsumer); |
66 }; | 69 }; |
67 | 70 |
68 class WebDataServiceTest : public testing::Test { | 71 class WebDataServiceTest : public testing::Test { |
69 public: | 72 public: |
70 WebDataServiceTest() : ui_thread_(ChromeThread::UI, &message_loop_) {} | 73 WebDataServiceTest() |
| 74 : ui_thread_(ChromeThread::UI, &message_loop_) {} |
| 75 |
71 protected: | 76 protected: |
72 virtual void SetUp() { | 77 virtual void SetUp() { |
| 78 name1_ = ASCIIToUTF16("name1"); |
| 79 name2_ = ASCIIToUTF16("name2"); |
| 80 value1_ = ASCIIToUTF16("value1"); |
| 81 value2_ = ASCIIToUTF16("value2"); |
| 82 |
73 PathService::Get(chrome::DIR_TEST_DATA, &profile_dir_); | 83 PathService::Get(chrome::DIR_TEST_DATA, &profile_dir_); |
74 const std::string test_profile = "WebDataServiceTest"; | 84 const std::string test_profile = "WebDataServiceTest"; |
75 profile_dir_ = profile_dir_.AppendASCII(test_profile); | 85 profile_dir_ = profile_dir_.AppendASCII(test_profile); |
76 file_util::Delete(profile_dir_, true); | 86 file_util::Delete(profile_dir_, true); |
77 file_util::CreateDirectory(profile_dir_); | 87 file_util::CreateDirectory(profile_dir_); |
78 wds_ = new WebDataService(); | 88 wds_ = new WebDataService(); |
79 wds_->Init(profile_dir_); | 89 wds_->Init(profile_dir_); |
80 } | 90 } |
81 | 91 |
82 virtual void TearDown() { | 92 virtual void TearDown() { |
83 if (wds_.get()) | 93 if (wds_.get()) |
84 wds_->Shutdown(); | 94 wds_->Shutdown(); |
85 file_util::Delete(profile_dir_, true); | 95 file_util::Delete(profile_dir_, true); |
86 | 96 |
87 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask); | 97 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
88 MessageLoop::current()->Run(); | 98 MessageLoop::current()->Run(); |
89 } | 99 } |
90 | 100 |
| 101 void AppendFormField(const string16& name, |
| 102 const string16& value, |
| 103 std::vector<webkit_glue::FormField>* form_fields) { |
| 104 form_fields->push_back( |
| 105 webkit_glue::FormField(EmptyString16(), |
| 106 name, |
| 107 EmptyString16(), |
| 108 value)); |
| 109 } |
| 110 |
91 MessageLoopForUI message_loop_; | 111 MessageLoopForUI message_loop_; |
92 ChromeThread ui_thread_; | 112 ChromeThread ui_thread_; |
| 113 string16 name1_; |
| 114 string16 name2_; |
| 115 string16 value1_; |
| 116 string16 value2_; |
93 FilePath profile_dir_; | 117 FilePath profile_dir_; |
94 scoped_refptr<WebDataService> wds_; | 118 scoped_refptr<WebDataService> wds_; |
95 NotificationRegistrar registrar_; | 119 NotificationRegistrar registrar_; |
96 NotificationObserverMock observer_; | 120 NotificationObserverMock observer_; |
97 }; | 121 }; |
98 | 122 |
99 TEST_F(WebDataServiceTest, AutofillAdd) { | 123 TEST_F(WebDataServiceTest, AutofillAdd) { |
100 const string16 kName1 = ASCIIToUTF16("name1"); | |
101 const string16 kValue1 = ASCIIToUTF16("value1"); | |
102 const string16 kName2 = ASCIIToUTF16("name2"); | |
103 const string16 kValue2 = ASCIIToUTF16("value2"); | |
104 | |
105 const AutofillChange expected_changes[] = { | 124 const AutofillChange expected_changes[] = { |
106 AutofillChange(AutofillChange::ADD, AutofillKey(kName1, kValue1)), | 125 AutofillChange(AutofillChange::ADD, AutofillKey(name1_, value1_)), |
107 AutofillChange(AutofillChange::ADD, AutofillKey(kName2, kValue2)) | 126 AutofillChange(AutofillChange::ADD, AutofillKey(name2_, value2_)) |
108 }; | 127 }; |
109 | 128 |
110 // This will verify that the correct notification is triggered, | 129 // This will verify that the correct notification is triggered, |
111 // passing the correct list of autofill keys in the details. | 130 // passing the correct list of autofill keys in the details. |
112 EXPECT_CALL( | 131 EXPECT_CALL( |
113 observer_, | 132 observer_, |
114 Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED), | 133 Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED), |
115 NotificationService::AllSources(), | 134 NotificationService::AllSources(), |
116 Property(&Details<const AutofillChangeList>::ptr, | 135 Property(&Details<const AutofillChangeList>::ptr, |
117 Pointee(ElementsAreArray(expected_changes))))). | 136 Pointee(ElementsAreArray(expected_changes))))). |
118 WillOnce(QuitUIMessageLoop()); | 137 WillOnce(QuitUIMessageLoop()); |
119 | 138 |
120 registrar_.Add(&observer_, | 139 registrar_.Add(&observer_, |
121 NotificationType::AUTOFILL_ENTRIES_CHANGED, | 140 NotificationType::AUTOFILL_ENTRIES_CHANGED, |
122 NotificationService::AllSources()); | 141 NotificationService::AllSources()); |
123 | 142 |
124 std::vector<webkit_glue::FormField> form_fields; | 143 std::vector<webkit_glue::FormField> form_fields; |
125 form_fields.push_back( | 144 AppendFormField(name1_, value1_, &form_fields); |
126 webkit_glue::FormField(EmptyString16(), | 145 AppendFormField(name2_, value2_, &form_fields); |
127 kName1, | |
128 EmptyString16(), | |
129 kValue1)); | |
130 form_fields.push_back( | |
131 webkit_glue::FormField(EmptyString16(), | |
132 kName2, | |
133 EmptyString16(), | |
134 kValue2)); | |
135 wds_->AddFormFieldValues(form_fields); | 146 wds_->AddFormFieldValues(form_fields); |
136 | 147 |
137 // The message loop will exit when the mock observer is notified. | 148 // The message loop will exit when the mock observer is notified. |
138 MessageLoop::current()->Run(); | 149 MessageLoop::current()->Run(); |
139 | 150 |
140 AutofillWebDataServiceConsumer consumer; | 151 AutofillWebDataServiceConsumer consumer; |
141 WebDataService::Handle handle; | 152 WebDataService::Handle handle; |
142 static const int limit = 10; | 153 static const int limit = 10; |
143 handle = wds_->GetFormValuesForElementName( | 154 handle = wds_->GetFormValuesForElementName( |
144 kName1, EmptyString16(), limit, &consumer); | 155 name1_, EmptyString16(), limit, &consumer); |
145 | 156 |
146 // The message loop will exit when the consumer is called. | 157 // The message loop will exit when the consumer is called. |
147 MessageLoop::current()->Run(); | 158 MessageLoop::current()->Run(); |
148 | 159 |
149 EXPECT_EQ(handle, consumer.handle()); | 160 EXPECT_EQ(handle, consumer.handle()); |
150 ASSERT_EQ(1U, consumer.values().size()); | 161 ASSERT_EQ(1U, consumer.values().size()); |
151 EXPECT_EQ(kValue1, consumer.values()[0]); | 162 EXPECT_EQ(value1_, consumer.values()[0]); |
152 } | 163 } |
153 | 164 |
154 TEST_F(WebDataServiceTest, AutofillRemoveOne) { | 165 TEST_F(WebDataServiceTest, AutofillRemoveOne) { |
155 const string16 kName1 = ASCIIToUTF16("name1"); | |
156 const string16 kValue1 = ASCIIToUTF16("value1"); | |
157 | |
158 // First add some values to autofill. | 166 // First add some values to autofill. |
159 EXPECT_CALL(observer_, Observe(_, _, _)).WillOnce(QuitUIMessageLoop()); | 167 EXPECT_CALL(observer_, Observe(_, _, _)).WillOnce(QuitUIMessageLoop()); |
160 registrar_.Add(&observer_, | 168 registrar_.Add(&observer_, |
161 NotificationType::AUTOFILL_ENTRIES_CHANGED, | 169 NotificationType::AUTOFILL_ENTRIES_CHANGED, |
162 NotificationService::AllSources()); | 170 NotificationService::AllSources()); |
163 std::vector<webkit_glue::FormField> form_fields; | 171 std::vector<webkit_glue::FormField> form_fields; |
164 form_fields.push_back( | 172 AppendFormField(name1_, value1_, &form_fields); |
165 webkit_glue::FormField(EmptyString16(), | |
166 kName1, | |
167 EmptyString16(), | |
168 kValue1)); | |
169 wds_->AddFormFieldValues(form_fields); | 173 wds_->AddFormFieldValues(form_fields); |
170 | 174 |
171 // The message loop will exit when the mock observer is notified. | 175 // The message loop will exit when the mock observer is notified. |
| 176 MessageLoop::current()->Run(); |
| 177 |
| 178 // This will verify that the correct notification is triggered, |
| 179 // passing the correct list of autofill keys in the details. |
| 180 const AutofillChange expected_changes[] = { |
| 181 AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_)) |
| 182 }; |
| 183 EXPECT_CALL( |
| 184 observer_, |
| 185 Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED), |
| 186 NotificationService::AllSources(), |
| 187 Property(&Details<const AutofillChangeList>::ptr, |
| 188 Pointee(ElementsAreArray(expected_changes))))). |
| 189 WillOnce(QuitUIMessageLoop()); |
| 190 wds_->RemoveFormValueForElementName(name1_, value1_); |
| 191 |
| 192 // The message loop will exit when the mock observer is notified. |
| 193 MessageLoop::current()->Run(); |
| 194 } |
| 195 |
| 196 TEST_F(WebDataServiceTest, AutofillRemoveMany) { |
| 197 TimeDelta one_day(TimeDelta::FromDays(1)); |
| 198 Time t = Time::Now(); |
| 199 |
| 200 EXPECT_CALL(observer_, Observe(_, _, _)).WillOnce(QuitUIMessageLoop()); |
| 201 registrar_.Add(&observer_, |
| 202 NotificationType::AUTOFILL_ENTRIES_CHANGED, |
| 203 NotificationService::AllSources()); |
| 204 std::vector<webkit_glue::FormField> form_fields; |
| 205 AppendFormField(name1_, value1_, &form_fields); |
| 206 AppendFormField(name2_, value2_, &form_fields); |
| 207 wds_->AddFormFieldValues(form_fields); |
| 208 |
| 209 // The message loop will exit when the mock observer is notified. |
172 MessageLoop::current()->Run(); | 210 MessageLoop::current()->Run(); |
173 | 211 |
174 // This will verify that the correct notification is triggered, | 212 // This will verify that the correct notification is triggered, |
175 // passing the correct list of autofill keys in the details. | 213 // passing the correct list of autofill keys in the details. |
176 const AutofillChange expected_changes[] = { | 214 const AutofillChange expected_changes[] = { |
177 AutofillChange(AutofillChange::REMOVE, AutofillKey(kName1, kValue1)) | 215 AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_)), |
| 216 AutofillChange(AutofillChange::REMOVE, AutofillKey(name2_, value2_)) |
178 }; | 217 }; |
179 EXPECT_CALL( | 218 EXPECT_CALL( |
180 observer_, | 219 observer_, |
181 Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED), | 220 Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED), |
182 NotificationService::AllSources(), | 221 NotificationService::AllSources(), |
183 Property(&Details<const AutofillChangeList>::ptr, | 222 Property(&Details<const AutofillChangeList>::ptr, |
184 Pointee(ElementsAreArray(expected_changes))))). | 223 Pointee(ElementsAreArray(expected_changes))))). |
185 WillOnce(QuitUIMessageLoop()); | 224 WillOnce(QuitUIMessageLoop()); |
186 wds_->RemoveFormValueForElementName(kName1, kValue1); | 225 wds_->RemoveFormElementsAddedBetween(t, t + one_day); |
187 | 226 |
188 // The message loop will exit when the mock observer is notified. | 227 // The message loop will exit when the mock observer is notified. |
189 MessageLoop::current()->Run(); | 228 MessageLoop::current()->Run(); |
190 } | 229 } |
OLD | NEW |