Roll src/third_party/WebKit 96fb88b:6bbd108 (svn 201045:201047)
[chromium-blink-merge.git] / sync / internal_api / public / change_record_unittest.cc
blobf12974ebcea258513cdd1a95b6070df5d50da4eb
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.
5 #include "sync/internal_api/public/change_record.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/test/values_test_util.h"
10 #include "base/values.h"
11 #include "sync/protocol/extension_specifics.pb.h"
12 #include "sync/protocol/proto_value_conversions.h"
13 #include "sync/protocol/sync.pb.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace syncer {
18 namespace {
20 using base::ExpectDictDictionaryValue;
21 using base::ExpectDictStringValue;
22 using testing::Invoke;
23 using testing::StrictMock;
25 class ChangeRecordTest : public testing::Test {};
27 void ExpectChangeRecordActionValue(ChangeRecord::Action expected_value,
28 const base::DictionaryValue& value,
29 const std::string& key) {
30 std::string str_value;
31 EXPECT_TRUE(value.GetString(key, &str_value));
32 switch (expected_value) {
33 case ChangeRecord::ACTION_ADD:
34 EXPECT_EQ("Add", str_value);
35 break;
36 case ChangeRecord::ACTION_UPDATE:
37 EXPECT_EQ("Update", str_value);
38 break;
39 case ChangeRecord::ACTION_DELETE:
40 EXPECT_EQ("Delete", str_value);
41 break;
42 default:
43 NOTREACHED();
44 break;
48 void CheckChangeRecordValue(
49 const ChangeRecord& record,
50 const base::DictionaryValue& value) {
51 ExpectChangeRecordActionValue(record.action, value, "action");
52 ExpectDictStringValue(base::Int64ToString(record.id), value, "id");
53 if (record.action == ChangeRecord::ACTION_DELETE) {
54 scoped_ptr<base::DictionaryValue> expected_extra_value;
55 if (record.extra.get()) {
56 expected_extra_value = record.extra->ToValue();
58 const base::Value* extra_value = NULL;
59 EXPECT_EQ(record.extra.get() != NULL,
60 value.Get("extra", &extra_value));
61 EXPECT_TRUE(base::Value::Equals(extra_value, expected_extra_value.get()));
63 scoped_ptr<base::DictionaryValue> expected_specifics_value(
64 EntitySpecificsToValue(record.specifics));
65 ExpectDictDictionaryValue(*expected_specifics_value,
66 value, "specifics");
70 class TestExtraChangeRecordData : public ExtraPasswordChangeRecordData {
71 public:
72 TestExtraChangeRecordData()
73 : to_value_calls_(0), expected_to_value_calls_(0) {}
75 ~TestExtraChangeRecordData() override {
76 EXPECT_EQ(expected_to_value_calls_, to_value_calls_);
79 scoped_ptr<base::DictionaryValue> ToValue() const override {
80 const_cast<TestExtraChangeRecordData*>(this)->to_value_calls_++;
81 return dict_->CreateDeepCopy();
84 void set_dictionary_value(scoped_ptr<base::DictionaryValue> dict) {
85 dict_ = dict.Pass();
88 void set_expected_to_value_calls(size_t expectation) {
89 expected_to_value_calls_ = expectation;
92 private:
93 scoped_ptr<base::DictionaryValue> dict_;
94 size_t to_value_calls_;
95 size_t expected_to_value_calls_;
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");
104 const int64 kTestId = 5;
106 // Add
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 scoped_ptr<base::DictionaryValue> value(record.ToValue());
114 CheckChangeRecordValue(record, *value);
117 // Update
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 scoped_ptr<base::DictionaryValue> value(record.ToValue());
125 CheckChangeRecordValue(record, *value);
128 // Delete (no extra)
130 ChangeRecord record;
131 record.action = ChangeRecord::ACTION_DELETE;
132 record.id = kTestId;
133 record.specifics = old_specifics;
134 scoped_ptr<base::DictionaryValue> value(record.ToValue());
135 CheckChangeRecordValue(record, *value);
138 // Delete (with extra)
140 ChangeRecord record;
141 record.action = ChangeRecord::ACTION_DELETE;
142 record.id = kTestId;
143 record.specifics = old_specifics;
145 scoped_ptr<base::DictionaryValue> extra_value(new base::DictionaryValue());
146 extra_value->SetString("foo", "bar");
147 scoped_ptr<TestExtraChangeRecordData> extra(
148 new TestExtraChangeRecordData());
149 extra->set_dictionary_value(extra_value.Pass());
150 extra->set_expected_to_value_calls(2U);
152 record.extra.reset(extra.release());
153 scoped_ptr<base::DictionaryValue> value(record.ToValue());
154 CheckChangeRecordValue(record, *value);
158 } // namespace
159 } // namespace syncer