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 10 matching lines...) Expand all Loading... |
21 namespace image_writer { | 21 namespace image_writer { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 using testing::_; | 25 using testing::_; |
26 using testing::AnyNumber; | 26 using testing::AnyNumber; |
27 using testing::AtLeast; | 27 using testing::AtLeast; |
28 using testing::Gt; | 28 using testing::Gt; |
29 using testing::Lt; | 29 using testing::Lt; |
30 | 30 |
31 // This class gives us access to the protected methods of Operation so that we | 31 // This class gives us a generic Operation with the ability to set or inspect |
32 // can call them directly. It also allows us to selectively disable some | 32 // the current path to the image file. |
33 // phases. | |
34 class OperationForTest : public Operation { | 33 class OperationForTest : public Operation { |
35 public: | 34 public: |
36 OperationForTest(base::WeakPtr<OperationManager> manager, | 35 OperationForTest(base::WeakPtr<OperationManager> manager_, |
37 const ExtensionId& extension_id, | 36 const ExtensionId& extension_id, |
38 const std::string& storage_unit_id) | 37 const std::string& device_path) |
39 : Operation(manager, extension_id, storage_unit_id) {} | 38 : Operation(manager_, extension_id, device_path) {} |
40 | 39 |
41 virtual void Start() OVERRIDE { | 40 // Expose internal stages for testing. |
| 41 void Unzip(const base::Closure& continuation) { |
| 42 Operation::Unzip(continuation); |
42 } | 43 } |
43 | 44 |
44 void UnzipStart(scoped_ptr<base::FilePath> zip_file) { | 45 void Write(const base::Closure& continuation) { |
45 Operation::UnzipStart(zip_file.Pass()); | 46 Operation::Write(continuation); |
46 } | 47 } |
47 | 48 |
48 void WriteStart() { | 49 void VerifyWrite(const base::Closure& continuation) { |
49 Operation::WriteStart(); | 50 Operation::VerifyWrite(continuation); |
50 } | 51 } |
51 | 52 |
52 void VerifyWriteStart() { | 53 // Helpers to set-up state for intermediate stages. |
53 Operation::VerifyWriteStart(); | 54 void SetImagePath(const base::FilePath image_path) { |
| 55 image_path_ = image_path; |
54 } | 56 } |
55 | 57 |
56 void Finish() { | 58 base::FilePath GetImagePath() { return image_path_; } |
57 Operation::Finish(); | 59 |
58 } | |
59 private: | 60 private: |
60 virtual ~OperationForTest() {}; | 61 virtual ~OperationForTest() {}; |
61 }; | 62 }; |
62 | 63 |
63 class ImageWriterOperationTest : public ImageWriterUnitTestBase { | 64 class ImageWriterOperationTest : public ImageWriterUnitTestBase { |
64 protected: | 65 protected: |
65 virtual void SetUp() OVERRIDE { | 66 virtual void SetUp() OVERRIDE { |
66 ImageWriterUnitTestBase::SetUp(); | 67 ImageWriterUnitTestBase::SetUp(); |
67 | 68 |
68 // Create the zip file. | 69 // Create the zip file. |
69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 70 base::FilePath image_dir = temp_dir_.path().AppendASCII("zip"); |
70 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), | 71 ASSERT_TRUE(base::CreateDirectory(image_dir)); |
71 &image_file_)); | 72 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_)); |
72 ASSERT_TRUE(base::CreateTemporaryFile(&zip_file_)); | |
73 | 73 |
74 scoped_ptr<char[]> buffer(new char[kTestFileSize]); | 74 FillFile(image_path_, kImagePattern, kTestFileSize); |
75 memset(buffer.get(), kImagePattern, kTestFileSize); | |
76 file_util::WriteFile(image_file_, buffer.get(), kTestFileSize); | |
77 | 75 |
78 zip::Zip(temp_dir_.path(), zip_file_, true); | 76 zip_file_ = temp_dir_.path().AppendASCII("test_image.zip"); |
| 77 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true)); |
79 } | 78 } |
80 | 79 |
81 virtual void TearDown() OVERRIDE { | 80 virtual void TearDown() OVERRIDE { |
82 ImageWriterUnitTestBase::TearDown(); | 81 ImageWriterUnitTestBase::TearDown(); |
83 } | 82 } |
84 | 83 |
85 base::ScopedTempDir temp_dir_; | 84 base::FilePath image_path_; |
86 base::FilePath image_file_; | |
87 base::FilePath zip_file_; | 85 base::FilePath zip_file_; |
| 86 |
| 87 MockOperationManager manager_; |
88 }; | 88 }; |
89 | 89 |
90 } // namespace | 90 } // namespace |
91 | 91 |
92 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 92 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) { |
93 // Tests a successful unzip. | |
94 TEST_F(ImageWriterOperationTest, Unzip) { | |
95 MockOperationManager manager; | |
96 | |
97 scoped_refptr<OperationForTest> operation( | 93 scoped_refptr<OperationForTest> operation( |
98 new OperationForTest(manager.AsWeakPtr(), | 94 new OperationForTest(manager_.AsWeakPtr(), |
99 kDummyExtensionId, | 95 kDummyExtensionId, |
100 test_device_path_.AsUTF8Unsafe())); | 96 test_device_path_.AsUTF8Unsafe())); |
101 | 97 |
102 scoped_ptr<base::FilePath> zip_file(new base::FilePath(zip_file_)); | 98 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0); |
103 | 99 |
104 // At least one progress report > 0% and < 100%. | 100 operation->SetImagePath(test_image_path_); |
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 | 101 |
125 content::BrowserThread::PostTask(content::BrowserThread::FILE, | 102 operation->Start(); |
126 FROM_HERE, | 103 content::BrowserThread::PostTask( |
127 base::Bind(&OperationForTest::UnzipStart, | 104 content::BrowserThread::FILE, |
128 operation, | 105 FROM_HERE, |
129 base::Passed(&zip_file))); | 106 base::Bind( |
| 107 &OperationForTest::Unzip, operation, base::Bind(&base::DoNothing))); |
| 108 |
| 109 base::RunLoop().RunUntilIdle(); |
| 110 } |
| 111 |
| 112 TEST_F(ImageWriterOperationTest, UnzipZipFile) { |
| 113 scoped_refptr<OperationForTest> operation( |
| 114 new OperationForTest(manager_.AsWeakPtr(), |
| 115 kDummyExtensionId, |
| 116 test_device_path_.AsUTF8Unsafe())); |
| 117 |
| 118 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
| 119 EXPECT_CALL(manager_, |
| 120 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, _)) |
| 121 .Times(AtLeast(1)); |
| 122 EXPECT_CALL(manager_, |
| 123 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 0)) |
| 124 .Times(AtLeast(1)); |
| 125 EXPECT_CALL(manager_, |
| 126 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 100)) |
| 127 .Times(AtLeast(1)); |
| 128 |
| 129 operation->SetImagePath(zip_file_); |
| 130 |
| 131 operation->Start(); |
| 132 content::BrowserThread::PostTask( |
| 133 content::BrowserThread::FILE, |
| 134 FROM_HERE, |
| 135 base::Bind( |
| 136 &OperationForTest::Unzip, operation, base::Bind(&base::DoNothing))); |
130 | 137 |
131 base::RunLoop().RunUntilIdle(); | 138 base::RunLoop().RunUntilIdle(); |
132 | 139 |
133 EXPECT_TRUE(base::ContentsEqual(image_file_, test_device_path_)); | 140 EXPECT_TRUE(base::ContentsEqual(image_path_, operation->GetImagePath())); |
| 141 } |
| 142 |
| 143 #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 144 TEST_F(ImageWriterOperationTest, WriteImageToDevice) { |
| 145 |
| 146 scoped_refptr<OperationForTest> operation( |
| 147 new OperationForTest(manager_.AsWeakPtr(), |
| 148 kDummyExtensionId, |
| 149 test_device_path_.AsUTF8Unsafe())); |
| 150 |
| 151 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
| 152 EXPECT_CALL(manager_, |
| 153 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _)) |
| 154 .Times(AtLeast(1)); |
| 155 EXPECT_CALL(manager_, |
| 156 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0)) |
| 157 .Times(AtLeast(1)); |
| 158 EXPECT_CALL(manager_, |
| 159 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100)) |
| 160 .Times(AtLeast(1)); |
| 161 |
| 162 operation->SetImagePath(test_image_path_); |
| 163 |
| 164 operation->Start(); |
| 165 content::BrowserThread::PostTask( |
| 166 content::BrowserThread::FILE, |
| 167 FROM_HERE, |
| 168 base::Bind( |
| 169 &OperationForTest::Write, operation, base::Bind(&base::DoNothing))); |
| 170 |
| 171 base::RunLoop().RunUntilIdle(); |
| 172 |
| 173 EXPECT_TRUE(base::ContentsEqual(test_image_path_, test_device_path_)); |
134 } | 174 } |
135 #endif | 175 #endif |
136 | 176 |
137 TEST_F(ImageWriterOperationTest, Creation) { | 177 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) { |
138 MockOperationManager manager; | 178 scoped_refptr<OperationForTest> operation( |
139 scoped_refptr<Operation> op( | 179 new OperationForTest(manager_.AsWeakPtr(), |
140 new OperationForTest(manager.AsWeakPtr(), | |
141 kDummyExtensionId, | 180 kDummyExtensionId, |
142 test_device_path_.AsUTF8Unsafe())); | 181 test_device_path_.AsUTF8Unsafe())); |
143 | 182 |
| 183 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); |
| 184 EXPECT_CALL( |
| 185 manager_, |
| 186 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) |
| 187 .Times(AtLeast(1)); |
| 188 EXPECT_CALL( |
| 189 manager_, |
| 190 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0)) |
| 191 .Times(AtLeast(1)); |
| 192 EXPECT_CALL( |
| 193 manager_, |
| 194 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) |
| 195 .Times(AtLeast(1)); |
| 196 |
| 197 FillFile(test_device_path_, kImagePattern, kTestFileSize); |
| 198 operation->SetImagePath(test_image_path_); |
| 199 |
| 200 operation->Start(); |
| 201 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| 202 FROM_HERE, |
| 203 base::Bind(&OperationForTest::VerifyWrite, |
| 204 operation, |
| 205 base::Bind(&base::DoNothing))); |
| 206 |
| 207 base::RunLoop().RunUntilIdle(); |
| 208 } |
| 209 |
| 210 TEST_F(ImageWriterOperationTest, VerifyFileFailure) { |
| 211 scoped_refptr<OperationForTest> operation( |
| 212 new OperationForTest(manager_.AsWeakPtr(), |
| 213 kDummyExtensionId, |
| 214 test_device_path_.AsUTF8Unsafe())); |
| 215 |
| 216 EXPECT_CALL( |
| 217 manager_, |
| 218 OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _)) |
| 219 .Times(1); |
| 220 EXPECT_CALL( |
| 221 manager_, |
| 222 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) |
| 223 .Times(AnyNumber()); |
| 224 EXPECT_CALL( |
| 225 manager_, |
| 226 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) |
| 227 .Times(0); |
| 228 |
| 229 FillFile(test_device_path_, kDevicePattern, kTestFileSize); |
| 230 operation->SetImagePath(test_image_path_); |
| 231 |
| 232 operation->Start(); |
| 233 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| 234 FROM_HERE, |
| 235 base::Bind(&OperationForTest::VerifyWrite, |
| 236 operation, |
| 237 base::Bind(&base::DoNothing))); |
| 238 |
| 239 base::RunLoop().RunUntilIdle(); |
| 240 } |
| 241 |
| 242 // Tests that on creation the operation has the expected state. |
| 243 TEST_F(ImageWriterOperationTest, Creation) { |
| 244 scoped_refptr<Operation> op( |
| 245 new OperationForTest(manager_.AsWeakPtr(), |
| 246 kDummyExtensionId, |
| 247 test_device_path_.AsUTF8Unsafe())); |
| 248 |
144 EXPECT_EQ(0, op->GetProgress()); | 249 EXPECT_EQ(0, op->GetProgress()); |
145 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage()); | 250 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage()); |
146 } | 251 } |
147 | 252 |
148 } // namespace image_writer | 253 } // namespace image_writer |
149 } // namespace extensions | 254 } // namespace extensions |
OLD | NEW |