OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/file_path.h" | 5 #include "base/files/file_path.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/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 10 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
11 #include "chrome/browser/download/download_prefs.h" | 11 #include "chrome/browser/download/download_prefs.h" |
12 #include "chrome/browser/download/download_target_info.h" | |
13 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
15 #include "chrome/test/base/testing_pref_service_syncable.h" | 14 #include "chrome/test/base/testing_pref_service_syncable.h" |
16 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
17 #include "content/public/browser/download_interrupt_reasons.h" | 16 #include "content/public/browser/download_interrupt_reasons.h" |
18 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
19 #include "content/public/browser/web_contents_delegate.h" | 18 #include "content/public/browser/web_contents_delegate.h" |
20 #include "content/public/test/mock_download_item.h" | 19 #include "content/public/test/mock_download_item.h" |
21 #include "content/public/test/mock_download_manager.h" | 20 #include "content/public/test/mock_download_manager.h" |
22 #include "content/public/test/test_browser_thread.h" | 21 #include "content/public/test/test_browser_thread.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 ACTION_P(ScheduleCallback, result) { | 58 ACTION_P(ScheduleCallback, result) { |
60 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result)); | 59 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result)); |
61 } | 60 } |
62 | 61 |
63 // Similar to ScheduleCallback, but binds 2 arguments. | 62 // Similar to ScheduleCallback, but binds 2 arguments. |
64 ACTION_P2(ScheduleCallback2, result0, result1) { | 63 ACTION_P2(ScheduleCallback2, result0, result1) { |
65 base::MessageLoop::current()->PostTask( | 64 base::MessageLoop::current()->PostTask( |
66 FROM_HERE, base::Bind(arg0, result0, result1)); | 65 FROM_HERE, base::Bind(arg0, result0, result1)); |
67 } | 66 } |
68 | 67 |
| 68 struct DownloadTarget { |
| 69 base::FilePath target_path; |
| 70 base::FilePath intermediate_path; |
| 71 DownloadItem::TargetDisposition target_disposition; |
| 72 content::DownloadDangerType danger_type; |
| 73 }; |
| 74 |
69 // Subclass of the ChromeDownloadManagerDelegate that uses a mock | 75 // Subclass of the ChromeDownloadManagerDelegate that uses a mock |
70 // DownloadProtectionService. | 76 // DownloadProtectionService. |
71 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate { | 77 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate { |
72 public: | 78 public: |
73 explicit TestChromeDownloadManagerDelegate(Profile* profile) | 79 explicit TestChromeDownloadManagerDelegate(Profile* profile) |
74 : ChromeDownloadManagerDelegate(profile) { | 80 : ChromeDownloadManagerDelegate(profile) { |
75 } | 81 } |
76 | 82 |
77 virtual safe_browsing::DownloadProtectionService* | 83 virtual safe_browsing::DownloadProtectionService* |
78 GetDownloadProtectionService() OVERRIDE { | 84 GetDownloadProtectionService() OVERRIDE { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 content::MockDownloadItem* CreateActiveDownloadItem(int32 id); | 145 content::MockDownloadItem* CreateActiveDownloadItem(int32 id); |
140 | 146 |
141 // Given the relative path |path|, returns the full path under the temporary | 147 // Given the relative path |path|, returns the full path under the temporary |
142 // downloads directory. | 148 // downloads directory. |
143 base::FilePath GetPathInDownloadDir(const char* path); | 149 base::FilePath GetPathInDownloadDir(const char* path); |
144 | 150 |
145 // Set the kDownloadDefaultDirectory user preference to |path|. | 151 // Set the kDownloadDefaultDirectory user preference to |path|. |
146 void SetDefaultDownloadPath(const base::FilePath& path); | 152 void SetDefaultDownloadPath(const base::FilePath& path); |
147 | 153 |
148 void DetermineDownloadTarget(DownloadItem* download, | 154 void DetermineDownloadTarget(DownloadItem* download, |
149 DownloadTargetInfo* result); | 155 DownloadTarget* result); |
150 | 156 |
151 const base::FilePath& default_download_path() const; | 157 const base::FilePath& default_download_path() const; |
152 TestChromeDownloadManagerDelegate* delegate(); | 158 TestChromeDownloadManagerDelegate* delegate(); |
153 content::MockDownloadManager* download_manager(); | 159 content::MockDownloadManager* download_manager(); |
154 DownloadPrefs* download_prefs(); | 160 DownloadPrefs* download_prefs(); |
155 | 161 |
156 private: | 162 private: |
| 163 void OnDownloadTargetDone(DownloadTarget* result, |
| 164 const base::FilePath& target_path, |
| 165 DownloadItem::TargetDisposition disposition, |
| 166 content::DownloadDangerType danger_type, |
| 167 const base::FilePath& intermediate_path); |
| 168 |
157 TestingPrefServiceSyncable* pref_service_; | 169 TestingPrefServiceSyncable* pref_service_; |
158 base::ScopedTempDir test_download_dir_; | 170 base::ScopedTempDir test_download_dir_; |
159 scoped_ptr<content::MockDownloadManager> download_manager_; | 171 scoped_ptr<content::MockDownloadManager> download_manager_; |
160 scoped_refptr<TestChromeDownloadManagerDelegate> delegate_; | 172 scoped_refptr<TestChromeDownloadManagerDelegate> delegate_; |
161 MockWebContentsDelegate web_contents_delegate_; | 173 MockWebContentsDelegate web_contents_delegate_; |
162 }; | 174 }; |
163 | 175 |
164 ChromeDownloadManagerDelegateTest::ChromeDownloadManagerDelegateTest() | 176 ChromeDownloadManagerDelegateTest::ChromeDownloadManagerDelegateTest() |
165 : download_manager_(new ::testing::NiceMock<content::MockDownloadManager>) { | 177 : download_manager_(new ::testing::NiceMock<content::MockDownloadManager>) { |
166 } | 178 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 test_download_dir_.path().AppendASCII(relative_path); | 245 test_download_dir_.path().AppendASCII(relative_path); |
234 return full_path.NormalizePathSeparators(); | 246 return full_path.NormalizePathSeparators(); |
235 } | 247 } |
236 | 248 |
237 void ChromeDownloadManagerDelegateTest::SetDefaultDownloadPath( | 249 void ChromeDownloadManagerDelegateTest::SetDefaultDownloadPath( |
238 const base::FilePath& path) { | 250 const base::FilePath& path) { |
239 pref_service_->SetFilePath(prefs::kDownloadDefaultDirectory, path); | 251 pref_service_->SetFilePath(prefs::kDownloadDefaultDirectory, path); |
240 pref_service_->SetFilePath(prefs::kSaveFileDefaultDirectory, path); | 252 pref_service_->SetFilePath(prefs::kSaveFileDefaultDirectory, path); |
241 } | 253 } |
242 | 254 |
243 void StoreDownloadTargetInfo(const base::Closure& closure, | |
244 DownloadTargetInfo* target_info, | |
245 const base::FilePath& target_path, | |
246 DownloadItem::TargetDisposition target_disposition, | |
247 content::DownloadDangerType danger_type, | |
248 const base::FilePath& intermediate_path) { | |
249 target_info->target_path = target_path; | |
250 target_info->target_disposition = target_disposition; | |
251 target_info->danger_type = danger_type; | |
252 target_info->intermediate_path = intermediate_path; | |
253 closure.Run(); | |
254 } | |
255 | |
256 void ChromeDownloadManagerDelegateTest::DetermineDownloadTarget( | 255 void ChromeDownloadManagerDelegateTest::DetermineDownloadTarget( |
257 DownloadItem* download_item, | 256 DownloadItem* download_item, |
258 DownloadTargetInfo* result) { | 257 DownloadTarget* result) { |
259 base::RunLoop loop_runner; | 258 base::WeakPtrFactory<ChromeDownloadManagerDelegateTest> factory(this); |
260 delegate()->DetermineDownloadTarget( | 259 delegate()->DetermineDownloadTarget( |
261 download_item, | 260 download_item, |
262 base::Bind(&StoreDownloadTargetInfo, loop_runner.QuitClosure(), result)); | 261 base::Bind(&ChromeDownloadManagerDelegateTest::OnDownloadTargetDone, |
263 loop_runner.Run(); | 262 factory.GetWeakPtr(), base::Unretained(result))); |
| 263 base::RunLoop loop_runner; |
| 264 loop_runner.RunUntilIdle(); |
| 265 } |
| 266 |
| 267 void ChromeDownloadManagerDelegateTest::OnDownloadTargetDone( |
| 268 DownloadTarget* result, |
| 269 const base::FilePath& target_path, |
| 270 DownloadItem::TargetDisposition target_disposition, |
| 271 content::DownloadDangerType danger_type, |
| 272 const base::FilePath& intermediate_path) { |
| 273 result->target_path = target_path; |
| 274 result->intermediate_path = intermediate_path; |
| 275 result->target_disposition = target_disposition; |
| 276 result->danger_type = danger_type; |
264 } | 277 } |
265 | 278 |
266 const base::FilePath& ChromeDownloadManagerDelegateTest::default_download_path() | 279 const base::FilePath& ChromeDownloadManagerDelegateTest::default_download_path() |
267 const { | 280 const { |
268 return test_download_dir_.path(); | 281 return test_download_dir_.path(); |
269 } | 282 } |
270 | 283 |
271 TestChromeDownloadManagerDelegate* | 284 TestChromeDownloadManagerDelegate* |
272 ChromeDownloadManagerDelegateTest::delegate() { | 285 ChromeDownloadManagerDelegateTest::delegate() { |
273 return delegate_.get(); | 286 return delegate_.get(); |
(...skipping 27 matching lines...) Expand all Loading... |
301 EXPECT_CALL(*automatic_download, GetURL()) | 314 EXPECT_CALL(*automatic_download, GetURL()) |
302 .Times(::testing::AnyNumber()) | 315 .Times(::testing::AnyNumber()) |
303 .WillRepeatedly(ReturnRef(download_url)); | 316 .WillRepeatedly(ReturnRef(download_url)); |
304 EXPECT_CALL(*automatic_download, GetTargetDisposition()) | 317 EXPECT_CALL(*automatic_download, GetTargetDisposition()) |
305 .Times(::testing::AnyNumber()) | 318 .Times(::testing::AnyNumber()) |
306 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | 319 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); |
307 | 320 |
308 { | 321 { |
309 // When the prompt is displayed for the first download, the user selects a | 322 // When the prompt is displayed for the first download, the user selects a |
310 // path in a different directory. | 323 // path in a different directory. |
311 DownloadTargetInfo result; | 324 DownloadTarget result; |
312 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); | 325 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); |
313 base::FilePath user_selected_path(GetPathInDownloadDir("bar/baz.txt")); | 326 base::FilePath user_selected_path(GetPathInDownloadDir("bar/baz.txt")); |
314 EXPECT_CALL(*delegate(), | 327 EXPECT_CALL(*delegate(), |
315 MockPromptUserForDownloadPath(save_as_download.get(), | 328 MockPromptUserForDownloadPath(save_as_download.get(), |
316 expected_prompt_path, _)) | 329 expected_prompt_path, _)) |
317 .WillOnce(Return(user_selected_path)); | 330 .WillOnce(Return(user_selected_path)); |
318 DetermineDownloadTarget(save_as_download.get(), &result); | 331 DetermineDownloadTarget(save_as_download.get(), &result); |
319 EXPECT_EQ(user_selected_path, result.target_path); | 332 EXPECT_EQ(user_selected_path, result.target_path); |
320 VerifyAndClearExpectations(); | 333 VerifyAndClearExpectations(); |
321 } | 334 } |
322 | 335 |
323 { | 336 { |
324 // The prompt path for the second download is the user selected directroy | 337 // The prompt path for the second download is the user selected directroy |
325 // from the previous download. | 338 // from the previous download. |
326 DownloadTargetInfo result; | 339 DownloadTarget result; |
327 base::FilePath expected_prompt_path(GetPathInDownloadDir("bar/foo.txt")); | 340 base::FilePath expected_prompt_path(GetPathInDownloadDir("bar/foo.txt")); |
328 EXPECT_CALL(*delegate(), | 341 EXPECT_CALL(*delegate(), |
329 MockPromptUserForDownloadPath(save_as_download.get(), | 342 MockPromptUserForDownloadPath(save_as_download.get(), |
330 expected_prompt_path, _)) | 343 expected_prompt_path, _)) |
331 .WillOnce(Return(base::FilePath())); | 344 .WillOnce(Return(base::FilePath())); |
332 DetermineDownloadTarget(save_as_download.get(), &result); | 345 DetermineDownloadTarget(save_as_download.get(), &result); |
333 VerifyAndClearExpectations(); | 346 VerifyAndClearExpectations(); |
334 } | 347 } |
335 | 348 |
336 { | 349 { |
337 // Start an automatic download. This one should get the default download | 350 // Start an automatic download. This one should get the default download |
338 // path since the last download path only affects Save As downloads. | 351 // path since the last download path only affects Save As downloads. |
339 DownloadTargetInfo result; | 352 DownloadTarget result; |
340 base::FilePath expected_path(GetPathInDownloadDir("foo.txt")); | 353 base::FilePath expected_path(GetPathInDownloadDir("foo.txt")); |
341 DetermineDownloadTarget(automatic_download.get(), &result); | 354 DetermineDownloadTarget(automatic_download.get(), &result); |
342 EXPECT_EQ(expected_path, result.target_path); | 355 EXPECT_EQ(expected_path, result.target_path); |
343 VerifyAndClearExpectations(); | 356 VerifyAndClearExpectations(); |
344 } | 357 } |
345 | 358 |
346 { | 359 { |
347 // The prompt path for the next download should be the default. | 360 // The prompt path for the next download should be the default. |
348 download_prefs()->SetSaveFilePath(download_prefs()->DownloadPath()); | 361 download_prefs()->SetSaveFilePath(download_prefs()->DownloadPath()); |
349 DownloadTargetInfo result; | 362 DownloadTarget result; |
350 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); | 363 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); |
351 EXPECT_CALL(*delegate(), | 364 EXPECT_CALL(*delegate(), |
352 MockPromptUserForDownloadPath(save_as_download.get(), | 365 MockPromptUserForDownloadPath(save_as_download.get(), |
353 expected_prompt_path, _)) | 366 expected_prompt_path, _)) |
354 .WillOnce(Return(base::FilePath())); | 367 .WillOnce(Return(base::FilePath())); |
355 DetermineDownloadTarget(save_as_download.get(), &result); | 368 DetermineDownloadTarget(save_as_download.get(), &result); |
356 VerifyAndClearExpectations(); | 369 VerifyAndClearExpectations(); |
357 } | 370 } |
358 } | 371 } |
OLD | NEW |