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

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

Issue 170713007: Resubmit of 149313003: Significantly cleans up the ImageWriter Operation class and subclasses. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes comments. 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 10 matching lines...) Expand all
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 virtual void StartImpl() OVERRIDE {}
41
42 // Expose internal stages for testing.
43 void Unzip(const base::Closure& continuation) {
44 Operation::Unzip(continuation);
42 } 45 }
43 46
44 void UnzipStart(scoped_ptr<base::FilePath> zip_file) { 47 void Write(const base::Closure& continuation) {
45 Operation::UnzipStart(zip_file.Pass()); 48 Operation::Write(continuation);
46 } 49 }
47 50
48 void WriteStart() { 51 void VerifyWrite(const base::Closure& continuation) {
49 Operation::WriteStart(); 52 Operation::VerifyWrite(continuation);
50 } 53 }
51 54
52 void VerifyWriteStart() { 55 // Helpers to set-up state for intermediate stages.
53 Operation::VerifyWriteStart(); 56 void SetImagePath(const base::FilePath image_path) {
57 image_path_ = image_path;
54 } 58 }
55 59
56 void Finish() { 60 base::FilePath GetImagePath() { return image_path_; }
57 Operation::Finish(); 61
58 }
59 private: 62 private:
60 virtual ~OperationForTest() {}; 63 virtual ~OperationForTest() {};
61 }; 64 };
62 65
63 class ImageWriterOperationTest : public ImageWriterUnitTestBase { 66 class ImageWriterOperationTest : public ImageWriterUnitTestBase {
64 protected: 67 protected:
65 virtual void SetUp() OVERRIDE { 68 virtual void SetUp() OVERRIDE {
66 ImageWriterUnitTestBase::SetUp(); 69 ImageWriterUnitTestBase::SetUp();
67 70
68 // Create the zip file. 71 // Create the zip file.
69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 72 base::FilePath image_dir = temp_dir_.path().AppendASCII("zip");
70 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), 73 ASSERT_TRUE(base::CreateDirectory(image_dir));
71 &image_file_)); 74 ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_));
72 ASSERT_TRUE(base::CreateTemporaryFile(&zip_file_));
73 75
74 scoped_ptr<char[]> buffer(new char[kTestFileSize]); 76 FillFile(image_path_, kImagePattern, kTestFileSize);
75 memset(buffer.get(), kImagePattern, kTestFileSize);
76 file_util::WriteFile(image_file_, buffer.get(), kTestFileSize);
77 77
78 zip::Zip(temp_dir_.path(), zip_file_, true); 78 zip_file_ = temp_dir_.path().AppendASCII("test_image.zip");
79 ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true));
79 } 80 }
80 81
81 virtual void TearDown() OVERRIDE { 82 virtual void TearDown() OVERRIDE {
82 ImageWriterUnitTestBase::TearDown(); 83 ImageWriterUnitTestBase::TearDown();
83 } 84 }
84 85
85 base::ScopedTempDir temp_dir_; 86 base::FilePath image_path_;
86 base::FilePath image_file_;
87 base::FilePath zip_file_; 87 base::FilePath zip_file_;
88
89 MockOperationManager manager_;
88 }; 90 };
89 91
90 } // namespace 92 } // namespace
91 93
92 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) 94 TEST_F(ImageWriterOperationTest, UnzipNonZipFile) {
93 // Tests a successful unzip.
94 TEST_F(ImageWriterOperationTest, Unzip) {
95 MockOperationManager manager;
96
97 scoped_refptr<OperationForTest> operation( 95 scoped_refptr<OperationForTest> operation(
98 new OperationForTest(manager.AsWeakPtr(), 96 new OperationForTest(manager_.AsWeakPtr(),
99 kDummyExtensionId, 97 kDummyExtensionId,
100 test_device_path_.AsUTF8Unsafe())); 98 test_device_path_.AsUTF8Unsafe()));
101 99
102 scoped_ptr<base::FilePath> zip_file(new base::FilePath(zip_file_)); 100 EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0);
103 101
104 // At least one progress report > 0% and < 100%. 102 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 103
125 content::BrowserThread::PostTask(content::BrowserThread::FILE, 104 operation->Start();
126 FROM_HERE, 105 content::BrowserThread::PostTask(
127 base::Bind(&OperationForTest::UnzipStart, 106 content::BrowserThread::FILE,
128 operation, 107 FROM_HERE,
129 base::Passed(&zip_file))); 108 base::Bind(
109 &OperationForTest::Unzip, operation, base::Bind(&base::DoNothing)));
110
111 base::RunLoop().RunUntilIdle();
112 }
113
114 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);
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)));
130 139
131 base::RunLoop().RunUntilIdle(); 140 base::RunLoop().RunUntilIdle();
132 141
133 EXPECT_TRUE(base::ContentsEqual(image_file_, test_device_path_)); 142 EXPECT_TRUE(base::ContentsEqual(image_path_, operation->GetImagePath()));
143 }
144
145 #if defined(OS_LINUX)
146 TEST_F(ImageWriterOperationTest, WriteImageToDevice) {
147
148 scoped_refptr<OperationForTest> operation(
149 new OperationForTest(manager_.AsWeakPtr(),
150 kDummyExtensionId,
151 test_device_path_.AsUTF8Unsafe()));
152
153 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
154 EXPECT_CALL(manager_,
155 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _))
156 .Times(AtLeast(1));
157 EXPECT_CALL(manager_,
158 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0))
159 .Times(AtLeast(1));
160 EXPECT_CALL(manager_,
161 OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100))
162 .Times(AtLeast(1));
163
164 operation->SetImagePath(test_image_path_);
165
166 operation->Start();
167 content::BrowserThread::PostTask(
168 content::BrowserThread::FILE,
169 FROM_HERE,
170 base::Bind(
171 &OperationForTest::Write, operation, base::Bind(&base::DoNothing)));
172
173 base::RunLoop().RunUntilIdle();
174
175 #if !defined(OS_CHROMEOS)
176 // Chrome OS tests don't actually write to the disk because that's handled by
177 // the DBUS process.
178 EXPECT_TRUE(base::ContentsEqual(test_image_path_, test_device_path_));
179 #endif
134 } 180 }
135 #endif 181 #endif
136 182
137 TEST_F(ImageWriterOperationTest, Creation) { 183 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
138 MockOperationManager manager; 184 // Chrome OS doesn't support verification in the ImageBurner, so these two tests
139 scoped_refptr<Operation> op( 185 // are skipped.
140 new OperationForTest(manager.AsWeakPtr(), 186
187 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) {
188 scoped_refptr<OperationForTest> operation(
189 new OperationForTest(manager_.AsWeakPtr(),
141 kDummyExtensionId, 190 kDummyExtensionId,
142 test_device_path_.AsUTF8Unsafe())); 191 test_device_path_.AsUTF8Unsafe()));
143 192
193 EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
194 EXPECT_CALL(
195 manager_,
196 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _))
197 .Times(AtLeast(1));
198 EXPECT_CALL(
199 manager_,
200 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0))
201 .Times(AtLeast(1));
202 EXPECT_CALL(
203 manager_,
204 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100))
205 .Times(AtLeast(1));
206
207 FillFile(test_device_path_, kImagePattern, kTestFileSize);
208 operation->SetImagePath(test_image_path_);
209
210 operation->Start();
211 content::BrowserThread::PostTask(content::BrowserThread::FILE,
212 FROM_HERE,
213 base::Bind(&OperationForTest::VerifyWrite,
214 operation,
215 base::Bind(&base::DoNothing)));
216
217 base::RunLoop().RunUntilIdle();
218 }
219
220 TEST_F(ImageWriterOperationTest, VerifyFileFailure) {
221 scoped_refptr<OperationForTest> operation(
222 new OperationForTest(manager_.AsWeakPtr(),
223 kDummyExtensionId,
224 test_device_path_.AsUTF8Unsafe()));
225
226 EXPECT_CALL(
227 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 .Times(AnyNumber());
234 EXPECT_CALL(
235 manager_,
236 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100))
237 .Times(0);
238
239 FillFile(test_device_path_, kDevicePattern, kTestFileSize);
240 operation->SetImagePath(test_image_path_);
241
242 operation->Start();
243 content::BrowserThread::PostTask(content::BrowserThread::FILE,
244 FROM_HERE,
245 base::Bind(&OperationForTest::VerifyWrite,
246 operation,
247 base::Bind(&base::DoNothing)));
248
249 base::RunLoop().RunUntilIdle();
250 }
251 #endif
252
253 // Tests that on creation the operation has the expected state.
254 TEST_F(ImageWriterOperationTest, Creation) {
255 scoped_refptr<Operation> op(
256 new OperationForTest(manager_.AsWeakPtr(),
257 kDummyExtensionId,
258 test_device_path_.AsUTF8Unsafe()));
259
144 EXPECT_EQ(0, op->GetProgress()); 260 EXPECT_EQ(0, op->GetProgress());
145 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage()); 261 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage());
146 } 262 }
147 263
148 } // namespace image_writer 264 } // namespace image_writer
149 } // namespace extensions 265 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698