OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/sync/internal_api/change_record.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/string_number_conversions.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/sync/syncable/syncable.h" |
| 11 #include "chrome/browser/sync/protocol/extension_specifics.pb.h" |
| 12 #include "chrome/browser/sync/protocol/proto_value_conversions.h" |
| 13 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 14 #include "chrome/test/base/values_test_util.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace sync_api { |
| 19 namespace { |
| 20 |
| 21 using test::ExpectDictDictionaryValue; |
| 22 using test::ExpectDictStringValue; |
| 23 using testing::Invoke; |
| 24 using testing::StrictMock; |
| 25 |
| 26 class ChangeRecordTest : public testing::Test {}; |
| 27 |
| 28 void ExpectChangeRecordActionValue(ChangeRecord::Action expected_value, |
| 29 const base::DictionaryValue& value, |
| 30 const std::string& key) { |
| 31 std::string str_value; |
| 32 EXPECT_TRUE(value.GetString(key, &str_value)); |
| 33 switch (expected_value) { |
| 34 case ChangeRecord::ACTION_ADD: |
| 35 EXPECT_EQ("Add", str_value); |
| 36 break; |
| 37 case ChangeRecord::ACTION_UPDATE: |
| 38 EXPECT_EQ("Update", str_value); |
| 39 break; |
| 40 case ChangeRecord::ACTION_DELETE: |
| 41 EXPECT_EQ("Delete", str_value); |
| 42 break; |
| 43 default: |
| 44 NOTREACHED(); |
| 45 break; |
| 46 } |
| 47 } |
| 48 |
| 49 void CheckChangeRecordValue( |
| 50 const ChangeRecord& record, |
| 51 const base::DictionaryValue& value, |
| 52 const syncable::ImmutableEntryKernelMutationMap& mutations) { |
| 53 ExpectChangeRecordActionValue(record.action, value, "action"); |
| 54 ExpectDictStringValue(base::Int64ToString(record.id), value, "id"); |
| 55 syncable::EntryKernelMutationMap::const_iterator it = |
| 56 mutations.Get().find(record.id); |
| 57 if (it != mutations.Get().end()) { |
| 58 scoped_ptr<base::DictionaryValue> expected_mutation_value( |
| 59 syncable::EntryKernelMutationToValue(it->second)); |
| 60 ExpectDictDictionaryValue(*expected_mutation_value, |
| 61 value, "mutation"); |
| 62 } else { |
| 63 ExpectDictStringValue("<none>", value, "mutation"); |
| 64 } |
| 65 if (record.action == ChangeRecord::ACTION_DELETE) { |
| 66 scoped_ptr<base::DictionaryValue> expected_extra_value; |
| 67 if (record.extra.get()) { |
| 68 expected_extra_value.reset(record.extra->ToValue()); |
| 69 } |
| 70 base::Value* extra_value = NULL; |
| 71 EXPECT_EQ(record.extra.get() != NULL, |
| 72 value.Get("extra", &extra_value)); |
| 73 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get())); |
| 74 |
| 75 scoped_ptr<DictionaryValue> expected_specifics_value( |
| 76 browser_sync::EntitySpecificsToValue(record.specifics)); |
| 77 ExpectDictDictionaryValue(*expected_specifics_value, |
| 78 value, "specifics"); |
| 79 } |
| 80 } |
| 81 |
| 82 class MockExtraChangeRecordData |
| 83 : public ExtraPasswordChangeRecordData { |
| 84 public: |
| 85 MOCK_CONST_METHOD0(ToValue, DictionaryValue*()); |
| 86 }; |
| 87 |
| 88 TEST_F(ChangeRecordTest, ChangeRecordToValue) { |
| 89 sync_pb::EntitySpecifics old_specifics; |
| 90 old_specifics.MutableExtension(sync_pb::extension)->set_id("old"); |
| 91 sync_pb::EntitySpecifics new_specifics; |
| 92 old_specifics.MutableExtension(sync_pb::extension)->set_id("new"); |
| 93 |
| 94 const int64 kTestId = 5; |
| 95 syncable::ImmutableEntryKernelMutationMap mutations; |
| 96 { |
| 97 syncable::EntryKernelMutationMap mutation_map; |
| 98 syncable::EntryKernelMutation mutation; |
| 99 mutation.original.mutable_ref(syncable::SPECIFICS).CopyFrom(old_specifics); |
| 100 mutation.mutated.mutable_ref(syncable::SPECIFICS).CopyFrom(new_specifics); |
| 101 mutation_map[1] = mutation; |
| 102 mutations = syncable::ImmutableEntryKernelMutationMap(&mutation_map); |
| 103 } |
| 104 |
| 105 // Add |
| 106 { |
| 107 ChangeRecord record; |
| 108 record.action = ChangeRecord::ACTION_ADD; |
| 109 record.id = kTestId; |
| 110 record.specifics = old_specifics; |
| 111 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); |
| 112 scoped_ptr<DictionaryValue> value(record.ToValue(mutations)); |
| 113 CheckChangeRecordValue(record, *value, mutations); |
| 114 } |
| 115 |
| 116 // Update |
| 117 { |
| 118 ChangeRecord record; |
| 119 record.action = ChangeRecord::ACTION_UPDATE; |
| 120 record.id = kTestId; |
| 121 record.specifics = old_specifics; |
| 122 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); |
| 123 scoped_ptr<DictionaryValue> value(record.ToValue(mutations)); |
| 124 CheckChangeRecordValue(record, *value, mutations); |
| 125 } |
| 126 |
| 127 // Delete (no extra) |
| 128 { |
| 129 ChangeRecord record; |
| 130 record.action = ChangeRecord::ACTION_DELETE; |
| 131 record.id = kTestId; |
| 132 record.specifics = old_specifics; |
| 133 scoped_ptr<DictionaryValue> value(record.ToValue(mutations)); |
| 134 CheckChangeRecordValue(record, *value, mutations); |
| 135 } |
| 136 |
| 137 // Delete (with extra) |
| 138 { |
| 139 ChangeRecord record; |
| 140 record.action = ChangeRecord::ACTION_DELETE; |
| 141 record.id = kTestId; |
| 142 record.specifics = old_specifics; |
| 143 |
| 144 DictionaryValue extra_value; |
| 145 extra_value.SetString("foo", "bar"); |
| 146 scoped_ptr<StrictMock<MockExtraChangeRecordData> > extra( |
| 147 new StrictMock<MockExtraChangeRecordData>()); |
| 148 EXPECT_CALL(*extra, ToValue()).Times(2).WillRepeatedly( |
| 149 Invoke(&extra_value, &DictionaryValue::DeepCopy)); |
| 150 |
| 151 record.extra.reset(extra.release()); |
| 152 scoped_ptr<DictionaryValue> value(record.ToValue(mutations)); |
| 153 CheckChangeRecordValue(record, *value, mutations); |
| 154 } |
| 155 } |
| 156 |
| 157 } // namespace |
| 158 } // namespace sync_api |
OLD | NEW |