OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/core/browser/autofill_metrics.h" | 5 #include "components/autofill/core/browser/autofill_metrics.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/run_loop.h" |
11 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
13 #include "base/time/time.h" | 15 #include "base/time/time.h" |
14 #include "chrome/browser/autofill/personal_data_manager_factory.h" | |
15 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h" | |
16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
17 #include "chrome/test/base/testing_profile.h" | |
18 #include "components/autofill/core/browser/autofill_external_delegate.h" | 16 #include "components/autofill/core/browser/autofill_external_delegate.h" |
19 #include "components/autofill/core/browser/autofill_manager.h" | 17 #include "components/autofill/core/browser/autofill_manager.h" |
20 #include "components/autofill/core/browser/autofill_manager_delegate.h" | |
21 #include "components/autofill/core/browser/autofill_test_utils.h" | 18 #include "components/autofill/core/browser/autofill_test_utils.h" |
22 #include "components/autofill/core/browser/personal_data_manager.h" | 19 #include "components/autofill/core/browser/personal_data_manager.h" |
23 #include "components/autofill/core/browser/test_autofill_driver.h" | 20 #include "components/autofill/core/browser/test_autofill_driver.h" |
| 21 #include "components/autofill/core/browser/test_autofill_manager_delegate.h" |
24 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 22 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
25 #include "components/autofill/core/common/form_data.h" | 23 #include "components/autofill/core/common/form_data.h" |
26 #include "components/autofill/core/common/form_field_data.h" | 24 #include "components/autofill/core/common/form_field_data.h" |
27 #include "components/autofill/core/common/forms_seen_state.h" | 25 #include "components/autofill/core/common/forms_seen_state.h" |
28 #include "components/webdata/common/web_data_results.h" | 26 #include "components/webdata/common/web_data_results.h" |
29 #include "content/public/test/test_utils.h" | |
30 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
32 #include "ui/gfx/rect.h" | 29 #include "ui/gfx/rect.h" |
33 #include "url/gurl.h" | 30 #include "url/gurl.h" |
34 | 31 |
35 using base::ASCIIToUTF16; | 32 using base::ASCIIToUTF16; |
36 using base::TimeDelta; | 33 using base::TimeDelta; |
37 using base::TimeTicks; | 34 using base::TimeTicks; |
38 using testing::_; | 35 using testing::_; |
39 using testing::AnyNumber; | 36 using testing::AnyNumber; |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 empty_form.fields[i].value = base::string16(); | 193 empty_form.fields[i].value = base::string16(); |
197 } | 194 } |
198 | 195 |
199 // |form_structure| will be owned by |form_structures()|. | 196 // |form_structure| will be owned by |form_structures()|. |
200 TestFormStructure* form_structure = new TestFormStructure(empty_form); | 197 TestFormStructure* form_structure = new TestFormStructure(empty_form); |
201 form_structure->SetFieldTypes(heuristic_types, server_types); | 198 form_structure->SetFieldTypes(heuristic_types, server_types); |
202 form_structures()->push_back(form_structure); | 199 form_structures()->push_back(form_structure); |
203 } | 200 } |
204 | 201 |
205 void FormSubmitted(const FormData& form, const TimeTicks& timestamp) { | 202 void FormSubmitted(const FormData& form, const TimeTicks& timestamp) { |
206 message_loop_runner_ = new content::MessageLoopRunner(); | 203 run_loop_.reset(new base::RunLoop()); |
207 if (!OnFormSubmitted(form, timestamp)) | 204 if (!OnFormSubmitted(form, timestamp)) |
208 return; | 205 return; |
209 | 206 |
210 // Wait for the asynchronous FormSubmitted() call to complete. | 207 // Wait for the asynchronous FormSubmitted() call to complete. |
211 message_loop_runner_->Run(); | 208 run_loop_->Run(); |
212 } | 209 } |
213 | 210 |
214 virtual void UploadFormDataAsyncCallback( | 211 virtual void UploadFormDataAsyncCallback( |
215 const FormStructure* submitted_form, | 212 const FormStructure* submitted_form, |
216 const base::TimeTicks& load_time, | 213 const base::TimeTicks& load_time, |
217 const base::TimeTicks& interaction_time, | 214 const base::TimeTicks& interaction_time, |
218 const base::TimeTicks& submission_time) OVERRIDE { | 215 const base::TimeTicks& submission_time) OVERRIDE { |
219 message_loop_runner_->Quit(); | 216 run_loop_->Quit(); |
220 | 217 |
221 AutofillManager::UploadFormDataAsyncCallback(submitted_form, | 218 AutofillManager::UploadFormDataAsyncCallback(submitted_form, |
222 load_time, | 219 load_time, |
223 interaction_time, | 220 interaction_time, |
224 submission_time); | 221 submission_time); |
225 } | 222 } |
226 | 223 |
227 private: | 224 private: |
228 bool autofill_enabled_; | 225 bool autofill_enabled_; |
229 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 226 scoped_ptr<base::RunLoop> run_loop_; |
230 | 227 |
231 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); | 228 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); |
232 }; | 229 }; |
233 | 230 |
234 } // namespace | 231 } // namespace |
235 | 232 |
236 class AutofillMetricsTest : public ChromeRenderViewHostTestHarness { | 233 class AutofillMetricsTest : public testing::Test { |
237 public: | 234 public: |
238 virtual ~AutofillMetricsTest(); | 235 virtual ~AutofillMetricsTest(); |
239 | 236 |
240 virtual void SetUp() OVERRIDE; | 237 virtual void SetUp() OVERRIDE; |
241 virtual void TearDown() OVERRIDE; | 238 virtual void TearDown() OVERRIDE; |
242 | 239 |
243 protected: | 240 protected: |
| 241 base::MessageLoop message_loop_; |
| 242 TestAutofillManagerDelegate manager_delegate_; |
244 scoped_ptr<TestAutofillDriver> autofill_driver_; | 243 scoped_ptr<TestAutofillDriver> autofill_driver_; |
245 scoped_ptr<TestAutofillManager> autofill_manager_; | 244 scoped_ptr<TestAutofillManager> autofill_manager_; |
246 scoped_ptr<TestPersonalDataManager> personal_data_; | 245 scoped_ptr<TestPersonalDataManager> personal_data_; |
247 scoped_ptr<AutofillExternalDelegate> external_delegate_; | 246 scoped_ptr<AutofillExternalDelegate> external_delegate_; |
248 }; | 247 }; |
249 | 248 |
250 AutofillMetricsTest::~AutofillMetricsTest() { | 249 AutofillMetricsTest::~AutofillMetricsTest() { |
251 // Order of destruction is important as AutofillManager relies on | 250 // Order of destruction is important as AutofillManager relies on |
252 // PersonalDataManager to be around when it gets destroyed. | 251 // PersonalDataManager to be around when it gets destroyed. |
253 autofill_manager_.reset(); | 252 autofill_manager_.reset(); |
254 } | 253 } |
255 | 254 |
256 void AutofillMetricsTest::SetUp() { | 255 void AutofillMetricsTest::SetUp() { |
257 ChromeRenderViewHostTestHarness::SetUp(); | 256 manager_delegate_.SetPrefs(test::PrefServiceForTesting()); |
258 | 257 |
259 // Ensure Mac OS X does not pop up a modal dialog for the Address Book. | 258 // Ensure Mac OS X does not pop up a modal dialog for the Address Book. |
260 autofill::test::DisableSystemServices(profile()->GetPrefs()); | 259 test::DisableSystemServices(manager_delegate_.GetPrefs()); |
261 | |
262 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL); | |
263 | |
264 TabAutofillManagerDelegate::CreateForWebContents(web_contents()); | |
265 autofill::TabAutofillManagerDelegate* manager_delegate = | |
266 autofill::TabAutofillManagerDelegate::FromWebContents(web_contents()); | |
267 | 260 |
268 personal_data_.reset(new TestPersonalDataManager()); | 261 personal_data_.reset(new TestPersonalDataManager()); |
269 personal_data_->set_database(manager_delegate->GetDatabase()); | 262 personal_data_->set_database(manager_delegate_.GetDatabase()); |
270 personal_data_->SetPrefService(profile()->GetPrefs()); | 263 personal_data_->SetPrefService(manager_delegate_.GetPrefs()); |
271 autofill_driver_.reset(new TestAutofillDriver()); | 264 autofill_driver_.reset(new TestAutofillDriver()); |
272 autofill_manager_.reset(new TestAutofillManager( | 265 autofill_manager_.reset(new TestAutofillManager( |
273 autofill_driver_.get(), manager_delegate, personal_data_.get())); | 266 autofill_driver_.get(), &manager_delegate_, personal_data_.get())); |
274 | 267 |
275 external_delegate_.reset(new AutofillExternalDelegate( | 268 external_delegate_.reset(new AutofillExternalDelegate( |
276 autofill_manager_.get(), | 269 autofill_manager_.get(), |
277 autofill_driver_.get())); | 270 autofill_driver_.get())); |
278 autofill_manager_->SetExternalDelegate(external_delegate_.get()); | 271 autofill_manager_->SetExternalDelegate(external_delegate_.get()); |
279 } | 272 } |
280 | 273 |
281 void AutofillMetricsTest::TearDown() { | 274 void AutofillMetricsTest::TearDown() { |
282 // Order of destruction is important as AutofillManager relies on | 275 // Order of destruction is important as AutofillManager relies on |
283 // PersonalDataManager to be around when it gets destroyed. | 276 // PersonalDataManager to be around when it gets destroyed. |
284 autofill_manager_.reset(); | 277 autofill_manager_.reset(); |
285 autofill_driver_.reset(); | 278 autofill_driver_.reset(); |
286 personal_data_.reset(); | 279 personal_data_.reset(); |
287 ChromeRenderViewHostTestHarness::TearDown(); | |
288 } | 280 } |
289 | 281 |
290 // Test that we log quality metrics appropriately. | 282 // Test that we log quality metrics appropriately. |
291 TEST_F(AutofillMetricsTest, QualityMetrics) { | 283 TEST_F(AutofillMetricsTest, QualityMetrics) { |
292 // Set up our form data. | 284 // Set up our form data. |
293 FormData form; | 285 FormData form; |
294 form.name = ASCIIToUTF16("TestForm"); | 286 form.name = ASCIIToUTF16("TestForm"); |
295 form.method = ASCIIToUTF16("POST"); | 287 form.method = ASCIIToUTF16("POST"); |
296 form.origin = GURL("http://example.com/form.html"); | 288 form.origin = GURL("http://example.com/form.html"); |
297 form.action = GURL("http://example.com/submit.html"); | 289 form.action = GURL("http://example.com/submit.html"); |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 EXPECT_CALL(*personal_data_->metric_logger(), | 581 EXPECT_CALL(*personal_data_->metric_logger(), |
590 LogStoredProfileCount(::testing::_)).Times(0); | 582 LogStoredProfileCount(::testing::_)).Times(0); |
591 personal_data_->LoadProfiles(); | 583 personal_data_->LoadProfiles(); |
592 } | 584 } |
593 | 585 |
594 // Test that we correctly log when Autofill is enabled. | 586 // Test that we correctly log when Autofill is enabled. |
595 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) { | 587 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) { |
596 personal_data_->set_autofill_enabled(true); | 588 personal_data_->set_autofill_enabled(true); |
597 EXPECT_CALL(*personal_data_->metric_logger(), | 589 EXPECT_CALL(*personal_data_->metric_logger(), |
598 LogIsAutofillEnabledAtStartup(true)).Times(1); | 590 LogIsAutofillEnabledAtStartup(true)).Times(1); |
599 autofill::TabAutofillManagerDelegate* manager_delegate = | 591 personal_data_->Init( |
600 autofill::TabAutofillManagerDelegate::FromWebContents(web_contents()); | 592 manager_delegate_.GetDatabase(), manager_delegate_.GetPrefs(), false); |
601 personal_data_->Init(manager_delegate->GetDatabase(), | |
602 profile()->GetPrefs(), | |
603 profile()->IsOffTheRecord()); | |
604 } | 593 } |
605 | 594 |
606 // Test that we correctly log when Autofill is disabled. | 595 // Test that we correctly log when Autofill is disabled. |
607 TEST_F(AutofillMetricsTest, AutofillIsDisabledAtStartup) { | 596 TEST_F(AutofillMetricsTest, AutofillIsDisabledAtStartup) { |
608 personal_data_->set_autofill_enabled(false); | 597 personal_data_->set_autofill_enabled(false); |
609 EXPECT_CALL(*personal_data_->metric_logger(), | 598 EXPECT_CALL(*personal_data_->metric_logger(), |
610 LogIsAutofillEnabledAtStartup(false)).Times(1); | 599 LogIsAutofillEnabledAtStartup(false)).Times(1); |
611 autofill::TabAutofillManagerDelegate* manager_delegate = | 600 personal_data_->Init( |
612 autofill::TabAutofillManagerDelegate::FromWebContents(web_contents()); | 601 manager_delegate_.GetDatabase(), manager_delegate_.GetPrefs(), false); |
613 personal_data_->Init(manager_delegate->GetDatabase(), | |
614 profile()->GetPrefs(), | |
615 profile()->IsOffTheRecord()); | |
616 } | 602 } |
617 | 603 |
618 // Test that we log the number of Autofill suggestions when filling a form. | 604 // Test that we log the number of Autofill suggestions when filling a form. |
619 TEST_F(AutofillMetricsTest, AddressSuggestionsCount) { | 605 TEST_F(AutofillMetricsTest, AddressSuggestionsCount) { |
620 // Set up our form data. | 606 // Set up our form data. |
621 FormData form; | 607 FormData form; |
622 form.name = ASCIIToUTF16("TestForm"); | 608 form.name = ASCIIToUTF16("TestForm"); |
623 form.method = ASCIIToUTF16("POST"); | 609 form.method = ASCIIToUTF16("POST"); |
624 form.origin = GURL("http://example.com/form.html"); | 610 form.origin = GURL("http://example.com/form.html"); |
625 form.action = GURL("http://example.com/submit.html"); | 611 form.action = GURL("http://example.com/submit.html"); |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1061 TimeTicks::FromInternalValue(5)); | 1047 TimeTicks::FromInternalValue(5)); |
1062 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(), | 1048 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(), |
1063 TimeTicks::FromInternalValue(3)); | 1049 TimeTicks::FromInternalValue(3)); |
1064 autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17)); | 1050 autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17)); |
1065 autofill_manager_->Reset(); | 1051 autofill_manager_->Reset(); |
1066 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger()); | 1052 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger()); |
1067 } | 1053 } |
1068 } | 1054 } |
1069 | 1055 |
1070 } // namespace autofill | 1056 } // namespace autofill |
OLD | NEW |