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 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPERATION_TEST_BASE_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPERATION_TEST_BASE_H_
10 #include "base/files/scoped_temp_dir.h"
11 #include "chrome/browser/chromeos/drive/drive.pb.h"
12 #include "chrome/browser/chromeos/drive/file_change.h"
13 #include "chrome/browser/chromeos/drive/file_errors.h"
14 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
15 #include "chrome/browser/chromeos/drive/test_util.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 class TestingPrefServiceSimple;
22 class SequencedTaskRunner;
28 class FakeDriveService;
29 class FakeFreeDiskSpaceGetter;
33 class AboutResourceLoader;
34 class ChangeListLoader;
36 class LoaderController;
37 class ResourceMetadata;
38 class ResourceMetadataStorage;
39 } // namespace internal
41 namespace file_system {
43 // Base fixture class for testing Drive file system operations. It sets up the
44 // basic set of Drive internal classes (ResourceMetadata, Cache, etc) on top of
45 // FakeDriveService for testing.
46 class OperationTestBase : public testing::Test {
48 // OperationDelegate that records all the events.
49 class LoggingDelegate : public OperationDelegate {
51 typedef base::Callback<bool(
52 const std::string& local_id,
53 const FileOperationCallback& callback)> WaitForSyncCompleteHandler;
58 // OperationDelegate overrides.
59 virtual void OnFileChangedByOperation(
60 const FileChange& changed_files) OVERRIDE;
61 virtual void OnEntryUpdatedByOperation(
62 const std::string& local_id) OVERRIDE;
63 virtual void OnDriveSyncError(DriveSyncErrorType type,
64 const std::string& local_id) OVERRIDE;
65 virtual bool WaitForSyncComplete(
66 const std::string& local_id,
67 const FileOperationCallback& callback) OVERRIDE;
69 // Gets the set of changed paths.
70 const FileChange& get_changed_files() { return changed_files_; }
72 // Gets the set of updated local IDs.
73 const std::set<std::string>& updated_local_ids() const {
74 return updated_local_ids_;
77 // Gets the list of drive sync errors.
78 const std::vector<DriveSyncErrorType>& drive_sync_errors() const {
79 return drive_sync_errors_;
82 // Sets the callback used to handle WaitForSyncComplete() method calls.
83 void set_wait_for_sync_complete_handler(
84 const WaitForSyncCompleteHandler& wait_for_sync_complete_handler) {
85 wait_for_sync_complete_handler_ = wait_for_sync_complete_handler;
89 FileChange changed_files_;
90 std::set<std::string> updated_local_ids_;
91 std::vector<DriveSyncErrorType> drive_sync_errors_;
92 WaitForSyncCompleteHandler wait_for_sync_complete_handler_;
96 explicit OperationTestBase(int test_thread_bundle_options);
97 virtual ~OperationTestBase();
99 // testing::Test overrides.
100 virtual void SetUp() OVERRIDE;
102 // Returns the path of the temporary directory for putting test files.
103 base::FilePath temp_dir() const { return temp_dir_.path(); }
105 // Synchronously gets the resource entry corresponding to the path from local
107 FileError GetLocalResourceEntry(const base::FilePath& path,
108 ResourceEntry* entry);
110 // Synchronously gets the resource entry corresponding to the ID from local
112 FileError GetLocalResourceEntryById(const std::string& local_id,
113 ResourceEntry* entry);
115 // Gets the local ID of the entry specified by the path.
116 std::string GetLocalId(const base::FilePath& path);
118 // Synchronously updates |metadata_| by fetching the change feed from the
120 FileError CheckForUpdates();
122 // Accessors for the components.
123 FakeDriveService* fake_service() {
124 return fake_drive_service_.get();
126 EventLogger* logger() { return logger_.get(); }
127 LoggingDelegate* delegate() { return &delegate_; }
128 JobScheduler* scheduler() { return scheduler_.get(); }
129 base::SequencedTaskRunner* blocking_task_runner() {
130 return blocking_task_runner_.get();
132 FakeFreeDiskSpaceGetter* fake_free_disk_space_getter() {
133 return fake_free_disk_space_getter_.get();
135 internal::FileCache* cache() { return cache_.get(); }
136 internal::ResourceMetadata* metadata() { return metadata_.get(); }
137 internal::LoaderController* loader_controller() {
138 return loader_controller_.get();
140 internal::ChangeListLoader* change_list_loader() {
141 return change_list_loader_.get();
145 content::TestBrowserThreadBundle thread_bundle_;
146 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
147 scoped_ptr<TestingPrefServiceSimple> pref_service_;
148 base::ScopedTempDir temp_dir_;
150 LoggingDelegate delegate_;
151 scoped_ptr<EventLogger> logger_;
152 scoped_ptr<FakeDriveService> fake_drive_service_;
153 scoped_ptr<JobScheduler> scheduler_;
154 scoped_ptr<internal::ResourceMetadataStorage,
155 test_util::DestroyHelperForTests> metadata_storage_;
156 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_;
157 scoped_ptr<internal::FileCache, test_util::DestroyHelperForTests> cache_;
158 scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests>
160 scoped_ptr<internal::AboutResourceLoader> about_resource_loader_;
161 scoped_ptr<internal::LoaderController> loader_controller_;
162 scoped_ptr<internal::ChangeListLoader> change_list_loader_;
165 } // namespace file_system
168 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPERATION_TEST_BASE_H_