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