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