file_task_executor_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 "chrome/browser/chromeos/drive/file_task_executor.h" 6 7#include <set> 8#include <string> 9 10#include "base/run_loop.h" 11#include "chrome/browser/chromeos/drive/fake_file_system.h" 12#include "chrome/browser/drive/fake_drive_service.h" 13#include "content/public/test/test_browser_thread_bundle.h" 14#include "google_apis/drive/drive_api_parser.h" 15#include "google_apis/drive/test_util.h" 16#include "storage/browser/fileapi/file_system_url.h" 17#include "testing/gtest/include/gtest/gtest.h" 18 19namespace drive { 20 21namespace { 22 23// Test harness for verifying the behavior of FileTaskExecutor. 24class TestDelegate : public FileTaskExecutorDelegate { 25 public: 26 explicit TestDelegate(std::set<std::string>* opend_urls) 27 : opend_urls_(opend_urls), 28 fake_drive_service_(new FakeDriveService), 29 fake_file_system_(new test_util::FakeFileSystem( 30 fake_drive_service_.get())) { 31 fake_drive_service_->set_open_url_format("http://openlink/%s/%s"); 32 } 33 34 // FileTaskExecutorDelegate overrides. 35 virtual FileSystemInterface* GetFileSystem() OVERRIDE { 36 return fake_file_system_.get(); 37 } 38 39 virtual DriveServiceInterface* GetDriveService() OVERRIDE { 40 return fake_drive_service_.get(); 41 } 42 43 virtual void OpenBrowserWindow(const GURL& open_link) OVERRIDE { 44 opend_urls_->insert(open_link.spec()); 45 } 46 47 // Sets up files on the fake Drive service. 48 bool SetUpTestFiles() { 49 { 50 google_apis::GDataErrorCode result = google_apis::GDATA_OTHER_ERROR; 51 scoped_ptr<google_apis::FileResource> file; 52 fake_drive_service_->AddNewFileWithResourceId( 53 "id1", 54 "text/plain", 55 "random data", 56 fake_drive_service_->GetRootResourceId(), 57 "file1.txt", 58 false, 59 google_apis::test_util::CreateCopyResultCallback(&result, &file)); 60 base::RunLoop().RunUntilIdle(); 61 if (result != google_apis::HTTP_CREATED) 62 return false; 63 } 64 { 65 google_apis::GDataErrorCode result = google_apis::GDATA_OTHER_ERROR; 66 scoped_ptr<google_apis::FileResource> file; 67 fake_drive_service_->AddNewFileWithResourceId( 68 "id2", 69 "text/plain", 70 "random data", 71 fake_drive_service_->GetRootResourceId(), 72 "file2.txt", 73 false, 74 google_apis::test_util::CreateCopyResultCallback(&result, &file)); 75 base::RunLoop().RunUntilIdle(); 76 if (result != google_apis::HTTP_CREATED) 77 return false; 78 } 79 return true; 80 } 81 82 private: 83 std::set<std::string>* const opend_urls_; 84 scoped_ptr<FakeDriveService> fake_drive_service_; 85 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; 86}; 87 88} // namespace 89 90TEST(FileTaskExecutorTest, DriveAppOpenSuccess) { 91 content::TestBrowserThreadBundle thread_bundle; 92 93 std::set<std::string> opend_urls; 94 95 // |delegate_ptr| will be owned by |executor|. 96 TestDelegate* const delegate_ptr = new TestDelegate(&opend_urls); 97 ASSERT_TRUE(delegate_ptr->SetUpTestFiles()); 98 // |executor| deletes itself after Execute() is finished. 99 FileTaskExecutor* const executor = new FileTaskExecutor( 100 scoped_ptr<FileTaskExecutorDelegate>(delegate_ptr), "test-app-id"); 101 102 std::vector<storage::FileSystemURL> urls; 103 urls.push_back(storage::FileSystemURL::CreateForTest( 104 GURL("http://origin/"), 105 storage::kFileSystemTypeDrive, 106 base::FilePath::FromUTF8Unsafe("/special/drive/root/file1.txt"))); 107 urls.push_back(storage::FileSystemURL::CreateForTest( 108 GURL("http://origin/"), 109 storage::kFileSystemTypeDrive, 110 base::FilePath::FromUTF8Unsafe("/special/drive/root/file2.txt"))); 111 112 extensions::api::file_manager_private::TaskResult result = 113 extensions::api::file_manager_private::TASK_RESULT_NONE; 114 executor->Execute(urls, 115 google_apis::test_util::CreateCopyResultCallback(&result)); 116 base::RunLoop().RunUntilIdle(); 117 118 EXPECT_EQ(extensions::api::file_manager_private::TASK_RESULT_OPENED, result); 119 ASSERT_EQ(2u, opend_urls.size()); 120 EXPECT_TRUE(opend_urls.count("http://openlink/id1/test-app-id")); 121 EXPECT_TRUE(opend_urls.count("http://openlink/id2/test-app-id")); 122} 123 124TEST(FileTaskExecutorTest, DriveAppOpenFailForNonExistingFile) { 125 content::TestBrowserThreadBundle thread_bundle; 126 127 std::set<std::string> opend_urls; 128 129 // |delegate_ptr| will be owned by |executor|. 130 TestDelegate* const delegate_ptr = new TestDelegate(&opend_urls); 131 ASSERT_TRUE(delegate_ptr->SetUpTestFiles()); 132 // |executor| deletes itself after Execute() is finished. 133 FileTaskExecutor* const executor = new FileTaskExecutor( 134 scoped_ptr<FileTaskExecutorDelegate>(delegate_ptr), "test-app-id"); 135 136 std::vector<storage::FileSystemURL> urls; 137 urls.push_back(storage::FileSystemURL::CreateForTest( 138 GURL("http://origin/"), 139 storage::kFileSystemTypeDrive, 140 base::FilePath::FromUTF8Unsafe("/special/drive/root/not-exist.txt"))); 141 142 extensions::api::file_manager_private::TaskResult result = 143 extensions::api::file_manager_private::TASK_RESULT_NONE; 144 executor->Execute(urls, 145 google_apis::test_util::CreateCopyResultCallback(&result)); 146 base::RunLoop().RunUntilIdle(); 147 148 EXPECT_EQ(extensions::api::file_manager_private::TASK_RESULT_FAILED, result); 149 ASSERT_TRUE(opend_urls.empty()); 150} 151 152} // namespace drive 153