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" |
| 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" |
5 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
6 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 10 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
| 11 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" |
7 #include "chrome/browser/extensions/api/image_writer_private/test_utils.h" | 12 #include "chrome/browser/extensions/api/image_writer_private/test_utils.h" |
| 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "third_party/zlib/google/zip.h" |
8 | 19 |
9 namespace extensions { | 20 namespace extensions { |
10 namespace image_writer { | 21 namespace image_writer { |
| 22 |
11 namespace { | 23 namespace { |
12 | 24 |
13 class ImageWriterOperationTest : public ImageWriterUnitTestBase { | 25 using testing::_; |
| 26 using testing::AnyNumber; |
| 27 using testing::AtLeast; |
| 28 using testing::Gt; |
| 29 using testing::Lt; |
| 30 |
| 31 // This class gives us access to the protected methods of Operation so that we |
| 32 // can call them directly. It also allows us to selectively disable some |
| 33 // phases. |
| 34 class OperationForTest : public Operation { |
| 35 public: |
| 36 OperationForTest(base::WeakPtr<OperationManager> manager, |
| 37 const ExtensionId& extension_id, |
| 38 const std::string& storage_unit_id) |
| 39 : Operation(manager, extension_id, storage_unit_id) {} |
| 40 |
| 41 virtual void Start() OVERRIDE { |
| 42 } |
| 43 |
| 44 void UnzipStart(scoped_ptr<base::FilePath> zip_file) { |
| 45 Operation::UnzipStart(zip_file.Pass()); |
| 46 } |
| 47 |
| 48 void WriteStart() { |
| 49 Operation::WriteStart(); |
| 50 } |
| 51 |
| 52 void VerifyWriteStart() { |
| 53 Operation::VerifyWriteStart(); |
| 54 } |
| 55 |
| 56 void Finish() { |
| 57 Operation::Finish(); |
| 58 } |
| 59 private: |
| 60 virtual ~OperationForTest() {}; |
14 }; | 61 }; |
15 | 62 |
16 class DummyOperation : public Operation { | 63 class ImageWriterOperationTest : public ImageWriterUnitTestBase { |
17 public: | 64 protected: |
18 DummyOperation(base::WeakPtr<OperationManager> manager, | 65 virtual void SetUp() OVERRIDE { |
19 const ExtensionId& extension_id, | 66 ImageWriterUnitTestBase::SetUp(); |
20 const std::string& storage_unit_id) | 67 |
21 : Operation(manager, extension_id, storage_unit_id) {}; | 68 // Create the zip file. |
22 virtual void Start() OVERRIDE {}; | 69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
23 private: | 70 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), |
24 virtual ~DummyOperation() {}; | 71 &image_file_)); |
| 72 ASSERT_TRUE(base::CreateTemporaryFile(&zip_file_)); |
| 73 |
| 74 scoped_ptr<char[]> buffer(new char[kTestFileSize]); |
| 75 memset(buffer.get(), kImagePattern, kTestFileSize); |
| 76 file_util::WriteFile(image_file_, buffer.get(), kTestFileSize); |
| 77 |
| 78 zip::Zip(temp_dir_.path(), zip_file_, true); |
| 79 } |
| 80 |
| 81 virtual void TearDown() OVERRIDE { |
| 82 ImageWriterUnitTestBase::TearDown(); |
| 83 } |
| 84 |
| 85 base::ScopedTempDir temp_dir_; |
| 86 base::FilePath image_file_; |
| 87 base::FilePath zip_file_; |
25 }; | 88 }; |
26 | 89 |
27 TEST_F(ImageWriterOperationTest, Create) { | 90 } // namespace |
| 91 |
| 92 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 93 // Tests a successful unzip. |
| 94 TEST_F(ImageWriterOperationTest, Unzip) { |
| 95 MockOperationManager manager; |
| 96 |
| 97 scoped_refptr<OperationForTest> operation( |
| 98 new OperationForTest(manager.AsWeakPtr(), |
| 99 kDummyExtensionId, |
| 100 test_device_path_.AsUTF8Unsafe())); |
| 101 |
| 102 scoped_ptr<base::FilePath> zip_file(new base::FilePath(zip_file_)); |
| 103 |
| 104 // At least one progress report > 0% and < 100%. |
| 105 EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
| 106 image_writer_api::STAGE_UNZIP, |
| 107 Lt(100))).Times(AtLeast(1)); |
| 108 // At least one progress report at 100%. |
| 109 EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
| 110 image_writer_api::STAGE_UNZIP, |
| 111 100)).Times(AtLeast(1)); |
| 112 // At least one progress report at 0%. |
| 113 EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
| 114 image_writer_api::STAGE_UNZIP, |
| 115 0)).Times(AtLeast(1)); |
| 116 // Any number of additional progress calls in later stages. |
| 117 EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
| 118 Gt(image_writer_api::STAGE_UNZIP), |
| 119 _)).Times(AnyNumber()); |
| 120 // One completion call. |
| 121 EXPECT_CALL(manager, OnComplete(kDummyExtensionId)).Times(1); |
| 122 // No errors |
| 123 EXPECT_CALL(manager, OnError(_, _, _, _)).Times(0); |
| 124 |
| 125 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| 126 FROM_HERE, |
| 127 base::Bind(&OperationForTest::UnzipStart, |
| 128 operation, |
| 129 base::Passed(&zip_file))); |
| 130 |
| 131 base::RunLoop().RunUntilIdle(); |
| 132 |
| 133 EXPECT_TRUE(base::ContentsEqual(image_file_, test_device_path_)); |
| 134 } |
| 135 #endif |
| 136 |
| 137 TEST_F(ImageWriterOperationTest, Creation) { |
28 MockOperationManager manager; | 138 MockOperationManager manager; |
29 scoped_refptr<Operation> op( | 139 scoped_refptr<Operation> op( |
30 new DummyOperation(manager.AsWeakPtr(), | 140 new OperationForTest(manager.AsWeakPtr(), |
31 kDummyExtensionId, | 141 kDummyExtensionId, |
32 test_device_path_.AsUTF8Unsafe())); | 142 test_device_path_.AsUTF8Unsafe())); |
33 | 143 |
34 EXPECT_EQ(0, op->GetProgress()); | 144 EXPECT_EQ(0, op->GetProgress()); |
35 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage()); | 145 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage()); |
36 } | 146 } |
37 | 147 |
38 } // namespace | |
39 } // namespace image_writer | 148 } // namespace image_writer |
40 } // namespace extensions | 149 } // namespace extensions |
OLD | NEW |