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