Index: chrome/browser/extensions/api/image_writer_private/operation_unittest.cc |
diff --git a/chrome/browser/extensions/api/image_writer_private/operation_unittest.cc b/chrome/browser/extensions/api/image_writer_private/operation_unittest.cc |
index d835836318cb2d46eed716fdf8d57a2cebaeb259..e99bd5c245b6e05d4ef60e18bbe52b5ab93ccc50 100644 |
--- a/chrome/browser/extensions/api/image_writer_private/operation_unittest.cc |
+++ b/chrome/browser/extensions/api/image_writer_private/operation_unittest.cc |
@@ -28,34 +28,37 @@ using testing::AtLeast; |
using testing::Gt; |
using testing::Lt; |
-// This class gives us access to the protected methods of Operation so that we |
-// can call them directly. It also allows us to selectively disable some |
-// phases. |
+// This class gives us a generic Operation with the ability to set or inspect |
+// the current path to the image file. |
class OperationForTest : public Operation { |
public: |
- OperationForTest(base::WeakPtr<OperationManager> manager, |
+ OperationForTest(base::WeakPtr<OperationManager> manager_, |
const ExtensionId& extension_id, |
- const std::string& storage_unit_id) |
- : Operation(manager, extension_id, storage_unit_id) {} |
+ const std::string& device_path) |
+ : Operation(manager_, extension_id, device_path) {} |
- virtual void Start() OVERRIDE { |
- } |
+ void StartImpl() OVERRIDE {} |
- void UnzipStart(scoped_ptr<base::FilePath> zip_file) { |
- Operation::UnzipStart(zip_file.Pass()); |
+ // Expose internal stages for testing. |
+ void Unzip(const base::Closure& continuation) { |
+ Operation::Unzip(continuation); |
} |
- void WriteStart() { |
- Operation::WriteStart(); |
+ void Write(const base::Closure& continuation) { |
+ Operation::Write(continuation); |
} |
- void VerifyWriteStart() { |
- Operation::VerifyWriteStart(); |
+ void VerifyWrite(const base::Closure& continuation) { |
+ Operation::VerifyWrite(continuation); |
} |
- void Finish() { |
- Operation::Finish(); |
+ // Helpers to set-up state for intermediate stages. |
+ void SetImagePath(const base::FilePath image_path) { |
+ image_path_ = image_path; |
} |
+ |
+ base::FilePath GetImagePath() { return image_path_; } |
+ |
private: |
virtual ~OperationForTest() {}; |
}; |
@@ -66,78 +69,182 @@ class ImageWriterOperationTest : public ImageWriterUnitTestBase { |
ImageWriterUnitTestBase::SetUp(); |
// Create the zip file. |
- ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
- ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), |
- &image_file_)); |
- ASSERT_TRUE(base::CreateTemporaryFile(&zip_file_)); |
+ base::FilePath image_dir = temp_dir_.path().AppendASCII("zip"); |
+ ASSERT_TRUE(base::CreateDirectory(image_dir)); |
+ ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_)); |
- scoped_ptr<char[]> buffer(new char[kTestFileSize]); |
- memset(buffer.get(), kImagePattern, kTestFileSize); |
- file_util::WriteFile(image_file_, buffer.get(), kTestFileSize); |
+ FillFile(image_path_, kImagePattern, kTestFileSize); |
- zip::Zip(temp_dir_.path(), zip_file_, true); |
+ zip_file_ = temp_dir_.path().AppendASCII("test_image.zip"); |
+ ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true)); |
} |
virtual void TearDown() OVERRIDE { |
ImageWriterUnitTestBase::TearDown(); |
} |
- base::ScopedTempDir temp_dir_; |
- base::FilePath image_file_; |
+ base::FilePath image_path_; |
base::FilePath zip_file_; |
+ |
+ MockOperationManager manager_; |
}; |
} // namespace |
-#if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
-// Tests a successful unzip. |
-TEST_F(ImageWriterOperationTest, Unzip) { |
- MockOperationManager manager; |
+TEST_F(ImageWriterOperationTest, UnzipNonZipFile) { |
+ scoped_refptr<OperationForTest> operation( |
+ new OperationForTest(manager_.AsWeakPtr(), |
+ kDummyExtensionId, |
+ test_device_path_.AsUTF8Unsafe())); |
+ |
+ EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0); |
+ |
+ operation->SetImagePath(test_image_path_); |
+ |
+ operation->Start(); |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::FILE, |
+ FROM_HERE, |
+ base::Bind( |
+ &OperationForTest::Unzip, operation, base::Bind(&base::DoNothing))); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+} |
+ |
+TEST_F(ImageWriterOperationTest, UnzipZipFile) { |
+ scoped_refptr<OperationForTest> operation( |
+ new OperationForTest(manager_.AsWeakPtr(), |
+ kDummyExtensionId, |
+ test_device_path_.AsUTF8Unsafe())); |
+ |
+ EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
+ EXPECT_CALL(manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, _)) |
+ .Times(AtLeast(1)); |
+ EXPECT_CALL(manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 0)) |
+ .Times(AtLeast(1)); |
+ EXPECT_CALL(manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 100)) |
+ .Times(AtLeast(1)); |
+ |
+ operation->SetImagePath(zip_file_); |
+ |
+ operation->Start(); |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::FILE, |
+ FROM_HERE, |
+ base::Bind( |
+ &OperationForTest::Unzip, operation, base::Bind(&base::DoNothing))); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ EXPECT_TRUE(base::ContentsEqual(image_path_, operation->GetImagePath())); |
+} |
+ |
+#if defined(OS_LINUX) || defined(OS_CHROMEOS) |
+TEST_F(ImageWriterOperationTest, WriteImageToDevice) { |
scoped_refptr<OperationForTest> operation( |
- new OperationForTest(manager.AsWeakPtr(), |
+ new OperationForTest(manager_.AsWeakPtr(), |
kDummyExtensionId, |
test_device_path_.AsUTF8Unsafe())); |
- scoped_ptr<base::FilePath> zip_file(new base::FilePath(zip_file_)); |
- |
- // At least one progress report > 0% and < 100%. |
- EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
- image_writer_api::STAGE_UNZIP, |
- Lt(100))).Times(AtLeast(1)); |
- // At least one progress report at 100%. |
- EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
- image_writer_api::STAGE_UNZIP, |
- 100)).Times(AtLeast(1)); |
- // At least one progress report at 0%. |
- EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
- image_writer_api::STAGE_UNZIP, |
- 0)).Times(AtLeast(1)); |
- // Any number of additional progress calls in later stages. |
- EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
- Gt(image_writer_api::STAGE_UNZIP), |
- _)).Times(AnyNumber()); |
- // One completion call. |
- EXPECT_CALL(manager, OnComplete(kDummyExtensionId)).Times(1); |
- // No errors |
- EXPECT_CALL(manager, OnError(_, _, _, _)).Times(0); |
+ EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
+ EXPECT_CALL(manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _)) |
+ .Times(AtLeast(1)); |
+ EXPECT_CALL(manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0)) |
+ .Times(AtLeast(1)); |
+ EXPECT_CALL(manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100)) |
+ .Times(AtLeast(1)); |
+ |
+ operation->SetImagePath(test_image_path_); |
+ operation->Start(); |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::FILE, |
+ FROM_HERE, |
+ base::Bind( |
+ &OperationForTest::Write, operation, base::Bind(&base::DoNothing))); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ EXPECT_TRUE(base::ContentsEqual(test_image_path_, test_device_path_)); |
+} |
+#endif |
+ |
+TEST_F(ImageWriterOperationTest, VerifyFileSuccess) { |
+ scoped_refptr<OperationForTest> operation( |
+ new OperationForTest(manager_.AsWeakPtr(), |
+ kDummyExtensionId, |
+ test_device_path_.AsUTF8Unsafe())); |
+ |
+ EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
+ EXPECT_CALL( |
+ manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) |
+ .Times(AtLeast(1)); |
+ EXPECT_CALL( |
+ manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0)) |
+ .Times(AtLeast(1)); |
+ EXPECT_CALL( |
+ manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) |
+ .Times(AtLeast(1)); |
+ |
+ FillFile(test_device_path_, kImagePattern, kTestFileSize); |
+ operation->SetImagePath(test_image_path_); |
+ |
+ operation->Start(); |
content::BrowserThread::PostTask(content::BrowserThread::FILE, |
FROM_HERE, |
- base::Bind(&OperationForTest::UnzipStart, |
+ base::Bind(&OperationForTest::VerifyWrite, |
operation, |
- base::Passed(&zip_file))); |
+ base::Bind(&base::DoNothing))); |
base::RunLoop().RunUntilIdle(); |
+} |
- EXPECT_TRUE(base::ContentsEqual(image_file_, test_device_path_)); |
+TEST_F(ImageWriterOperationTest, VerifyFileFailure) { |
+ scoped_refptr<OperationForTest> operation( |
+ new OperationForTest(manager_.AsWeakPtr(), |
+ kDummyExtensionId, |
+ test_device_path_.AsUTF8Unsafe())); |
+ |
+ EXPECT_CALL( |
+ manager_, |
+ OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _)) |
+ .Times(1); |
+ EXPECT_CALL( |
+ manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) |
+ .Times(AnyNumber()); |
+ EXPECT_CALL( |
+ manager_, |
+ OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) |
+ .Times(0); |
+ |
+ FillFile(test_device_path_, kDevicePattern, kTestFileSize); |
+ operation->SetImagePath(test_image_path_); |
+ |
+ operation->Start(); |
+ content::BrowserThread::PostTask(content::BrowserThread::FILE, |
+ FROM_HERE, |
+ base::Bind(&OperationForTest::VerifyWrite, |
+ operation, |
+ base::Bind(&base::DoNothing))); |
+ |
+ base::RunLoop().RunUntilIdle(); |
} |
-#endif |
+// Tests that on creation the operation has the expected state. |
TEST_F(ImageWriterOperationTest, Creation) { |
- MockOperationManager manager; |
scoped_refptr<Operation> op( |
- new OperationForTest(manager.AsWeakPtr(), |
+ new OperationForTest(manager_.AsWeakPtr(), |
kDummyExtensionId, |
test_device_path_.AsUTF8Unsafe())); |