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.
5 #include "sync/api/sync_data.h"
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/api/attachments/attachment_service.h"
15 #include "sync/api/attachments/attachment_service_impl.h"
16 #include "sync/api/attachments/attachment_service_proxy.h"
17 #include "sync/protocol/sync.pb.h"
18 #include "testing/gtest/include/gtest/gtest.h"
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();
32 class SyncDataTest
: public testing::Test
{
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
;
47 TEST_F(SyncDataTest
, NoArgCtor
) {
49 EXPECT_FALSE(data
.IsValid());
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());
60 TEST_F(SyncDataTest
, CreateLocalData
) {
61 specifics
.mutable_preference();
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());
72 TEST_F(SyncDataTest
, CreateLocalDataWithAttachments
) {
73 specifics
.mutable_preference();
74 scoped_refptr
<base::RefCountedMemory
> bytes(new base::RefCountedString
);
75 AttachmentList attachments
;
76 attachments
.push_back(Attachment::Create(bytes
));
77 attachments
.push_back(Attachment::Create(bytes
));
78 attachments
.push_back(Attachment::Create(bytes
));
80 SyncData data
= SyncData::CreateLocalDataWithAttachments(
81 kSyncTag
, kNonUniqueTitle
, specifics
, attachments
);
82 EXPECT_TRUE(data
.IsValid());
83 EXPECT_TRUE(data
.IsLocal());
84 EXPECT_EQ(kSyncTag
, SyncDataLocal(data
).GetTag());
85 EXPECT_EQ(kDatatype
, data
.GetDataType());
86 EXPECT_EQ(kNonUniqueTitle
, data
.GetTitle());
87 EXPECT_TRUE(data
.GetSpecifics().has_preference());
88 AttachmentIdList attachment_ids
= data
.GetAttachmentIds();
89 EXPECT_EQ(3U, attachment_ids
.size());
90 EXPECT_EQ(3U, SyncDataLocal(data
).GetLocalAttachmentsForUpload().size());
93 TEST_F(SyncDataTest
, CreateLocalDataWithAttachments_EmptyListOfAttachments
) {
94 specifics
.mutable_preference();
95 AttachmentList attachments
;
96 SyncData data
= SyncData::CreateLocalDataWithAttachments(
97 kSyncTag
, kNonUniqueTitle
, specifics
, attachments
);
98 EXPECT_TRUE(data
.IsValid());
99 EXPECT_TRUE(data
.IsLocal());
100 EXPECT_EQ(kSyncTag
, SyncDataLocal(data
).GetTag());
101 EXPECT_EQ(kDatatype
, data
.GetDataType());
102 EXPECT_EQ(kNonUniqueTitle
, data
.GetTitle());
103 EXPECT_TRUE(data
.GetSpecifics().has_preference());
104 EXPECT_TRUE(data
.GetAttachmentIds().empty());
105 EXPECT_TRUE(SyncDataLocal(data
).GetLocalAttachmentsForUpload().empty());
108 TEST_F(SyncDataTest
, CreateRemoteData
) {
109 specifics
.mutable_preference();
110 SyncData data
= SyncData::CreateRemoteData(kId
,
114 attachment_service_proxy
);
115 EXPECT_TRUE(data
.IsValid());
116 EXPECT_FALSE(data
.IsLocal());
117 EXPECT_EQ(kId
, SyncDataRemote(data
).GetId());
118 EXPECT_EQ(kLastModifiedTime
, SyncDataRemote(data
).GetModifiedTime());
119 EXPECT_TRUE(data
.GetSpecifics().has_preference());
120 EXPECT_TRUE(data
.GetAttachmentIds().empty());
123 // TODO(maniscalco): Add test cases that verify GetLocalAttachmentsForUpload and
124 // DropAttachments calls are passed through to the underlying AttachmentService.
128 } // namespace syncer