OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "sync/api/attachments/attachment_service_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/run_loop.h" | |
11 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" | |
12 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace syncer { | |
16 | |
17 class MockAttachmentStore : public AttachmentStore, | |
18 public base::SupportsWeakPtr<MockAttachmentStore> { | |
19 public: | |
20 MockAttachmentStore() {} | |
21 | |
22 virtual void Read(const AttachmentIdList& ids, | |
23 const ReadCallback& callback) OVERRIDE { | |
24 read_ids.push_back(ids); | |
25 read_callbacks.push_back(callback); | |
26 } | |
27 | |
28 virtual void Write(const AttachmentList& attachments, | |
29 const WriteCallback& callback) OVERRIDE { | |
30 NOTREACHED(); | |
31 } | |
32 | |
33 virtual void Drop(const AttachmentIdList& ids, | |
34 const DropCallback& callback) OVERRIDE { | |
35 NOTREACHED(); | |
36 } | |
37 | |
38 // Respond to Read request. Attachments found in loca_attachments should be | |
maniscalco
2014/05/29 22:35:54
s/loca_attachments/local_attachments/
pavely
2014/05/30 00:50:07
Done.
| |
39 // returned, everything else should be reported unavailable. | |
40 void RespondToRead(const AttachmentIdSet& local_attachments) { | |
41 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); | |
42 ReadCallback callback = read_callbacks.back(); | |
43 AttachmentIdList ids = read_ids.back(); | |
44 read_callbacks.pop_back(); | |
45 read_ids.pop_back(); | |
46 | |
47 scoped_ptr<AttachmentMap> attachments(new AttachmentMap()); | |
48 scoped_ptr<AttachmentIdList> unavailable_attachments( | |
49 new AttachmentIdList()); | |
50 for (AttachmentIdList::const_iterator iter = ids.begin(); iter != ids.end(); | |
51 ++iter) { | |
52 if (local_attachments.find(*iter) != local_attachments.end()) { | |
53 Attachment attachment = Attachment::CreateWithId(*iter, data); | |
54 attachments->insert(std::make_pair(*iter, attachment)); | |
55 } else { | |
56 unavailable_attachments->push_back(*iter); | |
57 } | |
58 } | |
59 callback.Run(SUCCESS, attachments.Pass(), unavailable_attachments.Pass()); | |
maniscalco
2014/05/29 22:35:54
Do you need to invoke the callback here or could y
maniscalco
2014/05/29 22:35:54
Should the result code depend on unavailable_attac
pavely
2014/05/30 00:50:07
Done.
pavely
2014/05/30 00:50:07
I thought about it when I was writing code. Decide
| |
60 } | |
61 | |
62 std::vector<AttachmentIdList> read_ids; | |
63 std::vector<ReadCallback> read_callbacks; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(MockAttachmentStore); | |
66 }; | |
67 | |
68 class MockAttachmentDownloader | |
69 : public AttachmentDownloader, | |
70 public base::SupportsWeakPtr<MockAttachmentDownloader> { | |
71 public: | |
72 MockAttachmentDownloader() {} | |
73 | |
74 virtual void DownloadAttachment(const AttachmentId& id, | |
75 const DownloadCallback& callback) OVERRIDE { | |
76 ASSERT_TRUE(download_requests.find(id) == download_requests.end()); | |
77 download_requests.insert(std::make_pair(id, callback)); | |
78 } | |
79 | |
80 // Multiple requests to download will be active at the same time. | |
81 // RespondToDownload should respond to only one of them. | |
82 void RespondToDownload(const AttachmentId& id, bool success) { | |
maniscalco
2014/05/29 22:35:54
Instead of passing a bool, can you pass a Download
pavely
2014/05/30 00:50:07
Done.
| |
83 ASSERT_TRUE(download_requests.find(id) != download_requests.end()); | |
84 DownloadResult result = | |
85 success ? DOWNLOAD_SUCCESS : DOWNLOAD_UNSPECIFIED_ERROR; | |
86 scoped_ptr<Attachment> attachment; | |
87 if (success) { | |
88 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); | |
89 attachment.reset(new Attachment(Attachment::CreateWithId(id, data))); | |
90 } | |
91 download_requests[id].Run(result, attachment.Pass()); | |
maniscalco
2014/05/29 22:35:54
Similar to an earlier comment, can you use PostTas
pavely
2014/05/30 00:50:07
Done.
| |
92 download_requests.erase(id); | |
93 } | |
94 | |
95 std::map<AttachmentId, DownloadCallback> download_requests; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(MockAttachmentDownloader); | |
98 }; | |
99 | |
100 class AttachmentServiceImplTest : public testing::Test { | |
101 protected: | |
102 AttachmentServiceImplTest() {} | |
103 | |
104 virtual void SetUp() OVERRIDE { | |
105 scoped_ptr<MockAttachmentStore> attachment_store(new MockAttachmentStore()); | |
106 scoped_ptr<AttachmentUploader> attachment_uploader( | |
107 new FakeAttachmentUploader()); | |
108 scoped_ptr<MockAttachmentDownloader> attachment_downloader( | |
109 new MockAttachmentDownloader()); | |
110 | |
111 attachment_store_ = attachment_store->AsWeakPtr(); | |
112 attachment_downloader_ = attachment_downloader->AsWeakPtr(); | |
113 | |
114 attachment_service_.reset(new AttachmentServiceImpl( | |
115 attachment_store.PassAs<AttachmentStore>(), | |
116 attachment_uploader.Pass(), | |
117 attachment_downloader.PassAs<AttachmentDownloader>(), | |
118 NULL)); | |
119 } | |
120 | |
121 virtual void TearDown() OVERRIDE { | |
122 attachment_service_.reset(); | |
123 ASSERT_FALSE(attachment_store_); | |
124 ASSERT_FALSE(attachment_downloader_); | |
125 } | |
126 | |
127 AttachmentService* attachment_service() { return attachment_service_.get(); } | |
128 | |
129 AttachmentService::GetOrDownloadCallback download_callback() { | |
130 return base::Bind(&AttachmentServiceImplTest::DownloadDone, | |
131 base::Unretained(this)); | |
132 } | |
133 | |
134 void DownloadDone(const AttachmentService::GetOrDownloadResult& result, | |
135 scoped_ptr<AttachmentMap> attachments) { | |
136 download_results_.push_back(result); | |
137 last_download_attachments_ = attachments.Pass(); | |
138 } | |
139 | |
140 void RunLoop() { | |
141 base::RunLoop run_loop; | |
142 run_loop.RunUntilIdle(); | |
143 } | |
144 | |
145 const std::vector<AttachmentService::GetOrDownloadResult>& | |
146 download_results() { | |
147 return download_results_; | |
148 } | |
149 | |
150 AttachmentMap* last_download_attachments() { | |
151 return last_download_attachments_.get(); | |
152 } | |
153 | |
154 MockAttachmentStore* store() { return attachment_store_.get(); } | |
155 | |
156 MockAttachmentDownloader* downloader() { | |
157 return attachment_downloader_.get(); | |
158 } | |
159 | |
160 private: | |
161 base::MessageLoop message_loop_; | |
162 base::WeakPtr<MockAttachmentStore> attachment_store_; | |
163 base::WeakPtr<MockAttachmentDownloader> attachment_downloader_; | |
164 scoped_ptr<AttachmentService> attachment_service_; | |
165 | |
166 std::vector<AttachmentService::GetOrDownloadResult> download_results_; | |
167 scoped_ptr<AttachmentMap> last_download_attachments_; | |
168 }; | |
169 | |
maniscalco
2014/05/29 22:35:54
Consider adding a test for GetOrDownload with an e
pavely
2014/05/30 00:50:07
Done.
| |
170 TEST_F(AttachmentServiceImplTest, GetOrDownload_Local) { | |
171 AttachmentIdList attachment_ids; | |
172 attachment_ids.push_back(AttachmentId::Create()); | |
173 attachment_service()->GetOrDownloadAttachments(attachment_ids, | |
174 download_callback()); | |
175 AttachmentIdSet local_attachments; | |
176 local_attachments.insert(attachment_ids[0]); | |
177 store()->RespondToRead(local_attachments); | |
178 | |
179 RunLoop(); | |
180 EXPECT_EQ(1U, download_results().size()); | |
181 EXPECT_EQ(1u, last_download_attachments()->size()); | |
maniscalco
2014/05/29 22:35:54
s/1u/1U/
pavely
2014/05/30 00:50:07
Done.
| |
182 EXPECT_TRUE(last_download_attachments()->find(attachment_ids[0]) != | |
183 last_download_attachments()->end()); | |
184 } | |
185 | |
maniscalco
2014/05/29 22:35:54
What happens if GetOrDownload is called with the s
pavely
2014/05/30 00:50:07
It will silently work.
I think it is better to mo
| |
186 TEST_F(AttachmentServiceImplTest, GetOrDownload_LocalRemoteUnavailable) { | |
187 // Create attachment list with 3 ids. | |
188 AttachmentIdList attachment_ids; | |
189 attachment_ids.push_back(AttachmentId::Create()); | |
190 attachment_ids.push_back(AttachmentId::Create()); | |
191 attachment_ids.push_back(AttachmentId::Create()); | |
192 // Call attachment service. | |
193 attachment_service()->GetOrDownloadAttachments(attachment_ids, | |
194 download_callback()); | |
195 // Ensure AttachmentStore is called. | |
196 EXPECT_FALSE(store()->read_ids.empty()); | |
197 | |
198 // make AttachmentStore return only attachment 0. | |
199 AttachmentIdSet local_attachments; | |
200 local_attachments.insert(attachment_ids[0]); | |
201 store()->RespondToRead(local_attachments); | |
202 // Ensure Downloader called with right attachment ids | |
203 EXPECT_EQ(2u, downloader()->download_requests.size()); | |
maniscalco
2014/05/29 22:35:54
s/2u/2U/
pavely
2014/05/30 00:50:07
Done.
| |
204 | |
205 // Make downloader return attachment 1. | |
206 downloader()->RespondToDownload(attachment_ids[1], true); | |
207 RunLoop(); | |
208 // Ensure consumer callback is not called. | |
209 EXPECT_TRUE(download_results().empty()); | |
210 | |
211 // Make downloader fail attachment 2. | |
212 downloader()->RespondToDownload(attachment_ids[2], false); | |
213 RunLoop(); | |
214 // Ensure callback is called | |
215 EXPECT_FALSE(download_results().empty()); | |
216 // There should be only two attachments returned, 0 and 1. | |
217 EXPECT_EQ(2u, last_download_attachments()->size()); | |
maniscalco
2014/05/29 22:35:54
s/2u/2U/
pavely
2014/05/30 00:50:07
Done.
| |
218 EXPECT_TRUE(last_download_attachments()->find(attachment_ids[0]) != | |
219 last_download_attachments()->end()); | |
220 EXPECT_TRUE(last_download_attachments()->find(attachment_ids[1]) != | |
221 last_download_attachments()->end()); | |
222 EXPECT_TRUE(last_download_attachments()->find(attachment_ids[2]) == | |
223 last_download_attachments()->end()); | |
224 } | |
225 | |
226 } // namespace syncer | |
OLD | NEW |