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