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

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

Issue 71683003: Have AutofillManagerDelegate supply the AutofillWebDataService to core code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Android fix Created 7 years, 1 month 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
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 <string> 5 #include <string>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/guid.h" 8 #include "base/guid.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
13 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
14 #include "components/autofill/core/browser/autofill_metrics.h" 14 #include "components/autofill/core/browser/autofill_metrics.h"
15 #include "components/autofill/core/browser/autofill_profile.h" 15 #include "components/autofill/core/browser/autofill_profile.h"
16 #include "components/autofill/core/browser/autofill_test_utils.h" 16 #include "components/autofill/core/browser/autofill_test_utils.h"
17 #include "components/autofill/core/browser/form_structure.h" 17 #include "components/autofill/core/browser/form_structure.h"
18 #include "components/autofill/core/browser/personal_data_manager.h" 18 #include "components/autofill/core/browser/personal_data_manager.h"
19 #include "components/autofill/core/browser/personal_data_manager_observer.h" 19 #include "components/autofill/core/browser/personal_data_manager_observer.h"
20 #include "components/autofill/core/browser/webdata/autofill_table.h"
20 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" 21 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
21 #include "components/autofill/core/common/form_data.h" 22 #include "components/autofill/core/common/form_data.h"
23 #include "components/webdata/common/web_data_service_base.h"
24 #include "components/webdata/common/web_database_service.h"
22 #include "components/webdata/encryptor/encryptor.h" 25 #include "components/webdata/encryptor/encryptor.h"
23 #include "content/public/test/test_browser_thread.h" 26 #include "content/public/test/test_browser_thread.h"
24 #include "testing/gmock/include/gmock/gmock.h" 27 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
26 29
27 using content::BrowserThread; 30 using content::BrowserThread;
28 31
29 namespace autofill { 32 namespace autofill {
30 namespace { 33 namespace {
31 34
(...skipping 24 matching lines...) Expand all
56 class PersonalDataManagerTest : public testing::Test { 59 class PersonalDataManagerTest : public testing::Test {
57 protected: 60 protected:
58 PersonalDataManagerTest() 61 PersonalDataManagerTest()
59 : ui_thread_(BrowserThread::UI, &message_loop_), 62 : ui_thread_(BrowserThread::UI, &message_loop_),
60 db_thread_(BrowserThread::DB) { 63 db_thread_(BrowserThread::DB) {
61 } 64 }
62 65
63 virtual void SetUp() { 66 virtual void SetUp() {
64 db_thread_.Start(); 67 db_thread_.Start();
65 68
69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
70 base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB");
71 wdbs_ = new WebDatabaseService(
72 path,
73 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
74 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB));
75 wdbs_->AddTable(scoped_ptr<WebDatabaseTable>(new AutofillTable("en-US")));
76 wdbs_->LoadDatabase();
77 wds_ = new AutofillWebDataService(
78 wdbs_,
79 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
80 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
81 WebDataServiceBase::ProfileErrorCallback());
82 wds_->Init();
83
66 profile_.reset(new TestingProfile); 84 profile_.reset(new TestingProfile);
67 profile_->CreateWebDataService(); 85 profile_->CreateWebDataService();
68 86
69 test::DisableSystemServices(profile_.get()); 87 test::DisableSystemServices(profile_.get());
70 ResetPersonalDataManager(); 88 ResetPersonalDataManager();
71 } 89 }
72 90
73 virtual void TearDown() { 91 virtual void TearDown() {
74 // Destruction order is imposed explicitly here. 92 // Destruction order is imposed explicitly here.
75 personal_data_.reset(NULL); 93 personal_data_.reset(NULL);
76 profile_.reset(NULL); 94 profile_.reset(NULL);
77 95
96 wds_->ShutdownOnUIThread();
97 wdbs_->ShutdownDatabase();
98 wds_ = NULL;
99 wdbs_ = NULL;
100
78 // Schedule another task on the DB thread to notify us that it's safe to 101 // Schedule another task on the DB thread to notify us that it's safe to
79 // stop the thread. 102 // stop the thread.
80 base::WaitableEvent done(false, false); 103 base::WaitableEvent done(false, false);
81 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 104 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
82 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); 105 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
83 done.Wait(); 106 done.Wait();
84 base::MessageLoop::current()->PostTask(FROM_HERE, 107 base::MessageLoop::current()->PostTask(FROM_HERE,
85 base::MessageLoop::QuitClosure()); 108 base::MessageLoop::QuitClosure());
86 base::MessageLoop::current()->Run(); 109 base::MessageLoop::current()->Run();
87 db_thread_.Stop(); 110 db_thread_.Stop();
88 } 111 }
89 112
90 void ResetPersonalDataManager() { 113 void ResetPersonalDataManager() {
91 personal_data_.reset(new PersonalDataManager("en-US")); 114 personal_data_.reset(new PersonalDataManager("en-US"));
92 personal_data_->Init(profile_.get(), 115 personal_data_->Init(scoped_refptr<AutofillWebDataService>(wds_),
93 profile_->GetPrefs(), 116 profile_->GetPrefs(),
94 profile_->IsOffTheRecord()); 117 profile_->IsOffTheRecord());
95 personal_data_->AddObserver(&personal_data_observer_); 118 personal_data_->AddObserver(&personal_data_observer_);
96 119
97 // Verify that the web database has been updated and the notification sent. 120 // Verify that the web database has been updated and the notification sent.
98 EXPECT_CALL(personal_data_observer_, 121 EXPECT_CALL(personal_data_observer_,
99 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop()); 122 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
100 base::MessageLoop::current()->Run(); 123 base::MessageLoop::current()->Run();
101 } 124 }
102 125
103 void MakeProfileIncognito() { 126 void MakeProfileIncognito() {
104 // Switch to an incognito profile. 127 // Switch to an incognito profile.
105 profile_->ForceIncognito(true); 128 profile_->ForceIncognito(true);
106 DCHECK(profile_->IsOffTheRecord()); 129 DCHECK(profile_->IsOffTheRecord());
107 } 130 }
108 131
109 base::MessageLoopForUI message_loop_; 132 base::MessageLoopForUI message_loop_;
110 content::TestBrowserThread ui_thread_; 133 content::TestBrowserThread ui_thread_;
111 content::TestBrowserThread db_thread_; 134 content::TestBrowserThread db_thread_;
Ilya Sherman 2013/11/14 23:45:55 We should really use a TestBrowserThreadBundle rat
blundell 2013/11/15 16:04:54 I looked at using the //chrome-level concepts for
Jói 2013/11/15 16:51:33 I agree, don't go back to concepts only available
112 scoped_ptr<TestingProfile> profile_; 135 scoped_ptr<TestingProfile> profile_;
136 scoped_refptr<AutofillWebDataService> wds_;
137 scoped_refptr<WebDatabaseService> wdbs_;
Ilya Sherman 2013/11/14 23:45:55 Please give these more descriptive names, e.g. "au
blundell 2013/11/15 16:04:54 Done.
138 base::ScopedTempDir temp_dir_;
113 scoped_ptr<PersonalDataManager> personal_data_; 139 scoped_ptr<PersonalDataManager> personal_data_;
114 PersonalDataLoadedObserverMock personal_data_observer_; 140 PersonalDataLoadedObserverMock personal_data_observer_;
115 }; 141 };
116 142
117 TEST_F(PersonalDataManagerTest, AddProfile) { 143 TEST_F(PersonalDataManagerTest, AddProfile) {
118 // Add profile0 to the database. 144 // Add profile0 to the database.
119 AutofillProfile profile0(autofill::test::GetFullProfile()); 145 AutofillProfile profile0(autofill::test::GetFullProfile());
120 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com")); 146 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com"));
121 personal_data_->AddProfile(profile0); 147 personal_data_->AddProfile(profile0);
122 148
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com"); 554 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
529 test::SetProfileInfo(&profile2, 555 test::SetProfileInfo(&profile2,
530 "Josephine", "Alicia", "Saenz", 556 "Josephine", "Alicia", "Saenz",
531 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL", 557 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
532 "32801", "US", "19482937549"); 558 "32801", "US", "19482937549");
533 559
534 // Adjust all labels. 560 // Adjust all labels.
535 profile_pointers.push_back(&profile2); 561 profile_pointers.push_back(&profile2);
536 AutofillProfile::AdjustInferredLabels(&profile_pointers); 562 AutofillProfile::AdjustInferredLabels(&profile_pointers);
537 563
538 scoped_refptr<AutofillWebDataService> wds = 564 wds_->AddAutofillProfile(profile2);
539 AutofillWebDataService::FromBrowserContext(profile_.get());
540 ASSERT_TRUE(wds.get());
541 wds->AddAutofillProfile(profile2);
542 565
543 personal_data_->Refresh(); 566 personal_data_->Refresh();
544 567
545 // Verify that the web database has been updated and the notification sent. 568 // Verify that the web database has been updated and the notification sent.
546 EXPECT_CALL(personal_data_observer_, 569 EXPECT_CALL(personal_data_observer_,
547 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop()); 570 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
548 base::MessageLoop::current()->Run(); 571 base::MessageLoop::current()->Run();
549 572
550 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles(); 573 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
551 ASSERT_EQ(3U, results2.size()); 574 ASSERT_EQ(3U, results2.size());
552 EXPECT_EQ(profile0, *results2[0]); 575 EXPECT_EQ(profile0, *results2[0]);
553 EXPECT_EQ(profile1, *results2[1]); 576 EXPECT_EQ(profile1, *results2[1]);
554 EXPECT_EQ(profile2, *results2[2]); 577 EXPECT_EQ(profile2, *results2[2]);
555 578
556 wds->RemoveAutofillProfile(profile1.guid()); 579 wds_->RemoveAutofillProfile(profile1.guid());
557 wds->RemoveAutofillProfile(profile2.guid()); 580 wds_->RemoveAutofillProfile(profile2.guid());
558 581
559 // Before telling the PDM to refresh, simulate an edit to one of the deleted 582 // Before telling the PDM to refresh, simulate an edit to one of the deleted
560 // profiles via a SetProfile update (this would happen if the Autofill window 583 // profiles via a SetProfile update (this would happen if the Autofill window
561 // was open with a previous snapshot of the profiles, and something 584 // was open with a previous snapshot of the profiles, and something
562 // [e.g. sync] removed a profile from the browser. In this edge case, we will 585 // [e.g. sync] removed a profile from the browser. In this edge case, we will
563 // end up in a consistent state by dropping the write). 586 // end up in a consistent state by dropping the write).
564 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Mar")); 587 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Mar"));
565 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo")); 588 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo"));
566 personal_data_->UpdateProfile(profile0); 589 personal_data_->UpdateProfile(profile0);
567 personal_data_->AddProfile(profile1); 590 personal_data_->AddProfile(profile1);
(...skipping 1915 matching lines...) Expand 10 before | Expand all | Expand 10 after
2483 AutofillProfile space_invader(base::GenerateGUID(), "Chrome settings"); 2506 AutofillProfile space_invader(base::GenerateGUID(), "Chrome settings");
2484 test::SetProfileInfo(&space_invader, "Marty", "", "Martian", 2507 test::SetProfileInfo(&space_invader, "Marty", "", "Martian",
2485 "mm@example.com", "", "1 Flying Object", "", "Valles Marineris", "", 2508 "mm@example.com", "", "1 Flying Object", "", "Valles Marineris", "",
2486 "", "XX", ""); 2509 "", "XX", "");
2487 personal_data_->AddProfile(moose); 2510 personal_data_->AddProfile(moose);
2488 ResetPersonalDataManager(); 2511 ResetPersonalDataManager();
2489 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress()); 2512 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2490 } 2513 }
2491 2514
2492 } // namespace autofill 2515 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698