fake_file_system.h revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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 GetFileByPathForSaving(const base::FilePath& file_path,
95                                      const GetFileCallback& callback) OVERRIDE;
96  virtual void GetFileContentByPath(
97      const base::FilePath& file_path,
98      const GetFileContentInitializedCallback& initialized_callback,
99      const google_apis::GetContentCallback& get_content_callback,
100      const FileOperationCallback& completion_callback) OVERRIDE;
101  virtual void GetResourceEntryByPath(
102      const base::FilePath& file_path,
103      const GetResourceEntryCallback& callback) OVERRIDE;
104  virtual void ReadDirectoryByPath(
105      const base::FilePath& file_path,
106      const ReadDirectoryCallback& callback) OVERRIDE;
107  virtual void Search(const std::string& search_query,
108                      const GURL& next_url,
109                      const SearchCallback& callback) OVERRIDE;
110  virtual void SearchMetadata(const std::string& query,
111                              int options,
112                              int at_most_num_matches,
113                              const SearchMetadataCallback& callback) OVERRIDE;
114  virtual void GetAvailableSpace(
115      const GetAvailableSpaceCallback& callback) OVERRIDE;
116  virtual void GetShareUrl(
117      const base::FilePath& file_path,
118      const GURL& embed_origin,
119      const GetShareUrlCallback& callback) OVERRIDE;
120  virtual void GetMetadata(
121      const GetFilesystemMetadataCallback& callback) OVERRIDE;
122  virtual void MarkCacheFileAsMounted(
123      const base::FilePath& drive_file_path,
124      const MarkMountedCallback& callback) OVERRIDE;
125  virtual void MarkCacheFileAsUnmounted(
126      const base::FilePath& cache_file_path,
127      const FileOperationCallback& callback) OVERRIDE;
128  virtual void GetCacheEntryByResourceId(
129      const std::string& resource_id,
130      const GetCacheEntryCallback& callback) OVERRIDE;
131  virtual void Reload() OVERRIDE;
132
133 private:
134  // Helper of GetResourceEntryById.
135  void GetResourceEntryByIdAfterGetResourceEntry(
136      const GetResourceEntryCallback& callback,
137      google_apis::GDataErrorCode error_in,
138      scoped_ptr<google_apis::ResourceEntry> resource_entry);
139
140  // Helpers of GetFileContentByPath.
141  // How the method works:
142  // 1) Gets ResourceEntry of the path.
143  // 2) Look at if there is a cache file or not. If found return it.
144  // 3) Otherwise start DownloadFile.
145  // 4) Runs the |completion_callback| upon the download completion.
146  void GetFileContentByPathAfterGetResourceEntry(
147      const GetFileContentInitializedCallback& initialized_callback,
148      const google_apis::GetContentCallback& get_content_callback,
149      const FileOperationCallback& completion_callback,
150      FileError error,
151      scoped_ptr<ResourceEntry> entry);
152  void GetFileContentByPathAfterGetWapiResourceEntry(
153      const GetFileContentInitializedCallback& initialized_callback,
154      const google_apis::GetContentCallback& get_content_callback,
155      const FileOperationCallback& completion_callback,
156      google_apis::GDataErrorCode gdata_error,
157      scoped_ptr<google_apis::ResourceEntry> gdata_entry);
158  void GetFileContentByPathAfterDownloadFile(
159      const FileOperationCallback& completion_callback,
160      google_apis::GDataErrorCode gdata_error,
161      const base::FilePath& temp_file);
162
163  // Helpers of GetResourceEntryByPath.
164  // How the method works:
165  // 1) If the path is root, gets AboutResrouce from the drive service
166  //    and create ResourceEntry.
167  // 2-1) Otherwise, gets the parent's ResourceEntry by recursive call.
168  // 2-2) Then, gets the resource list by restricting the parent with its id.
169  // 2-3) Search the results based on title, and return the ResourceEntry.
170  // Note that adding suffix (e.g. " (2)") for files sharing a same name is
171  // not supported in FakeFileSystem. Thus, even if the server has
172  // files sharing the same name under a directory, the second (or later)
173  // file cannot be taken with the suffixed name.
174  void GetResourceEntryByPathAfterGetAboutResource(
175      const GetResourceEntryCallback& callback,
176      google_apis::GDataErrorCode gdata_error,
177      scoped_ptr<google_apis::AboutResource> about_resource);
178  void GetResourceEntryByPathAfterGetParentEntryInfo(
179      const base::FilePath& base_name,
180      const GetResourceEntryCallback& callback,
181      FileError error,
182      scoped_ptr<ResourceEntry> parent_entry);
183  void GetResourceEntryByPathAfterGetResourceList(
184      const base::FilePath& base_name,
185      const GetResourceEntryCallback& callback,
186      google_apis::GDataErrorCode gdata_error,
187      scoped_ptr<google_apis::ResourceList> resource_list);
188
189  DriveServiceInterface* drive_service_;  // Not owned.
190  base::ScopedTempDir cache_dir_;
191
192  // Note: This should remain the last member so it'll be destroyed and
193  // invalidate the weak pointers before any other members are destroyed.
194  base::WeakPtrFactory<FakeFileSystem> weak_ptr_factory_;
195
196  DISALLOW_COPY_AND_ASSIGN(FakeFileSystem);
197};
198
199}  // namespace test_util
200}  // namespace drive
201
202#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_
203