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"
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
{
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
,
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
,
54 const std::string
& error_message
));
57 #if defined(OS_CHROMEOS)
58 // A fake for the DiskMountManager that will successfully call the unmount
60 class FakeDiskMountManager
: public chromeos::disks::MockDiskMountManager
{
62 FakeDiskMountManager();
63 virtual ~FakeDiskMountManager();
65 virtual void UnmountDeviceRecursively(
66 const std::string
& device_path
,
67 const UnmountDeviceRecursivelyCallbackType
& callback
) override
;
74 class FakeImageWriterClient
: public ImageWriterUtilityClient
{
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.
103 // Triggers the error callback.
104 void Error(const std::string
& message
);
105 // Triggers the cancel callback.
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
{
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
132 bool FillFile(const base::FilePath
& file
,
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
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();
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_
;
166 // Base class for unit tests that manages creating image and device files.
167 class ImageWriterUnitTestBase
: public testing::Test
{
169 ImageWriterUnitTestBase();
170 ~ImageWriterUnitTestBase() override
;
172 void SetUp() override
;
173 void TearDown() override
;
175 ImageWriterTestUtils test_utils_
;
178 content::TestBrowserThreadBundle thread_bundle_
;
181 } // namespace image_writer
182 } // namespace extensions
184 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_