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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "sync/engine/process_updates_command.h" | 6 #include "sync/engine/process_updates_command.h" |
7 #include "sync/internal_api/public/base/model_type.h" | 7 #include "sync/internal_api/public/base/model_type.h" |
| 8 #include "sync/internal_api/public/test/test_entry_factory.h" |
8 #include "sync/sessions/sync_session.h" | 9 #include "sync/sessions/sync_session.h" |
9 #include "sync/syncable/mutable_entry.h" | 10 #include "sync/syncable/mutable_entry.h" |
| 11 #include "sync/syncable/read_transaction.h" |
10 #include "sync/syncable/syncable_id.h" | 12 #include "sync/syncable/syncable_id.h" |
| 13 #include "sync/syncable/syncable_proto_util.h" |
| 14 #include "sync/syncable/write_transaction.h" |
11 #include "sync/test/engine/fake_model_worker.h" | 15 #include "sync/test/engine/fake_model_worker.h" |
12 #include "sync/test/engine/syncer_command_test.h" | 16 #include "sync/test/engine/syncer_command_test.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
14 | 18 |
15 namespace syncer { | 19 namespace syncer { |
16 | 20 |
| 21 using sync_pb::SyncEntity; |
17 using syncable::Id; | 22 using syncable::Id; |
18 using syncable::MutableEntry; | 23 using syncable::MutableEntry; |
19 using syncable::UNITTEST; | 24 using syncable::UNITTEST; |
20 using syncable::WriteTransaction; | 25 using syncable::WriteTransaction; |
21 | 26 |
22 namespace { | 27 namespace { |
23 | 28 |
24 class ProcessUpdatesCommandTest : public SyncerCommandTest { | 29 class ProcessUpdatesCommandTest : public SyncerCommandTest { |
25 protected: | 30 protected: |
26 ProcessUpdatesCommandTest() {} | 31 ProcessUpdatesCommandTest() {} |
27 virtual ~ProcessUpdatesCommandTest() {} | 32 virtual ~ProcessUpdatesCommandTest() {} |
28 | 33 |
29 virtual void SetUp() { | 34 virtual void SetUp() { |
30 workers()->push_back( | 35 workers()->push_back( |
31 make_scoped_refptr(new FakeModelWorker(GROUP_UI))); | 36 make_scoped_refptr(new FakeModelWorker(GROUP_UI))); |
32 workers()->push_back( | 37 workers()->push_back( |
33 make_scoped_refptr(new FakeModelWorker(GROUP_DB))); | 38 make_scoped_refptr(new FakeModelWorker(GROUP_DB))); |
34 (*mutable_routing_info())[PREFERENCES] = GROUP_UI; | 39 (*mutable_routing_info())[PREFERENCES] = GROUP_UI; |
35 (*mutable_routing_info())[BOOKMARKS] = GROUP_UI; | 40 (*mutable_routing_info())[BOOKMARKS] = GROUP_UI; |
36 (*mutable_routing_info())[AUTOFILL] = GROUP_DB; | 41 (*mutable_routing_info())[AUTOFILL] = GROUP_DB; |
37 SyncerCommandTest::SetUp(); | 42 SyncerCommandTest::SetUp(); |
| 43 test_entry_factory_.reset(new TestEntryFactory(directory())); |
38 } | 44 } |
39 | 45 |
40 void CreateLocalItem(const std::string& item_id, | 46 void CreateLocalItem(const std::string& item_id, |
41 const std::string& parent_id, | 47 const std::string& parent_id, |
42 const ModelType& type) { | 48 const ModelType& type) { |
43 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); | 49 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
44 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | 50 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
45 Id::CreateFromServerId(item_id)); | 51 Id::CreateFromServerId(item_id)); |
46 ASSERT_TRUE(entry.good()); | 52 ASSERT_TRUE(entry.good()); |
47 | 53 |
48 entry.Put(syncable::BASE_VERSION, 1); | 54 entry.Put(syncable::BASE_VERSION, 1); |
49 entry.Put(syncable::SERVER_VERSION, 1); | 55 entry.Put(syncable::SERVER_VERSION, 1); |
50 entry.Put(syncable::NON_UNIQUE_NAME, item_id); | 56 entry.Put(syncable::NON_UNIQUE_NAME, item_id); |
51 entry.Put(syncable::PARENT_ID, Id::CreateFromServerId(parent_id)); | 57 entry.Put(syncable::PARENT_ID, Id::CreateFromServerId(parent_id)); |
52 sync_pb::EntitySpecifics default_specifics; | 58 sync_pb::EntitySpecifics default_specifics; |
53 AddDefaultFieldValue(type, &default_specifics); | 59 AddDefaultFieldValue(type, &default_specifics); |
54 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); | 60 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); |
55 } | 61 } |
56 | 62 |
57 void AddUpdate(sync_pb::GetUpdatesResponse* updates, | 63 SyncEntity* AddUpdate(sync_pb::GetUpdatesResponse* updates, |
58 const std::string& id, const std::string& parent, | 64 const std::string& id, const std::string& parent, |
59 const ModelType& type) { | 65 const ModelType& type) { |
60 sync_pb::SyncEntity* e = updates->add_entries(); | 66 sync_pb::SyncEntity* e = updates->add_entries(); |
61 e->set_id_string("b1"); | 67 e->set_id_string(id); |
62 e->set_parent_id_string(parent); | 68 e->set_parent_id_string(parent); |
63 e->set_non_unique_name("b1"); | 69 e->set_non_unique_name(id); |
64 e->set_name("b1"); | 70 e->set_name(id); |
| 71 e->set_version(1000); |
65 AddDefaultFieldValue(type, e->mutable_specifics()); | 72 AddDefaultFieldValue(type, e->mutable_specifics()); |
| 73 return e; |
66 } | 74 } |
67 | 75 |
68 ProcessUpdatesCommand command_; | 76 ProcessUpdatesCommand command_; |
| 77 scoped_ptr<TestEntryFactory> test_entry_factory_; |
69 | 78 |
70 private: | 79 private: |
71 DISALLOW_COPY_AND_ASSIGN(ProcessUpdatesCommandTest); | 80 DISALLOW_COPY_AND_ASSIGN(ProcessUpdatesCommandTest); |
72 }; | 81 }; |
73 | 82 |
74 TEST_F(ProcessUpdatesCommandTest, GroupsToChange) { | 83 TEST_F(ProcessUpdatesCommandTest, GroupsToChange) { |
75 std::string root = syncable::GetNullId().GetServerId(); | 84 std::string root = syncable::GetNullId().GetServerId(); |
76 | 85 |
77 CreateLocalItem("b1", root, BOOKMARKS); | |
78 CreateLocalItem("b2", root, BOOKMARKS); | |
79 CreateLocalItem("p1", root, PREFERENCES); | 86 CreateLocalItem("p1", root, PREFERENCES); |
80 CreateLocalItem("a1", root, AUTOFILL); | 87 CreateLocalItem("a1", root, AUTOFILL); |
81 | 88 |
82 ExpectNoGroupsToChange(command_); | 89 ExpectNoGroupsToChange(command_); |
83 | 90 |
84 sync_pb::GetUpdatesResponse* updates = | 91 sync_pb::GetUpdatesResponse* updates = |
85 session()->mutable_status_controller()-> | 92 session()->mutable_status_controller()-> |
86 mutable_updates_response()->mutable_get_updates(); | 93 mutable_updates_response()->mutable_get_updates(); |
87 AddUpdate(updates, "b1", root, BOOKMARKS); | |
88 AddUpdate(updates, "b2", root, BOOKMARKS); | |
89 AddUpdate(updates, "p1", root, PREFERENCES); | 94 AddUpdate(updates, "p1", root, PREFERENCES); |
90 AddUpdate(updates, "a1", root, AUTOFILL); | 95 AddUpdate(updates, "a1", root, AUTOFILL); |
91 | 96 |
92 ExpectGroupsToChange(command_, GROUP_UI, GROUP_DB); | 97 ExpectGroupsToChange(command_, GROUP_UI, GROUP_DB); |
93 | 98 |
94 command_.ExecuteImpl(session()); | 99 command_.ExecuteImpl(session()); |
95 } | 100 } |
96 | 101 |
| 102 static const char kCacheGuid[] = "tuiWdG8hV+8y4RT9N5Aikg=="; |
| 103 |
| 104 // Test that the bookmark tag is set on newly created items. |
| 105 TEST_F(ProcessUpdatesCommandTest, NewBookmarkTag) { |
| 106 std::string root = syncable::GetNullId().GetServerId(); |
| 107 sync_pb::GetUpdatesResponse* updates = |
| 108 session()->mutable_status_controller()-> |
| 109 mutable_updates_response()->mutable_get_updates(); |
| 110 Id server_id = Id::CreateFromServerId("b1"); |
| 111 SyncEntity* e = |
| 112 AddUpdate(updates, SyncableIdToProto(server_id), root, BOOKMARKS); |
| 113 |
| 114 e->set_originator_cache_guid( |
| 115 std::string(kCacheGuid, arraysize(kCacheGuid)-1)); |
| 116 Id client_id = Id::CreateFromClientString("-42"); |
| 117 e->set_originator_client_item_id(client_id.value()); |
| 118 e->set_position_in_parent(0); |
| 119 |
| 120 command_.ExecuteImpl(session()); |
| 121 |
| 122 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 123 syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id); |
| 124 ASSERT_TRUE(entry.good()); |
| 125 EXPECT_TRUE( |
| 126 UniquePosition::IsValidSuffix(entry.Get(syncable::UNIQUE_BOOKMARK_TAG))); |
| 127 } |
| 128 |
| 129 // Test that the bookmark tag is updated on existing items. |
| 130 TEST_F(ProcessUpdatesCommandTest, UpdeateExistingBookmarkTag) { |
| 131 int64 handle = test_entry_factory_->CreateSyncedBookmarkItem("b1", false); |
| 132 |
| 133 Id server_id; |
| 134 std::string original_suffix; |
| 135 { |
| 136 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 137 syncable::Entry entry(&trans, syncable::GET_BY_HANDLE, handle); |
| 138 ASSERT_TRUE(entry.good()); |
| 139 original_suffix = entry.Get(syncable::UNIQUE_BOOKMARK_TAG); |
| 140 EXPECT_TRUE(UniquePosition::IsValidSuffix( |
| 141 entry.Get(syncable::UNIQUE_BOOKMARK_TAG))); |
| 142 server_id = entry.Get(syncable::ID); |
| 143 } |
| 144 |
| 145 std::string root = syncable::GetNullId().GetServerId(); |
| 146 sync_pb::GetUpdatesResponse* updates = |
| 147 session()->mutable_status_controller()-> |
| 148 mutable_updates_response()->mutable_get_updates(); |
| 149 SyncEntity* e = |
| 150 AddUpdate(updates, SyncableIdToProto(server_id), root, BOOKMARKS); |
| 151 |
| 152 e->set_originator_cache_guid( |
| 153 std::string(kCacheGuid, arraysize(kCacheGuid)-1)); |
| 154 Id client_id = Id::CreateFromClientString("-42"); |
| 155 e->set_originator_client_item_id(client_id.value()); |
| 156 e->set_position_in_parent(0); |
| 157 |
| 158 command_.ExecuteImpl(session()); |
| 159 |
| 160 { |
| 161 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 162 syncable::Entry entry(&trans, syncable::GET_BY_HANDLE, handle); |
| 163 ASSERT_TRUE(entry.good()); |
| 164 std::string new_suffix = entry.Get(syncable::UNIQUE_BOOKMARK_TAG); |
| 165 EXPECT_TRUE(UniquePosition::IsValidSuffix(new_suffix)); |
| 166 EXPECT_NE(original_suffix, new_suffix); |
| 167 } |
| 168 } |
| 169 |
97 } // namespace | 170 } // namespace |
98 | 171 |
99 } // namespace syncer | 172 } // namespace syncer |
OLD | NEW |