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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_
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 "google_apis/drive/gdata_errorcode.h"
18 namespace google_apis
{
23 } // namespace google_apis
27 class DriveServiceInterface
;
28 class FileSystemObserver
;
33 // This class implements a fake FileSystem which acts like a real Drive
34 // file system with FakeDriveService, for testing purpose.
35 // Note that this class doesn't support "caching" at the moment, so the number
36 // of interactions to the FakeDriveService may be bigger than the real
38 // Currently most methods are empty (not implemented).
39 class FakeFileSystem
: public FileSystemInterface
{
41 explicit FakeFileSystem(DriveServiceInterface
* drive_service
);
42 virtual ~FakeFileSystem();
44 // FileSystemInterface Overrides.
45 virtual void AddObserver(FileSystemObserver
* observer
) override
;
46 virtual void RemoveObserver(FileSystemObserver
* observer
) override
;
47 virtual void CheckForUpdates() override
;
48 virtual void TransferFileFromLocalToRemote(
49 const base::FilePath
& local_src_file_path
,
50 const base::FilePath
& remote_dest_file_path
,
51 const FileOperationCallback
& callback
) override
;
52 virtual void OpenFile(const base::FilePath
& file_path
,
54 const std::string
& mime_type
,
55 const OpenFileCallback
& callback
) override
;
56 virtual void Copy(const base::FilePath
& src_file_path
,
57 const base::FilePath
& dest_file_path
,
58 bool preserve_last_modified
,
59 const FileOperationCallback
& callback
) override
;
60 virtual void Move(const base::FilePath
& src_file_path
,
61 const base::FilePath
& dest_file_path
,
62 const FileOperationCallback
& callback
) override
;
63 virtual void Remove(const base::FilePath
& file_path
,
65 const FileOperationCallback
& callback
) override
;
66 virtual void CreateDirectory(const base::FilePath
& directory_path
,
69 const FileOperationCallback
& callback
) override
;
70 virtual void CreateFile(const base::FilePath
& file_path
,
72 const std::string
& mime_type
,
73 const FileOperationCallback
& callback
) override
;
74 virtual void TouchFile(const base::FilePath
& file_path
,
75 const base::Time
& last_access_time
,
76 const base::Time
& last_modified_time
,
77 const FileOperationCallback
& callback
) override
;
78 virtual void TruncateFile(const base::FilePath
& file_path
,
80 const FileOperationCallback
& callback
) override
;
81 virtual void Pin(const base::FilePath
& file_path
,
82 const FileOperationCallback
& callback
) override
;
83 virtual void Unpin(const base::FilePath
& file_path
,
84 const FileOperationCallback
& callback
) override
;
85 virtual void GetFile(const base::FilePath
& file_path
,
86 const GetFileCallback
& callback
) override
;
87 virtual void GetFileForSaving(const base::FilePath
& file_path
,
88 const GetFileCallback
& callback
) override
;
89 virtual base::Closure
GetFileContent(
90 const base::FilePath
& file_path
,
91 const GetFileContentInitializedCallback
& initialized_callback
,
92 const google_apis::GetContentCallback
& get_content_callback
,
93 const FileOperationCallback
& completion_callback
) override
;
94 virtual void GetResourceEntry(
95 const base::FilePath
& file_path
,
96 const GetResourceEntryCallback
& callback
) override
;
97 virtual void ReadDirectory(
98 const base::FilePath
& file_path
,
99 const ReadDirectoryEntriesCallback
& entries_callback
,
100 const FileOperationCallback
& completion_callback
) override
;
101 virtual void Search(const std::string
& search_query
,
102 const GURL
& next_link
,
103 const SearchCallback
& callback
) override
;
104 virtual void SearchMetadata(const std::string
& query
,
106 int at_most_num_matches
,
107 const SearchMetadataCallback
& callback
) override
;
108 virtual void GetAvailableSpace(
109 const GetAvailableSpaceCallback
& callback
) override
;
110 virtual void GetShareUrl(
111 const base::FilePath
& file_path
,
112 const GURL
& embed_origin
,
113 const GetShareUrlCallback
& callback
) override
;
114 virtual void GetMetadata(
115 const GetFilesystemMetadataCallback
& callback
) override
;
116 virtual void MarkCacheFileAsMounted(
117 const base::FilePath
& drive_file_path
,
118 const MarkMountedCallback
& callback
) override
;
119 virtual void MarkCacheFileAsUnmounted(
120 const base::FilePath
& cache_file_path
,
121 const FileOperationCallback
& callback
) override
;
122 virtual void AddPermission(const base::FilePath
& drive_file_path
,
123 const std::string
& email
,
124 google_apis::drive::PermissionRole role
,
125 const FileOperationCallback
& callback
) override
;
126 virtual void Reset(const FileOperationCallback
& callback
) override
;
127 virtual void GetPathFromResourceId(const std::string
& resource_id
,
128 const GetFilePathCallback
& callback
)
132 // Helpers of GetFileContent.
133 // How the method works:
134 // 1) Gets ResourceEntry of the path.
135 // 2) Look at if there is a cache file or not. If found return it.
136 // 3) Otherwise start DownloadFile.
137 // 4) Runs the |completion_callback| upon the download completion.
138 void GetFileContentAfterGetResourceEntry(
139 const GetFileContentInitializedCallback
& initialized_callback
,
140 const google_apis::GetContentCallback
& get_content_callback
,
141 const FileOperationCallback
& completion_callback
,
143 scoped_ptr
<ResourceEntry
> entry
);
144 void GetFileContentAfterGetFileResource(
145 const GetFileContentInitializedCallback
& initialized_callback
,
146 const google_apis::GetContentCallback
& get_content_callback
,
147 const FileOperationCallback
& completion_callback
,
148 google_apis::GDataErrorCode gdata_error
,
149 scoped_ptr
<google_apis::FileResource
> gdata_entry
);
150 void GetFileContentAfterDownloadFile(
151 const FileOperationCallback
& completion_callback
,
152 google_apis::GDataErrorCode gdata_error
,
153 const base::FilePath
& temp_file
);
155 // Helpers of GetResourceEntry.
156 // How the method works:
157 // 1) If the path is root, gets AboutResrouce from the drive service
158 // and create ResourceEntry.
159 // 2-1) Otherwise, gets the parent's ResourceEntry by recursive call.
160 // 2-2) Then, gets the resource list by restricting the parent with its id.
161 // 2-3) Search the results based on title, and return the ResourceEntry.
162 // Note that adding suffix (e.g. " (2)") for files sharing a same name is
163 // not supported in FakeFileSystem. Thus, even if the server has
164 // files sharing the same name under a directory, the second (or later)
165 // file cannot be taken with the suffixed name.
166 void GetResourceEntryAfterGetAboutResource(
167 const GetResourceEntryCallback
& callback
,
168 google_apis::GDataErrorCode gdata_error
,
169 scoped_ptr
<google_apis::AboutResource
> about_resource
);
170 void GetResourceEntryAfterGetParentEntryInfo(
171 const base::FilePath
& base_name
,
172 const GetResourceEntryCallback
& callback
,
174 scoped_ptr
<ResourceEntry
> parent_entry
);
175 void GetResourceEntryAfterGetFileList(
176 const base::FilePath
& base_name
,
177 const GetResourceEntryCallback
& callback
,
178 google_apis::GDataErrorCode gdata_error
,
179 scoped_ptr
<google_apis::FileList
> file_list
);
181 DriveServiceInterface
* drive_service_
; // Not owned.
182 base::ScopedTempDir cache_dir_
;
184 // Note: This should remain the last member so it'll be destroyed and
185 // invalidate the weak pointers before any other members are destroyed.
186 base::WeakPtrFactory
<FakeFileSystem
> weak_ptr_factory_
;
188 DISALLOW_COPY_AND_ASSIGN(FakeFileSystem
);
191 } // namespace test_util
194 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_