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

Side by Side Diff: components/autofill/core/browser/autofill_metrics_unittest.cc

Issue 177013005: Componentize AutofillMetricsTest. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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
« no previous file with comments | « chrome/chrome_tests_unit.gypi ('k') | components/components_tests.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
259 // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
260 autofill::test::DisableSystemServices(profile());
Ilya Sherman 2014/03/06 23:20:58 Something like this is still needed, right?
blundell 2014/03/07 15:07:15 Done.
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 257
268 personal_data_.reset(new TestPersonalDataManager()); 258 personal_data_.reset(new TestPersonalDataManager());
269 personal_data_->set_database(manager_delegate->GetDatabase()); 259 personal_data_->set_database(manager_delegate_.GetDatabase());
270 personal_data_->SetPrefService(profile()->GetPrefs()); 260 personal_data_->SetPrefService(manager_delegate_.GetPrefs());
271 autofill_driver_.reset(new TestAutofillDriver()); 261 autofill_driver_.reset(new TestAutofillDriver());
272 autofill_manager_.reset(new TestAutofillManager( 262 autofill_manager_.reset(new TestAutofillManager(
273 autofill_driver_.get(), manager_delegate, personal_data_.get())); 263 autofill_driver_.get(), &manager_delegate_, personal_data_.get()));
274 264
275 external_delegate_.reset(new AutofillExternalDelegate( 265 external_delegate_.reset(new AutofillExternalDelegate(
276 autofill_manager_.get(), 266 autofill_manager_.get(),
277 autofill_driver_.get())); 267 autofill_driver_.get()));
278 autofill_manager_->SetExternalDelegate(external_delegate_.get()); 268 autofill_manager_->SetExternalDelegate(external_delegate_.get());
279 } 269 }
280 270
281 void AutofillMetricsTest::TearDown() { 271 void AutofillMetricsTest::TearDown() {
282 // Order of destruction is important as AutofillManager relies on 272 // Order of destruction is important as AutofillManager relies on
283 // PersonalDataManager to be around when it gets destroyed. 273 // PersonalDataManager to be around when it gets destroyed.
284 autofill_manager_.reset(); 274 autofill_manager_.reset();
285 autofill_driver_.reset(); 275 autofill_driver_.reset();
286 personal_data_.reset(); 276 personal_data_.reset();
287 ChromeRenderViewHostTestHarness::TearDown();
288 } 277 }
289 278
290 // Test that we log quality metrics appropriately. 279 // Test that we log quality metrics appropriately.
291 TEST_F(AutofillMetricsTest, QualityMetrics) { 280 TEST_F(AutofillMetricsTest, QualityMetrics) {
292 // Set up our form data. 281 // Set up our form data.
293 FormData form; 282 FormData form;
294 form.name = ASCIIToUTF16("TestForm"); 283 form.name = ASCIIToUTF16("TestForm");
295 form.method = ASCIIToUTF16("POST"); 284 form.method = ASCIIToUTF16("POST");
296 form.origin = GURL("http://example.com/form.html"); 285 form.origin = GURL("http://example.com/form.html");
297 form.action = GURL("http://example.com/submit.html"); 286 form.action = GURL("http://example.com/submit.html");
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 EXPECT_CALL(*personal_data_->metric_logger(), 578 EXPECT_CALL(*personal_data_->metric_logger(),
590 LogStoredProfileCount(::testing::_)).Times(0); 579 LogStoredProfileCount(::testing::_)).Times(0);
591 personal_data_->LoadProfiles(); 580 personal_data_->LoadProfiles();
592 } 581 }
593 582
594 // Test that we correctly log when Autofill is enabled. 583 // Test that we correctly log when Autofill is enabled.
595 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) { 584 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) {
596 personal_data_->set_autofill_enabled(true); 585 personal_data_->set_autofill_enabled(true);
597 EXPECT_CALL(*personal_data_->metric_logger(), 586 EXPECT_CALL(*personal_data_->metric_logger(),
598 LogIsAutofillEnabledAtStartup(true)).Times(1); 587 LogIsAutofillEnabledAtStartup(true)).Times(1);
599 autofill::TabAutofillManagerDelegate* manager_delegate = 588 personal_data_->Init(
600 autofill::TabAutofillManagerDelegate::FromWebContents(web_contents()); 589 manager_delegate_.GetDatabase(), manager_delegate_.GetPrefs(), false);
601 personal_data_->Init(manager_delegate->GetDatabase(),
602 profile()->GetPrefs(),
603 profile()->IsOffTheRecord());
604 } 590 }
605 591
606 // Test that we correctly log when Autofill is disabled. 592 // Test that we correctly log when Autofill is disabled.
607 TEST_F(AutofillMetricsTest, AutofillIsDisabledAtStartup) { 593 TEST_F(AutofillMetricsTest, AutofillIsDisabledAtStartup) {
608 personal_data_->set_autofill_enabled(false); 594 personal_data_->set_autofill_enabled(false);
609 EXPECT_CALL(*personal_data_->metric_logger(), 595 EXPECT_CALL(*personal_data_->metric_logger(),
610 LogIsAutofillEnabledAtStartup(false)).Times(1); 596 LogIsAutofillEnabledAtStartup(false)).Times(1);
611 autofill::TabAutofillManagerDelegate* manager_delegate = 597 personal_data_->Init(
612 autofill::TabAutofillManagerDelegate::FromWebContents(web_contents()); 598 manager_delegate_.GetDatabase(), manager_delegate_.GetPrefs(), false);
613 personal_data_->Init(manager_delegate->GetDatabase(),
614 profile()->GetPrefs(),
615 profile()->IsOffTheRecord());
616 } 599 }
617 600
618 // Test that we log the number of Autofill suggestions when filling a form. 601 // Test that we log the number of Autofill suggestions when filling a form.
619 TEST_F(AutofillMetricsTest, AddressSuggestionsCount) { 602 TEST_F(AutofillMetricsTest, AddressSuggestionsCount) {
620 // Set up our form data. 603 // Set up our form data.
621 FormData form; 604 FormData form;
622 form.name = ASCIIToUTF16("TestForm"); 605 form.name = ASCIIToUTF16("TestForm");
623 form.method = ASCIIToUTF16("POST"); 606 form.method = ASCIIToUTF16("POST");
624 form.origin = GURL("http://example.com/form.html"); 607 form.origin = GURL("http://example.com/form.html");
625 form.action = GURL("http://example.com/submit.html"); 608 form.action = GURL("http://example.com/submit.html");
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 TimeTicks::FromInternalValue(5)); 1044 TimeTicks::FromInternalValue(5));
1062 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(), 1045 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1063 TimeTicks::FromInternalValue(3)); 1046 TimeTicks::FromInternalValue(3));
1064 autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17)); 1047 autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1065 autofill_manager_->Reset(); 1048 autofill_manager_->Reset();
1066 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger()); 1049 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1067 } 1050 }
1068 } 1051 }
1069 1052
1070 } // namespace autofill 1053 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/chrome_tests_unit.gypi ('k') | components/components_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698