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/memory/scoped_ptr.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/sys_info.h"
15 #include "chrome/app/chrome_command_ids.h"
16 #include "chrome/browser/chrome_content_browser_client.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/command_updater.h"
19 #include "chrome/browser/content_settings/host_content_settings_map.h"
20 #include "chrome/browser/defaults.h"
21 #include "chrome/browser/devtools/devtools_window_testing.h"
22 #include "chrome/browser/extensions/extension_browsertest.h"
23 #include "chrome/browser/extensions/extension_service.h"
24 #include "chrome/browser/extensions/tab_helper.h"
25 #include "chrome/browser/first_run/first_run.h"
26 #include "chrome/browser/lifetime/application_lifetime.h"
27 #include "chrome/browser/prefs/incognito_mode_prefs.h"
28 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/browser/profiles/profile_manager.h"
30 #include "chrome/browser/search/search.h"
31 #include "chrome/browser/sessions/session_backend.h"
32 #include "chrome/browser/sessions/session_service_factory.h"
33 #include "chrome/browser/translate/chrome_translate_client.h"
34 #include "chrome/browser/translate/cld_data_harness.h"
35 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h"
36 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h"
37 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h"
38 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h"
39 #include "chrome/browser/ui/browser.h"
40 #include "chrome/browser/ui/browser_command_controller.h"
41 #include "chrome/browser/ui/browser_commands.h"
42 #include "chrome/browser/ui/browser_finder.h"
43 #include "chrome/browser/ui/browser_iterator.h"
44 #include "chrome/browser/ui/browser_navigator.h"
45 #include "chrome/browser/ui/browser_tabstrip.h"
46 #include "chrome/browser/ui/browser_ui_prefs.h"
47 #include "chrome/browser/ui/browser_window.h"
48 #include "chrome/browser/ui/extensions/application_launch.h"
49 #include "chrome/browser/ui/host_desktop.h"
50 #include "chrome/browser/ui/startup/startup_browser_creator.h"
51 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
52 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
53 #include "chrome/browser/ui/tabs/tab_strip_model.h"
54 #include "chrome/common/chrome_switches.h"
55 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
56 #include "chrome/common/pref_names.h"
57 #include "chrome/common/url_constants.h"
58 #include "chrome/grit/chromium_strings.h"
59 #include "chrome/grit/generated_resources.h"
60 #include "chrome/test/base/in_process_browser_test.h"
61 #include "chrome/test/base/test_switches.h"
62 #include "chrome/test/base/ui_test_utils.h"
63 #include "components/translate/core/browser/language_state.h"
64 #include "components/translate/core/common/language_detection_details.h"
65 #include "content/public/browser/favicon_status.h"
66 #include "content/public/browser/host_zoom_map.h"
67 #include "content/public/browser/interstitial_page.h"
68 #include "content/public/browser/interstitial_page_delegate.h"
69 #include "content/public/browser/navigation_entry.h"
70 #include "content/public/browser/notification_service.h"
71 #include "content/public/browser/render_frame_host.h"
72 #include "content/public/browser/render_process_host.h"
73 #include "content/public/browser/render_view_host.h"
74 #include "content/public/browser/render_widget_host_view.h"
75 #include "content/public/browser/resource_context.h"
76 #include "content/public/browser/web_contents.h"
77 #include "content/public/browser/web_contents_observer.h"
78 #include "content/public/common/frame_navigate_params.h"
79 #include "content/public/common/page_transition_types.h"
80 #include "content/public/common/renderer_preferences.h"
81 #include "content/public/common/url_constants.h"
82 #include "content/public/test/browser_test_utils.h"
83 #include "content/public/test/test_navigation_observer.h"
84 #include "extensions/browser/extension_system.h"
85 #include "extensions/browser/uninstall_reason.h"
86 #include "extensions/common/extension.h"
87 #include "extensions/common/extension_set.h"
88 #include "net/dns/mock_host_resolver.h"
89 #include "net/test/spawned_test_server/spawned_test_server.h"
90 #include "ui/base/l10n/l10n_util.h"
92 #if defined(OS_MACOSX)
93 #include "base/mac/mac_util.h"
94 #include "base/mac/scoped_nsautorelease_pool.h"
95 #include "chrome/browser/ui/cocoa/run_loop_testing.h"
99 #include "base/i18n/rtl.h"
100 #include "chrome/browser/browser_process.h"
103 using base::ASCIIToUTF16
;
104 using content::InterstitialPage
;
105 using content::HostZoomMap
;
106 using content::NavigationController
;
107 using content::NavigationEntry
;
108 using content::OpenURLParams
;
109 using content::Referrer
;
110 using content::WebContents
;
111 using content::WebContentsObserver
;
112 using extensions::Extension
;
116 const char* kBeforeUnloadHTML
=
117 "<html><head><title>beforeunload</title></head><body>"
118 "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
121 const char* kOpenNewBeforeUnloadPage
=
122 "w=window.open(); w.onbeforeunload=function(e){return 'foo'};";
124 const base::FilePath::CharType
* kBeforeUnloadFile
=
125 FILE_PATH_LITERAL("beforeunload.html");
127 const base::FilePath::CharType
* kTitle1File
= FILE_PATH_LITERAL("title1.html");
128 const base::FilePath::CharType
* kTitle2File
= FILE_PATH_LITERAL("title2.html");
130 const base::FilePath::CharType kDocRoot
[] =
131 FILE_PATH_LITERAL("chrome/test/data");
133 // Given a page title, returns the expected window caption string.
134 base::string16
WindowCaptionFromPageTitle(const base::string16
& page_title
) {
135 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
136 // On Mac or ChromeOS, we don't want to suffix the page title with
137 // the application name.
138 if (page_title
.empty())
139 return l10n_util::GetStringUTF16(IDS_BROWSER_WINDOW_MAC_TAB_UNTITLED
);
142 if (page_title
.empty())
143 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
);
145 return l10n_util::GetStringFUTF16(IDS_BROWSER_WINDOW_TITLE_FORMAT
,
150 // Returns the number of active RenderProcessHosts.
151 int CountRenderProcessHosts() {
153 for (content::RenderProcessHost::iterator
i(
154 content::RenderProcessHost::AllHostsIterator());
155 !i
.IsAtEnd(); i
.Advance())
160 class MockTabStripModelObserver
: public TabStripModelObserver
{
162 MockTabStripModelObserver() : closing_count_(0) {}
164 virtual void TabClosingAt(TabStripModel
* tab_strip_model
,
165 WebContents
* contents
,
166 int index
) OVERRIDE
{
170 int closing_count() const { return closing_count_
; }
175 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver
);
178 class InterstitialObserver
: public content::WebContentsObserver
{
180 InterstitialObserver(content::WebContents
* web_contents
,
181 const base::Closure
& attach_callback
,
182 const base::Closure
& detach_callback
)
183 : WebContentsObserver(web_contents
),
184 attach_callback_(attach_callback
),
185 detach_callback_(detach_callback
) {
188 virtual void DidAttachInterstitialPage() OVERRIDE
{
189 attach_callback_
.Run();
192 virtual void DidDetachInterstitialPage() OVERRIDE
{
193 detach_callback_
.Run();
197 base::Closure attach_callback_
;
198 base::Closure detach_callback_
;
200 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver
);
203 // Causes the browser to swap processes on a redirect to an HTTPS URL.
204 class TransferHttpsRedirectsContentBrowserClient
205 : public chrome::ChromeContentBrowserClient
{
207 virtual bool ShouldSwapProcessesForRedirect(
208 content::ResourceContext
* resource_context
,
209 const GURL
& current_url
,
210 const GURL
& new_url
) OVERRIDE
{
211 return new_url
.SchemeIs(url::kHttpsScheme
);
215 // Used by CloseWithAppMenuOpen. Invokes CloseWindow on the supplied browser.
216 void CloseWindowCallback(Browser
* browser
) {
217 chrome::CloseWindow(browser
);
220 // Used by CloseWithAppMenuOpen. Posts a CloseWindowCallback and shows the app
222 void RunCloseWithAppMenuCallback(Browser
* browser
) {
223 // ShowAppMenu is modal under views. Schedule a task that closes the window.
224 base::MessageLoop::current()->PostTask(
225 FROM_HERE
, base::Bind(&CloseWindowCallback
, browser
));
226 chrome::ShowAppMenu(browser
);
229 // Displays "INTERSTITIAL" while the interstitial is attached.
230 // (InterstitialPage can be used in a test directly, but there would be no way
231 // to visually tell if it is showing or not.)
232 class TestInterstitialPage
: public content::InterstitialPageDelegate
{
234 TestInterstitialPage(WebContents
* tab
, bool new_navigation
, const GURL
& url
) {
235 interstitial_page_
= InterstitialPage::Create(
236 tab
, new_navigation
, url
, this);
237 interstitial_page_
->Show();
239 virtual ~TestInterstitialPage() { }
241 interstitial_page_
->Proceed();
244 interstitial_page_
->DontProceed();
247 virtual std::string
GetHTMLContents() OVERRIDE
{
248 return "<h1>INTERSTITIAL</h1>";
252 InterstitialPage
* interstitial_page_
; // Owns us.
255 class RenderViewSizeObserver
: public content::WebContentsObserver
{
257 RenderViewSizeObserver(content::WebContents
* web_contents
,
258 BrowserWindow
* browser_window
)
259 : WebContentsObserver(web_contents
),
260 browser_window_(browser_window
) {
263 void GetSizeForRenderViewHost(
264 content::RenderViewHost
* render_view_host
,
265 gfx::Size
* rwhv_create_size
,
266 gfx::Size
* rwhv_commit_size
,
267 gfx::Size
* wcv_commit_size
) {
268 RenderViewSizes::const_iterator result
= render_view_sizes_
.end();
269 result
= render_view_sizes_
.find(render_view_host
);
270 if (result
!= render_view_sizes_
.end()) {
271 *rwhv_create_size
= result
->second
.rwhv_create_size
;
272 *rwhv_commit_size
= result
->second
.rwhv_commit_size
;
273 *wcv_commit_size
= result
->second
.wcv_commit_size
;
277 void set_wcv_resize_insets(const gfx::Size
& wcv_resize_insets
) {
278 wcv_resize_insets_
= wcv_resize_insets
;
281 // Cache the size when RenderViewHost is first created.
282 virtual void RenderViewCreated(
283 content::RenderViewHost
* render_view_host
) OVERRIDE
{
284 render_view_sizes_
[render_view_host
].rwhv_create_size
=
285 render_view_host
->GetView()->GetViewBounds().size();
288 // Enlarge WebContentsView by |wcv_resize_insets_| while the navigation entry
290 virtual void DidStartNavigationToPendingEntry(
292 NavigationController::ReloadType reload_type
) OVERRIDE
{
293 if (wcv_resize_insets_
.IsEmpty())
295 // Resizing the main browser window by |wcv_resize_insets_| will
296 // automatically resize the WebContentsView by the same amount.
297 // Just resizing WebContentsView directly doesn't work on Linux, because the
298 // next automatic layout of the browser window will resize WebContentsView
299 // back to the previous size. To make it consistent, resize main browser
300 // window on all platforms.
301 gfx::Rect
bounds(browser_window_
->GetBounds());
302 gfx::Size
size(bounds
.size());
303 size
.Enlarge(wcv_resize_insets_
.width(), wcv_resize_insets_
.height());
304 bounds
.set_size(size
);
305 browser_window_
->SetBounds(bounds
);
306 // Let the message loop run so that resize actually takes effect.
307 content::RunAllPendingInMessageLoop();
310 // Cache the sizes of RenderWidgetHostView and WebContentsView when the
311 // navigation entry is committed, which is before
312 // WebContentsDelegate::DidNavigateMainFramePostCommit is called.
313 virtual void NavigationEntryCommitted(
314 const content::LoadCommittedDetails
& details
) OVERRIDE
{
315 content::RenderViewHost
* rvh
= web_contents()->GetRenderViewHost();
316 render_view_sizes_
[rvh
].rwhv_commit_size
=
317 web_contents()->GetRenderWidgetHostView()->GetViewBounds().size();
318 render_view_sizes_
[rvh
].wcv_commit_size
=
319 web_contents()->GetContainerBounds().size();
324 gfx::Size rwhv_create_size
; // Size of RenderWidgetHostView when created.
325 gfx::Size rwhv_commit_size
; // Size of RenderWidgetHostView when committed.
326 gfx::Size wcv_commit_size
; // Size of WebContentsView when committed.
329 typedef std::map
<content::RenderViewHost
*, Sizes
> RenderViewSizes
;
330 RenderViewSizes render_view_sizes_
;
331 // Enlarge WebContentsView by this size insets in
332 // DidStartNavigationToPendingEntry.
333 gfx::Size wcv_resize_insets_
;
334 BrowserWindow
* browser_window_
; // Weak ptr.
336 DISALLOW_COPY_AND_ASSIGN(RenderViewSizeObserver
);
341 class BrowserTest
: public ExtensionBrowserTest
{
343 // In RTL locales wrap the page title with RTL embedding characters so that it
344 // matches the value returned by GetWindowTitle().
345 base::string16
LocaleWindowCaptionFromPageTitle(
346 const base::string16
& expected_title
) {
347 base::string16 page_title
= WindowCaptionFromPageTitle(expected_title
);
349 std::string locale
= g_browser_process
->GetApplicationLocale();
350 if (base::i18n::GetTextDirectionForLocale(locale
.c_str()) ==
351 base::i18n::RIGHT_TO_LEFT
) {
352 base::i18n::WrapStringWithLTRFormatting(&page_title
);
357 // Do we need to use the above code on POSIX as well?
362 // Returns the app extension aptly named "App Test".
363 const Extension
* GetExtension() {
364 const extensions::ExtensionSet
* extensions
=
365 extensions::ExtensionSystem::Get(
366 browser()->profile())->extension_service()->extensions();
367 for (extensions::ExtensionSet::const_iterator it
= extensions
->begin();
368 it
!= extensions
->end(); ++it
) {
369 if ((*it
)->name() == "App Test")
377 // Launch the app on a page with no title, check that the app title was set
379 IN_PROC_BROWSER_TEST_F(BrowserTest
, NoTitle
) {
380 #if defined(OS_WIN) && defined(USE_ASH)
381 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
382 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
386 ui_test_utils::NavigateToURL(
387 browser(), ui_test_utils::GetTestUrl(
388 base::FilePath(base::FilePath::kCurrentDirectory
),
389 base::FilePath(kTitle1File
)));
390 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(ASCIIToUTF16("title1.html")),
391 browser()->GetWindowTitleForCurrentTab());
392 base::string16 tab_title
;
393 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title
));
394 EXPECT_EQ(ASCIIToUTF16("title1.html"), tab_title
);
397 // Launch the app, navigate to a page with a title, check that the app title
398 // was set correctly.
399 IN_PROC_BROWSER_TEST_F(BrowserTest
, Title
) {
400 #if defined(OS_WIN) && defined(USE_ASH)
401 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
402 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
406 ui_test_utils::NavigateToURL(
407 browser(), ui_test_utils::GetTestUrl(
408 base::FilePath(base::FilePath::kCurrentDirectory
),
409 base::FilePath(kTitle2File
)));
410 const base::string16
test_title(ASCIIToUTF16("Title Of Awesomeness"));
411 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(test_title
),
412 browser()->GetWindowTitleForCurrentTab());
413 base::string16 tab_title
;
414 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title
));
415 EXPECT_EQ(test_title
, tab_title
);
418 IN_PROC_BROWSER_TEST_F(BrowserTest
, JavascriptAlertActivatesTab
) {
419 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
420 base::FilePath::kCurrentDirectory
), base::FilePath(kTitle1File
)));
421 ui_test_utils::NavigateToURL(browser(), url
);
422 AddTabAtIndex(0, url
, content::PAGE_TRANSITION_TYPED
);
423 EXPECT_EQ(2, browser()->tab_strip_model()->count());
424 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
425 WebContents
* second_tab
= browser()->tab_strip_model()->GetWebContentsAt(1);
426 ASSERT_TRUE(second_tab
);
427 second_tab
->GetMainFrame()->ExecuteJavaScript(
428 ASCIIToUTF16("alert('Activate!');"));
429 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
430 alert
->CloseModalDialog();
431 EXPECT_EQ(2, browser()->tab_strip_model()->count());
432 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
436 #if defined(OS_WIN) && !defined(NDEBUG)
437 // http://crbug.com/114859. Times out frequently on Windows.
438 #define MAYBE_ThirtyFourTabs DISABLED_ThirtyFourTabs
440 #define MAYBE_ThirtyFourTabs ThirtyFourTabs
443 // Create 34 tabs and verify that a lot of processes have been created. The
444 // exact number of processes depends on the amount of memory. Previously we
445 // had a hard limit of 31 processes and this test is mainly directed at
446 // verifying that we don't crash when we pass this limit.
447 // Warning: this test can take >30 seconds when running on a slow (low
448 // memory?) Mac builder.
449 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_ThirtyFourTabs
) {
450 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
451 base::FilePath::kCurrentDirectory
), base::FilePath(kTitle2File
)));
453 // There is one initial tab.
454 const int kTabCount
= 34;
455 for (int ix
= 0; ix
!= (kTabCount
- 1); ++ix
) {
456 chrome::AddSelectedTabWithURL(browser(), url
,
457 content::PAGE_TRANSITION_TYPED
);
459 EXPECT_EQ(kTabCount
, browser()->tab_strip_model()->count());
461 // See GetMaxRendererProcessCount() in
462 // content/browser/renderer_host/render_process_host_impl.cc
463 // for the algorithm to decide how many processes to create.
464 const int kExpectedProcessCount
=
465 #if defined(ARCH_CPU_64_BITS)
470 if (base::SysInfo::AmountOfPhysicalMemoryMB() >= 2048) {
471 EXPECT_GE(CountRenderProcessHosts(), kExpectedProcessCount
);
473 EXPECT_LT(CountRenderProcessHosts(), kExpectedProcessCount
);
477 // Test that a browser-initiated navigation to an aborted URL load leaves around
478 // a pending entry if we start from the NTP but not from a normal page.
479 // See http://crbug.com/355537.
480 IN_PROC_BROWSER_TEST_F(BrowserTest
, ClearPendingOnFailUnlessNTP
) {
481 ASSERT_TRUE(test_server()->Start());
482 WebContents
* web_contents
=
483 browser()->tab_strip_model()->GetActiveWebContents();
484 GURL
ntp_url(chrome::GetNewTabPageURL(browser()->profile()));
485 ui_test_utils::NavigateToURL(browser(), ntp_url
);
487 // Navigate to a 204 URL (aborts with no content) on the NTP and make sure it
488 // sticks around so that the user can edit it.
489 GURL
abort_url(test_server()->GetURL("nocontent"));
491 content::WindowedNotificationObserver
stop_observer(
492 content::NOTIFICATION_LOAD_STOP
,
493 content::Source
<NavigationController
>(
494 &web_contents
->GetController()));
495 browser()->OpenURL(OpenURLParams(abort_url
, Referrer(), CURRENT_TAB
,
496 content::PAGE_TRANSITION_TYPED
, false));
497 stop_observer
.Wait();
498 EXPECT_TRUE(web_contents
->GetController().GetPendingEntry());
499 EXPECT_EQ(abort_url
, web_contents
->GetVisibleURL());
502 // Navigate to a real URL.
503 GURL
real_url(test_server()->GetURL("title1.html"));
504 ui_test_utils::NavigateToURL(browser(), real_url
);
505 EXPECT_EQ(real_url
, web_contents
->GetVisibleURL());
507 // Now navigating to a 204 URL should clear the pending entry.
509 content::WindowedNotificationObserver
stop_observer(
510 content::NOTIFICATION_LOAD_STOP
,
511 content::Source
<NavigationController
>(
512 &web_contents
->GetController()));
513 browser()->OpenURL(OpenURLParams(abort_url
, Referrer(), CURRENT_TAB
,
514 content::PAGE_TRANSITION_TYPED
, false));
515 stop_observer
.Wait();
516 EXPECT_FALSE(web_contents
->GetController().GetPendingEntry());
517 EXPECT_EQ(real_url
, web_contents
->GetVisibleURL());
521 // Test for crbug.com/297289. Ensure that modal dialogs are closed when a
522 // cross-process navigation is ready to commit.
523 IN_PROC_BROWSER_TEST_F(BrowserTest
, CrossProcessNavCancelsDialogs
) {
524 ASSERT_TRUE(test_server()->Start());
525 host_resolver()->AddRule("www.example.com", "127.0.0.1");
526 GURL
url(test_server()->GetURL("empty.html"));
527 ui_test_utils::NavigateToURL(browser(), url
);
529 // Test this with multiple alert dialogs to ensure that we can navigate away
530 // even if the renderer tries to synchronously create more.
531 // See http://crbug.com/312490.
532 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
533 contents
->GetMainFrame()->ExecuteJavaScript(
534 ASCIIToUTF16("alert('one'); alert('two');"));
535 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
536 EXPECT_TRUE(alert
->IsValid());
537 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
538 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
540 // A cross-site navigation should force the dialog to close.
541 GURL
url2("http://www.example.com/empty.html");
542 ui_test_utils::NavigateToURL(browser(), url2
);
543 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
545 // Make sure input events still work in the renderer process.
546 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
549 // Make sure that dialogs are closed after a renderer process dies, and that
550 // subsequent navigations work. See http://crbug/com/343265.
551 IN_PROC_BROWSER_TEST_F(BrowserTest
, SadTabCancelsDialogs
) {
552 ASSERT_TRUE(test_server()->Start());
553 host_resolver()->AddRule("www.example.com", "127.0.0.1");
554 GURL
beforeunload_url(test_server()->GetURL("files/beforeunload.html"));
555 ui_test_utils::NavigateToURL(browser(), beforeunload_url
);
557 // Start a navigation to trigger the beforeunload dialog.
558 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
559 contents
->GetMainFrame()->ExecuteJavaScript(
560 ASCIIToUTF16("window.location.href = 'data:text/html,foo'"));
561 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
562 EXPECT_TRUE(alert
->IsValid());
563 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
564 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
566 // Crash the renderer process and ensure the dialog is gone.
567 content::RenderProcessHost
* child_process
= contents
->GetRenderProcessHost();
568 content::RenderProcessHostWatcher
crash_observer(
570 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
571 base::KillProcess(child_process
->GetHandle(), 0, false);
572 crash_observer
.Wait();
573 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
575 // Make sure subsequent navigations work.
576 GURL
url2("http://www.example.com/files/empty.html");
577 ui_test_utils::NavigateToURL(browser(), url2
);
580 // Make sure that dialogs opened by subframes are closed when the process dies.
581 // See http://crbug.com/366510.
582 IN_PROC_BROWSER_TEST_F(BrowserTest
, SadTabCancelsSubframeDialogs
) {
583 // Navigate to an iframe that opens an alert dialog.
584 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
585 contents
->GetMainFrame()->ExecuteJavaScript(
586 ASCIIToUTF16("window.location.href = 'data:text/html,"
587 "<iframe srcdoc=\"<script>alert(1)</script>\">'"));
588 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
589 EXPECT_TRUE(alert
->IsValid());
590 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
591 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
593 // Crash the renderer process and ensure the dialog is gone.
594 content::RenderProcessHost
* child_process
= contents
->GetRenderProcessHost();
595 content::RenderProcessHostWatcher
crash_observer(
597 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
598 base::KillProcess(child_process
->GetHandle(), 0, false);
599 crash_observer
.Wait();
600 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
602 // Make sure subsequent navigations work.
603 GURL
url2("data:text/html,foo");
604 ui_test_utils::NavigateToURL(browser(), url2
);
607 // Test for crbug.com/22004. Reloading a page with a before unload handler and
608 // then canceling the dialog should not leave the throbber spinning.
609 IN_PROC_BROWSER_TEST_F(BrowserTest
, ReloadThenCancelBeforeUnload
) {
610 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
611 ui_test_utils::NavigateToURL(browser(), url
);
613 // Navigate to another page, but click cancel in the dialog. Make sure that
614 // the throbber stops spinning.
615 chrome::Reload(browser(), CURRENT_TAB
);
616 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
617 alert
->CloseModalDialog();
619 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
621 // Clear the beforeunload handler so the test can easily exit.
622 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
623 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
626 class RedirectObserver
: public content::WebContentsObserver
{
628 explicit RedirectObserver(content::WebContents
* web_contents
)
629 : WebContentsObserver(web_contents
) {
632 virtual void DidNavigateAnyFrame(
633 const content::LoadCommittedDetails
& details
,
634 const content::FrameNavigateParams
& params
) OVERRIDE
{
638 virtual void WebContentsDestroyed() OVERRIDE
{
639 // Make sure we don't close the tab while the observer is in scope.
640 // See http://crbug.com/314036.
641 FAIL() << "WebContents closed during navigation (http://crbug.com/314036).";
644 const content::FrameNavigateParams
& params() const {
649 content::FrameNavigateParams params_
;
651 DISALLOW_COPY_AND_ASSIGN(RedirectObserver
);
654 // Ensure that a transferred cross-process navigation does not generate
655 // DidStopLoading events until the navigation commits. If it did, then
656 // ui_test_utils::NavigateToURL would proceed before the URL had committed.
657 // http://crbug.com/243957.
658 IN_PROC_BROWSER_TEST_F(BrowserTest
, NoStopDuringTransferUntilCommit
) {
659 // Create HTTP and HTTPS servers for a cross-site transition.
660 ASSERT_TRUE(test_server()->Start());
661 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
662 net::SpawnedTestServer::kLocalhost
,
663 base::FilePath(kDocRoot
));
664 ASSERT_TRUE(https_test_server
.Start());
666 // Temporarily replace ContentBrowserClient with one that will cause a
667 // process swap on all redirects to HTTPS URLs.
668 TransferHttpsRedirectsContentBrowserClient new_client
;
669 content::ContentBrowserClient
* old_client
=
670 SetBrowserClientForTesting(&new_client
);
672 GURL
init_url(test_server()->GetURL("files/title1.html"));
673 ui_test_utils::NavigateToURL(browser(), init_url
);
675 // Navigate to a same-site page that redirects, causing a transfer.
676 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
678 // Create a RedirectObserver that goes away before we close the tab.
680 RedirectObserver
redirect_observer(contents
);
681 GURL
dest_url(https_test_server
.GetURL("files/title2.html"));
682 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
684 ui_test_utils::NavigateToURL(browser(), redirect_url
);
686 // We should immediately see the new committed entry.
687 EXPECT_FALSE(contents
->GetController().GetPendingEntry());
689 contents
->GetController().GetLastCommittedEntry()->GetURL());
691 // We should keep track of the original request URL, redirect chain, and
692 // page transition type during a transfer, since these are necessary for
693 // history autocomplete to work.
694 EXPECT_EQ(redirect_url
, contents
->GetController().GetLastCommittedEntry()->
695 GetOriginalRequestURL());
696 EXPECT_EQ(2U, redirect_observer
.params().redirects
.size());
697 EXPECT_EQ(redirect_url
, redirect_observer
.params().redirects
.at(0));
698 EXPECT_EQ(dest_url
, redirect_observer
.params().redirects
.at(1));
699 EXPECT_TRUE(PageTransitionCoreTypeIs(redirect_observer
.params().transition
,
700 content::PAGE_TRANSITION_TYPED
));
703 // Restore previous browser client.
704 SetBrowserClientForTesting(old_client
);
707 // Tests that a cross-process redirect will only cause the beforeunload
708 // handler to run once.
709 IN_PROC_BROWSER_TEST_F(BrowserTest
, SingleBeforeUnloadAfterRedirect
) {
710 // Create HTTP and HTTPS servers for a cross-site transition.
711 ASSERT_TRUE(test_server()->Start());
712 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
713 net::SpawnedTestServer::kLocalhost
,
714 base::FilePath(kDocRoot
));
715 ASSERT_TRUE(https_test_server
.Start());
717 // Temporarily replace ContentBrowserClient with one that will cause a
718 // process swap on all redirects to HTTPS URLs.
719 TransferHttpsRedirectsContentBrowserClient new_client
;
720 content::ContentBrowserClient
* old_client
=
721 SetBrowserClientForTesting(&new_client
);
723 // Navigate to a page with a beforeunload handler.
724 GURL
url(test_server()->GetURL("files/beforeunload.html"));
725 ui_test_utils::NavigateToURL(browser(), url
);
727 // Navigate to a URL that redirects to another process and approve the
728 // beforeunload dialog that pops up.
729 content::WindowedNotificationObserver
nav_observer(
730 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
731 content::NotificationService::AllSources());
732 GURL
https_url(https_test_server
.GetURL("files/title1.html"));
733 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
735 browser()->OpenURL(OpenURLParams(redirect_url
, Referrer(), CURRENT_TAB
,
736 content::PAGE_TRANSITION_TYPED
, false));
737 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
739 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
740 alert
->native_dialog()->AcceptAppModalDialog();
743 // Restore previous browser client.
744 SetBrowserClientForTesting(old_client
);
747 // Test for crbug.com/80401. Canceling a before unload dialog should reset
748 // the URL to the previous page's URL.
749 IN_PROC_BROWSER_TEST_F(BrowserTest
, CancelBeforeUnloadResetsURL
) {
750 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
751 base::FilePath::kCurrentDirectory
), base::FilePath(kBeforeUnloadFile
)));
752 ui_test_utils::NavigateToURL(browser(), url
);
754 // Navigate to a page that triggers a cross-site transition.
755 ASSERT_TRUE(test_server()->Start());
756 GURL
url2(test_server()->GetURL("files/title1.html"));
757 browser()->OpenURL(OpenURLParams(
758 url2
, Referrer(), CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false));
760 content::WindowedNotificationObserver
host_destroyed_observer(
761 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED
,
762 content::NotificationService::AllSources());
764 // Cancel the dialog.
765 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
766 alert
->CloseModalDialog();
768 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
770 // Verify there are no pending history items after the dialog is cancelled.
771 // (see crbug.com/93858)
772 NavigationEntry
* entry
= browser()->tab_strip_model()->
773 GetActiveWebContents()->GetController().GetPendingEntry();
774 EXPECT_EQ(NULL
, entry
);
776 // Wait for the ShouldClose_ACK to arrive. We can detect it by waiting for
777 // the pending RVH to be destroyed.
778 host_destroyed_observer
.Wait();
779 EXPECT_EQ(url
, browser()->toolbar_model()->GetURL());
781 // Clear the beforeunload handler so the test can easily exit.
782 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
783 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
786 // Crashy on mac. http://crbug.com/38522 Crashy on win too (after 3 years).
787 // Flaky timeouts on a MemorySanitizer bot. http://crbug.com/410891
788 #if defined(OS_MACOSX) || \
790 defined(MEMORY_SANITIZER) || \
791 defined(ADDRESS_SANITIZER)
792 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
793 DISABLED_SingleBeforeUnloadAfterWindowClose
795 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
796 SingleBeforeUnloadAfterWindowClose
799 // Test for crbug.com/11647. A page closed with window.close() should not have
800 // two beforeunload dialogs shown.
801 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_SingleBeforeUnloadAfterWindowClose
) {
802 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
803 ExecuteJavaScriptForTests(ASCIIToUTF16(kOpenNewBeforeUnloadPage
));
805 // Close the new window with JavaScript, which should show a single
806 // beforeunload dialog. Then show another alert, to make it easy to verify
807 // that a second beforeunload dialog isn't shown.
808 browser()->tab_strip_model()->GetWebContentsAt(0)->GetMainFrame()->
809 ExecuteJavaScriptForTests(ASCIIToUTF16("w.close(); alert('bar');"));
810 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
811 alert
->native_dialog()->AcceptAppModalDialog();
813 alert
= ui_test_utils::WaitForAppModalDialog();
814 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->
815 is_before_unload_dialog());
816 alert
->native_dialog()->AcceptAppModalDialog();
819 // BrowserTest.BeforeUnloadVsBeforeReload times out on Windows.
820 // http://crbug.com/130411
822 #define MAYBE_BeforeUnloadVsBeforeReload DISABLED_BeforeUnloadVsBeforeReload
824 #define MAYBE_BeforeUnloadVsBeforeReload BeforeUnloadVsBeforeReload
827 // Test that when a page has an onunload handler, reloading a page shows a
828 // different dialog than navigating to a different page.
829 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_BeforeUnloadVsBeforeReload
) {
830 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
831 ui_test_utils::NavigateToURL(browser(), url
);
833 // Reload the page, and check that we get a "before reload" dialog.
834 chrome::Reload(browser(), CURRENT_TAB
);
835 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
836 EXPECT_TRUE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
838 // Cancel the reload.
839 alert
->native_dialog()->CancelAppModalDialog();
841 // Navigate to another url, and check that we get a "before unload" dialog.
842 GURL
url2(url::kAboutBlankURL
);
843 browser()->OpenURL(OpenURLParams(
844 url2
, Referrer(), CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false));
846 alert
= ui_test_utils::WaitForAppModalDialog();
847 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
849 // Accept the navigation so we end up on a page without a beforeunload hook.
850 alert
->native_dialog()->AcceptAppModalDialog();
853 // BeforeUnloadAtQuitWithTwoWindows is a regression test for
854 // http://crbug.com/11842. It opens two windows, one of which has a
855 // beforeunload handler and attempts to exit cleanly.
856 class BeforeUnloadAtQuitWithTwoWindows
: public InProcessBrowserTest
{
858 // This test is for testing a specific shutdown behavior. This mimics what
859 // happens in InProcessBrowserTest::RunTestOnMainThread and QuitBrowsers, but
860 // ensures that it happens through the single IDC_EXIT of the test.
861 virtual void TearDownOnMainThread() OVERRIDE
{
862 // Cycle both the MessageLoop and the Cocoa runloop twice to flush out any
863 // Chrome work that generates Cocoa work. Do this twice since there are two
864 // Browsers that must be closed.
868 // Run the application event loop to completion, which will cycle the
869 // native MessagePump on all platforms.
870 base::MessageLoop::current()->PostTask(FROM_HERE
,
871 base::MessageLoop::QuitClosure());
872 base::MessageLoop::current()->Run();
874 // Take care of any remaining Cocoa work.
877 // At this point, quit should be for real now.
878 ASSERT_EQ(0u, chrome::GetTotalBrowserCount());
881 // A helper function that cycles the MessageLoop, and on Mac, the Cocoa run
882 // loop. It also drains the NSAutoreleasePool.
883 void CycleRunLoops() {
884 content::RunAllPendingInMessageLoop();
885 #if defined(OS_MACOSX)
886 chrome::testing::NSRunLoopRunAllPending();
887 AutoreleasePool()->Recycle();
892 // Disabled, http://crbug.com/159214 .
893 IN_PROC_BROWSER_TEST_F(BeforeUnloadAtQuitWithTwoWindows
,
894 DISABLED_IfThisTestTimesOutItIndicatesFAILURE
) {
895 // In the first browser, set up a page that has a beforeunload handler.
896 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
897 ui_test_utils::NavigateToURL(browser(), url
);
899 // Open a second browser window at about:blank.
900 ui_test_utils::BrowserAddedObserver browser_added_observer
;
901 chrome::NewEmptyWindow(browser()->profile(), chrome::GetActiveDesktop());
902 Browser
* second_window
= browser_added_observer
.WaitForSingleNewBrowser();
903 ui_test_utils::NavigateToURL(second_window
, GURL(url::kAboutBlankURL
));
905 // Tell the application to quit. IDC_EXIT calls AttemptUserExit, which on
906 // everything but ChromeOS allows unload handlers to block exit. On that
907 // platform, though, it exits unconditionally. See the comment and bug ID
908 // in AttemptUserExit() in application_lifetime.cc.
909 #if defined(OS_CHROMEOS)
910 chrome::AttemptExit();
912 chrome::ExecuteCommand(second_window
, IDC_EXIT
);
915 // The beforeunload handler will run at exit, ensure it does, and then accept
916 // it to allow shutdown to proceed.
917 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
920 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
921 alert
->native_dialog()->AcceptAppModalDialog();
923 // But wait there's more! If this test times out, it likely means that the
924 // browser has not been able to quit correctly, indicating there's a
925 // regression of the bug noted above.
928 // Test that scripts can fork a new renderer process for a cross-site popup,
929 // based on http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab.
930 // The script must open a new tab, set its window.opener to null, and navigate
931 // it to a cross-site URL. It should also work for meta-refreshes.
932 // See http://crbug.com/93517.
933 IN_PROC_BROWSER_TEST_F(BrowserTest
, NullOpenerRedirectForksProcess
) {
934 CommandLine::ForCurrentProcess()->AppendSwitch(
935 switches::kDisablePopupBlocking
);
937 // Create http and https servers for a cross-site transition.
938 ASSERT_TRUE(test_server()->Start());
939 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
940 net::SpawnedTestServer::kLocalhost
,
941 base::FilePath(kDocRoot
));
942 ASSERT_TRUE(https_test_server
.Start());
943 GURL
http_url(test_server()->GetURL("files/title1.html"));
944 GURL
https_url(https_test_server
.GetURL(std::string()));
946 // Start with an http URL.
947 ui_test_utils::NavigateToURL(browser(), http_url
);
948 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
949 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
951 // Now open a tab to a blank page, set its opener to null, and redirect it
953 std::string redirect_popup
= "w=window.open();";
954 redirect_popup
+= "w.opener=null;";
955 redirect_popup
+= "w.document.location=\"";
956 redirect_popup
+= https_url
.spec();
957 redirect_popup
+= "\";";
959 content::WindowedNotificationObserver
popup_observer(
960 chrome::NOTIFICATION_TAB_ADDED
,
961 content::NotificationService::AllSources());
962 content::WindowedNotificationObserver
nav_observer(
963 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
964 content::NotificationService::AllSources());
965 oldtab
->GetMainFrame()->
966 ExecuteJavaScriptForTests(ASCIIToUTF16(redirect_popup
));
968 // Wait for popup window to appear and finish navigating.
969 popup_observer
.Wait();
970 ASSERT_EQ(2, browser()->tab_strip_model()->count());
971 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
973 EXPECT_NE(oldtab
, newtab
);
975 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
976 EXPECT_EQ(https_url
.spec(),
977 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
979 // Popup window should not be in the opener's process.
980 content::RenderProcessHost
* popup_process
=
981 newtab
->GetRenderProcessHost();
982 EXPECT_NE(process
, popup_process
);
984 // Now open a tab to a blank page, set its opener to null, and use a
985 // meta-refresh to navigate it instead.
986 std::string refresh_popup
= "w=window.open();";
987 refresh_popup
+= "w.opener=null;";
988 refresh_popup
+= "w.document.write(";
989 refresh_popup
+= "'<META HTTP-EQUIV=\"refresh\" content=\"0; url=";
990 refresh_popup
+= https_url
.spec();
991 refresh_popup
+= "\">');w.document.close();";
993 content::WindowedNotificationObserver
popup_observer2(
994 chrome::NOTIFICATION_TAB_ADDED
,
995 content::NotificationService::AllSources());
996 content::WindowedNotificationObserver
nav_observer2(
997 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
998 content::NotificationService::AllSources());
999 oldtab
->GetMainFrame()->
1000 ExecuteJavaScriptForTests(ASCIIToUTF16(refresh_popup
));
1002 // Wait for popup window to appear and finish navigating.
1003 popup_observer2
.Wait();
1004 ASSERT_EQ(3, browser()->tab_strip_model()->count());
1005 WebContents
* newtab2
= browser()->tab_strip_model()->GetActiveWebContents();
1006 EXPECT_TRUE(newtab2
);
1007 EXPECT_NE(oldtab
, newtab2
);
1008 nav_observer2
.Wait();
1009 ASSERT_TRUE(newtab2
->GetController().GetLastCommittedEntry());
1010 EXPECT_EQ(https_url
.spec(),
1011 newtab2
->GetController().GetLastCommittedEntry()->GetURL().spec());
1013 // This popup window should also not be in the opener's process.
1014 content::RenderProcessHost
* popup_process2
=
1015 newtab2
->GetRenderProcessHost();
1016 EXPECT_NE(process
, popup_process2
);
1019 // Tests that other popup navigations that do not follow the steps at
1020 // http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab will not
1021 // fork a new renderer process.
1022 IN_PROC_BROWSER_TEST_F(BrowserTest
, OtherRedirectsDontForkProcess
) {
1023 CommandLine::ForCurrentProcess()->AppendSwitch(
1024 switches::kDisablePopupBlocking
);
1026 // Create http and https servers for a cross-site transition.
1027 ASSERT_TRUE(test_server()->Start());
1028 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1029 net::SpawnedTestServer::kLocalhost
,
1030 base::FilePath(kDocRoot
));
1031 ASSERT_TRUE(https_test_server
.Start());
1032 GURL
http_url(test_server()->GetURL("files/title1.html"));
1033 GURL
https_url(https_test_server
.GetURL(std::string()));
1035 // Start with an http URL.
1036 ui_test_utils::NavigateToURL(browser(), http_url
);
1037 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
1038 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
1040 // Now open a tab to a blank page, set its opener to null, and redirect it
1042 std::string dont_fork_popup
= "w=window.open();";
1043 dont_fork_popup
+= "w.document.location=\"";
1044 dont_fork_popup
+= https_url
.spec();
1045 dont_fork_popup
+= "\";";
1047 content::WindowedNotificationObserver
popup_observer(
1048 chrome::NOTIFICATION_TAB_ADDED
,
1049 content::NotificationService::AllSources());
1050 content::WindowedNotificationObserver
nav_observer(
1051 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1052 content::NotificationService::AllSources());
1053 oldtab
->GetMainFrame()->
1054 ExecuteJavaScriptForTests(ASCIIToUTF16(dont_fork_popup
));
1056 // Wait for popup window to appear and finish navigating.
1057 popup_observer
.Wait();
1058 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1059 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
1060 EXPECT_TRUE(newtab
);
1061 EXPECT_NE(oldtab
, newtab
);
1062 nav_observer
.Wait();
1063 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
1064 EXPECT_EQ(https_url
.spec(),
1065 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1067 // Popup window should still be in the opener's process.
1068 content::RenderProcessHost
* popup_process
=
1069 newtab
->GetRenderProcessHost();
1070 EXPECT_EQ(process
, popup_process
);
1072 // Same thing if the current tab tries to navigate itself.
1073 std::string navigate_str
= "document.location=\"";
1074 navigate_str
+= https_url
.spec();
1075 navigate_str
+= "\";";
1077 content::WindowedNotificationObserver
nav_observer2(
1078 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1079 content::NotificationService::AllSources());
1080 oldtab
->GetMainFrame()->ExecuteJavaScriptForTests(ASCIIToUTF16(navigate_str
));
1081 nav_observer2
.Wait();
1082 ASSERT_TRUE(oldtab
->GetController().GetLastCommittedEntry());
1083 EXPECT_EQ(https_url
.spec(),
1084 oldtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1086 // Original window should still be in the original process.
1087 content::RenderProcessHost
* new_process
= newtab
->GetRenderProcessHost();
1088 EXPECT_EQ(process
, new_process
);
1091 // Test that get_process_idle_time() returns reasonable values when compared
1092 // with time deltas measured locally.
1093 IN_PROC_BROWSER_TEST_F(BrowserTest
, RenderIdleTime
) {
1094 base::TimeTicks start
= base::TimeTicks::Now();
1095 ui_test_utils::NavigateToURL(
1096 browser(), ui_test_utils::GetTestUrl(
1097 base::FilePath(base::FilePath::kCurrentDirectory
),
1098 base::FilePath(kTitle1File
)));
1099 content::RenderProcessHost::iterator
it(
1100 content::RenderProcessHost::AllHostsIterator());
1101 for (; !it
.IsAtEnd(); it
.Advance()) {
1102 base::TimeDelta renderer_td
=
1103 it
.GetCurrentValue()->GetChildProcessIdleTime();
1104 base::TimeDelta browser_td
= base::TimeTicks::Now() - start
;
1105 EXPECT_TRUE(browser_td
>= renderer_td
);
1109 // Test IDC_CREATE_SHORTCUTS command is enabled for url scheme file, ftp, http
1110 // and https and disabled for chrome://, about:// etc.
1111 // TODO(pinkerton): Disable app-mode in the model until we implement it
1112 // on the Mac. http://crbug.com/13148
1113 #if !defined(OS_MACOSX)
1114 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFile
) {
1115 CommandUpdater
* command_updater
=
1116 browser()->command_controller()->command_updater();
1118 static const base::FilePath::CharType
* kEmptyFile
=
1119 FILE_PATH_LITERAL("empty.html");
1120 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1121 base::FilePath::kCurrentDirectory
), base::FilePath(kEmptyFile
)));
1122 ASSERT_TRUE(file_url
.SchemeIs(url::kFileScheme
));
1123 ui_test_utils::NavigateToURL(browser(), file_url
);
1124 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1127 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttp
) {
1128 CommandUpdater
* command_updater
=
1129 browser()->command_controller()->command_updater();
1131 ASSERT_TRUE(test_server()->Start());
1132 GURL
http_url(test_server()->GetURL(std::string()));
1133 ASSERT_TRUE(http_url
.SchemeIs(url::kHttpScheme
));
1134 ui_test_utils::NavigateToURL(browser(), http_url
);
1135 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1138 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttps
) {
1139 CommandUpdater
* command_updater
=
1140 browser()->command_controller()->command_updater();
1142 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1143 net::SpawnedTestServer::kLocalhost
,
1144 base::FilePath(kDocRoot
));
1145 ASSERT_TRUE(test_server
.Start());
1146 GURL
https_url(test_server
.GetURL("/"));
1147 ASSERT_TRUE(https_url
.SchemeIs(url::kHttpsScheme
));
1148 ui_test_utils::NavigateToURL(browser(), https_url
);
1149 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1152 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFtp
) {
1153 CommandUpdater
* command_updater
=
1154 browser()->command_controller()->command_updater();
1156 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_FTP
,
1157 net::SpawnedTestServer::kLocalhost
,
1158 base::FilePath(kDocRoot
));
1159 ASSERT_TRUE(test_server
.Start());
1160 GURL
ftp_url(test_server
.GetURL(std::string()));
1161 ASSERT_TRUE(ftp_url
.SchemeIs(url::kFtpScheme
));
1162 ui_test_utils::NavigateToURL(browser(), ftp_url
);
1163 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1166 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutInvalid
) {
1167 CommandUpdater
* command_updater
=
1168 browser()->command_controller()->command_updater();
1170 // Urls that should not have shortcuts.
1171 GURL
new_tab_url(chrome::kChromeUINewTabURL
);
1172 ui_test_utils::NavigateToURL(browser(), new_tab_url
);
1173 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1175 GURL
history_url(chrome::kChromeUIHistoryURL
);
1176 ui_test_utils::NavigateToURL(browser(), history_url
);
1177 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1179 GURL
downloads_url(chrome::kChromeUIDownloadsURL
);
1180 ui_test_utils::NavigateToURL(browser(), downloads_url
);
1181 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1183 GURL
blank_url(url::kAboutBlankURL
);
1184 ui_test_utils::NavigateToURL(browser(), blank_url
);
1185 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1188 // Change a tab into an application window.
1189 // DISABLED: http://crbug.com/72310
1190 IN_PROC_BROWSER_TEST_F(BrowserTest
, DISABLED_ConvertTabToAppShortcut
) {
1191 ASSERT_TRUE(test_server()->Start());
1192 GURL
http_url(test_server()->GetURL(std::string()));
1193 ASSERT_TRUE(http_url
.SchemeIs(url::kHttpScheme
));
1195 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1196 WebContents
* initial_tab
= browser()->tab_strip_model()->GetWebContentsAt(0);
1197 WebContents
* app_tab
= chrome::AddSelectedTabWithURL(
1198 browser(), http_url
, content::PAGE_TRANSITION_TYPED
);
1199 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1200 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
1201 browser()->host_desktop_type()));
1203 // Normal tabs should accept load drops.
1204 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1205 EXPECT_TRUE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1207 // Turn |app_tab| into a tab in an app panel.
1208 chrome::ConvertTabToAppWindow(browser(), app_tab
);
1210 // The launch should have created a new browser.
1211 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1212 browser()->host_desktop_type()));
1214 // Find the new browser.
1215 Browser
* app_browser
= NULL
;
1216 for (chrome::BrowserIterator it
; !it
.done() && !app_browser
; it
.Next()) {
1217 if (*it
!= browser())
1220 ASSERT_TRUE(app_browser
);
1222 // Check that the tab contents is in the new browser, and not in the old.
1223 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1224 ASSERT_EQ(initial_tab
, browser()->tab_strip_model()->GetWebContentsAt(0));
1226 // Check that the appliaction browser has a single tab, and that tab contains
1227 // the content that we app-ified.
1228 ASSERT_EQ(1, app_browser
->tab_strip_model()->count());
1229 ASSERT_EQ(app_tab
, app_browser
->tab_strip_model()->GetWebContentsAt(0));
1231 // Normal tabs should accept load drops.
1232 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1234 // The tab in an app window should not.
1235 EXPECT_FALSE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1238 #endif // !defined(OS_MACOSX)
1240 // Test RenderView correctly send back favicon url for web page that redirects
1241 // to an anchor in javascript body.onload handler.
1242 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1243 DISABLED_FaviconOfOnloadRedirectToAnchorPage
) {
1244 ASSERT_TRUE(test_server()->Start());
1245 GURL
url(test_server()->GetURL("files/onload_redirect_to_anchor.html"));
1246 GURL
expected_favicon_url(test_server()->GetURL("files/test.png"));
1248 ui_test_utils::NavigateToURL(browser(), url
);
1250 NavigationEntry
* entry
= browser()->tab_strip_model()->
1251 GetActiveWebContents()->GetController().GetLastCommittedEntry();
1252 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1255 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined (OS_WIN)
1256 // http://crbug.com/83828. On Mac 10.6, the failure rate is 14%
1257 #define MAYBE_FaviconChange DISABLED_FaviconChange
1259 #define MAYBE_FaviconChange FaviconChange
1261 // Test that an icon can be changed from JS.
1262 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_FaviconChange
) {
1263 static const base::FilePath::CharType
* kFile
=
1264 FILE_PATH_LITERAL("onload_change_favicon.html");
1265 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1266 base::FilePath::kCurrentDirectory
), base::FilePath(kFile
)));
1267 ASSERT_TRUE(file_url
.SchemeIs(url::kFileScheme
));
1268 ui_test_utils::NavigateToURL(browser(), file_url
);
1270 NavigationEntry
* entry
= browser()->tab_strip_model()->
1271 GetActiveWebContents()->GetController().GetLastCommittedEntry();
1272 static const base::FilePath::CharType
* kIcon
=
1273 FILE_PATH_LITERAL("test1.png");
1274 GURL
expected_favicon_url(ui_test_utils::GetTestUrl(base::FilePath(
1275 base::FilePath::kCurrentDirectory
), base::FilePath(kIcon
)));
1276 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1279 // http://crbug.com/172336
1281 #define MAYBE_TabClosingWhenRemovingExtension \
1282 DISABLED_TabClosingWhenRemovingExtension
1284 #define MAYBE_TabClosingWhenRemovingExtension TabClosingWhenRemovingExtension
1286 // Makes sure TabClosing is sent when uninstalling an extension that is an app
1288 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_TabClosingWhenRemovingExtension
) {
1289 ASSERT_TRUE(test_server()->Start());
1290 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1291 GURL
url(test_server()->GetURL("empty.html"));
1292 TabStripModel
* model
= browser()->tab_strip_model();
1294 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1296 const Extension
* extension_app
= GetExtension();
1298 ui_test_utils::NavigateToURL(browser(), url
);
1300 WebContents
* app_contents
= WebContents::Create(
1301 WebContents::CreateParams(browser()->profile()));
1302 extensions::TabHelper::CreateForWebContents(app_contents
);
1303 extensions::TabHelper
* extensions_tab_helper
=
1304 extensions::TabHelper::FromWebContents(app_contents
);
1305 extensions_tab_helper
->SetExtensionApp(extension_app
);
1307 model
->AddWebContents(app_contents
, 0, content::PageTransitionFromInt(0),
1308 TabStripModel::ADD_NONE
);
1309 model
->SetTabPinned(0, true);
1310 ui_test_utils::NavigateToURL(browser(), url
);
1312 MockTabStripModelObserver observer
;
1313 model
->AddObserver(&observer
);
1315 // Uninstall the extension and make sure TabClosing is sent.
1316 ExtensionService
* service
= extensions::ExtensionSystem::Get(
1317 browser()->profile())->extension_service();
1318 service
->UninstallExtension(GetExtension()->id(),
1319 extensions::UNINSTALL_REASON_FOR_TESTING
,
1320 base::Bind(&base::DoNothing
),
1322 EXPECT_EQ(1, observer
.closing_count());
1324 model
->RemoveObserver(&observer
);
1326 // There should only be one tab now.
1327 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1330 #if !defined(OS_MACOSX)
1331 // Open with --app-id=<id>, and see that an app window opens.
1332 IN_PROC_BROWSER_TEST_F(BrowserTest
, AppIdSwitch
) {
1333 ASSERT_TRUE(test_server()->Start());
1336 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1337 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1338 const Extension
* extension_app
= GetExtension();
1340 CommandLine
command_line(CommandLine::NO_PROGRAM
);
1341 command_line
.AppendSwitchASCII(switches::kAppId
, extension_app
->id());
1343 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1344 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1345 StartupBrowserCreatorImpl
launch(base::FilePath(), command_line
, first_run
);
1346 ASSERT_TRUE(launch
.OpenApplicationWindow(browser()->profile(), NULL
));
1348 // Check that the new browser has an app name.
1349 // The launch should have created a new browser.
1350 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1351 browser()->host_desktop_type()));
1353 // Find the new browser.
1354 Browser
* new_browser
= NULL
;
1355 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1356 if (*it
!= browser())
1359 ASSERT_TRUE(new_browser
);
1360 ASSERT_TRUE(new_browser
!= browser());
1362 // The browser's app_name should include the app's ID.
1364 new_browser
->app_name_
.find(extension_app
->id()),
1365 std::string::npos
) << new_browser
->app_name_
;
1368 // Open an app window and the dev tools window and ensure that the location
1369 // bar settings are correct.
1370 IN_PROC_BROWSER_TEST_F(BrowserTest
, ShouldShowLocationBar
) {
1371 ASSERT_TRUE(test_server()->Start());
1374 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1375 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1376 const Extension
* extension_app
= GetExtension();
1378 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1379 WebContents
* app_window
=
1380 OpenApplication(AppLaunchParams(browser()->profile(),
1382 extensions::LAUNCH_CONTAINER_WINDOW
,
1384 ASSERT_TRUE(app_window
);
1386 DevToolsWindow
* devtools_window
=
1387 DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), false);
1389 // The launch should have created a new app browser and a dev tools browser.
1391 chrome::GetBrowserCount(browser()->profile(),
1392 browser()->host_desktop_type()));
1394 // Find the new browsers.
1395 Browser
* app_browser
= NULL
;
1396 Browser
* dev_tools_browser
= NULL
;
1397 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
1398 if (*it
== browser()) {
1400 } else if ((*it
)->app_name() == DevToolsWindow::kDevToolsApp
) {
1401 dev_tools_browser
= *it
;
1406 ASSERT_TRUE(dev_tools_browser
);
1407 ASSERT_TRUE(app_browser
);
1408 ASSERT_TRUE(app_browser
!= browser());
1411 dev_tools_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1413 app_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1415 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_window
);
1419 // Tests that the CLD (Compact Language Detection) works properly.
1420 IN_PROC_BROWSER_TEST_F(BrowserTest
, PageLanguageDetection
) {
1421 scoped_ptr
<test::CldDataHarness
> cld_data_harness
=
1422 test::CreateCldDataHarness();
1423 ASSERT_NO_FATAL_FAILURE(cld_data_harness
->Init());
1424 ASSERT_TRUE(test_server()->Start());
1426 translate::LanguageDetectionDetails details
;
1428 // Open a new tab with a page in English.
1429 AddTabAtIndex(0, GURL(test_server()->GetURL("files/english_page.html")),
1430 content::PAGE_TRANSITION_TYPED
);
1432 WebContents
* current_web_contents
=
1433 browser()->tab_strip_model()->GetActiveWebContents();
1434 ChromeTranslateClient
* chrome_translate_client
=
1435 ChromeTranslateClient::FromWebContents(current_web_contents
);
1436 content::Source
<WebContents
> source(current_web_contents
);
1438 ui_test_utils::WindowedNotificationObserverWithDetails
<
1439 translate::LanguageDetectionDetails
>
1440 en_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1443 chrome_translate_client
->GetLanguageState().original_language());
1444 en_language_detected_signal
.Wait();
1445 EXPECT_TRUE(en_language_detected_signal
.GetDetailsFor(
1446 source
.map_key(), &details
));
1447 EXPECT_EQ("en", details
.adopted_language
);
1449 chrome_translate_client
->GetLanguageState().original_language());
1451 // Now navigate to a page in French.
1452 ui_test_utils::WindowedNotificationObserverWithDetails
<
1453 translate::LanguageDetectionDetails
>
1454 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1456 ui_test_utils::NavigateToURL(
1457 browser(), GURL(test_server()->GetURL("files/french_page.html")));
1458 fr_language_detected_signal
.Wait();
1459 details
.adopted_language
.clear();
1460 EXPECT_TRUE(fr_language_detected_signal
.GetDetailsFor(
1461 source
.map_key(), &details
));
1462 EXPECT_EQ("fr", details
.adopted_language
);
1464 chrome_translate_client
->GetLanguageState().original_language());
1467 // Chromeos defaults to restoring the last session, so this test isn't
1469 #if !defined(OS_CHROMEOS)
1470 #if defined(OS_MACOSX)
1471 // Crashy, http://crbug.com/38522
1472 #define RestorePinnedTabs DISABLED_RestorePinnedTabs
1474 // Makes sure pinned tabs are restored correctly on start.
1475 IN_PROC_BROWSER_TEST_F(BrowserTest
, RestorePinnedTabs
) {
1476 ASSERT_TRUE(test_server()->Start());
1478 // Add an pinned app tab.
1479 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1480 GURL
url(test_server()->GetURL("empty.html"));
1481 TabStripModel
* model
= browser()->tab_strip_model();
1482 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1483 const Extension
* extension_app
= GetExtension();
1484 ui_test_utils::NavigateToURL(browser(), url
);
1485 WebContents
* app_contents
= WebContents::Create(
1486 WebContents::CreateParams(browser()->profile()));
1487 extensions::TabHelper::CreateForWebContents(app_contents
);
1488 extensions::TabHelper
* extensions_tab_helper
=
1489 extensions::TabHelper::FromWebContents(app_contents
);
1490 extensions_tab_helper
->SetExtensionApp(extension_app
);
1491 model
->AddWebContents(app_contents
, 0, content::PageTransitionFromInt(0),
1492 TabStripModel::ADD_NONE
);
1493 model
->SetTabPinned(0, true);
1494 ui_test_utils::NavigateToURL(browser(), url
);
1496 // Add a non pinned tab.
1497 chrome::NewTab(browser());
1499 // Add a pinned non-app tab.
1500 chrome::NewTab(browser());
1501 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL
));
1502 model
->SetTabPinned(2, true);
1504 // Write out the pinned tabs.
1505 PinnedTabCodec::WritePinnedTabs(browser()->profile());
1507 // Simulate launching again.
1508 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1509 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1510 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1511 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, first_run
);
1512 launch
.profile_
= browser()->profile();
1513 launch
.ProcessStartupURLs(std::vector
<GURL
>(),
1514 browser()->host_desktop_type());
1516 // The launch should have created a new browser.
1517 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1518 browser()->host_desktop_type()));
1520 // Find the new browser.
1521 Browser
* new_browser
= NULL
;
1522 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1523 if (*it
!= browser())
1526 ASSERT_TRUE(new_browser
);
1527 ASSERT_TRUE(new_browser
!= browser());
1529 // We should get back an additional tab for the app, and another for the
1530 // default home page.
1531 ASSERT_EQ(3, new_browser
->tab_strip_model()->count());
1533 // Make sure the state matches.
1534 TabStripModel
* new_model
= new_browser
->tab_strip_model();
1535 EXPECT_TRUE(new_model
->IsAppTab(0));
1536 EXPECT_FALSE(new_model
->IsAppTab(1));
1537 EXPECT_FALSE(new_model
->IsAppTab(2));
1539 EXPECT_TRUE(new_model
->IsTabPinned(0));
1540 EXPECT_TRUE(new_model
->IsTabPinned(1));
1541 EXPECT_FALSE(new_model
->IsTabPinned(2));
1543 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
1544 new_model
->GetWebContentsAt(2)->GetURL());
1547 extensions::TabHelper::FromWebContents(
1548 new_model
->GetWebContentsAt(0))->extension_app() == extension_app
);
1550 #endif // !defined(OS_CHROMEOS)
1552 // This test verifies we don't crash when closing the last window and the app
1554 IN_PROC_BROWSER_TEST_F(BrowserTest
, CloseWithAppMenuOpen
) {
1555 if (browser_defaults::kBrowserAliveWithNoWindows
)
1558 // We need a message loop running for menus on windows.
1559 base::MessageLoop::current()->PostTask(
1560 FROM_HERE
, base::Bind(&RunCloseWithAppMenuCallback
, browser()));
1563 #if !defined(OS_MACOSX)
1564 IN_PROC_BROWSER_TEST_F(BrowserTest
, OpenAppWindowLikeNtp
) {
1565 ASSERT_TRUE(test_server()->Start());
1568 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1569 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1570 const Extension
* extension_app
= GetExtension();
1572 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1573 WebContents
* app_window
= OpenApplication(
1574 AppLaunchParams(browser()->profile(), extension_app
,
1575 extensions::LAUNCH_CONTAINER_WINDOW
, NEW_WINDOW
));
1576 ASSERT_TRUE(app_window
);
1578 // Apps launched in a window from the NTP have an extensions tab helper but
1579 // do not have extension_app set in it.
1580 ASSERT_TRUE(extensions::TabHelper::FromWebContents(app_window
));
1582 extensions::TabHelper::FromWebContents(app_window
)->extension_app());
1583 EXPECT_EQ(extensions::AppLaunchInfo::GetFullLaunchURL(extension_app
),
1584 app_window
->GetURL());
1586 // The launch should have created a new browser.
1587 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1588 browser()->host_desktop_type()));
1590 // Find the new browser.
1591 Browser
* new_browser
= NULL
;
1592 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1593 if (*it
!= browser())
1596 ASSERT_TRUE(new_browser
);
1597 ASSERT_TRUE(new_browser
!= browser());
1599 EXPECT_TRUE(new_browser
->is_app());
1601 // The browser's app name should include the extension's id.
1602 std::string app_name
= new_browser
->app_name_
;
1603 EXPECT_NE(app_name
.find(extension_app
->id()), std::string::npos
)
1604 << "Name " << app_name
<< " should contain id "<< extension_app
->id();
1606 #endif // !defined(OS_MACOSX)
1608 // Makes sure the browser doesn't crash when
1609 // set_show_state(ui::SHOW_STATE_MAXIMIZED) has been invoked.
1610 IN_PROC_BROWSER_TEST_F(BrowserTest
, StartMaximized
) {
1611 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1612 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(types
); ++i
) {
1613 Browser::CreateParams
params(types
[i
], browser()->profile(),
1614 browser()->host_desktop_type());
1615 params
.initial_show_state
= ui::SHOW_STATE_MAXIMIZED
;
1616 AddBlankTabAndShow(new Browser(params
));
1620 // Aura doesn't support minimized window. crbug.com/104571.
1621 #if defined(USE_AURA)
1622 #define MAYBE_StartMinimized DISABLED_StartMinimized
1624 #define MAYBE_StartMinimized StartMinimized
1626 // Makes sure the browser doesn't crash when
1627 // set_show_state(ui::SHOW_STATE_MINIMIZED) has been invoked.
1628 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_StartMinimized
) {
1629 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1630 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(types
); ++i
) {
1631 Browser::CreateParams
params(types
[i
], browser()->profile(),
1632 browser()->host_desktop_type());
1633 params
.initial_show_state
= ui::SHOW_STATE_MINIMIZED
;
1634 AddBlankTabAndShow(new Browser(params
));
1638 // Makes sure the forward button is disabled immediately when navigating
1639 // forward to a slow-to-commit page.
1640 IN_PROC_BROWSER_TEST_F(BrowserTest
, ForwardDisabledOnForward
) {
1641 GURL
blank_url(url::kAboutBlankURL
);
1642 ui_test_utils::NavigateToURL(browser(), blank_url
);
1644 ui_test_utils::NavigateToURL(
1645 browser(), ui_test_utils::GetTestUrl(
1646 base::FilePath(base::FilePath::kCurrentDirectory
),
1647 base::FilePath(kTitle1File
)));
1649 content::WindowedNotificationObserver
back_nav_load_observer(
1650 content::NOTIFICATION_LOAD_STOP
,
1651 content::Source
<NavigationController
>(
1652 &browser()->tab_strip_model()->GetActiveWebContents()->
1654 chrome::GoBack(browser(), CURRENT_TAB
);
1655 back_nav_load_observer
.Wait();
1656 CommandUpdater
* command_updater
=
1657 browser()->command_controller()->command_updater();
1658 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1660 content::WindowedNotificationObserver
forward_nav_load_observer(
1661 content::NOTIFICATION_LOAD_STOP
,
1662 content::Source
<NavigationController
>(
1663 &browser()->tab_strip_model()->GetActiveWebContents()->
1665 chrome::GoForward(browser(), CURRENT_TAB
);
1666 // This check will happen before the navigation completes, since the browser
1667 // won't process the renderer's response until the Wait() call below.
1668 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1669 forward_nav_load_observer
.Wait();
1672 // Makes sure certain commands are disabled when Incognito mode is forced.
1673 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisableMenuItemsWhenIncognitoIsForced
) {
1674 CommandUpdater
* command_updater
=
1675 browser()->command_controller()->command_updater();
1676 // At the beginning, all commands are enabled.
1677 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1678 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1679 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1680 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1681 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1682 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1684 // Set Incognito to FORCED.
1685 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1686 IncognitoModePrefs::FORCED
);
1687 // Bookmarks & Settings commands should get disabled.
1688 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1689 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1690 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1691 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1692 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1693 // New Incognito Window command, however, should be enabled.
1694 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1696 // Create a new browser.
1697 Browser
* new_browser
=
1698 new Browser(Browser::CreateParams(
1699 browser()->profile()->GetOffTheRecordProfile(),
1700 browser()->host_desktop_type()));
1701 CommandUpdater
* new_command_updater
=
1702 new_browser
->command_controller()->command_updater();
1703 // It should have Bookmarks & Settings commands disabled by default.
1704 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1705 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(
1706 IDC_SHOW_BOOKMARK_MANAGER
));
1707 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1708 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1709 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1710 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1713 // Makes sure New Incognito Window command is disabled when Incognito mode is
1715 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1716 NoNewIncognitoWindowWhenIncognitoIsDisabled
) {
1717 CommandUpdater
* command_updater
=
1718 browser()->command_controller()->command_updater();
1719 // Set Incognito to DISABLED.
1720 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1721 IncognitoModePrefs::DISABLED
);
1722 // Make sure New Incognito Window command is disabled. All remaining commands
1723 // should be enabled.
1724 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1725 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1726 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1727 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1728 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1729 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1731 // Create a new browser.
1732 Browser
* new_browser
=
1733 new Browser(Browser::CreateParams(browser()->profile(),
1734 browser()->host_desktop_type()));
1735 CommandUpdater
* new_command_updater
=
1736 new_browser
->command_controller()->command_updater();
1737 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1738 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1739 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1740 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1741 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1742 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1745 // Makes sure Extensions and Settings commands are disabled in certain
1746 // circumstances even though normally they should stay enabled.
1747 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1748 DisableExtensionsAndSettingsWhenIncognitoIsDisabled
) {
1749 CommandUpdater
* command_updater
=
1750 browser()->command_controller()->command_updater();
1751 // Disable extensions. This should disable Extensions menu.
1752 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
1753 set_extensions_enabled(false);
1754 // Set Incognito to DISABLED.
1755 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1756 IncognitoModePrefs::DISABLED
);
1757 // Make sure Manage Extensions command is disabled.
1758 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1759 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1760 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1761 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1762 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1764 // Create a popup (non-main-UI-type) browser. Settings command as well
1765 // as Extensions should be disabled.
1766 Browser
* popup_browser
= new Browser(
1767 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1768 browser()->host_desktop_type()));
1769 CommandUpdater
* popup_command_updater
=
1770 popup_browser
->command_controller()->command_updater();
1771 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1772 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1773 EXPECT_TRUE(popup_command_updater
->IsCommandEnabled(
1774 IDC_SHOW_BOOKMARK_MANAGER
));
1775 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1778 // Makes sure Extensions and Settings commands are disabled in certain
1779 // circumstances even though normally they should stay enabled.
1780 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1781 DisableOptionsAndImportMenuItemsConsistently
) {
1782 // Create a popup browser.
1783 Browser
* popup_browser
= new Browser(
1784 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1785 browser()->host_desktop_type()));
1786 CommandUpdater
* command_updater
=
1787 popup_browser
->command_controller()->command_updater();
1788 // OPTIONS and IMPORT_SETTINGS are disabled for a non-normal UI.
1789 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1790 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1792 // Set Incognito to FORCED.
1793 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1794 IncognitoModePrefs::FORCED
);
1795 // OPTIONS and IMPORT_SETTINGS are disabled when Incognito is forced.
1796 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1797 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1798 // Set Incognito to AVAILABLE.
1799 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1800 IncognitoModePrefs::ENABLED
);
1801 // OPTIONS and IMPORT_SETTINGS are still disabled since it is a non-normal UI.
1802 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1803 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1808 void OnZoomLevelChanged(const base::Closure
& callback
,
1809 const HostZoomMap::ZoomLevelChange
& host
) {
1816 // Flakes regularly on Windows XP
1817 // http://crbug.com/146040
1818 #define MAYBE_PageZoom DISABLED_PageZoom
1820 #define MAYBE_PageZoom PageZoom
1825 int GetZoomPercent(const content::WebContents
* contents
,
1827 bool* enable_minus
) {
1828 int percent
= ZoomController::FromWebContents(contents
)->GetZoomPercent();
1829 *enable_plus
= percent
< contents
->GetMaximumZoomPercent();
1830 *enable_minus
= percent
> contents
->GetMinimumZoomPercent();
1836 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_PageZoom
) {
1837 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1838 bool enable_plus
, enable_minus
;
1841 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1842 new content::MessageLoopRunner
);
1843 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1844 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1845 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1846 content::HostZoomMap::GetDefaultForBrowserContext(
1847 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1848 chrome::Zoom(browser(), content::PAGE_ZOOM_IN
);
1851 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 110);
1852 EXPECT_TRUE(enable_plus
);
1853 EXPECT_TRUE(enable_minus
);
1857 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1858 new content::MessageLoopRunner
);
1859 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1860 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1861 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1862 content::HostZoomMap::GetDefaultForBrowserContext(
1863 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1864 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1867 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 100);
1868 EXPECT_TRUE(enable_plus
);
1869 EXPECT_TRUE(enable_minus
);
1873 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1874 new content::MessageLoopRunner
);
1875 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1876 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1877 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1878 content::HostZoomMap::GetDefaultForBrowserContext(
1879 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1880 chrome::Zoom(browser(), content::PAGE_ZOOM_OUT
);
1883 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 90);
1884 EXPECT_TRUE(enable_plus
);
1885 EXPECT_TRUE(enable_minus
);
1888 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1891 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCommandDisable
) {
1892 ASSERT_TRUE(test_server()->Start());
1893 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1894 GURL
url(test_server()->GetURL("empty.html"));
1895 ui_test_utils::NavigateToURL(browser(), url
);
1897 CommandUpdater
* command_updater
=
1898 browser()->command_controller()->command_updater();
1899 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1900 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1901 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1902 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1904 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1906 TestInterstitialPage
* interstitial
= NULL
;
1908 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1909 new content::MessageLoopRunner
);
1911 InterstitialObserver
observer(contents
,
1912 loop_runner
->QuitClosure(),
1914 interstitial
= new TestInterstitialPage(contents
, false, GURL());
1918 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1920 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1921 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1922 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1923 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1926 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1927 new content::MessageLoopRunner
);
1929 InterstitialObserver
observer(contents
,
1931 loop_runner
->QuitClosure());
1932 interstitial
->Proceed();
1934 // interstitial is deleted now.
1937 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1938 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1939 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1940 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1943 // Ensure that creating an interstitial page closes any JavaScript dialogs
1944 // that were present on the previous page. See http://crbug.com/295695.
1945 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialClosesDialogs
) {
1946 ASSERT_TRUE(test_server()->Start());
1947 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1948 GURL
url(test_server()->GetURL("empty.html"));
1949 ui_test_utils::NavigateToURL(browser(), url
);
1951 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1952 contents
->GetMainFrame()->ExecuteJavaScript(
1953 ASCIIToUTF16("alert('Dialog showing!');"));
1954 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
1955 EXPECT_TRUE(alert
->IsValid());
1956 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
1957 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
1959 TestInterstitialPage
* interstitial
= NULL
;
1961 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1962 new content::MessageLoopRunner
);
1964 InterstitialObserver
observer(contents
,
1965 loop_runner
->QuitClosure(),
1967 interstitial
= new TestInterstitialPage(contents
, false, GURL());
1971 // The interstitial should have closed the dialog.
1972 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1973 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
1976 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1977 new content::MessageLoopRunner
);
1979 InterstitialObserver
observer(contents
,
1981 loop_runner
->QuitClosure());
1982 interstitial
->DontProceed();
1984 // interstitial is deleted now.
1987 // Make sure input events still work in the renderer process.
1988 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
1992 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCloseTab
) {
1993 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1996 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1997 new content::MessageLoopRunner
);
1999 InterstitialObserver
observer(contents
,
2000 loop_runner
->QuitClosure(),
2002 // Interstitial will delete itself when we close the tab.
2003 new TestInterstitialPage(contents
, false, GURL());
2007 EXPECT_TRUE(contents
->ShowingInterstitialPage());
2010 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
2011 new content::MessageLoopRunner
);
2013 InterstitialObserver
observer(contents
,
2015 loop_runner
->QuitClosure());
2016 chrome::CloseTab(browser());
2018 // interstitial is deleted now.
2022 class MockWebContentsObserver
: public WebContentsObserver
{
2024 explicit MockWebContentsObserver(WebContents
* web_contents
)
2025 : WebContentsObserver(web_contents
),
2026 got_user_gesture_(false) {
2029 virtual void DidGetUserGesture() OVERRIDE
{
2030 got_user_gesture_
= true;
2033 bool got_user_gesture() const {
2034 return got_user_gesture_
;
2037 void set_got_user_gesture(bool got_it
) {
2038 got_user_gesture_
= got_it
;
2042 bool got_user_gesture_
;
2044 DISALLOW_COPY_AND_ASSIGN(MockWebContentsObserver
);
2047 IN_PROC_BROWSER_TEST_F(BrowserTest
, UserGesturesReported
) {
2048 // Regression test for http://crbug.com/110707. Also tests that a user
2049 // gesture is sent when a normal navigation (via e.g. the omnibox) is
2051 WebContents
* web_contents
=
2052 browser()->tab_strip_model()->GetActiveWebContents();
2053 MockWebContentsObserver
mock_observer(web_contents
);
2055 ASSERT_TRUE(test_server()->Start());
2056 GURL
url(test_server()->GetURL("empty.html"));
2058 ui_test_utils::NavigateToURL(browser(), url
);
2059 EXPECT_TRUE(mock_observer
.got_user_gesture());
2061 mock_observer
.set_got_user_gesture(false);
2062 chrome::Reload(browser(), CURRENT_TAB
);
2063 EXPECT_TRUE(mock_observer
.got_user_gesture());
2066 // TODO(ben): this test was never enabled. It has bit-rotted since being added.
2067 // It originally lived in browser_unittest.cc, but has been moved here to make
2068 // room for real browser unit tests.
2070 class BrowserTest2
: public InProcessBrowserTest
{
2073 host_resolver_proc_
= new net::RuleBasedHostResolverProc(NULL
);
2074 // Avoid making external DNS lookups. In this test we don't need this
2076 host_resolver_proc_
->AddSimulatedFailure("*.google.com");
2077 scoped_host_resolver_proc_
.Init(host_resolver_proc_
.get());
2081 scoped_refptr
<net::RuleBasedHostResolverProc
> host_resolver_proc_
;
2082 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_
;
2085 IN_PROC_BROWSER_TEST_F(BrowserTest2
, NoTabsInPopups
) {
2086 chrome::RegisterAppPrefs(L
"Test");
2088 // We start with a normal browser with one tab.
2089 EXPECT_EQ(1, browser()->tab_strip_model()->count());
2091 // Open a popup browser with a single blank foreground tab.
2092 Browser
* popup_browser
= new Browser(
2093 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile()));
2094 chrome::AddTabAt(popup_browser
, GURL(), -1, true);
2095 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2097 // Now try opening another tab in the popup browser.
2098 AddTabWithURLParams
params1(url
, content::PAGE_TRANSITION_TYPED
);
2099 popup_browser
->AddTabWithURL(¶ms1
);
2100 EXPECT_EQ(popup_browser
, params1
.target
);
2102 // The popup should still only have one tab.
2103 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2105 // The normal browser should now have two.
2106 EXPECT_EQ(2, browser()->tab_strip_model()->count());
2108 // Open an app frame browser with a single blank foreground tab.
2109 Browser
* app_browser
= new Browser(Browser::CreateParams::CreateForApp(
2110 L
"Test", browser()->profile(), false));
2111 chrome::AddTabAt(app_browser
, GURL(), -1, true);
2112 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2114 // Now try opening another tab in the app browser.
2115 AddTabWithURLParams
params2(GURL(url::kAboutBlankURL
),
2116 content::PAGE_TRANSITION_TYPED
);
2117 app_browser
->AddTabWithURL(¶ms2
);
2118 EXPECT_EQ(app_browser
, params2
.target
);
2120 // The popup should still only have one tab.
2121 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2123 // The normal browser should now have three.
2124 EXPECT_EQ(3, browser()->tab_strip_model()->count());
2126 // Open an app frame popup browser with a single blank foreground tab.
2127 Browser
* app_popup_browser
= new Browser(Browser::CreateParams::CreateForApp(
2128 L
"Test", browser()->profile(), false));
2129 chrome::AddTabAt(app_popup_browser
, GURL(), -1, true);
2130 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2132 // Now try opening another tab in the app popup browser.
2133 AddTabWithURLParams
params3(GURL(url::kAboutBlankURL
),
2134 content::PAGE_TRANSITION_TYPED
);
2135 app_popup_browser
->AddTabWithURL(¶ms3
);
2136 EXPECT_EQ(app_popup_browser
, params3
.target
);
2138 // The popup should still only have one tab.
2139 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2141 // The normal browser should now have four.
2142 EXPECT_EQ(4, browser()->tab_strip_model()->count());
2144 // Close the additional browsers.
2145 popup_browser
->tab_strip_model()->CloseAllTabs();
2146 app_browser
->tab_strip_model()->CloseAllTabs();
2147 app_popup_browser
->tab_strip_model()->CloseAllTabs();
2151 IN_PROC_BROWSER_TEST_F(BrowserTest
, WindowOpenClose
) {
2152 CommandLine::ForCurrentProcess()->AppendSwitch(
2153 switches::kDisablePopupBlocking
);
2154 GURL url
= ui_test_utils::GetTestUrl(
2155 base::FilePath(), base::FilePath().AppendASCII("window.close.html"));
2157 base::string16 title
= ASCIIToUTF16("Title Of Awesomeness");
2158 content::TitleWatcher
title_watcher(
2159 browser()->tab_strip_model()->GetActiveWebContents(), title
);
2160 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url
, 2);
2161 EXPECT_EQ(title
, title_watcher
.WaitAndGetTitle());
2164 // TODO(linux_aura) http://crbug.com/163931
2165 // Mac disabled: http://crbug.com/169820
2166 #if !defined(OS_MACOSX) && \
2167 !(defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
2168 IN_PROC_BROWSER_TEST_F(BrowserTest
, FullscreenBookmarkBar
) {
2169 #if defined(OS_WIN) && defined(USE_ASH)
2170 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2171 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2175 chrome::ToggleBookmarkBar(browser());
2176 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2177 chrome::ToggleFullscreenMode(browser());
2178 EXPECT_TRUE(browser()->window()->IsFullscreen());
2179 #if defined(OS_MACOSX)
2180 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2181 #elif defined(OS_CHROMEOS)
2182 // TODO(jamescook): If immersive fullscreen is disabled by default, test
2183 // for BookmarkBar::HIDDEN.
2184 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2186 EXPECT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2191 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisallowFileUrlUniversalAccessTest
) {
2192 GURL url
= ui_test_utils::GetTestUrl(
2194 base::FilePath().AppendASCII("fileurl_universalaccess.html"));
2196 base::string16
expected_title(ASCIIToUTF16("Disallowed"));
2197 content::TitleWatcher
title_watcher(
2198 browser()->tab_strip_model()->GetActiveWebContents(), expected_title
);
2199 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("Allowed"));
2200 ui_test_utils::NavigateToURL(browser(), url
);
2201 ASSERT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
2204 class KioskModeTest
: public BrowserTest
{
2208 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2209 command_line
->AppendSwitch(switches::kKioskMode
);
2213 #if defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
2214 // Mac: http://crbug.com/103912
2215 // Linux: http://crbug.com/163931
2216 #define MAYBE_EnableKioskModeTest DISABLED_EnableKioskModeTest
2218 #define MAYBE_EnableKioskModeTest EnableKioskModeTest
2220 IN_PROC_BROWSER_TEST_F(KioskModeTest
, MAYBE_EnableKioskModeTest
) {
2221 // Check if browser is in fullscreen mode.
2222 ASSERT_TRUE(browser()->window()->IsFullscreen());
2223 ASSERT_FALSE(browser()->window()->IsFullscreenBubbleVisible());
2227 // This test verifies that Chrome can be launched with a user-data-dir path
2228 // which contains non ASCII characters.
2229 class LaunchBrowserWithNonAsciiUserDatadir
: public BrowserTest
{
2231 LaunchBrowserWithNonAsciiUserDatadir() {}
2233 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2234 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
2235 base::FilePath tmp_profile
= temp_dir_
.path().AppendASCII("tmp_profile");
2236 tmp_profile
= tmp_profile
.Append(L
"Test Chrome G\u00E9raldine");
2238 ASSERT_TRUE(base::CreateDirectory(tmp_profile
));
2239 command_line
->AppendSwitchPath(switches::kUserDataDir
, tmp_profile
);
2242 base::ScopedTempDir temp_dir_
;
2245 IN_PROC_BROWSER_TEST_F(LaunchBrowserWithNonAsciiUserDatadir
,
2246 TestNonAsciiUserDataDir
) {
2247 // Verify that the window is present.
2248 ASSERT_TRUE(browser());
2250 #endif // defined(OS_WIN)
2252 // Tests to ensure that the browser continues running in the background after
2253 // the last window closes.
2254 class RunInBackgroundTest
: public BrowserTest
{
2256 RunInBackgroundTest() {}
2258 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2259 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2263 IN_PROC_BROWSER_TEST_F(RunInBackgroundTest
, RunInBackgroundBasicTest
) {
2264 // Close the browser window, then open a new one - the browser should keep
2266 Profile
* profile
= browser()->profile();
2267 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2268 content::WindowedNotificationObserver
observer(
2269 chrome::NOTIFICATION_BROWSER_CLOSED
,
2270 content::Source
<Browser
>(browser()));
2271 chrome::CloseWindow(browser());
2273 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2275 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2276 chrome::NewEmptyWindow(profile
, chrome::GetActiveDesktop());
2277 browser_added_observer
.WaitForSingleNewBrowser();
2279 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2282 // Tests to ensure that the browser continues running in the background after
2283 // the last window closes.
2284 class NoStartupWindowTest
: public BrowserTest
{
2286 NoStartupWindowTest() {}
2288 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2289 command_line
->AppendSwitch(switches::kNoStartupWindow
);
2290 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2294 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, NoStartupWindowBasicTest
) {
2295 #if defined(OS_WIN) && defined(USE_ASH)
2296 // kNoStartupWindow doesn't make sense in Metro+Ash.
2297 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2301 // No browser window should be started by default.
2302 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2304 // Starting a browser window should work just fine.
2305 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2306 CreateBrowser(ProfileManager::GetActiveUserProfile());
2307 browser_added_observer
.WaitForSingleNewBrowser();
2309 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2312 // Chromeos needs to track app windows because it considers them to be part of
2314 #if !defined(OS_CHROMEOS)
2315 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, DontInitSessionServiceForApps
) {
2316 #if defined(OS_WIN) && defined(USE_ASH)
2317 // kNoStartupWindow doesn't make sense in Metro+Ash.
2318 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2322 Profile
* profile
= ProfileManager::GetActiveUserProfile();
2324 SessionService
* session_service
=
2325 SessionServiceFactory::GetForProfile(profile
);
2326 ASSERT_FALSE(session_service
->processed_any_commands());
2328 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2329 CreateBrowserForApp("blah", profile
);
2330 browser_added_observer
.WaitForSingleNewBrowser();
2332 ASSERT_FALSE(session_service
->processed_any_commands());
2334 #endif // !defined(OS_CHROMEOS)
2336 // This test needs to be placed outside the anonymous namespace because we
2337 // need to access private type of Browser.
2338 class AppModeTest
: public BrowserTest
{
2342 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2343 GURL url
= ui_test_utils::GetTestUrl(
2344 base::FilePath(), base::FilePath().AppendASCII("title1.html"));
2345 command_line
->AppendSwitchASCII(switches::kApp
, url
.spec());
2349 IN_PROC_BROWSER_TEST_F(AppModeTest
, EnableAppModeTest
) {
2350 #if defined(OS_WIN) && defined(USE_ASH)
2351 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2352 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2356 // Test that an application browser window loads correctly.
2358 // Verify the browser is in application mode.
2359 EXPECT_TRUE(browser()->is_app());
2362 // Confirm chrome://version contains some expected content.
2363 IN_PROC_BROWSER_TEST_F(BrowserTest
, AboutVersion
) {
2364 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL
));
2365 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
2366 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("WebKit"), true, true,
2369 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("OS"), true, true,
2372 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("JavaScript"), true,
2377 static const base::FilePath::CharType
* kTestDir
=
2378 FILE_PATH_LITERAL("click_modifier");
2379 static const char kFirstPageTitle
[] = "First window";
2380 static const char kSecondPageTitle
[] = "New window!";
2382 class ClickModifierTest
: public InProcessBrowserTest
{
2384 ClickModifierTest() {
2387 // Returns a url that opens a new window or tab when clicked, via javascript.
2388 GURL
GetWindowOpenURL() {
2389 return ui_test_utils::GetTestUrl(
2390 base::FilePath(kTestDir
),
2391 base::FilePath(FILE_PATH_LITERAL("window_open.html")));
2394 // Returns a url that follows a simple link when clicked, unless affected by
2397 return ui_test_utils::GetTestUrl(
2398 base::FilePath(kTestDir
),
2399 base::FilePath(FILE_PATH_LITERAL("href.html")));
2402 base::string16
getFirstPageTitle() {
2403 return ASCIIToUTF16(kFirstPageTitle
);
2406 base::string16
getSecondPageTitle() {
2407 return ASCIIToUTF16(kSecondPageTitle
);
2410 // Loads our test page and simulates a single click using the supplied button
2411 // and modifiers. The click will cause either a navigation or the creation of
2412 // a new window or foreground or background tab. We verify that the expected
2413 // disposition occurs.
2414 void RunTest(Browser
* browser
,
2417 blink::WebMouseEvent::Button button
,
2418 WindowOpenDisposition disposition
) {
2419 ui_test_utils::NavigateToURL(browser
, url
);
2420 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2421 browser
->host_desktop_type()));
2422 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2423 content::WebContents
* web_contents
=
2424 browser
->tab_strip_model()->GetActiveWebContents();
2425 EXPECT_EQ(url
, web_contents
->GetURL());
2427 if (disposition
== CURRENT_TAB
) {
2428 content::WebContents
* web_contents
=
2429 browser
->tab_strip_model()->GetActiveWebContents();
2430 content::TestNavigationObserver
same_tab_observer(web_contents
);
2431 SimulateMouseClick(web_contents
, modifiers
, button
);
2432 same_tab_observer
.Wait();
2433 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2434 browser
->host_desktop_type()));
2435 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2436 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2440 content::WindowedNotificationObserver
observer(
2441 chrome::NOTIFICATION_TAB_ADDED
,
2442 content::NotificationService::AllSources());
2443 SimulateMouseClick(web_contents
, modifiers
, button
);
2446 if (disposition
== NEW_WINDOW
) {
2447 EXPECT_EQ(2u, chrome::GetBrowserCount(browser
->profile(),
2448 browser
->host_desktop_type()));
2452 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2453 browser
->host_desktop_type()));
2454 EXPECT_EQ(2, browser
->tab_strip_model()->count());
2455 web_contents
= browser
->tab_strip_model()->GetActiveWebContents();
2456 WaitForLoadStop(web_contents
);
2457 if (disposition
== NEW_FOREGROUND_TAB
) {
2458 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2460 ASSERT_EQ(NEW_BACKGROUND_TAB
, disposition
);
2461 EXPECT_EQ(getFirstPageTitle(), web_contents
->GetTitle());
2466 DISALLOW_COPY_AND_ASSIGN(ClickModifierTest
);
2469 // Tests for clicking on elements with handlers that run window.open.
2471 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenBasicClickTest
) {
2473 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2474 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2475 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2478 // TODO(ericu): Alt-click behavior on window.open is platform-dependent and not
2479 // well defined. Should we add tests so we know if it changes?
2481 // Shift-clicks open in a new window.
2482 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftClickTest
) {
2483 int modifiers
= blink::WebInputEvent::ShiftKey
;
2484 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2485 WindowOpenDisposition disposition
= NEW_WINDOW
;
2486 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2489 // Control-clicks open in a background tab.
2490 // On OSX meta [the command key] takes the place of control.
2491 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlClickTest
) {
2492 #if defined(OS_MACOSX)
2493 int modifiers
= blink::WebInputEvent::MetaKey
;
2495 int modifiers
= blink::WebInputEvent::ControlKey
;
2497 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2498 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2499 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2502 // Control-shift-clicks open in a foreground tab.
2503 // On OSX meta [the command key] takes the place of control.
2504 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlShiftClickTest
) {
2505 #if defined(OS_MACOSX)
2506 int modifiers
= blink::WebInputEvent::MetaKey
;
2508 int modifiers
= blink::WebInputEvent::ControlKey
;
2510 modifiers
|= blink::WebInputEvent::ShiftKey
;
2511 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2512 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2513 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2516 // Middle-clicks open in a background tab.
2517 #if defined(OS_LINUX)
2518 // http://crbug.com/396347
2519 #define MAYBE_WindowOpenMiddleClickTest DISABLED_WindowOpenMiddleClickTest
2521 #define MAYBE_WindowOpenMiddleClickTest WindowOpenMiddleClickTest
2523 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, MAYBE_WindowOpenMiddleClickTest
) {
2525 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2526 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2527 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2530 // Shift-middle-clicks open in a foreground tab.
2531 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftMiddleClickTest
) {
2532 int modifiers
= blink::WebInputEvent::ShiftKey
;
2533 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2534 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2535 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2538 // Tests for clicking on normal links.
2540 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefBasicClickTest
) {
2542 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2543 WindowOpenDisposition disposition
= CURRENT_TAB
;
2544 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2547 // TODO(ericu): Alt-click behavior on links is platform-dependent and not well
2548 // defined. Should we add tests so we know if it changes?
2550 // Shift-clicks open in a new window.
2551 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefShiftClickTest
) {
2552 int modifiers
= blink::WebInputEvent::ShiftKey
;
2553 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2554 WindowOpenDisposition disposition
= NEW_WINDOW
;
2555 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2558 // Control-clicks open in a background tab.
2559 // On OSX meta [the command key] takes the place of control.
2560 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefControlClickTest
) {
2561 #if defined(OS_MACOSX)
2562 int modifiers
= blink::WebInputEvent::MetaKey
;
2564 int modifiers
= blink::WebInputEvent::ControlKey
;
2566 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2567 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2568 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2571 // Control-shift-clicks open in a foreground tab.
2572 // On OSX meta [the command key] takes the place of control.
2573 // http://crbug.com/396347
2574 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, DISABLED_HrefControlShiftClickTest
) {
2575 #if defined(OS_MACOSX)
2576 int modifiers
= blink::WebInputEvent::MetaKey
;
2578 int modifiers
= blink::WebInputEvent::ControlKey
;
2580 modifiers
|= blink::WebInputEvent::ShiftKey
;
2581 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2582 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2583 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2586 // Middle-clicks open in a background tab.
2587 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefMiddleClickTest
) {
2589 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2590 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2591 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2594 // Shift-middle-clicks open in a foreground tab.
2595 // http://crbug.com/396347
2596 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, DISABLED_HrefShiftMiddleClickTest
) {
2597 int modifiers
= blink::WebInputEvent::ShiftKey
;
2598 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2599 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2600 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2603 IN_PROC_BROWSER_TEST_F(BrowserTest
, GetSizeForNewRenderView
) {
2604 // The instant extended NTP has javascript that does not work with
2605 // ui_test_utils::NavigateToURL. The NTP rvh reloads when the browser tries
2606 // to navigate away from the page, which causes the WebContents to end up in
2607 // an inconsistent state. (is_loaded = true, last_commited_url=ntp,
2608 // visible_url=title1.html)
2609 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled
,
2611 ASSERT_TRUE(test_server()->Start());
2612 // Create an HTTPS server for cross-site transition.
2613 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
2614 net::SpawnedTestServer::kLocalhost
,
2615 base::FilePath(kDocRoot
));
2616 ASSERT_TRUE(https_test_server
.Start());
2619 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2620 ASSERT_EQ(BookmarkBar::DETACHED
, browser()->bookmark_bar_state());
2621 WebContents
* web_contents
=
2622 browser()->tab_strip_model()->GetActiveWebContents();
2623 content::RenderViewHost
* prev_rvh
= web_contents
->GetRenderViewHost();
2624 const int height_inset
=
2625 browser()->window()->GetRenderViewHeightInsetWithDetachedBookmarkBar();
2626 const gfx::Size initial_wcv_size
=
2627 web_contents
->GetContainerBounds().size();
2628 RenderViewSizeObserver
observer(web_contents
, browser()->window());
2630 // Navigate to a non-NTP page, without resizing WebContentsView.
2631 ui_test_utils::NavigateToURL(browser(),
2632 test_server()->GetURL("files/title1.html"));
2633 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2634 // A new RenderViewHost should be created.
2635 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2636 prev_rvh
= web_contents
->GetRenderViewHost();
2637 gfx::Size rwhv_create_size0
, rwhv_commit_size0
, wcv_commit_size0
;
2638 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2642 // The create height of RenderWidgetHostView should include the height inset.
2643 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2644 initial_wcv_size
.height() + height_inset
),
2646 // When a navigation entry is committed, the size of RenderWidgetHostView
2647 // should be the same as when it was first created.
2648 EXPECT_EQ(rwhv_create_size0
, rwhv_commit_size0
);
2649 // Sizes of the current RenderWidgetHostView and WebContentsView should not
2650 // change before and after WebContentsDelegate::DidNavigateMainFramePostCommit
2651 // (implemented by Browser); we obtain the sizes before PostCommit via
2652 // WebContentsObserver::NavigationEntryCommitted (implemented by
2653 // RenderViewSizeObserver).
2654 EXPECT_EQ(rwhv_commit_size0
,
2655 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2656 // The behavior differs between OSX and views.
2657 // In OSX, the wcv does not change size until after the commit, when the
2658 // bookmark bar disappears (correct).
2659 // In views, the wcv changes size at commit time.
2660 #if defined(OS_MACOSX)
2661 EXPECT_EQ(gfx::Size(wcv_commit_size0
.width(),
2662 wcv_commit_size0
.height() + height_inset
),
2663 web_contents
->GetContainerBounds().size());
2665 EXPECT_EQ(wcv_commit_size0
, web_contents
->GetContainerBounds().size());
2668 // Navigate to another non-NTP page, without resizing WebContentsView.
2669 ui_test_utils::NavigateToURL(browser(),
2670 https_test_server
.GetURL("files/title2.html"));
2671 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2672 // A new RenderVieHost should be created.
2673 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2674 gfx::Size rwhv_create_size1
, rwhv_commit_size1
, wcv_commit_size1
;
2675 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2679 EXPECT_EQ(rwhv_create_size1
, rwhv_commit_size1
);
2680 EXPECT_EQ(rwhv_commit_size1
,
2681 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2682 EXPECT_EQ(wcv_commit_size1
, web_contents
->GetContainerBounds().size());
2684 // Navigate from NTP to a non-NTP page, resizing WebContentsView while
2685 // navigation entry is pending.
2686 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2687 gfx::Size
wcv_resize_insets(1, 1);
2688 observer
.set_wcv_resize_insets(wcv_resize_insets
);
2689 ui_test_utils::NavigateToURL(browser(),
2690 test_server()->GetURL("files/title2.html"));
2691 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2692 gfx::Size rwhv_create_size2
, rwhv_commit_size2
, wcv_commit_size2
;
2693 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2698 // The behavior on OSX and Views is incorrect in this edge case, but they are
2699 // differently incorrect.
2700 // The behavior should be:
2701 // initial wcv size: (100,100) (to choose random numbers)
2702 // initial rwhv size: (100,140)
2703 // commit wcv size: (101, 101)
2704 // commit rwhv size: (101, 141)
2705 // final wcv size: (101, 141)
2706 // final rwhv size: (101, 141)
2708 // On OSX, the commit rwhv size is (101, 101)
2709 // On views, the commit wcv size is (101, 141)
2710 // All other sizes are correct.
2712 // The create height of RenderWidgetHostView should include the height inset.
2713 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2714 initial_wcv_size
.height() + height_inset
),
2716 gfx::Size
exp_commit_size(initial_wcv_size
);
2718 #if defined(OS_MACOSX)
2719 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2720 wcv_resize_insets
.height());
2722 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2723 wcv_resize_insets
.height() + height_inset
);
2725 EXPECT_EQ(exp_commit_size
, rwhv_commit_size2
);
2726 EXPECT_EQ(exp_commit_size
, wcv_commit_size2
);
2727 gfx::Size
exp_final_size(initial_wcv_size
);
2728 exp_final_size
.Enlarge(wcv_resize_insets
.width(),
2729 wcv_resize_insets
.height() + height_inset
);
2730 EXPECT_EQ(exp_final_size
,
2731 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2732 EXPECT_EQ(exp_final_size
, web_contents
->GetContainerBounds().size());