OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/utf_string_conversions.h" | |
6 #include "base/stringprintf.h" | |
5 #include "chrome/app/chrome_command_ids.h" | 7 #include "chrome/app/chrome_command_ids.h" |
8 #include "chrome/browser/browser_process.h" | |
6 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" | 9 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" |
7 #include "chrome/browser/extensions/extension_apitest.h" | 10 #include "chrome/browser/extensions/extension_apitest.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate. h" | |
14 #include "chrome/browser/renderer_host/test_navigation_listener.h" | |
10 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 15 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
11 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_tabstrip.h" | 17 #include "chrome/browser/ui/browser_tabstrip.h" |
18 #include "chrome/common/chrome_notification_types.h" | |
13 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
15 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
17 #include "content/public/common/context_menu_params.h" | 23 #include "content/public/common/context_menu_params.h" |
18 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
19 #include "net/base/mock_host_resolver.h" | 25 #include "net/base/mock_host_resolver.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
22 | 28 |
(...skipping 14 matching lines...) Expand all Loading... | |
37 private: | 43 private: |
38 virtual void PlatformInit() {} | 44 virtual void PlatformInit() {} |
39 virtual void PlatformCancel() {} | 45 virtual void PlatformCancel() {} |
40 virtual bool GetAcceleratorForCommandId(int, ui::Accelerator*) { | 46 virtual bool GetAcceleratorForCommandId(int, ui::Accelerator*) { |
41 return false; | 47 return false; |
42 } | 48 } |
43 | 49 |
44 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); | 50 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); |
45 }; | 51 }; |
46 | 52 |
53 // Waits for a WC to be created. Once it starts loading |delay_url| (after at | |
54 // least the first navigation has committed), it delays the load, executes | |
55 // |script| in the last committed RVH and resumes the load when |until_url| | |
56 // commits. | |
battre
2012/08/07 13:34:40
Add comment, that |script| is supposed to trigger
| |
57 class DelayLoadStartAndExecuteJavascript | |
58 : public content::NotificationObserver, | |
59 public content::WebContentsObserver { | |
60 public: | |
61 DelayLoadStartAndExecuteJavascript( | |
62 TestNavigationListener* test_navigation_listener, | |
63 const GURL& delay_url, | |
64 const GURL& until_url, | |
65 const std::string& script) | |
battre
2012/08/07 13:34:40
opt: change the order to delay_url -> script -> un
| |
66 : content::WebContentsObserver(), | |
67 test_navigation_listener_(test_navigation_listener), | |
68 delay_url_(delay_url), | |
69 until_url_(until_url), | |
70 script_(script), | |
71 script_was_executed_(false), | |
72 rvh_(NULL) { | |
73 registrar_.Add(this, | |
74 chrome::NOTIFICATION_TAB_ADDED, | |
75 content::NotificationService::AllSources()); | |
76 test_navigation_listener_->DelayRequestsForURL(delay_url_); | |
77 } | |
78 virtual ~DelayLoadStartAndExecuteJavascript() {} | |
79 | |
80 virtual void Observe(int type, | |
81 const content::NotificationSource& source, | |
82 const content::NotificationDetails& details) OVERRIDE { | |
83 if (type != chrome::NOTIFICATION_TAB_ADDED) { | |
84 NOTREACHED(); | |
85 return; | |
86 } | |
87 content::WebContentsObserver::Observe( | |
88 content::Details<content::WebContents>(details).ptr()); | |
89 registrar_.RemoveAll(); | |
90 } | |
91 | |
92 virtual void DidStartProvisionalLoadForFrame( | |
93 int64 frame_id, | |
94 bool is_main_frame, | |
95 const GURL& validated_url, | |
96 bool is_error_page, | |
97 content::RenderViewHost* render_view_host) OVERRIDE { | |
98 if (validated_url != delay_url_ || !rvh_) | |
99 return; | |
100 | |
101 rvh_->ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16(script_)); | |
102 script_was_executed_ = true; | |
103 } | |
104 | |
105 virtual void DidCommitProvisionalLoadForFrame( | |
106 int64 frame_id, | |
107 bool is_main_frame, | |
108 const GURL& url, | |
109 content::PageTransition transition_type, | |
110 content::RenderViewHost* render_view_host) OVERRIDE { | |
111 if (script_was_executed_ && url == until_url_) { | |
112 content::WebContentsObserver::Observe(NULL); | |
113 test_navigation_listener_->ResumeAll(); | |
114 } | |
115 rvh_ = render_view_host; | |
116 } | |
117 | |
118 private: | |
119 content::NotificationRegistrar registrar_; | |
120 | |
121 scoped_refptr<TestNavigationListener> test_navigation_listener_; | |
122 | |
123 GURL delay_url_; | |
124 GURL until_url_; | |
125 std::string script_; | |
126 bool script_was_executed_; | |
127 content::RenderViewHost* rvh_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(DelayLoadStartAndExecuteJavascript); | |
130 }; | |
131 | |
47 } // namespace | 132 } // namespace |
48 | 133 |
49 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WebNavigation) { | 134 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WebNavigation) { |
50 FrameNavigationState::set_allow_extension_scheme(true); | 135 FrameNavigationState::set_allow_extension_scheme(true); |
51 | 136 |
52 CommandLine::ForCurrentProcess()->AppendSwitch( | 137 CommandLine::ForCurrentProcess()->AppendSwitch( |
53 switches::kAllowLegacyExtensionManifests); | 138 switches::kAllowLegacyExtensionManifests); |
54 | 139 |
55 ASSERT_TRUE( | 140 ASSERT_TRUE( |
56 RunExtensionSubtest("webnavigation", "test_api.html")) << message_; | 141 RunExtensionSubtest("webnavigation", "test_api.html")) << message_; |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 FrameNavigationState::set_allow_extension_scheme(true); | 398 FrameNavigationState::set_allow_extension_scheme(true); |
314 | 399 |
315 CommandLine::ForCurrentProcess()->AppendSwitch( | 400 CommandLine::ForCurrentProcess()->AppendSwitch( |
316 switches::kAllowLegacyExtensionManifests); | 401 switches::kAllowLegacyExtensionManifests); |
317 | 402 |
318 ASSERT_TRUE( | 403 ASSERT_TRUE( |
319 RunExtensionSubtest("webnavigation", "test_history.html")) | 404 RunExtensionSubtest("webnavigation", "test_history.html")) |
320 << message_; | 405 << message_; |
321 } | 406 } |
322 | 407 |
408 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WebNavigationCrossProcess) { | |
409 FrameNavigationState::set_allow_extension_scheme(true); | |
410 host_resolver()->AddRule("*", "127.0.0.1"); | |
411 ASSERT_TRUE(StartTestServer()); | |
412 | |
413 CommandLine::ForCurrentProcess()->AppendSwitch( | |
414 switches::kAllowLegacyExtensionManifests); | |
415 | |
416 LoadExtension(test_data_dir_.AppendASCII("webnavigation").AppendASCII("app")); | |
417 LoadExtension(test_data_dir_.AppendASCII("webnavigation")); | |
418 | |
419 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
420 const extensions::Extension* extension = | |
421 service->GetExtensionById(last_loaded_extension_id_, false); | |
422 | |
423 scoped_refptr<TestNavigationListener> test_navigation_listener = | |
424 new TestNavigationListener; | |
425 | |
426 g_browser_process->resource_dispatcher_host_delegate()-> | |
427 SetTestNavigationListener(test_navigation_listener.get()); | |
428 | |
429 // See crossProcess/d.html. | |
430 DelayLoadStartAndExecuteJavascript call_script( | |
431 test_navigation_listener.get(), | |
432 test_server()->GetURL("test1"), | |
433 extension->GetResourceURL("crossProcess/empty.html"), | |
434 "navigate2()"); | |
435 | |
436 // See crossProcess/e.html. | |
437 DelayLoadStartAndExecuteJavascript call_script2( | |
438 test_navigation_listener.get(), | |
439 test_server()->GetURL("test2"), | |
440 extension->GetResourceURL("crossProcess/empty.html"), | |
441 "updateHistory()"); | |
442 | |
443 // See crossProcess/f.html. | |
444 DelayLoadStartAndExecuteJavascript call_script3( | |
445 test_navigation_listener.get(), | |
446 test_server()->GetURL("test3"), | |
447 extension->GetResourceURL(base::StringPrintf( | |
448 "crossProcess/f.html?%d#foo", | |
449 test_server()->host_port_pair().port())), | |
450 "updateFragment()"); | |
451 | |
452 // See crossProcess/g.html. | |
453 DelayLoadStartAndExecuteJavascript call_script4( | |
454 test_navigation_listener.get(), | |
455 test_server()->GetURL("test4"), | |
456 extension->GetResourceURL(base::StringPrintf( | |
457 "crossProcess/g.html?%d#foo", | |
458 test_server()->host_port_pair().port())), | |
459 "updateFragment()"); | |
460 | |
461 // See crossProcess/h.html. | |
462 DelayLoadStartAndExecuteJavascript call_script5( | |
463 test_navigation_listener.get(), | |
464 test_server()->GetURL("test5"), | |
465 extension->GetResourceURL("crossProcess/empty.html"), | |
466 "updateHistory()"); | |
467 | |
468 // See crossProcess/i.html. | |
469 DelayLoadStartAndExecuteJavascript call_script6( | |
470 test_navigation_listener.get(), | |
471 test_server()->GetURL("test6"), | |
472 extension->GetResourceURL("crossProcess/empty.html"), | |
473 "updateHistory()"); | |
474 | |
475 ASSERT_TRUE(RunPageTest( | |
476 extension->GetResourceURL("test_crossProcess.html").spec())) | |
477 << message_; | |
478 | |
479 g_browser_process->resource_dispatcher_host_delegate()-> | |
480 SetTestNavigationListener(NULL); | |
481 } | |
482 | |
323 } // namespace extensions | 483 } // namespace extensions |
OLD | NEW |