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/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"
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 AttachmentIdList attachment_ids
;
75 attachment_ids
.push_back(AttachmentId::Create());
76 attachment_ids
.push_back(AttachmentId::Create());
77 attachment_ids
.push_back(AttachmentId::Create());
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());
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());
105 TEST_F(SyncDataTest
, CreateRemoteData
) {
106 specifics
.mutable_preference();
107 SyncData data
= SyncData::CreateRemoteData(kId
,
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());
120 // TODO(maniscalco): Add test cases that verify GetLocalAttachmentsForUpload and
121 // DropAttachments calls are passed through to the underlying AttachmentService.
125 } // namespace syncer