1 // Copyright 2013 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_system/operation_test_base.h"
7 #include "base/prefs/testing_pref_service.h"
8 #include "base/threading/sequenced_worker_pool.h"
9 #include "chrome/browser/chromeos/drive/change_list_loader.h"
10 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
11 #include "components/drive/event_logger.h"
12 #include "components/drive/fake_free_disk_space_getter.h"
13 #include "components/drive/file_cache.h"
14 #include "components/drive/file_change.h"
15 #include "components/drive/job_scheduler.h"
16 #include "components/drive/resource_metadata.h"
17 #include "components/drive/service/fake_drive_service.h"
18 #include "components/drive/service/test_util.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/test/test_utils.h"
21 #include "google_apis/drive/test_util.h"
24 namespace file_system
{
26 OperationTestBase::LoggingDelegate::LoggingDelegate() {
29 OperationTestBase::LoggingDelegate::~LoggingDelegate() {
32 void OperationTestBase::LoggingDelegate::OnFileChangedByOperation(
33 const FileChange
& changed_files
) {
34 changed_files_
.Apply(changed_files
);
37 void OperationTestBase::LoggingDelegate::OnEntryUpdatedByOperation(
38 const ClientContext
& /* client_context */,
39 const std::string
& local_id
) {
40 updated_local_ids_
.insert(local_id
);
43 void OperationTestBase::LoggingDelegate::OnDriveSyncError(
44 DriveSyncErrorType type
, const std::string
& local_id
) {
45 drive_sync_errors_
.push_back(type
);
48 bool OperationTestBase::LoggingDelegate::WaitForSyncComplete(
49 const std::string
& local_id
,
50 const FileOperationCallback
& callback
) {
51 return wait_for_sync_complete_handler_
.is_null() ?
52 false : wait_for_sync_complete_handler_
.Run(local_id
, callback
);
55 OperationTestBase::OperationTestBase() {
58 OperationTestBase::OperationTestBase(int test_thread_bundle_options
)
59 : thread_bundle_(test_thread_bundle_options
) {
62 OperationTestBase::~OperationTestBase() {
65 void OperationTestBase::SetUp() {
66 scoped_refptr
<base::SequencedWorkerPool
> pool
=
67 content::BrowserThread::GetBlockingPool();
68 blocking_task_runner_
=
69 pool
->GetSequencedTaskRunner(pool
->GetSequenceToken());
71 pref_service_
.reset(new TestingPrefServiceSimple
);
72 test_util::RegisterDrivePrefs(pref_service_
->registry());
74 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
76 logger_
.reset(new EventLogger
);
78 fake_drive_service_
.reset(new FakeDriveService
);
79 ASSERT_TRUE(test_util::SetUpTestEntries(fake_drive_service_
.get()));
81 scheduler_
.reset(new JobScheduler(
84 fake_drive_service_
.get(),
85 blocking_task_runner_
.get()));
87 metadata_storage_
.reset(new internal::ResourceMetadataStorage(
88 temp_dir_
.path(), blocking_task_runner_
.get()));
90 base::PostTaskAndReplyWithResult(
91 blocking_task_runner_
.get(),
93 base::Bind(&internal::ResourceMetadataStorage::Initialize
,
94 base::Unretained(metadata_storage_
.get())),
95 google_apis::test_util::CreateCopyResultCallback(&success
));
96 content::RunAllBlockingPoolTasksUntilIdle();
99 fake_free_disk_space_getter_
.reset(new FakeFreeDiskSpaceGetter
);
100 cache_
.reset(new internal::FileCache(metadata_storage_
.get(),
102 blocking_task_runner_
.get(),
103 fake_free_disk_space_getter_
.get()));
105 base::PostTaskAndReplyWithResult(
106 blocking_task_runner_
.get(),
108 base::Bind(&internal::FileCache::Initialize
,
109 base::Unretained(cache_
.get())),
110 google_apis::test_util::CreateCopyResultCallback(&success
));
111 content::RunAllBlockingPoolTasksUntilIdle();
112 ASSERT_TRUE(success
);
114 metadata_
.reset(new internal::ResourceMetadata(metadata_storage_
.get(),
116 blocking_task_runner_
));
118 FileError error
= FILE_ERROR_FAILED
;
119 base::PostTaskAndReplyWithResult(
120 blocking_task_runner_
.get(),
122 base::Bind(&internal::ResourceMetadata::Initialize
,
123 base::Unretained(metadata_
.get())),
124 google_apis::test_util::CreateCopyResultCallback(&error
));
125 content::RunAllBlockingPoolTasksUntilIdle();
126 ASSERT_EQ(FILE_ERROR_OK
, error
);
128 // Makes sure the FakeDriveService's content is loaded to the metadata_.
129 about_resource_loader_
.reset(new internal::AboutResourceLoader(
131 loader_controller_
.reset(new internal::LoaderController
);
132 change_list_loader_
.reset(new internal::ChangeListLoader(
134 blocking_task_runner_
.get(),
137 about_resource_loader_
.get(),
138 loader_controller_
.get()));
139 change_list_loader_
->LoadIfNeeded(
140 google_apis::test_util::CreateCopyResultCallback(&error
));
141 content::RunAllBlockingPoolTasksUntilIdle();
142 ASSERT_EQ(FILE_ERROR_OK
, error
);
145 FileError
OperationTestBase::GetLocalResourceEntry(const base::FilePath
& path
,
146 ResourceEntry
* entry
) {
147 FileError error
= FILE_ERROR_FAILED
;
148 base::PostTaskAndReplyWithResult(
149 blocking_task_runner(),
151 base::Bind(&internal::ResourceMetadata::GetResourceEntryByPath
,
152 base::Unretained(metadata()), path
, entry
),
153 base::Bind(google_apis::test_util::CreateCopyResultCallback(&error
)));
154 content::RunAllBlockingPoolTasksUntilIdle();
158 FileError
OperationTestBase::GetLocalResourceEntryById(
159 const std::string
& local_id
,
160 ResourceEntry
* entry
) {
161 FileError error
= FILE_ERROR_FAILED
;
162 base::PostTaskAndReplyWithResult(
163 blocking_task_runner(),
165 base::Bind(&internal::ResourceMetadata::GetResourceEntryById
,
166 base::Unretained(metadata()), local_id
, entry
),
167 base::Bind(google_apis::test_util::CreateCopyResultCallback(&error
)));
168 content::RunAllBlockingPoolTasksUntilIdle();
172 std::string
OperationTestBase::GetLocalId(const base::FilePath
& path
) {
173 std::string local_id
;
174 FileError error
= FILE_ERROR_FAILED
;
175 base::PostTaskAndReplyWithResult(
176 blocking_task_runner(),
178 base::Bind(&internal::ResourceMetadata::GetIdByPath
,
179 base::Unretained(metadata()), path
, &local_id
),
180 base::Bind(google_apis::test_util::CreateCopyResultCallback(&error
)));
181 content::RunAllBlockingPoolTasksUntilIdle();
182 EXPECT_EQ(FILE_ERROR_OK
, error
) << path
.value();
186 FileError
OperationTestBase::CheckForUpdates() {
187 FileError error
= FILE_ERROR_FAILED
;
188 change_list_loader_
->CheckForUpdates(
189 google_apis::test_util::CreateCopyResultCallback(&error
));
190 content::RunAllBlockingPoolTasksUntilIdle();
194 } // namespace file_system