Chromium Code Reviews| Index: chrome/browser/chromeos/drive/file_task_executor_unittest.cc |
| diff --git a/chrome/browser/chromeos/drive/file_task_executor_unittest.cc b/chrome/browser/chromeos/drive/file_task_executor_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..85eff55d44df8403ab5f5132f3419fe34f3901e4 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/drive/file_task_executor_unittest.cc |
| @@ -0,0 +1,152 @@ |
| +// 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 "chrome/browser/chromeos/drive/file_task_executor.h" |
| + |
| +#include <set> |
| +#include <string> |
| + |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/chromeos/drive/fake_file_system.h" |
| +#include "chrome/browser/drive/fake_drive_service.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "google_apis/drive/drive_api_parser.h" |
| +#include "google_apis/drive/test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webkit/browser/fileapi/file_system_url.h" |
| + |
| +namespace drive { |
| + |
| +namespace { |
| + |
| +// Test harness for verifying the behavior of FileTaskExecutor. |
| +class TestDelegate : public FileTaskExecutorDelegate { |
| + public: |
| + explicit TestDelegate(std::set<std::string>* opend_urls) |
| + : opend_urls_(opend_urls), |
| + fake_drive_service_(new FakeDriveService), |
| + fake_file_system_(new test_util::FakeFileSystem( |
| + fake_drive_service_.get())) { |
| + fake_drive_service_->set_open_url_format("http://openlink/%s/%s"); |
| + } |
| + |
| + // FileTaskExecutorDelegate overrides. |
| + virtual FileSystemInterface* GetFileSystem() OVERRIDE { |
| + return fake_file_system_.get(); |
| + } |
| + |
| + virtual DriveServiceInterface* GetDriveService() OVERRIDE { |
| + return fake_drive_service_.get(); |
| + } |
| + |
| + virtual void OpenBrowserWindow(const GURL& open_link) OVERRIDE { |
| + opend_urls_->insert(open_link.spec()); |
| + } |
| + |
| + // Sets up files on the fake Drive service. |
| + bool SetUpTestFiles() { |
| + { |
| + google_apis::GDataErrorCode result = google_apis::GDATA_OTHER_ERROR; |
| + scoped_ptr<google_apis::FileResource> file; |
| + fake_drive_service_->AddNewFileWithResourceId( |
| + "id1", |
| + "text/plain", |
| + "random data", |
| + fake_drive_service_->GetRootResourceId(), |
| + "file1.txt", |
| + false, |
| + google_apis::test_util::CreateCopyResultCallback(&result, &file)); |
| + base::RunLoop().RunUntilIdle(); |
| + if (result != google_apis::HTTP_CREATED) |
| + return false; |
| + } |
| + { |
| + google_apis::GDataErrorCode result = google_apis::GDATA_OTHER_ERROR; |
| + scoped_ptr<google_apis::FileResource> file; |
| + fake_drive_service_->AddNewFileWithResourceId( |
| + "id2", |
| + "text/plain", |
| + "random data", |
| + fake_drive_service_->GetRootResourceId(), |
| + "file2.txt", |
| + false, |
| + google_apis::test_util::CreateCopyResultCallback(&result, &file)); |
| + base::RunLoop().RunUntilIdle(); |
| + if (result != google_apis::HTTP_CREATED) |
| + return false; |
| + } |
| + return true; |
| + } |
| + |
| + private: |
| + std::set<std::string>* opend_urls_; |
|
hirono
2014/07/16 04:33:19
nit: std::set<std::string>* const
kinaba
2014/07/16 04:40:25
Done.
|
| + scoped_ptr<FakeDriveService> fake_drive_service_; |
| + scoped_ptr<test_util::FakeFileSystem> fake_file_system_; |
| +}; |
| + |
| +} // namespace |
| + |
| +TEST(FileTaskExecutorTest, DriveAppOpenSuccess) { |
| + content::TestBrowserThreadBundle thread_bundle; |
| + |
| + std::set<std::string> opend_urls; |
| + |
| + // |delegate_ptr| will be owned by |executor|. |
| + TestDelegate* delegate_ptr = new TestDelegate(&opend_urls); |
|
hirono
2014/07/16 04:33:20
nit: TestDelegate* const
kinaba
2014/07/16 04:40:26
Done.
|
| + ASSERT_TRUE(delegate_ptr->SetUpTestFiles()); |
| + // |executor| deletes itself after Execute() is finished. |
| + FileTaskExecutor* executor = new FileTaskExecutor( |
|
hirono
2014/07/16 04:33:19
nit: FileTaskExecutor* const
kinaba
2014/07/16 04:40:26
Done.
|
| + scoped_ptr<FileTaskExecutorDelegate>(delegate_ptr), "test-app-id"); |
| + |
| + std::vector<fileapi::FileSystemURL> urls; |
| + urls.push_back(fileapi::FileSystemURL::CreateForTest( |
| + GURL("http://origin/"), |
| + fileapi::kFileSystemTypeDrive, |
| + base::FilePath::FromUTF8Unsafe("/special/drive/root/file1.txt"))); |
| + urls.push_back(fileapi::FileSystemURL::CreateForTest( |
| + GURL("http://origin/"), |
| + fileapi::kFileSystemTypeDrive, |
| + base::FilePath::FromUTF8Unsafe("/special/drive/root/file2.txt"))); |
| + |
| + extensions::api::file_browser_private::TaskResult result = |
|
hirono
2014/07/16 04:33:19
nit: const
kinaba
2014/07/16 04:40:26
This is to be modified in CreateCopyResultCallback
|
| + extensions::api::file_browser_private::TASK_RESULT_NONE; |
| + executor->Execute(urls, |
| + google_apis::test_util::CreateCopyResultCallback(&result)); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_EQ(extensions::api::file_browser_private::TASK_RESULT_OPENED, result); |
| + ASSERT_EQ(2u, opend_urls.size()); |
| + EXPECT_TRUE(opend_urls.count("http://openlink/id1/test-app-id")); |
| + EXPECT_TRUE(opend_urls.count("http://openlink/id2/test-app-id")); |
| +} |
| + |
| +TEST(FileTaskExecutorTest, DriveAppOpenFailForNonExistingFile) { |
| + content::TestBrowserThreadBundle thread_bundle; |
| + |
| + std::set<std::string> opend_urls; |
| + |
| + // |delegate_ptr| will be owned by |executor|. |
| + TestDelegate* delegate_ptr = new TestDelegate(&opend_urls); |
|
hirono
2014/07/16 04:33:19
ditto.
kinaba
2014/07/16 04:40:26
Done.
|
| + ASSERT_TRUE(delegate_ptr->SetUpTestFiles()); |
| + // |executor| deletes itself after Execute() is finished. |
| + FileTaskExecutor* executor = new FileTaskExecutor( |
|
hirono
2014/07/16 04:33:19
ditto.
kinaba
2014/07/16 04:40:26
Done.
|
| + scoped_ptr<FileTaskExecutorDelegate>(delegate_ptr), "test-app-id"); |
| + |
| + std::vector<fileapi::FileSystemURL> urls; |
| + urls.push_back(fileapi::FileSystemURL::CreateForTest( |
| + GURL("http://origin/"), |
| + fileapi::kFileSystemTypeDrive, |
| + base::FilePath::FromUTF8Unsafe("/special/drive/root/not-exist.txt"))); |
| + |
| + extensions::api::file_browser_private::TaskResult result = |
|
hirono
2014/07/16 04:33:19
ditto
kinaba
2014/07/16 04:40:26
ditto (it has to be mutable.)
|
| + extensions::api::file_browser_private::TASK_RESULT_NONE; |
| + executor->Execute(urls, |
| + google_apis::test_util::CreateCopyResultCallback(&result)); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_EQ(extensions::api::file_browser_private::TASK_RESULT_FAILED, result); |
| + ASSERT_TRUE(opend_urls.empty()); |
| +} |
| + |
| +} // namespace drive |