1 // Copyright 2014 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 "sync/api/sync_data.h"
6
7 #include <string>
8
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/time/time.h"
13 #include "sync/api/attachments/attachment_id.h"
14 #include "sync/internal_api/public/attachments/attachment_service.h"
15 #include "sync/internal_api/public/attachments/attachment_service_impl.h"
16 #include "sync/internal_api/public/attachments/attachment_service_proxy.h"
17 #include "sync/protocol/sync.pb.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using std::string;
21
22 namespace syncer {
23
24 namespace {
25
26 const string kSyncTag = "3984729834";
27 const ModelType kDatatype = syncer::PREFERENCES;
28 const string kNonUniqueTitle = "my preference";
29 const int64 kId = 439829;
30 const base::Time kLastModifiedTime = base::Time();
31
32 class SyncDataTest : public testing::Test {
33 protected:
SyncDataTest()34 SyncDataTest()
35 : attachment_service(AttachmentServiceImpl::CreateForTest()),
36 attachment_service_weak_ptr_factory(attachment_service.get()),
37 attachment_service_proxy(
38 base::ThreadTaskRunnerHandle::Get(),
39 attachment_service_weak_ptr_factory.GetWeakPtr()) {}
40 base::MessageLoop loop;
41 sync_pb::EntitySpecifics specifics;
42 scoped_ptr<AttachmentService> attachment_service;
43 base::WeakPtrFactory<AttachmentService> attachment_service_weak_ptr_factory;
44 AttachmentServiceProxy attachment_service_proxy;
45 };
46
TEST_F(SyncDataTest,NoArgCtor)47 TEST_F(SyncDataTest, NoArgCtor) {
48 SyncData data;
49 EXPECT_FALSE(data.IsValid());
50 }
51
TEST_F(SyncDataTest,CreateLocalDelete)52 TEST_F(SyncDataTest, CreateLocalDelete) {
53 SyncData data = SyncData::CreateLocalDelete(kSyncTag, kDatatype);
54 EXPECT_TRUE(data.IsValid());
55 EXPECT_TRUE(data.IsLocal());
56 EXPECT_EQ(kSyncTag, SyncDataLocal(data).GetTag());
57 EXPECT_EQ(kDatatype, data.GetDataType());
58 }
59
TEST_F(SyncDataTest,CreateLocalData)60 TEST_F(SyncDataTest, CreateLocalData) {
61 specifics.mutable_preference();
62 SyncData data =
63 SyncData::CreateLocalData(kSyncTag, kNonUniqueTitle, specifics);
64 EXPECT_TRUE(data.IsValid());
65 EXPECT_TRUE(data.IsLocal());
66 EXPECT_EQ(kSyncTag, SyncDataLocal(data).GetTag());
67 EXPECT_EQ(kDatatype, data.GetDataType());
68 EXPECT_EQ(kNonUniqueTitle, data.GetTitle());
69 EXPECT_TRUE(data.GetSpecifics().has_preference());
70 }
71
TEST_F(SyncDataTest,CreateLocalDataWithAttachments)72 TEST_F(SyncDataTest, CreateLocalDataWithAttachments) {
73 specifics.mutable_preference();
74 AttachmentIdList attachment_ids;
75 attachment_ids.push_back(AttachmentId::Create());
76 attachment_ids.push_back(AttachmentId::Create());
77 attachment_ids.push_back(AttachmentId::Create());
78
79 SyncData data = SyncData::CreateLocalDataWithAttachments(
80 kSyncTag, kNonUniqueTitle, specifics, attachment_ids);
81 EXPECT_TRUE(data.IsValid());
82 EXPECT_TRUE(data.IsLocal());
83 EXPECT_EQ(kSyncTag, SyncDataLocal(data).GetTag());
84 EXPECT_EQ(kDatatype, data.GetDataType());
85 EXPECT_EQ(kNonUniqueTitle, data.GetTitle());
86 EXPECT_TRUE(data.GetSpecifics().has_preference());
87 attachment_ids = data.GetAttachmentIds();
88 EXPECT_EQ(3U, attachment_ids.size());
89 }
90
TEST_F(SyncDataTest,CreateLocalDataWithAttachments_EmptyListOfAttachments)91 TEST_F(SyncDataTest, CreateLocalDataWithAttachments_EmptyListOfAttachments) {
92 specifics.mutable_preference();
93 AttachmentIdList attachment_ids;
94 SyncData data = SyncData::CreateLocalDataWithAttachments(
95 kSyncTag, kNonUniqueTitle, specifics, attachment_ids);
96 EXPECT_TRUE(data.IsValid());
97 EXPECT_TRUE(data.IsLocal());
98 EXPECT_EQ(kSyncTag, SyncDataLocal(data).GetTag());
99 EXPECT_EQ(kDatatype, data.GetDataType());
100 EXPECT_EQ(kNonUniqueTitle, data.GetTitle());
101 EXPECT_TRUE(data.GetSpecifics().has_preference());
102 EXPECT_TRUE(data.GetAttachmentIds().empty());
103 }
104
TEST_F(SyncDataTest,CreateRemoteData)105 TEST_F(SyncDataTest, CreateRemoteData) {
106 specifics.mutable_preference();
107 SyncData data = SyncData::CreateRemoteData(kId,
108 specifics,
109 kLastModifiedTime,
110 AttachmentIdList(),
111 attachment_service_proxy);
112 EXPECT_TRUE(data.IsValid());
113 EXPECT_FALSE(data.IsLocal());
114 EXPECT_EQ(kId, SyncDataRemote(data).GetId());
115 EXPECT_EQ(kLastModifiedTime, SyncDataRemote(data).GetModifiedTime());
116 EXPECT_TRUE(data.GetSpecifics().has_preference());
117 EXPECT_TRUE(data.GetAttachmentIds().empty());
118 }
119
120 // TODO(maniscalco): Add test cases that verify GetLocalAttachmentsForUpload and
121 // DropAttachments calls are passed through to the underlying AttachmentService.
122
123 } // namespace
124
125 } // namespace syncer
126