Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Side by Side Diff: chrome/browser/extensions/api/image_writer_private/operation_unittest.cc

Issue 61643015: Adds imageWriterPrivate support for Windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes test cleanup ordering for Chrome OS. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 70
71 // Create the zip file. 71 // Create the zip file.
72 base::FilePath image_dir = temp_dir_.path().AppendASCII("zip"); 72 base::FilePath image_dir = temp_dir_.path().AppendASCII("zip");
73 ASSERT_TRUE(base::CreateDirectory(image_dir)); 73 ASSERT_TRUE(base::CreateDirectory(image_dir));
74 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_)); 74 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_));
75 75
76 FillFile(image_path_, kImagePattern, kTestFileSize); 76 FillFile(image_path_, kImagePattern, kTestFileSize);
77 77
78 zip_file_ = temp_dir_.path().AppendASCII("test_image.zip"); 78 zip_file_ = temp_dir_.path().AppendASCII("test_image.zip");
79 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true)); 79 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true));
80
81 // Operation setup.
82 operation_ = new OperationForTest(manager_.AsWeakPtr(),
83 kDummyExtensionId,
84 test_device_path_.AsUTF8Unsafe());
85 client_ = FakeImageWriterClient::Create();
86 operation_->SetImagePath(test_image_path_);
80 } 87 }
81 88
82 virtual void TearDown() OVERRIDE { 89 virtual void TearDown() OVERRIDE {
90 // Ensure all callbacks have been destroyed and cleanup occurs.
91 client_->Shutdown();
92 operation_->Cancel();
93
83 ImageWriterUnitTestBase::TearDown(); 94 ImageWriterUnitTestBase::TearDown();
84 } 95 }
85 96
86 base::FilePath image_path_; 97 base::FilePath image_path_;
87 base::FilePath zip_file_; 98 base::FilePath zip_file_;
88 99
89 MockOperationManager manager_; 100 MockOperationManager manager_;
101 scoped_refptr<FakeImageWriterClient> client_;
102 scoped_refptr<OperationForTest> operation_;
90 }; 103 };
91 104
92 } // namespace 105 } // namespace
93 106
107 // Unizpping a non-zip should do nothing.
94 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) { 108 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) {
95 scoped_refptr<OperationForTest> operation(
96 new OperationForTest(manager_.AsWeakPtr(),
97 kDummyExtensionId,
98 test_device_path_.AsUTF8Unsafe()));
99
100 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0); 109 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0);
101 110
102 operation->SetImagePath(test_image_path_); 111 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
112 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0);
113 EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0);
103 114
104 operation->Start(); 115 operation_->Start();
105 content::BrowserThread::PostTask( 116 content::BrowserThread::PostTask(
106 content::BrowserThread::FILE, 117 content::BrowserThread::FILE,
107 FROM_HERE, 118 FROM_HERE,
108 base::Bind( 119 base::Bind(
109 &OperationForTest::Unzip, operation, base::Bind(&base::DoNothing))); 120 &OperationForTest::Unzip, operation_, base::Bind(&base::DoNothing)));
110 121
111 base::RunLoop().RunUntilIdle(); 122 base::RunLoop().RunUntilIdle();
112 } 123 }
113 124
114 TEST_F(ImageWriterOperationTest, UnzipZipFile) { 125 TEST_F(ImageWriterOperationTest, UnzipZipFile) {
115 scoped_refptr<OperationForTest> operation(
116 new OperationForTest(manager_.AsWeakPtr(),
117 kDummyExtensionId,
118 test_device_path_.AsUTF8Unsafe()));
119
120 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); 126 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
121 EXPECT_CALL(manager_, 127 EXPECT_CALL(manager_,
122 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, _)) 128 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, _))
123 .Times(AtLeast(1)); 129 .Times(AtLeast(1));
124 EXPECT_CALL(manager_, 130 EXPECT_CALL(manager_,
125 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 0)) 131 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 0))
126 .Times(AtLeast(1)); 132 .Times(AtLeast(1));
127 EXPECT_CALL(manager_, 133 EXPECT_CALL(manager_,
128 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 100)) 134 OnProgress(kDummyExtensionId, image_writer_api::STAGE_UNZIP, 100))
129 .Times(AtLeast(1)); 135 .Times(AtLeast(1));
130 136
131 operation->SetImagePath(zip_file_); 137 operation_->SetImagePath(zip_file_);
132 138
133 operation->Start(); 139 operation_->Start();
134 content::BrowserThread::PostTask( 140 content::BrowserThread::PostTask(
135 content::BrowserThread::FILE, 141 content::BrowserThread::FILE,
136 FROM_HERE, 142 FROM_HERE,
137 base::Bind( 143 base::Bind(
138 &OperationForTest::Unzip, operation, base::Bind(&base::DoNothing))); 144 &OperationForTest::Unzip, operation_, base::Bind(&base::DoNothing)));
139 145
140 base::RunLoop().RunUntilIdle(); 146 base::RunLoop().RunUntilIdle();
141 147
142 EXPECT_TRUE(base::ContentsEqual(image_path_, operation->GetImagePath())); 148 EXPECT_TRUE(base::ContentsEqual(image_path_, operation_->GetImagePath()));
143 } 149 }
144 150
145 #if defined(OS_LINUX) 151 #if defined(OS_LINUX)
146 TEST_F(ImageWriterOperationTest, WriteImageToDevice) { 152 TEST_F(ImageWriterOperationTest, WriteImageToDevice) {
147 153 #if !defined(OS_CHROMEOS)
148 scoped_refptr<OperationForTest> operation( 154 operation_->SetUtilityClientForTesting(client_);
149 new OperationForTest(manager_.AsWeakPtr(), 155 #endif
150 kDummyExtensionId,
151 test_device_path_.AsUTF8Unsafe()));
152 156
153 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); 157 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
154 EXPECT_CALL(manager_, 158 EXPECT_CALL(manager_,
155 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _)) 159 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _))
156 .Times(AtLeast(1)); 160 .Times(AtLeast(1));
157 EXPECT_CALL(manager_, 161 EXPECT_CALL(manager_,
158 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0)) 162 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0))
159 .Times(AtLeast(1)); 163 .Times(AtLeast(1));
160 EXPECT_CALL(manager_, 164 EXPECT_CALL(manager_,
161 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100)) 165 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100))
162 .Times(AtLeast(1)); 166 .Times(AtLeast(1));
163 167
164 operation->SetImagePath(test_image_path_); 168 operation_->Start();
165
166 operation->Start();
167 content::BrowserThread::PostTask( 169 content::BrowserThread::PostTask(
168 content::BrowserThread::FILE, 170 content::BrowserThread::FILE,
169 FROM_HERE, 171 FROM_HERE,
170 base::Bind( 172 base::Bind(
171 &OperationForTest::Write, operation, base::Bind(&base::DoNothing))); 173 &OperationForTest::Write, operation_, base::Bind(&base::DoNothing)));
172 174
173 base::RunLoop().RunUntilIdle(); 175 base::RunLoop().RunUntilIdle();
174 176
175 #if !defined(OS_CHROMEOS) 177 #if !defined(OS_CHROMEOS)
176 // Chrome OS tests don't actually write to the disk because that's handled by 178 client_->Progress(0);
177 // the DBUS process. 179 client_->Progress(kTestFileSize / 2);
178 EXPECT_TRUE(base::ContentsEqual(test_image_path_, test_device_path_)); 180 client_->Progress(kTestFileSize);
181 client_->Success();
182
183 base::RunLoop().RunUntilIdle();
179 #endif 184 #endif
180 } 185 }
181 #endif 186 #endif
182 187
183 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) 188 #if !defined(OS_CHROMEOS)
184 // Chrome OS doesn't support verification in the ImageBurner, so these two tests 189 // Chrome OS doesn't support verification in the ImageBurner, so these two tests
185 // are skipped. 190 // are skipped.
186 191
187 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) { 192 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) {
188 scoped_refptr<OperationForTest> operation( 193 operation_->SetUtilityClientForTesting(client_);
189 new OperationForTest(manager_.AsWeakPtr(),
190 kDummyExtensionId,
191 test_device_path_.AsUTF8Unsafe()));
192 194
193 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); 195 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
194 EXPECT_CALL( 196 EXPECT_CALL(
195 manager_, 197 manager_,
196 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) 198 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _))
197 .Times(AtLeast(1)); 199 .Times(AtLeast(1));
198 EXPECT_CALL( 200 EXPECT_CALL(
199 manager_, 201 manager_,
200 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0)) 202 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0))
201 .Times(AtLeast(1)); 203 .Times(AtLeast(1));
202 EXPECT_CALL( 204 EXPECT_CALL(
203 manager_, 205 manager_,
204 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) 206 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100))
205 .Times(AtLeast(1)); 207 .Times(AtLeast(1));
206 208
207 FillFile(test_device_path_, kImagePattern, kTestFileSize); 209 FillFile(test_device_path_, kImagePattern, kTestFileSize);
208 operation->SetImagePath(test_image_path_);
209 210
210 operation->Start(); 211 operation_->Start();
211 content::BrowserThread::PostTask(content::BrowserThread::FILE, 212 content::BrowserThread::PostTask(content::BrowserThread::FILE,
212 FROM_HERE, 213 FROM_HERE,
213 base::Bind(&OperationForTest::VerifyWrite, 214 base::Bind(&OperationForTest::VerifyWrite,
214 operation, 215 operation_,
215 base::Bind(&base::DoNothing))); 216 base::Bind(&base::DoNothing)));
216 217
217 base::RunLoop().RunUntilIdle(); 218 base::RunLoop().RunUntilIdle();
219
220 client_->Progress(0);
221 client_->Progress(kTestFileSize / 2);
222 client_->Progress(kTestFileSize);
223 client_->Success();
224
225 base::RunLoop().RunUntilIdle();
218 } 226 }
219 227
220 TEST_F(ImageWriterOperationTest, VerifyFileFailure) { 228 TEST_F(ImageWriterOperationTest, VerifyFileFailure) {
221 scoped_refptr<OperationForTest> operation( 229 operation_->SetUtilityClientForTesting(client_);
222 new OperationForTest(manager_.AsWeakPtr(),
223 kDummyExtensionId,
224 test_device_path_.AsUTF8Unsafe()));
225 230
226 EXPECT_CALL( 231 EXPECT_CALL(
227 manager_, 232 manager_,
228 OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _))
229 .Times(1);
230 EXPECT_CALL(
231 manager_,
232 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) 233 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _))
233 .Times(AnyNumber()); 234 .Times(AnyNumber());
234 EXPECT_CALL( 235 EXPECT_CALL(
235 manager_, 236 manager_,
236 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) 237 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100))
237 .Times(0); 238 .Times(0);
239 EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0);
240 EXPECT_CALL(
241 manager_,
242 OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _))
243 .Times(1);
238 244
239 FillFile(test_device_path_, kDevicePattern, kTestFileSize); 245 FillFile(test_device_path_, kDevicePattern, kTestFileSize);
240 operation->SetImagePath(test_image_path_);
241 246
242 operation->Start(); 247 operation_->Start();
243 content::BrowserThread::PostTask(content::BrowserThread::FILE, 248 content::BrowserThread::PostTask(content::BrowserThread::FILE,
244 FROM_HERE, 249 FROM_HERE,
245 base::Bind(&OperationForTest::VerifyWrite, 250 base::Bind(&OperationForTest::VerifyWrite,
246 operation, 251 operation_,
247 base::Bind(&base::DoNothing))); 252 base::Bind(&base::DoNothing)));
248 253
249 base::RunLoop().RunUntilIdle(); 254 base::RunLoop().RunUntilIdle();
255
256 client_->Progress(0);
257 client_->Progress(kTestFileSize / 2);
258 client_->Error(error::kVerificationFailed);
259
260 base::RunLoop().RunUntilIdle();
250 } 261 }
251 #endif 262 #endif
252 263
253 // Tests that on creation the operation has the expected state. 264 // Tests that on creation the operation_ has the expected state.
254 TEST_F(ImageWriterOperationTest, Creation) { 265 TEST_F(ImageWriterOperationTest, Creation) {
255 scoped_refptr<Operation> op( 266 EXPECT_EQ(0, operation_->GetProgress());
256 new OperationForTest(manager_.AsWeakPtr(), 267 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, operation_->GetStage());
257 kDummyExtensionId,
258 test_device_path_.AsUTF8Unsafe()));
259
260 EXPECT_EQ(0, op->GetProgress());
261 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage());
262 } 268 }
263 269
264 } // namespace image_writer 270 } // namespace image_writer
265 } // namespace extensions 271 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698