Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Unified Diff: chrome/utility/image_writer/image_writer_unittest.cc

Issue 61643015: Adds imageWriterPrivate support for Windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup and now working on Windows with minimal changes. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/utility/image_writer/image_writer_unittest.cc
diff --git a/chrome/utility/image_writer/image_writer_unittest.cc b/chrome/utility/image_writer/image_writer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..044edcf79ab4da489bb93edd2281df0dc58a155f
--- /dev/null
+++ b/chrome/utility/image_writer/image_writer_unittest.cc
@@ -0,0 +1,201 @@
+// Copyright 2013 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/files/scoped_temp_dir.h"
+#include "base/run_loop.h"
+#include "chrome/utility/image_writer/error_messages.h"
+#include "chrome/utility/image_writer/image_writer.h"
+#include "chrome/utility/image_writer/image_writer_handler.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace chrome {
+namespace image_writer {
+
+using testing::_;
+using testing::AnyNumber;
+using testing::AtLeast;
+using testing::Lt;
+
+namespace {
+
+const int64 kTestFileSize = 1 << 13; // 8 kB
+const int kTestPattern = 0x55555555;
+
+class ImageWriterUtilityTest : public testing::Test {
+ protected:
+ virtual void SetUp() OVERRIDE {
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+ ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &image_path_));
+ ASSERT_TRUE(
+ base::CreateTemporaryFileInDir(temp_dir_.path(), &device_path_));
+ }
+
+ virtual void TearDown() OVERRIDE {}
+
+ void FillFile(const base::FilePath& path) {
+ scoped_ptr<char[]> buffer(new char[kTestFileSize]);
+ memset(buffer.get(), kTestPattern, kTestFileSize);
+
+ file_util::WriteFile(path, buffer.get(), kTestFileSize);
+ }
+
+ base::FilePath image_path_;
+ base::FilePath device_path_;
+
+ private:
+ base::MessageLoop message_loop_;
+ base::ScopedTempDir temp_dir_;
+};
+
+class MockHandler : public ImageWriterHandler {
+ public:
+ MOCK_METHOD1(SendProgress, void(int64));
+ MOCK_METHOD1(SendFailed, void(const std::string& message));
+ MOCK_METHOD0(SendSucceeded, void());
+ MOCK_METHOD1(OnMessageReceived, bool(const IPC::Message& message));
+};
+
+} // namespace
+
+TEST_F(ImageWriterUtilityTest, WriteSuccessful) {
+ MockHandler mock_handler;
+ base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler);
+
+ scoped_refptr<ImageWriter> image_writer(
+ new ImageWriter(mock_handler_factory.GetWeakPtr()));
+
+ EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber());
+ EXPECT_CALL(mock_handler, SendProgress(kTestFileSize)).Times(1);
+ EXPECT_CALL(mock_handler, SendProgress(0)).Times(1);
+ EXPECT_CALL(mock_handler, SendSucceeded()).Times(1);
+ EXPECT_CALL(mock_handler, SendFailed(_)).Times(0);
+
+ FillFile(image_path_);
+ image_writer->Write(image_path_, device_path_);
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(ImageWriterUtilityTest, WriteInvalidImageFile) {
+ MockHandler mock_handler;
+ base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler);
+ scoped_refptr<ImageWriter> image_writer(
+ new ImageWriter(mock_handler_factory.GetWeakPtr()));
+
+ EXPECT_CALL(mock_handler, SendProgress(_)).Times(0);
+ EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
+ EXPECT_CALL(mock_handler, SendFailed(error::kOpenImage)).Times(1);
+
+ base::DeleteFile(image_path_, false);
+ image_writer->Write(image_path_, device_path_);
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(ImageWriterUtilityTest, WriteInvalidDeviceFile) {
+ MockHandler mock_handler;
+ base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler);
+ scoped_refptr<ImageWriter> image_writer(
+ new ImageWriter(mock_handler_factory.GetWeakPtr()));
+
+ EXPECT_CALL(mock_handler, SendProgress(_)).Times(0);
+ EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
+ EXPECT_CALL(mock_handler, SendFailed(error::kOpenDevice)).Times(1);
+
+ base::DeleteFile(device_path_, false);
+ image_writer->Write(image_path_, device_path_);
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(ImageWriterUtilityTest, VerifySuccessful) {
+ MockHandler mock_handler;
+ base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler);
+ scoped_refptr<ImageWriter> image_writer(
+ new ImageWriter(mock_handler_factory.GetWeakPtr()));
+
+ EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber());
+ EXPECT_CALL(mock_handler, SendProgress(kTestFileSize)).Times(1);
+ EXPECT_CALL(mock_handler, SendProgress(0)).Times(1);
+ EXPECT_CALL(mock_handler, SendSucceeded()).Times(1);
+ EXPECT_CALL(mock_handler, SendFailed(_)).Times(0);
+
+ FillFile(image_path_);
+ FillFile(device_path_);
+
+ image_writer->Verify(image_path_, device_path_);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(ImageWriterUtilityTest, VerifyInvalidImageFile) {
+ MockHandler mock_handler;
+ base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler);
+ scoped_refptr<ImageWriter> image_writer(
+ new ImageWriter(mock_handler_factory.GetWeakPtr()));
+
+ EXPECT_CALL(mock_handler, SendProgress(_)).Times(0);
+ EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
+ EXPECT_CALL(mock_handler, SendFailed(error::kOpenImage)).Times(1);
+
+ base::DeleteFile(image_path_, false);
+
+ image_writer->Verify(image_path_, device_path_);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(ImageWriterUtilityTest, VerifyInvalidDeviceFile) {
+ MockHandler mock_handler;
+ base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler);
+ scoped_refptr<ImageWriter> image_writer(
+ new ImageWriter(mock_handler_factory.GetWeakPtr()));
+
+ EXPECT_CALL(mock_handler, SendProgress(_)).Times(0);
+ EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
+ EXPECT_CALL(mock_handler, SendFailed(error::kOpenDevice)).Times(1);
+
+ base::DeleteFile(device_path_, false);
+
+ image_writer->Verify(image_path_, device_path_);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(ImageWriterUtilityTest, VerifyFailed) {
+ MockHandler mock_handler;
+ base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler);
+ scoped_refptr<ImageWriter> image_writer(
+ new ImageWriter(mock_handler_factory.GetWeakPtr()));
+
+ EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber());
+ EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
+ EXPECT_CALL(mock_handler, SendFailed(error::kVerificationFailed)).Times(1);
+
+ FillFile(image_path_);
+ image_writer->Verify(image_path_, device_path_);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(ImageWriterUtilityTest, WriteThenVerify) {
+ MockHandler mock_handler;
+ base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler);
+ scoped_refptr<ImageWriter> image_writer(
+ new ImageWriter(mock_handler_factory.GetWeakPtr()));
+
+ EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber());
+ EXPECT_CALL(mock_handler, SendSucceeded()).Times(2);
+ EXPECT_CALL(mock_handler, SendFailed(_)).Times(0);
+
+ image_writer->Write(image_path_, device_path_);
+
+ base::RunLoop().RunUntilIdle();
+
+ image_writer->Verify(image_path_, device_path_);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+} // namespace image_writer
+} // namespace chrome

Powered by Google App Engine
This is Rietveld 408576698