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.
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/files/file_path.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/sys_info.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/chrome_content_browser_client.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/command_updater.h"
18 #include "chrome/browser/content_settings/host_content_settings_map.h"
19 #include "chrome/browser/defaults.h"
20 #include "chrome/browser/devtools/devtools_window.h"
21 #include "chrome/browser/extensions/extension_browsertest.h"
22 #include "chrome/browser/extensions/extension_service.h"
23 #include "chrome/browser/extensions/tab_helper.h"
24 #include "chrome/browser/first_run/first_run.h"
25 #include "chrome/browser/lifetime/application_lifetime.h"
26 #include "chrome/browser/prefs/incognito_mode_prefs.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/profiles/profile_manager.h"
29 #include "chrome/browser/search/search.h"
30 #include "chrome/browser/sessions/session_backend.h"
31 #include "chrome/browser/sessions/session_service_factory.h"
32 #include "chrome/browser/translate/translate_tab_helper.h"
33 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h"
34 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h"
35 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h"
36 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h"
37 #include "chrome/browser/ui/browser.h"
38 #include "chrome/browser/ui/browser_command_controller.h"
39 #include "chrome/browser/ui/browser_commands.h"
40 #include "chrome/browser/ui/browser_finder.h"
41 #include "chrome/browser/ui/browser_iterator.h"
42 #include "chrome/browser/ui/browser_navigator.h"
43 #include "chrome/browser/ui/browser_tabstrip.h"
44 #include "chrome/browser/ui/browser_ui_prefs.h"
45 #include "chrome/browser/ui/browser_window.h"
46 #include "chrome/browser/ui/extensions/application_launch.h"
47 #include "chrome/browser/ui/host_desktop.h"
48 #include "chrome/browser/ui/startup/startup_browser_creator.h"
49 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
50 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
51 #include "chrome/browser/ui/tabs/tab_strip_model.h"
52 #include "chrome/common/chrome_switches.h"
53 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
54 #include "chrome/common/pref_names.h"
55 #include "chrome/common/url_constants.h"
56 #include "chrome/test/base/in_process_browser_test.h"
57 #include "chrome/test/base/test_switches.h"
58 #include "chrome/test/base/ui_test_utils.h"
59 #include "components/translate/core/common/language_detection_details.h"
60 #include "content/public/browser/favicon_status.h"
61 #include "content/public/browser/host_zoom_map.h"
62 #include "content/public/browser/interstitial_page.h"
63 #include "content/public/browser/interstitial_page_delegate.h"
64 #include "content/public/browser/navigation_entry.h"
65 #include "content/public/browser/notification_service.h"
66 #include "content/public/browser/render_frame_host.h"
67 #include "content/public/browser/render_process_host.h"
68 #include "content/public/browser/render_view_host.h"
69 #include "content/public/browser/render_widget_host_view.h"
70 #include "content/public/browser/resource_context.h"
71 #include "content/public/browser/web_contents.h"
72 #include "content/public/browser/web_contents_observer.h"
73 #include "content/public/browser/web_contents_view.h"
74 #include "content/public/common/frame_navigate_params.h"
75 #include "content/public/common/page_transition_types.h"
76 #include "content/public/common/renderer_preferences.h"
77 #include "content/public/common/url_constants.h"
78 #include "content/public/test/browser_test_utils.h"
79 #include "content/public/test/test_navigation_observer.h"
80 #include "extensions/browser/extension_system.h"
81 #include "extensions/common/extension.h"
82 #include "extensions/common/extension_set.h"
83 #include "grit/chromium_strings.h"
84 #include "grit/generated_resources.h"
85 #include "net/dns/mock_host_resolver.h"
86 #include "net/test/spawned_test_server/spawned_test_server.h"
87 #include "ui/base/l10n/l10n_util.h"
89 #if defined(OS_MACOSX)
90 #include "base/mac/mac_util.h"
91 #include "base/mac/scoped_nsautorelease_pool.h"
92 #include "chrome/browser/ui/cocoa/run_loop_testing.h"
96 #include "base/i18n/rtl.h"
97 #include "chrome/browser/browser_process.h"
100 using base::ASCIIToUTF16
;
101 using content::InterstitialPage
;
102 using content::HostZoomMap
;
103 using content::NavigationController
;
104 using content::NavigationEntry
;
105 using content::OpenURLParams
;
106 using content::Referrer
;
107 using content::WebContents
;
108 using content::WebContentsObserver
;
109 using extensions::Extension
;
113 const char* kBeforeUnloadHTML
=
114 "<html><head><title>beforeunload</title></head><body>"
115 "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
118 const char* kOpenNewBeforeUnloadPage
=
119 "w=window.open(); w.onbeforeunload=function(e){return 'foo'};";
121 const base::FilePath::CharType
* kBeforeUnloadFile
=
122 FILE_PATH_LITERAL("beforeunload.html");
124 const base::FilePath::CharType
* kTitle1File
= FILE_PATH_LITERAL("title1.html");
125 const base::FilePath::CharType
* kTitle2File
= FILE_PATH_LITERAL("title2.html");
127 const base::FilePath::CharType kDocRoot
[] =
128 FILE_PATH_LITERAL("chrome/test/data");
130 // Given a page title, returns the expected window caption string.
131 base::string16
WindowCaptionFromPageTitle(const base::string16
& page_title
) {
132 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
133 // On Mac or ChromeOS, we don't want to suffix the page title with
134 // the application name.
135 if (page_title
.empty())
136 return l10n_util::GetStringUTF16(IDS_BROWSER_WINDOW_MAC_TAB_UNTITLED
);
139 if (page_title
.empty())
140 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
);
142 return l10n_util::GetStringFUTF16(IDS_BROWSER_WINDOW_TITLE_FORMAT
,
147 // Returns the number of active RenderProcessHosts.
148 int CountRenderProcessHosts() {
150 for (content::RenderProcessHost::iterator
i(
151 content::RenderProcessHost::AllHostsIterator());
152 !i
.IsAtEnd(); i
.Advance())
157 class MockTabStripModelObserver
: public TabStripModelObserver
{
159 MockTabStripModelObserver() : closing_count_(0) {}
161 virtual void TabClosingAt(TabStripModel
* tab_strip_model
,
162 WebContents
* contents
,
163 int index
) OVERRIDE
{
167 int closing_count() const { return closing_count_
; }
172 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver
);
175 class InterstitialObserver
: public content::WebContentsObserver
{
177 InterstitialObserver(content::WebContents
* web_contents
,
178 const base::Closure
& attach_callback
,
179 const base::Closure
& detach_callback
)
180 : WebContentsObserver(web_contents
),
181 attach_callback_(attach_callback
),
182 detach_callback_(detach_callback
) {
185 virtual void DidAttachInterstitialPage() OVERRIDE
{
186 attach_callback_
.Run();
189 virtual void DidDetachInterstitialPage() OVERRIDE
{
190 detach_callback_
.Run();
194 base::Closure attach_callback_
;
195 base::Closure detach_callback_
;
197 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver
);
200 // Causes the browser to swap processes on a redirect to an HTTPS URL.
201 class TransferHttpsRedirectsContentBrowserClient
202 : public chrome::ChromeContentBrowserClient
{
204 virtual bool ShouldSwapProcessesForRedirect(
205 content::ResourceContext
* resource_context
,
206 const GURL
& current_url
,
207 const GURL
& new_url
) OVERRIDE
{
208 return new_url
.SchemeIs(content::kHttpsScheme
);
212 // Used by CloseWithAppMenuOpen. Invokes CloseWindow on the supplied browser.
213 void CloseWindowCallback(Browser
* browser
) {
214 chrome::CloseWindow(browser
);
217 // Used by CloseWithAppMenuOpen. Posts a CloseWindowCallback and shows the app
219 void RunCloseWithAppMenuCallback(Browser
* browser
) {
220 // ShowAppMenu is modal under views. Schedule a task that closes the window.
221 base::MessageLoop::current()->PostTask(
222 FROM_HERE
, base::Bind(&CloseWindowCallback
, browser
));
223 chrome::ShowAppMenu(browser
);
226 // Displays "INTERSTITIAL" while the interstitial is attached.
227 // (InterstitialPage can be used in a test directly, but there would be no way
228 // to visually tell if it is showing or not.)
229 class TestInterstitialPage
: public content::InterstitialPageDelegate
{
231 TestInterstitialPage(WebContents
* tab
, bool new_navigation
, const GURL
& url
) {
232 interstitial_page_
= InterstitialPage::Create(
233 tab
, new_navigation
, url
, this);
234 interstitial_page_
->Show();
236 virtual ~TestInterstitialPage() { }
238 interstitial_page_
->Proceed();
241 interstitial_page_
->DontProceed();
244 virtual std::string
GetHTMLContents() OVERRIDE
{
245 return "<h1>INTERSTITIAL</h1>";
249 InterstitialPage
* interstitial_page_
; // Owns us.
252 class RenderViewSizeObserver
: public content::WebContentsObserver
{
254 RenderViewSizeObserver(content::WebContents
* web_contents
,
255 BrowserWindow
* browser_window
)
256 : WebContentsObserver(web_contents
),
257 browser_window_(browser_window
) {
260 void GetSizeForRenderViewHost(
261 content::RenderViewHost
* render_view_host
,
262 gfx::Size
* rwhv_create_size
,
263 gfx::Size
* rwhv_commit_size
,
264 gfx::Size
* wcv_commit_size
) {
265 RenderViewSizes::const_iterator result
= render_view_sizes_
.end();
266 result
= render_view_sizes_
.find(render_view_host
);
267 if (result
!= render_view_sizes_
.end()) {
268 *rwhv_create_size
= result
->second
.rwhv_create_size
;
269 *rwhv_commit_size
= result
->second
.rwhv_commit_size
;
270 *wcv_commit_size
= result
->second
.wcv_commit_size
;
274 void set_wcv_resize_insets(const gfx::Size
& wcv_resize_insets
) {
275 wcv_resize_insets_
= wcv_resize_insets
;
278 // Cache the size when RenderViewHost is first created.
279 virtual void RenderViewCreated(
280 content::RenderViewHost
* render_view_host
) OVERRIDE
{
281 render_view_sizes_
[render_view_host
].rwhv_create_size
=
282 render_view_host
->GetView()->GetViewBounds().size();
285 // Enlarge WebContentsView by |wcv_resize_insets_| while the navigation entry
287 virtual void DidStartNavigationToPendingEntry(
289 NavigationController::ReloadType reload_type
) OVERRIDE
{
290 if (wcv_resize_insets_
.IsEmpty())
292 // Resizing the main browser window by |wcv_resize_insets_| will
293 // automatically resize the WebContentsView by the same amount.
294 // Just resizing WebContentsView directly doesn't work on Linux, because the
295 // next automatic layout of the browser window will resize WebContentsView
296 // back to the previous size. To make it consistent, resize main browser
297 // window on all platforms.
298 gfx::Rect
bounds(browser_window_
->GetBounds());
299 gfx::Size
size(bounds
.size());
300 size
.Enlarge(wcv_resize_insets_
.width(), wcv_resize_insets_
.height());
301 bounds
.set_size(size
);
302 browser_window_
->SetBounds(bounds
);
303 // Let the message loop run so that resize actually takes effect.
304 content::RunAllPendingInMessageLoop();
307 // Cache the sizes of RenderWidgetHostView and WebContentsView when the
308 // navigation entry is committed, which is before
309 // WebContentsDelegate::DidNavigateMainFramePostCommit is called.
310 virtual void NavigationEntryCommitted(
311 const content::LoadCommittedDetails
& details
) OVERRIDE
{
312 content::RenderViewHost
* rvh
= web_contents()->GetRenderViewHost();
313 render_view_sizes_
[rvh
].rwhv_commit_size
=
314 web_contents()->GetRenderWidgetHostView()->GetViewBounds().size();
315 render_view_sizes_
[rvh
].wcv_commit_size
=
316 web_contents()->GetView()->GetContainerSize();
321 gfx::Size rwhv_create_size
; // Size of RenderWidgetHostView when created.
322 gfx::Size rwhv_commit_size
; // Size of RenderWidgetHostView when committed.
323 gfx::Size wcv_commit_size
; // Size of WebContentsView when committed.
326 typedef std::map
<content::RenderViewHost
*, Sizes
> RenderViewSizes
;
327 RenderViewSizes render_view_sizes_
;
328 // Enlarge WebContentsView by this size insets in
329 // DidStartNavigationToPendingEntry.
330 gfx::Size wcv_resize_insets_
;
331 BrowserWindow
* browser_window_
; // Weak ptr.
333 DISALLOW_COPY_AND_ASSIGN(RenderViewSizeObserver
);
338 class BrowserTest
: public ExtensionBrowserTest
{
340 // In RTL locales wrap the page title with RTL embedding characters so that it
341 // matches the value returned by GetWindowTitle().
342 base::string16
LocaleWindowCaptionFromPageTitle(
343 const base::string16
& expected_title
) {
344 base::string16 page_title
= WindowCaptionFromPageTitle(expected_title
);
346 std::string locale
= g_browser_process
->GetApplicationLocale();
347 if (base::i18n::GetTextDirectionForLocale(locale
.c_str()) ==
348 base::i18n::RIGHT_TO_LEFT
) {
349 base::i18n::WrapStringWithLTRFormatting(&page_title
);
354 // Do we need to use the above code on POSIX as well?
359 // Returns the app extension aptly named "App Test".
360 const Extension
* GetExtension() {
361 const extensions::ExtensionSet
* extensions
=
362 extensions::ExtensionSystem::Get(
363 browser()->profile())->extension_service()->extensions();
364 for (extensions::ExtensionSet::const_iterator it
= extensions
->begin();
365 it
!= extensions
->end(); ++it
) {
366 if ((*it
)->name() == "App Test")
374 // Launch the app on a page with no title, check that the app title was set
376 IN_PROC_BROWSER_TEST_F(BrowserTest
, NoTitle
) {
377 #if defined(OS_WIN) && defined(USE_ASH)
378 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
379 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
383 ui_test_utils::NavigateToURL(
384 browser(), ui_test_utils::GetTestUrl(
385 base::FilePath(base::FilePath::kCurrentDirectory
),
386 base::FilePath(kTitle1File
)));
387 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(ASCIIToUTF16("title1.html")),
388 browser()->GetWindowTitleForCurrentTab());
389 base::string16 tab_title
;
390 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title
));
391 EXPECT_EQ(ASCIIToUTF16("title1.html"), tab_title
);
394 // Launch the app, navigate to a page with a title, check that the app title
395 // was set correctly.
396 IN_PROC_BROWSER_TEST_F(BrowserTest
, Title
) {
397 #if defined(OS_WIN) && defined(USE_ASH)
398 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
399 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
403 ui_test_utils::NavigateToURL(
404 browser(), ui_test_utils::GetTestUrl(
405 base::FilePath(base::FilePath::kCurrentDirectory
),
406 base::FilePath(kTitle2File
)));
407 const base::string16
test_title(ASCIIToUTF16("Title Of Awesomeness"));
408 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(test_title
),
409 browser()->GetWindowTitleForCurrentTab());
410 base::string16 tab_title
;
411 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title
));
412 EXPECT_EQ(test_title
, tab_title
);
415 IN_PROC_BROWSER_TEST_F(BrowserTest
, JavascriptAlertActivatesTab
) {
416 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
417 base::FilePath::kCurrentDirectory
), base::FilePath(kTitle1File
)));
418 ui_test_utils::NavigateToURL(browser(), url
);
419 AddTabAtIndex(0, url
, content::PAGE_TRANSITION_TYPED
);
420 EXPECT_EQ(2, browser()->tab_strip_model()->count());
421 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
422 WebContents
* second_tab
= browser()->tab_strip_model()->GetWebContentsAt(1);
423 ASSERT_TRUE(second_tab
);
424 second_tab
->GetMainFrame()->ExecuteJavaScript(
425 ASCIIToUTF16("alert('Activate!');"));
426 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
427 alert
->CloseModalDialog();
428 EXPECT_EQ(2, browser()->tab_strip_model()->count());
429 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
433 #if defined(OS_WIN) && !defined(NDEBUG)
434 // http://crbug.com/114859. Times out frequently on Windows.
435 #define MAYBE_ThirtyFourTabs DISABLED_ThirtyFourTabs
437 #define MAYBE_ThirtyFourTabs ThirtyFourTabs
440 // Create 34 tabs and verify that a lot of processes have been created. The
441 // exact number of processes depends on the amount of memory. Previously we
442 // had a hard limit of 31 processes and this test is mainly directed at
443 // verifying that we don't crash when we pass this limit.
444 // Warning: this test can take >30 seconds when running on a slow (low
445 // memory?) Mac builder.
446 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_ThirtyFourTabs
) {
447 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
448 base::FilePath::kCurrentDirectory
), base::FilePath(kTitle2File
)));
450 // There is one initial tab.
451 const int kTabCount
= 34;
452 for (int ix
= 0; ix
!= (kTabCount
- 1); ++ix
) {
453 chrome::AddSelectedTabWithURL(browser(), url
,
454 content::PAGE_TRANSITION_TYPED
);
456 EXPECT_EQ(kTabCount
, browser()->tab_strip_model()->count());
458 // See GetMaxRendererProcessCount() in
459 // content/browser/renderer_host/render_process_host_impl.cc
460 // for the algorithm to decide how many processes to create.
461 const int kExpectedProcessCount
=
462 #if defined(ARCH_CPU_64_BITS)
467 if (base::SysInfo::AmountOfPhysicalMemoryMB() >= 2048) {
468 EXPECT_GE(CountRenderProcessHosts(), kExpectedProcessCount
);
470 EXPECT_LT(CountRenderProcessHosts(), kExpectedProcessCount
);
474 // Test that a browser-initiated navigation to an aborted URL load leaves around
475 // a pending entry if we start from the NTP but not from a normal page.
476 // See http://crbug.com/355537.
477 IN_PROC_BROWSER_TEST_F(BrowserTest
, ClearPendingOnFailUnlessNTP
) {
478 ASSERT_TRUE(test_server()->Start());
479 WebContents
* web_contents
=
480 browser()->tab_strip_model()->GetActiveWebContents();
481 GURL
ntp_url(chrome::GetNewTabPageURL(browser()->profile()));
482 ui_test_utils::NavigateToURL(browser(), ntp_url
);
484 // Navigate to a 204 URL (aborts with no content) on the NTP and make sure it
485 // sticks around so that the user can edit it.
486 GURL
abort_url(test_server()->GetURL("nocontent"));
488 content::WindowedNotificationObserver
stop_observer(
489 content::NOTIFICATION_LOAD_STOP
,
490 content::Source
<NavigationController
>(
491 &web_contents
->GetController()));
492 browser()->OpenURL(OpenURLParams(abort_url
, Referrer(), CURRENT_TAB
,
493 content::PAGE_TRANSITION_TYPED
, false));
494 stop_observer
.Wait();
495 EXPECT_TRUE(web_contents
->GetController().GetPendingEntry());
496 EXPECT_EQ(abort_url
, web_contents
->GetVisibleURL());
499 // Navigate to a real URL.
500 GURL
real_url(test_server()->GetURL("title1.html"));
501 ui_test_utils::NavigateToURL(browser(), real_url
);
502 EXPECT_EQ(real_url
, web_contents
->GetVisibleURL());
504 // Now navigating to a 204 URL should clear the pending entry.
506 content::WindowedNotificationObserver
stop_observer(
507 content::NOTIFICATION_LOAD_STOP
,
508 content::Source
<NavigationController
>(
509 &web_contents
->GetController()));
510 browser()->OpenURL(OpenURLParams(abort_url
, Referrer(), CURRENT_TAB
,
511 content::PAGE_TRANSITION_TYPED
, false));
512 stop_observer
.Wait();
513 EXPECT_FALSE(web_contents
->GetController().GetPendingEntry());
514 EXPECT_EQ(real_url
, web_contents
->GetVisibleURL());
518 // Test for crbug.com/297289. Ensure that modal dialogs are closed when a
519 // cross-process navigation is ready to commit.
520 IN_PROC_BROWSER_TEST_F(BrowserTest
, CrossProcessNavCancelsDialogs
) {
521 ASSERT_TRUE(test_server()->Start());
522 host_resolver()->AddRule("www.example.com", "127.0.0.1");
523 GURL
url(test_server()->GetURL("empty.html"));
524 ui_test_utils::NavigateToURL(browser(), url
);
526 // Test this with multiple alert dialogs to ensure that we can navigate away
527 // even if the renderer tries to synchronously create more.
528 // See http://crbug.com/312490.
529 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
530 contents
->GetMainFrame()->ExecuteJavaScript(
531 ASCIIToUTF16("alert('one'); alert('two');"));
532 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
533 EXPECT_TRUE(alert
->IsValid());
534 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
535 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
537 // A cross-site navigation should force the dialog to close.
538 GURL
url2("http://www.example.com/empty.html");
539 ui_test_utils::NavigateToURL(browser(), url2
);
540 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
542 // Make sure input events still work in the renderer process.
543 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
546 // Make sure that dialogs are closed after a renderer process dies, and that
547 // subsequent navigations work. See http://crbug/com/343265.
548 IN_PROC_BROWSER_TEST_F(BrowserTest
, SadTabCancelsDialogs
) {
549 ASSERT_TRUE(test_server()->Start());
550 host_resolver()->AddRule("www.example.com", "127.0.0.1");
551 GURL
beforeunload_url(test_server()->GetURL("files/beforeunload.html"));
552 ui_test_utils::NavigateToURL(browser(), beforeunload_url
);
554 // Start a navigation to trigger the beforeunload dialog.
555 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
556 contents
->GetMainFrame()->ExecuteJavaScript(
557 ASCIIToUTF16("window.location.href = 'data:text/html,foo'"));
558 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
559 EXPECT_TRUE(alert
->IsValid());
560 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
561 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
563 // Crash the renderer process and ensure the dialog is gone.
564 content::RenderProcessHost
* child_process
= contents
->GetRenderProcessHost();
565 content::RenderProcessHostWatcher
crash_observer(
567 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
568 base::KillProcess(child_process
->GetHandle(), 0, false);
569 crash_observer
.Wait();
570 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
572 // Make sure subsequent navigations work.
573 GURL
url2("http://www.example.com/files/empty.html");
574 ui_test_utils::NavigateToURL(browser(), url2
);
577 // Make sure that dialogs opened by subframes are closed when the process dies.
578 // See http://crbug.com/366510.
579 IN_PROC_BROWSER_TEST_F(BrowserTest
, SadTabCancelsSubframeDialogs
) {
580 // Navigate to an iframe that opens an alert dialog.
581 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
582 contents
->GetMainFrame()->ExecuteJavaScript(
583 ASCIIToUTF16("window.location.href = 'data:text/html,"
584 "<iframe srcdoc=\"<script>alert(1)</script>\">'"));
585 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
586 EXPECT_TRUE(alert
->IsValid());
587 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
588 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
590 // Crash the renderer process and ensure the dialog is gone.
591 content::RenderProcessHost
* child_process
= contents
->GetRenderProcessHost();
592 content::RenderProcessHostWatcher
crash_observer(
594 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
595 base::KillProcess(child_process
->GetHandle(), 0, false);
596 crash_observer
.Wait();
597 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
599 // Make sure subsequent navigations work.
600 GURL
url2("data:text/html,foo");
601 ui_test_utils::NavigateToURL(browser(), url2
);
604 // Test for crbug.com/22004. Reloading a page with a before unload handler and
605 // then canceling the dialog should not leave the throbber spinning.
606 IN_PROC_BROWSER_TEST_F(BrowserTest
, ReloadThenCancelBeforeUnload
) {
607 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
608 ui_test_utils::NavigateToURL(browser(), url
);
610 // Navigate to another page, but click cancel in the dialog. Make sure that
611 // the throbber stops spinning.
612 chrome::Reload(browser(), CURRENT_TAB
);
613 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
614 alert
->CloseModalDialog();
616 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
618 // Clear the beforeunload handler so the test can easily exit.
619 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
620 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
623 class RedirectObserver
: public content::WebContentsObserver
{
625 explicit RedirectObserver(content::WebContents
* web_contents
)
626 : WebContentsObserver(web_contents
) {
629 virtual void DidNavigateAnyFrame(
630 const content::LoadCommittedDetails
& details
,
631 const content::FrameNavigateParams
& params
) OVERRIDE
{
635 virtual void WebContentsDestroyed(WebContents
* contents
) OVERRIDE
{
636 // Make sure we don't close the tab while the observer is in scope.
637 // See http://crbug.com/314036.
638 FAIL() << "WebContents closed during navigation (http://crbug.com/314036).";
641 const content::FrameNavigateParams
& params() const {
646 content::FrameNavigateParams params_
;
648 DISALLOW_COPY_AND_ASSIGN(RedirectObserver
);
651 // Ensure that a transferred cross-process navigation does not generate
652 // DidStopLoading events until the navigation commits. If it did, then
653 // ui_test_utils::NavigateToURL would proceed before the URL had committed.
654 // http://crbug.com/243957.
655 IN_PROC_BROWSER_TEST_F(BrowserTest
, NoStopDuringTransferUntilCommit
) {
656 // Create HTTP and HTTPS servers for a cross-site transition.
657 ASSERT_TRUE(test_server()->Start());
658 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
659 net::SpawnedTestServer::kLocalhost
,
660 base::FilePath(kDocRoot
));
661 ASSERT_TRUE(https_test_server
.Start());
663 // Temporarily replace ContentBrowserClient with one that will cause a
664 // process swap on all redirects to HTTPS URLs.
665 TransferHttpsRedirectsContentBrowserClient new_client
;
666 content::ContentBrowserClient
* old_client
=
667 SetBrowserClientForTesting(&new_client
);
669 GURL
init_url(test_server()->GetURL("files/title1.html"));
670 ui_test_utils::NavigateToURL(browser(), init_url
);
672 // Navigate to a same-site page that redirects, causing a transfer.
673 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
675 // Create a RedirectObserver that goes away before we close the tab.
677 RedirectObserver
redirect_observer(contents
);
678 GURL
dest_url(https_test_server
.GetURL("files/title2.html"));
679 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
681 ui_test_utils::NavigateToURL(browser(), redirect_url
);
683 // We should immediately see the new committed entry.
684 EXPECT_FALSE(contents
->GetController().GetPendingEntry());
686 contents
->GetController().GetLastCommittedEntry()->GetURL());
688 // We should keep track of the original request URL, redirect chain, and
689 // page transition type during a transfer, since these are necessary for
690 // history autocomplete to work.
691 EXPECT_EQ(redirect_url
, contents
->GetController().GetLastCommittedEntry()->
692 GetOriginalRequestURL());
693 EXPECT_EQ(2U, redirect_observer
.params().redirects
.size());
694 EXPECT_EQ(redirect_url
, redirect_observer
.params().redirects
.at(0));
695 EXPECT_EQ(dest_url
, redirect_observer
.params().redirects
.at(1));
696 EXPECT_TRUE(PageTransitionCoreTypeIs(redirect_observer
.params().transition
,
697 content::PAGE_TRANSITION_TYPED
));
700 // Restore previous browser client.
701 SetBrowserClientForTesting(old_client
);
704 // Tests that a cross-process redirect will only cause the beforeunload
705 // handler to run once.
706 IN_PROC_BROWSER_TEST_F(BrowserTest
, SingleBeforeUnloadAfterRedirect
) {
707 // Create HTTP and HTTPS servers for a cross-site transition.
708 ASSERT_TRUE(test_server()->Start());
709 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
710 net::SpawnedTestServer::kLocalhost
,
711 base::FilePath(kDocRoot
));
712 ASSERT_TRUE(https_test_server
.Start());
714 // Temporarily replace ContentBrowserClient with one that will cause a
715 // process swap on all redirects to HTTPS URLs.
716 TransferHttpsRedirectsContentBrowserClient new_client
;
717 content::ContentBrowserClient
* old_client
=
718 SetBrowserClientForTesting(&new_client
);
720 // Navigate to a page with a beforeunload handler.
721 GURL
url(test_server()->GetURL("files/beforeunload.html"));
722 ui_test_utils::NavigateToURL(browser(), url
);
724 // Navigate to a URL that redirects to another process and approve the
725 // beforeunload dialog that pops up.
726 content::WindowedNotificationObserver
nav_observer(
727 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
728 content::NotificationService::AllSources());
729 GURL
https_url(https_test_server
.GetURL("files/title1.html"));
730 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
732 browser()->OpenURL(OpenURLParams(redirect_url
, Referrer(), CURRENT_TAB
,
733 content::PAGE_TRANSITION_TYPED
, false));
734 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
736 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
737 alert
->native_dialog()->AcceptAppModalDialog();
740 // Restore previous browser client.
741 SetBrowserClientForTesting(old_client
);
744 // Test for crbug.com/80401. Canceling a before unload dialog should reset
745 // the URL to the previous page's URL.
746 IN_PROC_BROWSER_TEST_F(BrowserTest
, CancelBeforeUnloadResetsURL
) {
747 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
748 base::FilePath::kCurrentDirectory
), base::FilePath(kBeforeUnloadFile
)));
749 ui_test_utils::NavigateToURL(browser(), url
);
751 // Navigate to a page that triggers a cross-site transition.
752 ASSERT_TRUE(test_server()->Start());
753 GURL
url2(test_server()->GetURL("files/title1.html"));
754 browser()->OpenURL(OpenURLParams(
755 url2
, Referrer(), CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false));
757 content::WindowedNotificationObserver
host_destroyed_observer(
758 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED
,
759 content::NotificationService::AllSources());
761 // Cancel the dialog.
762 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
763 alert
->CloseModalDialog();
765 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
767 // Verify there are no pending history items after the dialog is cancelled.
768 // (see crbug.com/93858)
769 NavigationEntry
* entry
= browser()->tab_strip_model()->
770 GetActiveWebContents()->GetController().GetPendingEntry();
771 EXPECT_EQ(NULL
, entry
);
773 // Wait for the ShouldClose_ACK to arrive. We can detect it by waiting for
774 // the pending RVH to be destroyed.
775 host_destroyed_observer
.Wait();
776 EXPECT_EQ(url
, browser()->toolbar_model()->GetURL());
778 // Clear the beforeunload handler so the test can easily exit.
779 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
780 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
783 // Crashy on mac. http://crbug.com/38522 Crashy on win too (after 3 years).
784 #if defined(OS_MACOSX) || defined(OS_WIN)
785 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
786 DISABLED_SingleBeforeUnloadAfterWindowClose
788 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
789 SingleBeforeUnloadAfterWindowClose
792 // Test for crbug.com/11647. A page closed with window.close() should not have
793 // two beforeunload dialogs shown.
794 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_SingleBeforeUnloadAfterWindowClose
) {
795 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
796 ExecuteJavaScript(ASCIIToUTF16(kOpenNewBeforeUnloadPage
));
798 // Close the new window with JavaScript, which should show a single
799 // beforeunload dialog. Then show another alert, to make it easy to verify
800 // that a second beforeunload dialog isn't shown.
801 browser()->tab_strip_model()->GetWebContentsAt(0)->GetMainFrame()->
802 ExecuteJavaScript(ASCIIToUTF16("w.close(); alert('bar');"));
803 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
804 alert
->native_dialog()->AcceptAppModalDialog();
806 alert
= ui_test_utils::WaitForAppModalDialog();
807 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->
808 is_before_unload_dialog());
809 alert
->native_dialog()->AcceptAppModalDialog();
812 // BrowserTest.BeforeUnloadVsBeforeReload times out on Windows.
813 // http://crbug.com/130411
815 #define MAYBE_BeforeUnloadVsBeforeReload DISABLED_BeforeUnloadVsBeforeReload
817 #define MAYBE_BeforeUnloadVsBeforeReload BeforeUnloadVsBeforeReload
820 // Test that when a page has an onunload handler, reloading a page shows a
821 // different dialog than navigating to a different page.
822 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_BeforeUnloadVsBeforeReload
) {
823 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
824 ui_test_utils::NavigateToURL(browser(), url
);
826 // Reload the page, and check that we get a "before reload" dialog.
827 chrome::Reload(browser(), CURRENT_TAB
);
828 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
829 EXPECT_TRUE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
831 // Cancel the reload.
832 alert
->native_dialog()->CancelAppModalDialog();
834 // Navigate to another url, and check that we get a "before unload" dialog.
835 GURL
url2(std::string("about:blank"));
836 browser()->OpenURL(OpenURLParams(
837 url2
, Referrer(), CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false));
839 alert
= ui_test_utils::WaitForAppModalDialog();
840 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
842 // Accept the navigation so we end up on a page without a beforeunload hook.
843 alert
->native_dialog()->AcceptAppModalDialog();
846 // BeforeUnloadAtQuitWithTwoWindows is a regression test for
847 // http://crbug.com/11842. It opens two windows, one of which has a
848 // beforeunload handler and attempts to exit cleanly.
849 class BeforeUnloadAtQuitWithTwoWindows
: public InProcessBrowserTest
{
851 // This test is for testing a specific shutdown behavior. This mimics what
852 // happens in InProcessBrowserTest::RunTestOnMainThread and QuitBrowsers, but
853 // ensures that it happens through the single IDC_EXIT of the test.
854 virtual void CleanUpOnMainThread() OVERRIDE
{
855 // Cycle both the MessageLoop and the Cocoa runloop twice to flush out any
856 // Chrome work that generates Cocoa work. Do this twice since there are two
857 // Browsers that must be closed.
861 // Run the application event loop to completion, which will cycle the
862 // native MessagePump on all platforms.
863 base::MessageLoop::current()->PostTask(FROM_HERE
,
864 base::MessageLoop::QuitClosure());
865 base::MessageLoop::current()->Run();
867 // Take care of any remaining Cocoa work.
870 // At this point, quit should be for real now.
871 ASSERT_EQ(0u, chrome::GetTotalBrowserCount());
874 // A helper function that cycles the MessageLoop, and on Mac, the Cocoa run
875 // loop. It also drains the NSAutoreleasePool.
876 void CycleRunLoops() {
877 content::RunAllPendingInMessageLoop();
878 #if defined(OS_MACOSX)
879 chrome::testing::NSRunLoopRunAllPending();
880 AutoreleasePool()->Recycle();
885 // Disabled, http://crbug.com/159214 .
886 IN_PROC_BROWSER_TEST_F(BeforeUnloadAtQuitWithTwoWindows
,
887 DISABLED_IfThisTestTimesOutItIndicatesFAILURE
) {
888 // In the first browser, set up a page that has a beforeunload handler.
889 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
890 ui_test_utils::NavigateToURL(browser(), url
);
892 // Open a second browser window at about:blank.
893 ui_test_utils::BrowserAddedObserver browser_added_observer
;
894 chrome::NewEmptyWindow(browser()->profile(), chrome::GetActiveDesktop());
895 Browser
* second_window
= browser_added_observer
.WaitForSingleNewBrowser();
896 ui_test_utils::NavigateToURL(second_window
, GURL("about:blank"));
898 // Tell the application to quit. IDC_EXIT calls AttemptUserExit, which on
899 // everything but ChromeOS allows unload handlers to block exit. On that
900 // platform, though, it exits unconditionally. See the comment and bug ID
901 // in AttemptUserExit() in application_lifetime.cc.
902 #if defined(OS_CHROMEOS)
903 chrome::AttemptExit();
905 chrome::ExecuteCommand(second_window
, IDC_EXIT
);
908 // The beforeunload handler will run at exit, ensure it does, and then accept
909 // it to allow shutdown to proceed.
910 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
913 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
914 alert
->native_dialog()->AcceptAppModalDialog();
916 // But wait there's more! If this test times out, it likely means that the
917 // browser has not been able to quit correctly, indicating there's a
918 // regression of the bug noted above.
921 // Test that scripts can fork a new renderer process for a cross-site popup,
922 // based on http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab.
923 // The script must open a new tab, set its window.opener to null, and navigate
924 // it to a cross-site URL. It should also work for meta-refreshes.
925 // See http://crbug.com/93517.
926 IN_PROC_BROWSER_TEST_F(BrowserTest
, NullOpenerRedirectForksProcess
) {
927 CommandLine::ForCurrentProcess()->AppendSwitch(
928 switches::kDisablePopupBlocking
);
930 // Create http and https servers for a cross-site transition.
931 ASSERT_TRUE(test_server()->Start());
932 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
933 net::SpawnedTestServer::kLocalhost
,
934 base::FilePath(kDocRoot
));
935 ASSERT_TRUE(https_test_server
.Start());
936 GURL
http_url(test_server()->GetURL("files/title1.html"));
937 GURL
https_url(https_test_server
.GetURL(std::string()));
939 // Start with an http URL.
940 ui_test_utils::NavigateToURL(browser(), http_url
);
941 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
942 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
944 // Now open a tab to a blank page, set its opener to null, and redirect it
946 std::string redirect_popup
= "w=window.open();";
947 redirect_popup
+= "w.opener=null;";
948 redirect_popup
+= "w.document.location=\"";
949 redirect_popup
+= https_url
.spec();
950 redirect_popup
+= "\";";
952 content::WindowedNotificationObserver
popup_observer(
953 chrome::NOTIFICATION_TAB_ADDED
,
954 content::NotificationService::AllSources());
955 content::WindowedNotificationObserver
nav_observer(
956 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
957 content::NotificationService::AllSources());
958 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(redirect_popup
));
960 // Wait for popup window to appear and finish navigating.
961 popup_observer
.Wait();
962 ASSERT_EQ(2, browser()->tab_strip_model()->count());
963 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
965 EXPECT_NE(oldtab
, newtab
);
967 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
968 EXPECT_EQ(https_url
.spec(),
969 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
971 // Popup window should not be in the opener's process.
972 content::RenderProcessHost
* popup_process
=
973 newtab
->GetRenderProcessHost();
974 EXPECT_NE(process
, popup_process
);
976 // Now open a tab to a blank page, set its opener to null, and use a
977 // meta-refresh to navigate it instead.
978 std::string refresh_popup
= "w=window.open();";
979 refresh_popup
+= "w.opener=null;";
980 refresh_popup
+= "w.document.write(";
981 refresh_popup
+= "'<META HTTP-EQUIV=\"refresh\" content=\"0; url=";
982 refresh_popup
+= https_url
.spec();
983 refresh_popup
+= "\">');w.document.close();";
985 content::WindowedNotificationObserver
popup_observer2(
986 chrome::NOTIFICATION_TAB_ADDED
,
987 content::NotificationService::AllSources());
988 content::WindowedNotificationObserver
nav_observer2(
989 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
990 content::NotificationService::AllSources());
991 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(refresh_popup
));
993 // Wait for popup window to appear and finish navigating.
994 popup_observer2
.Wait();
995 ASSERT_EQ(3, browser()->tab_strip_model()->count());
996 WebContents
* newtab2
= browser()->tab_strip_model()->GetActiveWebContents();
997 EXPECT_TRUE(newtab2
);
998 EXPECT_NE(oldtab
, newtab2
);
999 nav_observer2
.Wait();
1000 ASSERT_TRUE(newtab2
->GetController().GetLastCommittedEntry());
1001 EXPECT_EQ(https_url
.spec(),
1002 newtab2
->GetController().GetLastCommittedEntry()->GetURL().spec());
1004 // This popup window should also not be in the opener's process.
1005 content::RenderProcessHost
* popup_process2
=
1006 newtab2
->GetRenderProcessHost();
1007 EXPECT_NE(process
, popup_process2
);
1010 // Tests that other popup navigations that do not follow the steps at
1011 // http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab will not
1012 // fork a new renderer process.
1013 IN_PROC_BROWSER_TEST_F(BrowserTest
, OtherRedirectsDontForkProcess
) {
1014 CommandLine::ForCurrentProcess()->AppendSwitch(
1015 switches::kDisablePopupBlocking
);
1017 // Create http and https servers for a cross-site transition.
1018 ASSERT_TRUE(test_server()->Start());
1019 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1020 net::SpawnedTestServer::kLocalhost
,
1021 base::FilePath(kDocRoot
));
1022 ASSERT_TRUE(https_test_server
.Start());
1023 GURL
http_url(test_server()->GetURL("files/title1.html"));
1024 GURL
https_url(https_test_server
.GetURL(std::string()));
1026 // Start with an http URL.
1027 ui_test_utils::NavigateToURL(browser(), http_url
);
1028 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
1029 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
1031 // Now open a tab to a blank page, set its opener to null, and redirect it
1033 std::string dont_fork_popup
= "w=window.open();";
1034 dont_fork_popup
+= "w.document.location=\"";
1035 dont_fork_popup
+= https_url
.spec();
1036 dont_fork_popup
+= "\";";
1038 content::WindowedNotificationObserver
popup_observer(
1039 chrome::NOTIFICATION_TAB_ADDED
,
1040 content::NotificationService::AllSources());
1041 content::WindowedNotificationObserver
nav_observer(
1042 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1043 content::NotificationService::AllSources());
1044 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(dont_fork_popup
));
1046 // Wait for popup window to appear and finish navigating.
1047 popup_observer
.Wait();
1048 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1049 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
1050 EXPECT_TRUE(newtab
);
1051 EXPECT_NE(oldtab
, newtab
);
1052 nav_observer
.Wait();
1053 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
1054 EXPECT_EQ(https_url
.spec(),
1055 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1057 // Popup window should still be in the opener's process.
1058 content::RenderProcessHost
* popup_process
=
1059 newtab
->GetRenderProcessHost();
1060 EXPECT_EQ(process
, popup_process
);
1062 // Same thing if the current tab tries to navigate itself.
1063 std::string navigate_str
= "document.location=\"";
1064 navigate_str
+= https_url
.spec();
1065 navigate_str
+= "\";";
1067 content::WindowedNotificationObserver
nav_observer2(
1068 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1069 content::NotificationService::AllSources());
1070 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(navigate_str
));
1071 nav_observer2
.Wait();
1072 ASSERT_TRUE(oldtab
->GetController().GetLastCommittedEntry());
1073 EXPECT_EQ(https_url
.spec(),
1074 oldtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1076 // Original window should still be in the original process.
1077 content::RenderProcessHost
* new_process
= newtab
->GetRenderProcessHost();
1078 EXPECT_EQ(process
, new_process
);
1081 // Test that get_process_idle_time() returns reasonable values when compared
1082 // with time deltas measured locally.
1083 IN_PROC_BROWSER_TEST_F(BrowserTest
, RenderIdleTime
) {
1084 base::TimeTicks start
= base::TimeTicks::Now();
1085 ui_test_utils::NavigateToURL(
1086 browser(), ui_test_utils::GetTestUrl(
1087 base::FilePath(base::FilePath::kCurrentDirectory
),
1088 base::FilePath(kTitle1File
)));
1089 content::RenderProcessHost::iterator
it(
1090 content::RenderProcessHost::AllHostsIterator());
1091 for (; !it
.IsAtEnd(); it
.Advance()) {
1092 base::TimeDelta renderer_td
=
1093 it
.GetCurrentValue()->GetChildProcessIdleTime();
1094 base::TimeDelta browser_td
= base::TimeTicks::Now() - start
;
1095 EXPECT_TRUE(browser_td
>= renderer_td
);
1099 // Test IDC_CREATE_SHORTCUTS command is enabled for url scheme file, ftp, http
1100 // and https and disabled for chrome://, about:// etc.
1101 // TODO(pinkerton): Disable app-mode in the model until we implement it
1102 // on the Mac. http://crbug.com/13148
1103 #if !defined(OS_MACOSX)
1104 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFile
) {
1105 CommandUpdater
* command_updater
=
1106 browser()->command_controller()->command_updater();
1108 static const base::FilePath::CharType
* kEmptyFile
=
1109 FILE_PATH_LITERAL("empty.html");
1110 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1111 base::FilePath::kCurrentDirectory
), base::FilePath(kEmptyFile
)));
1112 ASSERT_TRUE(file_url
.SchemeIs(content::kFileScheme
));
1113 ui_test_utils::NavigateToURL(browser(), file_url
);
1114 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1117 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttp
) {
1118 CommandUpdater
* command_updater
=
1119 browser()->command_controller()->command_updater();
1121 ASSERT_TRUE(test_server()->Start());
1122 GURL
http_url(test_server()->GetURL(std::string()));
1123 ASSERT_TRUE(http_url
.SchemeIs(content::kHttpScheme
));
1124 ui_test_utils::NavigateToURL(browser(), http_url
);
1125 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1128 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttps
) {
1129 CommandUpdater
* command_updater
=
1130 browser()->command_controller()->command_updater();
1132 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1133 net::SpawnedTestServer::kLocalhost
,
1134 base::FilePath(kDocRoot
));
1135 ASSERT_TRUE(test_server
.Start());
1136 GURL
https_url(test_server
.GetURL("/"));
1137 ASSERT_TRUE(https_url
.SchemeIs(content::kHttpsScheme
));
1138 ui_test_utils::NavigateToURL(browser(), https_url
);
1139 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1142 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFtp
) {
1143 CommandUpdater
* command_updater
=
1144 browser()->command_controller()->command_updater();
1146 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_FTP
,
1147 net::SpawnedTestServer::kLocalhost
,
1148 base::FilePath(kDocRoot
));
1149 ASSERT_TRUE(test_server
.Start());
1150 GURL
ftp_url(test_server
.GetURL(std::string()));
1151 ASSERT_TRUE(ftp_url
.SchemeIs(content::kFtpScheme
));
1152 ui_test_utils::NavigateToURL(browser(), ftp_url
);
1153 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1156 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutInvalid
) {
1157 CommandUpdater
* command_updater
=
1158 browser()->command_controller()->command_updater();
1160 // Urls that should not have shortcuts.
1161 GURL
new_tab_url(chrome::kChromeUINewTabURL
);
1162 ui_test_utils::NavigateToURL(browser(), new_tab_url
);
1163 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1165 GURL
history_url(chrome::kChromeUIHistoryURL
);
1166 ui_test_utils::NavigateToURL(browser(), history_url
);
1167 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1169 GURL
downloads_url(chrome::kChromeUIDownloadsURL
);
1170 ui_test_utils::NavigateToURL(browser(), downloads_url
);
1171 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1173 GURL
blank_url(content::kAboutBlankURL
);
1174 ui_test_utils::NavigateToURL(browser(), blank_url
);
1175 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1178 // Change a tab into an application window.
1179 // DISABLED: http://crbug.com/72310
1180 IN_PROC_BROWSER_TEST_F(BrowserTest
, DISABLED_ConvertTabToAppShortcut
) {
1181 ASSERT_TRUE(test_server()->Start());
1182 GURL
http_url(test_server()->GetURL(std::string()));
1183 ASSERT_TRUE(http_url
.SchemeIs(content::kHttpScheme
));
1185 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1186 WebContents
* initial_tab
= browser()->tab_strip_model()->GetWebContentsAt(0);
1187 WebContents
* app_tab
= chrome::AddSelectedTabWithURL(
1188 browser(), http_url
, content::PAGE_TRANSITION_TYPED
);
1189 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1190 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
1191 browser()->host_desktop_type()));
1193 // Normal tabs should accept load drops.
1194 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1195 EXPECT_TRUE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1197 // Turn |app_tab| into a tab in an app panel.
1198 chrome::ConvertTabToAppWindow(browser(), app_tab
);
1200 // The launch should have created a new browser.
1201 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1202 browser()->host_desktop_type()));
1204 // Find the new browser.
1205 Browser
* app_browser
= NULL
;
1206 for (chrome::BrowserIterator it
; !it
.done() && !app_browser
; it
.Next()) {
1207 if (*it
!= browser())
1210 ASSERT_TRUE(app_browser
);
1212 // Check that the tab contents is in the new browser, and not in the old.
1213 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1214 ASSERT_EQ(initial_tab
, browser()->tab_strip_model()->GetWebContentsAt(0));
1216 // Check that the appliaction browser has a single tab, and that tab contains
1217 // the content that we app-ified.
1218 ASSERT_EQ(1, app_browser
->tab_strip_model()->count());
1219 ASSERT_EQ(app_tab
, app_browser
->tab_strip_model()->GetWebContentsAt(0));
1221 // Normal tabs should accept load drops.
1222 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1224 // The tab in an app window should not.
1225 EXPECT_FALSE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1228 #endif // !defined(OS_MACOSX)
1230 // Test RenderView correctly send back favicon url for web page that redirects
1231 // to an anchor in javascript body.onload handler.
1232 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1233 DISABLED_FaviconOfOnloadRedirectToAnchorPage
) {
1234 ASSERT_TRUE(test_server()->Start());
1235 GURL
url(test_server()->GetURL("files/onload_redirect_to_anchor.html"));
1236 GURL
expected_favicon_url(test_server()->GetURL("files/test.png"));
1238 ui_test_utils::NavigateToURL(browser(), url
);
1240 NavigationEntry
* entry
= browser()->tab_strip_model()->
1241 GetActiveWebContents()->GetController().GetActiveEntry();
1242 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1245 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined (OS_WIN)
1246 // http://crbug.com/83828. On Mac 10.6, the failure rate is 14%
1247 #define MAYBE_FaviconChange DISABLED_FaviconChange
1249 #define MAYBE_FaviconChange FaviconChange
1251 // Test that an icon can be changed from JS.
1252 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_FaviconChange
) {
1253 static const base::FilePath::CharType
* kFile
=
1254 FILE_PATH_LITERAL("onload_change_favicon.html");
1255 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1256 base::FilePath::kCurrentDirectory
), base::FilePath(kFile
)));
1257 ASSERT_TRUE(file_url
.SchemeIs(content::kFileScheme
));
1258 ui_test_utils::NavigateToURL(browser(), file_url
);
1260 NavigationEntry
* entry
= browser()->tab_strip_model()->
1261 GetActiveWebContents()->GetController().GetActiveEntry();
1262 static const base::FilePath::CharType
* kIcon
=
1263 FILE_PATH_LITERAL("test1.png");
1264 GURL
expected_favicon_url(ui_test_utils::GetTestUrl(base::FilePath(
1265 base::FilePath::kCurrentDirectory
), base::FilePath(kIcon
)));
1266 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1269 // http://crbug.com/172336
1271 #define MAYBE_TabClosingWhenRemovingExtension \
1272 DISABLED_TabClosingWhenRemovingExtension
1274 #define MAYBE_TabClosingWhenRemovingExtension TabClosingWhenRemovingExtension
1276 // Makes sure TabClosing is sent when uninstalling an extension that is an app
1278 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_TabClosingWhenRemovingExtension
) {
1279 ASSERT_TRUE(test_server()->Start());
1280 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1281 GURL
url(test_server()->GetURL("empty.html"));
1282 TabStripModel
* model
= browser()->tab_strip_model();
1284 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1286 const Extension
* extension_app
= GetExtension();
1288 ui_test_utils::NavigateToURL(browser(), url
);
1290 WebContents
* app_contents
= WebContents::Create(
1291 WebContents::CreateParams(browser()->profile()));
1292 extensions::TabHelper::CreateForWebContents(app_contents
);
1293 extensions::TabHelper
* extensions_tab_helper
=
1294 extensions::TabHelper::FromWebContents(app_contents
);
1295 extensions_tab_helper
->SetExtensionApp(extension_app
);
1297 model
->AddWebContents(app_contents
, 0, content::PageTransitionFromInt(0),
1298 TabStripModel::ADD_NONE
);
1299 model
->SetTabPinned(0, true);
1300 ui_test_utils::NavigateToURL(browser(), url
);
1302 MockTabStripModelObserver observer
;
1303 model
->AddObserver(&observer
);
1305 // Uninstall the extension and make sure TabClosing is sent.
1306 ExtensionService
* service
= extensions::ExtensionSystem::Get(
1307 browser()->profile())->extension_service();
1308 service
->UninstallExtension(GetExtension()->id(), false, NULL
);
1309 EXPECT_EQ(1, observer
.closing_count());
1311 model
->RemoveObserver(&observer
);
1313 // There should only be one tab now.
1314 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1317 #if !defined(OS_MACOSX)
1318 // Open with --app-id=<id>, and see that an app window opens.
1319 IN_PROC_BROWSER_TEST_F(BrowserTest
, AppIdSwitch
) {
1320 ASSERT_TRUE(test_server()->Start());
1323 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1324 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1325 const Extension
* extension_app
= GetExtension();
1327 CommandLine
command_line(CommandLine::NO_PROGRAM
);
1328 command_line
.AppendSwitchASCII(switches::kAppId
, extension_app
->id());
1330 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1331 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1332 StartupBrowserCreatorImpl
launch(base::FilePath(), command_line
, first_run
);
1333 ASSERT_TRUE(launch
.OpenApplicationWindow(browser()->profile(), NULL
));
1335 // Check that the new browser has an app name.
1336 // The launch should have created a new browser.
1337 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1338 browser()->host_desktop_type()));
1340 // Find the new browser.
1341 Browser
* new_browser
= NULL
;
1342 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1343 if (*it
!= browser())
1346 ASSERT_TRUE(new_browser
);
1347 ASSERT_TRUE(new_browser
!= browser());
1349 // The browser's app_name should include the app's ID.
1351 new_browser
->app_name_
.find(extension_app
->id()),
1352 std::string::npos
) << new_browser
->app_name_
;
1355 // Open an app window and the dev tools window and ensure that the location
1356 // bar settings are correct.
1357 IN_PROC_BROWSER_TEST_F(BrowserTest
, ShouldShowLocationBar
) {
1358 ASSERT_TRUE(test_server()->Start());
1361 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1362 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1363 const Extension
* extension_app
= GetExtension();
1365 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1366 WebContents
* app_window
=
1367 OpenApplication(AppLaunchParams(browser()->profile(),
1369 extensions::LAUNCH_CONTAINER_WINDOW
,
1371 ASSERT_TRUE(app_window
);
1373 DevToolsWindow::OpenDevToolsWindowForTest(
1374 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(),
1377 // The launch should have created a new app browser and a dev tools browser.
1379 chrome::GetBrowserCount(browser()->profile(),
1380 browser()->host_desktop_type()));
1382 // Find the new browsers.
1383 Browser
* app_browser
= NULL
;
1384 Browser
* dev_tools_browser
= NULL
;
1385 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
1386 if (*it
== browser()) {
1388 } else if ((*it
)->app_name() == DevToolsWindow::kDevToolsApp
) {
1389 dev_tools_browser
= *it
;
1394 ASSERT_TRUE(dev_tools_browser
);
1395 ASSERT_TRUE(app_browser
);
1396 ASSERT_TRUE(app_browser
!= browser());
1399 dev_tools_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1401 app_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1405 // Tests that the CLD (Compact Language Detection) works properly.
1406 IN_PROC_BROWSER_TEST_F(BrowserTest
, PageLanguageDetection
) {
1407 ASSERT_TRUE(test_server()->Start());
1410 LanguageDetectionDetails details
;
1412 // Open a new tab with a page in English.
1413 AddTabAtIndex(0, GURL(test_server()->GetURL("files/english_page.html")),
1414 content::PAGE_TRANSITION_TYPED
);
1416 WebContents
* current_web_contents
=
1417 browser()->tab_strip_model()->GetActiveWebContents();
1418 TranslateTabHelper
* translate_tab_helper
=
1419 TranslateTabHelper::FromWebContents(current_web_contents
);
1420 content::Source
<WebContents
> source(current_web_contents
);
1422 ui_test_utils::WindowedNotificationObserverWithDetails
<
1423 LanguageDetectionDetails
>
1424 en_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1426 EXPECT_EQ("", translate_tab_helper
->GetLanguageState().original_language());
1427 en_language_detected_signal
.Wait();
1428 EXPECT_TRUE(en_language_detected_signal
.GetDetailsFor(
1429 source
.map_key(), &details
));
1430 EXPECT_EQ("en", details
.adopted_language
);
1431 EXPECT_EQ("en", translate_tab_helper
->GetLanguageState().original_language());
1433 // Now navigate to a page in French.
1434 ui_test_utils::WindowedNotificationObserverWithDetails
<
1435 LanguageDetectionDetails
>
1436 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1438 ui_test_utils::NavigateToURL(
1439 browser(), GURL(test_server()->GetURL("files/french_page.html")));
1440 fr_language_detected_signal
.Wait();
1441 details
.adopted_language
.clear();
1442 EXPECT_TRUE(fr_language_detected_signal
.GetDetailsFor(
1443 source
.map_key(), &details
));
1444 EXPECT_EQ("fr", details
.adopted_language
);
1445 EXPECT_EQ("fr", translate_tab_helper
->GetLanguageState().original_language());
1448 // Chromeos defaults to restoring the last session, so this test isn't
1450 #if !defined(OS_CHROMEOS)
1451 #if defined(OS_MACOSX)
1452 // Crashy, http://crbug.com/38522
1453 #define RestorePinnedTabs DISABLED_RestorePinnedTabs
1455 // Makes sure pinned tabs are restored correctly on start.
1456 IN_PROC_BROWSER_TEST_F(BrowserTest
, RestorePinnedTabs
) {
1457 ASSERT_TRUE(test_server()->Start());
1459 // Add an pinned app tab.
1460 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1461 GURL
url(test_server()->GetURL("empty.html"));
1462 TabStripModel
* model
= browser()->tab_strip_model();
1463 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1464 const Extension
* extension_app
= GetExtension();
1465 ui_test_utils::NavigateToURL(browser(), url
);
1466 WebContents
* app_contents
= WebContents::Create(
1467 WebContents::CreateParams(browser()->profile()));
1468 extensions::TabHelper::CreateForWebContents(app_contents
);
1469 extensions::TabHelper
* extensions_tab_helper
=
1470 extensions::TabHelper::FromWebContents(app_contents
);
1471 extensions_tab_helper
->SetExtensionApp(extension_app
);
1472 model
->AddWebContents(app_contents
, 0, content::PageTransitionFromInt(0),
1473 TabStripModel::ADD_NONE
);
1474 model
->SetTabPinned(0, true);
1475 ui_test_utils::NavigateToURL(browser(), url
);
1477 // Add a non pinned tab.
1478 chrome::NewTab(browser());
1480 // Add a pinned non-app tab.
1481 chrome::NewTab(browser());
1482 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL
));
1483 model
->SetTabPinned(2, true);
1485 // Write out the pinned tabs.
1486 PinnedTabCodec::WritePinnedTabs(browser()->profile());
1488 // Simulate launching again.
1489 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1490 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1491 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1492 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, first_run
);
1493 launch
.profile_
= browser()->profile();
1494 launch
.ProcessStartupURLs(std::vector
<GURL
>(),
1495 browser()->host_desktop_type());
1497 // The launch should have created a new browser.
1498 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1499 browser()->host_desktop_type()));
1501 // Find the new browser.
1502 Browser
* new_browser
= NULL
;
1503 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1504 if (*it
!= browser())
1507 ASSERT_TRUE(new_browser
);
1508 ASSERT_TRUE(new_browser
!= browser());
1510 // We should get back an additional tab for the app, and another for the
1511 // default home page.
1512 ASSERT_EQ(3, new_browser
->tab_strip_model()->count());
1514 // Make sure the state matches.
1515 TabStripModel
* new_model
= new_browser
->tab_strip_model();
1516 EXPECT_TRUE(new_model
->IsAppTab(0));
1517 EXPECT_FALSE(new_model
->IsAppTab(1));
1518 EXPECT_FALSE(new_model
->IsAppTab(2));
1520 EXPECT_TRUE(new_model
->IsTabPinned(0));
1521 EXPECT_TRUE(new_model
->IsTabPinned(1));
1522 EXPECT_FALSE(new_model
->IsTabPinned(2));
1524 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
1525 new_model
->GetWebContentsAt(2)->GetURL());
1528 extensions::TabHelper::FromWebContents(
1529 new_model
->GetWebContentsAt(0))->extension_app() == extension_app
);
1531 #endif // !defined(OS_CHROMEOS)
1533 // This test verifies we don't crash when closing the last window and the app
1535 // TODO(linux_aura) http://crbug.com/163931
1536 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
1537 #define MAYBE_CloseWithAppMenuOpen DISABLED_CloseWithAppMenuOpen
1539 #define MAYBE_CloseWithAppMenuOpen CloseWithAppMenuOpen
1541 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_CloseWithAppMenuOpen
) {
1542 if (browser_defaults::kBrowserAliveWithNoWindows
)
1545 // We need a message loop running for menus on windows.
1546 base::MessageLoop::current()->PostTask(
1547 FROM_HERE
, base::Bind(&RunCloseWithAppMenuCallback
, browser()));
1550 #if !defined(OS_MACOSX)
1551 IN_PROC_BROWSER_TEST_F(BrowserTest
, OpenAppWindowLikeNtp
) {
1552 ASSERT_TRUE(test_server()->Start());
1555 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1556 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1557 const Extension
* extension_app
= GetExtension();
1559 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1560 WebContents
* app_window
= OpenApplication(
1561 AppLaunchParams(browser()->profile(), extension_app
,
1562 extensions::LAUNCH_CONTAINER_WINDOW
, NEW_WINDOW
));
1563 ASSERT_TRUE(app_window
);
1565 // Apps launched in a window from the NTP have an extensions tab helper but
1566 // do not have extension_app set in it.
1567 ASSERT_TRUE(extensions::TabHelper::FromWebContents(app_window
));
1569 extensions::TabHelper::FromWebContents(app_window
)->extension_app());
1570 EXPECT_EQ(extensions::AppLaunchInfo::GetFullLaunchURL(extension_app
),
1571 app_window
->GetURL());
1573 // The launch should have created a new browser.
1574 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1575 browser()->host_desktop_type()));
1577 // Find the new browser.
1578 Browser
* new_browser
= NULL
;
1579 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1580 if (*it
!= browser())
1583 ASSERT_TRUE(new_browser
);
1584 ASSERT_TRUE(new_browser
!= browser());
1586 EXPECT_TRUE(new_browser
->is_app());
1588 // The browser's app name should include the extension's id.
1589 std::string app_name
= new_browser
->app_name_
;
1590 EXPECT_NE(app_name
.find(extension_app
->id()), std::string::npos
)
1591 << "Name " << app_name
<< " should contain id "<< extension_app
->id();
1593 #endif // !defined(OS_MACOSX)
1595 // Makes sure the browser doesn't crash when
1596 // set_show_state(ui::SHOW_STATE_MAXIMIZED) has been invoked.
1597 IN_PROC_BROWSER_TEST_F(BrowserTest
, StartMaximized
) {
1598 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1599 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(types
); ++i
) {
1600 Browser::CreateParams
params(types
[i
], browser()->profile(),
1601 browser()->host_desktop_type());
1602 params
.initial_show_state
= ui::SHOW_STATE_MAXIMIZED
;
1603 AddBlankTabAndShow(new Browser(params
));
1607 // Aura doesn't support minimized window. crbug.com/104571.
1608 #if defined(USE_AURA)
1609 #define MAYBE_StartMinimized DISABLED_StartMinimized
1611 #define MAYBE_StartMinimized StartMinimized
1613 // Makes sure the browser doesn't crash when
1614 // set_show_state(ui::SHOW_STATE_MINIMIZED) has been invoked.
1615 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_StartMinimized
) {
1616 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1617 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(types
); ++i
) {
1618 Browser::CreateParams
params(types
[i
], browser()->profile(),
1619 browser()->host_desktop_type());
1620 params
.initial_show_state
= ui::SHOW_STATE_MINIMIZED
;
1621 AddBlankTabAndShow(new Browser(params
));
1625 // Makes sure the forward button is disabled immediately when navigating
1626 // forward to a slow-to-commit page.
1627 IN_PROC_BROWSER_TEST_F(BrowserTest
, ForwardDisabledOnForward
) {
1628 GURL
blank_url(content::kAboutBlankURL
);
1629 ui_test_utils::NavigateToURL(browser(), blank_url
);
1631 ui_test_utils::NavigateToURL(
1632 browser(), ui_test_utils::GetTestUrl(
1633 base::FilePath(base::FilePath::kCurrentDirectory
),
1634 base::FilePath(kTitle1File
)));
1636 content::WindowedNotificationObserver
back_nav_load_observer(
1637 content::NOTIFICATION_LOAD_STOP
,
1638 content::Source
<NavigationController
>(
1639 &browser()->tab_strip_model()->GetActiveWebContents()->
1641 chrome::GoBack(browser(), CURRENT_TAB
);
1642 back_nav_load_observer
.Wait();
1643 CommandUpdater
* command_updater
=
1644 browser()->command_controller()->command_updater();
1645 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1647 content::WindowedNotificationObserver
forward_nav_load_observer(
1648 content::NOTIFICATION_LOAD_STOP
,
1649 content::Source
<NavigationController
>(
1650 &browser()->tab_strip_model()->GetActiveWebContents()->
1652 chrome::GoForward(browser(), CURRENT_TAB
);
1653 // This check will happen before the navigation completes, since the browser
1654 // won't process the renderer's response until the Wait() call below.
1655 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1656 forward_nav_load_observer
.Wait();
1659 // Makes sure certain commands are disabled when Incognito mode is forced.
1660 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisableMenuItemsWhenIncognitoIsForced
) {
1661 CommandUpdater
* command_updater
=
1662 browser()->command_controller()->command_updater();
1663 // At the beginning, all commands are enabled.
1664 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1665 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1666 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1667 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1668 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1669 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1671 // Set Incognito to FORCED.
1672 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1673 IncognitoModePrefs::FORCED
);
1674 // Bookmarks & Settings commands should get disabled.
1675 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1676 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1677 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1678 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1679 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1680 // New Incognito Window command, however, should be enabled.
1681 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1683 // Create a new browser.
1684 Browser
* new_browser
=
1685 new Browser(Browser::CreateParams(
1686 browser()->profile()->GetOffTheRecordProfile(),
1687 browser()->host_desktop_type()));
1688 CommandUpdater
* new_command_updater
=
1689 new_browser
->command_controller()->command_updater();
1690 // It should have Bookmarks & Settings commands disabled by default.
1691 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1692 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(
1693 IDC_SHOW_BOOKMARK_MANAGER
));
1694 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1695 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1696 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1697 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1700 // Makes sure New Incognito Window command is disabled when Incognito mode is
1702 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1703 NoNewIncognitoWindowWhenIncognitoIsDisabled
) {
1704 CommandUpdater
* command_updater
=
1705 browser()->command_controller()->command_updater();
1706 // Set Incognito to DISABLED.
1707 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1708 IncognitoModePrefs::DISABLED
);
1709 // Make sure New Incognito Window command is disabled. All remaining commands
1710 // should be enabled.
1711 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1712 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1713 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1714 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1715 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1716 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1718 // Create a new browser.
1719 Browser
* new_browser
=
1720 new Browser(Browser::CreateParams(browser()->profile(),
1721 browser()->host_desktop_type()));
1722 CommandUpdater
* new_command_updater
=
1723 new_browser
->command_controller()->command_updater();
1724 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1725 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1726 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1727 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1728 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1729 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1732 // Makes sure Extensions and Settings commands are disabled in certain
1733 // circumstances even though normally they should stay enabled.
1734 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1735 DisableExtensionsAndSettingsWhenIncognitoIsDisabled
) {
1736 CommandUpdater
* command_updater
=
1737 browser()->command_controller()->command_updater();
1738 // Disable extensions. This should disable Extensions menu.
1739 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
1740 set_extensions_enabled(false);
1741 // Set Incognito to DISABLED.
1742 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1743 IncognitoModePrefs::DISABLED
);
1744 // Make sure Manage Extensions command is disabled.
1745 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1746 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1747 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1748 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1749 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1751 // Create a popup (non-main-UI-type) browser. Settings command as well
1752 // as Extensions should be disabled.
1753 Browser
* popup_browser
= new Browser(
1754 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1755 browser()->host_desktop_type()));
1756 CommandUpdater
* popup_command_updater
=
1757 popup_browser
->command_controller()->command_updater();
1758 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1759 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1760 EXPECT_TRUE(popup_command_updater
->IsCommandEnabled(
1761 IDC_SHOW_BOOKMARK_MANAGER
));
1762 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1765 // Makes sure Extensions and Settings commands are disabled in certain
1766 // circumstances even though normally they should stay enabled.
1767 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1768 DisableOptionsAndImportMenuItemsConsistently
) {
1769 // Create a popup browser.
1770 Browser
* popup_browser
= new Browser(
1771 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1772 browser()->host_desktop_type()));
1773 CommandUpdater
* command_updater
=
1774 popup_browser
->command_controller()->command_updater();
1775 // OPTIONS and IMPORT_SETTINGS are disabled for a non-normal UI.
1776 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1777 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1779 // Set Incognito to FORCED.
1780 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1781 IncognitoModePrefs::FORCED
);
1782 // OPTIONS and IMPORT_SETTINGS are disabled when Incognito is forced.
1783 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1784 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1785 // Set Incognito to AVAILABLE.
1786 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1787 IncognitoModePrefs::ENABLED
);
1788 // OPTIONS and IMPORT_SETTINGS are still disabled since it is a non-normal UI.
1789 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1790 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1795 void OnZoomLevelChanged(const base::Closure
& callback
,
1796 const HostZoomMap::ZoomLevelChange
& host
) {
1803 // Flakes regularly on Windows XP
1804 // http://crbug.com/146040
1805 #define MAYBE_PageZoom DISABLED_PageZoom
1807 #define MAYBE_PageZoom PageZoom
1809 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_PageZoom
) {
1810 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1811 bool enable_plus
, enable_minus
;
1814 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1815 new content::MessageLoopRunner
);
1816 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1817 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1818 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1819 content::HostZoomMap::GetForBrowserContext(
1820 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1821 chrome::Zoom(browser(), content::PAGE_ZOOM_IN
);
1824 EXPECT_EQ(contents
->GetZoomPercent(&enable_plus
, &enable_minus
), 110);
1825 EXPECT_TRUE(enable_plus
);
1826 EXPECT_TRUE(enable_minus
);
1830 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1831 new content::MessageLoopRunner
);
1832 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1833 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1834 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1835 content::HostZoomMap::GetForBrowserContext(
1836 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1837 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1840 EXPECT_EQ(contents
->GetZoomPercent(&enable_plus
, &enable_minus
), 100);
1841 EXPECT_TRUE(enable_plus
);
1842 EXPECT_TRUE(enable_minus
);
1846 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1847 new content::MessageLoopRunner
);
1848 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1849 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1850 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1851 content::HostZoomMap::GetForBrowserContext(
1852 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1853 chrome::Zoom(browser(), content::PAGE_ZOOM_OUT
);
1856 EXPECT_EQ(contents
->GetZoomPercent(&enable_plus
, &enable_minus
), 90);
1857 EXPECT_TRUE(enable_plus
);
1858 EXPECT_TRUE(enable_minus
);
1861 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1864 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCommandDisable
) {
1865 ASSERT_TRUE(test_server()->Start());
1866 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1867 GURL
url(test_server()->GetURL("empty.html"));
1868 ui_test_utils::NavigateToURL(browser(), url
);
1870 CommandUpdater
* command_updater
=
1871 browser()->command_controller()->command_updater();
1872 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1873 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1874 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1875 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1877 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1879 TestInterstitialPage
* interstitial
= NULL
;
1881 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1882 new content::MessageLoopRunner
);
1884 InterstitialObserver
observer(contents
,
1885 loop_runner
->QuitClosure(),
1887 interstitial
= new TestInterstitialPage(contents
, false, GURL());
1891 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1893 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1894 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1895 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1896 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1899 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1900 new content::MessageLoopRunner
);
1902 InterstitialObserver
observer(contents
,
1904 loop_runner
->QuitClosure());
1905 interstitial
->Proceed();
1907 // interstitial is deleted now.
1910 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1911 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1912 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1913 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1916 // Ensure that creating an interstitial page closes any JavaScript dialogs
1917 // that were present on the previous page. See http://crbug.com/295695.
1918 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialClosesDialogs
) {
1919 ASSERT_TRUE(test_server()->Start());
1920 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1921 GURL
url(test_server()->GetURL("empty.html"));
1922 ui_test_utils::NavigateToURL(browser(), url
);
1924 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1925 contents
->GetMainFrame()->ExecuteJavaScript(
1926 ASCIIToUTF16("alert('Dialog showing!');"));
1927 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
1928 EXPECT_TRUE(alert
->IsValid());
1929 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
1930 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
1932 TestInterstitialPage
* interstitial
= NULL
;
1934 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1935 new content::MessageLoopRunner
);
1937 InterstitialObserver
observer(contents
,
1938 loop_runner
->QuitClosure(),
1940 interstitial
= new TestInterstitialPage(contents
, false, GURL());
1944 // The interstitial should have closed the dialog.
1945 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1946 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
1949 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1950 new content::MessageLoopRunner
);
1952 InterstitialObserver
observer(contents
,
1954 loop_runner
->QuitClosure());
1955 interstitial
->DontProceed();
1957 // interstitial is deleted now.
1960 // Make sure input events still work in the renderer process.
1961 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
1965 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCloseTab
) {
1966 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1969 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1970 new content::MessageLoopRunner
);
1972 InterstitialObserver
observer(contents
,
1973 loop_runner
->QuitClosure(),
1975 // Interstitial will delete itself when we close the tab.
1976 new TestInterstitialPage(contents
, false, GURL());
1980 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1983 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1984 new content::MessageLoopRunner
);
1986 InterstitialObserver
observer(contents
,
1988 loop_runner
->QuitClosure());
1989 chrome::CloseTab(browser());
1991 // interstitial is deleted now.
1995 class MockWebContentsObserver
: public WebContentsObserver
{
1997 explicit MockWebContentsObserver(WebContents
* web_contents
)
1998 : WebContentsObserver(web_contents
),
1999 got_user_gesture_(false) {
2002 virtual void DidGetUserGesture() OVERRIDE
{
2003 got_user_gesture_
= true;
2006 bool got_user_gesture() const {
2007 return got_user_gesture_
;
2010 void set_got_user_gesture(bool got_it
) {
2011 got_user_gesture_
= got_it
;
2015 bool got_user_gesture_
;
2017 DISALLOW_COPY_AND_ASSIGN(MockWebContentsObserver
);
2020 IN_PROC_BROWSER_TEST_F(BrowserTest
, UserGesturesReported
) {
2021 // Regression test for http://crbug.com/110707. Also tests that a user
2022 // gesture is sent when a normal navigation (via e.g. the omnibox) is
2024 WebContents
* web_contents
=
2025 browser()->tab_strip_model()->GetActiveWebContents();
2026 MockWebContentsObserver
mock_observer(web_contents
);
2028 ASSERT_TRUE(test_server()->Start());
2029 GURL
url(test_server()->GetURL("empty.html"));
2031 ui_test_utils::NavigateToURL(browser(), url
);
2032 EXPECT_TRUE(mock_observer
.got_user_gesture());
2034 mock_observer
.set_got_user_gesture(false);
2035 chrome::Reload(browser(), CURRENT_TAB
);
2036 EXPECT_TRUE(mock_observer
.got_user_gesture());
2039 // TODO(ben): this test was never enabled. It has bit-rotted since being added.
2040 // It originally lived in browser_unittest.cc, but has been moved here to make
2041 // room for real browser unit tests.
2043 class BrowserTest2
: public InProcessBrowserTest
{
2046 host_resolver_proc_
= new net::RuleBasedHostResolverProc(NULL
);
2047 // Avoid making external DNS lookups. In this test we don't need this
2049 host_resolver_proc_
->AddSimulatedFailure("*.google.com");
2050 scoped_host_resolver_proc_
.Init(host_resolver_proc_
.get());
2054 scoped_refptr
<net::RuleBasedHostResolverProc
> host_resolver_proc_
;
2055 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_
;
2058 IN_PROC_BROWSER_TEST_F(BrowserTest2
, NoTabsInPopups
) {
2059 chrome::RegisterAppPrefs(L
"Test");
2061 // We start with a normal browser with one tab.
2062 EXPECT_EQ(1, browser()->tab_strip_model()->count());
2064 // Open a popup browser with a single blank foreground tab.
2065 Browser
* popup_browser
= new Browser(
2066 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile()));
2067 chrome::AddTabAt(popup_browser
, GURL(), -1, true);
2068 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2070 // Now try opening another tab in the popup browser.
2071 AddTabWithURLParams
params1(url
, content::PAGE_TRANSITION_TYPED
);
2072 popup_browser
->AddTabWithURL(¶ms1
);
2073 EXPECT_EQ(popup_browser
, params1
.target
);
2075 // The popup should still only have one tab.
2076 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2078 // The normal browser should now have two.
2079 EXPECT_EQ(2, browser()->tab_strip_model()->count());
2081 // Open an app frame browser with a single blank foreground tab.
2082 Browser
* app_browser
= new Browser(Browser::CreateParams::CreateForApp(
2083 L
"Test", browser()->profile(), false));
2084 chrome::AddTabAt(app_browser
, GURL(), -1, true);
2085 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2087 // Now try opening another tab in the app browser.
2088 AddTabWithURLParams
params2(GURL(content::kAboutBlankURL
),
2089 content::PAGE_TRANSITION_TYPED
);
2090 app_browser
->AddTabWithURL(¶ms2
);
2091 EXPECT_EQ(app_browser
, params2
.target
);
2093 // The popup should still only have one tab.
2094 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2096 // The normal browser should now have three.
2097 EXPECT_EQ(3, browser()->tab_strip_model()->count());
2099 // Open an app frame popup browser with a single blank foreground tab.
2100 Browser
* app_popup_browser
= new Browser(Browser::CreateParams::CreateForApp(
2101 L
"Test", browser()->profile(), false));
2102 chrome::AddTabAt(app_popup_browser
, GURL(), -1, true);
2103 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2105 // Now try opening another tab in the app popup browser.
2106 AddTabWithURLParams
params3(GURL(content::kAboutBlankURL
),
2107 content::PAGE_TRANSITION_TYPED
);
2108 app_popup_browser
->AddTabWithURL(¶ms3
);
2109 EXPECT_EQ(app_popup_browser
, params3
.target
);
2111 // The popup should still only have one tab.
2112 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2114 // The normal browser should now have four.
2115 EXPECT_EQ(4, browser()->tab_strip_model()->count());
2117 // Close the additional browsers.
2118 popup_browser
->tab_strip_model()->CloseAllTabs();
2119 app_browser
->tab_strip_model()->CloseAllTabs();
2120 app_popup_browser
->tab_strip_model()->CloseAllTabs();
2124 IN_PROC_BROWSER_TEST_F(BrowserTest
, WindowOpenClose
) {
2125 CommandLine::ForCurrentProcess()->AppendSwitch(
2126 switches::kDisablePopupBlocking
);
2127 GURL url
= ui_test_utils::GetTestUrl(
2128 base::FilePath(), base::FilePath().AppendASCII("window.close.html"));
2130 base::string16 title
= ASCIIToUTF16("Title Of Awesomeness");
2131 content::TitleWatcher
title_watcher(
2132 browser()->tab_strip_model()->GetActiveWebContents(), title
);
2133 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url
, 2);
2134 EXPECT_EQ(title
, title_watcher
.WaitAndGetTitle());
2137 // GTK doesn't use the Browser's fullscreen state.
2138 // TODO(linux_aura) http://crbug.com/163931
2139 // Mac disabled: http://crbug.com/169820
2140 #if !defined(TOOLKIT_GTK) && !defined(OS_MACOSX) && \
2141 !(defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
2142 IN_PROC_BROWSER_TEST_F(BrowserTest
, FullscreenBookmarkBar
) {
2143 #if defined(OS_WIN) && defined(USE_ASH)
2144 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2145 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2149 chrome::ToggleBookmarkBar(browser());
2150 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2151 chrome::ToggleFullscreenMode(browser());
2152 EXPECT_TRUE(browser()->window()->IsFullscreen());
2153 #if defined(OS_MACOSX)
2154 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2155 #elif defined(OS_CHROMEOS)
2156 // TODO(jamescook): If immersive fullscreen is disabled by default, test
2157 // for BookmarkBar::HIDDEN.
2158 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2160 EXPECT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2165 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisallowFileUrlUniversalAccessTest
) {
2166 GURL url
= ui_test_utils::GetTestUrl(
2168 base::FilePath().AppendASCII("fileurl_universalaccess.html"));
2170 base::string16
expected_title(ASCIIToUTF16("Disallowed"));
2171 content::TitleWatcher
title_watcher(
2172 browser()->tab_strip_model()->GetActiveWebContents(), expected_title
);
2173 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("Allowed"));
2174 ui_test_utils::NavigateToURL(browser(), url
);
2175 ASSERT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
2178 class KioskModeTest
: public BrowserTest
{
2182 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2183 command_line
->AppendSwitch(switches::kKioskMode
);
2187 #if defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
2188 // http://crbug.com/103912
2189 // TODO(linux_aura) http://crbug.com/163931
2190 #define MAYBE_EnableKioskModeTest DISABLED_EnableKioskModeTest
2192 #define MAYBE_EnableKioskModeTest EnableKioskModeTest
2194 IN_PROC_BROWSER_TEST_F(KioskModeTest
, MAYBE_EnableKioskModeTest
) {
2195 // Check if browser is in fullscreen mode.
2196 ASSERT_TRUE(browser()->window()->IsFullscreen());
2197 ASSERT_FALSE(browser()->window()->IsFullscreenBubbleVisible());
2201 // This test verifies that Chrome can be launched with a user-data-dir path
2202 // which contains non ASCII characters.
2203 class LaunchBrowserWithNonAsciiUserDatadir
: public BrowserTest
{
2205 LaunchBrowserWithNonAsciiUserDatadir() {}
2207 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2208 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
2209 base::FilePath tmp_profile
= temp_dir_
.path().AppendASCII("tmp_profile");
2210 tmp_profile
= tmp_profile
.Append(L
"Test Chrome G\u00E9raldine");
2212 ASSERT_TRUE(base::CreateDirectory(tmp_profile
));
2213 command_line
->AppendSwitchPath(switches::kUserDataDir
, tmp_profile
);
2216 base::ScopedTempDir temp_dir_
;
2219 IN_PROC_BROWSER_TEST_F(LaunchBrowserWithNonAsciiUserDatadir
,
2220 TestNonAsciiUserDataDir
) {
2221 // Verify that the window is present.
2222 ASSERT_TRUE(browser());
2224 #endif // defined(OS_WIN)
2226 // Tests to ensure that the browser continues running in the background after
2227 // the last window closes.
2228 class RunInBackgroundTest
: public BrowserTest
{
2230 RunInBackgroundTest() {}
2232 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2233 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2237 IN_PROC_BROWSER_TEST_F(RunInBackgroundTest
, RunInBackgroundBasicTest
) {
2238 // Close the browser window, then open a new one - the browser should keep
2240 Profile
* profile
= browser()->profile();
2241 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2242 content::WindowedNotificationObserver
observer(
2243 chrome::NOTIFICATION_BROWSER_CLOSED
,
2244 content::Source
<Browser
>(browser()));
2245 chrome::CloseWindow(browser());
2247 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2249 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2250 chrome::NewEmptyWindow(profile
, chrome::GetActiveDesktop());
2251 browser_added_observer
.WaitForSingleNewBrowser();
2253 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2256 // Tests to ensure that the browser continues running in the background after
2257 // the last window closes.
2258 class NoStartupWindowTest
: public BrowserTest
{
2260 NoStartupWindowTest() {}
2262 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2263 command_line
->AppendSwitch(switches::kNoStartupWindow
);
2264 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2268 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, NoStartupWindowBasicTest
) {
2269 #if defined(OS_WIN) && defined(USE_ASH)
2270 // kNoStartupWindow doesn't make sense in Metro+Ash.
2271 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2275 // No browser window should be started by default.
2276 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2278 // Starting a browser window should work just fine.
2279 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2280 CreateBrowser(ProfileManager::GetActiveUserProfile());
2281 browser_added_observer
.WaitForSingleNewBrowser();
2283 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2286 // Chromeos needs to track app windows because it considers them to be part of
2288 #if !defined(OS_CHROMEOS)
2289 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, DontInitSessionServiceForApps
) {
2290 #if defined(OS_WIN) && defined(USE_ASH)
2291 // kNoStartupWindow doesn't make sense in Metro+Ash.
2292 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2296 Profile
* profile
= ProfileManager::GetActiveUserProfile();
2298 SessionService
* session_service
=
2299 SessionServiceFactory::GetForProfile(profile
);
2300 ASSERT_FALSE(session_service
->processed_any_commands());
2302 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2303 CreateBrowserForApp("blah", profile
);
2304 browser_added_observer
.WaitForSingleNewBrowser();
2306 ASSERT_FALSE(session_service
->processed_any_commands());
2308 #endif // !defined(OS_CHROMEOS)
2310 // This test needs to be placed outside the anonymous namespace because we
2311 // need to access private type of Browser.
2312 class AppModeTest
: public BrowserTest
{
2316 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2317 GURL url
= ui_test_utils::GetTestUrl(
2318 base::FilePath(), base::FilePath().AppendASCII("title1.html"));
2319 command_line
->AppendSwitchASCII(switches::kApp
, url
.spec());
2323 IN_PROC_BROWSER_TEST_F(AppModeTest
, EnableAppModeTest
) {
2324 #if defined(OS_WIN) && defined(USE_ASH)
2325 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2326 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2330 // Test that an application browser window loads correctly.
2332 // Verify the browser is in application mode.
2333 EXPECT_TRUE(browser()->is_app());
2336 // Confirm chrome://version contains some expected content.
2337 IN_PROC_BROWSER_TEST_F(BrowserTest
, AboutVersion
) {
2338 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL
));
2339 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
2340 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("WebKit"), true, true,
2343 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("OS"), true, true,
2346 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("JavaScript"), true,
2351 static const base::FilePath::CharType
* kTestDir
=
2352 FILE_PATH_LITERAL("click_modifier");
2353 static const char kFirstPageTitle
[] = "First window";
2354 static const char kSecondPageTitle
[] = "New window!";
2356 class ClickModifierTest
: public InProcessBrowserTest
{
2358 ClickModifierTest() {
2361 // Returns a url that opens a new window or tab when clicked, via javascript.
2362 GURL
GetWindowOpenURL() {
2363 return ui_test_utils::GetTestUrl(
2364 base::FilePath(kTestDir
),
2365 base::FilePath(FILE_PATH_LITERAL("window_open.html")));
2368 // Returns a url that follows a simple link when clicked, unless affected by
2371 return ui_test_utils::GetTestUrl(
2372 base::FilePath(kTestDir
),
2373 base::FilePath(FILE_PATH_LITERAL("href.html")));
2376 base::string16
getFirstPageTitle() {
2377 return ASCIIToUTF16(kFirstPageTitle
);
2380 base::string16
getSecondPageTitle() {
2381 return ASCIIToUTF16(kSecondPageTitle
);
2384 // Loads our test page and simulates a single click using the supplied button
2385 // and modifiers. The click will cause either a navigation or the creation of
2386 // a new window or foreground or background tab. We verify that the expected
2387 // disposition occurs.
2388 void RunTest(Browser
* browser
,
2391 blink::WebMouseEvent::Button button
,
2392 WindowOpenDisposition disposition
) {
2393 ui_test_utils::NavigateToURL(browser
, url
);
2394 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2395 browser
->host_desktop_type()));
2396 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2397 content::WebContents
* web_contents
=
2398 browser
->tab_strip_model()->GetActiveWebContents();
2399 EXPECT_EQ(url
, web_contents
->GetURL());
2401 if (disposition
== CURRENT_TAB
) {
2402 content::WebContents
* web_contents
=
2403 browser
->tab_strip_model()->GetActiveWebContents();
2404 content::TestNavigationObserver
same_tab_observer(web_contents
);
2405 SimulateMouseClick(web_contents
, modifiers
, button
);
2406 same_tab_observer
.Wait();
2407 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2408 browser
->host_desktop_type()));
2409 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2410 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2414 content::WindowedNotificationObserver
observer(
2415 chrome::NOTIFICATION_TAB_ADDED
,
2416 content::NotificationService::AllSources());
2417 SimulateMouseClick(web_contents
, modifiers
, button
);
2420 if (disposition
== NEW_WINDOW
) {
2421 EXPECT_EQ(2u, chrome::GetBrowserCount(browser
->profile(),
2422 browser
->host_desktop_type()));
2426 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2427 browser
->host_desktop_type()));
2428 EXPECT_EQ(2, browser
->tab_strip_model()->count());
2429 web_contents
= browser
->tab_strip_model()->GetActiveWebContents();
2430 WaitForLoadStop(web_contents
);
2431 if (disposition
== NEW_FOREGROUND_TAB
) {
2432 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2434 ASSERT_EQ(NEW_BACKGROUND_TAB
, disposition
);
2435 EXPECT_EQ(getFirstPageTitle(), web_contents
->GetTitle());
2440 DISALLOW_COPY_AND_ASSIGN(ClickModifierTest
);
2443 // Tests for clicking on elements with handlers that run window.open.
2445 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenBasicClickTest
) {
2447 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2448 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2449 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2452 // TODO(ericu): Alt-click behavior on window.open is platform-dependent and not
2453 // well defined. Should we add tests so we know if it changes?
2455 // Shift-clicks open in a new window.
2456 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftClickTest
) {
2457 int modifiers
= blink::WebInputEvent::ShiftKey
;
2458 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2459 WindowOpenDisposition disposition
= NEW_WINDOW
;
2460 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2463 // Control-clicks open in a background tab.
2464 // On OSX meta [the command key] takes the place of control.
2465 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlClickTest
) {
2466 #if defined(OS_MACOSX)
2467 int modifiers
= blink::WebInputEvent::MetaKey
;
2469 int modifiers
= blink::WebInputEvent::ControlKey
;
2471 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2472 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2473 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2476 // Control-shift-clicks open in a foreground tab.
2477 // On OSX meta [the command key] takes the place of control.
2478 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlShiftClickTest
) {
2479 #if defined(OS_MACOSX)
2480 int modifiers
= blink::WebInputEvent::MetaKey
;
2482 int modifiers
= blink::WebInputEvent::ControlKey
;
2484 modifiers
|= blink::WebInputEvent::ShiftKey
;
2485 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2486 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2487 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2490 // Middle-clicks open in a background tab.
2491 // TODO(linux_aura) http://crbug.com/163931
2492 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
2493 #define MAYBE_WindowOpenMiddleClickTest DISABLED_WindowOpenMiddleClickTest
2495 #define MAYBE_WindowOpenMiddleClickTest WindowOpenMiddleClickTest
2497 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, MAYBE_WindowOpenMiddleClickTest
) {
2499 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2500 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2501 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2504 // Shift-middle-clicks open in a foreground tab.
2505 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftMiddleClickTest
) {
2506 int modifiers
= blink::WebInputEvent::ShiftKey
;
2507 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2508 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2509 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2512 // Tests for clicking on normal links.
2514 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefBasicClickTest
) {
2516 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2517 WindowOpenDisposition disposition
= CURRENT_TAB
;
2518 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2521 // TODO(ericu): Alt-click behavior on links is platform-dependent and not well
2522 // defined. Should we add tests so we know if it changes?
2524 // Shift-clicks open in a new window.
2525 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefShiftClickTest
) {
2526 int modifiers
= blink::WebInputEvent::ShiftKey
;
2527 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2528 WindowOpenDisposition disposition
= NEW_WINDOW
;
2529 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2532 // Control-clicks open in a background tab.
2533 // On OSX meta [the command key] takes the place of control.
2534 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefControlClickTest
) {
2535 #if defined(OS_MACOSX)
2536 int modifiers
= blink::WebInputEvent::MetaKey
;
2538 int modifiers
= blink::WebInputEvent::ControlKey
;
2540 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2541 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2542 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2545 // Control-shift-clicks open in a foreground tab.
2546 // On OSX meta [the command key] takes the place of control.
2547 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefControlShiftClickTest
) {
2548 #if defined(OS_MACOSX)
2549 int modifiers
= blink::WebInputEvent::MetaKey
;
2551 int modifiers
= blink::WebInputEvent::ControlKey
;
2553 modifiers
|= blink::WebInputEvent::ShiftKey
;
2554 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2555 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2556 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2559 // Middle-clicks open in a background tab.
2560 // TODO(linux_aura) http://crbug.com/163931
2561 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
2562 #define MAYBE_HrefMiddleClickTest DISABLED_HrefMiddleClickTest
2564 #define MAYBE_HrefMiddleClickTest HrefMiddleClickTest
2566 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, MAYBE_HrefMiddleClickTest
) {
2568 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2569 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2570 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2573 // Shift-middle-clicks open in a foreground tab.
2574 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefShiftMiddleClickTest
) {
2575 int modifiers
= blink::WebInputEvent::ShiftKey
;
2576 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2577 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2578 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2581 IN_PROC_BROWSER_TEST_F(BrowserTest
, GetSizeForNewRenderView
) {
2582 // The instant extended NTP has javascript that does not work with
2583 // ui_test_utils::NavigateToURL. The NTP rvh reloads when the browser tries
2584 // to navigate away from the page, which causes the WebContents to end up in
2585 // an inconsistent state. (is_loaded = true, last_commited_url=ntp,
2586 // visible_url=title1.html)
2587 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled
,
2589 ASSERT_TRUE(test_server()->Start());
2590 // Create an HTTPS server for cross-site transition.
2591 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
2592 net::SpawnedTestServer::kLocalhost
,
2593 base::FilePath(kDocRoot
));
2594 ASSERT_TRUE(https_test_server
.Start());
2597 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2598 ASSERT_EQ(BookmarkBar::DETACHED
, browser()->bookmark_bar_state());
2599 WebContents
* web_contents
=
2600 browser()->tab_strip_model()->GetActiveWebContents();
2601 content::RenderViewHost
* prev_rvh
= web_contents
->GetRenderViewHost();
2602 const int height_inset
=
2603 browser()->window()->GetRenderViewHeightInsetWithDetachedBookmarkBar();
2604 const gfx::Size initial_wcv_size
=
2605 web_contents
->GetView()->GetContainerSize();
2606 RenderViewSizeObserver
observer(web_contents
, browser()->window());
2608 // Navigate to a non-NTP page, without resizing WebContentsView.
2609 ui_test_utils::NavigateToURL(browser(),
2610 test_server()->GetURL("files/title1.html"));
2611 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2612 // A new RenderViewHost should be created.
2613 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2614 prev_rvh
= web_contents
->GetRenderViewHost();
2615 gfx::Size rwhv_create_size0
, rwhv_commit_size0
, wcv_commit_size0
;
2616 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2620 // The create height of RenderWidgetHostView should include the height inset.
2621 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2622 initial_wcv_size
.height() + height_inset
),
2624 // When a navigation entry is committed, the size of RenderWidgetHostView
2625 // should be the same as when it was first created.
2626 EXPECT_EQ(rwhv_create_size0
, rwhv_commit_size0
);
2627 // Sizes of the current RenderWidgetHostView and WebContentsView should not
2628 // change before and after WebContentsDelegate::DidNavigateMainFramePostCommit
2629 // (implemented by Browser); we obtain the sizes before PostCommit via
2630 // WebContentsObserver::NavigationEntryCommitted (implemented by
2631 // RenderViewSizeObserver).
2632 EXPECT_EQ(rwhv_commit_size0
,
2633 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2634 // The behavior differs between OSX and views.
2635 // In OSX, the wcv does not change size until after the commit, when the
2636 // bookmark bar disappears (correct).
2637 // In views, the wcv changes size at commit time.
2638 #if defined(OS_MACOSX)
2639 EXPECT_EQ(gfx::Size(wcv_commit_size0
.width(),
2640 wcv_commit_size0
.height() + height_inset
),
2641 web_contents
->GetView()->GetContainerSize());
2643 EXPECT_EQ(wcv_commit_size0
, web_contents
->GetView()->GetContainerSize());
2646 // Navigate to another non-NTP page, without resizing WebContentsView.
2647 ui_test_utils::NavigateToURL(browser(),
2648 https_test_server
.GetURL("files/title2.html"));
2649 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2650 // A new RenderVieHost should be created.
2651 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2652 gfx::Size rwhv_create_size1
, rwhv_commit_size1
, wcv_commit_size1
;
2653 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2657 EXPECT_EQ(rwhv_create_size1
, rwhv_commit_size1
);
2658 EXPECT_EQ(rwhv_commit_size1
,
2659 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2660 EXPECT_EQ(wcv_commit_size1
, web_contents
->GetView()->GetContainerSize());
2662 // Navigate from NTP to a non-NTP page, resizing WebContentsView while
2663 // navigation entry is pending.
2664 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2665 gfx::Size
wcv_resize_insets(1, 1);
2666 observer
.set_wcv_resize_insets(wcv_resize_insets
);
2667 ui_test_utils::NavigateToURL(browser(),
2668 test_server()->GetURL("files/title2.html"));
2669 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2670 gfx::Size rwhv_create_size2
, rwhv_commit_size2
, wcv_commit_size2
;
2671 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2676 // The behavior on OSX and Views is incorrect in this edge case, but they are
2677 // differently incorrect.
2678 // The behavior should be:
2679 // initial wcv size: (100,100) (to choose random numbers)
2680 // initial rwhv size: (100,140)
2681 // commit wcv size: (101, 101)
2682 // commit rwhv size: (101, 141)
2683 // final wcv size: (101, 141)
2684 // final rwhv size: (101, 141)
2686 // On OSX, the commit rwhv size is (101, 101)
2687 // On views, the commit wcv size is (101, 141)
2688 // All other sizes are correct.
2690 // The create height of RenderWidgetHostView should include the height inset.
2691 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2692 initial_wcv_size
.height() + height_inset
),
2694 gfx::Size
exp_commit_size(initial_wcv_size
);
2696 #if defined(OS_MACOSX)
2697 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2698 wcv_resize_insets
.height());
2700 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2701 wcv_resize_insets
.height() + height_inset
);
2703 EXPECT_EQ(exp_commit_size
, rwhv_commit_size2
);
2704 EXPECT_EQ(exp_commit_size
, wcv_commit_size2
);
2705 gfx::Size
exp_final_size(initial_wcv_size
);
2706 exp_final_size
.Enlarge(wcv_resize_insets
.width(),
2707 wcv_resize_insets
.height() + height_inset
);
2708 EXPECT_EQ(exp_final_size
,
2709 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2710 EXPECT_EQ(exp_final_size
, web_contents
->GetView()->GetContainerSize());