summaryrefslogtreecommitdiffstats
path: root/chrome/test
diff options
context:
space:
mode:
authorfinnur@chromium.org <finnur@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-17 17:00:12 +0000
committerfinnur@chromium.org <finnur@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-17 17:00:12 +0000
commitb5464901bd28beacfe68d7b0fae6d258d51e32f3 (patch)
tree9de0768dd47a4b2eef46847a56d6faf738e7bc43 /chrome/test
parent2e116bc834bd71cf306a62d6175ae2baa15851d5 (diff)
downloadchromium_src-b5464901bd28beacfe68d7b0fae6d258d51e32f3.zip
chromium_src-b5464901bd28beacfe68d7b0fae6d258d51e32f3.tar.gz
chromium_src-b5464901bd28beacfe68d7b0fae6d258d51e32f3.tar.bz2
Fix 10573: Dismissing Find-in page doesn't set focus
to the link found. We no longer use the selection controller to highlight the active match. Before this change, the focus would not be set if the user had changed the selection. After this change, the focus will be set unless the user has selected something on the page. I also wrote an in-browser unit test for this to catch this regression in the future, but it is disabled due to problem with running multiple in-process browser tests in a row (teardown problem). BUG=10573 TEST=Covered by in process browser test now, see bug for repro steps. Review URL: http://codereview.chromium.org/79024 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13945 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
-rw-r--r--chrome/test/data/find_in_page/end_state.html45
-rw-r--r--chrome/test/ui_test_utils.cc41
-rw-r--r--chrome/test/ui_test_utils.h40
3 files changed, 124 insertions, 2 deletions
diff --git a/chrome/test/data/find_in_page/end_state.html b/chrome/test/data/find_in_page/end_state.html
new file mode 100644
index 0000000..bf8332c
--- /dev/null
+++ b/chrome/test/data/find_in_page/end_state.html
@@ -0,0 +1,45 @@
+<html>
+<head>
+<script>
+ var focused_elem = "{nothing focused}";
+
+ function setFocusedElem(id) {
+ focused_elem = id;
+ updateLog();
+ }
+
+ function clearFocus() {
+ focused_elem = "";
+ updateLog();
+ }
+
+ function getFocusedElement() {
+ return focused_elem;
+ }
+
+ function updateLog() {
+ document.getElementById("log").innerHTML = "Focused element: " + focused_elem;
+ }
+
+ function selectLink1() {
+ clearFocus();
+ var sel = window.getSelection();
+ var range = document.createRange();
+ range.setStartBefore(document.getElementById('link1'));
+ range.setEndAfter(document.getElementById('link1'));
+ sel.addRange(range);
+ return "";
+ }
+
+</script>
+</head>
+
+<body>
+ This is
+ a <a id="link1" href="http://www.google.com" onfocus="setFocusedElem(this.id)" onblur="clearFocus()">link</a>
+ to <a id="link2" href="http://www.google.com" onfocus="setFocusedElem(this.id)" onblur="clearFocus()">Google</a>.
+
+ <br><br><br>
+ <div id="log"><div>
+</body>
+</html> \ No newline at end of file
diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc
index 6af1091..be86d17 100644
--- a/chrome/test/ui_test_utils.cc
+++ b/chrome/test/ui_test_utils.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -6,6 +6,7 @@
#include "base/message_loop.h"
#include "chrome/browser/browser.h"
+#include "chrome/browser/dom_operation_notification_details.h"
#include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/browser/tab_contents/web_contents.h"
#include "chrome/common/notification_registrar.h"
@@ -98,4 +99,42 @@ void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
WaitForNavigations(controller, number_of_navigations);
}
+JavaScriptRunner::JavaScriptRunner(WebContents* web_contents,
+ const std::wstring& frame_xpath,
+ const std::wstring& jscript)
+ : web_contents_(web_contents),
+ frame_xpath_(frame_xpath),
+ jscript_(jscript) {
+ NotificationService::current()->
+ AddObserver(this, NotificationType::DOM_OPERATION_RESPONSE,
+ Source<WebContents>(web_contents));
+}
+
+void JavaScriptRunner::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ Details<DomOperationNotificationDetails> dom_op_details(details);
+ result_ = dom_op_details->json();
+ // The Jasonified response has quotes, remove them.
+ if (result_.length() > 1 && result_[0] == '"')
+ result_ = result_.substr(1, result_.length() - 2);
+
+ NotificationService::current()->
+ RemoveObserver(this, NotificationType::DOM_OPERATION_RESPONSE,
+ Source<WebContents>(web_contents_));
+ MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
+}
+
+std::string JavaScriptRunner::Run() {
+ // The DOMAutomationController requires an automation ID, even though we are
+ // not using it in this case.
+ web_contents_->render_view_host()->ExecuteJavascriptInWebFrame(
+ frame_xpath_, L"window.domAutomationController.setAutomationId(0);");
+
+ web_contents_->render_view_host()->ExecuteJavascriptInWebFrame(frame_xpath_,
+ jscript_);
+ ui_test_utils::RunMessageLoop();
+ return result_;
+}
+
} // namespace ui_test_utils
diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h
index ced32d2..2ac73f9 100644
--- a/chrome/test/ui_test_utils.h
+++ b/chrome/test/ui_test_utils.h
@@ -1,13 +1,17 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_TEST_UI_TEST_UTILS_H_
#define CHROME_TEST_UI_TEST_UTILS_H_
+#include "base/basictypes.h"
+#include "chrome/common/notification_observer.h"
+
class Browser;
class GURL;
class NavigationController;
+class WebContents;
// A collections of functions designed for use with InProcessBrowserTest.
namespace ui_test_utils {
@@ -35,6 +39,40 @@ void NavigateToURL(Browser* browser, const GURL& url);
void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
const GURL& url,
int number_of_navigations);
+
+
+// This class enables you to send JavaScript as a string from the browser to the
+// renderer for execution in a frame of your choice.
+class JavaScriptRunner : public NotificationObserver {
+ public:
+ // Constructor. |web_contents| is a pointer to the WebContents you want to run
+ // the JavaScript code in. |frame_xpath| is a path to the frame to run it in.
+ // |jscript| is a string containing the JavaScript code to run, for example:
+ // "window.domAutomationController.send(alert('hello world'));". The
+ // JavaScript code will execute when Run is called. Note: In order for the
+ // domAutomationController to work, you must call EnableDOMAutomation() in
+ // your test class first.
+ JavaScriptRunner(WebContents* web_contents,
+ const std::wstring& frame_xpath,
+ const std::wstring& jscript);
+
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details);
+
+ // Executes the JavaScript code passed in to the constructor. See also comment
+ // about EnableDOMAutomation in the constructor.
+ std::string Run();
+
+ private:
+ WebContents* web_contents_;
+ std::wstring frame_xpath_;
+ std::wstring jscript_;
+ std::string result_;
+
+ DISALLOW_COPY_AND_ASSIGN(JavaScriptRunner);
+};
+
}
#endif // CHROME_TEST_UI_TEST_UTILS_H_