Roll src/third_party/WebKit 6d85854:7e30d51 (svn 202247:202248)
[chromium-blink-merge.git] / sync / api / sync_change_unittest.cc
blob919fe53233cdc593f32f4ec4b4dba1836a19c69f
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/api/sync_change.h"
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "sync/api/attachments/attachment_id.h"
14 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
15 #include "sync/protocol/preference_specifics.pb.h"
16 #include "sync/protocol/proto_value_conversions.h"
17 #include "sync/protocol/sync.pb.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace syncer {
22 // Ordered list of SyncChange's.
23 typedef std::vector<SyncChange> SyncChangeList;
25 namespace {
27 class SyncChangeTest : public testing::Test {
28 private:
29 base::MessageLoop message_loop;
32 TEST_F(SyncChangeTest, LocalDelete) {
33 SyncChange::SyncChangeType change_type = SyncChange::ACTION_DELETE;
34 std::string tag = "client_tag";
35 SyncChange e(FROM_HERE,
36 change_type,
37 SyncData::CreateLocalDelete(tag, PREFERENCES));
38 EXPECT_EQ(change_type, e.change_type());
39 EXPECT_EQ(tag, SyncDataLocal(e.sync_data()).GetTag());
40 EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
43 TEST_F(SyncChangeTest, LocalUpdate) {
44 SyncChange::SyncChangeType change_type = SyncChange::ACTION_UPDATE;
45 sync_pb::EntitySpecifics specifics;
46 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference();
47 pref_specifics->set_name("test");
48 std::string tag = "client_tag";
49 std::string title = "client_title";
50 SyncChange e(FROM_HERE,
51 change_type,
52 SyncData::CreateLocalData(tag, title, specifics));
53 EXPECT_EQ(change_type, e.change_type());
54 EXPECT_EQ(tag, SyncDataLocal(e.sync_data()).GetTag());
55 EXPECT_EQ(title, e.sync_data().GetTitle());
56 EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
57 scoped_ptr<base::DictionaryValue> ref_spec(EntitySpecificsToValue(specifics));
58 scoped_ptr<base::DictionaryValue> e_spec(EntitySpecificsToValue(
59 e.sync_data().GetSpecifics()));
60 EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
63 TEST_F(SyncChangeTest, LocalAdd) {
64 SyncChange::SyncChangeType change_type = SyncChange::ACTION_ADD;
65 sync_pb::EntitySpecifics specifics;
66 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference();
67 pref_specifics->set_name("test");
68 std::string tag = "client_tag";
69 std::string title = "client_title";
70 SyncChange e(FROM_HERE,
71 change_type,
72 SyncData::CreateLocalData(tag, title, specifics));
73 EXPECT_EQ(change_type, e.change_type());
74 EXPECT_EQ(tag, SyncDataLocal(e.sync_data()).GetTag());
75 EXPECT_EQ(title, e.sync_data().GetTitle());
76 EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
77 scoped_ptr<base::DictionaryValue> ref_spec(EntitySpecificsToValue(specifics));
78 scoped_ptr<base::DictionaryValue> e_spec(EntitySpecificsToValue(
79 e.sync_data().GetSpecifics()));
80 EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
83 TEST_F(SyncChangeTest, SyncerChanges) {
84 SyncChangeList change_list;
86 // Create an update.
87 sync_pb::EntitySpecifics update_specifics;
88 sync_pb::PreferenceSpecifics* pref_specifics =
89 update_specifics.mutable_preference();
90 pref_specifics->set_name("update");
91 change_list.push_back(
92 SyncChange(FROM_HERE,
93 SyncChange::ACTION_UPDATE,
94 SyncData::CreateRemoteData(
96 update_specifics,
97 base::Time(),
98 syncer::AttachmentIdList(),
99 syncer::AttachmentServiceProxyForTest::Create())));
101 // Create an add.
102 sync_pb::EntitySpecifics add_specifics;
103 pref_specifics = add_specifics.mutable_preference();
104 pref_specifics->set_name("add");
105 change_list.push_back(
106 SyncChange(FROM_HERE,
107 SyncChange::ACTION_ADD,
108 SyncData::CreateRemoteData(
110 add_specifics,
111 base::Time(),
112 syncer::AttachmentIdList(),
113 syncer::AttachmentServiceProxyForTest::Create())));
115 // Create a delete.
116 sync_pb::EntitySpecifics delete_specifics;
117 pref_specifics = delete_specifics.mutable_preference();
118 pref_specifics->set_name("add");
119 change_list.push_back(
120 SyncChange(FROM_HERE,
121 SyncChange::ACTION_DELETE,
122 SyncData::CreateRemoteData(
124 delete_specifics,
125 base::Time(),
126 syncer::AttachmentIdList(),
127 syncer::AttachmentServiceProxyForTest::Create())));
129 ASSERT_EQ(3U, change_list.size());
131 // Verify update.
132 SyncChange e = change_list[0];
133 EXPECT_EQ(SyncChange::ACTION_UPDATE, e.change_type());
134 EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
135 scoped_ptr<base::DictionaryValue> ref_spec(EntitySpecificsToValue(
136 update_specifics));
137 scoped_ptr<base::DictionaryValue> e_spec(EntitySpecificsToValue(
138 e.sync_data().GetSpecifics()));
139 EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
141 // Verify add.
142 e = change_list[1];
143 EXPECT_EQ(SyncChange::ACTION_ADD, e.change_type());
144 EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
145 ref_spec = EntitySpecificsToValue(add_specifics);
146 e_spec = EntitySpecificsToValue(e.sync_data().GetSpecifics());
147 EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
149 // Verify delete.
150 e = change_list[2];
151 EXPECT_EQ(SyncChange::ACTION_DELETE, e.change_type());
152 EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
153 ref_spec = EntitySpecificsToValue(delete_specifics);
154 e_spec = EntitySpecificsToValue(e.sync_data().GetSpecifics());
155 EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
158 } // namespace
160 } // namespace syncer