diff options
author | isherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-30 21:50:05 +0000 |
---|---|---|
committer | isherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-30 21:50:05 +0000 |
commit | 9fabbf77b5b467003287b055aece906a4330de86 (patch) | |
tree | 3727f1d511342a55c9ece39c7a7b264f027081f8 | |
parent | ee520884a87f24a5e34ef660657d49f2ec993f6e (diff) | |
download | chromium_src-9fabbf77b5b467003287b055aece906a4330de86.zip chromium_src-9fabbf77b5b467003287b055aece906a4330de86.tar.gz chromium_src-9fabbf77b5b467003287b055aece906a4330de86.tar.bz2 |
Make ui_test_utils::ExecuteJavaScript() friendlier to use. Also, add WARN_UNUSED_RESULT to make it less likely that callers ignore failures.
BUG=57216
TEST=tests should still run
Review URL: http://codereview.chromium.org/3569005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@61119 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/extensions/app_process_apitest.cc | 13 | ||||
-rw-r--r-- | chrome/browser/extensions/browser_action_apitest.cc | 4 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_browsertests_misc.cc | 106 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_devtools_browsertests.cc | 28 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_incognito_apitest.cc | 8 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_startup_browsertest.cc | 8 | ||||
-rw-r--r-- | chrome/browser/find_bar_host_browsertest.cc | 31 | ||||
-rw-r--r-- | chrome/browser/geolocation/geolocation_browsertest.cc | 8 | ||||
-rw-r--r-- | chrome/browser/in_process_webkit/indexed_db_browsertest.cc | 14 | ||||
-rw-r--r-- | chrome/renderer/automation/dom_automation_controller.cc | 10 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.cc | 108 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.h | 18 |
12 files changed, 187 insertions, 169 deletions
diff --git a/chrome/browser/extensions/app_process_apitest.cc b/chrome/browser/extensions/app_process_apitest.cc index 689a88c..deca3b0 100644 --- a/chrome/browser/extensions/app_process_apitest.cc +++ b/chrome/browser/extensions/app_process_apitest.cc @@ -23,13 +23,8 @@ static void WindowOpenHelper(Browser* browser, RenderViewHost* opener_host, const GURL& url, bool newtab_process_should_equal_opener) { - bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - opener_host, L"", - L"window.open('" + UTF8ToWide(url.spec()) + L"');" - L"window.domAutomationController.send(true);", - &result); - ASSERT_TRUE(result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( + opener_host, L"", L"window.open('" + UTF8ToWide(url.spec()) + L"');")); // The above window.open call is not user-initiated, it will create // a popup window instead of a new tab in current window. @@ -51,13 +46,13 @@ static void WindowOpenHelper(Browser* browser, // Simulates a page navigating itself to an URL, and waits for the navigation. static void NavigateTabHelper(TabContents* contents, const GURL& url) { bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( contents->render_view_host(), L"", L"window.addEventListener('unload', function() {" L" window.domAutomationController.send(true);" L"}, false);" L"window.location = '" + UTF8ToWide(url.spec()) + L"';", - &result); + &result)); ASSERT_TRUE(result); if (!contents->controller().GetLastCommittedEntry() || diff --git a/chrome/browser/extensions/browser_action_apitest.cc b/chrome/browser/extensions/browser_action_apitest.cc index 82f1320..67e533b 100644 --- a/chrome/browser/extensions/browser_action_apitest.cc +++ b/chrome/browser/extensions/browser_action_apitest.cc @@ -76,12 +76,12 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) { // Verify the command worked. TabContents* tab = browser()->GetSelectedTabContents(); bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( tab->render_view_host(), L"", L"setInterval(function(){" L" if(document.body.bgColor == 'red'){" L" window.domAutomationController.send(true)}}, 100)", - &result); + &result)); ASSERT_TRUE(result); } diff --git a/chrome/browser/extensions/extension_browsertests_misc.cc b/chrome/browser/extensions/extension_browsertests_misc.cc index 9a466bd..012420a 100644 --- a/chrome/browser/extensions/extension_browsertests_misc.cc +++ b/chrome/browser/extensions/extension_browsertests_misc.cc @@ -98,19 +98,19 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, OriginPrivileges) { ui_test_utils::NavigateToURL( browser(), origin_privileges_index.ReplaceComponents(make_host_a_com)); std::string result; - ui_test_utils::ExecuteJavaScriptAndExtractString( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( browser()->GetSelectedTabContents()->render_view_host(), L"", L"window.domAutomationController.send(document.title)", - &result); + &result)); EXPECT_EQ(result, "Loaded"); // A web host that does not have permission. ui_test_utils::NavigateToURL( browser(), origin_privileges_index.ReplaceComponents(make_host_b_com)); - ui_test_utils::ExecuteJavaScriptAndExtractString( - browser()->GetSelectedTabContents()->render_view_host(), L"", + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( + browser()->GetSelectedTabContents()->render_view_host(), L"", L"window.domAutomationController.send(document.title)", - &result); + &result)); EXPECT_EQ(result, "Image failed to load"); // A data URL. Data URLs should always be able to load chrome-extension:// @@ -121,10 +121,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, OriginPrivileges) { .AppendASCII("index.html"), &file_source)); ui_test_utils::NavigateToURL(browser(), GURL(std::string("data:text/html;charset=utf-8,") + file_source)); - ui_test_utils::ExecuteJavaScriptAndExtractString( - browser()->GetSelectedTabContents()->render_view_host(), L"", + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( + browser()->GetSelectedTabContents()->render_view_host(), L"", L"window.domAutomationController.send(document.title)", - &result); + &result)); EXPECT_EQ(result, "Loaded"); // A different extension. Extensions should always be able to load each @@ -134,10 +134,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, OriginPrivileges) { ui_test_utils::NavigateToURL( browser(), GURL("chrome-extension://pbkkcbgdkliohhfaeefcijaghglkahja/index.html")); - ui_test_utils::ExecuteJavaScriptAndExtractString( - browser()->GetSelectedTabContents()->render_view_host(), L"", + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( + browser()->GetSelectedTabContents()->render_view_host(), L"", L"window.domAutomationController.send(document.title)", - &result); + &result)); EXPECT_EQ(result, "Loaded"); } @@ -154,9 +154,9 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, TabContents) { GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/page.html")); bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( browser()->GetSelectedTabContents()->render_view_host(), L"", - L"testTabsAPI()", &result); + L"testTabsAPI()", &result)); EXPECT_TRUE(result); // There was a bug where we would crash if we navigated to a page in the same @@ -166,9 +166,9 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, TabContents) { browser(), GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/page.html")); result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( browser()->GetSelectedTabContents()->render_view_host(), L"", - L"testTabsAPI()", &result); + L"testTabsAPI()", &result)); EXPECT_TRUE(result); } @@ -610,22 +610,20 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, LastError) { ExtensionHost* host = FindHostWithPath(manager, "/bg.html", 1); bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testLastError()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + host->render_view_host(), L"", L"testLastError()", &result)); EXPECT_TRUE(result); } // Helper function for common code shared by the 3 WindowOpen tests below. -static TabContents* WindowOpenHelper(Browser* browser, const GURL& start_url, - const std::string& newtab_url) { +static void WindowOpenHelper(Browser* browser, const GURL& start_url, + const std::string& newtab_url, + TabContents** newtab_result) { ui_test_utils::NavigateToURL(browser, start_url); - bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( browser->GetSelectedTabContents()->render_view_host(), L"", - L"window.open('" + UTF8ToWide(newtab_url) + L"');" - L"window.domAutomationController.send(true);", &result); - EXPECT_TRUE(result); + L"window.open('" + UTF8ToWide(newtab_url) + L"');")); // Now the active tab in last active window should be the new tab. Browser* last_active_browser = BrowserList::GetLastActive(); @@ -636,9 +634,10 @@ static TabContents* WindowOpenHelper(Browser* browser, const GURL& start_url, if (!newtab->controller().GetLastCommittedEntry() || newtab->controller().GetLastCommittedEntry()->url() != expected_url) ui_test_utils::WaitForNavigation(&newtab->controller()); - EXPECT_EQ(expected_url, newtab->controller().GetLastCommittedEntry()->url()); - - return newtab; + EXPECT_EQ(expected_url, + newtab->controller().GetLastCommittedEntry()->url()); + if (newtab_result) + *newtab_result = newtab; } // Tests that an extension page can call window.open to an extension URL and @@ -647,15 +646,16 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) { ASSERT_TRUE(LoadExtension( test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"))); - TabContents* newtab = WindowOpenHelper( + TabContents* newtab; + ASSERT_NO_FATAL_FAILURE(WindowOpenHelper( browser(), GURL(std::string("chrome-extension://") + last_loaded_extension_id_ + "/test.html"), - "newtab.html"); + "newtab.html", &newtab)); bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - newtab->render_view_host(), L"", L"testExtensionApi()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + newtab->render_view_host(), L"", L"testExtensionApi()", &result)); EXPECT_TRUE(result); } @@ -665,11 +665,11 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) { ASSERT_TRUE(LoadExtension( test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"))); - WindowOpenHelper( + ASSERT_NO_FATAL_FAILURE(WindowOpenHelper( browser(), GURL(std::string("chrome-extension://") + last_loaded_extension_id_ + "/test.html"), - "chrome-extension://thisissurelynotavalidextensionid/newtab.html"); + "chrome-extension://thisissurelynotavalidextensionid/newtab.html", NULL)); // If we got to this point, we didn't crash, so we're good. } @@ -682,16 +682,18 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) { ASSERT_TRUE(LoadExtension( test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"))); - TabContents* newtab = WindowOpenHelper( + TabContents* newtab; + ASSERT_NO_FATAL_FAILURE(WindowOpenHelper( browser(), GURL("about:blank"), std::string("chrome-extension://") + last_loaded_extension_id_ + - "/newtab.html"); + "/newtab.html", + &newtab)); // Extension API should succeed. bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - newtab->render_view_host(), L"", L"testExtensionApi()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + newtab->render_view_host(), L"", L"testExtensionApi()", &result)); EXPECT_TRUE(result); } @@ -717,8 +719,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, MAYBE_PluginLoadUnload) { // With no extensions, the plugin should not be loaded. bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - tab->render_view_host(), L"", L"testPluginWorks()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + tab->render_view_host(), L"", L"testPluginWorks()", &result)); EXPECT_FALSE(result); ExtensionsService* service = browser()->profile()->GetExtensionsService(); @@ -727,13 +729,13 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, MAYBE_PluginLoadUnload) { EXPECT_EQ(size_before + 1, service->extensions()->size()); // Now the plugin should be in the cache, but we have to reload the page for // it to work. - ui_test_utils::ExecuteJavaScriptAndExtractBool( - tab->render_view_host(), L"", L"testPluginWorks()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + tab->render_view_host(), L"", L"testPluginWorks()", &result)); EXPECT_FALSE(result); browser()->Reload(CURRENT_TAB); ui_test_utils::WaitForNavigationInCurrentTab(browser()); - ui_test_utils::ExecuteJavaScriptAndExtractBool( - tab->render_view_host(), L"", L"testPluginWorks()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + tab->render_view_host(), L"", L"testPluginWorks()", &result)); EXPECT_TRUE(result); EXPECT_EQ(size_before + 1, service->extensions()->size()); @@ -742,8 +744,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, MAYBE_PluginLoadUnload) { // Now the plugin should be unloaded, and the page should be broken. - ui_test_utils::ExecuteJavaScriptAndExtractBool( - tab->render_view_host(), L"", L"testPluginWorks()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + tab->render_view_host(), L"", L"testPluginWorks()", &result)); EXPECT_FALSE(result); // If we reload the extension and page, it should work again. @@ -752,8 +754,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, MAYBE_PluginLoadUnload) { EXPECT_EQ(size_before + 1, service->extensions()->size()); browser()->Reload(CURRENT_TAB); ui_test_utils::WaitForNavigationInCurrentTab(browser()); - ui_test_utils::ExecuteJavaScriptAndExtractBool( - tab->render_view_host(), L"", L"testPluginWorks()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + tab->render_view_host(), L"", L"testPluginWorks()", &result)); EXPECT_TRUE(result); } @@ -764,7 +766,6 @@ static const wchar_t* jscript_click_option_button = L" document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE," L" null).snapshotItem(0);" L" button.click();" - L" window.domAutomationController.send(0);" L"})();"; // Test that an extension with an options page makes an 'Options' button appear @@ -782,9 +783,9 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_OptionsPage) { // Go to the chrome://extensions page and click the Options button. ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIExtensionsURL)); TabStripModel* tab_strip = browser()->tabstrip_model(); - TabContents* extensions_tab = browser()->GetSelectedTabContents(); - ui_test_utils::ExecuteJavaScript(extensions_tab->render_view_host(), L"", - jscript_click_option_button); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( + browser()->GetSelectedTabContents()->render_view_host(), L"", + jscript_click_option_button)); // If the options page hasn't already come up, wait for it. if (tab_strip->count() == 1) { @@ -849,8 +850,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, PropertyAppIsInstalled) { // Test that trying to set window.chrome.app.isInstalled throws // an exception. - ASSERT_TRUE( - ui_test_utils::ExecuteJavaScriptAndExtractString( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( browser()->GetSelectedTabContents()->render_view_host(), L"", L"window.domAutomationController.send(" diff --git a/chrome/browser/extensions/extension_devtools_browsertests.cc b/chrome/browser/extensions/extension_devtools_browsertests.cc index 4afcb3c..c195aa7 100644 --- a/chrome/browser/extensions/extension_devtools_browsertests.cc +++ b/chrome/browser/extensions/extension_devtools_browsertests.cc @@ -69,8 +69,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionDevToolsBrowserTest, FLAKY_TimelineApi) { bool result = false; std::wstring register_listeners_js = base::StringPrintf( L"setListenersOnTab(%d)", tab_id); - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", register_listeners_js, &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + host->render_view_host(), L"", register_listeners_js, &result)); EXPECT_TRUE(result); // Setting the events should have caused an ExtensionDevToolsBridge to be @@ -85,16 +85,16 @@ IN_PROC_BROWSER_TEST_F(ExtensionDevToolsBrowserTest, FLAKY_TimelineApi) { DevToolsClientMsg_DispatchToAPU pageEventMessage(""); devtools_client_host->SendMessageToClient(pageEventMessage); - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testReceivePageEvent()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + host->render_view_host(), L"", L"testReceivePageEvent()", &result)); EXPECT_TRUE(result); // Test onTabClose event. result = false; devtools_manager->UnregisterDevToolsClientHostFor( tab_contents->render_view_host()); - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testReceiveTabCloseEvent()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + host->render_view_host(), L"", L"testReceiveTabCloseEvent()", &result)); EXPECT_TRUE(result); } @@ -125,8 +125,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionDevToolsBrowserTest, ProcessRefCounting) { bool result = false; std::wstring register_listeners_js = base::StringPrintf( L"setListenersOnTab(%d)", tab_id); - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host_one->render_view_host(), L"", register_listeners_js, &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + host_one->render_view_host(), L"", register_listeners_js, &result)); EXPECT_TRUE(result); // Setting the event listeners should have caused an ExtensionDevToolsBridge @@ -137,15 +137,15 @@ IN_PROC_BROWSER_TEST_F(ExtensionDevToolsBrowserTest, ProcessRefCounting) { // Register listeners from the second extension as well. std::wstring script = base::StringPrintf(L"registerListenersForTab(%d)", tab_id); - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host_two->render_view_host(), L"", script, &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + host_two->render_view_host(), L"", script, &result)); EXPECT_TRUE(result); // Removing the listeners from the first extension should leave the bridge // alive. result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host_one->render_view_host(), L"", L"unregisterListeners()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + host_one->render_view_host(), L"", L"unregisterListeners()", &result)); EXPECT_TRUE(result); ASSERT_TRUE(devtools_manager->GetDevToolsClientHostFor( tab_contents->render_view_host())); @@ -153,8 +153,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionDevToolsBrowserTest, ProcessRefCounting) { // Removing the listeners from the second extension should tear the bridge // down. result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host_two->render_view_host(), L"", L"unregisterListeners()", &result); + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( + host_two->render_view_host(), L"", L"unregisterListeners()", &result)); EXPECT_TRUE(result); ASSERT_FALSE(devtools_manager->GetDevToolsClientHostFor( tab_contents->render_view_host())); diff --git a/chrome/browser/extensions/extension_incognito_apitest.cc b/chrome/browser/extensions/extension_incognito_apitest.cc index c287fda..cd29ab8 100644 --- a/chrome/browser/extensions/extension_incognito_apitest.cc +++ b/chrome/browser/extensions/extension_incognito_apitest.cc @@ -35,10 +35,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, IncognitoNoScript) { // Verify the script didn't run. bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( tab->render_view_host(), L"", L"window.domAutomationController.send(document.title == 'Unmodified')", - &result); + &result)); EXPECT_TRUE(result); } @@ -71,10 +71,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, IncognitoYesScript) { // Verify the script ran. bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( tab->render_view_host(), L"", L"window.domAutomationController.send(document.title == 'modified')", - &result); + &result)); EXPECT_TRUE(result); } diff --git a/chrome/browser/extensions/extension_startup_browsertest.cc b/chrome/browser/extensions/extension_startup_browsertest.cc index 5342bc1..3f961f7 100644 --- a/chrome/browser/extensions/extension_startup_browsertest.cc +++ b/chrome/browser/extensions/extension_startup_browsertest.cc @@ -116,19 +116,19 @@ class ExtensionStartupTestBase : public InProcessBrowserTest { ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( browser()->GetSelectedTabContents()->render_view_host(), L"", L"window.domAutomationController.send(" L"document.defaultView.getComputedStyle(document.body, null)." L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", - &result); + &result)); EXPECT_EQ(expect_css, result); result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( browser()->GetSelectedTabContents()->render_view_host(), L"", L"window.domAutomationController.send(document.title == 'Modified')", - &result); + &result)); EXPECT_EQ(expect_script, result); } diff --git a/chrome/browser/find_bar_host_browsertest.cc b/chrome/browser/find_bar_host_browsertest.cc index 11c5f3e..22a1c26 100644 --- a/chrome/browser/find_bar_host_browsertest.cc +++ b/chrome/browser/find_bar_host_browsertest.cc @@ -195,14 +195,16 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFrames) { EXPECT_EQ(0, ordinal); } -std::string FocusedOnPage(TabContents* tab_contents) { - std::string result; - ui_test_utils::ExecuteJavaScriptAndExtractString( +// Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. +bool FocusedOnPage(TabContents* tab_contents, std::string* result) + WARN_UNUSED_RESULT; + +bool FocusedOnPage(TabContents* tab_contents, std::string* result) { + return ui_test_utils::ExecuteJavaScriptAndExtractString( tab_contents->render_view_host(), L"", L"window.domAutomationController.send(getFocusedElement());", - &result); - return result; + result); } // This tests the FindInPage end-state, in other words: what is focused when you @@ -219,7 +221,9 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageEndState) { ASSERT_TRUE(NULL != tab_contents); // Verify that nothing has focus. - ASSERT_STREQ("{nothing focused}", FocusedOnPage(tab_contents).c_str()); + std::string result; + ASSERT_TRUE(FocusedOnPage(tab_contents, &result)); + ASSERT_STREQ("{nothing focused}", result.c_str()); // Search for a text that exists within a link on the page. int ordinal = 0; @@ -231,7 +235,8 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageEndState) { tab_contents->StopFinding(FindBarController::kKeepSelection); // Verify that the link is focused. - EXPECT_STREQ("link1", FocusedOnPage(tab_contents).c_str()); + ASSERT_TRUE(FocusedOnPage(tab_contents, &result)); + EXPECT_STREQ("link1", result.c_str()); // Search for a text that exists within a link on the page. EXPECT_EQ(1, FindInPageWchar(tab_contents, L"Google", @@ -239,18 +244,18 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageEndState) { EXPECT_EQ(1, ordinal); // Move the selection to link 1, after searching. - std::string result; - ui_test_utils::ExecuteJavaScriptAndExtractString( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( tab_contents->render_view_host(), L"", L"window.domAutomationController.send(selectLink1());", - &result); + &result)); // End the find session. tab_contents->StopFinding(FindBarController::kKeepSelection); // Verify that link2 is not focused. - EXPECT_STREQ("", FocusedOnPage(tab_contents).c_str()); + ASSERT_TRUE(FocusedOnPage(tab_contents, &result)); + EXPECT_STREQ("", result.c_str()); } // This test loads a single-frame page and makes sure the ordinal returned makes @@ -314,11 +319,11 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, // Move the selection to link 1, after searching. std::string result; - ui_test_utils::ExecuteJavaScriptAndExtractString( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( tab_contents->render_view_host(), L"", L"window.domAutomationController.send(selectLink1());", - &result); + &result)); // Do a find-next after the selection. This should move forward // from there to the 3rd instance of 'google'. diff --git a/chrome/browser/geolocation/geolocation_browsertest.cc b/chrome/browser/geolocation/geolocation_browsertest.cc index 382b614..6617f90 100644 --- a/chrome/browser/geolocation/geolocation_browsertest.cc +++ b/chrome/browser/geolocation/geolocation_browsertest.cc @@ -62,9 +62,9 @@ class IFrameLoader : public NotificationObserver { script = StringPrintf( "window.domAutomationController.send(getIFrameSrc(%d))", iframe_id); std::string iframe_src; - ui_test_utils::ExecuteJavaScriptAndExtractString( + EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( browser->GetSelectedTabContents()->render_view_host(), - L"", UTF8ToWide(script), &iframe_src); + L"", UTF8ToWide(script), &iframe_src)); iframe_url_ = GURL(iframe_src); } @@ -321,9 +321,9 @@ class GeolocationBrowserTest : public InProcessBrowserTest { std::string script = StringPrintf( "window.domAutomationController.send(%s)", function.c_str()); std::string result; - ui_test_utils::ExecuteJavaScriptAndExtractString( + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( tab_contents->render_view_host(), - iframe_xpath_, UTF8ToWide(script), &result); + iframe_xpath_, UTF8ToWide(script), &result)); EXPECT_EQ(expected, result); } diff --git a/chrome/browser/in_process_webkit/indexed_db_browsertest.cc b/chrome/browser/in_process_webkit/indexed_db_browsertest.cc index 15f004e..5ea451a 100644 --- a/chrome/browser/in_process_webkit/indexed_db_browsertest.cc +++ b/chrome/browser/in_process_webkit/indexed_db_browsertest.cc @@ -67,15 +67,6 @@ class IndexedDBBrowserTest : public InProcessBrowserTest { return ui_test_utils::GetTestUrl(kTestDir, file_path); } - std::string GetTestLog() { - std::string script = "window.domAutomationController.send(getLog())"; - std::string js_result; - ui_test_utils::ExecuteJavaScriptAndExtractString( - browser()->GetSelectedTabContents()->render_view_host(), - L"", UTF8ToWide(script), &js_result); - return js_result; - } - void SimpleTest(const GURL& test_url) { // The test page will open a cursor on IndexedDB, then navigate to either a // #pass or #fail ref. @@ -83,7 +74,10 @@ class IndexedDBBrowserTest : public InProcessBrowserTest { browser(), test_url, 2); std::string result = browser()->GetSelectedTabContents()->GetURL().ref(); if (result != "pass") { - std::string js_result = GetTestLog(); + std::string js_result; + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( + browser()->GetSelectedTabContents()->render_view_host(), L"", + L"window.domAutomationController.send(getLog())", &js_result)); FAIL() << "Failed: " << js_result; } } diff --git a/chrome/renderer/automation/dom_automation_controller.cc b/chrome/renderer/automation/dom_automation_controller.cc index 6c3e8a5..8d708d1 100644 --- a/chrome/renderer/automation/dom_automation_controller.cc +++ b/chrome/renderer/automation/dom_automation_controller.cc @@ -72,19 +72,17 @@ void DomAutomationController::Send(const CppArgumentList& args, } } - bool succeeded = serializer.Serialize(*value); - if (!succeeded) { + if (!serializer.Serialize(*value)) { result->SetNull(); return; } - succeeded = sender_->Send( - new ViewHostMsg_DomOperationResponse(routing_id_, json, automation_id_)); + bool succeeded = sender_->Send( + new ViewHostMsg_DomOperationResponse(routing_id_, json, automation_id_)); + result->Set(succeeded); automation_id_ = MSG_ROUTING_NONE; - result->Set(succeeded); - return; } void DomAutomationController::SendJSON(const CppArgumentList& args, diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc index 964ae60..f570707 100644 --- a/chrome/test/ui_test_utils.cc +++ b/chrome/test/ui_test_utils.cc @@ -109,13 +109,11 @@ class DOMOperationObserver : public NotificationObserver { MessageLoopForUI::current()->Quit(); } - bool GetResponse(std::string* response) { + bool GetResponse(std::string* response) WARN_UNUSED_RESULT { *response = response_; return did_respond_; } - std::string response() const { return response_; } - private: NotificationRegistrar registrar_; std::string response_; @@ -277,18 +275,18 @@ class InProcessJavaScriptExecutionController protected: // Executes |script| and sets the JSON response |json|. - bool ExecuteJavaScriptAndGetJSON(const std::string& script, - std::string* json) { + virtual bool ExecuteJavaScriptAndGetJSON(const std::string& script, + std::string* json) { render_view_host_->ExecuteJavascriptInWebFrame(L"", UTF8ToWide(script)); DOMOperationObserver dom_op_observer(render_view_host_); return dom_op_observer.GetResponse(json); } - void FirstObjectAdded() { + virtual void FirstObjectAdded() { AddRef(); } - void LastObjectRemoved() { + virtual void LastObjectRemoved() { Release(); } @@ -297,6 +295,52 @@ class InProcessJavaScriptExecutionController RenderViewHost* render_view_host_; }; +// Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. +bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, + const std::wstring& frame_xpath, + const std::wstring& original_script, + scoped_ptr<Value>* result) WARN_UNUSED_RESULT; + +// Executes the passed |original_script| in the frame pointed to by +// |frame_xpath|. If |result| is not NULL, stores the value that the evaluation +// of the script in |result|. Returns true on success. +bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, + const std::wstring& frame_xpath, + const std::wstring& original_script, + scoped_ptr<Value>* result) { + // TODO(jcampan): we should make the domAutomationController not require an + // automation id. + std::wstring script = L"window.domAutomationController.setAutomationId(0);" + + original_script; + render_view_host->ExecuteJavascriptInWebFrame(frame_xpath, script); + DOMOperationObserver dom_op_observer(render_view_host); + std::string json; + if (!dom_op_observer.GetResponse(&json)) + return false; + + // Nothing more to do for callers that ignore the returned JS value. + if (!result) + return true; + + // Wrap |json| in an array before deserializing because valid JSON has an + // array or an object as the root. + json.insert(0, "["); + json.append("]"); + + scoped_ptr<Value> root_val(base::JSONReader::Read(json, true)); + if (!root_val->IsType(Value::TYPE_LIST)) + return false; + + ListValue* list = static_cast<ListValue*>(root_val.get()); + Value* result_val; + if (!list || !list->GetSize() || + !list->Remove(0, &result_val)) // Remove gives us ownership of the value. + return false; + + result->reset(result_val); + return true; +} + } // namespace void RunMessageLoop() { @@ -410,32 +454,12 @@ DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { return executor->GetObjectProxy<DOMElementProxy>(element_handle); } -Value* ExecuteJavaScript(RenderViewHost* render_view_host, - const std::wstring& frame_xpath, - const std::wstring& original_script) { - // TODO(jcampan): we should make the domAutomationController not require an - // automation id. - std::wstring script = L"window.domAutomationController.setAutomationId(0);" + - original_script; - render_view_host->ExecuteJavascriptInWebFrame(frame_xpath, script); - DOMOperationObserver dom_op_observer(render_view_host); - std::string json = dom_op_observer.response(); - // Wrap |json| in an array before deserializing because valid JSON has an - // array or an object as the root. - json.insert(0, "["); - json.append("]"); - - scoped_ptr<Value> root_val(base::JSONReader::Read(json, true)); - if (!root_val->IsType(Value::TYPE_LIST)) - return NULL; - - ListValue* list = static_cast<ListValue*>(root_val.get()); - Value* result; - if (!list || !list->GetSize() || - !list->Remove(0, &result)) // Remove gives us ownership of the value. - return NULL; - - return result; +bool ExecuteJavaScript(RenderViewHost* render_view_host, + const std::wstring& frame_xpath, + const std::wstring& original_script) { + std::wstring script = + original_script + L"window.domAutomationController.send(0);"; + return ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, NULL); } bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, @@ -443,9 +467,9 @@ bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, const std::wstring& script, int* result) { DCHECK(result); - scoped_ptr<Value> value(ExecuteJavaScript(render_view_host, frame_xpath, - script)); - if (!value.get()) + scoped_ptr<Value> value; + if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || + !value.get()) return false; return value->GetAsInteger(result); @@ -456,9 +480,9 @@ bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host, const std::wstring& script, bool* result) { DCHECK(result); - scoped_ptr<Value> value(ExecuteJavaScript(render_view_host, frame_xpath, - script)); - if (!value.get()) + scoped_ptr<Value> value; + if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || + !value.get()) return false; return value->GetAsBoolean(result); @@ -469,9 +493,9 @@ bool ExecuteJavaScriptAndExtractString(RenderViewHost* render_view_host, const std::wstring& script, std::string* result) { DCHECK(result); - scoped_ptr<Value> value(ExecuteJavaScript(render_view_host, frame_xpath, - script)); - if (!value.get()) + scoped_ptr<Value> value; + if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || + !value.get()) return false; return value->GetAsString(result); diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h index 9e6a2e1..fef6f6b5 100644 --- a/chrome/test/ui_test_utils.h +++ b/chrome/test/ui_test_utils.h @@ -107,11 +107,13 @@ void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, DOMElementProxyRef GetActiveDOMDocument(Browser* browser); // Executes the passed |script| in the frame pointed to by |frame_xpath| (use -// empty string for main frame) and returns the value the evaluation of the -// script returned. The caller owns the returned value. -Value* ExecuteJavaScript(RenderViewHost* render_view_host, - const std::wstring& frame_xpath, - const std::wstring& script); +// empty string for main frame). The |script| should not invoke +// domAutomationController.send(); otherwise, your test will hang or be flaky. +// If you want to extract a result, use one of the below functions. +// Returns true on success. +bool ExecuteJavaScript(RenderViewHost* render_view_host, + const std::wstring& frame_xpath, + const std::wstring& script) WARN_UNUSED_RESULT; // The following methods executes the passed |script| in the frame pointed to by // |frame_xpath| (use empty string for main frame) and sets |result| to the @@ -123,15 +125,15 @@ Value* ExecuteJavaScript(RenderViewHost* render_view_host, bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, const std::wstring& frame_xpath, const std::wstring& script, - int* result); + int* result) WARN_UNUSED_RESULT; bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host, const std::wstring& frame_xpath, const std::wstring& script, - bool* result); + bool* result) WARN_UNUSED_RESULT; bool ExecuteJavaScriptAndExtractString(RenderViewHost* render_view_host, const std::wstring& frame_xpath, const std::wstring& script, - std::string* result); + std::string* result) WARN_UNUSED_RESULT; // Generate the file path for testing a particular test. // The file for the tests is all located in |