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 << 13; // 8 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 base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler); |
| 66 |
| 67 scoped_refptr<ImageWriter> image_writer( |
| 68 new ImageWriter(mock_handler_factory.GetWeakPtr())); |
| 69 |
| 70 EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber()); |
| 71 EXPECT_CALL(mock_handler, SendProgress(kTestFileSize)).Times(1); |
| 72 EXPECT_CALL(mock_handler, SendProgress(0)).Times(1); |
| 73 EXPECT_CALL(mock_handler, SendSucceeded()).Times(1); |
| 74 EXPECT_CALL(mock_handler, SendFailed(_)).Times(0); |
| 75 |
| 76 FillFile(image_path_); |
| 77 image_writer->Write(image_path_, device_path_); |
| 78 base::RunLoop().RunUntilIdle(); |
| 79 } |
| 80 |
| 81 TEST_F(ImageWriterUtilityTest, WriteInvalidImageFile) { |
| 82 MockHandler mock_handler; |
| 83 base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler); |
| 84 scoped_refptr<ImageWriter> image_writer( |
| 85 new ImageWriter(mock_handler_factory.GetWeakPtr())); |
| 86 |
| 87 EXPECT_CALL(mock_handler, SendProgress(_)).Times(0); |
| 88 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 89 EXPECT_CALL(mock_handler, SendFailed(error::kOpenImage)).Times(1); |
| 90 |
| 91 base::DeleteFile(image_path_, false); |
| 92 image_writer->Write(image_path_, device_path_); |
| 93 base::RunLoop().RunUntilIdle(); |
| 94 } |
| 95 |
| 96 TEST_F(ImageWriterUtilityTest, WriteInvalidDeviceFile) { |
| 97 MockHandler mock_handler; |
| 98 base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler); |
| 99 scoped_refptr<ImageWriter> image_writer( |
| 100 new ImageWriter(mock_handler_factory.GetWeakPtr())); |
| 101 |
| 102 EXPECT_CALL(mock_handler, SendProgress(_)).Times(0); |
| 103 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 104 EXPECT_CALL(mock_handler, SendFailed(error::kOpenDevice)).Times(1); |
| 105 |
| 106 base::DeleteFile(device_path_, false); |
| 107 image_writer->Write(image_path_, device_path_); |
| 108 base::RunLoop().RunUntilIdle(); |
| 109 } |
| 110 |
| 111 TEST_F(ImageWriterUtilityTest, VerifySuccessful) { |
| 112 MockHandler mock_handler; |
| 113 base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler); |
| 114 scoped_refptr<ImageWriter> image_writer( |
| 115 new ImageWriter(mock_handler_factory.GetWeakPtr())); |
| 116 |
| 117 EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber()); |
| 118 EXPECT_CALL(mock_handler, SendProgress(kTestFileSize)).Times(1); |
| 119 EXPECT_CALL(mock_handler, SendProgress(0)).Times(1); |
| 120 EXPECT_CALL(mock_handler, SendSucceeded()).Times(1); |
| 121 EXPECT_CALL(mock_handler, SendFailed(_)).Times(0); |
| 122 |
| 123 FillFile(image_path_); |
| 124 FillFile(device_path_); |
| 125 |
| 126 image_writer->Verify(image_path_, device_path_); |
| 127 |
| 128 base::RunLoop().RunUntilIdle(); |
| 129 } |
| 130 |
| 131 TEST_F(ImageWriterUtilityTest, VerifyInvalidImageFile) { |
| 132 MockHandler mock_handler; |
| 133 base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler); |
| 134 scoped_refptr<ImageWriter> image_writer( |
| 135 new ImageWriter(mock_handler_factory.GetWeakPtr())); |
| 136 |
| 137 EXPECT_CALL(mock_handler, SendProgress(_)).Times(0); |
| 138 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 139 EXPECT_CALL(mock_handler, SendFailed(error::kOpenImage)).Times(1); |
| 140 |
| 141 base::DeleteFile(image_path_, false); |
| 142 |
| 143 image_writer->Verify(image_path_, device_path_); |
| 144 |
| 145 base::RunLoop().RunUntilIdle(); |
| 146 } |
| 147 |
| 148 TEST_F(ImageWriterUtilityTest, VerifyInvalidDeviceFile) { |
| 149 MockHandler mock_handler; |
| 150 base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler); |
| 151 scoped_refptr<ImageWriter> image_writer( |
| 152 new ImageWriter(mock_handler_factory.GetWeakPtr())); |
| 153 |
| 154 EXPECT_CALL(mock_handler, SendProgress(_)).Times(0); |
| 155 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 156 EXPECT_CALL(mock_handler, SendFailed(error::kOpenDevice)).Times(1); |
| 157 |
| 158 base::DeleteFile(device_path_, false); |
| 159 |
| 160 image_writer->Verify(image_path_, device_path_); |
| 161 |
| 162 base::RunLoop().RunUntilIdle(); |
| 163 } |
| 164 |
| 165 TEST_F(ImageWriterUtilityTest, VerifyFailed) { |
| 166 MockHandler mock_handler; |
| 167 base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler); |
| 168 scoped_refptr<ImageWriter> image_writer( |
| 169 new ImageWriter(mock_handler_factory.GetWeakPtr())); |
| 170 |
| 171 EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber()); |
| 172 EXPECT_CALL(mock_handler, SendSucceeded()).Times(0); |
| 173 EXPECT_CALL(mock_handler, SendFailed(error::kVerificationFailed)).Times(1); |
| 174 |
| 175 FillFile(image_path_); |
| 176 image_writer->Verify(image_path_, device_path_); |
| 177 |
| 178 base::RunLoop().RunUntilIdle(); |
| 179 } |
| 180 |
| 181 TEST_F(ImageWriterUtilityTest, WriteThenVerify) { |
| 182 MockHandler mock_handler; |
| 183 base::WeakPtrFactory<ImageWriterHandler> mock_handler_factory(&mock_handler); |
| 184 scoped_refptr<ImageWriter> image_writer( |
| 185 new ImageWriter(mock_handler_factory.GetWeakPtr())); |
| 186 |
| 187 EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber()); |
| 188 EXPECT_CALL(mock_handler, SendSucceeded()).Times(2); |
| 189 EXPECT_CALL(mock_handler, SendFailed(_)).Times(0); |
| 190 |
| 191 image_writer->Write(image_path_, device_path_); |
| 192 |
| 193 base::RunLoop().RunUntilIdle(); |
| 194 |
| 195 image_writer->Verify(image_path_, device_path_); |
| 196 |
| 197 base::RunLoop().RunUntilIdle(); |
| 198 } |
| 199 |
| 200 } // namespace image_writer |
| 201 } // namespace chrome |
OLD | NEW |