| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
| 10 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 10 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 class ImageWriterOperationTest : public ImageWriterUnitTestBase { | 66 class ImageWriterOperationTest : public ImageWriterUnitTestBase { |
| 67 protected: | 67 protected: |
| 68 ImageWriterOperationTest() | 68 ImageWriterOperationTest() |
| 69 : profile_(new TestingProfile), manager_(profile_.get()) {} | 69 : profile_(new TestingProfile), manager_(profile_.get()) {} |
| 70 virtual void SetUp() OVERRIDE { | 70 virtual void SetUp() OVERRIDE { |
| 71 ImageWriterUnitTestBase::SetUp(); | 71 ImageWriterUnitTestBase::SetUp(); |
| 72 | 72 |
| 73 // Create the zip file. | 73 // Create the zip file. |
| 74 base::FilePath image_dir = test_utils_.GetTempDir().AppendASCII("zip"); | 74 base::FilePath image_dir = temp_dir_.path().AppendASCII("zip"); |
| 75 ASSERT_TRUE(base::CreateDirectory(image_dir)); | 75 ASSERT_TRUE(base::CreateDirectory(image_dir)); |
| 76 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_)); | 76 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_)); |
| 77 | 77 |
| 78 test_utils_.FillFile(image_path_, kImagePattern, kTestFileSize); | 78 FillFile(image_path_, kImagePattern, kTestFileSize); |
| 79 | 79 |
| 80 zip_file_ = test_utils_.GetTempDir().AppendASCII("test_image.zip"); | 80 zip_file_ = temp_dir_.path().AppendASCII("test_image.zip"); |
| 81 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true)); | 81 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true)); |
| 82 | 82 |
| 83 // Operation setup. | 83 // Operation setup. |
| 84 operation_ = | 84 operation_ = new OperationForTest(manager_.AsWeakPtr(), |
| 85 new OperationForTest(manager_.AsWeakPtr(), | 85 kDummyExtensionId, |
| 86 kDummyExtensionId, | 86 test_device_path_.AsUTF8Unsafe()); |
| 87 test_utils_.GetDevicePath().AsUTF8Unsafe()); | 87 client_ = FakeImageWriterClient::Create(); |
| 88 operation_->SetImagePath(test_utils_.GetImagePath()); | 88 operation_->SetImagePath(test_image_path_); |
| 89 } | 89 } |
| 90 | 90 |
| 91 virtual void TearDown() OVERRIDE { | 91 virtual void TearDown() OVERRIDE { |
| 92 // Ensure all callbacks have been destroyed and cleanup occurs. | 92 // Ensure all callbacks have been destroyed and cleanup occurs. |
| 93 client_->Shutdown(); |
| 93 operation_->Cancel(); | 94 operation_->Cancel(); |
| 94 | 95 |
| 95 ImageWriterUnitTestBase::TearDown(); | 96 ImageWriterUnitTestBase::TearDown(); |
| 96 } | 97 } |
| 97 | 98 |
| 98 base::FilePath image_path_; | 99 base::FilePath image_path_; |
| 99 base::FilePath zip_file_; | 100 base::FilePath zip_file_; |
| 100 | 101 |
| 101 scoped_ptr<TestingProfile> profile_; | 102 scoped_ptr<TestingProfile> profile_; |
| 102 | 103 |
| 103 MockOperationManager manager_; | 104 MockOperationManager manager_; |
| 105 scoped_refptr<FakeImageWriterClient> client_; |
| 104 scoped_refptr<OperationForTest> operation_; | 106 scoped_refptr<OperationForTest> operation_; |
| 105 }; | 107 }; |
| 106 | 108 |
| 107 } // namespace | 109 } // namespace |
| 108 | 110 |
| 109 // Unizpping a non-zip should do nothing. | 111 // Unizpping a non-zip should do nothing. |
| 110 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) { | 112 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) { |
| 111 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0); | 113 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0); |
| 112 | 114 |
| 113 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); | 115 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 base::Bind( | 147 base::Bind( |
| 146 &OperationForTest::Unzip, operation_, base::Bind(&base::DoNothing))); | 148 &OperationForTest::Unzip, operation_, base::Bind(&base::DoNothing))); |
| 147 | 149 |
| 148 base::RunLoop().RunUntilIdle(); | 150 base::RunLoop().RunUntilIdle(); |
| 149 | 151 |
| 150 EXPECT_TRUE(base::ContentsEqual(image_path_, operation_->GetImagePath())); | 152 EXPECT_TRUE(base::ContentsEqual(image_path_, operation_->GetImagePath())); |
| 151 } | 153 } |
| 152 | 154 |
| 153 #if defined(OS_LINUX) | 155 #if defined(OS_LINUX) |
| 154 TEST_F(ImageWriterOperationTest, WriteImageToDevice) { | 156 TEST_F(ImageWriterOperationTest, WriteImageToDevice) { |
| 157 #if !defined(OS_CHROMEOS) |
| 158 operation_->SetUtilityClientForTesting(client_); |
| 159 #endif |
| 160 |
| 155 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); | 161 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
| 156 EXPECT_CALL(manager_, | 162 EXPECT_CALL(manager_, |
| 157 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _)) | 163 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _)) |
| 158 .Times(AtLeast(1)); | 164 .Times(AtLeast(1)); |
| 159 EXPECT_CALL(manager_, | 165 EXPECT_CALL(manager_, |
| 160 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0)) | 166 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0)) |
| 161 .Times(AtLeast(1)); | 167 .Times(AtLeast(1)); |
| 162 EXPECT_CALL(manager_, | 168 EXPECT_CALL(manager_, |
| 163 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100)) | 169 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100)) |
| 164 .Times(AtLeast(1)); | 170 .Times(AtLeast(1)); |
| 165 | 171 |
| 166 operation_->Start(); | 172 operation_->Start(); |
| 167 content::BrowserThread::PostTask( | 173 content::BrowserThread::PostTask( |
| 168 content::BrowserThread::FILE, | 174 content::BrowserThread::FILE, |
| 169 FROM_HERE, | 175 FROM_HERE, |
| 170 base::Bind( | 176 base::Bind( |
| 171 &OperationForTest::Write, operation_, base::Bind(&base::DoNothing))); | 177 &OperationForTest::Write, operation_, base::Bind(&base::DoNothing))); |
| 172 | 178 |
| 173 base::RunLoop().RunUntilIdle(); | 179 base::RunLoop().RunUntilIdle(); |
| 174 | 180 |
| 175 #if !defined(OS_CHROMEOS) | 181 #if !defined(OS_CHROMEOS) |
| 176 test_utils_.GetUtilityClient()->Progress(0); | 182 client_->Progress(0); |
| 177 test_utils_.GetUtilityClient()->Progress(kTestFileSize / 2); | 183 client_->Progress(kTestFileSize / 2); |
| 178 test_utils_.GetUtilityClient()->Progress(kTestFileSize); | 184 client_->Progress(kTestFileSize); |
| 179 test_utils_.GetUtilityClient()->Success(); | 185 client_->Success(); |
| 180 | 186 |
| 181 base::RunLoop().RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
| 182 #endif | 188 #endif |
| 183 } | 189 } |
| 184 #endif | 190 #endif |
| 185 | 191 |
| 186 #if !defined(OS_CHROMEOS) | 192 #if !defined(OS_CHROMEOS) |
| 187 // Chrome OS doesn't support verification in the ImageBurner, so these two tests | 193 // Chrome OS doesn't support verification in the ImageBurner, so these two tests |
| 188 // are skipped. | 194 // are skipped. |
| 189 | 195 |
| 190 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) { | 196 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) { |
| 197 operation_->SetUtilityClientForTesting(client_); |
| 198 |
| 191 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); | 199 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
| 192 EXPECT_CALL( | 200 EXPECT_CALL( |
| 193 manager_, | 201 manager_, |
| 194 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) | 202 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) |
| 195 .Times(AtLeast(1)); | 203 .Times(AtLeast(1)); |
| 196 EXPECT_CALL( | 204 EXPECT_CALL( |
| 197 manager_, | 205 manager_, |
| 198 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0)) | 206 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0)) |
| 199 .Times(AtLeast(1)); | 207 .Times(AtLeast(1)); |
| 200 EXPECT_CALL( | 208 EXPECT_CALL( |
| 201 manager_, | 209 manager_, |
| 202 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) | 210 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) |
| 203 .Times(AtLeast(1)); | 211 .Times(AtLeast(1)); |
| 204 | 212 |
| 205 test_utils_.FillFile( | 213 FillFile(test_device_path_, kImagePattern, kTestFileSize); |
| 206 test_utils_.GetDevicePath(), kImagePattern, kTestFileSize); | |
| 207 | 214 |
| 208 operation_->Start(); | 215 operation_->Start(); |
| 209 content::BrowserThread::PostTask(content::BrowserThread::FILE, | 216 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| 210 FROM_HERE, | 217 FROM_HERE, |
| 211 base::Bind(&OperationForTest::VerifyWrite, | 218 base::Bind(&OperationForTest::VerifyWrite, |
| 212 operation_, | 219 operation_, |
| 213 base::Bind(&base::DoNothing))); | 220 base::Bind(&base::DoNothing))); |
| 214 | 221 |
| 215 base::RunLoop().RunUntilIdle(); | 222 base::RunLoop().RunUntilIdle(); |
| 216 | 223 |
| 217 #if !defined(OS_CHROMEOS) | 224 client_->Progress(0); |
| 218 test_utils_.GetUtilityClient()->Progress(0); | 225 client_->Progress(kTestFileSize / 2); |
| 219 test_utils_.GetUtilityClient()->Progress(kTestFileSize / 2); | 226 client_->Progress(kTestFileSize); |
| 220 test_utils_.GetUtilityClient()->Progress(kTestFileSize); | 227 client_->Success(); |
| 221 test_utils_.GetUtilityClient()->Success(); | |
| 222 #endif | |
| 223 | 228 |
| 224 base::RunLoop().RunUntilIdle(); | 229 base::RunLoop().RunUntilIdle(); |
| 225 } | 230 } |
| 226 | 231 |
| 227 TEST_F(ImageWriterOperationTest, VerifyFileFailure) { | 232 TEST_F(ImageWriterOperationTest, VerifyFileFailure) { |
| 233 operation_->SetUtilityClientForTesting(client_); |
| 234 |
| 228 EXPECT_CALL( | 235 EXPECT_CALL( |
| 229 manager_, | 236 manager_, |
| 230 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) | 237 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) |
| 231 .Times(AnyNumber()); | 238 .Times(AnyNumber()); |
| 232 EXPECT_CALL( | 239 EXPECT_CALL( |
| 233 manager_, | 240 manager_, |
| 234 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) | 241 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) |
| 235 .Times(0); | 242 .Times(0); |
| 236 EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0); | 243 EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0); |
| 237 EXPECT_CALL( | 244 EXPECT_CALL( |
| 238 manager_, | 245 manager_, |
| 239 OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _)) | 246 OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _)) |
| 240 .Times(1); | 247 .Times(1); |
| 241 | 248 |
| 242 test_utils_.FillFile( | 249 FillFile(test_device_path_, kDevicePattern, kTestFileSize); |
| 243 test_utils_.GetDevicePath(), kDevicePattern, kTestFileSize); | |
| 244 | 250 |
| 245 operation_->Start(); | 251 operation_->Start(); |
| 246 content::BrowserThread::PostTask(content::BrowserThread::FILE, | 252 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| 247 FROM_HERE, | 253 FROM_HERE, |
| 248 base::Bind(&OperationForTest::VerifyWrite, | 254 base::Bind(&OperationForTest::VerifyWrite, |
| 249 operation_, | 255 operation_, |
| 250 base::Bind(&base::DoNothing))); | 256 base::Bind(&base::DoNothing))); |
| 251 | 257 |
| 252 base::RunLoop().RunUntilIdle(); | 258 base::RunLoop().RunUntilIdle(); |
| 253 | 259 |
| 254 test_utils_.GetUtilityClient()->Progress(0); | 260 client_->Progress(0); |
| 255 test_utils_.GetUtilityClient()->Progress(kTestFileSize / 2); | 261 client_->Progress(kTestFileSize / 2); |
| 256 test_utils_.GetUtilityClient()->Error(error::kVerificationFailed); | 262 client_->Error(error::kVerificationFailed); |
| 257 | 263 |
| 258 base::RunLoop().RunUntilIdle(); | 264 base::RunLoop().RunUntilIdle(); |
| 259 } | 265 } |
| 260 #endif | 266 #endif |
| 261 | 267 |
| 262 // Tests that on creation the operation_ has the expected state. | 268 // Tests that on creation the operation_ has the expected state. |
| 263 TEST_F(ImageWriterOperationTest, Creation) { | 269 TEST_F(ImageWriterOperationTest, Creation) { |
| 264 EXPECT_EQ(0, operation_->GetProgress()); | 270 EXPECT_EQ(0, operation_->GetProgress()); |
| 265 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, operation_->GetStage()); | 271 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, operation_->GetStage()); |
| 266 } | 272 } |
| 267 | 273 |
| 268 } // namespace image_writer | 274 } // namespace image_writer |
| 269 } // namespace extensions | 275 } // namespace extensions |
| OLD | NEW |