| 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 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 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 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 base::MessageLoop::current()->PostTask(FROM_HERE, | 81 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 82 base::Bind(callback, result)); | 82 base::Bind(callback, result)); |
| 83 } | 83 } |
| 84 | 84 |
| 85 std::vector<AttachmentIdList> read_ids; | 85 std::vector<AttachmentIdList> read_ids; |
| 86 std::vector<ReadCallback> read_callbacks; | 86 std::vector<ReadCallback> read_callbacks; |
| 87 std::vector<AttachmentList> write_attachments; | 87 std::vector<AttachmentList> write_attachments; |
| 88 std::vector<WriteCallback> write_callbacks; | 88 std::vector<WriteCallback> write_callbacks; |
| 89 | 89 |
| 90 private: | 90 private: |
| 91 virtual ~MockAttachmentStore() {} | 91 ~MockAttachmentStore() override {} |
| 92 | 92 |
| 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 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 12 matching lines...) Expand all Loading... |
| 126 DISALLOW_COPY_AND_ASSIGN(MockAttachmentDownloader); | 126 DISALLOW_COPY_AND_ASSIGN(MockAttachmentDownloader); |
| 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 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); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 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 void OnAttachmentUploaded(const AttachmentId& attachment_id) override { |
| 178 const AttachmentId& attachment_id) override { | |
| 179 on_attachment_uploaded_list_.push_back(attachment_id); | 178 on_attachment_uploaded_list_.push_back(attachment_id); |
| 180 } | 179 } |
| 181 | 180 |
| 182 void InitializeAttachmentService( | 181 void InitializeAttachmentService( |
| 183 scoped_ptr<MockAttachmentUploader> uploader, | 182 scoped_ptr<MockAttachmentUploader> uploader, |
| 184 scoped_ptr<MockAttachmentDownloader> downloader, | 183 scoped_ptr<MockAttachmentDownloader> downloader, |
| 185 AttachmentService::Delegate* delegate) { | 184 AttachmentService::Delegate* delegate) { |
| 186 scoped_refptr<MockAttachmentStore> attachment_store( | 185 scoped_refptr<MockAttachmentStore> attachment_store( |
| 187 new MockAttachmentStore()); | 186 new MockAttachmentStore()); |
| 188 attachment_store_ = attachment_store->AsWeakPtr(); | 187 attachment_store_ = attachment_store->AsWeakPtr(); |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 548 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
| 550 net::NetworkChangeNotifier::CONNECTION_WIFI); | 549 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 551 RunLoop(); | 550 RunLoop(); |
| 552 | 551 |
| 553 // No longer in backoff. | 552 // No longer in backoff. |
| 554 ASSERT_TRUE(mock_timer()->IsRunning()); | 553 ASSERT_TRUE(mock_timer()->IsRunning()); |
| 555 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); | 554 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); |
| 556 } | 555 } |
| 557 | 556 |
| 558 } // namespace syncer | 557 } // namespace syncer |
| OLD | NEW |