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.
5 #include "chrome/browser/chromeos/drive/file_task_executor.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/chromeos/drive/fake_file_system.h"
12 #include "components/drive/service/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"
23 // Test harness for verifying the behavior of FileTaskExecutor.
24 class TestDelegate
: public FileTaskExecutorDelegate
{
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");
34 // FileTaskExecutorDelegate overrides.
35 FileSystemInterface
* GetFileSystem() override
{
36 return fake_file_system_
.get();
39 DriveServiceInterface
* GetDriveService() override
{
40 return fake_drive_service_
.get();
43 void OpenBrowserWindow(const GURL
& open_link
) override
{
44 opend_urls_
->insert(open_link
.spec());
47 // Sets up files on the fake Drive service.
48 bool SetUpTestFiles() {
50 google_apis::DriveApiErrorCode result
= google_apis::DRIVE_OTHER_ERROR
;
51 scoped_ptr
<google_apis::FileResource
> file
;
52 fake_drive_service_
->AddNewFileWithResourceId(
56 fake_drive_service_
->GetRootResourceId(),
59 google_apis::test_util::CreateCopyResultCallback(&result
, &file
));
60 base::RunLoop().RunUntilIdle();
61 if (result
!= google_apis::HTTP_CREATED
)
65 google_apis::DriveApiErrorCode result
= google_apis::DRIVE_OTHER_ERROR
;
66 scoped_ptr
<google_apis::FileResource
> file
;
67 fake_drive_service_
->AddNewFileWithResourceId(
71 fake_drive_service_
->GetRootResourceId(),
74 google_apis::test_util::CreateCopyResultCallback(&result
, &file
));
75 base::RunLoop().RunUntilIdle();
76 if (result
!= google_apis::HTTP_CREATED
)
83 std::set
<std::string
>* const opend_urls_
;
84 scoped_ptr
<FakeDriveService
> fake_drive_service_
;
85 scoped_ptr
<test_util::FakeFileSystem
> fake_file_system_
;
90 TEST(FileTaskExecutorTest
, DriveAppOpenSuccess
) {
91 content::TestBrowserThreadBundle thread_bundle
;
93 std::set
<std::string
> opend_urls
;
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");
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")));
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();
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"));
124 TEST(FileTaskExecutorTest
, DriveAppOpenFailForNonExistingFile
) {
125 content::TestBrowserThreadBundle thread_bundle
;
127 std::set
<std::string
> opend_urls
;
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");
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")));
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();
148 EXPECT_EQ(extensions::api::file_manager_private::TASK_RESULT_FAILED
, result
);
149 ASSERT_TRUE(opend_urls
.empty());