Home | History | Annotate | Download | only in image_writer_private
      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_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_
      7 
      8 #include "base/files/file_util.h"
      9 #include "base/files/scoped_temp_dir.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "base/run_loop.h"
     12 #include "chrome/browser/extensions/api/image_writer_private/image_writer_utility_client.h"
     13 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
     14 #include "content/public/test/test_browser_thread_bundle.h"
     15 #include "content/public/test/test_utils.h"
     16 #include "testing/gmock/include/gmock/gmock.h"
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 
     19 #if defined(OS_CHROMEOS)
     20 #include "chromeos/disks/disk_mount_manager.h"
     21 #include "chromeos/disks/mock_disk_mount_manager.h"
     22 #endif
     23 
     24 namespace extensions {
     25 namespace image_writer {
     26 
     27 const char kDummyExtensionId[] = "DummyExtension";
     28 
     29 // Default file size to use in tests.  Currently 32kB.
     30 const int kTestFileSize = 32 * 1024;
     31 // Pattern to use in the image file.
     32 const int kImagePattern = 0x55555555; // 01010101
     33 // Pattern to use in the device file.
     34 const int kDevicePattern = 0xAAAAAAAA; // 10101010
     35 
     36 // A mock around the operation manager for tracking callbacks.  Note that there
     37 // are non-virtual methods on this class that should not be called in tests.
     38 class MockOperationManager : public OperationManager {
     39  public:
     40   MockOperationManager();
     41   explicit MockOperationManager(content::BrowserContext* context);
     42   virtual ~MockOperationManager();
     43 
     44   MOCK_METHOD3(OnProgress, void(const ExtensionId& extension_id,
     45                                 image_writer_api::Stage stage,
     46                                 int progress));
     47   // Callback for completion events.
     48   MOCK_METHOD1(OnComplete, void(const std::string& extension_id));
     49 
     50   // Callback for error events.
     51   MOCK_METHOD4(OnError, void(const ExtensionId& extension_id,
     52                              image_writer_api::Stage stage,
     53                              int progress,
     54                              const std::string& error_message));
     55 };
     56 
     57 #if defined(OS_CHROMEOS)
     58 // A fake for the DiskMountManager that will successfully call the unmount
     59 // callback.
     60 class FakeDiskMountManager : public chromeos::disks::MockDiskMountManager {
     61  public:
     62   FakeDiskMountManager();
     63   virtual ~FakeDiskMountManager();
     64 
     65   virtual void UnmountDeviceRecursively(
     66       const std::string& device_path,
     67       const UnmountDeviceRecursivelyCallbackType& callback) OVERRIDE;
     68 
     69  private:
     70   DiskMap disks_;
     71 };
     72 #endif
     73 
     74 class FakeImageWriterClient : public ImageWriterUtilityClient {
     75  public:
     76   FakeImageWriterClient();
     77 
     78   virtual void Write(const ProgressCallback& progress_callback,
     79                      const SuccessCallback& success_callback,
     80                      const ErrorCallback& error_callback,
     81                      const base::FilePath& source,
     82                      const base::FilePath& target) OVERRIDE;
     83 
     84   virtual void Verify(const ProgressCallback& progress_callback,
     85                       const SuccessCallback& success_callback,
     86                       const ErrorCallback& error_callback,
     87                       const base::FilePath& source,
     88                       const base::FilePath& target) OVERRIDE;
     89 
     90   virtual void Cancel(const CancelCallback& cancel_callback) OVERRIDE;
     91 
     92   virtual void Shutdown() OVERRIDE;
     93 
     94   // Sets a callback for when a Write call is made.
     95   void SetWriteCallback(const base::Closure& write_callback);
     96   // Sets a callback for when a Verify call is made.
     97   void SetVerifyCallback(const base::Closure& verify_callback);
     98 
     99   // Triggers the progress callback.
    100   void Progress(int64 progress);
    101   // Triggers the success callback.
    102   void Success();
    103   // Triggers the error callback.
    104   void Error(const std::string& message);
    105   // Triggers the cancel callback.
    106   void Cancel();
    107 
    108  private:
    109   virtual ~FakeImageWriterClient();
    110 
    111   ProgressCallback progress_callback_;
    112   SuccessCallback success_callback_;
    113   ErrorCallback error_callback_;
    114   CancelCallback cancel_callback_;
    115 
    116   base::Closure write_callback_;
    117   base::Closure verify_callback_;
    118 };
    119 
    120 class ImageWriterTestUtils {
    121  public:
    122   ImageWriterTestUtils();
    123   virtual ~ImageWriterTestUtils();
    124 
    125   // Verifies that the data in image_path was written to the file at
    126   // device_path.  This is different from base::ContentsEqual because the device
    127   // may be larger than the image.
    128   bool ImageWrittenToDevice();
    129 
    130   // Fills |file| with |length| bytes of |pattern|, overwriting any existing
    131   // data.
    132   bool FillFile(const base::FilePath& file,
    133                 const int pattern,
    134                 const int length);
    135 
    136   // Set up the test utils, creating temporary folders and such.
    137   // Note that browser tests should use the alternate form and pass "true" as an
    138   // argument.
    139   virtual void SetUp();
    140   // Set up the test utils, creating temporary folders and such.  If
    141   // |is_browser_test| is true then it will use alternate initialization
    142   // appropriate for a browser test.  This should be run in
    143   // |SetUpInProcessBrowserTestFixture|.
    144   virtual void SetUp(bool is_browser_test);
    145 
    146   virtual void TearDown();
    147 
    148   const base::FilePath& GetTempDir();
    149   const base::FilePath& GetImagePath();
    150   const base::FilePath& GetDevicePath();
    151 
    152 #if !defined(OS_CHROMEOS)
    153   FakeImageWriterClient* GetUtilityClient();
    154 #endif
    155 
    156  protected:
    157   base::ScopedTempDir temp_dir_;
    158   base::FilePath test_image_path_;
    159   base::FilePath test_device_path_;
    160 
    161 #if !defined(OS_CHROMEOS)
    162   scoped_refptr<FakeImageWriterClient> client_;
    163 #endif
    164 };
    165 
    166 // Base class for unit tests that manages creating image and device files.
    167 class ImageWriterUnitTestBase : public testing::Test {
    168  protected:
    169   ImageWriterUnitTestBase();
    170   virtual ~ImageWriterUnitTestBase();
    171 
    172   virtual void SetUp() OVERRIDE;
    173   virtual void TearDown() OVERRIDE;
    174 
    175   ImageWriterTestUtils test_utils_;
    176 
    177  private:
    178   content::TestBrowserThreadBundle thread_bundle_;
    179 };
    180 
    181 }  // namespace image_writer
    182 }  // namespace extensions
    183 
    184 #endif  // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_
    185