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/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/observer_list.h" | 8 #include "base/observer_list.h" |
9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/value_conversions.h" | 12 #include "base/value_conversions.h" |
13 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 13 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
14 #include "chrome/browser/download/download_prefs.h" | 14 #include "chrome/browser/download/download_prefs.h" |
15 #include "chrome/browser/download/download_util.h" | 15 #include "chrome/browser/download/download_util.h" |
16 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
17 #include "chrome/browser/safe_browsing/download_protection_service.h" | 17 #include "chrome/browser/safe_browsing/download_protection_service.h" |
18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
19 #include "chrome/common/extensions/extension_switch_utils.h" | 19 #include "chrome/common/extensions/extension_switch_utils.h" |
20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
21 #include "chrome/test/base/testing_pref_service.h" | 22 #include "chrome/test/base/testing_pref_service.h" |
22 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 24 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/browser/web_contents_delegate.h" |
23 #include "content/public/test/mock_download_item.h" | 26 #include "content/public/test/mock_download_item.h" |
24 #include "content/public/test/mock_download_manager.h" | 27 #include "content/public/test/mock_download_manager.h" |
25 #include "content/public/test/test_browser_thread.h" | 28 #include "content/public/test/test_browser_thread.h" |
| 29 #include "content/public/test/test_renderer_host.h" |
| 30 #include "content/public/test/web_contents_tester.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
28 | 33 |
29 using content::DownloadItem; | 34 using content::DownloadItem; |
30 using safe_browsing::DownloadProtectionService; | 35 using safe_browsing::DownloadProtectionService; |
31 using ::testing::AtMost; | 36 using ::testing::AtMost; |
32 using ::testing::Invoke; | 37 using ::testing::Invoke; |
33 using ::testing::Return; | 38 using ::testing::Return; |
34 using ::testing::ReturnPointee; | 39 using ::testing::ReturnPointee; |
35 using ::testing::ReturnRef; | 40 using ::testing::ReturnRef; |
36 using ::testing::ReturnRefOfCopy; | 41 using ::testing::ReturnRefOfCopy; |
37 using ::testing::WithArg; | 42 using ::testing::WithArg; |
38 using ::testing::_; | 43 using ::testing::_; |
39 | 44 |
40 namespace { | 45 namespace { |
41 | 46 |
| 47 class MockWebContentsDelegate : public content::WebContentsDelegate { |
| 48 public: |
| 49 virtual ~MockWebContentsDelegate() {} |
| 50 }; |
| 51 |
42 // Google Mock action that posts a task to the current message loop that invokes | 52 // Google Mock action that posts a task to the current message loop that invokes |
43 // the first argument of the mocked method as a callback. Said argument must be | 53 // the first argument of the mocked method as a callback. Said argument must be |
44 // a base::Callback<void(ParamType)>. |result| must be of |ParamType| and is | 54 // a base::Callback<void(ParamType)>. |result| must be of |ParamType| and is |
45 // bound as that parameter. | 55 // bound as that parameter. |
46 // Example: | 56 // Example: |
47 // class FooClass { | 57 // class FooClass { |
48 // public: | 58 // public: |
49 // virtual void Foo(base::Callback<void(bool)> callback); | 59 // virtual void Foo(base::Callback<void(bool)> callback); |
50 // }; | 60 // }; |
51 // ... | 61 // ... |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 #endif | 216 #endif |
207 | 217 |
208 private: | 218 private: |
209 ~TestChromeDownloadManagerDelegate() {} | 219 ~TestChromeDownloadManagerDelegate() {} |
210 | 220 |
211 #if defined(ENABLE_SAFE_BROWSING) | 221 #if defined(ENABLE_SAFE_BROWSING) |
212 scoped_ptr<TestDownloadProtectionService> download_protection_service_; | 222 scoped_ptr<TestDownloadProtectionService> download_protection_service_; |
213 #endif | 223 #endif |
214 }; | 224 }; |
215 | 225 |
216 class ChromeDownloadManagerDelegateTest : public ::testing::Test { | 226 class ChromeDownloadManagerDelegateTest : |
| 227 public ChromeRenderViewHostTestHarness { |
217 public: | 228 public: |
218 ChromeDownloadManagerDelegateTest(); | 229 ChromeDownloadManagerDelegateTest(); |
219 | 230 |
220 // ::testing::Test | 231 // ::testing::Test |
221 virtual void SetUp() OVERRIDE; | 232 virtual void SetUp() OVERRIDE; |
222 virtual void TearDown() OVERRIDE; | 233 virtual void TearDown() OVERRIDE; |
223 | 234 |
224 // Verifies and clears test expectations for |delegate_| and | 235 // Verifies and clears test expectations for |delegate_| and |
225 // |download_manager_|. | 236 // |download_manager_|. |
226 void VerifyAndClearExpectations(); | 237 void VerifyAndClearExpectations(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 | 272 |
262 private: | 273 private: |
263 // Verifies that |target_path|, |disposition|, |danger_type| and | 274 // Verifies that |target_path|, |disposition|, |danger_type| and |
264 // |intermediate_path| matches the expectations of |test_case|. | 275 // |intermediate_path| matches the expectations of |test_case|. |
265 void DownloadTargetVerifier(const DownloadTestCase* test_case, | 276 void DownloadTargetVerifier(const DownloadTestCase* test_case, |
266 const FilePath& target_path, | 277 const FilePath& target_path, |
267 DownloadItem::TargetDisposition disposition, | 278 DownloadItem::TargetDisposition disposition, |
268 content::DownloadDangerType danger_type, | 279 content::DownloadDangerType danger_type, |
269 const FilePath& intermediate_path); | 280 const FilePath& intermediate_path); |
270 | 281 |
271 MessageLoopForUI message_loop_; | |
272 TestingPrefService* pref_service_; | 282 TestingPrefService* pref_service_; |
273 ScopedTempDir test_download_dir_; | 283 ScopedTempDir test_download_dir_; |
274 TestingProfile profile_; | |
275 content::TestBrowserThread ui_thread_; | 284 content::TestBrowserThread ui_thread_; |
276 content::TestBrowserThread file_thread_; | 285 content::TestBrowserThread file_thread_; |
277 scoped_refptr<content::MockDownloadManager> download_manager_; | 286 scoped_refptr<content::MockDownloadManager> download_manager_; |
278 scoped_refptr<TestChromeDownloadManagerDelegate> delegate_; | 287 scoped_refptr<TestChromeDownloadManagerDelegate> delegate_; |
| 288 MockWebContentsDelegate web_contents_delegate_; |
279 }; | 289 }; |
280 | 290 |
281 ChromeDownloadManagerDelegateTest::ChromeDownloadManagerDelegateTest() | 291 ChromeDownloadManagerDelegateTest::ChromeDownloadManagerDelegateTest() |
282 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 292 : ChromeRenderViewHostTestHarness(), |
| 293 ui_thread_(content::BrowserThread::UI, &message_loop_), |
283 file_thread_(content::BrowserThread::FILE, &message_loop_), | 294 file_thread_(content::BrowserThread::FILE, &message_loop_), |
284 download_manager_(new content::MockDownloadManager), | 295 download_manager_(new content::MockDownloadManager) { |
285 delegate_(new TestChromeDownloadManagerDelegate(&profile_)) { | |
286 delegate_->SetDownloadManager(download_manager_.get()); | |
287 pref_service_ = profile_.GetTestingPrefService(); | |
288 } | 296 } |
289 | 297 |
290 void ChromeDownloadManagerDelegateTest::SetUp() { | 298 void ChromeDownloadManagerDelegateTest::SetUp() { |
| 299 ChromeRenderViewHostTestHarness::SetUp(); |
| 300 |
| 301 CHECK(profile()); |
| 302 delegate_ = new TestChromeDownloadManagerDelegate(profile()); |
| 303 delegate_->SetDownloadManager(download_manager_.get()); |
| 304 pref_service_ = profile()->GetTestingPrefService(); |
| 305 contents()->SetDelegate(&web_contents_delegate_); |
| 306 |
291 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); | 307 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); |
292 SetDefaultDownloadPath(test_download_dir_.path()); | 308 SetDefaultDownloadPath(test_download_dir_.path()); |
293 } | 309 } |
294 | 310 |
295 void ChromeDownloadManagerDelegateTest::TearDown() { | 311 void ChromeDownloadManagerDelegateTest::TearDown() { |
296 message_loop_.RunAllPending(); | 312 message_loop_.RunAllPending(); |
297 delegate_->Shutdown(); | 313 delegate_->Shutdown(); |
| 314 ChromeRenderViewHostTestHarness::TearDown(); |
298 } | 315 } |
299 | 316 |
300 void ChromeDownloadManagerDelegateTest::VerifyAndClearExpectations() { | 317 void ChromeDownloadManagerDelegateTest::VerifyAndClearExpectations() { |
301 ::testing::Mock::VerifyAndClearExpectations(delegate_); | 318 ::testing::Mock::VerifyAndClearExpectations(delegate_); |
302 ::testing::Mock::VerifyAndClearExpectations(download_manager_); | 319 ::testing::Mock::VerifyAndClearExpectations(download_manager_); |
303 } | 320 } |
304 | 321 |
305 content::MockDownloadItem* | 322 content::MockDownloadItem* |
306 ChromeDownloadManagerDelegateTest::CreateActiveDownloadItem(int32 id) { | 323 ChromeDownloadManagerDelegateTest::CreateActiveDownloadItem(int32 id) { |
307 content::MockDownloadItem* item = | 324 content::MockDownloadItem* item = |
308 new ::testing::NiceMock<content::MockDownloadItem>(); | 325 new ::testing::NiceMock<content::MockDownloadItem>(); |
309 ON_CALL(*item, GetFullPath()) | 326 ON_CALL(*item, GetFullPath()) |
310 .WillByDefault(ReturnRefOfCopy(FilePath())); | 327 .WillByDefault(ReturnRefOfCopy(FilePath())); |
311 ON_CALL(*item, GetHash()) | 328 ON_CALL(*item, GetHash()) |
312 .WillByDefault(ReturnRefOfCopy(std::string())); | 329 .WillByDefault(ReturnRefOfCopy(std::string())); |
313 ON_CALL(*item, GetReferrerUrl()) | 330 ON_CALL(*item, GetReferrerUrl()) |
314 .WillByDefault(ReturnRefOfCopy(GURL())); | 331 .WillByDefault(ReturnRefOfCopy(GURL())); |
315 ON_CALL(*item, GetTransitionType()) | 332 ON_CALL(*item, GetTransitionType()) |
316 .WillByDefault(Return(content::PAGE_TRANSITION_LINK)); | 333 .WillByDefault(Return(content::PAGE_TRANSITION_LINK)); |
317 ON_CALL(*item, HasUserGesture()) | 334 ON_CALL(*item, HasUserGesture()) |
318 .WillByDefault(Return(false)); | 335 .WillByDefault(Return(false)); |
319 ON_CALL(*item, IsDangerous()) | 336 ON_CALL(*item, IsDangerous()) |
320 .WillByDefault(Return(false)); | 337 .WillByDefault(Return(false)); |
321 ON_CALL(*item, IsTemporary()) | 338 ON_CALL(*item, IsTemporary()) |
322 .WillByDefault(Return(false)); | 339 .WillByDefault(Return(false)); |
| 340 ON_CALL(*item, GetWebContents()) |
| 341 .WillByDefault(Return(contents())); |
323 EXPECT_CALL(*item, GetId()) | 342 EXPECT_CALL(*item, GetId()) |
324 .WillRepeatedly(Return(id)); | 343 .WillRepeatedly(Return(id)); |
325 EXPECT_CALL(*download_manager_, GetActiveDownloadItem(id)) | 344 EXPECT_CALL(*download_manager_, GetActiveDownloadItem(id)) |
326 .WillRepeatedly(Return(item)); | 345 .WillRepeatedly(Return(item)); |
327 return item; | 346 return item; |
328 } | 347 } |
329 | 348 |
330 void ChromeDownloadManagerDelegateTest::EnableAutoOpenBasedOnExtension( | 349 void ChromeDownloadManagerDelegateTest::EnableAutoOpenBasedOnExtension( |
331 const FilePath& path) { | 350 const FilePath& path) { |
332 EXPECT_TRUE( | 351 EXPECT_TRUE( |
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
870 RunTestCases(kLastSavePathTestCases, arraysize(kLastSavePathTestCases)); | 889 RunTestCases(kLastSavePathTestCases, arraysize(kLastSavePathTestCases)); |
871 | 890 |
872 // Now clear the last download path. | 891 // Now clear the last download path. |
873 delegate()->ClearLastDownloadPath(); | 892 delegate()->ClearLastDownloadPath(); |
874 | 893 |
875 // Run the first test case again. Since the last download path was cleared, | 894 // Run the first test case again. Since the last download path was cleared, |
876 // this test case should behave identically to the first time it was run. | 895 // this test case should behave identically to the first time it was run. |
877 RunTestCases(kLastSavePathTestCases, 1); | 896 RunTestCases(kLastSavePathTestCases, 1); |
878 } | 897 } |
879 | 898 |
| 899 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_WebIntents) { |
| 900 const DownloadTestCase kWebIntentsTestCases[] = { |
| 901 { |
| 902 // 1: A file which would be dangerous, but is handled by web intents. |
| 903 // The name will be unaltered (the actual save name will have the |
| 904 // .webintents extension). |
| 905 AUTOMATIC, |
| 906 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 907 "http://example.com/feed.exe", "application/rss+xml", |
| 908 FILE_PATH_LITERAL(""), |
| 909 |
| 910 FILE_PATH_LITERAL("feed.exe.webintents"), |
| 911 FILE_PATH_LITERAL(""), |
| 912 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 913 |
| 914 EXPECT_CRDOWNLOAD |
| 915 }, |
| 916 |
| 917 { |
| 918 // 2: A download with a forced path won't be handled by web intents. |
| 919 FORCED, |
| 920 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 921 "http://example.com/feed.exe", "application/rss+xml", |
| 922 FILE_PATH_LITERAL("forced.feed.exe"), |
| 923 |
| 924 FILE_PATH_LITERAL("forced.feed.exe"), |
| 925 FILE_PATH_LITERAL(""), |
| 926 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 927 |
| 928 EXPECT_CRDOWNLOAD |
| 929 }, |
| 930 }; |
| 931 |
| 932 RunTestCases(kWebIntentsTestCases, arraysize(kWebIntentsTestCases)); |
| 933 } |
| 934 |
880 // TODO(asanka): Add more tests. | 935 // TODO(asanka): Add more tests. |
881 // * Default download path is not writable. | 936 // * Default download path is not writable. |
882 // * Download path doesn't exist. | 937 // * Download path doesn't exist. |
883 // * IsDangerousFile(). | 938 // * IsDangerousFile(). |
884 // * Filename generation. | 939 // * Filename generation. |
OLD | NEW |