Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / image_writer_private / test_utils.h
blob202f8df84de0596b6676ae7124748bb4c803b9a5
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_
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"
19 #if defined(OS_CHROMEOS)
20 #include "chromeos/disks/disk_mount_manager.h"
21 #include "chromeos/disks/mock_disk_mount_manager.h"
22 #endif
24 namespace extensions {
25 namespace image_writer {
27 const char kDummyExtensionId[] = "DummyExtension";
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
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();
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));
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));
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 ~FakeDiskMountManager() override;
65 void UnmountDeviceRecursively(
66 const std::string& device_path,
67 const UnmountDeviceRecursivelyCallbackType& callback) override;
69 private:
70 DiskMap disks_;
72 #endif
74 class FakeImageWriterClient : public ImageWriterUtilityClient {
75 public:
76 FakeImageWriterClient();
78 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;
84 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;
90 void Cancel(const CancelCallback& cancel_callback) override;
92 void Shutdown() override;
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);
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();
108 private:
109 ~FakeImageWriterClient() override;
111 ProgressCallback progress_callback_;
112 SuccessCallback success_callback_;
113 ErrorCallback error_callback_;
114 CancelCallback cancel_callback_;
116 base::Closure write_callback_;
117 base::Closure verify_callback_;
120 class ImageWriterTestUtils {
121 public:
122 ImageWriterTestUtils();
123 virtual ~ImageWriterTestUtils();
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();
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);
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);
146 virtual void TearDown();
148 const base::FilePath& GetTempDir();
149 const base::FilePath& GetImagePath();
150 const base::FilePath& GetDevicePath();
152 #if !defined(OS_CHROMEOS)
153 FakeImageWriterClient* GetUtilityClient();
154 #endif
156 protected:
157 base::ScopedTempDir temp_dir_;
158 base::FilePath test_image_path_;
159 base::FilePath test_device_path_;
161 #if !defined(OS_CHROMEOS)
162 scoped_refptr<FakeImageWriterClient> client_;
163 #endif
166 // Base class for unit tests that manages creating image and device files.
167 class ImageWriterUnitTestBase : public testing::Test {
168 protected:
169 ImageWriterUnitTestBase();
170 ~ImageWriterUnitTestBase() override;
172 void SetUp() override;
173 void TearDown() override;
175 ImageWriterTestUtils test_utils_;
177 private:
178 content::TestBrowserThreadBundle thread_bundle_;
181 } // namespace image_writer
182 } // namespace extensions
184 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_