Home | History | Annotate | Download | only in file_system
      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_errors.h"
     13 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
     14 #include "chrome/browser/chromeos/drive/test_util.h"
     15 #include "content/public/test/test_browser_thread_bundle.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 class TestingPrefServiceSimple;
     19 
     20 namespace base {
     21 class SequencedTaskRunner;
     22 }  // namespace base
     23 
     24 namespace drive {
     25 
     26 class EventLogger;
     27 class FakeDriveService;
     28 class FakeFreeDiskSpaceGetter;
     29 class JobScheduler;
     30 
     31 namespace internal {
     32 class AboutResourceLoader;
     33 class ChangeListLoader;
     34 class FileCache;
     35 class LoaderController;
     36 class ResourceMetadata;
     37 class ResourceMetadataStorage;
     38 }  // namespace internal
     39 
     40 namespace file_system {
     41 
     42 // Base fixture class for testing Drive file system operations. It sets up the
     43 // basic set of Drive internal classes (ResourceMetadata, Cache, etc) on top of
     44 // FakeDriveService for testing.
     45 class OperationTestBase : public testing::Test {
     46  protected:
     47   // OperationObserver that records all the events.
     48   class LoggingObserver : public OperationObserver {
     49    public:
     50     LoggingObserver();
     51     ~LoggingObserver();
     52 
     53     // OperationObserver overrides.
     54     virtual void OnDirectoryChangedByOperation(
     55         const base::FilePath& path) OVERRIDE;
     56     virtual void OnEntryUpdatedByOperation(
     57         const std::string& local_id) OVERRIDE;
     58     virtual void OnDriveSyncError(DriveSyncErrorType type,
     59                                   const std::string& local_id) OVERRIDE;
     60 
     61     // Gets the set of changed paths.
     62     const std::set<base::FilePath>& get_changed_paths() {
     63       return changed_paths_;
     64     }
     65 
     66     // Gets the set of updated local IDs.
     67     const std::set<std::string>& updated_local_ids() const {
     68       return updated_local_ids_;
     69     }
     70 
     71     // Gets the list of drive sync errors.
     72     const std::vector<DriveSyncErrorType>& drive_sync_errors() const {
     73       return drive_sync_errors_;
     74     }
     75 
     76    private:
     77     std::set<base::FilePath> changed_paths_;
     78     std::set<std::string> updated_local_ids_;
     79     std::vector<DriveSyncErrorType> drive_sync_errors_;
     80   };
     81 
     82   OperationTestBase();
     83   explicit OperationTestBase(int test_thread_bundle_options);
     84   virtual ~OperationTestBase();
     85 
     86   // testing::Test overrides.
     87   virtual void SetUp() OVERRIDE;
     88 
     89   // Returns the path of the temporary directory for putting test files.
     90   base::FilePath temp_dir() const { return temp_dir_.path(); }
     91 
     92   // Synchronously gets the resource entry corresponding to the path from local
     93   // ResourceMetadta.
     94   FileError GetLocalResourceEntry(const base::FilePath& path,
     95                                   ResourceEntry* entry);
     96 
     97   // Synchronously gets the resource entry corresponding to the ID from local
     98   // ResourceMetadta.
     99   FileError GetLocalResourceEntryById(const std::string& local_id,
    100                                       ResourceEntry* entry);
    101 
    102   // Gets the local ID of the entry specified by the path.
    103   std::string GetLocalId(const base::FilePath& path);
    104 
    105   // Synchronously updates |metadata_| by fetching the change feed from the
    106   // |fake_service_|.
    107   FileError CheckForUpdates();
    108 
    109   // Accessors for the components.
    110   FakeDriveService* fake_service() {
    111     return fake_drive_service_.get();
    112   }
    113   EventLogger* logger() { return logger_.get(); }
    114   LoggingObserver* observer() { return &observer_; }
    115   JobScheduler* scheduler() { return scheduler_.get(); }
    116   base::SequencedTaskRunner* blocking_task_runner() {
    117     return blocking_task_runner_.get();
    118   }
    119   FakeFreeDiskSpaceGetter* fake_free_disk_space_getter() {
    120     return fake_free_disk_space_getter_.get();
    121   }
    122   internal::FileCache* cache() { return cache_.get(); }
    123   internal::ResourceMetadata* metadata() { return metadata_.get(); }
    124   internal::LoaderController* loader_controller() {
    125     return loader_controller_.get();
    126   }
    127   internal::ChangeListLoader* change_list_loader() {
    128     return change_list_loader_.get();
    129   }
    130 
    131  private:
    132   content::TestBrowserThreadBundle thread_bundle_;
    133   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
    134   scoped_ptr<TestingPrefServiceSimple> pref_service_;
    135   base::ScopedTempDir temp_dir_;
    136 
    137   LoggingObserver observer_;
    138   scoped_ptr<EventLogger> logger_;
    139   scoped_ptr<FakeDriveService> fake_drive_service_;
    140   scoped_ptr<JobScheduler> scheduler_;
    141   scoped_ptr<internal::ResourceMetadataStorage,
    142              test_util::DestroyHelperForTests> metadata_storage_;
    143   scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_;
    144   scoped_ptr<internal::FileCache, test_util::DestroyHelperForTests> cache_;
    145   scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests>
    146       metadata_;
    147   scoped_ptr<internal::AboutResourceLoader> about_resource_loader_;
    148   scoped_ptr<internal::LoaderController> loader_controller_;
    149   scoped_ptr<internal::ChangeListLoader> change_list_loader_;
    150 };
    151 
    152 }  // namespace file_system
    153 }  // namespace drive
    154 
    155 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPERATION_TEST_BASE_H_
    156