diff options
author | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-21 07:31:11 +0000 |
---|---|---|
committer | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-21 07:31:11 +0000 |
commit | c70f9b84b30986220dc58993a9a41d6fdebefac2 (patch) | |
tree | bfd1dd487c509f9bfb65b065293d300c934b9282 /chrome | |
parent | 2f312ab5f419b77456eba5e0362af3adaf94f6ad (diff) | |
download | chromium_src-c70f9b84b30986220dc58993a9a41d6fdebefac2.zip chromium_src-c70f9b84b30986220dc58993a9a41d6fdebefac2.tar.gz chromium_src-c70f9b84b30986220dc58993a9a41d6fdebefac2.tar.bz2 |
Close a newly opened (by cmd-clicking on a link) tab if it resulted in a download.
To test this, the CL adds an automation message AutomationMsg_NavigateAsyncWithDisposition, and a method NavigateToURLAsyncWithDisposition to TabProxy.
The only functional change is in TabContents::OnStartDownload, the rest of the changes is for testing.
BUG=10764
TEST=DownloadTest.CloseNewTab
Manual test: go to http://build.chromium.org/buildbot/continuous/mac/LATEST/, open the link "chrome-mac.zip" in a new tab. The tab should close as soon as the download starts.
Review URL: http://codereview.chromium.org/1151007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@45158 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/automation/automation_provider.cc | 17 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider.h | 4 | ||||
-rw-r--r-- | chrome/browser/download/download_uitest.cc | 140 | ||||
-rw-r--r-- | chrome/browser/tab_contents/navigation_controller.cc | 4 | ||||
-rw-r--r-- | chrome/browser/tab_contents/navigation_controller.h | 3 | ||||
-rw-r--r-- | chrome/browser/tab_contents/tab_contents.cc | 6 | ||||
-rw-r--r-- | chrome/test/automation/automation_messages_internal.h | 8 | ||||
-rw-r--r-- | chrome/test/automation/tab_proxy.cc | 20 | ||||
-rw-r--r-- | chrome/test/automation/tab_proxy.h | 6 | ||||
-rw-r--r-- | chrome/test/data/download_page1.html | 11 | ||||
-rw-r--r-- | chrome/test/data/download_page2.html | 8 | ||||
-rw-r--r-- | chrome/test/data/download_page3.html | 9 | ||||
-rw-r--r-- | chrome/test/data/download_page4.html | 5 |
13 files changed, 236 insertions, 5 deletions
diff --git a/chrome/browser/automation/automation_provider.cc b/chrome/browser/automation/automation_provider.cc index 8fcdf03..5f51cad 100644 --- a/chrome/browser/automation/automation_provider.cc +++ b/chrome/browser/automation/automation_provider.cc @@ -328,6 +328,8 @@ void AutomationProvider::OnMessageReceived(const IPC::Message& message) { AutomationMsg_NavigateToURLBlockUntilNavigationsComplete, NavigateToURLBlockUntilNavigationsComplete) IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync) + IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition, + NavigationAsyncWithDisposition) IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack) IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward) IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload) @@ -642,8 +644,17 @@ void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete( Send(reply_message); } -void AutomationProvider::NavigationAsync(int handle, const GURL& url, - bool* status) { +void AutomationProvider::NavigationAsync(int handle, + const GURL& url, + bool* status) { + NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status); +} + +void AutomationProvider::NavigationAsyncWithDisposition( + int handle, + const GURL& url, + WindowOpenDisposition disposition, + bool* status) { *status = false; if (tab_tracker_->ContainsHandle(handle)) { @@ -656,7 +667,7 @@ void AutomationProvider::NavigationAsync(int handle, const GURL& url, if (browser) { // Don't add any listener unless a callback mechanism is desired. // TODO(vibhor): Do this if such a requirement arises in future. - browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); + browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED); *status = true; } } diff --git a/chrome/browser/automation/automation_provider.h b/chrome/browser/automation/automation_provider.h index cb07f1c..e4256c0 100644 --- a/chrome/browser/automation/automation_provider.h +++ b/chrome/browser/automation/automation_provider.h @@ -220,6 +220,10 @@ class AutomationProvider : public base::RefCounted<AutomationProvider>, int number_of_navigations, IPC::Message* reply_message); void NavigationAsync(int handle, const GURL& url, bool* status); + void NavigationAsyncWithDisposition(int handle, + const GURL& url, + WindowOpenDisposition disposition, + bool* status); void GoBack(int handle, IPC::Message* reply_message); void GoForward(int handle, IPC::Message* reply_message); void Reload(int handle, IPC::Message* reply_message); diff --git a/chrome/browser/download/download_uitest.cc b/chrome/browser/download/download_uitest.cc index 40a5af27..9a29c57 100644 --- a/chrome/browser/download/download_uitest.cc +++ b/chrome/browser/download/download_uitest.cc @@ -325,6 +325,9 @@ TEST_F(DownloadTest, FLAKY_IncognitoDownload) { ASSERT_EQ(2, window_count); scoped_refptr<BrowserProxy> incognito(automation()->GetBrowserWindow(1)); ASSERT_TRUE(incognito.get()); + // Wait for the new tab UI to load. + int load_time; + ASSERT_TRUE(automation()->WaitForInitialNewTabUILoad(&load_time)); // Download something. FilePath file(FILE_PATH_LITERAL("download-test1.lib")); @@ -347,4 +350,141 @@ TEST_F(DownloadTest, FLAKY_IncognitoDownload) { CheckDownload(file); } +TEST_F(DownloadTest, DontCloseNewTab1) { + scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); + ASSERT_TRUE(browser.get()); + int window_count = 0; + ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count)); + ASSERT_EQ(1, window_count); + EXPECT_EQ(1, GetTabCount()); + + scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); + ASSERT_TRUE(tab_proxy.get()); + + FilePath file1(FILE_PATH_LITERAL("download-test2.html")); + ASSERT_TRUE(tab_proxy->NavigateToURLAsyncWithDisposition( + URLRequestMockHTTPJob::GetMockUrl(file1), + NEW_BACKGROUND_TAB)); + // We should have two tabs now. + WaitUntilTabCount(2); +} + +TEST_F(DownloadTest, CloseNewTab1) { + scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); + ASSERT_TRUE(browser.get()); + int window_count = 0; + ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count)); + ASSERT_EQ(1, window_count); + EXPECT_EQ(1, GetTabCount()); + + scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); + ASSERT_TRUE(tab_proxy.get()); + + FilePath file(FILE_PATH_LITERAL("download-test1.lib")); + ASSERT_TRUE(tab_proxy->NavigateToURLAsyncWithDisposition( + URLRequestMockHTTPJob::GetMockUrl(file), + NEW_BACKGROUND_TAB)); + // When the download starts, we should still have one tab. + ASSERT_TRUE(WaitForDownloadShelfVisible(browser)); + EXPECT_EQ(1, GetTabCount()); + + CheckDownload(file); +} + +TEST_F(DownloadTest, DontCloseNewTab2) { + scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); + ASSERT_TRUE(browser.get()); + int window_count = 0; + ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count)); + ASSERT_EQ(1, window_count); + EXPECT_EQ(1, GetTabCount()); + + scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); + ASSERT_TRUE(tab_proxy.get()); + + ASSERT_TRUE(tab_proxy->NavigateToURL(URLRequestMockHTTPJob::GetMockUrl( + FilePath(FILE_PATH_LITERAL("download_page1.html"))))); + + FilePath file(FILE_PATH_LITERAL("download-test1.lib")); + ASSERT_TRUE(tab_proxy->NavigateToURLAsync(GURL("javascript:openNew()"))); + + ASSERT_TRUE(WaitForDownloadShelfVisible(browser)); + EXPECT_EQ(2, GetTabCount()); + + CheckDownload(file); +} + +TEST_F(DownloadTest, DontCloseNewTab3) { + scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); + ASSERT_TRUE(browser.get()); + int window_count = 0; + ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count)); + ASSERT_EQ(1, window_count); + EXPECT_EQ(1, GetTabCount()); + + scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); + ASSERT_TRUE(tab_proxy.get()); + + ASSERT_TRUE(tab_proxy->NavigateToURL(URLRequestMockHTTPJob::GetMockUrl( + FilePath(FILE_PATH_LITERAL("download_page2.html"))))); + + ASSERT_TRUE(tab_proxy->NavigateToURLAsync(GURL("javascript:openNew()"))); + + FilePath file(FILE_PATH_LITERAL("download-test1.lib")); + ASSERT_TRUE(tab_proxy->NavigateToURLAsync( + URLRequestMockHTTPJob::GetMockUrl(file))); + + ASSERT_TRUE(WaitForDownloadShelfVisible(browser)); + EXPECT_EQ(2, GetTabCount()); + + CheckDownload(file); +} + +TEST_F(DownloadTest, CloseNewTab2) { + scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); + ASSERT_TRUE(browser.get()); + int window_count = 0; + ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count)); + ASSERT_EQ(1, window_count); + EXPECT_EQ(1, GetTabCount()); + + scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); + ASSERT_TRUE(tab_proxy.get()); + + ASSERT_TRUE(tab_proxy->NavigateToURL(URLRequestMockHTTPJob::GetMockUrl( + FilePath(FILE_PATH_LITERAL("download_page3.html"))))); + + FilePath file(FILE_PATH_LITERAL("download-test1.lib")); + ASSERT_TRUE(tab_proxy->NavigateToURLAsync(GURL("javascript:openNew()"))); + + ASSERT_TRUE(WaitForDownloadShelfVisible(browser)); + EXPECT_EQ(1, GetTabCount()); + + CheckDownload(file); +} + +TEST_F(DownloadTest, CloseNewTab3) { + scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); + ASSERT_TRUE(browser.get()); + int window_count = 0; + ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count)); + ASSERT_EQ(1, window_count); + EXPECT_EQ(1, GetTabCount()); + + scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); + ASSERT_TRUE(tab_proxy.get()); + + ASSERT_TRUE(tab_proxy->NavigateToURL(URLRequestMockHTTPJob::GetMockUrl( + FilePath(FILE_PATH_LITERAL("download_page4.html"))))); + + FilePath file(FILE_PATH_LITERAL("download-test1.lib")); + ASSERT_TRUE(tab_proxy->NavigateToURLAsync( + GURL("javascript:document.getElementById('form').submit()"))); + + ASSERT_TRUE(WaitForDownloadShelfVisible(browser)); + EXPECT_EQ(1, GetTabCount()); + + CheckDownload(file); +} + } // namespace diff --git a/chrome/browser/tab_contents/navigation_controller.cc b/chrome/browser/tab_contents/navigation_controller.cc index 141712f..c036c31 100644 --- a/chrome/browser/tab_contents/navigation_controller.cc +++ b/chrome/browser/tab_contents/navigation_controller.cc @@ -240,6 +240,10 @@ void NavigationController::ContinuePendingReload() { } } +bool NavigationController::IsInitialNavigation() { + return last_document_loaded_.is_null(); +} + NavigationEntry* NavigationController::GetEntryWithPageID( SiteInstance* instance, int32 page_id) const { int index = GetEntryIndexWithPageID(instance, page_id); diff --git a/chrome/browser/tab_contents/navigation_controller.h b/chrome/browser/tab_contents/navigation_controller.h index 16011d3..dbbe8e8 100644 --- a/chrome/browser/tab_contents/navigation_controller.h +++ b/chrome/browser/tab_contents/navigation_controller.h @@ -407,6 +407,9 @@ class NavigationController { // Continues a repost that brought up a warning. void ContinuePendingReload(); + // Returns true if we are navigating to the URL the tab is opened with. + bool IsInitialNavigation(); + private: class RestoreHelper; friend class RestoreHelper; diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc index e2744e1..42b8a64 100644 --- a/chrome/browser/tab_contents/tab_contents.cc +++ b/chrome/browser/tab_contents/tab_contents.cc @@ -1113,8 +1113,12 @@ void TabContents::OnStartDownload(DownloadItem* download) { // Download in a constrained popup is shown in the tab that opened it. TabContents* tab_contents = delegate()->GetConstrainingContents(this); - if (tab_contents && tab_contents->delegate()) + if (tab_contents && tab_contents->delegate()) { tab_contents->delegate()->OnStartDownload(download); + // If the download occurs in a new tab, close it + if (controller_.IsInitialNavigation() && (tab_contents == this)) + delegate()->CloseContents(this); + } } void TabContents::WillClose(ConstrainedWindow* window) { diff --git a/chrome/test/automation/automation_messages_internal.h b/chrome/test/automation/automation_messages_internal.h index 02ee3e4..9512969 100644 --- a/chrome/test/automation/automation_messages_internal.h +++ b/chrome/test/automation/automation_messages_internal.h @@ -1381,4 +1381,12 @@ IPC_BEGIN_MESSAGES(Automation) // Resets to the default theme. IPC_SYNC_MESSAGE_ROUTED0_0(AutomationMsg_ResetToDefaultTheme) + // Navigates asynchronously to a URL with a certain disposition, + // like in a new tab. + IPC_SYNC_MESSAGE_ROUTED3_1(AutomationMsg_NavigationAsyncWithDisposition, + int /* tab handle */, + GURL, + WindowOpenDisposition, + bool /* result */) + IPC_END_MESSAGES(Automation) diff --git a/chrome/test/automation/tab_proxy.cc b/chrome/test/automation/tab_proxy.cc index 495cf7a..a662f60 100644 --- a/chrome/test/automation/tab_proxy.cc +++ b/chrome/test/automation/tab_proxy.cc @@ -222,7 +222,25 @@ bool TabProxy::NavigateToURLAsync(const GURL& url) { return false; bool status = false; - sender_->Send(new AutomationMsg_NavigationAsync(0, handle_, url, &status)); + sender_->Send(new AutomationMsg_NavigationAsync(0, + handle_, + url, + &status)); + return status; +} + +bool TabProxy::NavigateToURLAsyncWithDisposition( + const GURL& url, + WindowOpenDisposition disposition) { + if (!is_valid()) + return false; + + bool status = false; + sender_->Send(new AutomationMsg_NavigationAsyncWithDisposition(0, + handle_, + url, + disposition, + &status)); return status; } diff --git a/chrome/test/automation/tab_proxy.h b/chrome/test/automation/tab_proxy.h index ad28090..7fabc50 100644 --- a/chrome/test/automation/tab_proxy.h +++ b/chrome/test/automation/tab_proxy.h @@ -150,6 +150,12 @@ class TabProxy : public AutomationResourceProxy, // TabProxy we attach won't know about it. See bug 666730. bool NavigateToURLAsync(const GURL& url) WARN_UNUSED_RESULT; + // Asynchronously navigates to a url using a non-default disposition. + // This can be used for example to open a URL in a new tab. + bool NavigateToURLAsyncWithDisposition( + const GURL& url, + WindowOpenDisposition disposition) WARN_UNUSED_RESULT; + // Replaces a vector contents with the redirect chain out of the given URL. // Returns true on success. Failure may be due to being unable to send the // message, parse the response, or a failure of the history system in the diff --git a/chrome/test/data/download_page1.html b/chrome/test/data/download_page1.html new file mode 100644 index 0000000..25854f8 --- /dev/null +++ b/chrome/test/data/download_page1.html @@ -0,0 +1,11 @@ +<script> +function openNew() { + var w = window.open(); + var download = "http://"+window.location.host+"/download-test1.lib"; + w.document.write('<bo' + 'dy onload="location.replace(\''+download+'\')">Important information about the download...</bo' + 'dy>'); + w.document.close(); +} +</script> +<body> + <button onclick="openNew()">click me</button> +</body>
\ No newline at end of file diff --git a/chrome/test/data/download_page2.html b/chrome/test/data/download_page2.html new file mode 100644 index 0000000..9c6d0dd --- /dev/null +++ b/chrome/test/data/download_page2.html @@ -0,0 +1,8 @@ +<script> +function openNew() { + var w = window.open(); +} +</script> +<body> + <button onclick="openNew()">click me</button> +</body>
\ No newline at end of file diff --git a/chrome/test/data/download_page3.html b/chrome/test/data/download_page3.html new file mode 100644 index 0000000..2037253 --- /dev/null +++ b/chrome/test/data/download_page3.html @@ -0,0 +1,9 @@ +<script> +function openNew() { + var download = "http://"+window.location.host+"/download-test1.lib"; + var w = window.open(download); +} +</script> +<body> + <button onclick="openNew()">click me</button> +</body>
\ No newline at end of file diff --git a/chrome/test/data/download_page4.html b/chrome/test/data/download_page4.html new file mode 100644 index 0000000..a8ea0c6 --- /dev/null +++ b/chrome/test/data/download_page4.html @@ -0,0 +1,5 @@ +<body> + <form id="form" action="download-test1.lib" target="_blank"> + <input type="submit" /> + </form> +</body>
\ No newline at end of file |