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 "google_apis/drive/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  // FileSystemInterface Overrides.
46  virtual void AddObserver(FileSystemObserver* observer) OVERRIDE;
47  virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE;
48  virtual void CheckForUpdates() OVERRIDE;
49  virtual void TransferFileFromLocalToRemote(
50      const base::FilePath& local_src_file_path,
51      const base::FilePath& remote_dest_file_path,
52      const FileOperationCallback& callback) OVERRIDE;
53  virtual void OpenFile(const base::FilePath& file_path,
54                        OpenMode open_mode,
55                        const std::string& mime_type,
56                        const OpenFileCallback& callback) OVERRIDE;
57  virtual void Copy(const base::FilePath& src_file_path,
58                    const base::FilePath& dest_file_path,
59                    bool preserve_last_modified,
60                    const FileOperationCallback& callback) OVERRIDE;
61  virtual void Move(const base::FilePath& src_file_path,
62                    const base::FilePath& dest_file_path,
63                    bool preserve_last_modified,
64                    const FileOperationCallback& callback) OVERRIDE;
65  virtual void Remove(const base::FilePath& file_path,
66                      bool is_recursive,
67                      const FileOperationCallback& callback) OVERRIDE;
68  virtual void CreateDirectory(const base::FilePath& directory_path,
69                               bool is_exclusive,
70                               bool is_recursive,
71                               const FileOperationCallback& callback) OVERRIDE;
72  virtual void CreateFile(const base::FilePath& file_path,
73                          bool is_exclusive,
74                          const std::string& mime_type,
75                          const FileOperationCallback& callback) OVERRIDE;
76  virtual void TouchFile(const base::FilePath& file_path,
77                         const base::Time& last_access_time,
78                         const base::Time& last_modified_time,
79                         const FileOperationCallback& callback) OVERRIDE;
80  virtual void TruncateFile(const base::FilePath& file_path,
81                            int64 length,
82                            const FileOperationCallback& callback) OVERRIDE;
83  virtual void Pin(const base::FilePath& file_path,
84                   const FileOperationCallback& callback) OVERRIDE;
85  virtual void Unpin(const base::FilePath& file_path,
86                     const FileOperationCallback& callback) OVERRIDE;
87  virtual void GetFile(const base::FilePath& file_path,
88                       const GetFileCallback& callback) OVERRIDE;
89  virtual void GetFileForSaving(const base::FilePath& file_path,
90                                const GetFileCallback& callback) OVERRIDE;
91  virtual void GetFileContent(
92      const base::FilePath& file_path,
93      const GetFileContentInitializedCallback& initialized_callback,
94      const google_apis::GetContentCallback& get_content_callback,
95      const FileOperationCallback& completion_callback) OVERRIDE;
96  virtual void GetResourceEntry(
97      const base::FilePath& file_path,
98      const GetResourceEntryCallback& callback) OVERRIDE;
99  virtual void ReadDirectory(
100      const base::FilePath& file_path,
101      const ReadDirectoryCallback& callback) OVERRIDE;
102  virtual void Search(const std::string& search_query,
103                      const GURL& next_link,
104                      const SearchCallback& callback) OVERRIDE;
105  virtual void SearchMetadata(const std::string& query,
106                              int options,
107                              int at_most_num_matches,
108                              const SearchMetadataCallback& callback) OVERRIDE;
109  virtual void GetAvailableSpace(
110      const GetAvailableSpaceCallback& callback) OVERRIDE;
111  virtual void GetShareUrl(
112      const base::FilePath& file_path,
113      const GURL& embed_origin,
114      const GetShareUrlCallback& callback) OVERRIDE;
115  virtual void GetMetadata(
116      const GetFilesystemMetadataCallback& callback) OVERRIDE;
117  virtual void MarkCacheFileAsMounted(
118      const base::FilePath& drive_file_path,
119      const MarkMountedCallback& callback) OVERRIDE;
120  virtual void MarkCacheFileAsUnmounted(
121      const base::FilePath& cache_file_path,
122      const FileOperationCallback& callback) OVERRIDE;
123  virtual void GetCacheEntry(
124      const base::FilePath& drive_file_path,
125      const GetCacheEntryCallback& callback) OVERRIDE;
126  virtual void Reload(const FileOperationCallback& callback) OVERRIDE;
127
128 private:
129  // Helper of GetResourceEntryById.
130  void GetResourceEntryByIdAfterGetResourceEntry(
131      const GetResourceEntryCallback& callback,
132      google_apis::GDataErrorCode error_in,
133      scoped_ptr<google_apis::ResourceEntry> resource_entry);
134
135  // Helpers of GetFileContent.
136  // How the method works:
137  // 1) Gets ResourceEntry of the path.
138  // 2) Look at if there is a cache file or not. If found return it.
139  // 3) Otherwise start DownloadFile.
140  // 4) Runs the |completion_callback| upon the download completion.
141  void GetFileContentAfterGetResourceEntry(
142      const GetFileContentInitializedCallback& initialized_callback,
143      const google_apis::GetContentCallback& get_content_callback,
144      const FileOperationCallback& completion_callback,
145      FileError error,
146      scoped_ptr<ResourceEntry> entry);
147  void GetFileContentAfterGetWapiResourceEntry(
148      const GetFileContentInitializedCallback& initialized_callback,
149      const google_apis::GetContentCallback& get_content_callback,
150      const FileOperationCallback& completion_callback,
151      google_apis::GDataErrorCode gdata_error,
152      scoped_ptr<google_apis::ResourceEntry> gdata_entry);
153  void GetFileContentAfterDownloadFile(
154      const FileOperationCallback& completion_callback,
155      google_apis::GDataErrorCode gdata_error,
156      const base::FilePath& temp_file);
157
158  // Helpers of GetResourceEntry.
159  // How the method works:
160  // 1) If the path is root, gets AboutResrouce from the drive service
161  //    and create ResourceEntry.
162  // 2-1) Otherwise, gets the parent's ResourceEntry by recursive call.
163  // 2-2) Then, gets the resource list by restricting the parent with its id.
164  // 2-3) Search the results based on title, and return the ResourceEntry.
165  // Note that adding suffix (e.g. " (2)") for files sharing a same name is
166  // not supported in FakeFileSystem. Thus, even if the server has
167  // files sharing the same name under a directory, the second (or later)
168  // file cannot be taken with the suffixed name.
169  void GetResourceEntryAfterGetAboutResource(
170      const GetResourceEntryCallback& callback,
171      google_apis::GDataErrorCode gdata_error,
172      scoped_ptr<google_apis::AboutResource> about_resource);
173  void GetResourceEntryAfterGetParentEntryInfo(
174      const base::FilePath& base_name,
175      const GetResourceEntryCallback& callback,
176      FileError error,
177      scoped_ptr<ResourceEntry> parent_entry);
178  void GetResourceEntryAfterGetResourceList(
179      const base::FilePath& base_name,
180      const GetResourceEntryCallback& callback,
181      google_apis::GDataErrorCode gdata_error,
182      scoped_ptr<google_apis::ResourceList> resource_list);
183
184  DriveServiceInterface* drive_service_;  // Not owned.
185  base::ScopedTempDir cache_dir_;
186
187  // Note: This should remain the last member so it'll be destroyed and
188  // invalidate the weak pointers before any other members are destroyed.
189  base::WeakPtrFactory<FakeFileSystem> weak_ptr_factory_;
190
191  DISALLOW_COPY_AND_ASSIGN(FakeFileSystem);
192};
193
194}  // namespace test_util
195}  // namespace drive
196
197#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_
198