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

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

Issue 336923002: Adds API test for imageWriterPrivate.writeFromFile (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Chrome OS fixes. Created 6 years, 5 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 }; 64 };
65 65
66 class ImageWriterOperationTest : public ImageWriterUnitTestBase { 66 class ImageWriterOperationTest : public ImageWriterUnitTestBase {
67 protected: 67 protected:
68 ImageWriterOperationTest() 68 ImageWriterOperationTest()
69 : profile_(new TestingProfile), manager_(profile_.get()) {} 69 : profile_(new TestingProfile), manager_(profile_.get()) {}
70 virtual void SetUp() OVERRIDE { 70 virtual void SetUp() OVERRIDE {
71 ImageWriterUnitTestBase::SetUp(); 71 ImageWriterUnitTestBase::SetUp();
72 72
73 // Create the zip file. 73 // Create the zip file.
74 base::FilePath image_dir = temp_dir_.path().AppendASCII("zip"); 74 base::FilePath image_dir = test_utils_.GetTempDir().AppendASCII("zip");
75 ASSERT_TRUE(base::CreateDirectory(image_dir)); 75 ASSERT_TRUE(base::CreateDirectory(image_dir));
76 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_)); 76 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_));
77 77
78 FillFile(image_path_, kImagePattern, kTestFileSize); 78 test_utils_.FillFile(image_path_, kImagePattern, kTestFileSize);
79 79
80 zip_file_ = temp_dir_.path().AppendASCII("test_image.zip"); 80 zip_file_ = test_utils_.GetTempDir().AppendASCII("test_image.zip");
81 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true)); 81 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true));
82 82
83 // Operation setup. 83 // Operation setup.
84 operation_ = new OperationForTest(manager_.AsWeakPtr(), 84 operation_ =
85 kDummyExtensionId, 85 new OperationForTest(manager_.AsWeakPtr(),
86 test_device_path_.AsUTF8Unsafe()); 86 kDummyExtensionId,
87 client_ = FakeImageWriterClient::Create(); 87 test_utils_.GetDevicePath().AsUTF8Unsafe());
88 operation_->SetImagePath(test_image_path_); 88 operation_->SetImagePath(test_utils_.GetImagePath());
89 } 89 }
90 90
91 virtual void TearDown() OVERRIDE { 91 virtual void TearDown() OVERRIDE {
92 // Ensure all callbacks have been destroyed and cleanup occurs. 92 // Ensure all callbacks have been destroyed and cleanup occurs.
93 client_->Shutdown();
94 operation_->Cancel(); 93 operation_->Cancel();
95 94
96 ImageWriterUnitTestBase::TearDown(); 95 ImageWriterUnitTestBase::TearDown();
97 } 96 }
98 97
99 base::FilePath image_path_; 98 base::FilePath image_path_;
100 base::FilePath zip_file_; 99 base::FilePath zip_file_;
101 100
102 scoped_ptr<TestingProfile> profile_; 101 scoped_ptr<TestingProfile> profile_;
103 102
104 MockOperationManager manager_; 103 MockOperationManager manager_;
105 scoped_refptr<FakeImageWriterClient> client_;
106 scoped_refptr<OperationForTest> operation_; 104 scoped_refptr<OperationForTest> operation_;
107 }; 105 };
108 106
109 } // namespace 107 } // namespace
110 108
111 // Unizpping a non-zip should do nothing. 109 // Unizpping a non-zip should do nothing.
112 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) { 110 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) {
113 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0); 111 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0);
114 112
115 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); 113 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 base::Bind( 145 base::Bind(
148 &OperationForTest::Unzip, operation_, base::Bind(&base::DoNothing))); 146 &OperationForTest::Unzip, operation_, base::Bind(&base::DoNothing)));
149 147
150 base::RunLoop().RunUntilIdle(); 148 base::RunLoop().RunUntilIdle();
151 149
152 EXPECT_TRUE(base::ContentsEqual(image_path_, operation_->GetImagePath())); 150 EXPECT_TRUE(base::ContentsEqual(image_path_, operation_->GetImagePath()));
153 } 151 }
154 152
155 #if defined(OS_LINUX) 153 #if defined(OS_LINUX)
156 TEST_F(ImageWriterOperationTest, WriteImageToDevice) { 154 TEST_F(ImageWriterOperationTest, WriteImageToDevice) {
157 #if !defined(OS_CHROMEOS)
158 operation_->SetUtilityClientForTesting(client_);
159 #endif
160
161 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); 155 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
162 EXPECT_CALL(manager_, 156 EXPECT_CALL(manager_,
163 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _)) 157 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _))
164 .Times(AtLeast(1)); 158 .Times(AtLeast(1));
165 EXPECT_CALL(manager_, 159 EXPECT_CALL(manager_,
166 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0)) 160 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0))
167 .Times(AtLeast(1)); 161 .Times(AtLeast(1));
168 EXPECT_CALL(manager_, 162 EXPECT_CALL(manager_,
169 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100)) 163 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100))
170 .Times(AtLeast(1)); 164 .Times(AtLeast(1));
171 165
172 operation_->Start(); 166 operation_->Start();
173 content::BrowserThread::PostTask( 167 content::BrowserThread::PostTask(
174 content::BrowserThread::FILE, 168 content::BrowserThread::FILE,
175 FROM_HERE, 169 FROM_HERE,
176 base::Bind( 170 base::Bind(
177 &OperationForTest::Write, operation_, base::Bind(&base::DoNothing))); 171 &OperationForTest::Write, operation_, base::Bind(&base::DoNothing)));
178 172
179 base::RunLoop().RunUntilIdle(); 173 base::RunLoop().RunUntilIdle();
180 174
181 #if !defined(OS_CHROMEOS) 175 #if !defined(OS_CHROMEOS)
182 client_->Progress(0); 176 test_utils_.GetUtilityClient()->Progress(0);
183 client_->Progress(kTestFileSize / 2); 177 test_utils_.GetUtilityClient()->Progress(kTestFileSize / 2);
184 client_->Progress(kTestFileSize); 178 test_utils_.GetUtilityClient()->Progress(kTestFileSize);
185 client_->Success(); 179 test_utils_.GetUtilityClient()->Success();
186 180
187 base::RunLoop().RunUntilIdle(); 181 base::RunLoop().RunUntilIdle();
188 #endif 182 #endif
189 } 183 }
190 #endif 184 #endif
191 185
192 #if !defined(OS_CHROMEOS) 186 #if !defined(OS_CHROMEOS)
193 // Chrome OS doesn't support verification in the ImageBurner, so these two tests 187 // Chrome OS doesn't support verification in the ImageBurner, so these two tests
194 // are skipped. 188 // are skipped.
195 189
196 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) { 190 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) {
197 operation_->SetUtilityClientForTesting(client_);
198
199 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0); 191 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
200 EXPECT_CALL( 192 EXPECT_CALL(
201 manager_, 193 manager_,
202 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) 194 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _))
203 .Times(AtLeast(1)); 195 .Times(AtLeast(1));
204 EXPECT_CALL( 196 EXPECT_CALL(
205 manager_, 197 manager_,
206 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0)) 198 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0))
207 .Times(AtLeast(1)); 199 .Times(AtLeast(1));
208 EXPECT_CALL( 200 EXPECT_CALL(
209 manager_, 201 manager_,
210 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) 202 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100))
211 .Times(AtLeast(1)); 203 .Times(AtLeast(1));
212 204
213 FillFile(test_device_path_, kImagePattern, kTestFileSize); 205 test_utils_.FillFile(
206 test_utils_.GetDevicePath(), kImagePattern, kTestFileSize);
214 207
215 operation_->Start(); 208 operation_->Start();
216 content::BrowserThread::PostTask(content::BrowserThread::FILE, 209 content::BrowserThread::PostTask(content::BrowserThread::FILE,
217 FROM_HERE, 210 FROM_HERE,
218 base::Bind(&OperationForTest::VerifyWrite, 211 base::Bind(&OperationForTest::VerifyWrite,
219 operation_, 212 operation_,
220 base::Bind(&base::DoNothing))); 213 base::Bind(&base::DoNothing)));
221 214
222 base::RunLoop().RunUntilIdle(); 215 base::RunLoop().RunUntilIdle();
223 216
224 client_->Progress(0); 217 #if !defined(OS_CHROMEOS)
225 client_->Progress(kTestFileSize / 2); 218 test_utils_.GetUtilityClient()->Progress(0);
226 client_->Progress(kTestFileSize); 219 test_utils_.GetUtilityClient()->Progress(kTestFileSize / 2);
227 client_->Success(); 220 test_utils_.GetUtilityClient()->Progress(kTestFileSize);
221 test_utils_.GetUtilityClient()->Success();
222 #endif
228 223
229 base::RunLoop().RunUntilIdle(); 224 base::RunLoop().RunUntilIdle();
230 } 225 }
231 226
232 TEST_F(ImageWriterOperationTest, VerifyFileFailure) { 227 TEST_F(ImageWriterOperationTest, VerifyFileFailure) {
233 operation_->SetUtilityClientForTesting(client_);
234
235 EXPECT_CALL( 228 EXPECT_CALL(
236 manager_, 229 manager_,
237 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _)) 230 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _))
238 .Times(AnyNumber()); 231 .Times(AnyNumber());
239 EXPECT_CALL( 232 EXPECT_CALL(
240 manager_, 233 manager_,
241 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100)) 234 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100))
242 .Times(0); 235 .Times(0);
243 EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0); 236 EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0);
244 EXPECT_CALL( 237 EXPECT_CALL(
245 manager_, 238 manager_,
246 OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _)) 239 OnError(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _, _))
247 .Times(1); 240 .Times(1);
248 241
249 FillFile(test_device_path_, kDevicePattern, kTestFileSize); 242 test_utils_.FillFile(
243 test_utils_.GetDevicePath(), kDevicePattern, kTestFileSize);
250 244
251 operation_->Start(); 245 operation_->Start();
252 content::BrowserThread::PostTask(content::BrowserThread::FILE, 246 content::BrowserThread::PostTask(content::BrowserThread::FILE,
253 FROM_HERE, 247 FROM_HERE,
254 base::Bind(&OperationForTest::VerifyWrite, 248 base::Bind(&OperationForTest::VerifyWrite,
255 operation_, 249 operation_,
256 base::Bind(&base::DoNothing))); 250 base::Bind(&base::DoNothing)));
257 251
258 base::RunLoop().RunUntilIdle(); 252 base::RunLoop().RunUntilIdle();
259 253
260 client_->Progress(0); 254 test_utils_.GetUtilityClient()->Progress(0);
261 client_->Progress(kTestFileSize / 2); 255 test_utils_.GetUtilityClient()->Progress(kTestFileSize / 2);
262 client_->Error(error::kVerificationFailed); 256 test_utils_.GetUtilityClient()->Error(error::kVerificationFailed);
263 257
264 base::RunLoop().RunUntilIdle(); 258 base::RunLoop().RunUntilIdle();
265 } 259 }
266 #endif 260 #endif
267 261
268 // Tests that on creation the operation_ has the expected state. 262 // Tests that on creation the operation_ has the expected state.
269 TEST_F(ImageWriterOperationTest, Creation) { 263 TEST_F(ImageWriterOperationTest, Creation) {
270 EXPECT_EQ(0, operation_->GetProgress()); 264 EXPECT_EQ(0, operation_->GetProgress());
271 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, operation_->GetStage()); 265 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, operation_->GetStage());
272 } 266 }
273 267
274 } // namespace image_writer 268 } // namespace image_writer
275 } // namespace extensions 269 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698