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/common/frame_navigate_params.h"
74 #include "content/public/common/page_transition_types.h"
75 #include "content/public/common/renderer_preferences.h"
76 #include "content/public/common/url_constants.h"
77 #include "content/public/test/browser_test_utils.h"
78 #include "content/public/test/test_navigation_observer.h"
79 #include "extensions/browser/extension_system.h"
80 #include "extensions/common/extension.h"
81 #include "extensions/common/extension_set.h"
82 #include "grit/chromium_strings.h"
83 #include "grit/generated_resources.h"
84 #include "net/dns/mock_host_resolver.h"
85 #include "net/test/spawned_test_server/spawned_test_server.h"
86 #include "ui/base/l10n/l10n_util.h"
88 #if defined(OS_MACOSX)
89 #include "base/mac/mac_util.h"
90 #include "base/mac/scoped_nsautorelease_pool.h"
91 #include "chrome/browser/ui/cocoa/run_loop_testing.h"
95 #include "base/i18n/rtl.h"
96 #include "chrome/browser/browser_process.h"
99 using base::ASCIIToUTF16
;
100 using content::InterstitialPage
;
101 using content::HostZoomMap
;
102 using content::NavigationController
;
103 using content::NavigationEntry
;
104 using content::OpenURLParams
;
105 using content::Referrer
;
106 using content::WebContents
;
107 using content::WebContentsObserver
;
108 using extensions::Extension
;
112 const char* kBeforeUnloadHTML
=
113 "<html><head><title>beforeunload</title></head><body>"
114 "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
117 const char* kOpenNewBeforeUnloadPage
=
118 "w=window.open(); w.onbeforeunload=function(e){return 'foo'};";
120 const base::FilePath::CharType
* kBeforeUnloadFile
=
121 FILE_PATH_LITERAL("beforeunload.html");
123 const base::FilePath::CharType
* kTitle1File
= FILE_PATH_LITERAL("title1.html");
124 const base::FilePath::CharType
* kTitle2File
= FILE_PATH_LITERAL("title2.html");
126 const base::FilePath::CharType kDocRoot
[] =
127 FILE_PATH_LITERAL("chrome/test/data");
129 // Given a page title, returns the expected window caption string.
130 base::string16
WindowCaptionFromPageTitle(const base::string16
& page_title
) {
131 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
132 // On Mac or ChromeOS, we don't want to suffix the page title with
133 // the application name.
134 if (page_title
.empty())
135 return l10n_util::GetStringUTF16(IDS_BROWSER_WINDOW_MAC_TAB_UNTITLED
);
138 if (page_title
.empty())
139 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
);
141 return l10n_util::GetStringFUTF16(IDS_BROWSER_WINDOW_TITLE_FORMAT
,
146 // Returns the number of active RenderProcessHosts.
147 int CountRenderProcessHosts() {
149 for (content::RenderProcessHost::iterator
i(
150 content::RenderProcessHost::AllHostsIterator());
151 !i
.IsAtEnd(); i
.Advance())
156 class MockTabStripModelObserver
: public TabStripModelObserver
{
158 MockTabStripModelObserver() : closing_count_(0) {}
160 virtual void TabClosingAt(TabStripModel
* tab_strip_model
,
161 WebContents
* contents
,
162 int index
) OVERRIDE
{
166 int closing_count() const { return closing_count_
; }
171 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver
);
174 class InterstitialObserver
: public content::WebContentsObserver
{
176 InterstitialObserver(content::WebContents
* web_contents
,
177 const base::Closure
& attach_callback
,
178 const base::Closure
& detach_callback
)
179 : WebContentsObserver(web_contents
),
180 attach_callback_(attach_callback
),
181 detach_callback_(detach_callback
) {
184 virtual void DidAttachInterstitialPage() OVERRIDE
{
185 attach_callback_
.Run();
188 virtual void DidDetachInterstitialPage() OVERRIDE
{
189 detach_callback_
.Run();
193 base::Closure attach_callback_
;
194 base::Closure detach_callback_
;
196 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver
);
199 // Causes the browser to swap processes on a redirect to an HTTPS URL.
200 class TransferHttpsRedirectsContentBrowserClient
201 : public chrome::ChromeContentBrowserClient
{
203 virtual bool ShouldSwapProcessesForRedirect(
204 content::ResourceContext
* resource_context
,
205 const GURL
& current_url
,
206 const GURL
& new_url
) OVERRIDE
{
207 return new_url
.SchemeIs(content::kHttpsScheme
);
211 // Used by CloseWithAppMenuOpen. Invokes CloseWindow on the supplied browser.
212 void CloseWindowCallback(Browser
* browser
) {
213 chrome::CloseWindow(browser
);
216 // Used by CloseWithAppMenuOpen. Posts a CloseWindowCallback and shows the app
218 void RunCloseWithAppMenuCallback(Browser
* browser
) {
219 // ShowAppMenu is modal under views. Schedule a task that closes the window.
220 base::MessageLoop::current()->PostTask(
221 FROM_HERE
, base::Bind(&CloseWindowCallback
, browser
));
222 chrome::ShowAppMenu(browser
);
225 // Displays "INTERSTITIAL" while the interstitial is attached.
226 // (InterstitialPage can be used in a test directly, but there would be no way
227 // to visually tell if it is showing or not.)
228 class TestInterstitialPage
: public content::InterstitialPageDelegate
{
230 TestInterstitialPage(WebContents
* tab
, bool new_navigation
, const GURL
& url
) {
231 interstitial_page_
= InterstitialPage::Create(
232 tab
, new_navigation
, url
, this);
233 interstitial_page_
->Show();
235 virtual ~TestInterstitialPage() { }
237 interstitial_page_
->Proceed();
240 interstitial_page_
->DontProceed();
243 virtual std::string
GetHTMLContents() OVERRIDE
{
244 return "<h1>INTERSTITIAL</h1>";
248 InterstitialPage
* interstitial_page_
; // Owns us.
251 class RenderViewSizeObserver
: public content::WebContentsObserver
{
253 RenderViewSizeObserver(content::WebContents
* web_contents
,
254 BrowserWindow
* browser_window
)
255 : WebContentsObserver(web_contents
),
256 browser_window_(browser_window
) {
259 void GetSizeForRenderViewHost(
260 content::RenderViewHost
* render_view_host
,
261 gfx::Size
* rwhv_create_size
,
262 gfx::Size
* rwhv_commit_size
,
263 gfx::Size
* wcv_commit_size
) {
264 RenderViewSizes::const_iterator result
= render_view_sizes_
.end();
265 result
= render_view_sizes_
.find(render_view_host
);
266 if (result
!= render_view_sizes_
.end()) {
267 *rwhv_create_size
= result
->second
.rwhv_create_size
;
268 *rwhv_commit_size
= result
->second
.rwhv_commit_size
;
269 *wcv_commit_size
= result
->second
.wcv_commit_size
;
273 void set_wcv_resize_insets(const gfx::Size
& wcv_resize_insets
) {
274 wcv_resize_insets_
= wcv_resize_insets
;
277 // Cache the size when RenderViewHost is first created.
278 virtual void RenderViewCreated(
279 content::RenderViewHost
* render_view_host
) OVERRIDE
{
280 render_view_sizes_
[render_view_host
].rwhv_create_size
=
281 render_view_host
->GetView()->GetViewBounds().size();
284 // Enlarge WebContentsView by |wcv_resize_insets_| while the navigation entry
286 virtual void DidStartNavigationToPendingEntry(
288 NavigationController::ReloadType reload_type
) OVERRIDE
{
289 if (wcv_resize_insets_
.IsEmpty())
291 // Resizing the main browser window by |wcv_resize_insets_| will
292 // automatically resize the WebContentsView by the same amount.
293 // Just resizing WebContentsView directly doesn't work on Linux, because the
294 // next automatic layout of the browser window will resize WebContentsView
295 // back to the previous size. To make it consistent, resize main browser
296 // window on all platforms.
297 gfx::Rect
bounds(browser_window_
->GetBounds());
298 gfx::Size
size(bounds
.size());
299 size
.Enlarge(wcv_resize_insets_
.width(), wcv_resize_insets_
.height());
300 bounds
.set_size(size
);
301 browser_window_
->SetBounds(bounds
);
302 // Let the message loop run so that resize actually takes effect.
303 content::RunAllPendingInMessageLoop();
306 // Cache the sizes of RenderWidgetHostView and WebContentsView when the
307 // navigation entry is committed, which is before
308 // WebContentsDelegate::DidNavigateMainFramePostCommit is called.
309 virtual void NavigationEntryCommitted(
310 const content::LoadCommittedDetails
& details
) OVERRIDE
{
311 content::RenderViewHost
* rvh
= web_contents()->GetRenderViewHost();
312 render_view_sizes_
[rvh
].rwhv_commit_size
=
313 web_contents()->GetRenderWidgetHostView()->GetViewBounds().size();
314 render_view_sizes_
[rvh
].wcv_commit_size
=
315 web_contents()->GetContainerBounds().size();
320 gfx::Size rwhv_create_size
; // Size of RenderWidgetHostView when created.
321 gfx::Size rwhv_commit_size
; // Size of RenderWidgetHostView when committed.
322 gfx::Size wcv_commit_size
; // Size of WebContentsView when committed.
325 typedef std::map
<content::RenderViewHost
*, Sizes
> RenderViewSizes
;
326 RenderViewSizes render_view_sizes_
;
327 // Enlarge WebContentsView by this size insets in
328 // DidStartNavigationToPendingEntry.
329 gfx::Size wcv_resize_insets_
;
330 BrowserWindow
* browser_window_
; // Weak ptr.
332 DISALLOW_COPY_AND_ASSIGN(RenderViewSizeObserver
);
337 class BrowserTest
: public ExtensionBrowserTest
{
339 // In RTL locales wrap the page title with RTL embedding characters so that it
340 // matches the value returned by GetWindowTitle().
341 base::string16
LocaleWindowCaptionFromPageTitle(
342 const base::string16
& expected_title
) {
343 base::string16 page_title
= WindowCaptionFromPageTitle(expected_title
);
345 std::string locale
= g_browser_process
->GetApplicationLocale();
346 if (base::i18n::GetTextDirectionForLocale(locale
.c_str()) ==
347 base::i18n::RIGHT_TO_LEFT
) {
348 base::i18n::WrapStringWithLTRFormatting(&page_title
);
353 // Do we need to use the above code on POSIX as well?
358 // Returns the app extension aptly named "App Test".
359 const Extension
* GetExtension() {
360 const extensions::ExtensionSet
* extensions
=
361 extensions::ExtensionSystem::Get(
362 browser()->profile())->extension_service()->extensions();
363 for (extensions::ExtensionSet::const_iterator it
= extensions
->begin();
364 it
!= extensions
->end(); ++it
) {
365 if ((*it
)->name() == "App Test")
373 // Launch the app on a page with no title, check that the app title was set
375 IN_PROC_BROWSER_TEST_F(BrowserTest
, NoTitle
) {
376 #if defined(OS_WIN) && defined(USE_ASH)
377 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
378 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
382 ui_test_utils::NavigateToURL(
383 browser(), ui_test_utils::GetTestUrl(
384 base::FilePath(base::FilePath::kCurrentDirectory
),
385 base::FilePath(kTitle1File
)));
386 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(ASCIIToUTF16("title1.html")),
387 browser()->GetWindowTitleForCurrentTab());
388 base::string16 tab_title
;
389 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title
));
390 EXPECT_EQ(ASCIIToUTF16("title1.html"), tab_title
);
393 // Launch the app, navigate to a page with a title, check that the app title
394 // was set correctly.
395 IN_PROC_BROWSER_TEST_F(BrowserTest
, Title
) {
396 #if defined(OS_WIN) && defined(USE_ASH)
397 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
398 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
402 ui_test_utils::NavigateToURL(
403 browser(), ui_test_utils::GetTestUrl(
404 base::FilePath(base::FilePath::kCurrentDirectory
),
405 base::FilePath(kTitle2File
)));
406 const base::string16
test_title(ASCIIToUTF16("Title Of Awesomeness"));
407 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(test_title
),
408 browser()->GetWindowTitleForCurrentTab());
409 base::string16 tab_title
;
410 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title
));
411 EXPECT_EQ(test_title
, tab_title
);
414 IN_PROC_BROWSER_TEST_F(BrowserTest
, JavascriptAlertActivatesTab
) {
415 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
416 base::FilePath::kCurrentDirectory
), base::FilePath(kTitle1File
)));
417 ui_test_utils::NavigateToURL(browser(), url
);
418 AddTabAtIndex(0, url
, content::PAGE_TRANSITION_TYPED
);
419 EXPECT_EQ(2, browser()->tab_strip_model()->count());
420 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
421 WebContents
* second_tab
= browser()->tab_strip_model()->GetWebContentsAt(1);
422 ASSERT_TRUE(second_tab
);
423 second_tab
->GetMainFrame()->ExecuteJavaScript(
424 ASCIIToUTF16("alert('Activate!');"));
425 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
426 alert
->CloseModalDialog();
427 EXPECT_EQ(2, browser()->tab_strip_model()->count());
428 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
432 #if defined(OS_WIN) && !defined(NDEBUG)
433 // http://crbug.com/114859. Times out frequently on Windows.
434 #define MAYBE_ThirtyFourTabs DISABLED_ThirtyFourTabs
436 #define MAYBE_ThirtyFourTabs ThirtyFourTabs
439 // Create 34 tabs and verify that a lot of processes have been created. The
440 // exact number of processes depends on the amount of memory. Previously we
441 // had a hard limit of 31 processes and this test is mainly directed at
442 // verifying that we don't crash when we pass this limit.
443 // Warning: this test can take >30 seconds when running on a slow (low
444 // memory?) Mac builder.
445 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_ThirtyFourTabs
) {
446 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
447 base::FilePath::kCurrentDirectory
), base::FilePath(kTitle2File
)));
449 // There is one initial tab.
450 const int kTabCount
= 34;
451 for (int ix
= 0; ix
!= (kTabCount
- 1); ++ix
) {
452 chrome::AddSelectedTabWithURL(browser(), url
,
453 content::PAGE_TRANSITION_TYPED
);
455 EXPECT_EQ(kTabCount
, browser()->tab_strip_model()->count());
457 // See GetMaxRendererProcessCount() in
458 // content/browser/renderer_host/render_process_host_impl.cc
459 // for the algorithm to decide how many processes to create.
460 const int kExpectedProcessCount
=
461 #if defined(ARCH_CPU_64_BITS)
466 if (base::SysInfo::AmountOfPhysicalMemoryMB() >= 2048) {
467 EXPECT_GE(CountRenderProcessHosts(), kExpectedProcessCount
);
469 EXPECT_LT(CountRenderProcessHosts(), kExpectedProcessCount
);
473 // Test that a browser-initiated navigation to an aborted URL load leaves around
474 // a pending entry if we start from the NTP but not from a normal page.
475 // See http://crbug.com/355537.
476 IN_PROC_BROWSER_TEST_F(BrowserTest
, ClearPendingOnFailUnlessNTP
) {
477 ASSERT_TRUE(test_server()->Start());
478 WebContents
* web_contents
=
479 browser()->tab_strip_model()->GetActiveWebContents();
480 GURL
ntp_url(chrome::GetNewTabPageURL(browser()->profile()));
481 ui_test_utils::NavigateToURL(browser(), ntp_url
);
483 // Navigate to a 204 URL (aborts with no content) on the NTP and make sure it
484 // sticks around so that the user can edit it.
485 GURL
abort_url(test_server()->GetURL("nocontent"));
487 content::WindowedNotificationObserver
stop_observer(
488 content::NOTIFICATION_LOAD_STOP
,
489 content::Source
<NavigationController
>(
490 &web_contents
->GetController()));
491 browser()->OpenURL(OpenURLParams(abort_url
, Referrer(), CURRENT_TAB
,
492 content::PAGE_TRANSITION_TYPED
, false));
493 stop_observer
.Wait();
494 EXPECT_TRUE(web_contents
->GetController().GetPendingEntry());
495 EXPECT_EQ(abort_url
, web_contents
->GetVisibleURL());
498 // Navigate to a real URL.
499 GURL
real_url(test_server()->GetURL("title1.html"));
500 ui_test_utils::NavigateToURL(browser(), real_url
);
501 EXPECT_EQ(real_url
, web_contents
->GetVisibleURL());
503 // Now navigating to a 204 URL should clear the pending entry.
505 content::WindowedNotificationObserver
stop_observer(
506 content::NOTIFICATION_LOAD_STOP
,
507 content::Source
<NavigationController
>(
508 &web_contents
->GetController()));
509 browser()->OpenURL(OpenURLParams(abort_url
, Referrer(), CURRENT_TAB
,
510 content::PAGE_TRANSITION_TYPED
, false));
511 stop_observer
.Wait();
512 EXPECT_FALSE(web_contents
->GetController().GetPendingEntry());
513 EXPECT_EQ(real_url
, web_contents
->GetVisibleURL());
517 // Test for crbug.com/297289. Ensure that modal dialogs are closed when a
518 // cross-process navigation is ready to commit.
519 IN_PROC_BROWSER_TEST_F(BrowserTest
, CrossProcessNavCancelsDialogs
) {
520 ASSERT_TRUE(test_server()->Start());
521 host_resolver()->AddRule("www.example.com", "127.0.0.1");
522 GURL
url(test_server()->GetURL("empty.html"));
523 ui_test_utils::NavigateToURL(browser(), url
);
525 // Test this with multiple alert dialogs to ensure that we can navigate away
526 // even if the renderer tries to synchronously create more.
527 // See http://crbug.com/312490.
528 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
529 contents
->GetMainFrame()->ExecuteJavaScript(
530 ASCIIToUTF16("alert('one'); alert('two');"));
531 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
532 EXPECT_TRUE(alert
->IsValid());
533 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
534 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
536 // A cross-site navigation should force the dialog to close.
537 GURL
url2("http://www.example.com/empty.html");
538 ui_test_utils::NavigateToURL(browser(), url2
);
539 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
541 // Make sure input events still work in the renderer process.
542 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
545 // Make sure that dialogs are closed after a renderer process dies, and that
546 // subsequent navigations work. See http://crbug/com/343265.
547 IN_PROC_BROWSER_TEST_F(BrowserTest
, SadTabCancelsDialogs
) {
548 ASSERT_TRUE(test_server()->Start());
549 host_resolver()->AddRule("www.example.com", "127.0.0.1");
550 GURL
beforeunload_url(test_server()->GetURL("files/beforeunload.html"));
551 ui_test_utils::NavigateToURL(browser(), beforeunload_url
);
553 // Start a navigation to trigger the beforeunload dialog.
554 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
555 contents
->GetMainFrame()->ExecuteJavaScript(
556 ASCIIToUTF16("window.location.href = 'data:text/html,foo'"));
557 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
558 EXPECT_TRUE(alert
->IsValid());
559 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
560 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
562 // Crash the renderer process and ensure the dialog is gone.
563 content::RenderProcessHost
* child_process
= contents
->GetRenderProcessHost();
564 content::RenderProcessHostWatcher
crash_observer(
566 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
567 base::KillProcess(child_process
->GetHandle(), 0, false);
568 crash_observer
.Wait();
569 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
571 // Make sure subsequent navigations work.
572 GURL
url2("http://www.example.com/files/empty.html");
573 ui_test_utils::NavigateToURL(browser(), url2
);
576 // Make sure that dialogs opened by subframes are closed when the process dies.
577 // See http://crbug.com/366510.
578 IN_PROC_BROWSER_TEST_F(BrowserTest
, SadTabCancelsSubframeDialogs
) {
579 // Navigate to an iframe that opens an alert dialog.
580 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
581 contents
->GetMainFrame()->ExecuteJavaScript(
582 ASCIIToUTF16("window.location.href = 'data:text/html,"
583 "<iframe srcdoc=\"<script>alert(1)</script>\">'"));
584 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
585 EXPECT_TRUE(alert
->IsValid());
586 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
587 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
589 // Crash the renderer process and ensure the dialog is gone.
590 content::RenderProcessHost
* child_process
= contents
->GetRenderProcessHost();
591 content::RenderProcessHostWatcher
crash_observer(
593 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
594 base::KillProcess(child_process
->GetHandle(), 0, false);
595 crash_observer
.Wait();
596 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
598 // Make sure subsequent navigations work.
599 GURL
url2("data:text/html,foo");
600 ui_test_utils::NavigateToURL(browser(), url2
);
603 // Test for crbug.com/22004. Reloading a page with a before unload handler and
604 // then canceling the dialog should not leave the throbber spinning.
605 IN_PROC_BROWSER_TEST_F(BrowserTest
, ReloadThenCancelBeforeUnload
) {
606 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
607 ui_test_utils::NavigateToURL(browser(), url
);
609 // Navigate to another page, but click cancel in the dialog. Make sure that
610 // the throbber stops spinning.
611 chrome::Reload(browser(), CURRENT_TAB
);
612 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
613 alert
->CloseModalDialog();
615 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
617 // Clear the beforeunload handler so the test can easily exit.
618 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
619 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
622 class RedirectObserver
: public content::WebContentsObserver
{
624 explicit RedirectObserver(content::WebContents
* web_contents
)
625 : WebContentsObserver(web_contents
) {
628 virtual void DidNavigateAnyFrame(
629 const content::LoadCommittedDetails
& details
,
630 const content::FrameNavigateParams
& params
) OVERRIDE
{
634 virtual void WebContentsDestroyed(WebContents
* contents
) OVERRIDE
{
635 // Make sure we don't close the tab while the observer is in scope.
636 // See http://crbug.com/314036.
637 FAIL() << "WebContents closed during navigation (http://crbug.com/314036).";
640 const content::FrameNavigateParams
& params() const {
645 content::FrameNavigateParams params_
;
647 DISALLOW_COPY_AND_ASSIGN(RedirectObserver
);
650 // Ensure that a transferred cross-process navigation does not generate
651 // DidStopLoading events until the navigation commits. If it did, then
652 // ui_test_utils::NavigateToURL would proceed before the URL had committed.
653 // http://crbug.com/243957.
654 IN_PROC_BROWSER_TEST_F(BrowserTest
, NoStopDuringTransferUntilCommit
) {
655 // Create HTTP and HTTPS servers for a cross-site transition.
656 ASSERT_TRUE(test_server()->Start());
657 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
658 net::SpawnedTestServer::kLocalhost
,
659 base::FilePath(kDocRoot
));
660 ASSERT_TRUE(https_test_server
.Start());
662 // Temporarily replace ContentBrowserClient with one that will cause a
663 // process swap on all redirects to HTTPS URLs.
664 TransferHttpsRedirectsContentBrowserClient new_client
;
665 content::ContentBrowserClient
* old_client
=
666 SetBrowserClientForTesting(&new_client
);
668 GURL
init_url(test_server()->GetURL("files/title1.html"));
669 ui_test_utils::NavigateToURL(browser(), init_url
);
671 // Navigate to a same-site page that redirects, causing a transfer.
672 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
674 // Create a RedirectObserver that goes away before we close the tab.
676 RedirectObserver
redirect_observer(contents
);
677 GURL
dest_url(https_test_server
.GetURL("files/title2.html"));
678 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
680 ui_test_utils::NavigateToURL(browser(), redirect_url
);
682 // We should immediately see the new committed entry.
683 EXPECT_FALSE(contents
->GetController().GetPendingEntry());
685 contents
->GetController().GetLastCommittedEntry()->GetURL());
687 // We should keep track of the original request URL, redirect chain, and
688 // page transition type during a transfer, since these are necessary for
689 // history autocomplete to work.
690 EXPECT_EQ(redirect_url
, contents
->GetController().GetLastCommittedEntry()->
691 GetOriginalRequestURL());
692 EXPECT_EQ(2U, redirect_observer
.params().redirects
.size());
693 EXPECT_EQ(redirect_url
, redirect_observer
.params().redirects
.at(0));
694 EXPECT_EQ(dest_url
, redirect_observer
.params().redirects
.at(1));
695 EXPECT_TRUE(PageTransitionCoreTypeIs(redirect_observer
.params().transition
,
696 content::PAGE_TRANSITION_TYPED
));
699 // Restore previous browser client.
700 SetBrowserClientForTesting(old_client
);
703 // Tests that a cross-process redirect will only cause the beforeunload
704 // handler to run once.
705 IN_PROC_BROWSER_TEST_F(BrowserTest
, SingleBeforeUnloadAfterRedirect
) {
706 // Create HTTP and HTTPS servers for a cross-site transition.
707 ASSERT_TRUE(test_server()->Start());
708 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
709 net::SpawnedTestServer::kLocalhost
,
710 base::FilePath(kDocRoot
));
711 ASSERT_TRUE(https_test_server
.Start());
713 // Temporarily replace ContentBrowserClient with one that will cause a
714 // process swap on all redirects to HTTPS URLs.
715 TransferHttpsRedirectsContentBrowserClient new_client
;
716 content::ContentBrowserClient
* old_client
=
717 SetBrowserClientForTesting(&new_client
);
719 // Navigate to a page with a beforeunload handler.
720 GURL
url(test_server()->GetURL("files/beforeunload.html"));
721 ui_test_utils::NavigateToURL(browser(), url
);
723 // Navigate to a URL that redirects to another process and approve the
724 // beforeunload dialog that pops up.
725 content::WindowedNotificationObserver
nav_observer(
726 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
727 content::NotificationService::AllSources());
728 GURL
https_url(https_test_server
.GetURL("files/title1.html"));
729 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
731 browser()->OpenURL(OpenURLParams(redirect_url
, Referrer(), CURRENT_TAB
,
732 content::PAGE_TRANSITION_TYPED
, false));
733 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
735 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
736 alert
->native_dialog()->AcceptAppModalDialog();
739 // Restore previous browser client.
740 SetBrowserClientForTesting(old_client
);
743 // Test for crbug.com/80401. Canceling a before unload dialog should reset
744 // the URL to the previous page's URL.
745 IN_PROC_BROWSER_TEST_F(BrowserTest
, CancelBeforeUnloadResetsURL
) {
746 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
747 base::FilePath::kCurrentDirectory
), base::FilePath(kBeforeUnloadFile
)));
748 ui_test_utils::NavigateToURL(browser(), url
);
750 // Navigate to a page that triggers a cross-site transition.
751 ASSERT_TRUE(test_server()->Start());
752 GURL
url2(test_server()->GetURL("files/title1.html"));
753 browser()->OpenURL(OpenURLParams(
754 url2
, Referrer(), CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false));
756 content::WindowedNotificationObserver
host_destroyed_observer(
757 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED
,
758 content::NotificationService::AllSources());
760 // Cancel the dialog.
761 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
762 alert
->CloseModalDialog();
764 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
766 // Verify there are no pending history items after the dialog is cancelled.
767 // (see crbug.com/93858)
768 NavigationEntry
* entry
= browser()->tab_strip_model()->
769 GetActiveWebContents()->GetController().GetPendingEntry();
770 EXPECT_EQ(NULL
, entry
);
772 // Wait for the ShouldClose_ACK to arrive. We can detect it by waiting for
773 // the pending RVH to be destroyed.
774 host_destroyed_observer
.Wait();
775 EXPECT_EQ(url
, browser()->toolbar_model()->GetURL());
777 // Clear the beforeunload handler so the test can easily exit.
778 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
779 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
782 // Crashy on mac. http://crbug.com/38522 Crashy on win too (after 3 years).
783 #if defined(OS_MACOSX) || defined(OS_WIN)
784 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
785 DISABLED_SingleBeforeUnloadAfterWindowClose
787 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
788 SingleBeforeUnloadAfterWindowClose
791 // Test for crbug.com/11647. A page closed with window.close() should not have
792 // two beforeunload dialogs shown.
793 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_SingleBeforeUnloadAfterWindowClose
) {
794 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
795 ExecuteJavaScript(ASCIIToUTF16(kOpenNewBeforeUnloadPage
));
797 // Close the new window with JavaScript, which should show a single
798 // beforeunload dialog. Then show another alert, to make it easy to verify
799 // that a second beforeunload dialog isn't shown.
800 browser()->tab_strip_model()->GetWebContentsAt(0)->GetMainFrame()->
801 ExecuteJavaScript(ASCIIToUTF16("w.close(); alert('bar');"));
802 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
803 alert
->native_dialog()->AcceptAppModalDialog();
805 alert
= ui_test_utils::WaitForAppModalDialog();
806 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->
807 is_before_unload_dialog());
808 alert
->native_dialog()->AcceptAppModalDialog();
811 // BrowserTest.BeforeUnloadVsBeforeReload times out on Windows.
812 // http://crbug.com/130411
814 #define MAYBE_BeforeUnloadVsBeforeReload DISABLED_BeforeUnloadVsBeforeReload
816 #define MAYBE_BeforeUnloadVsBeforeReload BeforeUnloadVsBeforeReload
819 // Test that when a page has an onunload handler, reloading a page shows a
820 // different dialog than navigating to a different page.
821 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_BeforeUnloadVsBeforeReload
) {
822 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
823 ui_test_utils::NavigateToURL(browser(), url
);
825 // Reload the page, and check that we get a "before reload" dialog.
826 chrome::Reload(browser(), CURRENT_TAB
);
827 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
828 EXPECT_TRUE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
830 // Cancel the reload.
831 alert
->native_dialog()->CancelAppModalDialog();
833 // Navigate to another url, and check that we get a "before unload" dialog.
834 GURL
url2(std::string("about:blank"));
835 browser()->OpenURL(OpenURLParams(
836 url2
, Referrer(), CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false));
838 alert
= ui_test_utils::WaitForAppModalDialog();
839 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
841 // Accept the navigation so we end up on a page without a beforeunload hook.
842 alert
->native_dialog()->AcceptAppModalDialog();
845 // BeforeUnloadAtQuitWithTwoWindows is a regression test for
846 // http://crbug.com/11842. It opens two windows, one of which has a
847 // beforeunload handler and attempts to exit cleanly.
848 class BeforeUnloadAtQuitWithTwoWindows
: public InProcessBrowserTest
{
850 // This test is for testing a specific shutdown behavior. This mimics what
851 // happens in InProcessBrowserTest::RunTestOnMainThread and QuitBrowsers, but
852 // ensures that it happens through the single IDC_EXIT of the test.
853 virtual void CleanUpOnMainThread() OVERRIDE
{
854 // Cycle both the MessageLoop and the Cocoa runloop twice to flush out any
855 // Chrome work that generates Cocoa work. Do this twice since there are two
856 // Browsers that must be closed.
860 // Run the application event loop to completion, which will cycle the
861 // native MessagePump on all platforms.
862 base::MessageLoop::current()->PostTask(FROM_HERE
,
863 base::MessageLoop::QuitClosure());
864 base::MessageLoop::current()->Run();
866 // Take care of any remaining Cocoa work.
869 // At this point, quit should be for real now.
870 ASSERT_EQ(0u, chrome::GetTotalBrowserCount());
873 // A helper function that cycles the MessageLoop, and on Mac, the Cocoa run
874 // loop. It also drains the NSAutoreleasePool.
875 void CycleRunLoops() {
876 content::RunAllPendingInMessageLoop();
877 #if defined(OS_MACOSX)
878 chrome::testing::NSRunLoopRunAllPending();
879 AutoreleasePool()->Recycle();
884 // Disabled, http://crbug.com/159214 .
885 IN_PROC_BROWSER_TEST_F(BeforeUnloadAtQuitWithTwoWindows
,
886 DISABLED_IfThisTestTimesOutItIndicatesFAILURE
) {
887 // In the first browser, set up a page that has a beforeunload handler.
888 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
889 ui_test_utils::NavigateToURL(browser(), url
);
891 // Open a second browser window at about:blank.
892 ui_test_utils::BrowserAddedObserver browser_added_observer
;
893 chrome::NewEmptyWindow(browser()->profile(), chrome::GetActiveDesktop());
894 Browser
* second_window
= browser_added_observer
.WaitForSingleNewBrowser();
895 ui_test_utils::NavigateToURL(second_window
, GURL("about:blank"));
897 // Tell the application to quit. IDC_EXIT calls AttemptUserExit, which on
898 // everything but ChromeOS allows unload handlers to block exit. On that
899 // platform, though, it exits unconditionally. See the comment and bug ID
900 // in AttemptUserExit() in application_lifetime.cc.
901 #if defined(OS_CHROMEOS)
902 chrome::AttemptExit();
904 chrome::ExecuteCommand(second_window
, IDC_EXIT
);
907 // The beforeunload handler will run at exit, ensure it does, and then accept
908 // it to allow shutdown to proceed.
909 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
912 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
913 alert
->native_dialog()->AcceptAppModalDialog();
915 // But wait there's more! If this test times out, it likely means that the
916 // browser has not been able to quit correctly, indicating there's a
917 // regression of the bug noted above.
920 // Test that scripts can fork a new renderer process for a cross-site popup,
921 // based on http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab.
922 // The script must open a new tab, set its window.opener to null, and navigate
923 // it to a cross-site URL. It should also work for meta-refreshes.
924 // See http://crbug.com/93517.
925 IN_PROC_BROWSER_TEST_F(BrowserTest
, NullOpenerRedirectForksProcess
) {
926 CommandLine::ForCurrentProcess()->AppendSwitch(
927 switches::kDisablePopupBlocking
);
929 // Create http and https servers for a cross-site transition.
930 ASSERT_TRUE(test_server()->Start());
931 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
932 net::SpawnedTestServer::kLocalhost
,
933 base::FilePath(kDocRoot
));
934 ASSERT_TRUE(https_test_server
.Start());
935 GURL
http_url(test_server()->GetURL("files/title1.html"));
936 GURL
https_url(https_test_server
.GetURL(std::string()));
938 // Start with an http URL.
939 ui_test_utils::NavigateToURL(browser(), http_url
);
940 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
941 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
943 // Now open a tab to a blank page, set its opener to null, and redirect it
945 std::string redirect_popup
= "w=window.open();";
946 redirect_popup
+= "w.opener=null;";
947 redirect_popup
+= "w.document.location=\"";
948 redirect_popup
+= https_url
.spec();
949 redirect_popup
+= "\";";
951 content::WindowedNotificationObserver
popup_observer(
952 chrome::NOTIFICATION_TAB_ADDED
,
953 content::NotificationService::AllSources());
954 content::WindowedNotificationObserver
nav_observer(
955 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
956 content::NotificationService::AllSources());
957 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(redirect_popup
));
959 // Wait for popup window to appear and finish navigating.
960 popup_observer
.Wait();
961 ASSERT_EQ(2, browser()->tab_strip_model()->count());
962 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
964 EXPECT_NE(oldtab
, newtab
);
966 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
967 EXPECT_EQ(https_url
.spec(),
968 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
970 // Popup window should not be in the opener's process.
971 content::RenderProcessHost
* popup_process
=
972 newtab
->GetRenderProcessHost();
973 EXPECT_NE(process
, popup_process
);
975 // Now open a tab to a blank page, set its opener to null, and use a
976 // meta-refresh to navigate it instead.
977 std::string refresh_popup
= "w=window.open();";
978 refresh_popup
+= "w.opener=null;";
979 refresh_popup
+= "w.document.write(";
980 refresh_popup
+= "'<META HTTP-EQUIV=\"refresh\" content=\"0; url=";
981 refresh_popup
+= https_url
.spec();
982 refresh_popup
+= "\">');w.document.close();";
984 content::WindowedNotificationObserver
popup_observer2(
985 chrome::NOTIFICATION_TAB_ADDED
,
986 content::NotificationService::AllSources());
987 content::WindowedNotificationObserver
nav_observer2(
988 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
989 content::NotificationService::AllSources());
990 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(refresh_popup
));
992 // Wait for popup window to appear and finish navigating.
993 popup_observer2
.Wait();
994 ASSERT_EQ(3, browser()->tab_strip_model()->count());
995 WebContents
* newtab2
= browser()->tab_strip_model()->GetActiveWebContents();
996 EXPECT_TRUE(newtab2
);
997 EXPECT_NE(oldtab
, newtab2
);
998 nav_observer2
.Wait();
999 ASSERT_TRUE(newtab2
->GetController().GetLastCommittedEntry());
1000 EXPECT_EQ(https_url
.spec(),
1001 newtab2
->GetController().GetLastCommittedEntry()->GetURL().spec());
1003 // This popup window should also not be in the opener's process.
1004 content::RenderProcessHost
* popup_process2
=
1005 newtab2
->GetRenderProcessHost();
1006 EXPECT_NE(process
, popup_process2
);
1009 // Tests that other popup navigations that do not follow the steps at
1010 // http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab will not
1011 // fork a new renderer process.
1012 IN_PROC_BROWSER_TEST_F(BrowserTest
, OtherRedirectsDontForkProcess
) {
1013 CommandLine::ForCurrentProcess()->AppendSwitch(
1014 switches::kDisablePopupBlocking
);
1016 // Create http and https servers for a cross-site transition.
1017 ASSERT_TRUE(test_server()->Start());
1018 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1019 net::SpawnedTestServer::kLocalhost
,
1020 base::FilePath(kDocRoot
));
1021 ASSERT_TRUE(https_test_server
.Start());
1022 GURL
http_url(test_server()->GetURL("files/title1.html"));
1023 GURL
https_url(https_test_server
.GetURL(std::string()));
1025 // Start with an http URL.
1026 ui_test_utils::NavigateToURL(browser(), http_url
);
1027 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
1028 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
1030 // Now open a tab to a blank page, set its opener to null, and redirect it
1032 std::string dont_fork_popup
= "w=window.open();";
1033 dont_fork_popup
+= "w.document.location=\"";
1034 dont_fork_popup
+= https_url
.spec();
1035 dont_fork_popup
+= "\";";
1037 content::WindowedNotificationObserver
popup_observer(
1038 chrome::NOTIFICATION_TAB_ADDED
,
1039 content::NotificationService::AllSources());
1040 content::WindowedNotificationObserver
nav_observer(
1041 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1042 content::NotificationService::AllSources());
1043 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(dont_fork_popup
));
1045 // Wait for popup window to appear and finish navigating.
1046 popup_observer
.Wait();
1047 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1048 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
1049 EXPECT_TRUE(newtab
);
1050 EXPECT_NE(oldtab
, newtab
);
1051 nav_observer
.Wait();
1052 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
1053 EXPECT_EQ(https_url
.spec(),
1054 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1056 // Popup window should still be in the opener's process.
1057 content::RenderProcessHost
* popup_process
=
1058 newtab
->GetRenderProcessHost();
1059 EXPECT_EQ(process
, popup_process
);
1061 // Same thing if the current tab tries to navigate itself.
1062 std::string navigate_str
= "document.location=\"";
1063 navigate_str
+= https_url
.spec();
1064 navigate_str
+= "\";";
1066 content::WindowedNotificationObserver
nav_observer2(
1067 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1068 content::NotificationService::AllSources());
1069 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(navigate_str
));
1070 nav_observer2
.Wait();
1071 ASSERT_TRUE(oldtab
->GetController().GetLastCommittedEntry());
1072 EXPECT_EQ(https_url
.spec(),
1073 oldtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1075 // Original window should still be in the original process.
1076 content::RenderProcessHost
* new_process
= newtab
->GetRenderProcessHost();
1077 EXPECT_EQ(process
, new_process
);
1080 // Test that get_process_idle_time() returns reasonable values when compared
1081 // with time deltas measured locally.
1082 IN_PROC_BROWSER_TEST_F(BrowserTest
, RenderIdleTime
) {
1083 base::TimeTicks start
= base::TimeTicks::Now();
1084 ui_test_utils::NavigateToURL(
1085 browser(), ui_test_utils::GetTestUrl(
1086 base::FilePath(base::FilePath::kCurrentDirectory
),
1087 base::FilePath(kTitle1File
)));
1088 content::RenderProcessHost::iterator
it(
1089 content::RenderProcessHost::AllHostsIterator());
1090 for (; !it
.IsAtEnd(); it
.Advance()) {
1091 base::TimeDelta renderer_td
=
1092 it
.GetCurrentValue()->GetChildProcessIdleTime();
1093 base::TimeDelta browser_td
= base::TimeTicks::Now() - start
;
1094 EXPECT_TRUE(browser_td
>= renderer_td
);
1098 // Test IDC_CREATE_SHORTCUTS command is enabled for url scheme file, ftp, http
1099 // and https and disabled for chrome://, about:// etc.
1100 // TODO(pinkerton): Disable app-mode in the model until we implement it
1101 // on the Mac. http://crbug.com/13148
1102 #if !defined(OS_MACOSX)
1103 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFile
) {
1104 CommandUpdater
* command_updater
=
1105 browser()->command_controller()->command_updater();
1107 static const base::FilePath::CharType
* kEmptyFile
=
1108 FILE_PATH_LITERAL("empty.html");
1109 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1110 base::FilePath::kCurrentDirectory
), base::FilePath(kEmptyFile
)));
1111 ASSERT_TRUE(file_url
.SchemeIs(content::kFileScheme
));
1112 ui_test_utils::NavigateToURL(browser(), file_url
);
1113 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1116 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttp
) {
1117 CommandUpdater
* command_updater
=
1118 browser()->command_controller()->command_updater();
1120 ASSERT_TRUE(test_server()->Start());
1121 GURL
http_url(test_server()->GetURL(std::string()));
1122 ASSERT_TRUE(http_url
.SchemeIs(content::kHttpScheme
));
1123 ui_test_utils::NavigateToURL(browser(), http_url
);
1124 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1127 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttps
) {
1128 CommandUpdater
* command_updater
=
1129 browser()->command_controller()->command_updater();
1131 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1132 net::SpawnedTestServer::kLocalhost
,
1133 base::FilePath(kDocRoot
));
1134 ASSERT_TRUE(test_server
.Start());
1135 GURL
https_url(test_server
.GetURL("/"));
1136 ASSERT_TRUE(https_url
.SchemeIs(content::kHttpsScheme
));
1137 ui_test_utils::NavigateToURL(browser(), https_url
);
1138 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1141 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFtp
) {
1142 CommandUpdater
* command_updater
=
1143 browser()->command_controller()->command_updater();
1145 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_FTP
,
1146 net::SpawnedTestServer::kLocalhost
,
1147 base::FilePath(kDocRoot
));
1148 ASSERT_TRUE(test_server
.Start());
1149 GURL
ftp_url(test_server
.GetURL(std::string()));
1150 ASSERT_TRUE(ftp_url
.SchemeIs(content::kFtpScheme
));
1151 ui_test_utils::NavigateToURL(browser(), ftp_url
);
1152 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1155 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutInvalid
) {
1156 CommandUpdater
* command_updater
=
1157 browser()->command_controller()->command_updater();
1159 // Urls that should not have shortcuts.
1160 GURL
new_tab_url(chrome::kChromeUINewTabURL
);
1161 ui_test_utils::NavigateToURL(browser(), new_tab_url
);
1162 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1164 GURL
history_url(chrome::kChromeUIHistoryURL
);
1165 ui_test_utils::NavigateToURL(browser(), history_url
);
1166 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1168 GURL
downloads_url(chrome::kChromeUIDownloadsURL
);
1169 ui_test_utils::NavigateToURL(browser(), downloads_url
);
1170 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1172 GURL
blank_url(content::kAboutBlankURL
);
1173 ui_test_utils::NavigateToURL(browser(), blank_url
);
1174 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1177 // Change a tab into an application window.
1178 // DISABLED: http://crbug.com/72310
1179 IN_PROC_BROWSER_TEST_F(BrowserTest
, DISABLED_ConvertTabToAppShortcut
) {
1180 ASSERT_TRUE(test_server()->Start());
1181 GURL
http_url(test_server()->GetURL(std::string()));
1182 ASSERT_TRUE(http_url
.SchemeIs(content::kHttpScheme
));
1184 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1185 WebContents
* initial_tab
= browser()->tab_strip_model()->GetWebContentsAt(0);
1186 WebContents
* app_tab
= chrome::AddSelectedTabWithURL(
1187 browser(), http_url
, content::PAGE_TRANSITION_TYPED
);
1188 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1189 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
1190 browser()->host_desktop_type()));
1192 // Normal tabs should accept load drops.
1193 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1194 EXPECT_TRUE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1196 // Turn |app_tab| into a tab in an app panel.
1197 chrome::ConvertTabToAppWindow(browser(), app_tab
);
1199 // The launch should have created a new browser.
1200 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1201 browser()->host_desktop_type()));
1203 // Find the new browser.
1204 Browser
* app_browser
= NULL
;
1205 for (chrome::BrowserIterator it
; !it
.done() && !app_browser
; it
.Next()) {
1206 if (*it
!= browser())
1209 ASSERT_TRUE(app_browser
);
1211 // Check that the tab contents is in the new browser, and not in the old.
1212 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1213 ASSERT_EQ(initial_tab
, browser()->tab_strip_model()->GetWebContentsAt(0));
1215 // Check that the appliaction browser has a single tab, and that tab contains
1216 // the content that we app-ified.
1217 ASSERT_EQ(1, app_browser
->tab_strip_model()->count());
1218 ASSERT_EQ(app_tab
, app_browser
->tab_strip_model()->GetWebContentsAt(0));
1220 // Normal tabs should accept load drops.
1221 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1223 // The tab in an app window should not.
1224 EXPECT_FALSE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1227 #endif // !defined(OS_MACOSX)
1229 // Test RenderView correctly send back favicon url for web page that redirects
1230 // to an anchor in javascript body.onload handler.
1231 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1232 DISABLED_FaviconOfOnloadRedirectToAnchorPage
) {
1233 ASSERT_TRUE(test_server()->Start());
1234 GURL
url(test_server()->GetURL("files/onload_redirect_to_anchor.html"));
1235 GURL
expected_favicon_url(test_server()->GetURL("files/test.png"));
1237 ui_test_utils::NavigateToURL(browser(), url
);
1239 NavigationEntry
* entry
= browser()->tab_strip_model()->
1240 GetActiveWebContents()->GetController().GetActiveEntry();
1241 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1244 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined (OS_WIN)
1245 // http://crbug.com/83828. On Mac 10.6, the failure rate is 14%
1246 #define MAYBE_FaviconChange DISABLED_FaviconChange
1248 #define MAYBE_FaviconChange FaviconChange
1250 // Test that an icon can be changed from JS.
1251 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_FaviconChange
) {
1252 static const base::FilePath::CharType
* kFile
=
1253 FILE_PATH_LITERAL("onload_change_favicon.html");
1254 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1255 base::FilePath::kCurrentDirectory
), base::FilePath(kFile
)));
1256 ASSERT_TRUE(file_url
.SchemeIs(content::kFileScheme
));
1257 ui_test_utils::NavigateToURL(browser(), file_url
);
1259 NavigationEntry
* entry
= browser()->tab_strip_model()->
1260 GetActiveWebContents()->GetController().GetActiveEntry();
1261 static const base::FilePath::CharType
* kIcon
=
1262 FILE_PATH_LITERAL("test1.png");
1263 GURL
expected_favicon_url(ui_test_utils::GetTestUrl(base::FilePath(
1264 base::FilePath::kCurrentDirectory
), base::FilePath(kIcon
)));
1265 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1268 // http://crbug.com/172336
1270 #define MAYBE_TabClosingWhenRemovingExtension \
1271 DISABLED_TabClosingWhenRemovingExtension
1273 #define MAYBE_TabClosingWhenRemovingExtension TabClosingWhenRemovingExtension
1275 // Makes sure TabClosing is sent when uninstalling an extension that is an app
1277 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_TabClosingWhenRemovingExtension
) {
1278 ASSERT_TRUE(test_server()->Start());
1279 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1280 GURL
url(test_server()->GetURL("empty.html"));
1281 TabStripModel
* model
= browser()->tab_strip_model();
1283 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1285 const Extension
* extension_app
= GetExtension();
1287 ui_test_utils::NavigateToURL(browser(), url
);
1289 WebContents
* app_contents
= WebContents::Create(
1290 WebContents::CreateParams(browser()->profile()));
1291 extensions::TabHelper::CreateForWebContents(app_contents
);
1292 extensions::TabHelper
* extensions_tab_helper
=
1293 extensions::TabHelper::FromWebContents(app_contents
);
1294 extensions_tab_helper
->SetExtensionApp(extension_app
);
1296 model
->AddWebContents(app_contents
, 0, content::PageTransitionFromInt(0),
1297 TabStripModel::ADD_NONE
);
1298 model
->SetTabPinned(0, true);
1299 ui_test_utils::NavigateToURL(browser(), url
);
1301 MockTabStripModelObserver observer
;
1302 model
->AddObserver(&observer
);
1304 // Uninstall the extension and make sure TabClosing is sent.
1305 ExtensionService
* service
= extensions::ExtensionSystem::Get(
1306 browser()->profile())->extension_service();
1307 service
->UninstallExtension(GetExtension()->id(), false, NULL
);
1308 EXPECT_EQ(1, observer
.closing_count());
1310 model
->RemoveObserver(&observer
);
1312 // There should only be one tab now.
1313 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1316 #if !defined(OS_MACOSX)
1317 // Open with --app-id=<id>, and see that an app window opens.
1318 IN_PROC_BROWSER_TEST_F(BrowserTest
, AppIdSwitch
) {
1319 ASSERT_TRUE(test_server()->Start());
1322 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1323 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1324 const Extension
* extension_app
= GetExtension();
1326 CommandLine
command_line(CommandLine::NO_PROGRAM
);
1327 command_line
.AppendSwitchASCII(switches::kAppId
, extension_app
->id());
1329 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1330 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1331 StartupBrowserCreatorImpl
launch(base::FilePath(), command_line
, first_run
);
1332 ASSERT_TRUE(launch
.OpenApplicationWindow(browser()->profile(), NULL
));
1334 // Check that the new browser has an app name.
1335 // The launch should have created a new browser.
1336 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1337 browser()->host_desktop_type()));
1339 // Find the new browser.
1340 Browser
* new_browser
= NULL
;
1341 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1342 if (*it
!= browser())
1345 ASSERT_TRUE(new_browser
);
1346 ASSERT_TRUE(new_browser
!= browser());
1348 // The browser's app_name should include the app's ID.
1350 new_browser
->app_name_
.find(extension_app
->id()),
1351 std::string::npos
) << new_browser
->app_name_
;
1354 // Open an app window and the dev tools window and ensure that the location
1355 // bar settings are correct.
1356 IN_PROC_BROWSER_TEST_F(BrowserTest
, ShouldShowLocationBar
) {
1357 ASSERT_TRUE(test_server()->Start());
1360 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1361 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1362 const Extension
* extension_app
= GetExtension();
1364 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1365 WebContents
* app_window
=
1366 OpenApplication(AppLaunchParams(browser()->profile(),
1368 extensions::LAUNCH_CONTAINER_WINDOW
,
1370 ASSERT_TRUE(app_window
);
1372 DevToolsWindow::OpenDevToolsWindowForTest(
1373 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(),
1376 // The launch should have created a new app browser and a dev tools browser.
1378 chrome::GetBrowserCount(browser()->profile(),
1379 browser()->host_desktop_type()));
1381 // Find the new browsers.
1382 Browser
* app_browser
= NULL
;
1383 Browser
* dev_tools_browser
= NULL
;
1384 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
1385 if (*it
== browser()) {
1387 } else if ((*it
)->app_name() == DevToolsWindow::kDevToolsApp
) {
1388 dev_tools_browser
= *it
;
1393 ASSERT_TRUE(dev_tools_browser
);
1394 ASSERT_TRUE(app_browser
);
1395 ASSERT_TRUE(app_browser
!= browser());
1398 dev_tools_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1400 app_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1404 // Tests that the CLD (Compact Language Detection) works properly.
1405 IN_PROC_BROWSER_TEST_F(BrowserTest
, PageLanguageDetection
) {
1406 ASSERT_TRUE(test_server()->Start());
1409 LanguageDetectionDetails details
;
1411 // Open a new tab with a page in English.
1412 AddTabAtIndex(0, GURL(test_server()->GetURL("files/english_page.html")),
1413 content::PAGE_TRANSITION_TYPED
);
1415 WebContents
* current_web_contents
=
1416 browser()->tab_strip_model()->GetActiveWebContents();
1417 TranslateTabHelper
* translate_tab_helper
=
1418 TranslateTabHelper::FromWebContents(current_web_contents
);
1419 content::Source
<WebContents
> source(current_web_contents
);
1421 ui_test_utils::WindowedNotificationObserverWithDetails
<
1422 LanguageDetectionDetails
>
1423 en_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1425 EXPECT_EQ("", translate_tab_helper
->GetLanguageState().original_language());
1426 en_language_detected_signal
.Wait();
1427 EXPECT_TRUE(en_language_detected_signal
.GetDetailsFor(
1428 source
.map_key(), &details
));
1429 EXPECT_EQ("en", details
.adopted_language
);
1430 EXPECT_EQ("en", translate_tab_helper
->GetLanguageState().original_language());
1432 // Now navigate to a page in French.
1433 ui_test_utils::WindowedNotificationObserverWithDetails
<
1434 LanguageDetectionDetails
>
1435 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1437 ui_test_utils::NavigateToURL(
1438 browser(), GURL(test_server()->GetURL("files/french_page.html")));
1439 fr_language_detected_signal
.Wait();
1440 details
.adopted_language
.clear();
1441 EXPECT_TRUE(fr_language_detected_signal
.GetDetailsFor(
1442 source
.map_key(), &details
));
1443 EXPECT_EQ("fr", details
.adopted_language
);
1444 EXPECT_EQ("fr", translate_tab_helper
->GetLanguageState().original_language());
1447 // Chromeos defaults to restoring the last session, so this test isn't
1449 #if !defined(OS_CHROMEOS)
1450 #if defined(OS_MACOSX)
1451 // Crashy, http://crbug.com/38522
1452 #define RestorePinnedTabs DISABLED_RestorePinnedTabs
1454 // Makes sure pinned tabs are restored correctly on start.
1455 IN_PROC_BROWSER_TEST_F(BrowserTest
, RestorePinnedTabs
) {
1456 ASSERT_TRUE(test_server()->Start());
1458 // Add an pinned app tab.
1459 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1460 GURL
url(test_server()->GetURL("empty.html"));
1461 TabStripModel
* model
= browser()->tab_strip_model();
1462 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1463 const Extension
* extension_app
= GetExtension();
1464 ui_test_utils::NavigateToURL(browser(), url
);
1465 WebContents
* app_contents
= WebContents::Create(
1466 WebContents::CreateParams(browser()->profile()));
1467 extensions::TabHelper::CreateForWebContents(app_contents
);
1468 extensions::TabHelper
* extensions_tab_helper
=
1469 extensions::TabHelper::FromWebContents(app_contents
);
1470 extensions_tab_helper
->SetExtensionApp(extension_app
);
1471 model
->AddWebContents(app_contents
, 0, content::PageTransitionFromInt(0),
1472 TabStripModel::ADD_NONE
);
1473 model
->SetTabPinned(0, true);
1474 ui_test_utils::NavigateToURL(browser(), url
);
1476 // Add a non pinned tab.
1477 chrome::NewTab(browser());
1479 // Add a pinned non-app tab.
1480 chrome::NewTab(browser());
1481 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL
));
1482 model
->SetTabPinned(2, true);
1484 // Write out the pinned tabs.
1485 PinnedTabCodec::WritePinnedTabs(browser()->profile());
1487 // Simulate launching again.
1488 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1489 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1490 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1491 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, first_run
);
1492 launch
.profile_
= browser()->profile();
1493 launch
.ProcessStartupURLs(std::vector
<GURL
>(),
1494 browser()->host_desktop_type());
1496 // The launch should have created a new browser.
1497 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1498 browser()->host_desktop_type()));
1500 // Find the new browser.
1501 Browser
* new_browser
= NULL
;
1502 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1503 if (*it
!= browser())
1506 ASSERT_TRUE(new_browser
);
1507 ASSERT_TRUE(new_browser
!= browser());
1509 // We should get back an additional tab for the app, and another for the
1510 // default home page.
1511 ASSERT_EQ(3, new_browser
->tab_strip_model()->count());
1513 // Make sure the state matches.
1514 TabStripModel
* new_model
= new_browser
->tab_strip_model();
1515 EXPECT_TRUE(new_model
->IsAppTab(0));
1516 EXPECT_FALSE(new_model
->IsAppTab(1));
1517 EXPECT_FALSE(new_model
->IsAppTab(2));
1519 EXPECT_TRUE(new_model
->IsTabPinned(0));
1520 EXPECT_TRUE(new_model
->IsTabPinned(1));
1521 EXPECT_FALSE(new_model
->IsTabPinned(2));
1523 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
1524 new_model
->GetWebContentsAt(2)->GetURL());
1527 extensions::TabHelper::FromWebContents(
1528 new_model
->GetWebContentsAt(0))->extension_app() == extension_app
);
1530 #endif // !defined(OS_CHROMEOS)
1532 // This test verifies we don't crash when closing the last window and the app
1534 // TODO(linux_aura) http://crbug.com/163931
1535 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
1536 #define MAYBE_CloseWithAppMenuOpen DISABLED_CloseWithAppMenuOpen
1538 #define MAYBE_CloseWithAppMenuOpen CloseWithAppMenuOpen
1540 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_CloseWithAppMenuOpen
) {
1541 if (browser_defaults::kBrowserAliveWithNoWindows
)
1544 // We need a message loop running for menus on windows.
1545 base::MessageLoop::current()->PostTask(
1546 FROM_HERE
, base::Bind(&RunCloseWithAppMenuCallback
, browser()));
1549 #if !defined(OS_MACOSX)
1550 IN_PROC_BROWSER_TEST_F(BrowserTest
, OpenAppWindowLikeNtp
) {
1551 ASSERT_TRUE(test_server()->Start());
1554 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1555 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1556 const Extension
* extension_app
= GetExtension();
1558 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1559 WebContents
* app_window
= OpenApplication(
1560 AppLaunchParams(browser()->profile(), extension_app
,
1561 extensions::LAUNCH_CONTAINER_WINDOW
, NEW_WINDOW
));
1562 ASSERT_TRUE(app_window
);
1564 // Apps launched in a window from the NTP have an extensions tab helper but
1565 // do not have extension_app set in it.
1566 ASSERT_TRUE(extensions::TabHelper::FromWebContents(app_window
));
1568 extensions::TabHelper::FromWebContents(app_window
)->extension_app());
1569 EXPECT_EQ(extensions::AppLaunchInfo::GetFullLaunchURL(extension_app
),
1570 app_window
->GetURL());
1572 // The launch should have created a new browser.
1573 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1574 browser()->host_desktop_type()));
1576 // Find the new browser.
1577 Browser
* new_browser
= NULL
;
1578 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1579 if (*it
!= browser())
1582 ASSERT_TRUE(new_browser
);
1583 ASSERT_TRUE(new_browser
!= browser());
1585 EXPECT_TRUE(new_browser
->is_app());
1587 // The browser's app name should include the extension's id.
1588 std::string app_name
= new_browser
->app_name_
;
1589 EXPECT_NE(app_name
.find(extension_app
->id()), std::string::npos
)
1590 << "Name " << app_name
<< " should contain id "<< extension_app
->id();
1592 #endif // !defined(OS_MACOSX)
1594 // Makes sure the browser doesn't crash when
1595 // set_show_state(ui::SHOW_STATE_MAXIMIZED) has been invoked.
1596 IN_PROC_BROWSER_TEST_F(BrowserTest
, StartMaximized
) {
1597 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1598 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(types
); ++i
) {
1599 Browser::CreateParams
params(types
[i
], browser()->profile(),
1600 browser()->host_desktop_type());
1601 params
.initial_show_state
= ui::SHOW_STATE_MAXIMIZED
;
1602 AddBlankTabAndShow(new Browser(params
));
1606 // Aura doesn't support minimized window. crbug.com/104571.
1607 #if defined(USE_AURA)
1608 #define MAYBE_StartMinimized DISABLED_StartMinimized
1610 #define MAYBE_StartMinimized StartMinimized
1612 // Makes sure the browser doesn't crash when
1613 // set_show_state(ui::SHOW_STATE_MINIMIZED) has been invoked.
1614 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_StartMinimized
) {
1615 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1616 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(types
); ++i
) {
1617 Browser::CreateParams
params(types
[i
], browser()->profile(),
1618 browser()->host_desktop_type());
1619 params
.initial_show_state
= ui::SHOW_STATE_MINIMIZED
;
1620 AddBlankTabAndShow(new Browser(params
));
1624 // Makes sure the forward button is disabled immediately when navigating
1625 // forward to a slow-to-commit page.
1626 IN_PROC_BROWSER_TEST_F(BrowserTest
, ForwardDisabledOnForward
) {
1627 GURL
blank_url(content::kAboutBlankURL
);
1628 ui_test_utils::NavigateToURL(browser(), blank_url
);
1630 ui_test_utils::NavigateToURL(
1631 browser(), ui_test_utils::GetTestUrl(
1632 base::FilePath(base::FilePath::kCurrentDirectory
),
1633 base::FilePath(kTitle1File
)));
1635 content::WindowedNotificationObserver
back_nav_load_observer(
1636 content::NOTIFICATION_LOAD_STOP
,
1637 content::Source
<NavigationController
>(
1638 &browser()->tab_strip_model()->GetActiveWebContents()->
1640 chrome::GoBack(browser(), CURRENT_TAB
);
1641 back_nav_load_observer
.Wait();
1642 CommandUpdater
* command_updater
=
1643 browser()->command_controller()->command_updater();
1644 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1646 content::WindowedNotificationObserver
forward_nav_load_observer(
1647 content::NOTIFICATION_LOAD_STOP
,
1648 content::Source
<NavigationController
>(
1649 &browser()->tab_strip_model()->GetActiveWebContents()->
1651 chrome::GoForward(browser(), CURRENT_TAB
);
1652 // This check will happen before the navigation completes, since the browser
1653 // won't process the renderer's response until the Wait() call below.
1654 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1655 forward_nav_load_observer
.Wait();
1658 // Makes sure certain commands are disabled when Incognito mode is forced.
1659 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisableMenuItemsWhenIncognitoIsForced
) {
1660 CommandUpdater
* command_updater
=
1661 browser()->command_controller()->command_updater();
1662 // At the beginning, all commands are enabled.
1663 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1664 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1665 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1666 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1667 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1668 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1670 // Set Incognito to FORCED.
1671 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1672 IncognitoModePrefs::FORCED
);
1673 // Bookmarks & Settings commands should get disabled.
1674 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1675 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1676 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1677 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1678 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1679 // New Incognito Window command, however, should be enabled.
1680 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1682 // Create a new browser.
1683 Browser
* new_browser
=
1684 new Browser(Browser::CreateParams(
1685 browser()->profile()->GetOffTheRecordProfile(),
1686 browser()->host_desktop_type()));
1687 CommandUpdater
* new_command_updater
=
1688 new_browser
->command_controller()->command_updater();
1689 // It should have Bookmarks & Settings commands disabled by default.
1690 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1691 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(
1692 IDC_SHOW_BOOKMARK_MANAGER
));
1693 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1694 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1695 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1696 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1699 // Makes sure New Incognito Window command is disabled when Incognito mode is
1701 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1702 NoNewIncognitoWindowWhenIncognitoIsDisabled
) {
1703 CommandUpdater
* command_updater
=
1704 browser()->command_controller()->command_updater();
1705 // Set Incognito to DISABLED.
1706 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1707 IncognitoModePrefs::DISABLED
);
1708 // Make sure New Incognito Window command is disabled. All remaining commands
1709 // should be enabled.
1710 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1711 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1712 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1713 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1714 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1715 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1717 // Create a new browser.
1718 Browser
* new_browser
=
1719 new Browser(Browser::CreateParams(browser()->profile(),
1720 browser()->host_desktop_type()));
1721 CommandUpdater
* new_command_updater
=
1722 new_browser
->command_controller()->command_updater();
1723 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1724 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1725 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1726 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1727 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1728 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1731 // Makes sure Extensions and Settings commands are disabled in certain
1732 // circumstances even though normally they should stay enabled.
1733 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1734 DisableExtensionsAndSettingsWhenIncognitoIsDisabled
) {
1735 CommandUpdater
* command_updater
=
1736 browser()->command_controller()->command_updater();
1737 // Disable extensions. This should disable Extensions menu.
1738 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
1739 set_extensions_enabled(false);
1740 // Set Incognito to DISABLED.
1741 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1742 IncognitoModePrefs::DISABLED
);
1743 // Make sure Manage Extensions command is disabled.
1744 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1745 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1746 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1747 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1748 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1750 // Create a popup (non-main-UI-type) browser. Settings command as well
1751 // as Extensions should be disabled.
1752 Browser
* popup_browser
= new Browser(
1753 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1754 browser()->host_desktop_type()));
1755 CommandUpdater
* popup_command_updater
=
1756 popup_browser
->command_controller()->command_updater();
1757 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1758 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1759 EXPECT_TRUE(popup_command_updater
->IsCommandEnabled(
1760 IDC_SHOW_BOOKMARK_MANAGER
));
1761 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1764 // Makes sure Extensions and Settings commands are disabled in certain
1765 // circumstances even though normally they should stay enabled.
1766 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1767 DisableOptionsAndImportMenuItemsConsistently
) {
1768 // Create a popup browser.
1769 Browser
* popup_browser
= new Browser(
1770 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1771 browser()->host_desktop_type()));
1772 CommandUpdater
* command_updater
=
1773 popup_browser
->command_controller()->command_updater();
1774 // OPTIONS and IMPORT_SETTINGS are disabled for a non-normal UI.
1775 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1776 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1778 // Set Incognito to FORCED.
1779 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1780 IncognitoModePrefs::FORCED
);
1781 // OPTIONS and IMPORT_SETTINGS are disabled when Incognito is forced.
1782 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1783 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1784 // Set Incognito to AVAILABLE.
1785 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1786 IncognitoModePrefs::ENABLED
);
1787 // OPTIONS and IMPORT_SETTINGS are still disabled since it is a non-normal UI.
1788 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1789 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1794 void OnZoomLevelChanged(const base::Closure
& callback
,
1795 const HostZoomMap::ZoomLevelChange
& host
) {
1802 // Flakes regularly on Windows XP
1803 // http://crbug.com/146040
1804 #define MAYBE_PageZoom DISABLED_PageZoom
1806 #define MAYBE_PageZoom PageZoom
1808 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_PageZoom
) {
1809 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1810 bool enable_plus
, enable_minus
;
1813 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1814 new content::MessageLoopRunner
);
1815 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1816 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1817 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1818 content::HostZoomMap::GetForBrowserContext(
1819 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1820 chrome::Zoom(browser(), content::PAGE_ZOOM_IN
);
1823 EXPECT_EQ(contents
->GetZoomPercent(&enable_plus
, &enable_minus
), 110);
1824 EXPECT_TRUE(enable_plus
);
1825 EXPECT_TRUE(enable_minus
);
1829 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1830 new content::MessageLoopRunner
);
1831 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1832 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1833 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1834 content::HostZoomMap::GetForBrowserContext(
1835 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1836 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1839 EXPECT_EQ(contents
->GetZoomPercent(&enable_plus
, &enable_minus
), 100);
1840 EXPECT_TRUE(enable_plus
);
1841 EXPECT_TRUE(enable_minus
);
1845 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1846 new content::MessageLoopRunner
);
1847 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1848 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1849 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1850 content::HostZoomMap::GetForBrowserContext(
1851 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1852 chrome::Zoom(browser(), content::PAGE_ZOOM_OUT
);
1855 EXPECT_EQ(contents
->GetZoomPercent(&enable_plus
, &enable_minus
), 90);
1856 EXPECT_TRUE(enable_plus
);
1857 EXPECT_TRUE(enable_minus
);
1860 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1863 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCommandDisable
) {
1864 ASSERT_TRUE(test_server()->Start());
1865 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1866 GURL
url(test_server()->GetURL("empty.html"));
1867 ui_test_utils::NavigateToURL(browser(), url
);
1869 CommandUpdater
* command_updater
=
1870 browser()->command_controller()->command_updater();
1871 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1872 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1873 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1874 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1876 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1878 TestInterstitialPage
* interstitial
= NULL
;
1880 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1881 new content::MessageLoopRunner
);
1883 InterstitialObserver
observer(contents
,
1884 loop_runner
->QuitClosure(),
1886 interstitial
= new TestInterstitialPage(contents
, false, GURL());
1890 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1892 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1893 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1894 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1895 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1898 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1899 new content::MessageLoopRunner
);
1901 InterstitialObserver
observer(contents
,
1903 loop_runner
->QuitClosure());
1904 interstitial
->Proceed();
1906 // interstitial is deleted now.
1909 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1910 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1911 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1912 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1915 // Ensure that creating an interstitial page closes any JavaScript dialogs
1916 // that were present on the previous page. See http://crbug.com/295695.
1917 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialClosesDialogs
) {
1918 ASSERT_TRUE(test_server()->Start());
1919 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1920 GURL
url(test_server()->GetURL("empty.html"));
1921 ui_test_utils::NavigateToURL(browser(), url
);
1923 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1924 contents
->GetMainFrame()->ExecuteJavaScript(
1925 ASCIIToUTF16("alert('Dialog showing!');"));
1926 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
1927 EXPECT_TRUE(alert
->IsValid());
1928 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
1929 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
1931 TestInterstitialPage
* interstitial
= NULL
;
1933 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1934 new content::MessageLoopRunner
);
1936 InterstitialObserver
observer(contents
,
1937 loop_runner
->QuitClosure(),
1939 interstitial
= new TestInterstitialPage(contents
, false, GURL());
1943 // The interstitial should have closed the dialog.
1944 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1945 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
1948 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1949 new content::MessageLoopRunner
);
1951 InterstitialObserver
observer(contents
,
1953 loop_runner
->QuitClosure());
1954 interstitial
->DontProceed();
1956 // interstitial is deleted now.
1959 // Make sure input events still work in the renderer process.
1960 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
1964 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCloseTab
) {
1965 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1968 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1969 new content::MessageLoopRunner
);
1971 InterstitialObserver
observer(contents
,
1972 loop_runner
->QuitClosure(),
1974 // Interstitial will delete itself when we close the tab.
1975 new TestInterstitialPage(contents
, false, GURL());
1979 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1982 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1983 new content::MessageLoopRunner
);
1985 InterstitialObserver
observer(contents
,
1987 loop_runner
->QuitClosure());
1988 chrome::CloseTab(browser());
1990 // interstitial is deleted now.
1994 class MockWebContentsObserver
: public WebContentsObserver
{
1996 explicit MockWebContentsObserver(WebContents
* web_contents
)
1997 : WebContentsObserver(web_contents
),
1998 got_user_gesture_(false) {
2001 virtual void DidGetUserGesture() OVERRIDE
{
2002 got_user_gesture_
= true;
2005 bool got_user_gesture() const {
2006 return got_user_gesture_
;
2009 void set_got_user_gesture(bool got_it
) {
2010 got_user_gesture_
= got_it
;
2014 bool got_user_gesture_
;
2016 DISALLOW_COPY_AND_ASSIGN(MockWebContentsObserver
);
2019 IN_PROC_BROWSER_TEST_F(BrowserTest
, UserGesturesReported
) {
2020 // Regression test for http://crbug.com/110707. Also tests that a user
2021 // gesture is sent when a normal navigation (via e.g. the omnibox) is
2023 WebContents
* web_contents
=
2024 browser()->tab_strip_model()->GetActiveWebContents();
2025 MockWebContentsObserver
mock_observer(web_contents
);
2027 ASSERT_TRUE(test_server()->Start());
2028 GURL
url(test_server()->GetURL("empty.html"));
2030 ui_test_utils::NavigateToURL(browser(), url
);
2031 EXPECT_TRUE(mock_observer
.got_user_gesture());
2033 mock_observer
.set_got_user_gesture(false);
2034 chrome::Reload(browser(), CURRENT_TAB
);
2035 EXPECT_TRUE(mock_observer
.got_user_gesture());
2038 // TODO(ben): this test was never enabled. It has bit-rotted since being added.
2039 // It originally lived in browser_unittest.cc, but has been moved here to make
2040 // room for real browser unit tests.
2042 class BrowserTest2
: public InProcessBrowserTest
{
2045 host_resolver_proc_
= new net::RuleBasedHostResolverProc(NULL
);
2046 // Avoid making external DNS lookups. In this test we don't need this
2048 host_resolver_proc_
->AddSimulatedFailure("*.google.com");
2049 scoped_host_resolver_proc_
.Init(host_resolver_proc_
.get());
2053 scoped_refptr
<net::RuleBasedHostResolverProc
> host_resolver_proc_
;
2054 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_
;
2057 IN_PROC_BROWSER_TEST_F(BrowserTest2
, NoTabsInPopups
) {
2058 chrome::RegisterAppPrefs(L
"Test");
2060 // We start with a normal browser with one tab.
2061 EXPECT_EQ(1, browser()->tab_strip_model()->count());
2063 // Open a popup browser with a single blank foreground tab.
2064 Browser
* popup_browser
= new Browser(
2065 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile()));
2066 chrome::AddTabAt(popup_browser
, GURL(), -1, true);
2067 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2069 // Now try opening another tab in the popup browser.
2070 AddTabWithURLParams
params1(url
, content::PAGE_TRANSITION_TYPED
);
2071 popup_browser
->AddTabWithURL(¶ms1
);
2072 EXPECT_EQ(popup_browser
, params1
.target
);
2074 // The popup should still only have one tab.
2075 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2077 // The normal browser should now have two.
2078 EXPECT_EQ(2, browser()->tab_strip_model()->count());
2080 // Open an app frame browser with a single blank foreground tab.
2081 Browser
* app_browser
= new Browser(Browser::CreateParams::CreateForApp(
2082 L
"Test", browser()->profile(), false));
2083 chrome::AddTabAt(app_browser
, GURL(), -1, true);
2084 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2086 // Now try opening another tab in the app browser.
2087 AddTabWithURLParams
params2(GURL(content::kAboutBlankURL
),
2088 content::PAGE_TRANSITION_TYPED
);
2089 app_browser
->AddTabWithURL(¶ms2
);
2090 EXPECT_EQ(app_browser
, params2
.target
);
2092 // The popup should still only have one tab.
2093 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2095 // The normal browser should now have three.
2096 EXPECT_EQ(3, browser()->tab_strip_model()->count());
2098 // Open an app frame popup browser with a single blank foreground tab.
2099 Browser
* app_popup_browser
= new Browser(Browser::CreateParams::CreateForApp(
2100 L
"Test", browser()->profile(), false));
2101 chrome::AddTabAt(app_popup_browser
, GURL(), -1, true);
2102 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2104 // Now try opening another tab in the app popup browser.
2105 AddTabWithURLParams
params3(GURL(content::kAboutBlankURL
),
2106 content::PAGE_TRANSITION_TYPED
);
2107 app_popup_browser
->AddTabWithURL(¶ms3
);
2108 EXPECT_EQ(app_popup_browser
, params3
.target
);
2110 // The popup should still only have one tab.
2111 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2113 // The normal browser should now have four.
2114 EXPECT_EQ(4, browser()->tab_strip_model()->count());
2116 // Close the additional browsers.
2117 popup_browser
->tab_strip_model()->CloseAllTabs();
2118 app_browser
->tab_strip_model()->CloseAllTabs();
2119 app_popup_browser
->tab_strip_model()->CloseAllTabs();
2123 IN_PROC_BROWSER_TEST_F(BrowserTest
, WindowOpenClose
) {
2124 CommandLine::ForCurrentProcess()->AppendSwitch(
2125 switches::kDisablePopupBlocking
);
2126 GURL url
= ui_test_utils::GetTestUrl(
2127 base::FilePath(), base::FilePath().AppendASCII("window.close.html"));
2129 base::string16 title
= ASCIIToUTF16("Title Of Awesomeness");
2130 content::TitleWatcher
title_watcher(
2131 browser()->tab_strip_model()->GetActiveWebContents(), title
);
2132 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url
, 2);
2133 EXPECT_EQ(title
, title_watcher
.WaitAndGetTitle());
2136 // GTK doesn't use the Browser's fullscreen state.
2137 // TODO(linux_aura) http://crbug.com/163931
2138 // Mac disabled: http://crbug.com/169820
2139 #if !defined(TOOLKIT_GTK) && !defined(OS_MACOSX) && \
2140 !(defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
2141 IN_PROC_BROWSER_TEST_F(BrowserTest
, FullscreenBookmarkBar
) {
2142 #if defined(OS_WIN) && defined(USE_ASH)
2143 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2144 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2148 chrome::ToggleBookmarkBar(browser());
2149 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2150 chrome::ToggleFullscreenMode(browser());
2151 EXPECT_TRUE(browser()->window()->IsFullscreen());
2152 #if defined(OS_MACOSX)
2153 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2154 #elif defined(OS_CHROMEOS)
2155 // TODO(jamescook): If immersive fullscreen is disabled by default, test
2156 // for BookmarkBar::HIDDEN.
2157 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2159 EXPECT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2164 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisallowFileUrlUniversalAccessTest
) {
2165 GURL url
= ui_test_utils::GetTestUrl(
2167 base::FilePath().AppendASCII("fileurl_universalaccess.html"));
2169 base::string16
expected_title(ASCIIToUTF16("Disallowed"));
2170 content::TitleWatcher
title_watcher(
2171 browser()->tab_strip_model()->GetActiveWebContents(), expected_title
);
2172 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("Allowed"));
2173 ui_test_utils::NavigateToURL(browser(), url
);
2174 ASSERT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
2177 class KioskModeTest
: public BrowserTest
{
2181 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2182 command_line
->AppendSwitch(switches::kKioskMode
);
2186 #if defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
2187 // http://crbug.com/103912
2188 // TODO(linux_aura) http://crbug.com/163931
2189 #define MAYBE_EnableKioskModeTest DISABLED_EnableKioskModeTest
2191 #define MAYBE_EnableKioskModeTest EnableKioskModeTest
2193 IN_PROC_BROWSER_TEST_F(KioskModeTest
, MAYBE_EnableKioskModeTest
) {
2194 // Check if browser is in fullscreen mode.
2195 ASSERT_TRUE(browser()->window()->IsFullscreen());
2196 ASSERT_FALSE(browser()->window()->IsFullscreenBubbleVisible());
2200 // This test verifies that Chrome can be launched with a user-data-dir path
2201 // which contains non ASCII characters.
2202 class LaunchBrowserWithNonAsciiUserDatadir
: public BrowserTest
{
2204 LaunchBrowserWithNonAsciiUserDatadir() {}
2206 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2207 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
2208 base::FilePath tmp_profile
= temp_dir_
.path().AppendASCII("tmp_profile");
2209 tmp_profile
= tmp_profile
.Append(L
"Test Chrome G\u00E9raldine");
2211 ASSERT_TRUE(base::CreateDirectory(tmp_profile
));
2212 command_line
->AppendSwitchPath(switches::kUserDataDir
, tmp_profile
);
2215 base::ScopedTempDir temp_dir_
;
2218 IN_PROC_BROWSER_TEST_F(LaunchBrowserWithNonAsciiUserDatadir
,
2219 TestNonAsciiUserDataDir
) {
2220 // Verify that the window is present.
2221 ASSERT_TRUE(browser());
2223 #endif // defined(OS_WIN)
2225 // Tests to ensure that the browser continues running in the background after
2226 // the last window closes.
2227 class RunInBackgroundTest
: public BrowserTest
{
2229 RunInBackgroundTest() {}
2231 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2232 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2236 IN_PROC_BROWSER_TEST_F(RunInBackgroundTest
, RunInBackgroundBasicTest
) {
2237 // Close the browser window, then open a new one - the browser should keep
2239 Profile
* profile
= browser()->profile();
2240 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2241 content::WindowedNotificationObserver
observer(
2242 chrome::NOTIFICATION_BROWSER_CLOSED
,
2243 content::Source
<Browser
>(browser()));
2244 chrome::CloseWindow(browser());
2246 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2248 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2249 chrome::NewEmptyWindow(profile
, chrome::GetActiveDesktop());
2250 browser_added_observer
.WaitForSingleNewBrowser();
2252 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2255 // Tests to ensure that the browser continues running in the background after
2256 // the last window closes.
2257 class NoStartupWindowTest
: public BrowserTest
{
2259 NoStartupWindowTest() {}
2261 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2262 command_line
->AppendSwitch(switches::kNoStartupWindow
);
2263 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2267 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, NoStartupWindowBasicTest
) {
2268 #if defined(OS_WIN) && defined(USE_ASH)
2269 // kNoStartupWindow doesn't make sense in Metro+Ash.
2270 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2274 // No browser window should be started by default.
2275 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2277 // Starting a browser window should work just fine.
2278 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2279 CreateBrowser(ProfileManager::GetActiveUserProfile());
2280 browser_added_observer
.WaitForSingleNewBrowser();
2282 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2285 // Chromeos needs to track app windows because it considers them to be part of
2287 #if !defined(OS_CHROMEOS)
2288 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, DontInitSessionServiceForApps
) {
2289 #if defined(OS_WIN) && defined(USE_ASH)
2290 // kNoStartupWindow doesn't make sense in Metro+Ash.
2291 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2295 Profile
* profile
= ProfileManager::GetActiveUserProfile();
2297 SessionService
* session_service
=
2298 SessionServiceFactory::GetForProfile(profile
);
2299 ASSERT_FALSE(session_service
->processed_any_commands());
2301 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2302 CreateBrowserForApp("blah", profile
);
2303 browser_added_observer
.WaitForSingleNewBrowser();
2305 ASSERT_FALSE(session_service
->processed_any_commands());
2307 #endif // !defined(OS_CHROMEOS)
2309 // This test needs to be placed outside the anonymous namespace because we
2310 // need to access private type of Browser.
2311 class AppModeTest
: public BrowserTest
{
2315 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2316 GURL url
= ui_test_utils::GetTestUrl(
2317 base::FilePath(), base::FilePath().AppendASCII("title1.html"));
2318 command_line
->AppendSwitchASCII(switches::kApp
, url
.spec());
2322 IN_PROC_BROWSER_TEST_F(AppModeTest
, EnableAppModeTest
) {
2323 #if defined(OS_WIN) && defined(USE_ASH)
2324 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2325 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2329 // Test that an application browser window loads correctly.
2331 // Verify the browser is in application mode.
2332 EXPECT_TRUE(browser()->is_app());
2335 // Confirm chrome://version contains some expected content.
2336 IN_PROC_BROWSER_TEST_F(BrowserTest
, AboutVersion
) {
2337 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL
));
2338 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
2339 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("WebKit"), true, true,
2342 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("OS"), true, true,
2345 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("JavaScript"), true,
2350 static const base::FilePath::CharType
* kTestDir
=
2351 FILE_PATH_LITERAL("click_modifier");
2352 static const char kFirstPageTitle
[] = "First window";
2353 static const char kSecondPageTitle
[] = "New window!";
2355 class ClickModifierTest
: public InProcessBrowserTest
{
2357 ClickModifierTest() {
2360 // Returns a url that opens a new window or tab when clicked, via javascript.
2361 GURL
GetWindowOpenURL() {
2362 return ui_test_utils::GetTestUrl(
2363 base::FilePath(kTestDir
),
2364 base::FilePath(FILE_PATH_LITERAL("window_open.html")));
2367 // Returns a url that follows a simple link when clicked, unless affected by
2370 return ui_test_utils::GetTestUrl(
2371 base::FilePath(kTestDir
),
2372 base::FilePath(FILE_PATH_LITERAL("href.html")));
2375 base::string16
getFirstPageTitle() {
2376 return ASCIIToUTF16(kFirstPageTitle
);
2379 base::string16
getSecondPageTitle() {
2380 return ASCIIToUTF16(kSecondPageTitle
);
2383 // Loads our test page and simulates a single click using the supplied button
2384 // and modifiers. The click will cause either a navigation or the creation of
2385 // a new window or foreground or background tab. We verify that the expected
2386 // disposition occurs.
2387 void RunTest(Browser
* browser
,
2390 blink::WebMouseEvent::Button button
,
2391 WindowOpenDisposition disposition
) {
2392 ui_test_utils::NavigateToURL(browser
, url
);
2393 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2394 browser
->host_desktop_type()));
2395 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2396 content::WebContents
* web_contents
=
2397 browser
->tab_strip_model()->GetActiveWebContents();
2398 EXPECT_EQ(url
, web_contents
->GetURL());
2400 if (disposition
== CURRENT_TAB
) {
2401 content::WebContents
* web_contents
=
2402 browser
->tab_strip_model()->GetActiveWebContents();
2403 content::TestNavigationObserver
same_tab_observer(web_contents
);
2404 SimulateMouseClick(web_contents
, modifiers
, button
);
2405 same_tab_observer
.Wait();
2406 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2407 browser
->host_desktop_type()));
2408 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2409 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2413 content::WindowedNotificationObserver
observer(
2414 chrome::NOTIFICATION_TAB_ADDED
,
2415 content::NotificationService::AllSources());
2416 SimulateMouseClick(web_contents
, modifiers
, button
);
2419 if (disposition
== NEW_WINDOW
) {
2420 EXPECT_EQ(2u, chrome::GetBrowserCount(browser
->profile(),
2421 browser
->host_desktop_type()));
2425 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2426 browser
->host_desktop_type()));
2427 EXPECT_EQ(2, browser
->tab_strip_model()->count());
2428 web_contents
= browser
->tab_strip_model()->GetActiveWebContents();
2429 WaitForLoadStop(web_contents
);
2430 if (disposition
== NEW_FOREGROUND_TAB
) {
2431 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2433 ASSERT_EQ(NEW_BACKGROUND_TAB
, disposition
);
2434 EXPECT_EQ(getFirstPageTitle(), web_contents
->GetTitle());
2439 DISALLOW_COPY_AND_ASSIGN(ClickModifierTest
);
2442 // Tests for clicking on elements with handlers that run window.open.
2444 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenBasicClickTest
) {
2446 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2447 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2448 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2451 // TODO(ericu): Alt-click behavior on window.open is platform-dependent and not
2452 // well defined. Should we add tests so we know if it changes?
2454 // Shift-clicks open in a new window.
2455 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftClickTest
) {
2456 int modifiers
= blink::WebInputEvent::ShiftKey
;
2457 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2458 WindowOpenDisposition disposition
= NEW_WINDOW
;
2459 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2462 // Control-clicks open in a background tab.
2463 // On OSX meta [the command key] takes the place of control.
2464 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlClickTest
) {
2465 #if defined(OS_MACOSX)
2466 int modifiers
= blink::WebInputEvent::MetaKey
;
2468 int modifiers
= blink::WebInputEvent::ControlKey
;
2470 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2471 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2472 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2475 // Control-shift-clicks open in a foreground tab.
2476 // On OSX meta [the command key] takes the place of control.
2477 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlShiftClickTest
) {
2478 #if defined(OS_MACOSX)
2479 int modifiers
= blink::WebInputEvent::MetaKey
;
2481 int modifiers
= blink::WebInputEvent::ControlKey
;
2483 modifiers
|= blink::WebInputEvent::ShiftKey
;
2484 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2485 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2486 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2489 // Middle-clicks open in a background tab.
2490 // TODO(linux_aura) http://crbug.com/163931
2491 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
2492 #define MAYBE_WindowOpenMiddleClickTest DISABLED_WindowOpenMiddleClickTest
2494 #define MAYBE_WindowOpenMiddleClickTest WindowOpenMiddleClickTest
2496 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, MAYBE_WindowOpenMiddleClickTest
) {
2498 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2499 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2500 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2503 // Shift-middle-clicks open in a foreground tab.
2504 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftMiddleClickTest
) {
2505 int modifiers
= blink::WebInputEvent::ShiftKey
;
2506 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2507 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2508 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2511 // Tests for clicking on normal links.
2513 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefBasicClickTest
) {
2515 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2516 WindowOpenDisposition disposition
= CURRENT_TAB
;
2517 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2520 // TODO(ericu): Alt-click behavior on links is platform-dependent and not well
2521 // defined. Should we add tests so we know if it changes?
2523 // Shift-clicks open in a new window.
2524 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefShiftClickTest
) {
2525 int modifiers
= blink::WebInputEvent::ShiftKey
;
2526 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2527 WindowOpenDisposition disposition
= NEW_WINDOW
;
2528 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2531 // Control-clicks open in a background tab.
2532 // On OSX meta [the command key] takes the place of control.
2533 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefControlClickTest
) {
2534 #if defined(OS_MACOSX)
2535 int modifiers
= blink::WebInputEvent::MetaKey
;
2537 int modifiers
= blink::WebInputEvent::ControlKey
;
2539 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2540 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2541 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2544 // Control-shift-clicks open in a foreground tab.
2545 // On OSX meta [the command key] takes the place of control.
2546 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefControlShiftClickTest
) {
2547 #if defined(OS_MACOSX)
2548 int modifiers
= blink::WebInputEvent::MetaKey
;
2550 int modifiers
= blink::WebInputEvent::ControlKey
;
2552 modifiers
|= blink::WebInputEvent::ShiftKey
;
2553 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2554 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2555 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2558 // Middle-clicks open in a background tab.
2559 // TODO(linux_aura) http://crbug.com/163931
2560 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
2561 #define MAYBE_HrefMiddleClickTest DISABLED_HrefMiddleClickTest
2563 #define MAYBE_HrefMiddleClickTest HrefMiddleClickTest
2565 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, MAYBE_HrefMiddleClickTest
) {
2567 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2568 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2569 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2572 // Shift-middle-clicks open in a foreground tab.
2573 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefShiftMiddleClickTest
) {
2574 int modifiers
= blink::WebInputEvent::ShiftKey
;
2575 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2576 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2577 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2580 IN_PROC_BROWSER_TEST_F(BrowserTest
, GetSizeForNewRenderView
) {
2581 // The instant extended NTP has javascript that does not work with
2582 // ui_test_utils::NavigateToURL. The NTP rvh reloads when the browser tries
2583 // to navigate away from the page, which causes the WebContents to end up in
2584 // an inconsistent state. (is_loaded = true, last_commited_url=ntp,
2585 // visible_url=title1.html)
2586 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled
,
2588 ASSERT_TRUE(test_server()->Start());
2589 // Create an HTTPS server for cross-site transition.
2590 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
2591 net::SpawnedTestServer::kLocalhost
,
2592 base::FilePath(kDocRoot
));
2593 ASSERT_TRUE(https_test_server
.Start());
2596 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2597 ASSERT_EQ(BookmarkBar::DETACHED
, browser()->bookmark_bar_state());
2598 WebContents
* web_contents
=
2599 browser()->tab_strip_model()->GetActiveWebContents();
2600 content::RenderViewHost
* prev_rvh
= web_contents
->GetRenderViewHost();
2601 const int height_inset
=
2602 browser()->window()->GetRenderViewHeightInsetWithDetachedBookmarkBar();
2603 const gfx::Size initial_wcv_size
=
2604 web_contents
->GetContainerBounds().size();
2605 RenderViewSizeObserver
observer(web_contents
, browser()->window());
2607 // Navigate to a non-NTP page, without resizing WebContentsView.
2608 ui_test_utils::NavigateToURL(browser(),
2609 test_server()->GetURL("files/title1.html"));
2610 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2611 // A new RenderViewHost should be created.
2612 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2613 prev_rvh
= web_contents
->GetRenderViewHost();
2614 gfx::Size rwhv_create_size0
, rwhv_commit_size0
, wcv_commit_size0
;
2615 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2619 // The create height of RenderWidgetHostView should include the height inset.
2620 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2621 initial_wcv_size
.height() + height_inset
),
2623 // When a navigation entry is committed, the size of RenderWidgetHostView
2624 // should be the same as when it was first created.
2625 EXPECT_EQ(rwhv_create_size0
, rwhv_commit_size0
);
2626 // Sizes of the current RenderWidgetHostView and WebContentsView should not
2627 // change before and after WebContentsDelegate::DidNavigateMainFramePostCommit
2628 // (implemented by Browser); we obtain the sizes before PostCommit via
2629 // WebContentsObserver::NavigationEntryCommitted (implemented by
2630 // RenderViewSizeObserver).
2631 EXPECT_EQ(rwhv_commit_size0
,
2632 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2633 // The behavior differs between OSX and views.
2634 // In OSX, the wcv does not change size until after the commit, when the
2635 // bookmark bar disappears (correct).
2636 // In views, the wcv changes size at commit time.
2637 #if defined(OS_MACOSX)
2638 EXPECT_EQ(gfx::Size(wcv_commit_size0
.width(),
2639 wcv_commit_size0
.height() + height_inset
),
2640 web_contents
->GetContainerBounds().size());
2642 EXPECT_EQ(wcv_commit_size0
, web_contents
->GetContainerBounds().size());
2645 // Navigate to another non-NTP page, without resizing WebContentsView.
2646 ui_test_utils::NavigateToURL(browser(),
2647 https_test_server
.GetURL("files/title2.html"));
2648 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2649 // A new RenderVieHost should be created.
2650 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2651 gfx::Size rwhv_create_size1
, rwhv_commit_size1
, wcv_commit_size1
;
2652 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2656 EXPECT_EQ(rwhv_create_size1
, rwhv_commit_size1
);
2657 EXPECT_EQ(rwhv_commit_size1
,
2658 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2659 EXPECT_EQ(wcv_commit_size1
, web_contents
->GetContainerBounds().size());
2661 // Navigate from NTP to a non-NTP page, resizing WebContentsView while
2662 // navigation entry is pending.
2663 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2664 gfx::Size
wcv_resize_insets(1, 1);
2665 observer
.set_wcv_resize_insets(wcv_resize_insets
);
2666 ui_test_utils::NavigateToURL(browser(),
2667 test_server()->GetURL("files/title2.html"));
2668 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2669 gfx::Size rwhv_create_size2
, rwhv_commit_size2
, wcv_commit_size2
;
2670 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2675 // The behavior on OSX and Views is incorrect in this edge case, but they are
2676 // differently incorrect.
2677 // The behavior should be:
2678 // initial wcv size: (100,100) (to choose random numbers)
2679 // initial rwhv size: (100,140)
2680 // commit wcv size: (101, 101)
2681 // commit rwhv size: (101, 141)
2682 // final wcv size: (101, 141)
2683 // final rwhv size: (101, 141)
2685 // On OSX, the commit rwhv size is (101, 101)
2686 // On views, the commit wcv size is (101, 141)
2687 // All other sizes are correct.
2689 // The create height of RenderWidgetHostView should include the height inset.
2690 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2691 initial_wcv_size
.height() + height_inset
),
2693 gfx::Size
exp_commit_size(initial_wcv_size
);
2695 #if defined(OS_MACOSX)
2696 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2697 wcv_resize_insets
.height());
2699 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2700 wcv_resize_insets
.height() + height_inset
);
2702 EXPECT_EQ(exp_commit_size
, rwhv_commit_size2
);
2703 EXPECT_EQ(exp_commit_size
, wcv_commit_size2
);
2704 gfx::Size
exp_final_size(initial_wcv_size
);
2705 exp_final_size
.Enlarge(wcv_resize_insets
.width(),
2706 wcv_resize_insets
.height() + height_inset
);
2707 EXPECT_EQ(exp_final_size
,
2708 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2709 EXPECT_EQ(exp_final_size
, web_contents
->GetContainerBounds().size());