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/fake_free_disk_space_getter.h"
11 #include "chrome/browser/chromeos/drive/file_cache.h"
12 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
13 #include "chrome/browser/chromeos/drive/job_scheduler.h"
14 #include "chrome/browser/chromeos/drive/resource_metadata.h"
15 #include "chrome/browser/chromeos/drive/test_util.h"
16 #include "chrome/browser/drive/fake_drive_service.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "google_apis/drive/test_util.h"
21 namespace file_system
{
23 OperationTestBase::LoggingObserver::LoggingObserver() {
26 OperationTestBase::LoggingObserver::~LoggingObserver() {
29 void OperationTestBase::LoggingObserver::OnDirectoryChangedByOperation(
30 const base::FilePath
& path
) {
31 changed_paths_
.insert(path
);
34 void OperationTestBase::LoggingObserver::OnCacheFileUploadNeededByOperation(
35 const std::string
& local_id
) {
36 upload_needed_local_ids_
.insert(local_id
);
39 void OperationTestBase::LoggingObserver::OnEntryUpdatedByOperation(
40 const std::string
& local_id
) {
41 updated_local_ids_
.insert(local_id
);
44 void OperationTestBase::LoggingObserver::OnDriveSyncError(
45 DriveSyncErrorType type
, const std::string
& local_id
) {
46 drive_sync_errors_
.push_back(type
);
49 OperationTestBase::OperationTestBase() {
52 OperationTestBase::OperationTestBase(int test_thread_bundle_options
)
53 : thread_bundle_(test_thread_bundle_options
) {
56 OperationTestBase::~OperationTestBase() {
59 void OperationTestBase::SetUp() {
60 scoped_refptr
<base::SequencedWorkerPool
> pool
=
61 content::BrowserThread::GetBlockingPool();
62 blocking_task_runner_
=
63 pool
->GetSequencedTaskRunner(pool
->GetSequenceToken());
65 pref_service_
.reset(new TestingPrefServiceSimple
);
66 test_util::RegisterDrivePrefs(pref_service_
->registry());
68 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
70 fake_drive_service_
.reset(new FakeDriveService
);
71 fake_drive_service_
->LoadResourceListForWapi(
72 "gdata/root_feed.json");
73 fake_drive_service_
->LoadAccountMetadataForWapi(
74 "gdata/account_metadata.json");
76 scheduler_
.reset(new JobScheduler(
78 fake_drive_service_
.get(),
79 blocking_task_runner_
.get()));
81 metadata_storage_
.reset(new internal::ResourceMetadataStorage(
82 temp_dir_
.path(), blocking_task_runner_
.get()));
84 base::PostTaskAndReplyWithResult(
85 blocking_task_runner_
.get(),
87 base::Bind(&internal::ResourceMetadataStorage::Initialize
,
88 base::Unretained(metadata_storage_
.get())),
89 google_apis::test_util::CreateCopyResultCallback(&success
));
90 test_util::RunBlockingPoolTask();
93 metadata_
.reset(new internal::ResourceMetadata(metadata_storage_
.get(),
94 blocking_task_runner_
));
96 FileError error
= FILE_ERROR_FAILED
;
97 base::PostTaskAndReplyWithResult(
98 blocking_task_runner_
.get(),
100 base::Bind(&internal::ResourceMetadata::Initialize
,
101 base::Unretained(metadata_
.get())),
102 google_apis::test_util::CreateCopyResultCallback(&error
));
103 test_util::RunBlockingPoolTask();
104 ASSERT_EQ(FILE_ERROR_OK
, error
);
106 fake_free_disk_space_getter_
.reset(new FakeFreeDiskSpaceGetter
);
107 cache_
.reset(new internal::FileCache(metadata_storage_
.get(),
109 blocking_task_runner_
.get(),
110 fake_free_disk_space_getter_
.get()));
112 base::PostTaskAndReplyWithResult(
113 blocking_task_runner_
.get(),
115 base::Bind(&internal::FileCache::Initialize
,
116 base::Unretained(cache_
.get())),
117 google_apis::test_util::CreateCopyResultCallback(&success
));
118 test_util::RunBlockingPoolTask();
119 ASSERT_TRUE(success
);
121 // Makes sure the FakeDriveService's content is loaded to the metadata_.
122 change_list_loader_
.reset(new internal::ChangeListLoader(
123 blocking_task_runner_
.get(),
126 fake_drive_service_
.get()));
127 change_list_loader_
->LoadForTesting(
128 google_apis::test_util::CreateCopyResultCallback(&error
));
129 test_util::RunBlockingPoolTask();
130 ASSERT_EQ(FILE_ERROR_OK
, error
);
133 FileError
OperationTestBase::GetLocalResourceEntry(const base::FilePath
& path
,
134 ResourceEntry
* entry
) {
135 FileError error
= FILE_ERROR_FAILED
;
136 base::PostTaskAndReplyWithResult(
137 blocking_task_runner(),
139 base::Bind(&internal::ResourceMetadata::GetResourceEntryByPath
,
140 base::Unretained(metadata()), path
, entry
),
141 base::Bind(google_apis::test_util::CreateCopyResultCallback(&error
)));
142 test_util::RunBlockingPoolTask();
146 FileError
OperationTestBase::GetLocalResourceEntryById(
147 const std::string
& local_id
,
148 ResourceEntry
* entry
) {
149 FileError error
= FILE_ERROR_FAILED
;
150 base::PostTaskAndReplyWithResult(
151 blocking_task_runner(),
153 base::Bind(&internal::ResourceMetadata::GetResourceEntryById
,
154 base::Unretained(metadata()), local_id
, entry
),
155 base::Bind(google_apis::test_util::CreateCopyResultCallback(&error
)));
156 test_util::RunBlockingPoolTask();
160 std::string
OperationTestBase::GetLocalId(const base::FilePath
& path
) {
161 std::string local_id
;
162 FileError error
= FILE_ERROR_FAILED
;
163 base::PostTaskAndReplyWithResult(
164 blocking_task_runner(),
166 base::Bind(&internal::ResourceMetadata::GetIdByPath
,
167 base::Unretained(metadata()), path
, &local_id
),
168 base::Bind(google_apis::test_util::CreateCopyResultCallback(&error
)));
169 test_util::RunBlockingPoolTask();
170 EXPECT_EQ(FILE_ERROR_OK
, error
) << path
.value();
174 FileError
OperationTestBase::CheckForUpdates() {
175 FileError error
= FILE_ERROR_FAILED
;
176 change_list_loader_
->CheckForUpdates(
177 google_apis::test_util::CreateCopyResultCallback(&error
));
178 test_util::RunBlockingPoolTask();
182 } // namespace file_system