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

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

Issue 149313003: Significantly cleans up the ImageWriter Operation class and subclasses. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes cross-compilation and test issues. 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) && !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_));
134 } 178 }
135 #endif 179 #endif
136 180
137 TEST_F(ImageWriterOperationTest, Creation) { 181 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
138 MockOperationManager manager; 182 // Chrome OS doesn't support verification in the ImageBurner, so these two tests
139 scoped_refptr<Operation> op( 183 // are skipped.
140 new OperationForTest(manager.AsWeakPtr(), 184
185 TEST_F(ImageWriterOperationTest, VerifyFileSuccess) {
186 scoped_refptr<OperationForTest> operation(
187 new OperationForTest(manager_.AsWeakPtr(),
141 kDummyExtensionId, 188 kDummyExtensionId,
142 test_device_path_.AsUTF8Unsafe())); 189 test_device_path_.AsUTF8Unsafe()));
143 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
144 EXPECT_EQ(0, op->GetProgress()); 258 EXPECT_EQ(0, op->GetProgress());
145 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage()); 259 EXPECT_EQ(image_writer_api::STAGE_UNKNOWN, op->GetStage());
146 } 260 }
147 261
148 } // namespace image_writer 262 } // namespace image_writer
149 } // namespace extensions 263 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698