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

Side by Side Diff: components/autofill/core/browser/webdata/autocomplete_sync_bridge_unittest.cc

Issue 2582713003: [USS] Impelementation for GetData and GetAllData (Closed)
Patch Set: self review Created 4 years 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/autofill/core/browser/webdata/autocomplete_sync_bridge.h"
6
7 #include "base/bind.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "base/time/time.h"
15 #include "components/autofill/core/browser/webdata/autofill_table.h"
16 #include "components/autofill/core/browser/webdata/autofill_webdata_backend.h"
17 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
18 #include "components/sync/model/data_batch.h"
19 #include "components/sync/model/fake_model_type_change_processor.h"
20 #include "components/sync/model/metadata_batch.h"
21 #include "components/webdata/common/web_database.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 using base::ASCIIToUTF16;
25 using base::Time;
26 using base::TimeDelta;
27 using sync_pb::AutofillSpecifics;
28 using syncer::SyncError;
29
30 namespace autofill {
31
32 namespace {
33
34 const char kNameFormat[] = "name %d";
35 const char kValueFormat[] = "value %d";
36
37 void VerifyEqual(const AutofillSpecifics& s1, const AutofillSpecifics& s2) {
skym 2016/12/19 17:40:09 Can you just call SerializeAsString() on both and
Gang Wu 2016/12/19 23:28:10 Done.
skym 2017/01/09 19:59:19 I think I was wrong here to suggest this. Seeing t
38 EXPECT_EQ(s1.name(), s2.name());
39 EXPECT_EQ(s1.value(), s2.value());
40 EXPECT_EQ(Time::FromInternalValue(s1.usage_timestamp(0)),
41 Time::FromInternalValue(s2.usage_timestamp(0)));
42 EXPECT_EQ(s1.has_profile(), s2.has_profile());
43 if (s1.has_profile() && s2.has_profile()) {
44 EXPECT_EQ(s1.profile().guid(), s2.profile().guid());
45 EXPECT_EQ(s1.profile().origin(), s2.profile().origin());
46 EXPECT_EQ(s1.profile().use_count(), s2.profile().use_count());
47 EXPECT_EQ(s1.profile().use_date(), s2.profile().use_date());
48 EXPECT_EQ(s1.profile().name_first(0), s2.profile().name_first(0));
49 EXPECT_EQ(s1.profile().name_middle(0), s2.profile().name_middle(0));
50 EXPECT_EQ(s1.profile().name_last(0), s2.profile().name_last(0));
51 EXPECT_EQ(s1.profile().name_full(0), s2.profile().name_full(0));
52 EXPECT_EQ(s1.profile().email_address(0), s2.profile().email_address(0));
53 EXPECT_EQ(s1.profile().company_name(), s2.profile().company_name());
54 EXPECT_EQ(s1.profile().address_home_line1(),
55 s2.profile().address_home_line1());
56 EXPECT_EQ(s1.profile().address_home_line2(),
57 s2.profile().address_home_line2());
58 EXPECT_EQ(s1.profile().address_home_city(),
59 s2.profile().address_home_city());
60 EXPECT_EQ(s1.profile().address_home_state(),
61 s2.profile().address_home_state());
62 EXPECT_EQ(s1.profile().address_home_zip(), s2.profile().address_home_zip());
63 EXPECT_EQ(s1.profile().address_home_country(),
64 s2.profile().address_home_country());
65 EXPECT_EQ(s1.profile().address_home_street_address(),
66 s2.profile().address_home_street_address());
67 EXPECT_EQ(s1.profile().address_home_sorting_code(),
68 s2.profile().address_home_sorting_code());
69 EXPECT_EQ(s1.profile().address_home_dependent_locality(),
70 s2.profile().address_home_dependent_locality());
71 EXPECT_EQ(s1.profile().address_home_language_code(),
72 s2.profile().address_home_language_code());
73 EXPECT_EQ(s1.profile().phone_home_whole_number(0),
74 s2.profile().phone_home_whole_number(0));
75 EXPECT_EQ(s1.profile().guid(), s2.profile().guid());
76 }
77 }
78
79 void VerifyDataBatch(std::map<std::string, AutofillSpecifics> expected,
skym 2016/12/19 17:40:09 Hmm, I wonder if we could make a templated version
Gang Wu 2016/12/19 23:28:11 Acknowledged.
80 SyncError error,
81 std::unique_ptr<syncer::DataBatch> batch) {
82 EXPECT_FALSE(error.IsSet());
83 while (batch->HasNext()) {
84 const syncer::KeyAndData& pair = batch->Next();
85 auto iter = expected.find(pair.first);
86 ASSERT_NE(iter, expected.end());
87 VerifyEqual(iter->second, pair.second->specifics.autofill());
88 // Removing allows us to verify we don't see the same item multiple times,
89 // and that we saw everything we expected.
90 expected.erase(iter);
91 }
92 EXPECT_TRUE(expected.empty());
93 }
94
95 AutofillEntry* MakeAutofillEntry(const char* name,
skym 2016/12/19 17:40:08 Seems like a bunch of code is getting copied out o
Gang Wu 2016/12/19 23:28:10 Done.
96 const char* value,
97 int time_shift0,
skym 2016/12/19 17:40:08 time_shift<n> is a weird naming scheme. Also, thi
Gang Wu 2016/12/19 23:28:10 Done.
98 int time_shift1) {
99 // Time deep in the past would cause Autocomplete sync to discard the
100 // entries.
101 static Time base_time = Time::Now().LocalMidnight();
102
103 Time date_created = base_time + TimeDelta::FromSeconds(time_shift0);
104 Time date_last_used = date_created;
105 if (time_shift1 >= 0)
106 date_last_used = base_time + TimeDelta::FromSeconds(time_shift1);
107 return new AutofillEntry(AutofillKey(ASCIIToUTF16(name), ASCIIToUTF16(value)),
108 date_created, date_last_used);
109 }
110
111 class FakeAutofillBackend : public AutofillWebDataBackend {
112 public:
113 FakeAutofillBackend() {}
114 ~FakeAutofillBackend() override {}
115 WebDatabase* GetDatabase() override { return db_; }
116 void AddObserver(
117 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {}
118 void RemoveObserver(
119 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {}
120 void RemoveExpiredFormElements() override {}
121 void NotifyOfMultipleAutofillChanges() override {}
122 void NotifyThatSyncHasStarted(syncer::ModelType /* model_type */) override {}
skym 2016/12/19 17:40:09 I didn't know you didn't have to name the variable
Gang Wu 2016/12/19 23:28:10 Done.
123 void SetWebDatabase(WebDatabase* db) { db_ = db; }
124
125 private:
126 WebDatabase* db_;
127 };
128
129 class FakeWebDataService : public AutofillWebDataService {
skym 2016/12/19 17:40:08 I'm probably missing something, but I don't unders
Gang Wu 2016/12/19 23:28:10 Done.
130 public:
131 FakeWebDataService()
132 : AutofillWebDataService(base::ThreadTaskRunnerHandle::Get(),
133 base::ThreadTaskRunnerHandle::Get()) {}
134
135 protected:
136 ~FakeWebDataService() override {}
137 };
138
139 } // namespace
140
141 class AutocompleteSyncBridgeTest : public testing::Test {
142 public:
143 AutocompleteSyncBridgeTest() {}
144 ~AutocompleteSyncBridgeTest() override {}
145
146 AutocompleteSyncBridge* Bridge() { return bridge_.get(); }
skym 2016/12/19 17:40:09 I think I typically see this kind of function name
Gang Wu 2016/12/19 23:28:10 Done.
147
148 protected:
149 std::unique_ptr<syncer::ModelTypeChangeProcessor>
150 CreateModelTypeChangeProcessor(syncer::ModelType type,
skym 2016/12/19 17:40:09 This method doesn't need to be an instanced method
Gang Wu 2016/12/19 23:28:10 Done.
151 syncer::ModelTypeSyncBridge* bridge) {
152 auto processor = base::MakeUnique<syncer::FakeModelTypeChangeProcessor>();
153 return std::move(processor);
154 }
155
156 void SaveSpecificsToTable(const AutofillSpecifics& specifics) {
157 std::vector<AutofillEntry> new_entries;
158 new_entries.push_back(
159 AutocompleteSyncBridge::CreateAutofillEntry(&specifics));
160 table_->UpdateAutofillEntries(new_entries);
skym 2016/12/19 17:40:08 You can use C++11 list-initialization to inline th
Gang Wu 2016/12/19 23:28:10 Acknowledged.
161 }
162
163 AutofillSpecifics CreateSpecifics(int suffix) {
164 AutofillSpecifics specifics;
165 specifics.set_name(base::StringPrintf(kNameFormat, suffix));
166 specifics.set_value(base::StringPrintf(kValueFormat, suffix));
167 specifics.add_usage_timestamp(entry_->date_created().ToInternalValue());
168 return specifics;
169 }
170
171 std::string GetStorageKey(const AutofillSpecifics& specifics) {
172 return AutocompleteSyncBridge::FormatStorageKey(specifics.name(),
173 specifics.value());
174 }
175
176 void SetUp() override {
skym 2016/12/19 17:40:08 Seems to me like you could move most of the conten
Gang Wu 2016/12/19 23:28:11 Done. since assert cannot be in constructor, so I
177 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
178 file_ = temp_dir_.GetPath().AppendASCII("SyncTestWebDatabase");
179
180 web_data_service_ = new FakeWebDataService();
181 table_.reset(new AutofillTable);
skym 2016/12/19 17:40:08 table_ = base::MakeUnique<AutofillTable>();
Gang Wu 2016/12/19 23:28:10 Done.
182 db_.reset(new WebDatabase);
skym 2016/12/19 17:40:08 MakeUnique
Gang Wu 2016/12/19 23:28:10 Done.
183 db_->AddTable(table_.get());
184 ASSERT_EQ(sql::INIT_OK, db_->Init(file_));
185 backend_.SetWebDatabase(db_.get());
186
187 entry_.reset(MakeAutofillEntry("0", "0", 0, -1));
188
189 bridge_.reset(new AutocompleteSyncBridge(
190 &backend_,
191 base::Bind(&AutocompleteSyncBridgeTest::CreateModelTypeChangeProcessor,
192 base::Unretained(this))));
193 }
194
195 void TearDown() override {}
196
197 base::FilePath file_;
198 base::MessageLoop message_loop_;
199 base::ScopedTempDir temp_dir_;
200 FakeAutofillBackend backend_;
201 scoped_refptr<FakeWebDataService> web_data_service_;
202 std::unique_ptr<AutofillTable> table_;
203 std::unique_ptr<WebDatabase> db_;
204 std::unique_ptr<AutocompleteSyncBridge> bridge_;
205 std::unique_ptr<AutofillEntry> entry_;
skym 2016/12/19 17:40:09 I don't like this entry instance field you're hold
Gang Wu 2016/12/19 23:28:10 Done.
206
207 private:
208 DISALLOW_COPY_AND_ASSIGN(AutocompleteSyncBridgeTest);
209 };
210
211 TEST_F(AutocompleteSyncBridgeTest, GetData) {
212 const AutofillSpecifics specifics1 = CreateSpecifics(1);
213 const AutofillSpecifics specifics2 = CreateSpecifics(2);
214 const AutofillSpecifics specifics3 = CreateSpecifics(3);
215 SaveSpecificsToTable(specifics1);
skym 2016/12/19 17:40:09 Would it be nicer if you could pass a vector of sp
Gang Wu 2016/12/19 23:28:10 Done.
216 SaveSpecificsToTable(specifics2);
217 SaveSpecificsToTable(specifics3);
218
219 const std::map<std::string, AutofillSpecifics> expected{
220 {GetStorageKey(specifics1), specifics1},
221 {GetStorageKey(specifics3), specifics3}};
222 Bridge()->GetData({GetStorageKey(specifics1), GetStorageKey(specifics3)},
skym 2016/12/19 17:40:08 You should also test calling GetData for a key tha
Gang Wu 2016/12/19 23:28:10 Done.
223 base::Bind(&VerifyDataBatch, expected));
224 }
225
226 TEST_F(AutocompleteSyncBridgeTest, GetAllData) {
227 const AutofillSpecifics specifics1 = CreateSpecifics(1);
228 const AutofillSpecifics specifics2 = CreateSpecifics(2);
229 const AutofillSpecifics specifics3 = CreateSpecifics(3);
230 SaveSpecificsToTable(specifics1);
231 SaveSpecificsToTable(specifics2);
232 SaveSpecificsToTable(specifics3);
233
234 const std::map<std::string, AutofillSpecifics> expected{
235 {GetStorageKey(specifics1), specifics1},
236 {GetStorageKey(specifics2), specifics2},
237 {GetStorageKey(specifics3), specifics3}};
238 Bridge()->GetAllData(base::Bind(&VerifyDataBatch, expected));
239 }
240
241 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698