OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "chrome/utility/image_writer/error_messages.h" |
| 9 #include "chrome/utility/image_writer/image_writer.h" |
| 10 #include "chrome/utility/image_writer/image_writer_handler.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace chrome { |
| 15 namespace image_writer { |
| 16 |
| 17 using testing::_; |
| 18 using testing::AnyNumber; |
| 19 using testing::AtLeast; |
| 20 using testing::Lt; |
| 21 |
| 22 namespace { |
| 23 |
| 24 const int64 kTestFileSize = 1 << 15; // 32 kB |
| 25 const int kTestPattern = 0x55555555; |
| 26 |
| 27 class ImageWriterUtilityTest : public testing::Test { |
| 28 protected: |
| 29 virtual void SetUp() OVERRIDE { |
| 30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 31 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &image_path_)); |
| 32 ASSERT_TRUE( |
| 33 base::CreateTemporaryFileInDir(temp_dir_.path(), &device_path_)); |
| 34 } |
| 35 |
| 36 virtual void TearDown() OVERRIDE {} |
| 37 |
| 38 void FillFile(const base::FilePath& path) { |
| 39 scoped_ptr<char[]> buffer(new char[kTestFileSize]); |
| 40 memset(buffer.get(), kTestPattern, kTestFileSize); |
| 41 |
| 42 file_util::WriteFile(path, buffer.get(), kTestFileSize); |
| 43 } |
| 44 |
| 45 base::FilePath image_path_; |
| 46 base::FilePath device_path_; |
| 47 |
| 48 private: |
| 49 base::MessageLoop message_loop_; |
| 50 base::ScopedTempDir temp_dir_; |
| 51 }; |
| 52 |
| 53 class MockHandler : public ImageWriterHandler { |
| 54 public: |
| 55 MOCK_METHOD1(SendProgress, void(int64)); |
| 56 MOCK_METHOD1(SendFailed, void(const std::string& message)); |
| 57 MOCK_METHOD0(SendSucceeded, void()); |
| 58 MOCK_METHOD1(OnMessageReceived, bool(const IPC::Message& message)); |
| 59 }; |
| 60 |
| 61 } // namespace |
| 62 |
| 63 TEST_F(ImageWriterUtilityTest, WriteSuccessful) { |
| 64 MockHandler mock_handler; |
| 65 ImageWriter image_writer(&mock_handler); |
| 66 |
| 67 EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber()); |
| 68 EXPECT_CALL(mock_handler, SendProgress(kTestFileSize)).Times(1); |
| 69 EXPECT_CALL(mock_handler, SendProgress(0)).Times(1); |
| 70 EXPECT_CALL(mock_handler, SendSucceeded()).Times(1); |
| 71 EXPECT_CALL(mock_handler, SendFailed(_)).Times(0); |
| 72 |
| 73 FillFile(image_path_); |
| 74 image_writer.Write(image_path_, device_path_); |
| 75 base::RunLoop().RunUntilIdle(); |
| 76 } |
| 77 |
| 78 TEST_F(ImageWriterUtilityTest, WriteInvalidImageFile) { |
| 79 MockHandler mock_handler; |
| 80 ImageWriter image_writer(&mock_handler); |
| 81 |
| 82 EXPECT_CALL(mock_handler, SendProgress(_)).Times(0); |
| 83 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 84 EXPECT_CALL(mock_handler, SendFailed(error::kOpenImage)).Times(1); |
| 85 |
| 86 base::DeleteFile(image_path_, false); |
| 87 image_writer.Write(image_path_, device_path_); |
| 88 base::RunLoop().RunUntilIdle(); |
| 89 } |
| 90 |
| 91 TEST_F(ImageWriterUtilityTest, WriteInvalidDeviceFile) { |
| 92 MockHandler mock_handler; |
| 93 ImageWriter image_writer(&mock_handler); |
| 94 |
| 95 EXPECT_CALL(mock_handler, SendProgress(_)).Times(0); |
| 96 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 97 EXPECT_CALL(mock_handler, SendFailed(error::kOpenDevice)).Times(1); |
| 98 |
| 99 base::DeleteFile(device_path_, false); |
| 100 image_writer.Write(image_path_, device_path_); |
| 101 base::RunLoop().RunUntilIdle(); |
| 102 } |
| 103 |
| 104 TEST_F(ImageWriterUtilityTest, VerifySuccessful) { |
| 105 MockHandler mock_handler; |
| 106 ImageWriter image_writer(&mock_handler); |
| 107 |
| 108 EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber()); |
| 109 EXPECT_CALL(mock_handler, SendProgress(kTestFileSize)).Times(1); |
| 110 EXPECT_CALL(mock_handler, SendProgress(0)).Times(1); |
| 111 EXPECT_CALL(mock_handler, SendSucceeded()).Times(1); |
| 112 EXPECT_CALL(mock_handler, SendFailed(_)).Times(0); |
| 113 |
| 114 FillFile(image_path_); |
| 115 FillFile(device_path_); |
| 116 |
| 117 image_writer.Verify(image_path_, device_path_); |
| 118 |
| 119 base::RunLoop().RunUntilIdle(); |
| 120 } |
| 121 |
| 122 TEST_F(ImageWriterUtilityTest, VerifyInvalidImageFile) { |
| 123 MockHandler mock_handler; |
| 124 ImageWriter image_writer(&mock_handler); |
| 125 |
| 126 EXPECT_CALL(mock_handler, SendProgress(_)).Times(0); |
| 127 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 128 EXPECT_CALL(mock_handler, SendFailed(error::kOpenImage)).Times(1); |
| 129 |
| 130 base::DeleteFile(image_path_, false); |
| 131 |
| 132 image_writer.Verify(image_path_, device_path_); |
| 133 |
| 134 base::RunLoop().RunUntilIdle(); |
| 135 } |
| 136 |
| 137 TEST_F(ImageWriterUtilityTest, VerifyInvalidDeviceFile) { |
| 138 MockHandler mock_handler; |
| 139 ImageWriter image_writer(&mock_handler); |
| 140 |
| 141 EXPECT_CALL(mock_handler, SendProgress(_)).Times(0); |
| 142 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 143 EXPECT_CALL(mock_handler, SendFailed(error::kOpenDevice)).Times(1); |
| 144 |
| 145 base::DeleteFile(device_path_, false); |
| 146 |
| 147 image_writer.Verify(image_path_, device_path_); |
| 148 |
| 149 base::RunLoop().RunUntilIdle(); |
| 150 } |
| 151 |
| 152 TEST_F(ImageWriterUtilityTest, VerifyFailed) { |
| 153 MockHandler mock_handler; |
| 154 ImageWriter image_writer(&mock_handler); |
| 155 |
| 156 EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber()); |
| 157 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 158 EXPECT_CALL(mock_handler, SendFailed(error::kVerificationFailed)).Times(1); |
| 159 |
| 160 FillFile(image_path_); |
| 161 image_writer.Verify(image_path_, device_path_); |
| 162 |
| 163 base::RunLoop().RunUntilIdle(); |
| 164 } |
| 165 |
| 166 TEST_F(ImageWriterUtilityTest, WriteThenVerify) { |
| 167 MockHandler mock_handler; |
| 168 ImageWriter image_writer(&mock_handler); |
| 169 |
| 170 EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber()); |
| 171 EXPECT_CALL(mock_handler, SendSucceeded()).Times(2); |
| 172 EXPECT_CALL(mock_handler, SendFailed(_)).Times(0); |
| 173 |
| 174 image_writer.Write(image_path_, device_path_); |
| 175 |
| 176 base::RunLoop().RunUntilIdle(); |
| 177 |
| 178 image_writer.Verify(image_path_, device_path_); |
| 179 |
| 180 base::RunLoop().RunUntilIdle(); |
| 181 } |
| 182 |
| 183 } // namespace image_writer |
| 184 } // namespace chrome |
OLD | NEW |