Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / web_navigation / web_navigation_apitest.cc
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
3 // found in the LICENSE file.
4
5 #include <list>
6 #include <set>
7
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chrome_browser_main.h"
16 #include "chrome/browser/chrome_browser_main_extra_parts.h"
17 #include "chrome/browser/chrome_content_browser_client.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/download/download_browsertest.h"
20 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
21 #include "chrome/browser/extensions/extension_apitest.h"
22 #include "chrome/browser/extensions/extension_service.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
25 #include "chrome/browser/tab_contents/render_view_context_menu.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model.h"
28 #include "chrome/test/base/ui_test_utils.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/render_process_host.h"
31 #include "content/public/browser/render_view_host.h"
32 #include "content/public/browser/resource_controller.h"
33 #include "content/public/browser/resource_dispatcher_host.h"
34 #include "content/public/browser/resource_throttle.h"
35 #include "content/public/browser/web_contents.h"
36 #include "content/public/common/context_menu_params.h"
37 #include "content/public/common/url_constants.h"
38 #include "content/public/test/browser_test_utils.h"
39 #include "extensions/browser/extension_system.h"
40 #include "extensions/common/switches.h"
41 #include "net/dns/mock_host_resolver.h"
42 #include "net/test/embedded_test_server/embedded_test_server.h"
43 #include "third_party/WebKit/public/web/WebContextMenuData.h"
44 #include "third_party/WebKit/public/web/WebInputEvent.h"
45 #include "webkit/common/resource_type.h"
46
47 using content::WebContents;
48
49 namespace extensions {
50
51 namespace {
52
53 // An UI-less RenderViewContextMenu.
54 class TestRenderViewContextMenu : public RenderViewContextMenu {
55  public:
56   TestRenderViewContextMenu(content::RenderFrameHost* render_frame_host,
57                             const content::ContextMenuParams& params)
58       : RenderViewContextMenu(render_frame_host, params) {
59   }
60   virtual ~TestRenderViewContextMenu() {}
61
62  private:
63   virtual void PlatformInit() OVERRIDE {}
64   virtual void PlatformCancel() OVERRIDE {}
65   virtual bool GetAcceleratorForCommandId(int, ui::Accelerator*) OVERRIDE {
66     return false;
67   }
68
69   DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu);
70 };
71
72
73 // This class can defer requests for arbitrary URLs.
74 class TestNavigationListener
75     : public base::RefCountedThreadSafe<TestNavigationListener> {
76  public:
77   TestNavigationListener() {}
78
79   // Add |url| to the set of URLs we should delay.
80   void DelayRequestsForURL(const GURL& url) {
81     if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
82       content::BrowserThread::PostTask(
83           content::BrowserThread::IO,
84           FROM_HERE,
85           base::Bind(&TestNavigationListener::DelayRequestsForURL, this, url));
86       return;
87     }
88     urls_to_delay_.insert(url);
89   }
90
91   // Resume all deferred requests.
92   void ResumeAll() {
93     if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
94       content::BrowserThread::PostTask(
95           content::BrowserThread::IO,
96           FROM_HERE,
97           base::Bind(&TestNavigationListener::ResumeAll, this));
98       return;
99     }
100     WeakThrottleList::const_iterator it;
101     for (it = throttles_.begin(); it != throttles_.end(); ++it) {
102       if (it->get())
103         (*it)->Resume();
104     }
105     throttles_.clear();
106   }
107
108   // Constructs a ResourceThrottle if the request for |url| should be held.
109   //
110   // Needs to be invoked on the IO thread.
111   content::ResourceThrottle* CreateResourceThrottle(
112       const GURL& url,
113       ResourceType::Type resource_type) {
114     DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
115     if (urls_to_delay_.find(url) == urls_to_delay_.end())
116       return NULL;
117
118     Throttle* throttle = new Throttle();
119     throttles_.push_back(throttle->AsWeakPtr());
120     return throttle;
121   }
122
123  private:
124   friend class base::RefCountedThreadSafe<TestNavigationListener>;
125
126   virtual ~TestNavigationListener() {}
127
128   // Stores a throttle per URL request that we have delayed.
129   class Throttle : public content::ResourceThrottle,
130                    public base::SupportsWeakPtr<Throttle> {
131    public:
132     void Resume() {
133       controller()->Resume();
134     }
135
136     // content::ResourceThrottle implementation.
137     virtual void WillStartRequest(bool* defer) OVERRIDE {
138       *defer = true;
139     }
140
141     virtual const char* GetNameForLogging() const OVERRIDE {
142       return "TestNavigationListener::Throttle";
143     }
144   };
145   typedef base::WeakPtr<Throttle> WeakThrottle;
146   typedef std::list<WeakThrottle> WeakThrottleList;
147   WeakThrottleList throttles_;
148
149   // The set of URLs to be delayed.
150   std::set<GURL> urls_to_delay_;
151
152   DISALLOW_COPY_AND_ASSIGN(TestNavigationListener);
153 };
154
155 // Waits for a WC to be created. Once it starts loading |delay_url| (after at
156 // least the first navigation has committed), it delays the load, executes
157 // |script| in the last committed RVH and resumes the load when a URL ending in
158 // |until_url_suffix| commits. This class expects |script| to trigger the load
159 // of an URL ending in |until_url_suffix|.
160 class DelayLoadStartAndExecuteJavascript
161     : public content::NotificationObserver,
162       public content::WebContentsObserver {
163  public:
164   DelayLoadStartAndExecuteJavascript(
165       TestNavigationListener* test_navigation_listener,
166       const GURL& delay_url,
167       const std::string& script,
168       const std::string& until_url_suffix)
169       : content::WebContentsObserver(),
170         test_navigation_listener_(test_navigation_listener),
171         delay_url_(delay_url),
172         until_url_suffix_(until_url_suffix),
173         script_(script),
174         script_was_executed_(false),
175         rvh_(NULL) {
176     registrar_.Add(this,
177                    chrome::NOTIFICATION_TAB_ADDED,
178                    content::NotificationService::AllSources());
179     test_navigation_listener_->DelayRequestsForURL(delay_url_);
180   }
181   virtual ~DelayLoadStartAndExecuteJavascript() {}
182
183   virtual void Observe(int type,
184                        const content::NotificationSource& source,
185                        const content::NotificationDetails& details) OVERRIDE {
186     if (type != chrome::NOTIFICATION_TAB_ADDED) {
187       NOTREACHED();
188       return;
189     }
190     content::WebContentsObserver::Observe(
191         content::Details<content::WebContents>(details).ptr());
192     registrar_.RemoveAll();
193   }
194
195   virtual void DidStartProvisionalLoadForFrame(
196       int64 frame_id,
197       int64 parent_frame_id,
198       bool is_main_frame,
199       const GURL& validated_url,
200       bool is_error_page,
201       bool is_iframe_srcdoc,
202       content::RenderViewHost* render_view_host) OVERRIDE {
203     if (validated_url != delay_url_ || !rvh_)
204       return;
205
206     rvh_->ExecuteJavascriptInWebFrame(base::string16(),
207                                       base::UTF8ToUTF16(script_));
208     script_was_executed_ = true;
209   }
210
211   virtual void DidCommitProvisionalLoadForFrame(
212       int64 frame_id,
213       const base::string16& frame_unique_name,
214       bool is_main_frame,
215       const GURL& url,
216       content::PageTransition transition_type,
217       content::RenderViewHost* render_view_host) OVERRIDE {
218     if (script_was_executed_ && EndsWith(url.spec(), until_url_suffix_, true)) {
219       content::WebContentsObserver::Observe(NULL);
220       test_navigation_listener_->ResumeAll();
221     }
222     rvh_ = render_view_host;
223   }
224
225  private:
226   content::NotificationRegistrar registrar_;
227
228   scoped_refptr<TestNavigationListener> test_navigation_listener_;
229
230   GURL delay_url_;
231   std::string until_url_suffix_;
232   std::string script_;
233   bool script_was_executed_;
234   content::RenderViewHost* rvh_;
235
236   DISALLOW_COPY_AND_ASSIGN(DelayLoadStartAndExecuteJavascript);
237 };
238
239 // A ResourceDispatcherHostDelegate that adds a TestNavigationObserver.
240 class TestResourceDispatcherHostDelegate
241     : public ChromeResourceDispatcherHostDelegate {
242  public:
243   TestResourceDispatcherHostDelegate(
244       prerender::PrerenderTracker* prerender_tracker,
245       TestNavigationListener* test_navigation_listener)
246       : ChromeResourceDispatcherHostDelegate(prerender_tracker),
247         test_navigation_listener_(test_navigation_listener) {
248   }
249   virtual ~TestResourceDispatcherHostDelegate() {}
250
251   virtual void RequestBeginning(
252       net::URLRequest* request,
253       content::ResourceContext* resource_context,
254       appcache::AppCacheService* appcache_service,
255       ResourceType::Type resource_type,
256       int child_id,
257       int route_id,
258       ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE {
259     ChromeResourceDispatcherHostDelegate::RequestBeginning(
260         request,
261         resource_context,
262         appcache_service,
263         resource_type,
264         child_id,
265         route_id,
266         throttles);
267     content::ResourceThrottle* throttle =
268         test_navigation_listener_->CreateResourceThrottle(request->url(),
269                                                           resource_type);
270     if (throttle)
271       throttles->push_back(throttle);
272   }
273
274  private:
275   scoped_refptr<TestNavigationListener> test_navigation_listener_;
276
277   DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherHostDelegate);
278 };
279
280 // Used to manage the lifetime of the TestResourceDispatcherHostDelegate which
281 // needs to be deleted before the threads are stopped.
282 class TestBrowserMainExtraParts : public ChromeBrowserMainExtraParts {
283  public:
284   explicit TestBrowserMainExtraParts(
285       TestNavigationListener* test_navigation_listener)
286       : test_navigation_listener_(test_navigation_listener) {
287   }
288   virtual ~TestBrowserMainExtraParts() {}
289
290   TestResourceDispatcherHostDelegate* resource_dispatcher_host_delegate() {
291     if (!resource_dispatcher_host_delegate_.get()) {
292       resource_dispatcher_host_delegate_.reset(
293           new TestResourceDispatcherHostDelegate(
294               g_browser_process->prerender_tracker(),
295               test_navigation_listener_.get()));
296     }
297     return resource_dispatcher_host_delegate_.get();
298   }
299
300   // ChromeBrowserMainExtraParts implementation.
301   virtual void PostMainMessageLoopRun() OVERRIDE {
302     resource_dispatcher_host_delegate_.reset();
303   }
304
305  private:
306   scoped_refptr<TestNavigationListener> test_navigation_listener_;
307   scoped_ptr<TestResourceDispatcherHostDelegate>
308       resource_dispatcher_host_delegate_;
309
310   DISALLOW_COPY_AND_ASSIGN(TestBrowserMainExtraParts);
311 };
312
313 // A ContentBrowserClient that doesn't forward the RDH created signal.
314 class TestContentBrowserClient : public chrome::ChromeContentBrowserClient {
315  public:
316   explicit TestContentBrowserClient(
317       TestNavigationListener* test_navigation_listener)
318       : test_navigation_listener_(test_navigation_listener) {
319   }
320   virtual ~TestContentBrowserClient() {}
321
322   virtual void ResourceDispatcherHostCreated() OVERRIDE {
323     // Don't invoke ChromeContentBrowserClient::ResourceDispatcherHostCreated.
324     // It would notify BrowserProcessImpl which would create a
325     // ChromeResourceDispatcherHostDelegate and other objects. Not creating
326     // other objects might turn out to be a problem in the future.
327     content::ResourceDispatcherHost::Get()->SetDelegate(
328         browser_main_extra_parts_->resource_dispatcher_host_delegate());
329   }
330
331   virtual content::BrowserMainParts* CreateBrowserMainParts(
332       const content::MainFunctionParams& parameters) OVERRIDE {
333     ChromeBrowserMainParts* main_parts = static_cast<ChromeBrowserMainParts*>(
334         ChromeContentBrowserClient::CreateBrowserMainParts(parameters));
335
336     browser_main_extra_parts_ =
337         new TestBrowserMainExtraParts(test_navigation_listener_.get());
338     main_parts->AddParts(browser_main_extra_parts_);
339     return main_parts;
340   }
341
342  private:
343   scoped_refptr<TestNavigationListener> test_navigation_listener_;
344   TestBrowserMainExtraParts* browser_main_extra_parts_;
345
346   DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient);
347 };
348
349 }  // namespace
350
351 class WebNavigationApiTest : public ExtensionApiTest {
352  public:
353   WebNavigationApiTest() {}
354   virtual ~WebNavigationApiTest() {}
355
356   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
357     ExtensionApiTest::SetUpInProcessBrowserTestFixture();
358
359     test_navigation_listener_ = new TestNavigationListener();
360     content_browser_client_.reset(
361         new TestContentBrowserClient(test_navigation_listener_.get()));
362     original_content_browser_client_ = content::SetBrowserClientForTesting(
363         content_browser_client_.get());
364
365     FrameNavigationState::set_allow_extension_scheme(true);
366
367     CommandLine::ForCurrentProcess()->AppendSwitch(
368         switches::kAllowLegacyExtensionManifests);
369
370     host_resolver()->AddRule("*", "127.0.0.1");
371   }
372
373   virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
374     ExtensionApiTest::TearDownInProcessBrowserTestFixture();
375     content::SetBrowserClientForTesting(original_content_browser_client_);
376   }
377
378   TestNavigationListener* test_navigation_listener() {
379     return test_navigation_listener_.get();
380   }
381
382  private:
383   scoped_refptr<TestNavigationListener> test_navigation_listener_;
384   scoped_ptr<TestContentBrowserClient> content_browser_client_;
385   content::ContentBrowserClient* original_content_browser_client_;
386
387   DISALLOW_COPY_AND_ASSIGN(WebNavigationApiTest);
388 };
389
390 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, Api) {
391   ASSERT_TRUE(StartEmbeddedTestServer());
392   ASSERT_TRUE(RunExtensionTest("webnavigation/api")) << message_;
393 }
394
395 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, GetFrame) {
396   ASSERT_TRUE(StartEmbeddedTestServer());
397   ASSERT_TRUE(RunExtensionTest("webnavigation/getFrame")) << message_;
398 }
399
400 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, ClientRedirect) {
401   ASSERT_TRUE(StartEmbeddedTestServer());
402   ASSERT_TRUE(RunExtensionTest("webnavigation/clientRedirect"))
403       << message_;
404 }
405
406 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, ServerRedirect) {
407   ASSERT_TRUE(StartEmbeddedTestServer());
408   ASSERT_TRUE(RunExtensionTest("webnavigation/serverRedirect"))
409       << message_;
410 }
411
412 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, Download) {
413   DownloadTestObserverNotInProgress download_observer(
414       content::BrowserContext::GetDownloadManager(profile()), 1);
415   download_observer.StartObserving();
416   ASSERT_TRUE(StartEmbeddedTestServer());
417   ASSERT_TRUE(RunExtensionTest("webnavigation/download"))
418       << message_;
419   download_observer.WaitForFinished();
420 }
421
422 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, ServerRedirectSingleProcess) {
423   ASSERT_TRUE(StartEmbeddedTestServer());
424
425   // Set max renderers to 1 to force running out of processes.
426   content::RenderProcessHost::SetMaxRendererProcessCount(1);
427
428   // Wait for the extension to set itself up and return control to us.
429   ASSERT_TRUE(
430       RunExtensionTest("webnavigation/serverRedirectSingleProcess"))
431       << message_;
432
433   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
434   content::WaitForLoadStop(tab);
435
436   ResultCatcher catcher;
437   GURL url(base::StringPrintf(
438       "http://www.a.com:%d/"
439       "extensions/api_test/webnavigation/serverRedirectSingleProcess/a.html",
440       embedded_test_server()->port()));
441
442   ui_test_utils::NavigateToURL(browser(), url);
443
444   url = GURL(base::StringPrintf(
445       "http://www.b.com:%d/server-redirect?http://www.b.com:%d/",
446       embedded_test_server()->port(),
447       embedded_test_server()->port()));
448
449   ui_test_utils::NavigateToURL(browser(), url);
450
451   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
452 }
453
454 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, ForwardBack) {
455   ASSERT_TRUE(StartEmbeddedTestServer());
456   ASSERT_TRUE(RunExtensionTest("webnavigation/forwardBack")) << message_;
457 }
458
459 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, IFrame) {
460   ASSERT_TRUE(StartEmbeddedTestServer());
461   ASSERT_TRUE(RunExtensionTest("webnavigation/iframe")) << message_;
462 }
463
464 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, SrcDoc) {
465   ASSERT_TRUE(StartEmbeddedTestServer());
466   ASSERT_TRUE(RunExtensionTest("webnavigation/srcdoc")) << message_;
467 }
468
469 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, OpenTab) {
470   ASSERT_TRUE(StartEmbeddedTestServer());
471   ASSERT_TRUE(RunExtensionTest("webnavigation/openTab")) << message_;
472 }
473
474 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, ReferenceFragment) {
475   ASSERT_TRUE(StartEmbeddedTestServer());
476   ASSERT_TRUE(RunExtensionTest("webnavigation/referenceFragment"))
477       << message_;
478 }
479
480 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, SimpleLoad) {
481   ASSERT_TRUE(StartEmbeddedTestServer());
482   ASSERT_TRUE(RunExtensionTest("webnavigation/simpleLoad")) << message_;
483 }
484
485 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, Failures) {
486   ASSERT_TRUE(StartEmbeddedTestServer());
487   ASSERT_TRUE(RunExtensionTest("webnavigation/failures")) << message_;
488 }
489
490 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, FilteredTest) {
491   ASSERT_TRUE(StartEmbeddedTestServer());
492   ASSERT_TRUE(RunExtensionTest("webnavigation/filtered")) << message_;
493 }
494
495 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, UserAction) {
496   ASSERT_TRUE(StartEmbeddedTestServer());
497
498   // Wait for the extension to set itself up and return control to us.
499   ASSERT_TRUE(RunExtensionTest("webnavigation/userAction")) << message_;
500
501   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
502   content::WaitForLoadStop(tab);
503
504   ResultCatcher catcher;
505
506   ExtensionService* service = extensions::ExtensionSystem::Get(
507       browser()->profile())->extension_service();
508   const extensions::Extension* extension =
509       service->GetExtensionById(last_loaded_extension_id(), false);
510   GURL url = extension->GetResourceURL("a.html");
511
512   ui_test_utils::NavigateToURL(browser(), url);
513
514   // This corresponds to "Open link in new tab".
515   content::ContextMenuParams params;
516   params.is_editable = false;
517   params.media_type = blink::WebContextMenuData::MediaTypeNone;
518   params.page_url = url;
519   params.frame_id = WebNavigationTabObserver::Get(tab)->
520       frame_navigation_state().GetMainFrameID().frame_num;
521   params.link_url = extension->GetResourceURL("b.html");
522
523   TestRenderViewContextMenu menu(tab->GetMainFrame(), params);
524   menu.Init();
525   menu.ExecuteCommand(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB, 0);
526
527   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
528 }
529
530 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, RequestOpenTab) {
531   ASSERT_TRUE(StartEmbeddedTestServer());
532
533   // Wait for the extension to set itself up and return control to us.
534   ASSERT_TRUE(RunExtensionTest("webnavigation/requestOpenTab"))
535       << message_;
536
537   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
538   content::WaitForLoadStop(tab);
539
540   ResultCatcher catcher;
541
542   ExtensionService* service = extensions::ExtensionSystem::Get(
543       browser()->profile())->extension_service();
544   const extensions::Extension* extension =
545       service->GetExtensionById(last_loaded_extension_id(), false);
546   GURL url = extension->GetResourceURL("a.html");
547
548   ui_test_utils::NavigateToURL(browser(), url);
549
550   // There's a link on a.html. Middle-click on it to open it in a new tab.
551   blink::WebMouseEvent mouse_event;
552   mouse_event.type = blink::WebInputEvent::MouseDown;
553   mouse_event.button = blink::WebMouseEvent::ButtonMiddle;
554   mouse_event.x = 7;
555   mouse_event.y = 7;
556   mouse_event.clickCount = 1;
557   tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
558   mouse_event.type = blink::WebInputEvent::MouseUp;
559   tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
560
561   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
562 }
563
564 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, TargetBlank) {
565   ASSERT_TRUE(StartEmbeddedTestServer());
566
567   // Wait for the extension to set itself up and return control to us.
568   ASSERT_TRUE(RunExtensionTest("webnavigation/targetBlank")) << message_;
569
570   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
571   content::WaitForLoadStop(tab);
572
573   ResultCatcher catcher;
574
575   GURL url = embedded_test_server()->GetURL(
576       "/extensions/api_test/webnavigation/targetBlank/a.html");
577
578   chrome::NavigateParams params(browser(), url, content::PAGE_TRANSITION_LINK);
579   ui_test_utils::NavigateToURL(&params);
580
581   // There's a link with target=_blank on a.html. Click on it to open it in a
582   // new tab.
583   blink::WebMouseEvent mouse_event;
584   mouse_event.type = blink::WebInputEvent::MouseDown;
585   mouse_event.button = blink::WebMouseEvent::ButtonLeft;
586   mouse_event.x = 7;
587   mouse_event.y = 7;
588   mouse_event.clickCount = 1;
589   tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
590   mouse_event.type = blink::WebInputEvent::MouseUp;
591   tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
592
593   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
594 }
595
596 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, TargetBlankIncognito) {
597   ASSERT_TRUE(StartEmbeddedTestServer());
598
599   // Wait for the extension to set itself up and return control to us.
600   ASSERT_TRUE(RunExtensionTestIncognito("webnavigation/targetBlank"))
601       << message_;
602
603   ResultCatcher catcher;
604
605   GURL url = embedded_test_server()->GetURL(
606       "/extensions/api_test/webnavigation/targetBlank/a.html");
607
608   Browser* otr_browser = ui_test_utils::OpenURLOffTheRecord(
609       browser()->profile(), url);
610   WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents();
611
612   // There's a link with target=_blank on a.html. Click on it to open it in a
613   // new tab.
614   blink::WebMouseEvent mouse_event;
615   mouse_event.type = blink::WebInputEvent::MouseDown;
616   mouse_event.button = blink::WebMouseEvent::ButtonLeft;
617   mouse_event.x = 7;
618   mouse_event.y = 7;
619   mouse_event.clickCount = 1;
620   tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
621   mouse_event.type = blink::WebInputEvent::MouseUp;
622   tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
623
624   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
625 }
626
627 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, History) {
628   ASSERT_TRUE(StartEmbeddedTestServer());
629   ASSERT_TRUE(RunExtensionTest("webnavigation/history")) << message_;
630 }
631
632 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, CrossProcess) {
633   ASSERT_TRUE(StartEmbeddedTestServer());
634
635   LoadExtension(test_data_dir_.AppendASCII("webnavigation").AppendASCII("app"));
636
637   // See crossProcess/d.html.
638   DelayLoadStartAndExecuteJavascript call_script(
639       test_navigation_listener(),
640       embedded_test_server()->GetURL("/test1"),
641       "navigate2()",
642       "empty.html");
643
644   ASSERT_TRUE(RunExtensionTest("webnavigation/crossProcess")) << message_;
645 }
646
647 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, CrossProcessFragment) {
648   ASSERT_TRUE(StartEmbeddedTestServer());
649
650   // See crossProcessFragment/f.html.
651   DelayLoadStartAndExecuteJavascript call_script3(
652       test_navigation_listener(),
653       embedded_test_server()->GetURL("/test3"),
654       "updateFragment()",
655       base::StringPrintf("f.html?%d#foo", embedded_test_server()->port()));
656
657   // See crossProcessFragment/g.html.
658   DelayLoadStartAndExecuteJavascript call_script4(
659       test_navigation_listener(),
660       embedded_test_server()->GetURL("/test4"),
661       "updateFragment()",
662       base::StringPrintf("g.html?%d#foo", embedded_test_server()->port()));
663
664   ASSERT_TRUE(RunExtensionTest("webnavigation/crossProcessFragment"))
665       << message_;
666 }
667
668 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, CrossProcessHistory) {
669   ASSERT_TRUE(StartEmbeddedTestServer());
670
671   // See crossProcessHistory/e.html.
672   DelayLoadStartAndExecuteJavascript call_script2(
673       test_navigation_listener(),
674       embedded_test_server()->GetURL("/test2"),
675       "updateHistory()",
676       "empty.html");
677
678   // See crossProcessHistory/h.html.
679   DelayLoadStartAndExecuteJavascript call_script5(
680       test_navigation_listener(),
681       embedded_test_server()->GetURL("/test5"),
682       "updateHistory()",
683       "empty.html");
684
685   // See crossProcessHistory/i.html.
686   DelayLoadStartAndExecuteJavascript call_script6(
687       test_navigation_listener(),
688       embedded_test_server()->GetURL("/test6"),
689       "updateHistory()",
690       "empty.html");
691
692   ASSERT_TRUE(RunExtensionTest("webnavigation/crossProcessHistory"))
693       << message_;
694 }
695
696 IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, Crash) {
697   ASSERT_TRUE(StartEmbeddedTestServer());
698
699   // Wait for the extension to set itself up and return control to us.
700   ASSERT_TRUE(RunExtensionTest("webnavigation/crash")) << message_;
701
702   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
703   content::WaitForLoadStop(tab);
704
705   ResultCatcher catcher;
706
707   GURL url(base::StringPrintf(
708       "http://www.a.com:%d/"
709           "extensions/api_test/webnavigation/crash/a.html",
710       embedded_test_server()->port()));
711   ui_test_utils::NavigateToURL(browser(), url);
712
713   ui_test_utils::NavigateToURL(browser(), GURL(content::kChromeUICrashURL));
714
715   url = GURL(base::StringPrintf(
716       "http://www.a.com:%d/"
717           "extensions/api_test/webnavigation/crash/b.html",
718       embedded_test_server()->port()));
719   ui_test_utils::NavigateToURL(browser(), url);
720
721   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
722 }
723
724 }  // namespace extensions