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 <memory> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "components/autofill/core/browser/webdata/autofill_table.h" |
| 18 #include "components/autofill/core/browser/webdata/autofill_webdata_backend.h" |
| 19 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 20 #include "components/sync/model/data_batch.h" |
| 21 #include "components/sync/model/fake_model_type_change_processor.h" |
| 22 #include "components/sync/model/metadata_batch.h" |
| 23 #include "components/webdata/common/web_database.h" |
| 24 #include "net/base/escape.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 |
| 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 std::unique_ptr<syncer::ModelTypeChangeProcessor> |
| 58 CreateModelTypeChangeProcessor(syncer::ModelType type, |
| 59 syncer::ModelTypeSyncBridge* bridge) { |
| 60 return base::MakeUnique<syncer::FakeModelTypeChangeProcessor>(); |
| 61 } |
| 62 |
| 63 class FakeAutofillBackend : public AutofillWebDataBackend { |
| 64 public: |
| 65 FakeAutofillBackend() {} |
| 66 ~FakeAutofillBackend() override {} |
| 67 WebDatabase* GetDatabase() override { return db_; } |
| 68 void AddObserver( |
| 69 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {} |
| 70 void RemoveObserver( |
| 71 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {} |
| 72 void RemoveExpiredFormElements() override {} |
| 73 void NotifyOfMultipleAutofillChanges() override {} |
| 74 void NotifyThatSyncHasStarted(syncer::ModelType model_type) override {} |
| 75 void SetWebDatabase(WebDatabase* db) { db_ = db; } |
| 76 |
| 77 private: |
| 78 WebDatabase* db_; |
| 79 }; |
| 80 |
| 81 } // namespace |
| 82 |
| 83 class AutocompleteSyncBridgeTest : public testing::Test { |
| 84 public: |
| 85 AutocompleteSyncBridgeTest() { |
| 86 if (temp_dir_.CreateUniqueTempDir()) { |
| 87 db_.AddTable(&table_); |
| 88 db_.Init(temp_dir_.GetPath().AppendASCII("SyncTestWebDatabase")); |
| 89 backend_.SetWebDatabase(&db_); |
| 90 |
| 91 bridge_.reset(new AutocompleteSyncBridge( |
| 92 &backend_, base::Bind(&CreateModelTypeChangeProcessor))); |
| 93 } |
| 94 } |
| 95 ~AutocompleteSyncBridgeTest() override {} |
| 96 |
| 97 protected: |
| 98 AutocompleteSyncBridge* bridge() { return bridge_.get(); } |
| 99 |
| 100 void SaveSpecificsToTable( |
| 101 const std::vector<AutofillSpecifics>& specifics_list) { |
| 102 std::vector<AutofillEntry> new_entries; |
| 103 for (const auto& specifics : specifics_list) { |
| 104 new_entries.push_back( |
| 105 AutocompleteSyncBridge::CreateAutofillEntry(specifics)); |
| 106 } |
| 107 table_.UpdateAutofillEntries(new_entries); |
| 108 } |
| 109 |
| 110 AutofillSpecifics CreateSpecifics(int suffix) { |
| 111 AutofillSpecifics specifics; |
| 112 specifics.set_name(base::StringPrintf(kNameFormat, suffix)); |
| 113 specifics.set_value(base::StringPrintf(kValueFormat, suffix)); |
| 114 specifics.add_usage_timestamp(0); |
| 115 return specifics; |
| 116 } |
| 117 |
| 118 std::string GetStorageKey(const AutofillSpecifics& specifics) { |
| 119 return net::EscapePath(specifics.name()) + "|" + |
| 120 net::EscapePath(specifics.value()); |
| 121 } |
| 122 |
| 123 private: |
| 124 base::ScopedTempDir temp_dir_; |
| 125 base::MessageLoop message_loop_; |
| 126 FakeAutofillBackend backend_; |
| 127 AutofillTable table_; |
| 128 WebDatabase db_; |
| 129 std::unique_ptr<AutocompleteSyncBridge> bridge_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(AutocompleteSyncBridgeTest); |
| 132 }; |
| 133 |
| 134 TEST_F(AutocompleteSyncBridgeTest, GetData) { |
| 135 const AutofillSpecifics specifics1 = CreateSpecifics(1); |
| 136 const AutofillSpecifics specifics2 = CreateSpecifics(2); |
| 137 const AutofillSpecifics specifics3 = CreateSpecifics(3); |
| 138 SaveSpecificsToTable({specifics1, specifics2, specifics3}); |
| 139 |
| 140 const std::map<std::string, AutofillSpecifics> expected{ |
| 141 {GetStorageKey(specifics1), specifics1}, |
| 142 {GetStorageKey(specifics3), specifics3}}; |
| 143 bridge()->GetData({GetStorageKey(specifics1), GetStorageKey(specifics3)}, |
| 144 base::Bind(&VerifyDataBatch, expected)); |
| 145 } |
| 146 |
| 147 TEST_F(AutocompleteSyncBridgeTest, GetDataNotExist) { |
| 148 const AutofillSpecifics specifics1 = CreateSpecifics(1); |
| 149 const AutofillSpecifics specifics2 = CreateSpecifics(2); |
| 150 const AutofillSpecifics specifics3 = CreateSpecifics(3); |
| 151 SaveSpecificsToTable({specifics1, specifics2}); |
| 152 |
| 153 const std::map<std::string, AutofillSpecifics> expected{ |
| 154 {GetStorageKey(specifics1), specifics1}, |
| 155 {GetStorageKey(specifics2), specifics2}}; |
| 156 bridge()->GetData({GetStorageKey(specifics1), GetStorageKey(specifics2), |
| 157 GetStorageKey(specifics3)}, |
| 158 base::Bind(&VerifyDataBatch, expected)); |
| 159 } |
| 160 |
| 161 TEST_F(AutocompleteSyncBridgeTest, GetAllData) { |
| 162 const AutofillSpecifics specifics1 = CreateSpecifics(1); |
| 163 const AutofillSpecifics specifics2 = CreateSpecifics(2); |
| 164 const AutofillSpecifics specifics3 = CreateSpecifics(3); |
| 165 SaveSpecificsToTable({specifics1, specifics2, specifics3}); |
| 166 |
| 167 const std::map<std::string, AutofillSpecifics> expected{ |
| 168 {GetStorageKey(specifics1), specifics1}, |
| 169 {GetStorageKey(specifics2), specifics2}, |
| 170 {GetStorageKey(specifics3), specifics3}}; |
| 171 bridge()->GetAllData(base::Bind(&VerifyDataBatch, expected)); |
| 172 } |
| 173 |
| 174 } // namespace autofill |
OLD | NEW |