fake_file_system.h revision bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3
1// Copyright (c) 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_FAKE_FILE_SYSTEM_H_ 6#define CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_ 7 8#include <string> 9 10#include "base/basictypes.h" 11#include "base/callback_forward.h" 12#include "base/files/scoped_temp_dir.h" 13#include "base/memory/scoped_ptr.h" 14#include "chrome/browser/chromeos/drive/file_errors.h" 15#include "chrome/browser/chromeos/drive/file_system_interface.h" 16#include "chrome/browser/google_apis/gdata_errorcode.h" 17 18namespace google_apis { 19 20class AboutResource; 21class ResourceEntry; 22class ResourceList; 23 24} // namespace google_apis 25 26namespace drive { 27 28class DriveServiceInterface; 29class FileSystemObserver; 30class ResourceEntry; 31 32namespace test_util { 33 34// This class implements a fake FileSystem which acts like a real Drive 35// file system with FakeDriveService, for testing purpose. 36// Note that this class doesn't support "caching" at the moment, so the number 37// of interactions to the FakeDriveService may be bigger than the real 38// implementation. 39// Currently most methods are empty (not implemented). 40class FakeFileSystem : public FileSystemInterface { 41 public: 42 explicit FakeFileSystem(DriveServiceInterface* drive_service); 43 virtual ~FakeFileSystem(); 44 45 // Initialization for testing. This can be called instead of Initialize 46 // for testing purpose. Returns true for success. 47 bool InitializeForTesting(); 48 49 // FileSystemInterface Overrides. 50 virtual void Initialize() OVERRIDE; 51 virtual void AddObserver(FileSystemObserver* observer) OVERRIDE; 52 virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE; 53 virtual void CheckForUpdates() OVERRIDE; 54 virtual void TransferFileFromRemoteToLocal( 55 const base::FilePath& remote_src_file_path, 56 const base::FilePath& local_dest_file_path, 57 const FileOperationCallback& callback) OVERRIDE; 58 virtual void TransferFileFromLocalToRemote( 59 const base::FilePath& local_src_file_path, 60 const base::FilePath& remote_dest_file_path, 61 const FileOperationCallback& callback) OVERRIDE; 62 virtual void OpenFile(const base::FilePath& file_path, 63 OpenMode open_mode, 64 const OpenFileCallback& callback) OVERRIDE; 65 virtual void Copy(const base::FilePath& src_file_path, 66 const base::FilePath& dest_file_path, 67 const FileOperationCallback& callback) OVERRIDE; 68 virtual void Move(const base::FilePath& src_file_path, 69 const base::FilePath& dest_file_path, 70 const FileOperationCallback& callback) OVERRIDE; 71 virtual void Remove(const base::FilePath& file_path, 72 bool is_recursive, 73 const FileOperationCallback& callback) OVERRIDE; 74 virtual void CreateDirectory(const base::FilePath& directory_path, 75 bool is_exclusive, 76 bool is_recursive, 77 const FileOperationCallback& callback) OVERRIDE; 78 virtual void CreateFile(const base::FilePath& file_path, 79 bool is_exclusive, 80 const FileOperationCallback& callback) OVERRIDE; 81 virtual void TouchFile(const base::FilePath& file_path, 82 const base::Time& last_access_time, 83 const base::Time& last_modified_time, 84 const FileOperationCallback& callback) OVERRIDE; 85 virtual void TruncateFile(const base::FilePath& file_path, 86 int64 length, 87 const FileOperationCallback& callback) OVERRIDE; 88 virtual void Pin(const base::FilePath& file_path, 89 const FileOperationCallback& callback) OVERRIDE; 90 virtual void Unpin(const base::FilePath& file_path, 91 const FileOperationCallback& callback) OVERRIDE; 92 virtual void GetFileByPath(const base::FilePath& file_path, 93 const GetFileCallback& callback) OVERRIDE; 94 virtual void GetFileContentByPath( 95 const base::FilePath& file_path, 96 const GetFileContentInitializedCallback& initialized_callback, 97 const google_apis::GetContentCallback& get_content_callback, 98 const FileOperationCallback& completion_callback) OVERRIDE; 99 virtual void GetResourceEntryByPath( 100 const base::FilePath& file_path, 101 const GetResourceEntryCallback& callback) OVERRIDE; 102 virtual void ReadDirectoryByPath( 103 const base::FilePath& file_path, 104 const ReadDirectoryCallback& callback) OVERRIDE; 105 virtual void Search(const std::string& search_query, 106 const GURL& next_url, 107 const SearchCallback& callback) OVERRIDE; 108 virtual void SearchMetadata(const std::string& query, 109 int options, 110 int at_most_num_matches, 111 const SearchMetadataCallback& callback) OVERRIDE; 112 virtual void GetAvailableSpace( 113 const GetAvailableSpaceCallback& callback) OVERRIDE; 114 virtual void GetShareUrl( 115 const base::FilePath& file_path, 116 const GURL& embed_origin, 117 const GetShareUrlCallback& callback) OVERRIDE; 118 virtual void GetMetadata( 119 const GetFilesystemMetadataCallback& callback) OVERRIDE; 120 virtual void MarkCacheFileAsMounted( 121 const base::FilePath& drive_file_path, 122 const MarkMountedCallback& callback) OVERRIDE; 123 virtual void MarkCacheFileAsUnmounted( 124 const base::FilePath& cache_file_path, 125 const FileOperationCallback& callback) OVERRIDE; 126 virtual void GetCacheEntryByResourceId( 127 const std::string& resource_id, 128 const std::string& md5, 129 const GetCacheEntryCallback& callback) OVERRIDE; 130 virtual void Reload() OVERRIDE; 131 132 private: 133 // Helper of GetResourceEntryById. 134 void GetResourceEntryByIdAfterGetResourceEntry( 135 const GetResourceEntryCallback& callback, 136 google_apis::GDataErrorCode error_in, 137 scoped_ptr<google_apis::ResourceEntry> resource_entry); 138 139 // Helpers of GetFileContentByPath. 140 // How the method works: 141 // 1) Gets ResourceEntry of the path. 142 // 2) Look at if there is a cache file or not. If found return it. 143 // 3) Otherwise start DownloadFile. 144 // 4) Runs the |completion_callback| upon the download completion. 145 void GetFileContentByPathAfterGetResourceEntry( 146 const GetFileContentInitializedCallback& initialized_callback, 147 const google_apis::GetContentCallback& get_content_callback, 148 const FileOperationCallback& completion_callback, 149 FileError error, 150 scoped_ptr<ResourceEntry> entry); 151 void GetFileContentByPathAfterGetWapiResourceEntry( 152 const GetFileContentInitializedCallback& initialized_callback, 153 const google_apis::GetContentCallback& get_content_callback, 154 const FileOperationCallback& completion_callback, 155 google_apis::GDataErrorCode gdata_error, 156 scoped_ptr<google_apis::ResourceEntry> gdata_entry); 157 void GetFileContentByPathAfterDownloadFile( 158 const FileOperationCallback& completion_callback, 159 google_apis::GDataErrorCode gdata_error, 160 const base::FilePath& temp_file); 161 162 // Helpers of GetResourceEntryByPath. 163 // How the method works: 164 // 1) If the path is root, gets AboutResrouce from the drive service 165 // and create ResourceEntry. 166 // 2-1) Otherwise, gets the parent's ResourceEntry by recursive call. 167 // 2-2) Then, gets the resource list by restricting the parent with its id. 168 // 2-3) Search the results based on title, and return the ResourceEntry. 169 // Note that adding suffix (e.g. " (2)") for files sharing a same name is 170 // not supported in FakeFileSystem. Thus, even if the server has 171 // files sharing the same name under a directory, the second (or later) 172 // file cannot be taken with the suffixed name. 173 void GetResourceEntryByPathAfterGetAboutResource( 174 const GetResourceEntryCallback& callback, 175 google_apis::GDataErrorCode gdata_error, 176 scoped_ptr<google_apis::AboutResource> about_resource); 177 void GetResourceEntryByPathAfterGetParentEntryInfo( 178 const base::FilePath& base_name, 179 const GetResourceEntryCallback& callback, 180 FileError error, 181 scoped_ptr<ResourceEntry> parent_entry); 182 void GetResourceEntryByPathAfterGetResourceList( 183 const base::FilePath& base_name, 184 const GetResourceEntryCallback& callback, 185 google_apis::GDataErrorCode gdata_error, 186 scoped_ptr<google_apis::ResourceList> resource_list); 187 188 DriveServiceInterface* drive_service_; // Not owned. 189 base::ScopedTempDir cache_dir_; 190 191 // Note: This should remain the last member so it'll be destroyed and 192 // invalidate the weak pointers before any other members are destroyed. 193 base::WeakPtrFactory<FakeFileSystem> weak_ptr_factory_; 194 195 DISALLOW_COPY_AND_ASSIGN(FakeFileSystem); 196}; 197 198} // namespace test_util 199} // namespace drive 200 201#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_ 202