summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorojan@google.com <ojan@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-17 01:14:00 +0000
committerojan@google.com <ojan@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-17 01:14:00 +0000
commit3f97e3fbaee75e0ed70c1f37d195aef9914b20ee (patch)
tree1fd180ee8394534155f705a50e04edd7846fdb01
parenta1a3db8ce5e32f1069042aa64448a7862ced7d86 (diff)
downloadchromium_src-3f97e3fbaee75e0ed70c1f37d195aef9914b20ee.zip
chromium_src-3f97e3fbaee75e0ed70c1f37d195aef9914b20ee.tar.gz
chromium_src-3f97e3fbaee75e0ed70c1f37d195aef9914b20ee.tar.bz2
Add some initial before unload tests. We still need tests
for multiple tabs with beforeunload/unload handlers. Review URL: http://codereview.chromium.org/14085 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7122 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/unload_uitest.cc126
-rw-r--r--chrome/test/ui/ui_test.cc5
-rw-r--r--chrome/test/ui/ui_test.h5
3 files changed, 125 insertions, 11 deletions
diff --git a/chrome/browser/unload_uitest.cc b/chrome/browser/unload_uitest.cc
index 9db22f3..471a3e8 100644
--- a/chrome/browser/unload_uitest.cc
+++ b/chrome/browser/unload_uitest.cc
@@ -8,6 +8,7 @@
#include "chrome/common/chrome_switches.h"
#include "chrome/test/automation/browser_proxy.h"
#include "chrome/test/ui/ui_test.h"
+#include "chrome/views/dialog_delegate.h"
#include "net/url_request/url_request_unittest.h"
const std::string NOLISTENERS_HTML =
@@ -17,6 +18,19 @@ const std::string UNLOAD_HTML =
"<html><head><title>unload</title></head><body>"
"<script>window.onunload=function(e){}</script></body></html>";
+const std::string BEFORE_UNLOAD_HTML =
+ "<html><head><title>beforeunload</title></head><body>"
+ "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
+ "</body></html>";
+
+const std::string TWO_SECOND_BEFORE_UNLOAD_HTML =
+ "<html><head><title>twosecondbeforeunload</title></head><body>"
+ "<script>window.onbeforeunload=function(e){"
+ "var start = new Date().getTime();"
+ "while(new Date().getTime() - start < 2000){}"
+ "return 'foo';"
+ "}</script></body></html>";
+
const std::string INFINITE_UNLOAD_HTML =
"<html><head><title>infiniteunload</title></head><body>"
"<script>window.onunload=function(e){while(true){}}</script>"
@@ -24,13 +38,20 @@ const std::string INFINITE_UNLOAD_HTML =
const std::string INFINITE_BEFORE_UNLOAD_HTML =
"<html><head><title>infinitebeforeunload</title></head><body>"
- "<script>window.onunload=function(e){while(true){}}</script>"
+ "<script>window.onbeforeunload=function(e){while(true){}}</script>"
"</body></html>";
const std::string INFINITE_UNLOAD_ALERT_HTML =
"<html><head><title>infiniteunloadalert</title></head><body>"
"<script>window.onunload=function(e){"
- "while(true) {}"
+ "while(true){}"
+ "alert('foo');"
+ "}</script></body></html>";
+
+const std::string INFINITE_BEFORE_UNLOAD_ALERT_HTML =
+ "<html><head><title>infinitebeforeunloadalert</title></head><body>"
+ "<script>window.onbeforeunload=function(e){"
+ "while(true){}"
"alert('foo');"
"}</script></body></html>";
@@ -38,12 +59,31 @@ const std::string TWO_SECOND_UNLOAD_ALERT_HTML =
"<html><head><title>twosecondunloadalert</title></head><body>"
"<script>window.onunload=function(e){"
"var start = new Date().getTime();"
- "while(new Date().getTime() - start < 2000) {}"
+ "while(new Date().getTime() - start < 2000){}"
+ "alert('foo');"
+ "}</script></body></html>";
+
+const std::string TWO_SECOND_BEFORE_UNLOAD_ALERT_HTML =
+ "<html><head><title>twosecondbeforeunloadalert</title></head><body>"
+ "<script>window.onbeforeunload=function(e){"
+ "var start = new Date().getTime();"
+ "while(new Date().getTime() - start < 2000){}"
"alert('foo');"
"}</script></body></html>";
class UnloadTest : public UITest {
public:
+ void WaitForBrowserClosed() {
+ const int kCheckDelayMs = 100;
+ int max_wait_time = 5000;
+ while (max_wait_time > 0) {
+ max_wait_time -= kCheckDelayMs;
+ Sleep(kCheckDelayMs);
+ if (!IsBrowserRunning())
+ break;
+ }
+ }
+
void CheckTitle(const std::wstring& expected_title) {
const int kCheckDelayMs = 100;
int max_wait_time = 5000;
@@ -64,10 +104,10 @@ class UnloadTest : public UITest {
}
void NavigateToNolistenersFileTwice() {
- NavigateToURL(
+ NavigateToURL(
URLRequestMockHTTPJob::GetMockUrl(L"title2.html"));
CheckTitle(L"Title Of Awesomeness");
- NavigateToURL(
+ NavigateToURL(
URLRequestMockHTTPJob::GetMockUrl(L"title2.html"));
CheckTitle(L"Title Of Awesomeness");
}
@@ -79,10 +119,10 @@ class UnloadTest : public UITest {
// TODO(ojan): We hit a DCHECK in RenderViewHost::OnMsgShouldCloseACK
// if we don't sleep here.
Sleep(400);
- NavigateToURLAsync(
+ NavigateToURLAsync(
URLRequestMockHTTPJob::GetMockUrl(L"title2.html"));
Sleep(400);
- NavigateToURLAsync(
+ NavigateToURL(
URLRequestMockHTTPJob::GetMockUrl(L"title2.html"));
CheckTitle(L"Title Of Awesomeness");
@@ -95,6 +135,17 @@ class UnloadTest : public UITest {
bool application_closed = false;
EXPECT_TRUE(CloseBrowser(browser.get(), &application_closed));
}
+
+ void ClickModalDialogButton(views::DialogDelegate::DialogButton button) {
+ bool modal_dialog_showing = false;
+ views::DialogDelegate::DialogButton available_buttons;
+ EXPECT_TRUE(automation()->WaitForAppModalDialog(3000));
+ EXPECT_TRUE(automation()->GetShowingAppModalDialog(&modal_dialog_showing,
+ &available_buttons));
+ ASSERT_TRUE(modal_dialog_showing);
+ EXPECT_TRUE((button & available_buttons) != NULL);
+ EXPECT_TRUE(automation()->ClickAppModalDialogButton(button));
+ }
};
// Navigate to a page with an infinite unload handler.
@@ -163,25 +214,78 @@ TEST_F(UnloadTest, BrowserCloseUnload) {
LoadUrlAndQuitBrowser(UNLOAD_HTML, L"unload");
}
+// Tests closing the browser with a beforeunload handler and clicking
+// OK in the beforeunload confirm dialog.
+TEST_F(UnloadTest, BrowserCloseBeforeUnloadOK) {
+ scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
+ NavigateToDataURL(BEFORE_UNLOAD_HTML, L"beforeunload");
+
+ CloseBrowserAsync(browser.get());
+ ClickModalDialogButton(views::DialogDelegate::DIALOGBUTTON_OK);
+ WaitForBrowserClosed();
+ EXPECT_FALSE(IsBrowserRunning());
+}
+
+// Tests closing the browser with a beforeunload handler and clicking
+// CANCEL in the beforeunload confirm dialog.
+TEST_F(UnloadTest, BrowserCloseBeforeUnloadCancel) {
+ scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
+ NavigateToDataURL(BEFORE_UNLOAD_HTML, L"beforeunload");
+
+ CloseBrowserAsync(browser.get());
+ ClickModalDialogButton(views::DialogDelegate::DIALOGBUTTON_CANCEL);
+ WaitForBrowserClosed();
+ EXPECT_TRUE(IsBrowserRunning());
+
+ CloseBrowserAsync(browser.get());
+ ClickModalDialogButton(views::DialogDelegate::DIALOGBUTTON_OK);
+ WaitForBrowserClosed();
+ EXPECT_FALSE(IsBrowserRunning());
+}
+
+// Tests closing the browser with a beforeunload handler that takes
+// two seconds to run.
+TEST_F(UnloadTest, BrowserCloseTwoSecondBeforeUnload) {
+ LoadUrlAndQuitBrowser(TWO_SECOND_BEFORE_UNLOAD_HTML,
+ L"twosecondbeforeunload");
+}
+
// Tests closing the browser on a page with an unload listener registered where
// the unload handler has an infinite loop.
TEST_F(UnloadTest, BrowserCloseInfiniteUnload) {
LoadUrlAndQuitBrowser(INFINITE_UNLOAD_HTML, L"infiniteunload");
}
+// Tests closing the browser with a beforeunload handler that hangs.
+TEST_F(UnloadTest, BrowserCloseInfiniteBeforeUnload) {
+ LoadUrlAndQuitBrowser(INFINITE_BEFORE_UNLOAD_HTML, L"infinitebeforeunload");
+}
+
// Tests closing the browser on a page with an unload listener registered where
// the unload handler has an infinite loop followed by an alert.
TEST_F(UnloadTest, BrowserCloseInfiniteUnloadAlert) {
LoadUrlAndQuitBrowser(INFINITE_UNLOAD_ALERT_HTML, L"infiniteunloadalert");
}
+// Tests closing the browser with a beforeunload handler that hangs then
+// pops up an alert.
+TEST_F(UnloadTest, BrowserCloseInfiniteBeforeUnloadAlert) {
+ LoadUrlAndQuitBrowser(INFINITE_BEFORE_UNLOAD_ALERT_HTML,
+ L"infinitebeforeunloadalert");
+}
+
// Tests closing the browser on a page with an unload listener registered where
// the unload handler has an 2 second long loop followed by an alert.
TEST_F(UnloadTest, BrowserCloseTwoSecondUnloadAlert) {
LoadUrlAndQuitBrowser(TWO_SECOND_UNLOAD_ALERT_HTML, L"twosecondunloadalert");
}
-// TODO(ojan): Test popping up an alert in the unload handler and test
-// beforeunload. In addition add tests where we open all of these pages
-// in the browser and then close it, as well as having two windows and
-// closing only one of them.
+// Tests closing the browser with a beforeunload handler that takes
+// two seconds to run then pops up an alert.
+TEST_F(UnloadTest, BrowserCloseTwoSecondBeforeUnloadAlert) {
+ LoadUrlAndQuitBrowser(TWO_SECOND_BEFORE_UNLOAD_ALERT_HTML,
+ L"twosecondbeforeunloadalert");
+}
+
+// TODO(ojan): Add tests for unload/beforeunload that have multiple tabs
+// and multiple windows. \ No newline at end of file
diff --git a/chrome/test/ui/ui_test.cc b/chrome/test/ui/ui_test.cc
index c3f0c4b..fbf03f9 100644
--- a/chrome/test/ui/ui_test.cc
+++ b/chrome/test/ui/ui_test.cc
@@ -570,6 +570,11 @@ std::wstring UITest::GetDownloadDirectory() {
return download_directory;
}
+void UITest::CloseBrowserAsync(BrowserProxy* browser) const {
+ server_->Send(
+ new AutomationMsg_CloseBrowserRequest(0, browser->handle()));
+}
+
bool UITest::CloseBrowser(BrowserProxy* browser,
bool* application_closed) const {
DCHECK(application_closed);
diff --git a/chrome/test/ui/ui_test.h b/chrome/test/ui/ui_test.h
index 4b9bf600..8a6aa0a 100644
--- a/chrome/test/ui/ui_test.h
+++ b/chrome/test/ui/ui_test.h
@@ -142,6 +142,11 @@ class UITest : public testing::Test {
bool WaitForBookmarkBarVisibilityChange(BrowserProxy* browser,
bool wait_for_open);
+ // Sends the request to close the browser without blocking.
+ // This is so we can interact with dialogs opened on browser close,
+ // e.g. the beforeunload confirm dialog.
+ void CloseBrowserAsync(BrowserProxy* browser) const;
+
// Closes the specified browser. Returns true if the browser was closed.
// This call is blocking. |application_closed| is set to true if this was
// the last browser window (and therefore as a result of it closing the