| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 // Create the zip file. | 71 // Create the zip file. |
| 72 base::FilePath image_dir = temp_dir_.path().AppendASCII("zip"); | 72 base::FilePath image_dir = temp_dir_.path().AppendASCII("zip"); |
| 73 ASSERT_TRUE(base::CreateDirectory(image_dir)); | 73 ASSERT_TRUE(base::CreateDirectory(image_dir)); |
| 74 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_)); | 74 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_)); |
| 75 | 75 |
| 76 FillFile(image_path_, kImagePattern, kTestFileSize); | 76 FillFile(image_path_, kImagePattern, kTestFileSize); |
| 77 | 77 |
| 78 zip_file_ = temp_dir_.path().AppendASCII("test_image.zip"); | 78 zip_file_ = temp_dir_.path().AppendASCII("test_image.zip"); |
| 79 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true)); | 79 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true)); |
| 80 | |
| 81 // Operation setup. | |
| 82 operation_ = new OperationForTest(manager_.AsWeakPtr(), | |
| 83 kDummyExtensionId, | |
| 84 test_device_path_.AsUTF8Unsafe()); | |
| 85 client_ = FakeImageWriterClient::Create(); | |
| 86 operation_->SetImagePath(test_image_path_); | |
| 87 } | 80 } |
| 88 | 81 |
| 89 virtual void TearDown() OVERRIDE { | 82 virtual void TearDown() OVERRIDE { |
| 90 // Ensure all callbacks have been destroyed and cleanup occurs. | |
| 91 client_->Shutdown(); | |
| 92 operation_->Cancel(); | |
| 93 | |
| 94 ImageWriterUnitTestBase::TearDown(); | 83 ImageWriterUnitTestBase::TearDown(); |
| 95 } | 84 } |
| 96 | 85 |
| 97 base::FilePath image_path_; | 86 base::FilePath image_path_; |
| 98 base::FilePath zip_file_; | 87 base::FilePath zip_file_; |
| 99 | 88 |
| 100 MockOperationManager manager_; | 89 MockOperationManager manager_; |
| 101 scoped_refptr<FakeImageWriterClient> client_; | |
| 102 scoped_refptr<OperationForTest> operation_; | |
| 103 }; | 90 }; |
| 104 | 91 |
| 105 } // namespace | 92 } // namespace |
| 106 | 93 |
| 107 // Unizpping a non-zip should do nothing. | |
| 108 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) { | 94 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) { |
| 95 scoped_refptr<OperationForTest> operation( |
| 96 new OperationForTest(manager_.AsWeakPtr(), |
| 97 kDummyExtensionId, |
| 98 test_device_path_.AsUTF8Unsafe())); |
| 99 |
| 109 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0); | 100 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0); |
| 110 | 101 |
| 111 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); | 102 operation->SetImagePath(test_image_path_); |
| 112 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0); | |
| 113 EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0); | |
| 114 | 103 |
| 115 operation_->Start(); | 104 operation->Start(); |
| 116 content::BrowserThread::PostTask( | 105 content::BrowserThread::PostTask( |
| 117 content::BrowserThread::FILE, | 106 content::BrowserThread::FILE, |
| 118 FROM_HERE, | 107 FROM_HERE, |
| 119 base::Bind( | 108 base::Bind( |
| 120 &OperationForTest::Unzip, operation_, base::Bind(&base::DoNothing))); | 109 &OperationForTest::Unzip, operation, base::Bind(&base::DoNothing))); |
| 121 | 110 |
| 122 base::RunLoop().RunUntilIdle(); | 111 base::RunLoop().RunUntilIdle(); |
| 123 } | 112 } |
| 124 | 113 |
| 125 TEST_F(ImageWriterOperationTest, UnzipZipFile) { | 114 TEST_F(ImageWriterOperationTest, UnzipZipFile) { |
| 115 scoped_refptr<OperationForTest> operation( |
| 116 new OperationForTest(manager_.AsWeakPtr(), |
| 117 kDummyExtensionId, |
| 118 test_device_path_.AsUTF8Unsafe())); |
| 119 |
| 126 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); | 120 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
| 127 EXPECT_CALL(manager_, | 121 EXPECT_CALL(manager_, |
| 128 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, _)) | 122 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, _)) |
| 129 .Times(AtLeast(1)); | 123 .Times(AtLeast(1)); |
| 130 EXPECT_CALL(manager_, | 124 EXPECT_CALL(manager_, |
| 131 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 0)) | 125 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 0)) |
| 132 .Times(AtLeast(1)); | 126 .Times(AtLeast(1)); |
| 133 EXPECT_CALL(manager_, | 127 EXPECT_CALL(manager_, |
| 134 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 100)) | 128 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 100)) |
| 135 .Times(AtLeast(1)); | 129 .Times(AtLeast(1)); |
| 136 | 130 |
| 137 operation_->SetImagePath(zip_file_); | 131 operation->SetImagePath(zip_file_); |
| 138 | 132 |
| 139 operation_->Start(); | 133 operation->Start(); |
| 140 content::BrowserThread::PostTask( | 134 content::BrowserThread::PostTask( |
| 141 content::BrowserThread::FILE, | 135 content::BrowserThread::FILE, |
| 142 FROM_HERE, | 136 FROM_HERE, |
| 143 base::Bind( | 137 base::Bind( |
| 144 &OperationForTest::Unzip, operation_, base::Bind(&base::DoNothing))); | 138 &OperationForTest::Unzip, operation, base::Bind(&base::DoNothing))); |
| 145 | 139 |
| 146 base::RunLoop().RunUntilIdle(); | 140 base::RunLoop().RunUntilIdle(); |
| 147 | 141 |
| 148 EXPECT_TRUE(base::ContentsEqual(image_path_, operation_->GetImagePath())); | 142 EXPECT_TRUE(base::ContentsEqual(image_path_, operation->GetImagePath())); |
| 149 } | 143 } |
| 150 | 144 |
| 151 #if defined(OS_LINUX) | 145 #if defined(OS_LINUX) |
| 152 TEST_F(ImageWriterOperationTest, WriteImageToDevice) { | 146 TEST_F(ImageWriterOperationTest, WriteImageToDevice) { |
| 153 #if !defined(OS_CHROMEOS) | 147 |
| 154 operation_->SetUtilityClientForTesting(client_); | 148 scoped_refptr<OperationForTest> operation( |
| 155 #endif | 149 new OperationForTest(manager_.AsWeakPtr(), |
| 150 kDummyExtensionId, |
| 151 test_device_path_.AsUTF8Unsafe())); |
| 156 | 152 |
| 157 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); | 153 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
| 158 EXPECT_CALL(manager_, | 154 EXPECT_CALL(manager_, |
| 159 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _)) | 155 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _)) |
| 160 .Times(AtLeast(1)); | 156 .Times(AtLeast(1)); |
| 161 EXPECT_CALL(manager_, | 157 EXPECT_CALL(manager_, |
| 162 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0)) | 158 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0)) |
| 163 .Times(AtLeast(1)); | 159 .Times(AtLeast(1)); |
| 164 EXPECT_CALL(manager_, | 160 EXPECT_CALL(manager_, |
| 165 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100)) | 161 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100)) |
| 166 .Times(AtLeast(1)); | 162 .Times(AtLeast(1)); |
| 167 | 163 |
| 168 operation_->Start(); | 164 operation->SetImagePath(test_image_path_); |
| 165 |
| 166 operation->Start(); |
| 169 content::BrowserThread::PostTask( | 167 content::BrowserThread::PostTask( |
| 170 content::BrowserThread::FILE, | 168 content::BrowserThread::FILE, |
| 171 FROM_HERE, | 169 FROM_HERE, |
| 172 base::Bind( | 170 base::Bind( |
| 173 &OperationForTest::Write, operation_, base::Bind(&base::DoNothing))); | 171 &OperationForTest::Write, operation, base::Bind(&base::DoNothing))); |
| 174 | 172 |
| 175 base::RunLoop().RunUntilIdle(); | 173 base::RunLoop().RunUntilIdle(); |
| 176 | 174 |
| 177 #if !defined(OS_CHROMEOS) | 175 #if !defined(OS_CHROMEOS) |
| 178 client_->Progress(0); | 176 // Chrome OS tests don't actually write to the disk because that's handled by |
| 179 client_->Progress(kTestFileSize / 2); | 177 // the DBUS process. |
| 180 client_->Progress(kTestFileSize); | 178 EXPECT_TRUE(base::ContentsEqual(test_image_path_, test_device_path_)); |
| 181 client_->Success(); | |
| 182 | |
| 183 base::RunLoop().RunUntilIdle(); | |
| 184 #endif | 179 #endif |
| 185 } | 180 } |
| 186 #endif | 181 #endif |
| 187 | 182 |
| 188 #if !defined(OS_CHROMEOS) | 183 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 189 // Chrome OS doesn't support verification in the ImageBurner, so these two tests | 184 // Chrome OS doesn't support verification in the ImageBurner, so these two tests |
| 190 // are skipped. | 185 // are skipped. |
| 191 | 186 |
| 192 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) { | 187 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) { |
| 193 operation_->SetUtilityClientForTesting(client_); | 188 scoped_refptr<OperationForTest> operation( |
| 189 new OperationForTest(manager_.AsWeakPtr(), |
| 190 kDummyExtensionId, |
| 191 test_device_path_.AsUTF8Unsafe())); |
| 194 | 192 |
| 195 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); | 193 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
| 196 EXPECT_CALL( | 194 EXPECT_CALL( |
| 197 manager_, | 195 manager_, |
| 198 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) | 196 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) |
| 199 .Times(AtLeast(1)); | 197 .Times(AtLeast(1)); |
| 200 EXPECT_CALL( | 198 EXPECT_CALL( |
| 201 manager_, | 199 manager_, |
| 202 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0)) | 200 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0)) |
| 203 .Times(AtLeast(1)); | 201 .Times(AtLeast(1)); |
| 204 EXPECT_CALL( | 202 EXPECT_CALL( |
| 205 manager_, | 203 manager_, |
| 206 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) | 204 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) |
| 207 .Times(AtLeast(1)); | 205 .Times(AtLeast(1)); |
| 208 | 206 |
| 209 FillFile(test_device_path_, kImagePattern, kTestFileSize); | 207 FillFile(test_device_path_, kImagePattern, kTestFileSize); |
| 208 operation->SetImagePath(test_image_path_); |
| 210 | 209 |
| 211 operation_->Start(); | 210 operation->Start(); |
| 212 content::BrowserThread::PostTask(content::BrowserThread::FILE, | 211 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| 213 FROM_HERE, | 212 FROM_HERE, |
| 214 base::Bind(&OperationForTest::VerifyWrite, | 213 base::Bind(&OperationForTest::VerifyWrite, |
| 215 operation_, | 214 operation, |
| 216 base::Bind(&base::DoNothing))); | 215 base::Bind(&base::DoNothing))); |
| 217 | 216 |
| 218 base::RunLoop().RunUntilIdle(); | 217 base::RunLoop().RunUntilIdle(); |
| 219 | |
| 220 client_->Progress(0); | |
| 221 client_->Progress(kTestFileSize / 2); | |
| 222 client_->Progress(kTestFileSize); | |
| 223 client_->Success(); | |
| 224 | |
| 225 base::RunLoop().RunUntilIdle(); | |
| 226 } | 218 } |
| 227 | 219 |
| 228 TEST_F(ImageWriterOperationTest, VerifyFileFailure) { | 220 TEST_F(ImageWriterOperationTest, VerifyFileFailure) { |
| 229 operation_->SetUtilityClientForTesting(client_); | 221 scoped_refptr<OperationForTest> operation( |
| 222 new OperationForTest(manager_.AsWeakPtr(), |
| 223 kDummyExtensionId, |
| 224 test_device_path_.AsUTF8Unsafe())); |
| 230 | 225 |
| 231 EXPECT_CALL( | 226 EXPECT_CALL( |
| 232 manager_, | 227 manager_, |
| 228 OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _)) |
| 229 .Times(1); |
| 230 EXPECT_CALL( |
| 231 manager_, |
| 233 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) | 232 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) |
| 234 .Times(AnyNumber()); | 233 .Times(AnyNumber()); |
| 235 EXPECT_CALL( | 234 EXPECT_CALL( |
| 236 manager_, | 235 manager_, |
| 237 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) | 236 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) |
| 238 .Times(0); | 237 .Times(0); |
| 239 EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0); | |
| 240 EXPECT_CALL( | |
| 241 manager_, | |
| 242 OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _)) | |
| 243 .Times(1); | |
| 244 | 238 |
| 245 FillFile(test_device_path_, kDevicePattern, kTestFileSize); | 239 FillFile(test_device_path_, kDevicePattern, kTestFileSize); |
| 240 operation->SetImagePath(test_image_path_); |
| 246 | 241 |
| 247 operation_->Start(); | 242 operation->Start(); |
| 248 content::BrowserThread::PostTask(content::BrowserThread::FILE, | 243 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| 249 FROM_HERE, | 244 FROM_HERE, |
| 250 base::Bind(&OperationForTest::VerifyWrite, | 245 base::Bind(&OperationForTest::VerifyWrite, |
| 251 operation_, | 246 operation, |
| 252 base::Bind(&base::DoNothing))); | 247 base::Bind(&base::DoNothing))); |
| 253 | 248 |
| 254 base::RunLoop().RunUntilIdle(); | 249 base::RunLoop().RunUntilIdle(); |
| 255 | |
| 256 client_->Progress(0); | |
| 257 client_->Progress(kTestFileSize / 2); | |
| 258 client_->Error(error::kVerificationFailed); | |
| 259 | |
| 260 base::RunLoop().RunUntilIdle(); | |
| 261 } | 250 } |
| 262 #endif | 251 #endif |
| 263 | 252 |
| 264 // Tests that on creation the operation_ has the expected state. | 253 // Tests that on creation the operation has the expected state. |
| 265 TEST_F(ImageWriterOperationTest, Creation) { | 254 TEST_F(ImageWriterOperationTest, Creation) { |
| 266 EXPECT_EQ(0, operation_->GetProgress()); | 255 scoped_refptr<Operation> op( |
| 267 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, operation_->GetStage()); | 256 new OperationForTest(manager_.AsWeakPtr(), |
| 257 kDummyExtensionId, |
| 258 test_device_path_.AsUTF8Unsafe())); |
| 259 |
| 260 EXPECT_EQ(0, op->GetProgress()); |
| 261 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage()); |
| 268 } | 262 } |
| 269 | 263 |
| 270 } // namespace image_writer | 264 } // namespace image_writer |
| 271 } // namespace extensions | 265 } // namespace extensions |
| OLD | NEW |