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

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

Issue 2660673002: [Sync] Not having an autocomplete ModelTypeState is valid. (Closed)
Patch Set: Update AutocompleteSyncBridge unittests to not rely on initial metadata. Created 3 years, 10 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
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 <stddef.h> 5 #include "components/autofill/core/browser/webdata/autofill_table.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string>
10 #include <tuple> 9 #include <tuple>
11 #include <utility> 10 #include <utility>
12 #include <vector>
13 11
14 #include "base/command_line.h" 12 #include "base/command_line.h"
15 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
16 #include "base/files/scoped_temp_dir.h" 14 #include "base/files/scoped_temp_dir.h"
17 #include "base/guid.h" 15 #include "base/guid.h"
18 #include "base/macros.h" 16 #include "base/macros.h"
19 #include "base/memory/scoped_vector.h" 17 #include "base/memory/scoped_vector.h"
20 #include "base/stl_util.h" 18 #include "base/stl_util.h"
21 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
23 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
24 #include "base/time/time.h" 22 #include "base/time/time.h"
25 #include "build/build_config.h" 23 #include "build/build_config.h"
26 #include "components/autofill/core/browser/autofill_profile.h" 24 #include "components/autofill/core/browser/autofill_profile.h"
27 #include "components/autofill/core/browser/autofill_test_utils.h" 25 #include "components/autofill/core/browser/autofill_test_utils.h"
28 #include "components/autofill/core/browser/autofill_type.h" 26 #include "components/autofill/core/browser/autofill_type.h"
29 #include "components/autofill/core/browser/credit_card.h" 27 #include "components/autofill/core/browser/credit_card.h"
30 #include "components/autofill/core/browser/webdata/autofill_change.h" 28 #include "components/autofill/core/browser/webdata/autofill_change.h"
31 #include "components/autofill/core/browser/webdata/autofill_entry.h" 29 #include "components/autofill/core/browser/webdata/autofill_entry.h"
32 #include "components/autofill/core/browser/webdata/autofill_table.h"
33 #include "components/autofill/core/common/autofill_constants.h" 30 #include "components/autofill/core/common/autofill_constants.h"
34 #include "components/autofill/core/common/autofill_switches.h" 31 #include "components/autofill/core/common/autofill_switches.h"
35 #include "components/autofill/core/common/autofill_util.h" 32 #include "components/autofill/core/common/autofill_util.h"
36 #include "components/autofill/core/common/form_field_data.h" 33 #include "components/autofill/core/common/form_field_data.h"
37 #include "components/os_crypt/os_crypt_mocker.h" 34 #include "components/os_crypt/os_crypt_mocker.h"
38 #include "components/sync/protocol/entity_metadata.pb.h" 35 #include "components/sync/protocol/entity_metadata.pb.h"
39 #include "components/sync/protocol/model_type_state.pb.h" 36 #include "components/sync/protocol/model_type_state.pb.h"
40 #include "components/webdata/common/web_database.h" 37 #include "components/webdata/common/web_database.h"
41 #include "sql/statement.h" 38 #include "sql/statement.h"
42 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
43 40
44 using base::ASCIIToUTF16; 41 using base::ASCIIToUTF16;
45 using base::Time; 42 using base::Time;
46 using base::TimeDelta; 43 using base::TimeDelta;
44 using sync_pb::EntityMetadata;
45 using sync_pb::ModelTypeState;
46 using syncer::EntityMetadataMap;
47 using syncer::MetadataBatch;
47 48
48 namespace autofill { 49 namespace autofill {
49 50
50 // So we can compare AutofillKeys with EXPECT_EQ(). 51 // So we can compare AutofillKeys with EXPECT_EQ().
51 std::ostream& operator<<(std::ostream& os, const AutofillKey& key) { 52 std::ostream& operator<<(std::ostream& os, const AutofillKey& key) {
52 return os << base::UTF16ToASCII(key.name()) << ", " 53 return os << base::UTF16ToASCII(key.name()) << ", "
53 << base::UTF16ToASCII(key.value()); 54 << base::UTF16ToASCII(key.value());
54 } 55 }
55 56
56 // So we can compare AutofillChanges with EXPECT_EQ(). 57 // So we can compare AutofillChanges with EXPECT_EQ().
(...skipping 1957 matching lines...) Expand 10 before | Expand all | Expand 10 after
2014 EXPECT_EQ(test_case.expected_suggestion_count, v.size()); 2015 EXPECT_EQ(test_case.expected_suggestion_count, v.size());
2015 for (size_t j = 0; j < test_case.expected_suggestion_count; ++j) { 2016 for (size_t j = 0; j < test_case.expected_suggestion_count; ++j) {
2016 EXPECT_EQ(ASCIIToUTF16(test_case.expected_suggestion[j]), v[j]); 2017 EXPECT_EQ(ASCIIToUTF16(test_case.expected_suggestion[j]), v[j]);
2017 } 2018 }
2018 2019
2019 changes.clear(); 2020 changes.clear();
2020 table_->RemoveFormElementsAddedBetween(t1, Time(), &changes); 2021 table_->RemoveFormElementsAddedBetween(t1, Time(), &changes);
2021 } 2022 }
2022 } 2023 }
2023 2024
2024 TEST_F(AutofillTableTest, GetAllSyncMetadata) { 2025 TEST_F(AutofillTableTest, AutofillNoMetadata) {
2025 sync_pb::EntityMetadata metadata; 2026 MetadataBatch metadata_batch;
2027 EXPECT_TRUE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch));
2028 EXPECT_EQ(0u, metadata_batch.TakeAllMetadata().size());
2029 EXPECT_EQ(ModelTypeState().SerializeAsString(),
2030 metadata_batch.GetModelTypeState().SerializeAsString());
2031 }
2032
2033 TEST_F(AutofillTableTest, AutofillGetAllSyncMetadata) {
2034 EntityMetadata metadata;
2026 std::string storage_key = "storage_key"; 2035 std::string storage_key = "storage_key";
2027 std::string storage_key2 = "storage_key2"; 2036 std::string storage_key2 = "storage_key2";
2028 metadata.set_sequence_number(1); 2037 metadata.set_sequence_number(1);
2029 2038
2030 EXPECT_TRUE( 2039 EXPECT_TRUE(
2031 table_->UpdateSyncMetadata(syncer::AUTOFILL, storage_key, metadata)); 2040 table_->UpdateSyncMetadata(syncer::AUTOFILL, storage_key, metadata));
2032 2041
2033 sync_pb::ModelTypeState model_type_state; 2042 ModelTypeState model_type_state;
2034 model_type_state.set_initial_sync_done(true); 2043 model_type_state.set_initial_sync_done(true);
2035 2044
2036 EXPECT_TRUE(table_->UpdateModelTypeState(syncer::AUTOFILL, model_type_state)); 2045 EXPECT_TRUE(table_->UpdateModelTypeState(syncer::AUTOFILL, model_type_state));
2037 2046
2038 metadata.set_sequence_number(2); 2047 metadata.set_sequence_number(2);
2039 EXPECT_TRUE( 2048 EXPECT_TRUE(
2040 table_->UpdateSyncMetadata(syncer::AUTOFILL, storage_key2, metadata)); 2049 table_->UpdateSyncMetadata(syncer::AUTOFILL, storage_key2, metadata));
2041 2050
2042 syncer::MetadataBatch metadata_batch; 2051 MetadataBatch metadata_batch;
2043 EXPECT_TRUE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch)); 2052 EXPECT_TRUE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch));
2044 2053
2045 EXPECT_TRUE(metadata_batch.GetModelTypeState().initial_sync_done()); 2054 EXPECT_TRUE(metadata_batch.GetModelTypeState().initial_sync_done());
2046 2055
2047 syncer::EntityMetadataMap metadata_records = metadata_batch.TakeAllMetadata(); 2056 EntityMetadataMap metadata_records = metadata_batch.TakeAllMetadata();
2048 2057
2049 EXPECT_EQ(metadata_records.size(), 2u); 2058 EXPECT_EQ(metadata_records.size(), 2u);
2050 EXPECT_EQ(metadata_records[storage_key].sequence_number(), 1); 2059 EXPECT_EQ(metadata_records[storage_key].sequence_number(), 1);
2051 EXPECT_EQ(metadata_records[storage_key2].sequence_number(), 2); 2060 EXPECT_EQ(metadata_records[storage_key2].sequence_number(), 2);
2052 2061
2053 // Now check that a model type state update replaces the old value 2062 // Now check that a model type state update replaces the old value
2054 model_type_state.set_initial_sync_done(false); 2063 model_type_state.set_initial_sync_done(false);
2055 EXPECT_TRUE(table_->UpdateModelTypeState(syncer::AUTOFILL, model_type_state)); 2064 EXPECT_TRUE(table_->UpdateModelTypeState(syncer::AUTOFILL, model_type_state));
2056 2065
2057 EXPECT_TRUE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch)); 2066 EXPECT_TRUE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch));
2058 EXPECT_FALSE(metadata_batch.GetModelTypeState().initial_sync_done()); 2067 EXPECT_FALSE(metadata_batch.GetModelTypeState().initial_sync_done());
2059 } 2068 }
2060 2069
2061 TEST_F(AutofillTableTest, WriteThenDeleteSyncMetadata) { 2070 TEST_F(AutofillTableTest, AutofillWriteThenDeleteSyncMetadata) {
2062 sync_pb::EntityMetadata metadata; 2071 EntityMetadata metadata;
2063 syncer::MetadataBatch metadata_batch; 2072 MetadataBatch metadata_batch;
2064 std::string storage_key = "storage_key"; 2073 std::string storage_key = "storage_key";
2065 sync_pb::ModelTypeState model_type_state; 2074 ModelTypeState model_type_state;
2066 2075
2067 model_type_state.set_initial_sync_done(true); 2076 model_type_state.set_initial_sync_done(true);
2068 2077
2069 metadata.set_client_tag_hash("client_hash"); 2078 metadata.set_client_tag_hash("client_hash");
2070 2079
2071 // Write the data into the store. 2080 // Write the data into the store.
2072 EXPECT_TRUE( 2081 EXPECT_TRUE(
2073 table_->UpdateSyncMetadata(syncer::AUTOFILL, storage_key, metadata)); 2082 table_->UpdateSyncMetadata(syncer::AUTOFILL, storage_key, metadata));
2074 EXPECT_TRUE(table_->UpdateModelTypeState(syncer::AUTOFILL, model_type_state)); 2083 EXPECT_TRUE(table_->UpdateModelTypeState(syncer::AUTOFILL, model_type_state));
2075 // Delete the data we just wrote. 2084 // Delete the data we just wrote.
2076 EXPECT_TRUE(table_->ClearSyncMetadata(syncer::AUTOFILL, storage_key)); 2085 EXPECT_TRUE(table_->ClearSyncMetadata(syncer::AUTOFILL, storage_key));
2077 // It shouldn't be there any more. 2086 // It shouldn't be there any more.
2078 EXPECT_TRUE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch)); 2087 EXPECT_TRUE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch));
2079 2088
2080 syncer::EntityMetadataMap metadata_records = metadata_batch.TakeAllMetadata(); 2089 EntityMetadataMap metadata_records = metadata_batch.TakeAllMetadata();
2081 EXPECT_EQ(metadata_records.size(), 0u); 2090 EXPECT_EQ(metadata_records.size(), 0u);
2082 2091
2083 // Now delete the model type state. 2092 // Now delete the model type state.
2084 EXPECT_TRUE(table_->ClearModelTypeState(syncer::AUTOFILL)); 2093 EXPECT_TRUE(table_->ClearModelTypeState(syncer::AUTOFILL));
2094 EXPECT_TRUE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch));
2095 EXPECT_EQ(ModelTypeState().SerializeAsString(),
2096 metadata_batch.GetModelTypeState().SerializeAsString());
2097 }
2098
2099 TEST_F(AutofillTableTest, AutofillCorruptSyncMetadata) {
2100 MetadataBatch metadata_batch;
2101 sql::Statement s(db_->GetSQLConnection()->GetUniqueStatement(
2102 "INSERT OR REPLACE INTO autofill_sync_metadata "
2103 "(storage_key, value) VALUES(?, ?)"));
2104 s.BindString(0, "storage_key");
2105 s.BindString(1, "unparseable");
2106 EXPECT_TRUE(s.Run());
2107
2085 EXPECT_FALSE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch)); 2108 EXPECT_FALSE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch));
2086 } 2109 }
2087 2110
2088 TEST_F(AutofillTableTest, CorruptSyncMetadata) { 2111 TEST_F(AutofillTableTest, AutofillCorruptModelTypeState) {
2089 syncer::MetadataBatch metadata_batch; 2112 MetadataBatch metadata_batch;
2090 sync_pb::ModelTypeState state;
2091 std::string storage_key = "storage_key";
2092
2093 sql::Statement s(db_->GetSQLConnection()->GetUniqueStatement( 2113 sql::Statement s(db_->GetSQLConnection()->GetUniqueStatement(
2094 "INSERT OR REPLACE INTO autofill_sync_metadata "
2095 "(storage_key, value) VALUES(?, ?)"));
2096 s.BindString(0, storage_key);
2097 s.BindString(1, "unparseable");
2098
2099 sql::Statement s2(db_->GetSQLConnection()->GetUniqueStatement(
2100 "INSERT OR REPLACE INTO autofill_model_type_state " 2114 "INSERT OR REPLACE INTO autofill_model_type_state "
2101 "(rowid, value) VALUES(1, ?)")); 2115 "(rowid, value) VALUES(1, ?)"));
2102 s2.BindString(0, "unparseable"); 2116 s.BindString(0, "unparseable");
2103
2104 EXPECT_TRUE(s.Run()); 2117 EXPECT_TRUE(s.Run());
2105 EXPECT_TRUE(s2.Run());
2106 2118
2107 EXPECT_FALSE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch)); 2119 EXPECT_FALSE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch));
2108 } 2120 }
2109 2121
2110 } // namespace autofill 2122 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698