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. 4 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPERATION_TEST_BASE_H_ 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPERATION_TEST_BASE_H_ 7 8 #include <set> 9 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" 18 19 class TestingPrefServiceSimple; 20 21 namespace base { 22 class SequencedTaskRunner; 23 } // namespace base 24 25 namespace drive { 26 27 class EventLogger; 28 class FakeDriveService; 29 class FakeFreeDiskSpaceGetter; 30 class JobScheduler; 31 32 namespace internal { 33 class AboutResourceLoader; 34 class ChangeListLoader; 35 class FileCache; 36 class LoaderController; 37 class ResourceMetadata; 38 class ResourceMetadataStorage; 39 } // namespace internal 40 41 namespace file_system { 42 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 { 47 protected: 48 // OperationDelegate that records all the events. 49 class LoggingDelegate : public OperationDelegate { 50 public: 51 typedef base::Callback<bool( 52 const std::string& local_id, 53 const FileOperationCallback& callback)> WaitForSyncCompleteHandler; 54 55 LoggingDelegate(); 56 ~LoggingDelegate(); 57 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; 68 69 // Gets the set of changed paths. 70 const FileChange& get_changed_files() { return changed_files_; } 71 72 // Gets the set of updated local IDs. 73 const std::set<std::string>& updated_local_ids() const { 74 return updated_local_ids_; 75 } 76 77 // Gets the list of drive sync errors. 78 const std::vector<DriveSyncErrorType>& drive_sync_errors() const { 79 return drive_sync_errors_; 80 } 81 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; 86 } 87 88 private: 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_; 93 }; 94 95 OperationTestBase(); 96 explicit OperationTestBase(int test_thread_bundle_options); 97 virtual ~OperationTestBase(); 98 99 // testing::Test overrides. 100 virtual void SetUp() OVERRIDE; 101 102 // Returns the path of the temporary directory for putting test files. 103 base::FilePath temp_dir() const { return temp_dir_.path(); } 104 105 // Synchronously gets the resource entry corresponding to the path from local 106 // ResourceMetadta. 107 FileError GetLocalResourceEntry(const base::FilePath& path, 108 ResourceEntry* entry); 109 110 // Synchronously gets the resource entry corresponding to the ID from local 111 // ResourceMetadta. 112 FileError GetLocalResourceEntryById(const std::string& local_id, 113 ResourceEntry* entry); 114 115 // Gets the local ID of the entry specified by the path. 116 std::string GetLocalId(const base::FilePath& path); 117 118 // Synchronously updates |metadata_| by fetching the change feed from the 119 // |fake_service_|. 120 FileError CheckForUpdates(); 121 122 // Accessors for the components. 123 FakeDriveService* fake_service() { 124 return fake_drive_service_.get(); 125 } 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(); 131 } 132 FakeFreeDiskSpaceGetter* fake_free_disk_space_getter() { 133 return fake_free_disk_space_getter_.get(); 134 } 135 internal::FileCache* cache() { return cache_.get(); } 136 internal::ResourceMetadata* metadata() { return metadata_.get(); } 137 internal::LoaderController* loader_controller() { 138 return loader_controller_.get(); 139 } 140 internal::ChangeListLoader* change_list_loader() { 141 return change_list_loader_.get(); 142 } 143 144 private: 145 content::TestBrowserThreadBundle thread_bundle_; 146 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; 147 scoped_ptr<TestingPrefServiceSimple> pref_service_; 148 base::ScopedTempDir temp_dir_; 149 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> 159 metadata_; 160 scoped_ptr<internal::AboutResourceLoader> about_resource_loader_; 161 scoped_ptr<internal::LoaderController> loader_controller_; 162 scoped_ptr<internal::ChangeListLoader> change_list_loader_; 163 }; 164 165 } // namespace file_system 166 } // namespace drive 167 168 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPERATION_TEST_BASE_H_ 169