OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "sync/api/sync_data.h" | 5 #include "sync/api/sync_data.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" |
9 #include "base/time/time.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_proxy.h" |
| 16 #include "sync/api/attachments/fake_attachment_service.h" |
10 #include "sync/protocol/sync.pb.h" | 17 #include "sync/protocol/sync.pb.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
12 | 19 |
13 using std::string; | 20 using std::string; |
14 | 21 |
15 namespace syncer { | 22 namespace syncer { |
16 | 23 |
17 namespace { | 24 namespace { |
18 | 25 |
19 const string kSyncTag = "3984729834"; | 26 const string kSyncTag = "3984729834"; |
20 const ModelType kDatatype = syncer::PREFERENCES; | 27 const ModelType kDatatype = syncer::PREFERENCES; |
21 const string kNonUniqueTitle = "my preference"; | 28 const string kNonUniqueTitle = "my preference"; |
22 const int64 kId = 439829; | 29 const int64 kId = 439829; |
23 const base::Time kLastModifiedTime = base::Time(); | 30 const base::Time kLastModifiedTime = base::Time(); |
24 | 31 |
25 typedef testing::Test SyncDataTest; | 32 class SyncDataTest : public testing::Test { |
| 33 protected: |
| 34 SyncDataTest() |
| 35 : attachment_service(FakeAttachmentService::CreateForTest()), |
| 36 attachment_service_weak_ptr_factory(attachment_service.get()), |
| 37 attachment_service_proxy( |
| 38 base::MessageLoopProxy::current(), |
| 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 }; |
26 | 46 |
27 TEST_F(SyncDataTest, NoArgCtor) { | 47 TEST_F(SyncDataTest, NoArgCtor) { |
28 SyncData data; | 48 SyncData data; |
29 EXPECT_FALSE(data.IsValid()); | 49 EXPECT_FALSE(data.IsValid()); |
30 } | 50 } |
31 | 51 |
32 TEST_F(SyncDataTest, CreateLocalDelete) { | 52 TEST_F(SyncDataTest, CreateLocalDelete) { |
33 SyncData data = SyncData::CreateLocalDelete(kSyncTag, kDatatype); | 53 SyncData data = SyncData::CreateLocalDelete(kSyncTag, kDatatype); |
34 EXPECT_TRUE(data.IsValid()); | 54 EXPECT_TRUE(data.IsValid()); |
35 EXPECT_TRUE(data.IsLocal()); | 55 EXPECT_TRUE(data.IsLocal()); |
36 EXPECT_EQ(kSyncTag, data.GetTag()); | 56 EXPECT_EQ(kSyncTag, data.GetTag()); |
37 EXPECT_EQ(kDatatype, data.GetDataType()); | 57 EXPECT_EQ(kDatatype, data.GetDataType()); |
38 } | 58 } |
39 | 59 |
40 TEST_F(SyncDataTest, CreateLocalData) { | 60 TEST_F(SyncDataTest, CreateLocalData) { |
41 sync_pb::EntitySpecifics specifics; | |
42 specifics.mutable_preference(); | 61 specifics.mutable_preference(); |
43 SyncData data = | 62 SyncData data = |
44 SyncData::CreateLocalData(kSyncTag, kNonUniqueTitle, specifics); | 63 SyncData::CreateLocalData(kSyncTag, kNonUniqueTitle, specifics); |
45 EXPECT_TRUE(data.IsValid()); | 64 EXPECT_TRUE(data.IsValid()); |
46 EXPECT_TRUE(data.IsLocal()); | 65 EXPECT_TRUE(data.IsLocal()); |
47 EXPECT_EQ(kSyncTag, data.GetTag()); | 66 EXPECT_EQ(kSyncTag, data.GetTag()); |
48 EXPECT_EQ(kDatatype, data.GetDataType()); | 67 EXPECT_EQ(kDatatype, data.GetDataType()); |
49 EXPECT_EQ(kNonUniqueTitle, data.GetTitle()); | 68 EXPECT_EQ(kNonUniqueTitle, data.GetTitle()); |
50 EXPECT_TRUE(data.GetSpecifics().has_preference()); | 69 EXPECT_TRUE(data.GetSpecifics().has_preference()); |
51 } | 70 } |
52 | 71 |
| 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)); |
| 79 |
| 80 SyncData data = SyncData::CreateLocalDataWithAttachments( |
| 81 kSyncTag, kNonUniqueTitle, specifics, attachments); |
| 82 EXPECT_TRUE(data.IsValid()); |
| 83 EXPECT_TRUE(data.IsLocal()); |
| 84 EXPECT_EQ(kSyncTag, 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 } |
| 91 |
| 92 TEST_F(SyncDataTest, CreateLocalDataWithAttachments_EmptyListOfAttachments) { |
| 93 specifics.mutable_preference(); |
| 94 AttachmentList attachments; |
| 95 SyncData data = SyncData::CreateLocalDataWithAttachments( |
| 96 kSyncTag, kNonUniqueTitle, specifics, attachments); |
| 97 EXPECT_TRUE(data.IsValid()); |
| 98 EXPECT_TRUE(data.IsLocal()); |
| 99 EXPECT_EQ(kSyncTag, data.GetTag()); |
| 100 EXPECT_EQ(kDatatype, data.GetDataType()); |
| 101 EXPECT_EQ(kNonUniqueTitle, data.GetTitle()); |
| 102 EXPECT_TRUE(data.GetSpecifics().has_preference()); |
| 103 EXPECT_TRUE(data.GetAttachmentIds().empty()); |
| 104 } |
| 105 |
53 TEST_F(SyncDataTest, CreateRemoteData) { | 106 TEST_F(SyncDataTest, CreateRemoteData) { |
54 sync_pb::EntitySpecifics specifics; | 107 specifics.mutable_preference(); |
| 108 SyncData data = SyncData::CreateRemoteData(kId, |
| 109 specifics, |
| 110 kLastModifiedTime, |
| 111 AttachmentIdList(), |
| 112 attachment_service_proxy); |
| 113 EXPECT_TRUE(data.IsValid()); |
| 114 EXPECT_FALSE(data.IsLocal()); |
| 115 EXPECT_EQ(kId, data.GetRemoteId()); |
| 116 EXPECT_EQ(kLastModifiedTime, data.GetRemoteModifiedTime()); |
| 117 EXPECT_TRUE(data.GetSpecifics().has_preference()); |
| 118 EXPECT_TRUE(data.GetAttachmentIds().empty()); |
| 119 } |
| 120 |
| 121 TEST_F(SyncDataTest, CreateRemoteData_WithoutAttachmentService) { |
55 specifics.mutable_preference(); | 122 specifics.mutable_preference(); |
56 SyncData data = SyncData::CreateRemoteData(kId, specifics, kLastModifiedTime); | 123 SyncData data = SyncData::CreateRemoteData(kId, specifics, kLastModifiedTime); |
57 EXPECT_TRUE(data.IsValid()); | 124 EXPECT_TRUE(data.IsValid()); |
58 EXPECT_FALSE(data.IsLocal()); | 125 EXPECT_FALSE(data.IsLocal()); |
59 EXPECT_EQ(kId, data.GetRemoteId()); | 126 EXPECT_EQ(kId, data.GetRemoteId()); |
60 EXPECT_EQ(kLastModifiedTime, data.GetRemoteModifiedTime()); | 127 EXPECT_EQ(kLastModifiedTime, data.GetRemoteModifiedTime()); |
61 EXPECT_TRUE(data.GetSpecifics().has_preference()); | 128 EXPECT_TRUE(data.GetSpecifics().has_preference()); |
62 } | 129 } |
63 | 130 |
| 131 // TODO(maniscalco): Add test cases that verify GetAttachments and |
| 132 // DropAttachments calls are passed through to the underlying AttachmentService. |
| 133 |
64 } // namespace | 134 } // namespace |
65 | 135 |
66 } // namespace syncer | 136 } // namespace syncer |
OLD | NEW |