Index: chrome/browser/extensions/api/web_navigation/web_navigation_apitest.cc |
diff --git a/chrome/browser/extensions/api/web_navigation/web_navigation_apitest.cc b/chrome/browser/extensions/api/web_navigation/web_navigation_apitest.cc |
index 1b4cfb7a4335f728fc6fb79c1dd45cf43c230f60..efb77bfcc18c483ca48e55397f848dde03ce483e 100644 |
--- a/chrome/browser/extensions/api/web_navigation/web_navigation_apitest.cc |
+++ b/chrome/browser/extensions/api/web_navigation/web_navigation_apitest.cc |
@@ -2,14 +2,20 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include "base/utf_string_conversions.h" |
+#include "base/stringprintf.h" |
#include "chrome/app/chrome_command_ids.h" |
+#include "chrome/browser/browser_process.h" |
#include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" |
#include "chrome/browser/extensions/extension_apitest.h" |
#include "chrome/browser/extensions/extension_service.h" |
#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h" |
+#include "chrome/browser/renderer_host/test_navigation_listener.h" |
#include "chrome/browser/tab_contents/render_view_context_menu.h" |
#include "chrome/browser/ui/browser.h" |
#include "chrome/browser/ui/browser_tabstrip.h" |
+#include "chrome/common/chrome_notification_types.h" |
#include "chrome/common/chrome_switches.h" |
#include "chrome/test/base/ui_test_utils.h" |
#include "content/public/browser/render_view_host.h" |
@@ -44,6 +50,85 @@ class TestRenderViewContextMenu : public RenderViewContextMenu { |
DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); |
}; |
+// Waits for a WC to be created. Once it starts loading |delay_url| (after at |
+// least the first navigation has committed), it delays the load, executes |
+// |script| in the last committed RVH and resumes the load when |until_url| |
+// commits. |
battre
2012/08/07 13:34:40
Add comment, that |script| is supposed to trigger
|
+class DelayLoadStartAndExecuteJavascript |
+ : public content::NotificationObserver, |
+ public content::WebContentsObserver { |
+ public: |
+ DelayLoadStartAndExecuteJavascript( |
+ TestNavigationListener* test_navigation_listener, |
+ const GURL& delay_url, |
+ const GURL& until_url, |
+ const std::string& script) |
battre
2012/08/07 13:34:40
opt: change the order to delay_url -> script -> un
|
+ : content::WebContentsObserver(), |
+ test_navigation_listener_(test_navigation_listener), |
+ delay_url_(delay_url), |
+ until_url_(until_url), |
+ script_(script), |
+ script_was_executed_(false), |
+ rvh_(NULL) { |
+ registrar_.Add(this, |
+ chrome::NOTIFICATION_TAB_ADDED, |
+ content::NotificationService::AllSources()); |
+ test_navigation_listener_->DelayRequestsForURL(delay_url_); |
+ } |
+ virtual ~DelayLoadStartAndExecuteJavascript() {} |
+ |
+ virtual void Observe(int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) OVERRIDE { |
+ if (type != chrome::NOTIFICATION_TAB_ADDED) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ content::WebContentsObserver::Observe( |
+ content::Details<content::WebContents>(details).ptr()); |
+ registrar_.RemoveAll(); |
+ } |
+ |
+ virtual void DidStartProvisionalLoadForFrame( |
+ int64 frame_id, |
+ bool is_main_frame, |
+ const GURL& validated_url, |
+ bool is_error_page, |
+ content::RenderViewHost* render_view_host) OVERRIDE { |
+ if (validated_url != delay_url_ || !rvh_) |
+ return; |
+ |
+ rvh_->ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16(script_)); |
+ script_was_executed_ = true; |
+ } |
+ |
+ virtual void DidCommitProvisionalLoadForFrame( |
+ int64 frame_id, |
+ bool is_main_frame, |
+ const GURL& url, |
+ content::PageTransition transition_type, |
+ content::RenderViewHost* render_view_host) OVERRIDE { |
+ if (script_was_executed_ && url == until_url_) { |
+ content::WebContentsObserver::Observe(NULL); |
+ test_navigation_listener_->ResumeAll(); |
+ } |
+ rvh_ = render_view_host; |
+ } |
+ |
+ private: |
+ content::NotificationRegistrar registrar_; |
+ |
+ scoped_refptr<TestNavigationListener> test_navigation_listener_; |
+ |
+ GURL delay_url_; |
+ GURL until_url_; |
+ std::string script_; |
+ bool script_was_executed_; |
+ content::RenderViewHost* rvh_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DelayLoadStartAndExecuteJavascript); |
+}; |
+ |
} // namespace |
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WebNavigation) { |
@@ -320,4 +405,79 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WebNavigationHistory) { |
<< message_; |
} |
+IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WebNavigationCrossProcess) { |
+ FrameNavigationState::set_allow_extension_scheme(true); |
+ host_resolver()->AddRule("*", "127.0.0.1"); |
+ ASSERT_TRUE(StartTestServer()); |
+ |
+ CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kAllowLegacyExtensionManifests); |
+ |
+ LoadExtension(test_data_dir_.AppendASCII("webnavigation").AppendASCII("app")); |
+ LoadExtension(test_data_dir_.AppendASCII("webnavigation")); |
+ |
+ ExtensionService* service = browser()->profile()->GetExtensionService(); |
+ const extensions::Extension* extension = |
+ service->GetExtensionById(last_loaded_extension_id_, false); |
+ |
+ scoped_refptr<TestNavigationListener> test_navigation_listener = |
+ new TestNavigationListener; |
+ |
+ g_browser_process->resource_dispatcher_host_delegate()-> |
+ SetTestNavigationListener(test_navigation_listener.get()); |
+ |
+ // See crossProcess/d.html. |
+ DelayLoadStartAndExecuteJavascript call_script( |
+ test_navigation_listener.get(), |
+ test_server()->GetURL("test1"), |
+ extension->GetResourceURL("crossProcess/empty.html"), |
+ "navigate2()"); |
+ |
+ // See crossProcess/e.html. |
+ DelayLoadStartAndExecuteJavascript call_script2( |
+ test_navigation_listener.get(), |
+ test_server()->GetURL("test2"), |
+ extension->GetResourceURL("crossProcess/empty.html"), |
+ "updateHistory()"); |
+ |
+ // See crossProcess/f.html. |
+ DelayLoadStartAndExecuteJavascript call_script3( |
+ test_navigation_listener.get(), |
+ test_server()->GetURL("test3"), |
+ extension->GetResourceURL(base::StringPrintf( |
+ "crossProcess/f.html?%d#foo", |
+ test_server()->host_port_pair().port())), |
+ "updateFragment()"); |
+ |
+ // See crossProcess/g.html. |
+ DelayLoadStartAndExecuteJavascript call_script4( |
+ test_navigation_listener.get(), |
+ test_server()->GetURL("test4"), |
+ extension->GetResourceURL(base::StringPrintf( |
+ "crossProcess/g.html?%d#foo", |
+ test_server()->host_port_pair().port())), |
+ "updateFragment()"); |
+ |
+ // See crossProcess/h.html. |
+ DelayLoadStartAndExecuteJavascript call_script5( |
+ test_navigation_listener.get(), |
+ test_server()->GetURL("test5"), |
+ extension->GetResourceURL("crossProcess/empty.html"), |
+ "updateHistory()"); |
+ |
+ // See crossProcess/i.html. |
+ DelayLoadStartAndExecuteJavascript call_script6( |
+ test_navigation_listener.get(), |
+ test_server()->GetURL("test6"), |
+ extension->GetResourceURL("crossProcess/empty.html"), |
+ "updateHistory()"); |
+ |
+ ASSERT_TRUE(RunPageTest( |
+ extension->GetResourceURL("test_crossProcess.html").spec())) |
+ << message_; |
+ |
+ g_browser_process->resource_dispatcher_host_delegate()-> |
+ SetTestNavigationListener(NULL); |
+} |
+ |
} // namespace extensions |