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

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: skym review Created 3 years, 12 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
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) {
38 EXPECT_EQ(s1.SerializeAsString(), s2.SerializeAsString());
39 }
40
41 void VerifyDataBatch(std::map<std::string, AutofillSpecifics> expected,
42 SyncError error,
43 std::unique_ptr<syncer::DataBatch> batch) {
44 EXPECT_FALSE(error.IsSet());
45 while (batch->HasNext()) {
46 const syncer::KeyAndData& pair = batch->Next();
47 auto iter = expected.find(pair.first);
48 ASSERT_NE(iter, expected.end());
49 VerifyEqual(iter->second, pair.second->specifics.autofill());
50 // Removing allows us to verify we don't see the same item multiple times,
51 // and that we saw everything we expected.
52 expected.erase(iter);
53 }
54 EXPECT_TRUE(expected.empty());
55 }
56
57 AutofillEntry MakeAutofillEntry(const char* name,
skym 2016/12/20 17:23:43 What's the reason for const char* instead of const
Gang Wu 2016/12/20 21:55:51 function removed
58 const char* value,
59 int create_time_shift,
skym 2016/12/20 17:23:43 What do you think of taking base::TimeDelta instea
Gang Wu 2016/12/20 21:55:50 Done.
60 int delete_time_shift) {
skym 2016/12/20 17:23:44 Why is this called delete_time_shift? Isn't this t
Gang Wu 2016/12/20 21:55:50 Done.
61 // Time deep in the past would cause Autocomplete sync to discard the
62 // entries.
63 static Time base_time = Time::Now().LocalMidnight();
skym 2016/12/20 17:23:44 I didn't realize this was static. That actually is
Gang Wu 2016/12/20 21:55:50 Done.
64
65 Time date_created = base_time + TimeDelta::FromSeconds(create_time_shift);
skym 2016/12/20 17:23:44 FromSeconds is actually the wrong conversion, righ
Gang Wu 2016/12/20 21:55:50 Done.
66 Time date_last_used = date_created;
67 if (delete_time_shift >= 0)
skym 2016/12/20 17:23:44 Lets remove this >= 0 behavior completely and just
Gang Wu 2016/12/20 21:55:51 Done.
68 date_last_used = base_time + TimeDelta::FromSeconds(delete_time_shift);
69 return AutofillEntry(AutofillKey(ASCIIToUTF16(name), ASCIIToUTF16(value)),
70 date_created, date_last_used);
71 }
72
73 AutofillEntry MakeAutofillEntry(const char* name,
74 const char* value,
75 int time_shift) {
76 return MakeAutofillEntry(name, value, time_shift, -1);
skym 2016/12/20 17:23:43 I think passing -1 is bad. I was trying to suggest
Gang Wu 2016/12/20 21:55:50 function removed.
77 }
78
79 std::unique_ptr<syncer::ModelTypeChangeProcessor>
80 CreateModelTypeChangeProcessor(syncer::ModelType type,
81 syncer::ModelTypeSyncBridge* bridge) {
82 auto processor = base::MakeUnique<syncer::FakeModelTypeChangeProcessor>();
skym 2016/12/20 17:23:43 No reason for this function body to be two lines.
Gang Wu 2016/12/20 21:55:50 Done.
83 return std::move(processor);
84 }
85
86 class FakeAutofillBackend : public AutofillWebDataBackend {
87 public:
88 FakeAutofillBackend() {}
89 ~FakeAutofillBackend() override {}
90 WebDatabase* GetDatabase() override { return db_; }
91 void AddObserver(
92 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {}
93 void RemoveObserver(
94 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {}
95 void RemoveExpiredFormElements() override {}
96 void NotifyOfMultipleAutofillChanges() override {}
97 void NotifyThatSyncHasStarted(syncer::ModelType model_type) override {}
98 void SetWebDatabase(WebDatabase* db) { db_ = db; }
99
100 private:
101 WebDatabase* db_;
102 };
103
104 } // namespace
105
106 class AutocompleteSyncBridgeTest : public testing::Test {
107 public:
108 AutocompleteSyncBridgeTest() : entry_(MakeAutofillEntry("0", "0", 0)) {}
109 ~AutocompleteSyncBridgeTest() override {}
110
111 AutocompleteSyncBridge* bridge() { return bridge_.get(); }
skym 2016/12/20 17:23:43 This can be protected too, right?
Gang Wu 2016/12/20 21:55:51 Done.
112
113 protected:
114 void SaveSpecificsToTable(const std::vector<AutofillSpecifics>& specifics) {
115 std::vector<AutofillEntry> new_entries;
116 for (const auto& specific : specifics) {
skym 2016/12/20 17:23:44 Sorry to get really nit picky here, but I'm not ha
Gang Wu 2016/12/20 21:55:51 Done.
117 new_entries.push_back(
118 AutocompleteSyncBridge::CreateAutofillEntry(specific));
119 }
120 table_.UpdateAutofillEntries(new_entries);
121 }
122
123 AutofillSpecifics CreateSpecifics(int suffix) {
124 AutofillSpecifics specifics;
125 specifics.set_name(base::StringPrintf(kNameFormat, suffix));
126 specifics.set_value(base::StringPrintf(kValueFormat, suffix));
127 specifics.add_usage_timestamp(entry_.date_created().ToInternalValue());
128 return specifics;
129 }
130
131 std::string GetStorageKey(const AutofillSpecifics& specifics) {
132 return AutocompleteSyncBridge::FormatStorageKey(specifics.name(),
133 specifics.value());
134 }
135
136 void SetUp() override {
137 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
138 file_ = temp_dir_.GetPath().AppendASCII("SyncTestWebDatabase");
skym 2016/12/20 17:23:44 This can be in the ctor. It doesn't actually do fi
Gang Wu 2016/12/20 21:55:51 https://cs.chromium.org/search/?q=CreateUniqueTemp
skym 2016/12/20 22:38:05 Looking at your search, there are a couple places
Gang Wu 2016/12/22 01:33:53 Done.
139
140 db_.AddTable(&table_);
skym 2016/12/20 17:23:44 Why not move this into the ctor?
Gang Wu 2016/12/20 21:55:51 same reason, autofill_table_unittest.cc:130, I jus
skym 2016/12/20 22:38:05 See https://github.com/google/googletest/blob/mast
Gang Wu 2016/12/22 01:33:53 Acknowledged.
141 ASSERT_EQ(sql::INIT_OK, db_.Init(file_));
skym 2016/12/20 17:23:44 Probably out of scope of this CL, but we'll likely
Gang Wu 2016/12/20 21:55:50 maybe cover in integration tests?
142 backend_.SetWebDatabase(&db_);
143
144 bridge_.reset(new AutocompleteSyncBridge(
145 &backend_, base::Bind(&CreateModelTypeChangeProcessor)));
146 }
147
148 void TearDown() override {}
skym 2016/12/20 17:23:43 I'd just remove this until we need it.
Gang Wu 2016/12/20 21:55:50 Done.
149
150 base::FilePath file_;
skym 2016/12/20 17:23:44 These should all be private.
Gang Wu 2016/12/20 21:55:51 Done.
151 base::MessageLoop message_loop_;
152 base::ScopedTempDir temp_dir_;
153 FakeAutofillBackend backend_;
154 AutofillTable table_;
155 WebDatabase db_;
156 std::unique_ptr<AutocompleteSyncBridge> bridge_;
157 AutofillEntry entry_;
skym 2016/12/20 17:23:44 Still, why do you hang onto this AutofillEntry obj
Gang Wu 2016/12/20 21:55:50 Done.
158
159 private:
160 DISALLOW_COPY_AND_ASSIGN(AutocompleteSyncBridgeTest);
161 };
162
163 TEST_F(AutocompleteSyncBridgeTest, GetData) {
164 const AutofillSpecifics specifics1 = CreateSpecifics(1);
165 const AutofillSpecifics specifics2 = CreateSpecifics(2);
166 const AutofillSpecifics specifics3 = CreateSpecifics(3);
167 SaveSpecificsToTable({specifics1, specifics2, specifics3});
168
169 const std::map<std::string, AutofillSpecifics> expected{
170 {GetStorageKey(specifics1), specifics1},
171 {GetStorageKey(specifics3), specifics3}};
172 bridge()->GetData({GetStorageKey(specifics1), GetStorageKey(specifics3)},
173 base::Bind(&VerifyDataBatch, expected));
174 }
175
176 TEST_F(AutocompleteSyncBridgeTest, GetDataNotExist) {
177 const AutofillSpecifics specifics1 = CreateSpecifics(1);
178 const AutofillSpecifics specifics2 = CreateSpecifics(2);
179 const AutofillSpecifics specifics3 = CreateSpecifics(3);
180 SaveSpecificsToTable({specifics1, specifics2});
181
182 const std::map<std::string, AutofillSpecifics> expected{
183 {GetStorageKey(specifics1), specifics1},
184 {GetStorageKey(specifics2), specifics2}};
185 bridge()->GetData({GetStorageKey(specifics1), GetStorageKey(specifics2),
186 GetStorageKey(specifics3)},
187 base::Bind(&VerifyDataBatch, expected));
188 }
189
190 TEST_F(AutocompleteSyncBridgeTest, GetAllData) {
191 const AutofillSpecifics specifics1 = CreateSpecifics(1);
192 const AutofillSpecifics specifics2 = CreateSpecifics(2);
193 const AutofillSpecifics specifics3 = CreateSpecifics(3);
194 SaveSpecificsToTable({specifics1, specifics2, specifics3});
195
196 const std::map<std::string, AutofillSpecifics> expected{
197 {GetStorageKey(specifics1), specifics1},
198 {GetStorageKey(specifics2), specifics2},
199 {GetStorageKey(specifics3), specifics3}};
200 bridge()->GetAllData(base::Bind(&VerifyDataBatch, expected));
201 }
202
203 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698