Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / image_writer_private / test_utils.cc
blobeea5d7acbfd1cecd24d98f68e22222ef36e5d9e8
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 #include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
7 #if defined(OS_CHROMEOS)
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/fake_image_burner_client.h"
10 #endif
12 namespace extensions {
13 namespace image_writer {
15 #if defined(OS_CHROMEOS)
16 namespace {
18 class ImageWriterFakeImageBurnerClient
19 : public chromeos::FakeImageBurnerClient {
20 public:
21 ImageWriterFakeImageBurnerClient() {}
22 ~ImageWriterFakeImageBurnerClient() override {}
24 void SetEventHandlers(
25 const BurnFinishedHandler& burn_finished_handler,
26 const BurnProgressUpdateHandler& burn_progress_update_handler) override {
27 burn_finished_handler_ = burn_finished_handler;
28 burn_progress_update_handler_ = burn_progress_update_handler;
31 void BurnImage(const std::string& from_path,
32 const std::string& to_path,
33 const ErrorCallback& error_callback) override {
34 base::MessageLoop::current()->PostTask(FROM_HERE,
35 base::Bind(burn_progress_update_handler_, to_path, 0, 100));
36 base::MessageLoop::current()->PostTask(FROM_HERE,
37 base::Bind(burn_progress_update_handler_, to_path, 50, 100));
38 base::MessageLoop::current()->PostTask(FROM_HERE,
39 base::Bind(burn_progress_update_handler_, to_path, 100, 100));
40 base::MessageLoop::current()->PostTask(FROM_HERE,
41 base::Bind(burn_finished_handler_, to_path, true, ""));
44 private:
45 BurnFinishedHandler burn_finished_handler_;
46 BurnProgressUpdateHandler burn_progress_update_handler_;
49 } // namespace
50 #endif
52 MockOperationManager::MockOperationManager() : OperationManager(NULL) {}
53 MockOperationManager::MockOperationManager(content::BrowserContext* context)
54 : OperationManager(context) {}
55 MockOperationManager::~MockOperationManager() {}
57 #if defined(OS_CHROMEOS)
58 FakeDiskMountManager::FakeDiskMountManager() {}
59 FakeDiskMountManager::~FakeDiskMountManager() {}
61 void FakeDiskMountManager::UnmountDeviceRecursively(
62 const std::string& device_path,
63 const UnmountDeviceRecursivelyCallbackType& callback) {
64 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true));
66 #endif
68 FakeImageWriterClient::FakeImageWriterClient() {}
69 FakeImageWriterClient::~FakeImageWriterClient() {}
71 void FakeImageWriterClient::Write(const ProgressCallback& progress_callback,
72 const SuccessCallback& success_callback,
73 const ErrorCallback& error_callback,
74 const base::FilePath& source,
75 const base::FilePath& target) {
76 progress_callback_ = progress_callback;
77 success_callback_ = success_callback;
78 error_callback_ = error_callback;
80 if (!write_callback_.is_null())
81 write_callback_.Run();
84 void FakeImageWriterClient::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) {
89 progress_callback_ = progress_callback;
90 success_callback_ = success_callback;
91 error_callback_ = error_callback;
93 if (!verify_callback_.is_null())
94 verify_callback_.Run();
97 void FakeImageWriterClient::Cancel(const CancelCallback& cancel_callback) {
98 cancel_callback_ = cancel_callback;
101 void FakeImageWriterClient::Shutdown() {
102 // Clear handlers to not hold any reference to the caller.
103 success_callback_.Reset();
104 progress_callback_.Reset();
105 error_callback_.Reset();
106 cancel_callback_.Reset();
108 write_callback_.Reset();
109 verify_callback_.Reset();
112 void FakeImageWriterClient::SetWriteCallback(
113 const base::Closure& write_callback) {
114 write_callback_ = write_callback;
117 void FakeImageWriterClient::SetVerifyCallback(
118 const base::Closure& verify_callback) {
119 verify_callback_ = verify_callback;
122 void FakeImageWriterClient::Progress(int64 progress) {
123 if (!progress_callback_.is_null())
124 progress_callback_.Run(progress);
127 void FakeImageWriterClient::Success() {
128 if (!success_callback_.is_null())
129 success_callback_.Run();
132 void FakeImageWriterClient::Error(const std::string& message) {
133 if (!error_callback_.is_null())
134 error_callback_.Run(message);
137 void FakeImageWriterClient::Cancel() {
138 if (!cancel_callback_.is_null())
139 cancel_callback_.Run();
142 ImageWriterTestUtils::ImageWriterTestUtils() {
144 ImageWriterTestUtils::~ImageWriterTestUtils() {
147 void ImageWriterTestUtils::SetUp() {
148 SetUp(false);
151 void ImageWriterTestUtils::SetUp(bool is_browser_test) {
152 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
153 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
154 &test_image_path_));
155 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
156 &test_device_path_));
158 ASSERT_TRUE(FillFile(test_image_path_, kImagePattern, kTestFileSize));
159 ASSERT_TRUE(FillFile(test_device_path_, kDevicePattern, kTestFileSize));
161 #if defined(OS_CHROMEOS)
162 if (!chromeos::DBusThreadManager::IsInitialized()) {
163 scoped_ptr<chromeos::DBusThreadManagerSetter> dbus_setter =
164 chromeos::DBusThreadManager::GetSetterForTesting();
165 scoped_ptr<chromeos::ImageBurnerClient>
166 image_burner_fake(new ImageWriterFakeImageBurnerClient());
167 dbus_setter->SetImageBurnerClient(image_burner_fake.Pass());
170 FakeDiskMountManager* disk_manager = new FakeDiskMountManager();
171 chromeos::disks::DiskMountManager::InitializeForTesting(disk_manager);
173 // Adds a disk entry for test_device_path_ with the same device and file path.
174 disk_manager->CreateDiskEntryForMountDevice(
175 chromeos::disks::DiskMountManager::MountPointInfo(
176 test_device_path_.value(),
177 "/dummy/mount",
178 chromeos::MOUNT_TYPE_DEVICE,
179 chromeos::disks::MOUNT_CONDITION_NONE),
180 "device_id",
181 "device_label",
182 "Vendor",
183 "Product",
184 chromeos::DEVICE_TYPE_USB,
185 kTestFileSize,
186 true,
187 true,
188 true,
189 false);
190 disk_manager->SetupDefaultReplies();
191 #else
192 client_ = new FakeImageWriterClient();
193 image_writer::Operation::SetUtilityClientForTesting(client_);
194 #endif
197 void ImageWriterTestUtils::TearDown() {
198 #if defined(OS_CHROMEOS)
199 if (chromeos::DBusThreadManager::IsInitialized()) {
200 chromeos::DBusThreadManager::Shutdown();
202 chromeos::disks::DiskMountManager::Shutdown();
203 #else
204 image_writer::Operation::SetUtilityClientForTesting(NULL);
205 client_->Shutdown();
206 #endif
209 const base::FilePath& ImageWriterTestUtils::GetTempDir() {
210 return temp_dir_.path();
213 const base::FilePath& ImageWriterTestUtils::GetImagePath() {
214 return test_image_path_;
217 const base::FilePath& ImageWriterTestUtils::GetDevicePath() {
218 return test_device_path_;
221 #if !defined(OS_CHROMEOS)
222 FakeImageWriterClient* ImageWriterTestUtils::GetUtilityClient() {
223 return client_.get();
225 #endif
227 bool ImageWriterTestUtils::ImageWrittenToDevice() {
228 scoped_ptr<char[]> image_buffer(new char[kTestFileSize]);
229 scoped_ptr<char[]> device_buffer(new char[kTestFileSize]);
231 int image_bytes_read =
232 ReadFile(test_image_path_, image_buffer.get(), kTestFileSize);
234 if (image_bytes_read < 0)
235 return false;
237 int device_bytes_read =
238 ReadFile(test_device_path_, device_buffer.get(), kTestFileSize);
240 if (image_bytes_read != device_bytes_read)
241 return false;
243 return memcmp(image_buffer.get(), device_buffer.get(), image_bytes_read) == 0;
246 bool ImageWriterTestUtils::FillFile(const base::FilePath& file,
247 const int pattern,
248 const int length) {
249 scoped_ptr<char[]> buffer(new char[length]);
250 memset(buffer.get(), pattern, length);
252 return base::WriteFile(file, buffer.get(), length) == length;
255 ImageWriterUnitTestBase::ImageWriterUnitTestBase()
256 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
258 ImageWriterUnitTestBase::~ImageWriterUnitTestBase() {
261 void ImageWriterUnitTestBase::SetUp() {
262 testing::Test::SetUp();
263 test_utils_.SetUp();
266 void ImageWriterUnitTestBase::TearDown() {
267 testing::Test::TearDown();
268 test_utils_.TearDown();
271 } // namespace image_writer
272 } // namespace extensions