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/internal_api/public/attachments/attachment_service_impl.h" | 5 #include "sync/internal_api/public/attachments/attachment_service_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/timer/mock_timer.h" | 11 #include "base/timer/mock_timer.h" |
12 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" | 12 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" |
13 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" | 13 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" |
14 #include "testing/gmock/include/gmock/gmock-matchers.h" | 14 #include "testing/gmock/include/gmock/gmock-matchers.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace syncer { | 17 namespace syncer { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 class MockAttachmentStore : public AttachmentStore, | 21 class MockAttachmentStore : public AttachmentStore, |
22 public base::SupportsWeakPtr<MockAttachmentStore> { | 22 public base::SupportsWeakPtr<MockAttachmentStore> { |
23 public: | 23 public: |
24 MockAttachmentStore() {} | 24 MockAttachmentStore() {} |
25 | 25 |
26 virtual void Read(const AttachmentIdList& ids, | 26 virtual void Read(const AttachmentIdList& ids, |
27 const ReadCallback& callback) OVERRIDE { | 27 const ReadCallback& callback) override { |
28 read_ids.push_back(ids); | 28 read_ids.push_back(ids); |
29 read_callbacks.push_back(callback); | 29 read_callbacks.push_back(callback); |
30 } | 30 } |
31 | 31 |
32 virtual void Write(const AttachmentList& attachments, | 32 virtual void Write(const AttachmentList& attachments, |
33 const WriteCallback& callback) OVERRIDE { | 33 const WriteCallback& callback) override { |
34 write_attachments.push_back(attachments); | 34 write_attachments.push_back(attachments); |
35 write_callbacks.push_back(callback); | 35 write_callbacks.push_back(callback); |
36 } | 36 } |
37 | 37 |
38 virtual void Drop(const AttachmentIdList& ids, | 38 virtual void Drop(const AttachmentIdList& ids, |
39 const DropCallback& callback) OVERRIDE { | 39 const DropCallback& callback) override { |
40 NOTREACHED(); | 40 NOTREACHED(); |
41 } | 41 } |
42 | 42 |
43 // Respond to Read request. Attachments found in local_attachments should be | 43 // Respond to Read request. Attachments found in local_attachments should be |
44 // returned, everything else should be reported unavailable. | 44 // returned, everything else should be reported unavailable. |
45 void RespondToRead(const AttachmentIdSet& local_attachments) { | 45 void RespondToRead(const AttachmentIdSet& local_attachments) { |
46 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); | 46 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); |
47 ReadCallback callback = read_callbacks.back(); | 47 ReadCallback callback = read_callbacks.back(); |
48 AttachmentIdList ids = read_ids.back(); | 48 AttachmentIdList ids = read_ids.back(); |
49 read_callbacks.pop_back(); | 49 read_callbacks.pop_back(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 DISALLOW_COPY_AND_ASSIGN(MockAttachmentStore); | 93 DISALLOW_COPY_AND_ASSIGN(MockAttachmentStore); |
94 }; | 94 }; |
95 | 95 |
96 class MockAttachmentDownloader | 96 class MockAttachmentDownloader |
97 : public AttachmentDownloader, | 97 : public AttachmentDownloader, |
98 public base::SupportsWeakPtr<MockAttachmentDownloader> { | 98 public base::SupportsWeakPtr<MockAttachmentDownloader> { |
99 public: | 99 public: |
100 MockAttachmentDownloader() {} | 100 MockAttachmentDownloader() {} |
101 | 101 |
102 virtual void DownloadAttachment(const AttachmentId& id, | 102 virtual void DownloadAttachment(const AttachmentId& id, |
103 const DownloadCallback& callback) OVERRIDE { | 103 const DownloadCallback& callback) override { |
104 ASSERT_TRUE(download_requests.find(id) == download_requests.end()); | 104 ASSERT_TRUE(download_requests.find(id) == download_requests.end()); |
105 download_requests.insert(std::make_pair(id, callback)); | 105 download_requests.insert(std::make_pair(id, callback)); |
106 } | 106 } |
107 | 107 |
108 // Multiple requests to download will be active at the same time. | 108 // Multiple requests to download will be active at the same time. |
109 // RespondToDownload should respond to only one of them. | 109 // RespondToDownload should respond to only one of them. |
110 void RespondToDownload(const AttachmentId& id, const DownloadResult& result) { | 110 void RespondToDownload(const AttachmentId& id, const DownloadResult& result) { |
111 ASSERT_TRUE(download_requests.find(id) != download_requests.end()); | 111 ASSERT_TRUE(download_requests.find(id) != download_requests.end()); |
112 scoped_ptr<Attachment> attachment; | 112 scoped_ptr<Attachment> attachment; |
113 if (result == DOWNLOAD_SUCCESS) { | 113 if (result == DOWNLOAD_SUCCESS) { |
(...skipping 13 matching lines...) Expand all Loading... |
127 }; | 127 }; |
128 | 128 |
129 class MockAttachmentUploader | 129 class MockAttachmentUploader |
130 : public AttachmentUploader, | 130 : public AttachmentUploader, |
131 public base::SupportsWeakPtr<MockAttachmentUploader> { | 131 public base::SupportsWeakPtr<MockAttachmentUploader> { |
132 public: | 132 public: |
133 MockAttachmentUploader() {} | 133 MockAttachmentUploader() {} |
134 | 134 |
135 // AttachmentUploader implementation. | 135 // AttachmentUploader implementation. |
136 virtual void UploadAttachment(const Attachment& attachment, | 136 virtual void UploadAttachment(const Attachment& attachment, |
137 const UploadCallback& callback) OVERRIDE { | 137 const UploadCallback& callback) override { |
138 const AttachmentId id = attachment.GetId(); | 138 const AttachmentId id = attachment.GetId(); |
139 ASSERT_TRUE(upload_requests.find(id) == upload_requests.end()); | 139 ASSERT_TRUE(upload_requests.find(id) == upload_requests.end()); |
140 upload_requests.insert(std::make_pair(id, callback)); | 140 upload_requests.insert(std::make_pair(id, callback)); |
141 } | 141 } |
142 | 142 |
143 void RespondToUpload(const AttachmentId& id, const UploadResult& result) { | 143 void RespondToUpload(const AttachmentId& id, const UploadResult& result) { |
144 ASSERT_TRUE(upload_requests.find(id) != upload_requests.end()); | 144 ASSERT_TRUE(upload_requests.find(id) != upload_requests.end()); |
145 base::MessageLoop::current()->PostTask( | 145 base::MessageLoop::current()->PostTask( |
146 FROM_HERE, base::Bind(upload_requests[id], result, id)); | 146 FROM_HERE, base::Bind(upload_requests[id], result, id)); |
147 upload_requests.erase(id); | 147 upload_requests.erase(id); |
148 } | 148 } |
149 | 149 |
150 std::map<AttachmentId, UploadCallback> upload_requests; | 150 std::map<AttachmentId, UploadCallback> upload_requests; |
151 | 151 |
152 DISALLOW_COPY_AND_ASSIGN(MockAttachmentUploader); | 152 DISALLOW_COPY_AND_ASSIGN(MockAttachmentUploader); |
153 }; | 153 }; |
154 | 154 |
155 } // namespace | 155 } // namespace |
156 | 156 |
157 class AttachmentServiceImplTest : public testing::Test, | 157 class AttachmentServiceImplTest : public testing::Test, |
158 public AttachmentService::Delegate { | 158 public AttachmentService::Delegate { |
159 protected: | 159 protected: |
160 AttachmentServiceImplTest() {} | 160 AttachmentServiceImplTest() {} |
161 | 161 |
162 virtual void SetUp() OVERRIDE { | 162 virtual void SetUp() override { |
163 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); | 163 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); |
164 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), | 164 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), |
165 make_scoped_ptr(new MockAttachmentDownloader()), | 165 make_scoped_ptr(new MockAttachmentDownloader()), |
166 this); | 166 this); |
167 } | 167 } |
168 | 168 |
169 virtual void TearDown() OVERRIDE { | 169 virtual void TearDown() override { |
170 attachment_service_.reset(); | 170 attachment_service_.reset(); |
171 ASSERT_FALSE(attachment_store_); | 171 ASSERT_FALSE(attachment_store_); |
172 ASSERT_FALSE(attachment_uploader_); | 172 ASSERT_FALSE(attachment_uploader_); |
173 ASSERT_FALSE(attachment_downloader_); | 173 ASSERT_FALSE(attachment_downloader_); |
174 } | 174 } |
175 | 175 |
176 // AttachmentService::Delegate implementation. | 176 // AttachmentService::Delegate implementation. |
177 virtual void OnAttachmentUploaded( | 177 virtual void OnAttachmentUploaded( |
178 const AttachmentId& attachment_id) OVERRIDE { | 178 const AttachmentId& attachment_id) override { |
179 on_attachment_uploaded_list_.push_back(attachment_id); | 179 on_attachment_uploaded_list_.push_back(attachment_id); |
180 } | 180 } |
181 | 181 |
182 void InitializeAttachmentService( | 182 void InitializeAttachmentService( |
183 scoped_ptr<MockAttachmentUploader> uploader, | 183 scoped_ptr<MockAttachmentUploader> uploader, |
184 scoped_ptr<MockAttachmentDownloader> downloader, | 184 scoped_ptr<MockAttachmentDownloader> downloader, |
185 AttachmentService::Delegate* delegate) { | 185 AttachmentService::Delegate* delegate) { |
186 scoped_refptr<MockAttachmentStore> attachment_store( | 186 scoped_refptr<MockAttachmentStore> attachment_store( |
187 new MockAttachmentStore()); | 187 new MockAttachmentStore()); |
188 attachment_store_ = attachment_store->AsWeakPtr(); | 188 attachment_store_ = attachment_store->AsWeakPtr(); |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 549 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
550 net::NetworkChangeNotifier::CONNECTION_WIFI); | 550 net::NetworkChangeNotifier::CONNECTION_WIFI); |
551 RunLoop(); | 551 RunLoop(); |
552 | 552 |
553 // No longer in backoff. | 553 // No longer in backoff. |
554 ASSERT_TRUE(mock_timer()->IsRunning()); | 554 ASSERT_TRUE(mock_timer()->IsRunning()); |
555 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); | 555 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); |
556 } | 556 } |
557 | 557 |
558 } // namespace syncer | 558 } // namespace syncer |
OLD | NEW |