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