OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 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" |
| 9 #include "chrome/browser/extensions/api/image_writer_private/operation.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" |
| 17 |
| 18 namespace extensions { |
| 19 namespace image_writer { |
| 20 |
| 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 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 }; |
| 61 |
| 62 // A mock for OperationManager |
| 63 class MockManager : public OperationManager { |
| 64 public: |
| 65 explicit MockManager(Profile* profile) : OperationManager(profile) {} |
| 66 |
| 67 MOCK_METHOD3(OnProgress, void(const ExtensionId&, |
| 68 image_writer_api::Stage, |
| 69 int)); |
| 70 MOCK_METHOD1(OnComplete, void(const ExtensionId&)); |
| 71 MOCK_METHOD4(OnError, void(const ExtensionId&, |
| 72 image_writer_api::Stage, |
| 73 int, |
| 74 const std::string&)); |
| 75 }; |
| 76 |
| 77 class ImageWriterOperationTest : public testing::Test { |
| 78 protected: |
| 79 void SetUp() OVERRIDE { |
| 80 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 81 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
| 82 &image_file_)); |
| 83 ASSERT_TRUE(file_util::CreateTemporaryFile(&device_file_)); |
| 84 ASSERT_TRUE(file_util::CreateTemporaryFile(&zip_file_)); |
| 85 |
| 86 char buffer[kTestFileSize]; |
| 87 memset(buffer, kTestPattern, kTestFileSize); |
| 88 file_util::WriteFile(image_file_, buffer, kTestFileSize); |
| 89 |
| 90 zip::Zip(temp_dir_.path(), zip_file_, true); |
| 91 } |
| 92 |
| 93 void TearDown() OVERRIDE { |
| 94 } |
| 95 |
| 96 base::FilePath zip_file_; |
| 97 base::FilePath image_file_; |
| 98 base::FilePath device_file_; |
| 99 TestingProfile test_profile_; |
| 100 base::ScopedTempDir temp_dir_; |
| 101 |
| 102 private: |
| 103 content::TestBrowserThreadBundle thread_bundle_; |
| 104 }; |
| 105 |
| 106 } // namespace |
| 107 |
| 108 // Tests a successful unzip. |
| 109 TEST_F(ImageWriterOperationTest, TestUnzip) { |
| 110 MockManager manager(&test_profile_); |
| 111 |
| 112 scoped_refptr<OperationForTest> operation( |
| 113 new OperationForTest(manager.AsWeakPtr(), |
| 114 kDummyExtensionId, |
| 115 device_file_.AsUTF8Unsafe())); |
| 116 |
| 117 scoped_ptr<base::FilePath> zip_file(new base::FilePath(zip_file_)); |
| 118 |
| 119 // At least one progress report > 0% and < 100%. |
| 120 EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
| 121 image_writer_api::STAGE_UNZIP, |
| 122 Lt(100))).Times(AtLeast(1)); |
| 123 // At least one progress report at 100%. |
| 124 EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
| 125 image_writer_api::STAGE_UNZIP, |
| 126 100)).Times(AtLeast(1)); |
| 127 // At least one progress report at 0%. |
| 128 EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
| 129 image_writer_api::STAGE_UNZIP, |
| 130 0)).Times(AtLeast(1)); |
| 131 // Any number of additional progress calls in later stages. |
| 132 EXPECT_CALL(manager, OnProgress(kDummyExtensionId, |
| 133 Gt(image_writer_api::STAGE_UNZIP), |
| 134 _)).Times(AnyNumber()); |
| 135 // One completion call. |
| 136 EXPECT_CALL(manager, OnComplete(kDummyExtensionId)).Times(1); |
| 137 // No errors |
| 138 EXPECT_CALL(manager, OnError(_, _, _, _)).Times(0); |
| 139 |
| 140 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| 141 FROM_HERE, |
| 142 base::Bind(&OperationForTest::UnzipStart, |
| 143 operation, |
| 144 base::Passed(&zip_file))); |
| 145 |
| 146 base::RunLoop().RunUntilIdle(); |
| 147 |
| 148 EXPECT_TRUE(base::ContentsEqual(image_file_, device_file_)); |
| 149 } |
| 150 |
| 151 } // namespace image_writer |
| 152 } // namespace extensions |
OLD | NEW |