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