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