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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/test/test_file_util.h" | 14 #include "base/test/test_file_util.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/app/chrome_command_ids.h" |
16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/cancelable_request.h" | 18 #include "chrome/browser/cancelable_request.h" |
18 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 19 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
19 #include "chrome/browser/download/download_crx_util.h" | 20 #include "chrome/browser/download/download_crx_util.h" |
20 #include "chrome/browser/download/download_history.h" | 21 #include "chrome/browser/download/download_history.h" |
21 #include "chrome/browser/download/download_prefs.h" | 22 #include "chrome/browser/download/download_prefs.h" |
22 #include "chrome/browser/download/download_service.h" | 23 #include "chrome/browser/download/download_service.h" |
23 #include "chrome/browser/download/download_service_factory.h" | 24 #include "chrome/browser/download/download_service_factory.h" |
24 #include "chrome/browser/download/download_shelf.h" | 25 #include "chrome/browser/download/download_shelf.h" |
25 #include "chrome/browser/download/download_test_observer.h" | 26 #include "chrome/browser/download/download_test_observer.h" |
26 #include "chrome/browser/download/download_util.h" | 27 #include "chrome/browser/download/download_util.h" |
27 #include "chrome/browser/extensions/extension_install_ui.h" | 28 #include "chrome/browser/extensions/extension_install_ui.h" |
28 #include "chrome/browser/extensions/extension_service.h" | 29 #include "chrome/browser/extensions/extension_service.h" |
29 #include "chrome/browser/history/history.h" | 30 #include "chrome/browser/history/history.h" |
30 #include "chrome/browser/net/url_request_mock_util.h" | 31 #include "chrome/browser/net/url_request_mock_util.h" |
31 #include "chrome/browser/prefs/pref_service.h" | 32 #include "chrome/browser/prefs/pref_service.h" |
32 #include "chrome/browser/profiles/profile.h" | 33 #include "chrome/browser/profiles/profile.h" |
| 34 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
33 #include "chrome/browser/ui/browser.h" | 35 #include "chrome/browser/ui/browser.h" |
34 #include "chrome/browser/ui/browser_list.h" | 36 #include "chrome/browser/ui/browser_list.h" |
35 #include "chrome/browser/ui/browser_window.h" | 37 #include "chrome/browser/ui/browser_window.h" |
36 #include "chrome/browser/ui/webui/active_downloads_ui.h" | 38 #include "chrome/browser/ui/webui/active_downloads_ui.h" |
37 #include "chrome/common/chrome_notification_types.h" | 39 #include "chrome/common/chrome_notification_types.h" |
38 #include "chrome/common/chrome_paths.h" | 40 #include "chrome/common/chrome_paths.h" |
39 #include "chrome/common/pref_names.h" | 41 #include "chrome/common/pref_names.h" |
40 #include "chrome/common/url_constants.h" | 42 #include "chrome/common/url_constants.h" |
41 #include "chrome/test/base/in_process_browser_test.h" | 43 #include "chrome/test/base/in_process_browser_test.h" |
42 #include "chrome/test/base/ui_test_utils.h" | 44 #include "chrome/test/base/ui_test_utils.h" |
43 #include "content/browser/download/download_file_manager.h" | 45 #include "content/browser/download/download_file_manager.h" |
44 #include "content/browser/download/download_persistent_store_info.h" | 46 #include "content/browser/download/download_persistent_store_info.h" |
45 #include "content/browser/net/url_request_mock_http_job.h" | 47 #include "content/browser/net/url_request_mock_http_job.h" |
46 #include "content/browser/net/url_request_slow_download_job.h" | 48 #include "content/browser/net/url_request_slow_download_job.h" |
47 #include "content/browser/renderer_host/render_view_host.h" | 49 #include "content/browser/renderer_host/render_view_host.h" |
48 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 50 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
49 #include "content/public/browser/download_item.h" | 51 #include "content/public/browser/download_item.h" |
50 #include "content/public/browser/download_manager.h" | 52 #include "content/public/browser/download_manager.h" |
51 #include "content/public/browser/notification_source.h" | 53 #include "content/public/browser/notification_source.h" |
52 #include "content/public/browser/web_contents.h" | 54 #include "content/public/browser/web_contents.h" |
53 #include "content/public/common/page_transition_types.h" | 55 #include "content/public/common/page_transition_types.h" |
54 #include "net/base/net_util.h" | 56 #include "net/base/net_util.h" |
55 #include "testing/gtest/include/gtest/gtest.h" | 57 #include "testing/gtest/include/gtest/gtest.h" |
| 58 #include "webkit/glue/context_menu.h" |
56 | 59 |
57 using content::BrowserThread; | 60 using content::BrowserThread; |
58 using content::DownloadItem; | 61 using content::DownloadItem; |
59 using content::DownloadManager; | 62 using content::DownloadManager; |
60 using content::WebContents; | 63 using content::WebContents; |
61 | 64 |
62 namespace { | 65 namespace { |
63 | 66 |
64 // IDs and paths of CRX files used in tests. | 67 // IDs and paths of CRX files used in tests. |
65 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; | 68 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 | 248 |
246 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {} | 249 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {} |
247 virtual void OnInstallFailure(const string16& error) {} | 250 virtual void OnInstallFailure(const string16& error) {} |
248 }; | 251 }; |
249 | 252 |
250 static DownloadManager* DownloadManagerForBrowser(Browser* browser) { | 253 static DownloadManager* DownloadManagerForBrowser(Browser* browser) { |
251 return DownloadServiceFactory::GetForProfile(browser->profile()) | 254 return DownloadServiceFactory::GetForProfile(browser->profile()) |
252 ->GetDownloadManager(); | 255 ->GetDownloadManager(); |
253 } | 256 } |
254 | 257 |
| 258 class TestRenderViewContextMenu : public RenderViewContextMenu { |
| 259 public: |
| 260 TestRenderViewContextMenu(WebContents* web_contents, |
| 261 const ContextMenuParams& params) |
| 262 : RenderViewContextMenu(web_contents, params) { |
| 263 } |
| 264 virtual ~TestRenderViewContextMenu() {} |
| 265 |
| 266 private: |
| 267 virtual void PlatformInit() {} |
| 268 virtual bool GetAcceleratorForCommandId(int, ui::Accelerator*) { |
| 269 return false; |
| 270 } |
| 271 |
| 272 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); |
| 273 }; |
| 274 |
255 } // namespace | 275 } // namespace |
256 | 276 |
257 // While an object of this class exists, it will mock out download | 277 // While an object of this class exists, it will mock out download |
258 // opening for all downloads created on the specified download manager. | 278 // opening for all downloads created on the specified download manager. |
259 class MockDownloadOpeningObserver : public content::DownloadManager::Observer { | 279 class MockDownloadOpeningObserver : public content::DownloadManager::Observer { |
260 public: | 280 public: |
261 explicit MockDownloadOpeningObserver(DownloadManager* manager) | 281 explicit MockDownloadOpeningObserver(DownloadManager* manager) |
262 : download_manager_(manager) { | 282 : download_manager_(manager) { |
263 download_manager_->AddObserver(this); | 283 download_manager_->AddObserver(this); |
264 } | 284 } |
(...skipping 1502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1767 DownloadManagerForBrowser(browser()), 1, DownloadItem::COMPLETE, | 1787 DownloadManagerForBrowser(browser()), 1, DownloadItem::COMPLETE, |
1768 false, DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); | 1788 false, DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
1769 browser()->SavePage(); | 1789 browser()->SavePage(); |
1770 waiter->WaitForFinished(); | 1790 waiter->WaitForFinished(); |
1771 | 1791 |
1772 // Validate that the correct file was downloaded. | 1792 // Validate that the correct file was downloaded. |
1773 GetDownloads(browser(), &download_items); | 1793 GetDownloads(browser(), &download_items); |
1774 EXPECT_TRUE(waiter->select_file_dialog_seen()); | 1794 EXPECT_TRUE(waiter->select_file_dialog_seen()); |
1775 ASSERT_EQ(1u, download_items.size()); | 1795 ASSERT_EQ(1u, download_items.size()); |
1776 ASSERT_EQ(url, download_items[0]->GetOriginalUrl()); | 1796 ASSERT_EQ(url, download_items[0]->GetOriginalUrl()); |
| 1797 |
| 1798 // Try to download it via a context menu. |
| 1799 scoped_ptr<DownloadTestObserver> waiter_context_menu( |
| 1800 new DownloadTestObserver( |
| 1801 DownloadManagerForBrowser(browser()), 1, DownloadItem::COMPLETE, |
| 1802 false, DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 1803 ContextMenuParams context_menu_params; |
| 1804 context_menu_params.media_type = WebKit::WebContextMenuData::MediaTypeImage; |
| 1805 context_menu_params.src_url = url; |
| 1806 context_menu_params.page_url = url; |
| 1807 TestRenderViewContextMenu menu(browser()->GetSelectedWebContents(), |
| 1808 context_menu_params); |
| 1809 menu.Init(); |
| 1810 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_SAVEIMAGEAS); |
| 1811 waiter_context_menu->WaitForFinished(); |
| 1812 |
| 1813 // Validate that the correct file was downloaded via the context menu. |
| 1814 download_items.clear(); |
| 1815 GetDownloads(browser(), &download_items); |
| 1816 EXPECT_TRUE(waiter_context_menu->select_file_dialog_seen()); |
| 1817 ASSERT_EQ(2u, download_items.size()); |
| 1818 ASSERT_EQ(url, download_items[0]->GetOriginalUrl()); |
| 1819 ASSERT_EQ(url, download_items[1]->GetOriginalUrl()); |
1777 } | 1820 } |
1778 | 1821 |
1779 IN_PROC_BROWSER_TEST_F(DownloadTest, SavePageNonHTMLViaPost) { | 1822 IN_PROC_BROWSER_TEST_F(DownloadTest, SavePageNonHTMLViaPost) { |
1780 // Do initial setup. | 1823 // Do initial setup. |
1781 ASSERT_TRUE(InitialSetup(false)); | 1824 ASSERT_TRUE(InitialSetup(false)); |
1782 ASSERT_TRUE(test_server()->Start()); | 1825 ASSERT_TRUE(test_server()->Start()); |
1783 NullSelectFile(browser()); | 1826 NullSelectFile(browser()); |
1784 std::vector<DownloadItem*> download_items; | 1827 std::vector<DownloadItem*> download_items; |
1785 GetDownloads(browser(), &download_items); | 1828 GetDownloads(browser(), &download_items); |
1786 ASSERT_TRUE(download_items.empty()); | 1829 ASSERT_TRUE(download_items.empty()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1819 DownloadManagerForBrowser(browser()), 1, DownloadItem::COMPLETE, | 1862 DownloadManagerForBrowser(browser()), 1, DownloadItem::COMPLETE, |
1820 false, DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); | 1863 false, DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
1821 browser()->SavePage(); | 1864 browser()->SavePage(); |
1822 waiter->WaitForFinished(); | 1865 waiter->WaitForFinished(); |
1823 | 1866 |
1824 // Validate that the correct file was downloaded. | 1867 // Validate that the correct file was downloaded. |
1825 GetDownloads(browser(), &download_items); | 1868 GetDownloads(browser(), &download_items); |
1826 EXPECT_TRUE(waiter->select_file_dialog_seen()); | 1869 EXPECT_TRUE(waiter->select_file_dialog_seen()); |
1827 ASSERT_EQ(1u, download_items.size()); | 1870 ASSERT_EQ(1u, download_items.size()); |
1828 ASSERT_EQ(jpeg_url, download_items[0]->GetOriginalUrl()); | 1871 ASSERT_EQ(jpeg_url, download_items[0]->GetOriginalUrl()); |
| 1872 |
| 1873 // Try to download it via a context menu. |
| 1874 scoped_ptr<DownloadTestObserver> waiter_context_menu( |
| 1875 new DownloadTestObserver( |
| 1876 DownloadManagerForBrowser(browser()), 1, DownloadItem::COMPLETE, |
| 1877 false, DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 1878 ContextMenuParams context_menu_params; |
| 1879 context_menu_params.media_type = WebKit::WebContextMenuData::MediaTypeImage; |
| 1880 context_menu_params.src_url = jpeg_url; |
| 1881 context_menu_params.page_url = jpeg_url; |
| 1882 TestRenderViewContextMenu menu(web_contents, context_menu_params); |
| 1883 menu.Init(); |
| 1884 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_SAVEIMAGEAS); |
| 1885 waiter_context_menu->WaitForFinished(); |
| 1886 |
| 1887 // Validate that the correct file was downloaded via the context menu. |
| 1888 download_items.clear(); |
| 1889 GetDownloads(browser(), &download_items); |
| 1890 EXPECT_TRUE(waiter_context_menu->select_file_dialog_seen()); |
| 1891 ASSERT_EQ(2u, download_items.size()); |
| 1892 ASSERT_EQ(jpeg_url, download_items[0]->GetOriginalUrl()); |
| 1893 ASSERT_EQ(jpeg_url, download_items[1]->GetOriginalUrl()); |
1829 } | 1894 } |
OLD | NEW |