OLD | NEW |
---|---|
(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/threading/thread_task_runner_handle.h" | |
13 #include "components/autofill/core/browser/webdata/autofill_table.h" | |
14 #include "components/autofill/core/browser/webdata/autofill_webdata_backend.h" | |
15 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | |
16 #include "components/sync/model/data_batch.h" | |
17 #include "components/sync/model/fake_model_type_change_processor.h" | |
18 #include "components/sync/model/metadata_batch.h" | |
19 #include "components/webdata/common/web_database.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 using sync_pb::AutofillSpecifics; | |
23 using syncer::SyncError; | |
24 | |
25 namespace autofill { | |
26 | |
27 namespace { | |
28 | |
29 const char kNameFormat[] = "name %d"; | |
30 const char kValueFormat[] = "value %d"; | |
31 | |
32 void VerifyEqual(const AutofillSpecifics& s1, const AutofillSpecifics& s2) { | |
33 EXPECT_EQ(s1.SerializeAsString(), s2.SerializeAsString()); | |
34 } | |
35 | |
36 void VerifyDataBatch(std::map<std::string, AutofillSpecifics> expected, | |
37 SyncError error, | |
38 std::unique_ptr<syncer::DataBatch> batch) { | |
39 EXPECT_FALSE(error.IsSet()); | |
40 while (batch->HasNext()) { | |
41 const syncer::KeyAndData& pair = batch->Next(); | |
42 auto iter = expected.find(pair.first); | |
43 ASSERT_NE(iter, expected.end()); | |
44 VerifyEqual(iter->second, pair.second->specifics.autofill()); | |
45 // Removing allows us to verify we don't see the same item multiple times, | |
46 // and that we saw everything we expected. | |
47 expected.erase(iter); | |
48 } | |
49 EXPECT_TRUE(expected.empty()); | |
50 } | |
51 | |
52 std::unique_ptr<syncer::ModelTypeChangeProcessor> | |
53 CreateModelTypeChangeProcessor(syncer::ModelType type, | |
54 syncer::ModelTypeSyncBridge* bridge) { | |
55 return base::MakeUnique<syncer::FakeModelTypeChangeProcessor>(); | |
56 } | |
57 | |
58 class FakeAutofillBackend : public AutofillWebDataBackend { | |
59 public: | |
60 FakeAutofillBackend() {} | |
61 ~FakeAutofillBackend() override {} | |
62 WebDatabase* GetDatabase() override { return db_; } | |
63 void AddObserver( | |
64 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {} | |
65 void RemoveObserver( | |
66 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {} | |
67 void RemoveExpiredFormElements() override {} | |
68 void NotifyOfMultipleAutofillChanges() override {} | |
69 void NotifyThatSyncHasStarted(syncer::ModelType model_type) override {} | |
70 void SetWebDatabase(WebDatabase* db) { db_ = db; } | |
71 | |
72 private: | |
73 WebDatabase* db_; | |
74 }; | |
75 | |
76 } // namespace | |
77 | |
78 class AutocompleteSyncBridgeTest : public testing::Test { | |
79 public: | |
80 AutocompleteSyncBridgeTest() {} | |
81 ~AutocompleteSyncBridgeTest() override {} | |
82 | |
83 protected: | |
84 AutocompleteSyncBridge* bridge() { return bridge_.get(); } | |
85 | |
86 void SaveSpecificsToTable( | |
87 const std::vector<AutofillSpecifics>& specifics_list) { | |
88 std::vector<AutofillEntry> new_entries; | |
89 for (const auto& specifics : specifics_list) { | |
90 new_entries.push_back( | |
91 AutocompleteSyncBridge::CreateAutofillEntry(specifics)); | |
92 } | |
93 table_.UpdateAutofillEntries(new_entries); | |
94 } | |
95 | |
96 AutofillSpecifics CreateSpecifics(int suffix) { | |
97 AutofillSpecifics specifics; | |
98 specifics.set_name(base::StringPrintf(kNameFormat, suffix)); | |
99 specifics.set_value(base::StringPrintf(kValueFormat, suffix)); | |
100 specifics.add_usage_timestamp(0); | |
101 return specifics; | |
102 } | |
103 | |
104 std::string GetStorageKey(const AutofillSpecifics& specifics) { | |
105 return AutocompleteSyncBridge::FormatStorageKey(specifics.name(), | |
vabr (Chromium)
2016/12/21 08:48:07
Could you use some of the public-facing methods ba
Gang Wu
2016/12/22 01:33:54
Done.
| |
106 specifics.value()); | |
107 } | |
108 | |
109 void SetUp() override { | |
110 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
vabr (Chromium)
2016/12/21 08:48:07
Could the work of SetUp be done in the constructor
Gang Wu
2016/12/22 01:33:53
Done.
| |
111 file_ = temp_dir_.GetPath().AppendASCII("SyncTestWebDatabase"); | |
112 | |
113 db_.AddTable(&table_); | |
114 ASSERT_EQ(sql::INIT_OK, db_.Init(file_)); | |
115 backend_.SetWebDatabase(&db_); | |
116 | |
117 bridge_.reset(new AutocompleteSyncBridge( | |
118 &backend_, base::Bind(&CreateModelTypeChangeProcessor))); | |
119 } | |
120 | |
121 private: | |
122 base::FilePath file_; | |
123 base::MessageLoop message_loop_; | |
124 base::ScopedTempDir temp_dir_; | |
125 FakeAutofillBackend backend_; | |
126 AutofillTable table_; | |
127 WebDatabase db_; | |
128 std::unique_ptr<AutocompleteSyncBridge> bridge_; | |
vabr (Chromium)
2016/12/21 08:48:07
Please #include <memory> for this.
Gang Wu
2016/12/22 01:33:53
Done.
| |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(AutocompleteSyncBridgeTest); | |
vabr (Chromium)
2016/12/21 08:48:07
Please #include "base/macros.h" for this.
Gang Wu
2016/12/22 01:33:54
Done.
| |
131 }; | |
132 | |
133 TEST_F(AutocompleteSyncBridgeTest, GetData) { | |
134 const AutofillSpecifics specifics1 = CreateSpecifics(1); | |
135 const AutofillSpecifics specifics2 = CreateSpecifics(2); | |
136 const AutofillSpecifics specifics3 = CreateSpecifics(3); | |
137 SaveSpecificsToTable({specifics1, specifics2, specifics3}); | |
138 | |
139 const std::map<std::string, AutofillSpecifics> expected{ | |
140 {GetStorageKey(specifics1), specifics1}, | |
141 {GetStorageKey(specifics3), specifics3}}; | |
142 bridge()->GetData({GetStorageKey(specifics1), GetStorageKey(specifics3)}, | |
143 base::Bind(&VerifyDataBatch, expected)); | |
144 } | |
145 | |
146 TEST_F(AutocompleteSyncBridgeTest, GetDataNotExist) { | |
147 const AutofillSpecifics specifics1 = CreateSpecifics(1); | |
148 const AutofillSpecifics specifics2 = CreateSpecifics(2); | |
149 const AutofillSpecifics specifics3 = CreateSpecifics(3); | |
150 SaveSpecificsToTable({specifics1, specifics2}); | |
151 | |
152 const std::map<std::string, AutofillSpecifics> expected{ | |
153 {GetStorageKey(specifics1), specifics1}, | |
154 {GetStorageKey(specifics2), specifics2}}; | |
155 bridge()->GetData({GetStorageKey(specifics1), GetStorageKey(specifics2), | |
156 GetStorageKey(specifics3)}, | |
157 base::Bind(&VerifyDataBatch, expected)); | |
158 } | |
159 | |
160 TEST_F(AutocompleteSyncBridgeTest, GetAllData) { | |
161 const AutofillSpecifics specifics1 = CreateSpecifics(1); | |
162 const AutofillSpecifics specifics2 = CreateSpecifics(2); | |
163 const AutofillSpecifics specifics3 = CreateSpecifics(3); | |
164 SaveSpecificsToTable({specifics1, specifics2, specifics3}); | |
165 | |
166 const std::map<std::string, AutofillSpecifics> expected{ | |
167 {GetStorageKey(specifics1), specifics1}, | |
168 {GetStorageKey(specifics2), specifics2}, | |
169 {GetStorageKey(specifics3), specifics3}}; | |
170 bridge()->GetAllData(base::Bind(&VerifyDataBatch, expected)); | |
171 } | |
172 | |
173 } // namespace autofill | |
OLD | NEW |