Index: chrome/utility/image_writer/image_writer_test_utils.cc |
diff --git a/chrome/utility/image_writer/image_writer_test_utils.cc b/chrome/utility/image_writer/image_writer_test_utils.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fae8a64c452e9d3c2d0db748d227a226748cf7c7 |
--- /dev/null |
+++ b/chrome/utility/image_writer/image_writer_test_utils.cc |
@@ -0,0 +1,89 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/file_util.h" |
+#include "base/platform_file.h" |
+#include "chrome/utility/image_writer/image_writer_test_utils.h" |
+ |
+namespace chrome { |
+namespace image_writer { |
+ |
+const int kTestFileSize = 1024 * 1024; // 1 MB |
+const char kTestBytePattern = 0x55; // 10101010 |
+ |
+bool TempFilesForTesting::SetUp() { |
+ return temp_dir_.CreateUniqueTempDir() |
+ && file_util::CreateTemporaryFileInDir(temp_dir_.path(), &image_path_) |
+ && file_util::CreateTemporaryFileInDir(temp_dir_.path(), &device_path_); |
+} |
+ |
+base::FilePath TempFilesForTesting::ImagePath() { |
+ return image_path_; |
+} |
+ |
+base::FilePath TempFilesForTesting::DevicePath() { |
+ return device_path_; |
+} |
+ |
+bool TempFilesForTesting::InitializeImage() { |
+ return InitializeFile(image_path_, kTestFileSize, kTestBytePattern); |
+} |
+ |
+bool TempFilesForTesting::InitializeFile(const base::FilePath& path, |
+ int length, |
+ char pattern) { |
+ char buffer[length]; |
+ memset(buffer, pattern, length); |
+ bool created; |
+ base::PlatformFileError error; |
+ |
+ base::PlatformFile file = base::CreatePlatformFile( |
+ path, |
+ base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, |
+ &created, |
+ &error); |
+ |
+ if (!created || error != base::PLATFORM_FILE_OK) { |
+ DLOG(ERROR) << "Unable to create/truncate temporary file."; |
+ return false; |
+ } |
+ |
+ |
+ int bytes_written = base::WritePlatformFile(file, 0, buffer, length); |
+ |
+ if (bytes_written != length) { |
+ DLOG(ERROR) << "Unable to write all bytes to temporary file." |
+ << " Expected " << length << " but was " << bytes_written; |
+ return false; |
+ } |
+ |
+ if (!base::ClosePlatformFile(file)) { |
+ DLOG(ERROR) << "Unable to close temporary file."; |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+bool TempFilesForTesting::CompareImageAndDevice() { |
+ char image_buffer[kTestFileSize]; |
+ char device_buffer[kTestFileSize]; |
+ |
+ int bytes_read_image = |
+ file_util::ReadFile(image_path_, image_buffer, kTestFileSize); |
+ int bytes_read_device = |
+ file_util::ReadFile(device_path_, device_buffer, kTestFileSize); |
+ |
+ if (bytes_read_image != bytes_read_device) { |
+ DLOG(ERROR) << "Byte counts do not match." |
+ << " Image: " << bytes_read_image |
+ << " Device: " << bytes_read_device; |
+ return false; |
+ } |
+ |
+ return memcmp(image_buffer, device_buffer, kTestFileSize) == 0; |
+} |
+ |
+} // namespace image_writer |
+} // namespace chrome |