| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "sync/internal_api/public/change_record.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <string> | |
| 12 #include <utility> | |
| 13 | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/test/values_test_util.h" | |
| 16 #include "base/values.h" | |
| 17 #include "sync/protocol/extension_specifics.pb.h" | |
| 18 #include "sync/protocol/proto_value_conversions.h" | |
| 19 #include "sync/protocol/sync.pb.h" | |
| 20 #include "testing/gmock/include/gmock/gmock.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 namespace syncer { | |
| 24 namespace { | |
| 25 | |
| 26 using base::ExpectDictDictionaryValue; | |
| 27 using base::ExpectDictStringValue; | |
| 28 using testing::Invoke; | |
| 29 using testing::StrictMock; | |
| 30 | |
| 31 class ChangeRecordTest : public testing::Test {}; | |
| 32 | |
| 33 void ExpectChangeRecordActionValue(ChangeRecord::Action expected_value, | |
| 34 const base::DictionaryValue& value, | |
| 35 const std::string& key) { | |
| 36 std::string str_value; | |
| 37 EXPECT_TRUE(value.GetString(key, &str_value)); | |
| 38 switch (expected_value) { | |
| 39 case ChangeRecord::ACTION_ADD: | |
| 40 EXPECT_EQ("Add", str_value); | |
| 41 break; | |
| 42 case ChangeRecord::ACTION_UPDATE: | |
| 43 EXPECT_EQ("Update", str_value); | |
| 44 break; | |
| 45 case ChangeRecord::ACTION_DELETE: | |
| 46 EXPECT_EQ("Delete", str_value); | |
| 47 break; | |
| 48 default: | |
| 49 NOTREACHED(); | |
| 50 break; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 void CheckChangeRecordValue( | |
| 55 const ChangeRecord& record, | |
| 56 const base::DictionaryValue& value) { | |
| 57 ExpectChangeRecordActionValue(record.action, value, "action"); | |
| 58 ExpectDictStringValue(base::Int64ToString(record.id), value, "id"); | |
| 59 if (record.action == ChangeRecord::ACTION_DELETE) { | |
| 60 std::unique_ptr<base::DictionaryValue> expected_extra_value; | |
| 61 if (record.extra.get()) { | |
| 62 expected_extra_value = record.extra->ToValue(); | |
| 63 } | |
| 64 const base::Value* extra_value = NULL; | |
| 65 EXPECT_EQ(record.extra.get() != NULL, | |
| 66 value.Get("extra", &extra_value)); | |
| 67 EXPECT_TRUE(base::Value::Equals(extra_value, expected_extra_value.get())); | |
| 68 | |
| 69 std::unique_ptr<base::DictionaryValue> expected_specifics_value( | |
| 70 EntitySpecificsToValue(record.specifics)); | |
| 71 ExpectDictDictionaryValue(*expected_specifics_value, | |
| 72 value, "specifics"); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 class TestExtraChangeRecordData : public ExtraPasswordChangeRecordData { | |
| 77 public: | |
| 78 TestExtraChangeRecordData() | |
| 79 : to_value_calls_(0), expected_to_value_calls_(0) {} | |
| 80 | |
| 81 ~TestExtraChangeRecordData() override { | |
| 82 EXPECT_EQ(expected_to_value_calls_, to_value_calls_); | |
| 83 } | |
| 84 | |
| 85 std::unique_ptr<base::DictionaryValue> ToValue() const override { | |
| 86 const_cast<TestExtraChangeRecordData*>(this)->to_value_calls_++; | |
| 87 return dict_->CreateDeepCopy(); | |
| 88 } | |
| 89 | |
| 90 void set_dictionary_value(std::unique_ptr<base::DictionaryValue> dict) { | |
| 91 dict_ = std::move(dict); | |
| 92 } | |
| 93 | |
| 94 void set_expected_to_value_calls(size_t expectation) { | |
| 95 expected_to_value_calls_ = expectation; | |
| 96 } | |
| 97 | |
| 98 private: | |
| 99 std::unique_ptr<base::DictionaryValue> dict_; | |
| 100 size_t to_value_calls_; | |
| 101 size_t expected_to_value_calls_; | |
| 102 }; | |
| 103 | |
| 104 TEST_F(ChangeRecordTest, ChangeRecordToValue) { | |
| 105 sync_pb::EntitySpecifics old_specifics; | |
| 106 old_specifics.mutable_extension()->set_id("old"); | |
| 107 sync_pb::EntitySpecifics new_specifics; | |
| 108 old_specifics.mutable_extension()->set_id("new"); | |
| 109 | |
| 110 const int64_t kTestId = 5; | |
| 111 | |
| 112 // Add | |
| 113 { | |
| 114 ChangeRecord record; | |
| 115 record.action = ChangeRecord::ACTION_ADD; | |
| 116 record.id = kTestId; | |
| 117 record.specifics = old_specifics; | |
| 118 record.extra.reset(new TestExtraChangeRecordData()); | |
| 119 std::unique_ptr<base::DictionaryValue> value(record.ToValue()); | |
| 120 CheckChangeRecordValue(record, *value); | |
| 121 } | |
| 122 | |
| 123 // Update | |
| 124 { | |
| 125 ChangeRecord record; | |
| 126 record.action = ChangeRecord::ACTION_UPDATE; | |
| 127 record.id = kTestId; | |
| 128 record.specifics = old_specifics; | |
| 129 record.extra.reset(new TestExtraChangeRecordData()); | |
| 130 std::unique_ptr<base::DictionaryValue> value(record.ToValue()); | |
| 131 CheckChangeRecordValue(record, *value); | |
| 132 } | |
| 133 | |
| 134 // Delete (no extra) | |
| 135 { | |
| 136 ChangeRecord record; | |
| 137 record.action = ChangeRecord::ACTION_DELETE; | |
| 138 record.id = kTestId; | |
| 139 record.specifics = old_specifics; | |
| 140 std::unique_ptr<base::DictionaryValue> value(record.ToValue()); | |
| 141 CheckChangeRecordValue(record, *value); | |
| 142 } | |
| 143 | |
| 144 // Delete (with extra) | |
| 145 { | |
| 146 ChangeRecord record; | |
| 147 record.action = ChangeRecord::ACTION_DELETE; | |
| 148 record.id = kTestId; | |
| 149 record.specifics = old_specifics; | |
| 150 | |
| 151 std::unique_ptr<base::DictionaryValue> extra_value( | |
| 152 new base::DictionaryValue()); | |
| 153 extra_value->SetString("foo", "bar"); | |
| 154 std::unique_ptr<TestExtraChangeRecordData> extra( | |
| 155 new TestExtraChangeRecordData()); | |
| 156 extra->set_dictionary_value(std::move(extra_value)); | |
| 157 extra->set_expected_to_value_calls(2U); | |
| 158 | |
| 159 record.extra.reset(extra.release()); | |
| 160 std::unique_ptr<base::DictionaryValue> value(record.ToValue()); | |
| 161 CheckChangeRecordValue(record, *value); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 } // namespace | |
| 166 } // namespace syncer | |
| OLD | NEW |