diff options
author | pavely@chromium.org <pavely@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-24 13:10:32 +0000 |
---|---|---|
committer | pavely@chromium.org <pavely@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-24 13:10:32 +0000 |
commit | effb2428965667514179fa5ca234959bfc95a19d (patch) | |
tree | 0299625b8adb6a730fcfc9fd72ac07b0fcc27a7d /sync | |
parent | 66613bc0c8f94a325bacad2c2f65e50ebcbf6637 (diff) | |
download | chromium_src-effb2428965667514179fa5ca234959bfc95a19d.zip chromium_src-effb2428965667514179fa5ca234959bfc95a19d.tar.gz chromium_src-effb2428965667514179fa5ca234959bfc95a19d.tar.bz2 |
AttachmentDownloader implementation.
This is rudimentary implemenatation of AttachmentDownloader along with fake for tests.
BUG=376073
R=maniscalco@chromium.org
Review URL: https://codereview.chromium.org/296153014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272733 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sync')
8 files changed, 254 insertions, 0 deletions
diff --git a/sync/internal_api/attachments/attachment_downloader_impl.cc b/sync/internal_api/attachments/attachment_downloader_impl.cc new file mode 100644 index 0000000..c89b376 --- /dev/null +++ b/sync/internal_api/attachments/attachment_downloader_impl.cc @@ -0,0 +1,32 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sync/internal_api/public/attachments/attachment_downloader_impl.h" + +#include "base/bind.h" +#include "base/message_loop/message_loop.h" + +namespace syncer { + +AttachmentDownloaderImpl::AttachmentDownloaderImpl() { +} + +AttachmentDownloaderImpl::~AttachmentDownloaderImpl() { + DCHECK(CalledOnValidThread()); +} + +void AttachmentDownloaderImpl::DownloadAttachment( + const AttachmentId& attachment_id, + const DownloadCallback& callback) { + DCHECK(CalledOnValidThread()); + // No real implementation yet. Fail every request with + // DOWNLOAD_UNSPECIFIED_ERROR. + scoped_ptr<Attachment> attachment; + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind( + callback, DOWNLOAD_UNSPECIFIED_ERROR, base::Passed(&attachment))); +} + +} // namespace syncer diff --git a/sync/internal_api/attachments/attachment_downloader_impl_unittest.cc b/sync/internal_api/attachments/attachment_downloader_impl_unittest.cc new file mode 100644 index 0000000..240aae17 --- /dev/null +++ b/sync/internal_api/attachments/attachment_downloader_impl_unittest.cc @@ -0,0 +1,61 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sync/internal_api/public/attachments/attachment_downloader_impl.h" + +#include "base/bind.h" +#include "base/message_loop/message_loop.h" +#include "base/run_loop.h" +#include "sync/api/attachments/attachment.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace syncer { + +class AttachmentDownloaderImplTest : public testing::Test { + protected: + AttachmentDownloaderImplTest() {} + + virtual void SetUp() OVERRIDE {} + + virtual void TearDown() OVERRIDE {} + + AttachmentDownloader* downloader() { + return &attachment_downloader_; + } + + AttachmentDownloader::DownloadCallback download_callback() { + return base::Bind(&AttachmentDownloaderImplTest::DownloadDone, + base::Unretained(this)); + } + + const std::vector<AttachmentDownloader::DownloadResult>& download_results() { + return download_results_; + } + + void RunMessageLoop() { + base::RunLoop run_loop; + run_loop.RunUntilIdle(); + } + + private: + void DownloadDone(const AttachmentDownloader::DownloadResult& result, + scoped_ptr<Attachment> attachment) { + download_results_.push_back(result); + } + + base::MessageLoop message_loop_; + AttachmentDownloaderImpl attachment_downloader_; + std::vector<AttachmentDownloader::DownloadResult> download_results_; +}; + +TEST_F(AttachmentDownloaderImplTest, DownloadAttachment_Fail) { + AttachmentId attachment_id = AttachmentId::Create(); + downloader()->DownloadAttachment(attachment_id, download_callback()); + RunMessageLoop(); + EXPECT_EQ(1U, download_results().size()); + EXPECT_EQ(AttachmentDownloader::DOWNLOAD_UNSPECIFIED_ERROR, + download_results()[0]); +} + +} // namespace syncer diff --git a/sync/internal_api/attachments/fake_attachment_downloader.cc b/sync/internal_api/attachments/fake_attachment_downloader.cc new file mode 100644 index 0000000..1f2795e --- /dev/null +++ b/sync/internal_api/attachments/fake_attachment_downloader.cc @@ -0,0 +1,34 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sync/internal_api/public/attachments/fake_attachment_downloader.h" + +#include "base/bind.h" +#include "base/message_loop/message_loop.h" + +namespace syncer { + +FakeAttachmentDownloader::FakeAttachmentDownloader() { +} + +FakeAttachmentDownloader::~FakeAttachmentDownloader() { + DCHECK(CalledOnValidThread()); +} + +void FakeAttachmentDownloader::DownloadAttachment( + const AttachmentId& attachment_id, + const DownloadCallback& callback) { + DCHECK(CalledOnValidThread()); + // This is happy fake downloader, it always successfully downloads empty + // attachment. + scoped_refptr<base::RefCountedMemory> data(new base::RefCountedBytes()); + scoped_ptr<Attachment> attachment; + attachment.reset( + new Attachment(Attachment::CreateWithId(attachment_id, data))); + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(callback, DOWNLOAD_SUCCESS, base::Passed(&attachment))); +} + +} // namespace syncer diff --git a/sync/internal_api/attachments/fake_attachment_downloader_unittest.cc b/sync/internal_api/attachments/fake_attachment_downloader_unittest.cc new file mode 100644 index 0000000..af496f5 --- /dev/null +++ b/sync/internal_api/attachments/fake_attachment_downloader_unittest.cc @@ -0,0 +1,60 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sync/internal_api/public/attachments/fake_attachment_downloader.h" + +#include "base/bind.h" +#include "base/message_loop/message_loop.h" +#include "base/run_loop.h" +#include "sync/api/attachments/attachment.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace syncer { + +class FakeAttachmentDownloaderTest : public testing::Test { + protected: + FakeAttachmentDownloaderTest() {} + + virtual void SetUp() OVERRIDE {} + + virtual void TearDown() OVERRIDE {} + + AttachmentDownloader* downloader() { + return &attachment_downloader_; + } + + AttachmentDownloader::DownloadCallback download_callback() { + return base::Bind(&FakeAttachmentDownloaderTest::DownloadDone, + base::Unretained(this)); + } + + const std::vector<AttachmentDownloader::DownloadResult>& download_results() { + return download_results_; + } + + void RunMessageLoop() { + base::RunLoop run_loop; + run_loop.RunUntilIdle(); + } + + private: + void DownloadDone(const AttachmentDownloader::DownloadResult& result, + scoped_ptr<Attachment> attachment) { + download_results_.push_back(result); + } + + base::MessageLoop message_loop_; + FakeAttachmentDownloader attachment_downloader_; + std::vector<AttachmentDownloader::DownloadResult> download_results_; +}; + +TEST_F(FakeAttachmentDownloaderTest, DownloadAttachment) { + AttachmentId attachment_id = AttachmentId::Create(); + downloader()->DownloadAttachment(attachment_id, download_callback()); + RunMessageLoop(); + EXPECT_EQ(1U, download_results().size()); + EXPECT_EQ(AttachmentDownloader::DOWNLOAD_SUCCESS, download_results()[0]); +} + +} // namespace syncer diff --git a/sync/internal_api/public/attachments/attachment_downloader_impl.h b/sync/internal_api/public/attachments/attachment_downloader_impl.h new file mode 100644 index 0000000..757e66b0 --- /dev/null +++ b/sync/internal_api/public/attachments/attachment_downloader_impl.h @@ -0,0 +1,30 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ +#define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ + +#include "base/threading/non_thread_safe.h" +#include "sync/api/attachments/attachment_downloader.h" + +namespace syncer { + +// An implementation of AttachmentDownloader. +class SYNC_EXPORT AttachmentDownloaderImpl : public AttachmentDownloader, + public base::NonThreadSafe { + public: + AttachmentDownloaderImpl(); + virtual ~AttachmentDownloaderImpl(); + + // AttachmentDownloader implementation. + virtual void DownloadAttachment(const AttachmentId& attachment_id, + const DownloadCallback& callback) OVERRIDE; + + private: + DISALLOW_COPY_AND_ASSIGN(AttachmentDownloaderImpl); +}; + +} // namespace syncer + +#endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ diff --git a/sync/internal_api/public/attachments/fake_attachment_downloader.h b/sync/internal_api/public/attachments/fake_attachment_downloader.h new file mode 100644 index 0000000..9d1b0b3 --- /dev/null +++ b/sync/internal_api/public/attachments/fake_attachment_downloader.h @@ -0,0 +1,31 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_FAKE_ATTACHMENT_DOWNLOADER_H_ +#define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_FAKE_ATTACHMENT_DOWNLOADER_H_ + +#include "base/threading/non_thread_safe.h" +#include "sync/api/attachments/attachment_downloader.h" + +namespace syncer { + +// FakeAttachmentDownloader is for tests. For every request it posts a success +// callback with empty attachment. +class SYNC_EXPORT FakeAttachmentDownloader : public AttachmentDownloader, + public base::NonThreadSafe { + public: + FakeAttachmentDownloader(); + virtual ~FakeAttachmentDownloader(); + + // AttachmentDownloader implementation. + virtual void DownloadAttachment(const AttachmentId& attachment_id, + const DownloadCallback& callback) OVERRIDE; + + private: + DISALLOW_COPY_AND_ASSIGN(FakeAttachmentDownloader); +}; + +} // namespace syncer + +#endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_FAKE_ATTACHMENT_DOWNLOADER_H_ diff --git a/sync/sync_internal_api.gypi b/sync/sync_internal_api.gypi index 67ec2dc..f248552 100644 --- a/sync/sync_internal_api.gypi +++ b/sync/sync_internal_api.gypi @@ -16,7 +16,9 @@ '../url/url.gyp:url_lib', ], 'sources': [ + 'internal_api/attachments/attachment_downloader_impl.cc', 'internal_api/attachments/attachment_uploader_impl.cc', + 'internal_api/attachments/fake_attachment_downloader.cc', 'internal_api/attachments/fake_attachment_store.cc', 'internal_api/attachments/fake_attachment_uploader.cc', 'internal_api/base_node.cc', @@ -45,7 +47,9 @@ 'internal_api/js_sync_manager_observer.h', 'internal_api/protocol_event_buffer.cc', 'internal_api/protocol_event_buffer.h', + 'internal_api/public/attachments/attachment_downloader_impl.h', 'internal_api/public/attachments/attachment_uploader_impl.h', + 'internal_api/public/attachments/fake_attachment_downloader.h', 'internal_api/public/attachments/fake_attachment_store.h', 'internal_api/public/attachments/fake_attachment_uploader.h', 'internal_api/public/base/ack_handle.cc', diff --git a/sync/sync_tests.gypi b/sync/sync_tests.gypi index 0948f00b..28782eb 100644 --- a/sync/sync_tests.gypi +++ b/sync/sync_tests.gypi @@ -427,7 +427,9 @@ '..', ], 'sources': [ + 'internal_api/attachments/attachment_downloader_impl_unittest.cc', 'internal_api/attachments/attachment_uploader_impl_unittest.cc', + 'internal_api/attachments/fake_attachment_downloader_unittest.cc', 'internal_api/attachments/fake_attachment_store_unittest.cc', 'internal_api/attachments/fake_attachment_uploader_unittest.cc', 'internal_api/debug_info_event_listener_unittest.cc', |