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

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: Response to review 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 web_database_ = new WebDatabaseService(
72 path,
73 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
74 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB));
75 web_database_->AddTable(
76 scoped_ptr<WebDatabaseTable>(new AutofillTable("en-US")));
77 web_database_->LoadDatabase();
78 autofill_database_service_ = new AutofillWebDataService(
79 web_database_,
80 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
81 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
82 WebDataServiceBase::ProfileErrorCallback());
83 autofill_database_service_->Init();
84
66 profile_.reset(new TestingProfile); 85 profile_.reset(new TestingProfile);
67 profile_->CreateWebDataService(); 86 profile_->CreateWebDataService();
68 87
69 test::DisableSystemServices(profile_.get()); 88 test::DisableSystemServices(profile_.get());
70 ResetPersonalDataManager(); 89 ResetPersonalDataManager();
71 } 90 }
72 91
73 virtual void TearDown() { 92 virtual void TearDown() {
74 // Destruction order is imposed explicitly here. 93 // Destruction order is imposed explicitly here.
75 personal_data_.reset(NULL); 94 personal_data_.reset(NULL);
76 profile_.reset(NULL); 95 profile_.reset(NULL);
77 96
97 autofill_database_service_->ShutdownOnUIThread();
98 web_database_->ShutdownDatabase();
99 autofill_database_service_ = NULL;
100 web_database_ = NULL;
101
78 // Schedule another task on the DB thread to notify us that it's safe to 102 // Schedule another task on the DB thread to notify us that it's safe to
79 // stop the thread. 103 // stop the thread.
80 base::WaitableEvent done(false, false); 104 base::WaitableEvent done(false, false);
81 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 105 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
82 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); 106 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
83 done.Wait(); 107 done.Wait();
84 base::MessageLoop::current()->PostTask(FROM_HERE, 108 base::MessageLoop::current()->PostTask(FROM_HERE,
85 base::MessageLoop::QuitClosure()); 109 base::MessageLoop::QuitClosure());
86 base::MessageLoop::current()->Run(); 110 base::MessageLoop::current()->Run();
87 db_thread_.Stop(); 111 db_thread_.Stop();
88 } 112 }
89 113
90 void ResetPersonalDataManager() { 114 void ResetPersonalDataManager() {
91 personal_data_.reset(new PersonalDataManager("en-US")); 115 personal_data_.reset(new PersonalDataManager("en-US"));
92 personal_data_->Init(profile_.get(), 116 personal_data_->Init(
93 profile_->GetPrefs(), 117 scoped_refptr<AutofillWebDataService>(autofill_database_service_),
94 profile_->IsOffTheRecord()); 118 profile_->GetPrefs(),
119 profile_->IsOffTheRecord());
95 personal_data_->AddObserver(&personal_data_observer_); 120 personal_data_->AddObserver(&personal_data_observer_);
96 121
97 // Verify that the web database has been updated and the notification sent. 122 // Verify that the web database has been updated and the notification sent.
98 EXPECT_CALL(personal_data_observer_, 123 EXPECT_CALL(personal_data_observer_,
99 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop()); 124 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
100 base::MessageLoop::current()->Run(); 125 base::MessageLoop::current()->Run();
101 } 126 }
102 127
103 void MakeProfileIncognito() { 128 void MakeProfileIncognito() {
104 // Switch to an incognito profile. 129 // Switch to an incognito profile.
105 profile_->ForceIncognito(true); 130 profile_->ForceIncognito(true);
106 DCHECK(profile_->IsOffTheRecord()); 131 DCHECK(profile_->IsOffTheRecord());
107 } 132 }
108 133
109 base::MessageLoopForUI message_loop_; 134 base::MessageLoopForUI message_loop_;
110 content::TestBrowserThread ui_thread_; 135 content::TestBrowserThread ui_thread_;
111 content::TestBrowserThread db_thread_; 136 content::TestBrowserThread db_thread_;
112 scoped_ptr<TestingProfile> profile_; 137 scoped_ptr<TestingProfile> profile_;
138 scoped_refptr<AutofillWebDataService> autofill_database_service_;
139 scoped_refptr<WebDatabaseService> web_database_;
140 base::ScopedTempDir temp_dir_;
113 scoped_ptr<PersonalDataManager> personal_data_; 141 scoped_ptr<PersonalDataManager> personal_data_;
114 PersonalDataLoadedObserverMock personal_data_observer_; 142 PersonalDataLoadedObserverMock personal_data_observer_;
115 }; 143 };
116 144
117 TEST_F(PersonalDataManagerTest, AddProfile) { 145 TEST_F(PersonalDataManagerTest, AddProfile) {
118 // Add profile0 to the database. 146 // Add profile0 to the database.
119 AutofillProfile profile0(autofill::test::GetFullProfile()); 147 AutofillProfile profile0(autofill::test::GetFullProfile());
120 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com")); 148 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com"));
121 personal_data_->AddProfile(profile0); 149 personal_data_->AddProfile(profile0);
122 150
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com"); 556 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
529 test::SetProfileInfo(&profile2, 557 test::SetProfileInfo(&profile2,
530 "Josephine", "Alicia", "Saenz", 558 "Josephine", "Alicia", "Saenz",
531 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL", 559 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
532 "32801", "US", "19482937549"); 560 "32801", "US", "19482937549");
533 561
534 // Adjust all labels. 562 // Adjust all labels.
535 profile_pointers.push_back(&profile2); 563 profile_pointers.push_back(&profile2);
536 AutofillProfile::AdjustInferredLabels(&profile_pointers); 564 AutofillProfile::AdjustInferredLabels(&profile_pointers);
537 565
538 scoped_refptr<AutofillWebDataService> wds = 566 autofill_database_service_->AddAutofillProfile(profile2);
539 AutofillWebDataService::FromBrowserContext(profile_.get());
540 ASSERT_TRUE(wds.get());
541 wds->AddAutofillProfile(profile2);
542 567
543 personal_data_->Refresh(); 568 personal_data_->Refresh();
544 569
545 // Verify that the web database has been updated and the notification sent. 570 // Verify that the web database has been updated and the notification sent.
546 EXPECT_CALL(personal_data_observer_, 571 EXPECT_CALL(personal_data_observer_,
547 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop()); 572 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
548 base::MessageLoop::current()->Run(); 573 base::MessageLoop::current()->Run();
549 574
550 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles(); 575 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
551 ASSERT_EQ(3U, results2.size()); 576 ASSERT_EQ(3U, results2.size());
552 EXPECT_EQ(profile0, *results2[0]); 577 EXPECT_EQ(profile0, *results2[0]);
553 EXPECT_EQ(profile1, *results2[1]); 578 EXPECT_EQ(profile1, *results2[1]);
554 EXPECT_EQ(profile2, *results2[2]); 579 EXPECT_EQ(profile2, *results2[2]);
555 580
556 wds->RemoveAutofillProfile(profile1.guid()); 581 autofill_database_service_->RemoveAutofillProfile(profile1.guid());
557 wds->RemoveAutofillProfile(profile2.guid()); 582 autofill_database_service_->RemoveAutofillProfile(profile2.guid());
558 583
559 // Before telling the PDM to refresh, simulate an edit to one of the deleted 584 // 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 585 // profiles via a SetProfile update (this would happen if the Autofill window
561 // was open with a previous snapshot of the profiles, and something 586 // 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 587 // [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). 588 // end up in a consistent state by dropping the write).
564 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Mar")); 589 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Mar"));
565 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo")); 590 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo"));
566 personal_data_->UpdateProfile(profile0); 591 personal_data_->UpdateProfile(profile0);
567 personal_data_->AddProfile(profile1); 592 personal_data_->AddProfile(profile1);
(...skipping 1915 matching lines...) Expand 10 before | Expand all | Expand 10 after
2483 AutofillProfile space_invader(base::GenerateGUID(), "Chrome settings"); 2508 AutofillProfile space_invader(base::GenerateGUID(), "Chrome settings");
2484 test::SetProfileInfo(&space_invader, "Marty", "", "Martian", 2509 test::SetProfileInfo(&space_invader, "Marty", "", "Martian",
2485 "mm@example.com", "", "1 Flying Object", "", "Valles Marineris", "", 2510 "mm@example.com", "", "1 Flying Object", "", "Valles Marineris", "",
2486 "", "XX", ""); 2511 "", "XX", "");
2487 personal_data_->AddProfile(moose); 2512 personal_data_->AddProfile(moose);
2488 ResetPersonalDataManager(); 2513 ResetPersonalDataManager();
2489 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress()); 2514 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2490 } 2515 }
2491 2516
2492 } // namespace autofill 2517 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698