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/test/base/in_process_browser_test.h"
59 #include "chrome/test/base/test_switches.h"
60 #include "chrome/test/base/ui_test_utils.h"
61 #include "components/translate/core/browser/language_state.h"
62 #include "components/translate/core/common/language_detection_details.h"
63 #include "content/public/browser/favicon_status.h"
64 #include "content/public/browser/host_zoom_map.h"
65 #include "content/public/browser/interstitial_page.h"
66 #include "content/public/browser/interstitial_page_delegate.h"
67 #include "content/public/browser/navigation_entry.h"
68 #include "content/public/browser/notification_service.h"
69 #include "content/public/browser/render_frame_host.h"
70 #include "content/public/browser/render_process_host.h"
71 #include "content/public/browser/render_view_host.h"
72 #include "content/public/browser/render_widget_host_view.h"
73 #include "content/public/browser/resource_context.h"
74 #include "content/public/browser/web_contents.h"
75 #include "content/public/browser/web_contents_observer.h"
76 #include "content/public/common/frame_navigate_params.h"
77 #include "content/public/common/page_transition_types.h"
78 #include "content/public/common/renderer_preferences.h"
79 #include "content/public/common/url_constants.h"
80 #include "content/public/test/browser_test_utils.h"
81 #include "content/public/test/test_navigation_observer.h"
82 #include "extensions/browser/extension_system.h"
83 #include "extensions/browser/uninstall_reason.h"
84 #include "extensions/common/extension.h"
85 #include "extensions/common/extension_set.h"
86 #include "grit/chromium_strings.h"
87 #include "grit/generated_resources.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 #if defined(OS_MACOSX) || defined(OS_WIN)
788 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
789 DISABLED_SingleBeforeUnloadAfterWindowClose
791 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
792 SingleBeforeUnloadAfterWindowClose
795 // Test for crbug.com/11647. A page closed with window.close() should not have
796 // two beforeunload dialogs shown.
797 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_SingleBeforeUnloadAfterWindowClose
) {
798 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
799 ExecuteJavaScript(ASCIIToUTF16(kOpenNewBeforeUnloadPage
));
801 // Close the new window with JavaScript, which should show a single
802 // beforeunload dialog. Then show another alert, to make it easy to verify
803 // that a second beforeunload dialog isn't shown.
804 browser()->tab_strip_model()->GetWebContentsAt(0)->GetMainFrame()->
805 ExecuteJavaScript(ASCIIToUTF16("w.close(); alert('bar');"));
806 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
807 alert
->native_dialog()->AcceptAppModalDialog();
809 alert
= ui_test_utils::WaitForAppModalDialog();
810 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->
811 is_before_unload_dialog());
812 alert
->native_dialog()->AcceptAppModalDialog();
815 // BrowserTest.BeforeUnloadVsBeforeReload times out on Windows.
816 // http://crbug.com/130411
818 #define MAYBE_BeforeUnloadVsBeforeReload DISABLED_BeforeUnloadVsBeforeReload
820 #define MAYBE_BeforeUnloadVsBeforeReload BeforeUnloadVsBeforeReload
823 // Test that when a page has an onunload handler, reloading a page shows a
824 // different dialog than navigating to a different page.
825 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_BeforeUnloadVsBeforeReload
) {
826 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
827 ui_test_utils::NavigateToURL(browser(), url
);
829 // Reload the page, and check that we get a "before reload" dialog.
830 chrome::Reload(browser(), CURRENT_TAB
);
831 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
832 EXPECT_TRUE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
834 // Cancel the reload.
835 alert
->native_dialog()->CancelAppModalDialog();
837 // Navigate to another url, and check that we get a "before unload" dialog.
838 GURL
url2(url::kAboutBlankURL
);
839 browser()->OpenURL(OpenURLParams(
840 url2
, Referrer(), CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false));
842 alert
= ui_test_utils::WaitForAppModalDialog();
843 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
845 // Accept the navigation so we end up on a page without a beforeunload hook.
846 alert
->native_dialog()->AcceptAppModalDialog();
849 // BeforeUnloadAtQuitWithTwoWindows is a regression test for
850 // http://crbug.com/11842. It opens two windows, one of which has a
851 // beforeunload handler and attempts to exit cleanly.
852 class BeforeUnloadAtQuitWithTwoWindows
: public InProcessBrowserTest
{
854 // This test is for testing a specific shutdown behavior. This mimics what
855 // happens in InProcessBrowserTest::RunTestOnMainThread and QuitBrowsers, but
856 // ensures that it happens through the single IDC_EXIT of the test.
857 virtual void TearDownOnMainThread() OVERRIDE
{
858 // Cycle both the MessageLoop and the Cocoa runloop twice to flush out any
859 // Chrome work that generates Cocoa work. Do this twice since there are two
860 // Browsers that must be closed.
864 // Run the application event loop to completion, which will cycle the
865 // native MessagePump on all platforms.
866 base::MessageLoop::current()->PostTask(FROM_HERE
,
867 base::MessageLoop::QuitClosure());
868 base::MessageLoop::current()->Run();
870 // Take care of any remaining Cocoa work.
873 // At this point, quit should be for real now.
874 ASSERT_EQ(0u, chrome::GetTotalBrowserCount());
877 // A helper function that cycles the MessageLoop, and on Mac, the Cocoa run
878 // loop. It also drains the NSAutoreleasePool.
879 void CycleRunLoops() {
880 content::RunAllPendingInMessageLoop();
881 #if defined(OS_MACOSX)
882 chrome::testing::NSRunLoopRunAllPending();
883 AutoreleasePool()->Recycle();
888 // Disabled, http://crbug.com/159214 .
889 IN_PROC_BROWSER_TEST_F(BeforeUnloadAtQuitWithTwoWindows
,
890 DISABLED_IfThisTestTimesOutItIndicatesFAILURE
) {
891 // In the first browser, set up a page that has a beforeunload handler.
892 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
893 ui_test_utils::NavigateToURL(browser(), url
);
895 // Open a second browser window at about:blank.
896 ui_test_utils::BrowserAddedObserver browser_added_observer
;
897 chrome::NewEmptyWindow(browser()->profile(), chrome::GetActiveDesktop());
898 Browser
* second_window
= browser_added_observer
.WaitForSingleNewBrowser();
899 ui_test_utils::NavigateToURL(second_window
, GURL(url::kAboutBlankURL
));
901 // Tell the application to quit. IDC_EXIT calls AttemptUserExit, which on
902 // everything but ChromeOS allows unload handlers to block exit. On that
903 // platform, though, it exits unconditionally. See the comment and bug ID
904 // in AttemptUserExit() in application_lifetime.cc.
905 #if defined(OS_CHROMEOS)
906 chrome::AttemptExit();
908 chrome::ExecuteCommand(second_window
, IDC_EXIT
);
911 // The beforeunload handler will run at exit, ensure it does, and then accept
912 // it to allow shutdown to proceed.
913 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
916 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
917 alert
->native_dialog()->AcceptAppModalDialog();
919 // But wait there's more! If this test times out, it likely means that the
920 // browser has not been able to quit correctly, indicating there's a
921 // regression of the bug noted above.
924 // Test that scripts can fork a new renderer process for a cross-site popup,
925 // based on http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab.
926 // The script must open a new tab, set its window.opener to null, and navigate
927 // it to a cross-site URL. It should also work for meta-refreshes.
928 // See http://crbug.com/93517.
929 IN_PROC_BROWSER_TEST_F(BrowserTest
, NullOpenerRedirectForksProcess
) {
930 CommandLine::ForCurrentProcess()->AppendSwitch(
931 switches::kDisablePopupBlocking
);
933 // Create http and https servers for a cross-site transition.
934 ASSERT_TRUE(test_server()->Start());
935 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
936 net::SpawnedTestServer::kLocalhost
,
937 base::FilePath(kDocRoot
));
938 ASSERT_TRUE(https_test_server
.Start());
939 GURL
http_url(test_server()->GetURL("files/title1.html"));
940 GURL
https_url(https_test_server
.GetURL(std::string()));
942 // Start with an http URL.
943 ui_test_utils::NavigateToURL(browser(), http_url
);
944 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
945 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
947 // Now open a tab to a blank page, set its opener to null, and redirect it
949 std::string redirect_popup
= "w=window.open();";
950 redirect_popup
+= "w.opener=null;";
951 redirect_popup
+= "w.document.location=\"";
952 redirect_popup
+= https_url
.spec();
953 redirect_popup
+= "\";";
955 content::WindowedNotificationObserver
popup_observer(
956 chrome::NOTIFICATION_TAB_ADDED
,
957 content::NotificationService::AllSources());
958 content::WindowedNotificationObserver
nav_observer(
959 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
960 content::NotificationService::AllSources());
961 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(redirect_popup
));
963 // Wait for popup window to appear and finish navigating.
964 popup_observer
.Wait();
965 ASSERT_EQ(2, browser()->tab_strip_model()->count());
966 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
968 EXPECT_NE(oldtab
, newtab
);
970 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
971 EXPECT_EQ(https_url
.spec(),
972 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
974 // Popup window should not be in the opener's process.
975 content::RenderProcessHost
* popup_process
=
976 newtab
->GetRenderProcessHost();
977 EXPECT_NE(process
, popup_process
);
979 // Now open a tab to a blank page, set its opener to null, and use a
980 // meta-refresh to navigate it instead.
981 std::string refresh_popup
= "w=window.open();";
982 refresh_popup
+= "w.opener=null;";
983 refresh_popup
+= "w.document.write(";
984 refresh_popup
+= "'<META HTTP-EQUIV=\"refresh\" content=\"0; url=";
985 refresh_popup
+= https_url
.spec();
986 refresh_popup
+= "\">');w.document.close();";
988 content::WindowedNotificationObserver
popup_observer2(
989 chrome::NOTIFICATION_TAB_ADDED
,
990 content::NotificationService::AllSources());
991 content::WindowedNotificationObserver
nav_observer2(
992 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
993 content::NotificationService::AllSources());
994 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(refresh_popup
));
996 // Wait for popup window to appear and finish navigating.
997 popup_observer2
.Wait();
998 ASSERT_EQ(3, browser()->tab_strip_model()->count());
999 WebContents
* newtab2
= browser()->tab_strip_model()->GetActiveWebContents();
1000 EXPECT_TRUE(newtab2
);
1001 EXPECT_NE(oldtab
, newtab2
);
1002 nav_observer2
.Wait();
1003 ASSERT_TRUE(newtab2
->GetController().GetLastCommittedEntry());
1004 EXPECT_EQ(https_url
.spec(),
1005 newtab2
->GetController().GetLastCommittedEntry()->GetURL().spec());
1007 // This popup window should also not be in the opener's process.
1008 content::RenderProcessHost
* popup_process2
=
1009 newtab2
->GetRenderProcessHost();
1010 EXPECT_NE(process
, popup_process2
);
1013 // Tests that other popup navigations that do not follow the steps at
1014 // http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab will not
1015 // fork a new renderer process.
1016 IN_PROC_BROWSER_TEST_F(BrowserTest
, OtherRedirectsDontForkProcess
) {
1017 CommandLine::ForCurrentProcess()->AppendSwitch(
1018 switches::kDisablePopupBlocking
);
1020 // Create http and https servers for a cross-site transition.
1021 ASSERT_TRUE(test_server()->Start());
1022 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1023 net::SpawnedTestServer::kLocalhost
,
1024 base::FilePath(kDocRoot
));
1025 ASSERT_TRUE(https_test_server
.Start());
1026 GURL
http_url(test_server()->GetURL("files/title1.html"));
1027 GURL
https_url(https_test_server
.GetURL(std::string()));
1029 // Start with an http URL.
1030 ui_test_utils::NavigateToURL(browser(), http_url
);
1031 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
1032 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
1034 // Now open a tab to a blank page, set its opener to null, and redirect it
1036 std::string dont_fork_popup
= "w=window.open();";
1037 dont_fork_popup
+= "w.document.location=\"";
1038 dont_fork_popup
+= https_url
.spec();
1039 dont_fork_popup
+= "\";";
1041 content::WindowedNotificationObserver
popup_observer(
1042 chrome::NOTIFICATION_TAB_ADDED
,
1043 content::NotificationService::AllSources());
1044 content::WindowedNotificationObserver
nav_observer(
1045 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1046 content::NotificationService::AllSources());
1047 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(dont_fork_popup
));
1049 // Wait for popup window to appear and finish navigating.
1050 popup_observer
.Wait();
1051 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1052 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
1053 EXPECT_TRUE(newtab
);
1054 EXPECT_NE(oldtab
, newtab
);
1055 nav_observer
.Wait();
1056 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
1057 EXPECT_EQ(https_url
.spec(),
1058 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1060 // Popup window should still be in the opener's process.
1061 content::RenderProcessHost
* popup_process
=
1062 newtab
->GetRenderProcessHost();
1063 EXPECT_EQ(process
, popup_process
);
1065 // Same thing if the current tab tries to navigate itself.
1066 std::string navigate_str
= "document.location=\"";
1067 navigate_str
+= https_url
.spec();
1068 navigate_str
+= "\";";
1070 content::WindowedNotificationObserver
nav_observer2(
1071 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1072 content::NotificationService::AllSources());
1073 oldtab
->GetMainFrame()->ExecuteJavaScript(ASCIIToUTF16(navigate_str
));
1074 nav_observer2
.Wait();
1075 ASSERT_TRUE(oldtab
->GetController().GetLastCommittedEntry());
1076 EXPECT_EQ(https_url
.spec(),
1077 oldtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1079 // Original window should still be in the original process.
1080 content::RenderProcessHost
* new_process
= newtab
->GetRenderProcessHost();
1081 EXPECT_EQ(process
, new_process
);
1084 // Test that get_process_idle_time() returns reasonable values when compared
1085 // with time deltas measured locally.
1086 IN_PROC_BROWSER_TEST_F(BrowserTest
, RenderIdleTime
) {
1087 base::TimeTicks start
= base::TimeTicks::Now();
1088 ui_test_utils::NavigateToURL(
1089 browser(), ui_test_utils::GetTestUrl(
1090 base::FilePath(base::FilePath::kCurrentDirectory
),
1091 base::FilePath(kTitle1File
)));
1092 content::RenderProcessHost::iterator
it(
1093 content::RenderProcessHost::AllHostsIterator());
1094 for (; !it
.IsAtEnd(); it
.Advance()) {
1095 base::TimeDelta renderer_td
=
1096 it
.GetCurrentValue()->GetChildProcessIdleTime();
1097 base::TimeDelta browser_td
= base::TimeTicks::Now() - start
;
1098 EXPECT_TRUE(browser_td
>= renderer_td
);
1102 // Test IDC_CREATE_SHORTCUTS command is enabled for url scheme file, ftp, http
1103 // and https and disabled for chrome://, about:// etc.
1104 // TODO(pinkerton): Disable app-mode in the model until we implement it
1105 // on the Mac. http://crbug.com/13148
1106 #if !defined(OS_MACOSX)
1107 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFile
) {
1108 CommandUpdater
* command_updater
=
1109 browser()->command_controller()->command_updater();
1111 static const base::FilePath::CharType
* kEmptyFile
=
1112 FILE_PATH_LITERAL("empty.html");
1113 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1114 base::FilePath::kCurrentDirectory
), base::FilePath(kEmptyFile
)));
1115 ASSERT_TRUE(file_url
.SchemeIs(url::kFileScheme
));
1116 ui_test_utils::NavigateToURL(browser(), file_url
);
1117 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1120 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttp
) {
1121 CommandUpdater
* command_updater
=
1122 browser()->command_controller()->command_updater();
1124 ASSERT_TRUE(test_server()->Start());
1125 GURL
http_url(test_server()->GetURL(std::string()));
1126 ASSERT_TRUE(http_url
.SchemeIs(url::kHttpScheme
));
1127 ui_test_utils::NavigateToURL(browser(), http_url
);
1128 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1131 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttps
) {
1132 CommandUpdater
* command_updater
=
1133 browser()->command_controller()->command_updater();
1135 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1136 net::SpawnedTestServer::kLocalhost
,
1137 base::FilePath(kDocRoot
));
1138 ASSERT_TRUE(test_server
.Start());
1139 GURL
https_url(test_server
.GetURL("/"));
1140 ASSERT_TRUE(https_url
.SchemeIs(url::kHttpsScheme
));
1141 ui_test_utils::NavigateToURL(browser(), https_url
);
1142 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1145 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFtp
) {
1146 CommandUpdater
* command_updater
=
1147 browser()->command_controller()->command_updater();
1149 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_FTP
,
1150 net::SpawnedTestServer::kLocalhost
,
1151 base::FilePath(kDocRoot
));
1152 ASSERT_TRUE(test_server
.Start());
1153 GURL
ftp_url(test_server
.GetURL(std::string()));
1154 ASSERT_TRUE(ftp_url
.SchemeIs(url::kFtpScheme
));
1155 ui_test_utils::NavigateToURL(browser(), ftp_url
);
1156 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1159 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutInvalid
) {
1160 CommandUpdater
* command_updater
=
1161 browser()->command_controller()->command_updater();
1163 // Urls that should not have shortcuts.
1164 GURL
new_tab_url(chrome::kChromeUINewTabURL
);
1165 ui_test_utils::NavigateToURL(browser(), new_tab_url
);
1166 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1168 GURL
history_url(chrome::kChromeUIHistoryURL
);
1169 ui_test_utils::NavigateToURL(browser(), history_url
);
1170 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1172 GURL
downloads_url(chrome::kChromeUIDownloadsURL
);
1173 ui_test_utils::NavigateToURL(browser(), downloads_url
);
1174 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1176 GURL
blank_url(url::kAboutBlankURL
);
1177 ui_test_utils::NavigateToURL(browser(), blank_url
);
1178 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1181 // Change a tab into an application window.
1182 // DISABLED: http://crbug.com/72310
1183 IN_PROC_BROWSER_TEST_F(BrowserTest
, DISABLED_ConvertTabToAppShortcut
) {
1184 ASSERT_TRUE(test_server()->Start());
1185 GURL
http_url(test_server()->GetURL(std::string()));
1186 ASSERT_TRUE(http_url
.SchemeIs(url::kHttpScheme
));
1188 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1189 WebContents
* initial_tab
= browser()->tab_strip_model()->GetWebContentsAt(0);
1190 WebContents
* app_tab
= chrome::AddSelectedTabWithURL(
1191 browser(), http_url
, content::PAGE_TRANSITION_TYPED
);
1192 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1193 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
1194 browser()->host_desktop_type()));
1196 // Normal tabs should accept load drops.
1197 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1198 EXPECT_TRUE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1200 // Turn |app_tab| into a tab in an app panel.
1201 chrome::ConvertTabToAppWindow(browser(), app_tab
);
1203 // The launch should have created a new browser.
1204 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1205 browser()->host_desktop_type()));
1207 // Find the new browser.
1208 Browser
* app_browser
= NULL
;
1209 for (chrome::BrowserIterator it
; !it
.done() && !app_browser
; it
.Next()) {
1210 if (*it
!= browser())
1213 ASSERT_TRUE(app_browser
);
1215 // Check that the tab contents is in the new browser, and not in the old.
1216 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1217 ASSERT_EQ(initial_tab
, browser()->tab_strip_model()->GetWebContentsAt(0));
1219 // Check that the appliaction browser has a single tab, and that tab contains
1220 // the content that we app-ified.
1221 ASSERT_EQ(1, app_browser
->tab_strip_model()->count());
1222 ASSERT_EQ(app_tab
, app_browser
->tab_strip_model()->GetWebContentsAt(0));
1224 // Normal tabs should accept load drops.
1225 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1227 // The tab in an app window should not.
1228 EXPECT_FALSE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1231 #endif // !defined(OS_MACOSX)
1233 // Test RenderView correctly send back favicon url for web page that redirects
1234 // to an anchor in javascript body.onload handler.
1235 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1236 DISABLED_FaviconOfOnloadRedirectToAnchorPage
) {
1237 ASSERT_TRUE(test_server()->Start());
1238 GURL
url(test_server()->GetURL("files/onload_redirect_to_anchor.html"));
1239 GURL
expected_favicon_url(test_server()->GetURL("files/test.png"));
1241 ui_test_utils::NavigateToURL(browser(), url
);
1243 NavigationEntry
* entry
= browser()->tab_strip_model()->
1244 GetActiveWebContents()->GetController().GetLastCommittedEntry();
1245 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1248 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined (OS_WIN)
1249 // http://crbug.com/83828. On Mac 10.6, the failure rate is 14%
1250 #define MAYBE_FaviconChange DISABLED_FaviconChange
1252 #define MAYBE_FaviconChange FaviconChange
1254 // Test that an icon can be changed from JS.
1255 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_FaviconChange
) {
1256 static const base::FilePath::CharType
* kFile
=
1257 FILE_PATH_LITERAL("onload_change_favicon.html");
1258 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1259 base::FilePath::kCurrentDirectory
), base::FilePath(kFile
)));
1260 ASSERT_TRUE(file_url
.SchemeIs(url::kFileScheme
));
1261 ui_test_utils::NavigateToURL(browser(), file_url
);
1263 NavigationEntry
* entry
= browser()->tab_strip_model()->
1264 GetActiveWebContents()->GetController().GetLastCommittedEntry();
1265 static const base::FilePath::CharType
* kIcon
=
1266 FILE_PATH_LITERAL("test1.png");
1267 GURL
expected_favicon_url(ui_test_utils::GetTestUrl(base::FilePath(
1268 base::FilePath::kCurrentDirectory
), base::FilePath(kIcon
)));
1269 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1272 // http://crbug.com/172336
1274 #define MAYBE_TabClosingWhenRemovingExtension \
1275 DISABLED_TabClosingWhenRemovingExtension
1277 #define MAYBE_TabClosingWhenRemovingExtension TabClosingWhenRemovingExtension
1279 // Makes sure TabClosing is sent when uninstalling an extension that is an app
1281 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_TabClosingWhenRemovingExtension
) {
1282 ASSERT_TRUE(test_server()->Start());
1283 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1284 GURL
url(test_server()->GetURL("empty.html"));
1285 TabStripModel
* model
= browser()->tab_strip_model();
1287 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1289 const Extension
* extension_app
= GetExtension();
1291 ui_test_utils::NavigateToURL(browser(), url
);
1293 WebContents
* app_contents
= WebContents::Create(
1294 WebContents::CreateParams(browser()->profile()));
1295 extensions::TabHelper::CreateForWebContents(app_contents
);
1296 extensions::TabHelper
* extensions_tab_helper
=
1297 extensions::TabHelper::FromWebContents(app_contents
);
1298 extensions_tab_helper
->SetExtensionApp(extension_app
);
1300 model
->AddWebContents(app_contents
, 0, content::PageTransitionFromInt(0),
1301 TabStripModel::ADD_NONE
);
1302 model
->SetTabPinned(0, true);
1303 ui_test_utils::NavigateToURL(browser(), url
);
1305 MockTabStripModelObserver observer
;
1306 model
->AddObserver(&observer
);
1308 // Uninstall the extension and make sure TabClosing is sent.
1309 ExtensionService
* service
= extensions::ExtensionSystem::Get(
1310 browser()->profile())->extension_service();
1311 service
->UninstallExtension(
1312 GetExtension()->id(), extensions::UNINSTALL_REASON_FOR_TESTING
, NULL
);
1313 EXPECT_EQ(1, observer
.closing_count());
1315 model
->RemoveObserver(&observer
);
1317 // There should only be one tab now.
1318 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1321 #if !defined(OS_MACOSX)
1322 // Open with --app-id=<id>, and see that an app window opens.
1323 IN_PROC_BROWSER_TEST_F(BrowserTest
, AppIdSwitch
) {
1324 ASSERT_TRUE(test_server()->Start());
1327 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1328 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1329 const Extension
* extension_app
= GetExtension();
1331 CommandLine
command_line(CommandLine::NO_PROGRAM
);
1332 command_line
.AppendSwitchASCII(switches::kAppId
, extension_app
->id());
1334 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1335 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1336 StartupBrowserCreatorImpl
launch(base::FilePath(), command_line
, first_run
);
1337 ASSERT_TRUE(launch
.OpenApplicationWindow(browser()->profile(), NULL
));
1339 // Check that the new browser has an app name.
1340 // The launch should have created a new browser.
1341 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1342 browser()->host_desktop_type()));
1344 // Find the new browser.
1345 Browser
* new_browser
= NULL
;
1346 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1347 if (*it
!= browser())
1350 ASSERT_TRUE(new_browser
);
1351 ASSERT_TRUE(new_browser
!= browser());
1353 // The browser's app_name should include the app's ID.
1355 new_browser
->app_name_
.find(extension_app
->id()),
1356 std::string::npos
) << new_browser
->app_name_
;
1359 // Open an app window and the dev tools window and ensure that the location
1360 // bar settings are correct.
1361 IN_PROC_BROWSER_TEST_F(BrowserTest
, ShouldShowLocationBar
) {
1362 ASSERT_TRUE(test_server()->Start());
1365 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1366 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1367 const Extension
* extension_app
= GetExtension();
1369 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1370 WebContents
* app_window
=
1371 OpenApplication(AppLaunchParams(browser()->profile(),
1373 extensions::LAUNCH_CONTAINER_WINDOW
,
1375 ASSERT_TRUE(app_window
);
1377 DevToolsWindow
* devtools_window
=
1378 DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), false);
1380 // The launch should have created a new app browser and a dev tools browser.
1382 chrome::GetBrowserCount(browser()->profile(),
1383 browser()->host_desktop_type()));
1385 // Find the new browsers.
1386 Browser
* app_browser
= NULL
;
1387 Browser
* dev_tools_browser
= NULL
;
1388 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
1389 if (*it
== browser()) {
1391 } else if ((*it
)->app_name() == DevToolsWindow::kDevToolsApp
) {
1392 dev_tools_browser
= *it
;
1397 ASSERT_TRUE(dev_tools_browser
);
1398 ASSERT_TRUE(app_browser
);
1399 ASSERT_TRUE(app_browser
!= browser());
1402 dev_tools_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1404 app_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1406 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_window
);
1410 // Tests that the CLD (Compact Language Detection) works properly.
1411 IN_PROC_BROWSER_TEST_F(BrowserTest
, PageLanguageDetection
) {
1412 scoped_ptr
<test::CldDataHarness
> cld_data_harness
=
1413 test::CreateCldDataHarness();
1414 ASSERT_NO_FATAL_FAILURE(cld_data_harness
->Init());
1415 ASSERT_TRUE(test_server()->Start());
1417 translate::LanguageDetectionDetails details
;
1419 // Open a new tab with a page in English.
1420 AddTabAtIndex(0, GURL(test_server()->GetURL("files/english_page.html")),
1421 content::PAGE_TRANSITION_TYPED
);
1423 WebContents
* current_web_contents
=
1424 browser()->tab_strip_model()->GetActiveWebContents();
1425 ChromeTranslateClient
* chrome_translate_client
=
1426 ChromeTranslateClient::FromWebContents(current_web_contents
);
1427 content::Source
<WebContents
> source(current_web_contents
);
1429 ui_test_utils::WindowedNotificationObserverWithDetails
<
1430 translate::LanguageDetectionDetails
>
1431 en_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1434 chrome_translate_client
->GetLanguageState().original_language());
1435 en_language_detected_signal
.Wait();
1436 EXPECT_TRUE(en_language_detected_signal
.GetDetailsFor(
1437 source
.map_key(), &details
));
1438 EXPECT_EQ("en", details
.adopted_language
);
1440 chrome_translate_client
->GetLanguageState().original_language());
1442 // Now navigate to a page in French.
1443 ui_test_utils::WindowedNotificationObserverWithDetails
<
1444 translate::LanguageDetectionDetails
>
1445 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1447 ui_test_utils::NavigateToURL(
1448 browser(), GURL(test_server()->GetURL("files/french_page.html")));
1449 fr_language_detected_signal
.Wait();
1450 details
.adopted_language
.clear();
1451 EXPECT_TRUE(fr_language_detected_signal
.GetDetailsFor(
1452 source
.map_key(), &details
));
1453 EXPECT_EQ("fr", details
.adopted_language
);
1455 chrome_translate_client
->GetLanguageState().original_language());
1458 // Chromeos defaults to restoring the last session, so this test isn't
1460 #if !defined(OS_CHROMEOS)
1461 #if defined(OS_MACOSX)
1462 // Crashy, http://crbug.com/38522
1463 #define RestorePinnedTabs DISABLED_RestorePinnedTabs
1465 // Makes sure pinned tabs are restored correctly on start.
1466 IN_PROC_BROWSER_TEST_F(BrowserTest
, RestorePinnedTabs
) {
1467 ASSERT_TRUE(test_server()->Start());
1469 // Add an pinned app tab.
1470 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1471 GURL
url(test_server()->GetURL("empty.html"));
1472 TabStripModel
* model
= browser()->tab_strip_model();
1473 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1474 const Extension
* extension_app
= GetExtension();
1475 ui_test_utils::NavigateToURL(browser(), url
);
1476 WebContents
* app_contents
= WebContents::Create(
1477 WebContents::CreateParams(browser()->profile()));
1478 extensions::TabHelper::CreateForWebContents(app_contents
);
1479 extensions::TabHelper
* extensions_tab_helper
=
1480 extensions::TabHelper::FromWebContents(app_contents
);
1481 extensions_tab_helper
->SetExtensionApp(extension_app
);
1482 model
->AddWebContents(app_contents
, 0, content::PageTransitionFromInt(0),
1483 TabStripModel::ADD_NONE
);
1484 model
->SetTabPinned(0, true);
1485 ui_test_utils::NavigateToURL(browser(), url
);
1487 // Add a non pinned tab.
1488 chrome::NewTab(browser());
1490 // Add a pinned non-app tab.
1491 chrome::NewTab(browser());
1492 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL
));
1493 model
->SetTabPinned(2, true);
1495 // Write out the pinned tabs.
1496 PinnedTabCodec::WritePinnedTabs(browser()->profile());
1498 // Simulate launching again.
1499 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1500 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1501 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1502 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, first_run
);
1503 launch
.profile_
= browser()->profile();
1504 launch
.ProcessStartupURLs(std::vector
<GURL
>(),
1505 browser()->host_desktop_type());
1507 // The launch should have created a new browser.
1508 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1509 browser()->host_desktop_type()));
1511 // Find the new browser.
1512 Browser
* new_browser
= NULL
;
1513 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1514 if (*it
!= browser())
1517 ASSERT_TRUE(new_browser
);
1518 ASSERT_TRUE(new_browser
!= browser());
1520 // We should get back an additional tab for the app, and another for the
1521 // default home page.
1522 ASSERT_EQ(3, new_browser
->tab_strip_model()->count());
1524 // Make sure the state matches.
1525 TabStripModel
* new_model
= new_browser
->tab_strip_model();
1526 EXPECT_TRUE(new_model
->IsAppTab(0));
1527 EXPECT_FALSE(new_model
->IsAppTab(1));
1528 EXPECT_FALSE(new_model
->IsAppTab(2));
1530 EXPECT_TRUE(new_model
->IsTabPinned(0));
1531 EXPECT_TRUE(new_model
->IsTabPinned(1));
1532 EXPECT_FALSE(new_model
->IsTabPinned(2));
1534 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
1535 new_model
->GetWebContentsAt(2)->GetURL());
1538 extensions::TabHelper::FromWebContents(
1539 new_model
->GetWebContentsAt(0))->extension_app() == extension_app
);
1541 #endif // !defined(OS_CHROMEOS)
1543 // This test verifies we don't crash when closing the last window and the app
1545 IN_PROC_BROWSER_TEST_F(BrowserTest
, CloseWithAppMenuOpen
) {
1546 if (browser_defaults::kBrowserAliveWithNoWindows
)
1549 // We need a message loop running for menus on windows.
1550 base::MessageLoop::current()->PostTask(
1551 FROM_HERE
, base::Bind(&RunCloseWithAppMenuCallback
, browser()));
1554 #if !defined(OS_MACOSX)
1555 IN_PROC_BROWSER_TEST_F(BrowserTest
, OpenAppWindowLikeNtp
) {
1556 ASSERT_TRUE(test_server()->Start());
1559 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1560 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1561 const Extension
* extension_app
= GetExtension();
1563 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1564 WebContents
* app_window
= OpenApplication(
1565 AppLaunchParams(browser()->profile(), extension_app
,
1566 extensions::LAUNCH_CONTAINER_WINDOW
, NEW_WINDOW
));
1567 ASSERT_TRUE(app_window
);
1569 // Apps launched in a window from the NTP have an extensions tab helper but
1570 // do not have extension_app set in it.
1571 ASSERT_TRUE(extensions::TabHelper::FromWebContents(app_window
));
1573 extensions::TabHelper::FromWebContents(app_window
)->extension_app());
1574 EXPECT_EQ(extensions::AppLaunchInfo::GetFullLaunchURL(extension_app
),
1575 app_window
->GetURL());
1577 // The launch should have created a new browser.
1578 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1579 browser()->host_desktop_type()));
1581 // Find the new browser.
1582 Browser
* new_browser
= NULL
;
1583 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1584 if (*it
!= browser())
1587 ASSERT_TRUE(new_browser
);
1588 ASSERT_TRUE(new_browser
!= browser());
1590 EXPECT_TRUE(new_browser
->is_app());
1592 // The browser's app name should include the extension's id.
1593 std::string app_name
= new_browser
->app_name_
;
1594 EXPECT_NE(app_name
.find(extension_app
->id()), std::string::npos
)
1595 << "Name " << app_name
<< " should contain id "<< extension_app
->id();
1597 #endif // !defined(OS_MACOSX)
1599 // Makes sure the browser doesn't crash when
1600 // set_show_state(ui::SHOW_STATE_MAXIMIZED) has been invoked.
1601 IN_PROC_BROWSER_TEST_F(BrowserTest
, StartMaximized
) {
1602 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1603 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(types
); ++i
) {
1604 Browser::CreateParams
params(types
[i
], browser()->profile(),
1605 browser()->host_desktop_type());
1606 params
.initial_show_state
= ui::SHOW_STATE_MAXIMIZED
;
1607 AddBlankTabAndShow(new Browser(params
));
1611 // Aura doesn't support minimized window. crbug.com/104571.
1612 #if defined(USE_AURA)
1613 #define MAYBE_StartMinimized DISABLED_StartMinimized
1615 #define MAYBE_StartMinimized StartMinimized
1617 // Makes sure the browser doesn't crash when
1618 // set_show_state(ui::SHOW_STATE_MINIMIZED) has been invoked.
1619 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_StartMinimized
) {
1620 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1621 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(types
); ++i
) {
1622 Browser::CreateParams
params(types
[i
], browser()->profile(),
1623 browser()->host_desktop_type());
1624 params
.initial_show_state
= ui::SHOW_STATE_MINIMIZED
;
1625 AddBlankTabAndShow(new Browser(params
));
1629 // Makes sure the forward button is disabled immediately when navigating
1630 // forward to a slow-to-commit page.
1631 IN_PROC_BROWSER_TEST_F(BrowserTest
, ForwardDisabledOnForward
) {
1632 GURL
blank_url(url::kAboutBlankURL
);
1633 ui_test_utils::NavigateToURL(browser(), blank_url
);
1635 ui_test_utils::NavigateToURL(
1636 browser(), ui_test_utils::GetTestUrl(
1637 base::FilePath(base::FilePath::kCurrentDirectory
),
1638 base::FilePath(kTitle1File
)));
1640 content::WindowedNotificationObserver
back_nav_load_observer(
1641 content::NOTIFICATION_LOAD_STOP
,
1642 content::Source
<NavigationController
>(
1643 &browser()->tab_strip_model()->GetActiveWebContents()->
1645 chrome::GoBack(browser(), CURRENT_TAB
);
1646 back_nav_load_observer
.Wait();
1647 CommandUpdater
* command_updater
=
1648 browser()->command_controller()->command_updater();
1649 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1651 content::WindowedNotificationObserver
forward_nav_load_observer(
1652 content::NOTIFICATION_LOAD_STOP
,
1653 content::Source
<NavigationController
>(
1654 &browser()->tab_strip_model()->GetActiveWebContents()->
1656 chrome::GoForward(browser(), CURRENT_TAB
);
1657 // This check will happen before the navigation completes, since the browser
1658 // won't process the renderer's response until the Wait() call below.
1659 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1660 forward_nav_load_observer
.Wait();
1663 // Makes sure certain commands are disabled when Incognito mode is forced.
1664 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisableMenuItemsWhenIncognitoIsForced
) {
1665 CommandUpdater
* command_updater
=
1666 browser()->command_controller()->command_updater();
1667 // At the beginning, all commands are enabled.
1668 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1669 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1670 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1671 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1672 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1673 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1675 // Set Incognito to FORCED.
1676 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1677 IncognitoModePrefs::FORCED
);
1678 // Bookmarks & Settings commands should get disabled.
1679 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1680 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1681 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1682 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1683 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1684 // New Incognito Window command, however, should be enabled.
1685 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1687 // Create a new browser.
1688 Browser
* new_browser
=
1689 new Browser(Browser::CreateParams(
1690 browser()->profile()->GetOffTheRecordProfile(),
1691 browser()->host_desktop_type()));
1692 CommandUpdater
* new_command_updater
=
1693 new_browser
->command_controller()->command_updater();
1694 // It should have Bookmarks & Settings commands disabled by default.
1695 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1696 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(
1697 IDC_SHOW_BOOKMARK_MANAGER
));
1698 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1699 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1700 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1701 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1704 // Makes sure New Incognito Window command is disabled when Incognito mode is
1706 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1707 NoNewIncognitoWindowWhenIncognitoIsDisabled
) {
1708 CommandUpdater
* command_updater
=
1709 browser()->command_controller()->command_updater();
1710 // Set Incognito to DISABLED.
1711 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1712 IncognitoModePrefs::DISABLED
);
1713 // Make sure New Incognito Window command is disabled. All remaining commands
1714 // should be enabled.
1715 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1716 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1717 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1718 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1719 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1720 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1722 // Create a new browser.
1723 Browser
* new_browser
=
1724 new Browser(Browser::CreateParams(browser()->profile(),
1725 browser()->host_desktop_type()));
1726 CommandUpdater
* new_command_updater
=
1727 new_browser
->command_controller()->command_updater();
1728 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1729 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1730 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1731 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1732 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1733 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1736 // Makes sure Extensions and Settings commands are disabled in certain
1737 // circumstances even though normally they should stay enabled.
1738 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1739 DisableExtensionsAndSettingsWhenIncognitoIsDisabled
) {
1740 CommandUpdater
* command_updater
=
1741 browser()->command_controller()->command_updater();
1742 // Disable extensions. This should disable Extensions menu.
1743 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
1744 set_extensions_enabled(false);
1745 // Set Incognito to DISABLED.
1746 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1747 IncognitoModePrefs::DISABLED
);
1748 // Make sure Manage Extensions command is disabled.
1749 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1750 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1751 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1752 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1753 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1755 // Create a popup (non-main-UI-type) browser. Settings command as well
1756 // as Extensions should be disabled.
1757 Browser
* popup_browser
= new Browser(
1758 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1759 browser()->host_desktop_type()));
1760 CommandUpdater
* popup_command_updater
=
1761 popup_browser
->command_controller()->command_updater();
1762 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1763 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1764 EXPECT_TRUE(popup_command_updater
->IsCommandEnabled(
1765 IDC_SHOW_BOOKMARK_MANAGER
));
1766 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1769 // Makes sure Extensions and Settings commands are disabled in certain
1770 // circumstances even though normally they should stay enabled.
1771 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1772 DisableOptionsAndImportMenuItemsConsistently
) {
1773 // Create a popup browser.
1774 Browser
* popup_browser
= new Browser(
1775 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1776 browser()->host_desktop_type()));
1777 CommandUpdater
* command_updater
=
1778 popup_browser
->command_controller()->command_updater();
1779 // OPTIONS and IMPORT_SETTINGS are disabled for a non-normal UI.
1780 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1781 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1783 // Set Incognito to FORCED.
1784 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1785 IncognitoModePrefs::FORCED
);
1786 // OPTIONS and IMPORT_SETTINGS are disabled when Incognito is forced.
1787 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1788 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1789 // Set Incognito to AVAILABLE.
1790 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1791 IncognitoModePrefs::ENABLED
);
1792 // OPTIONS and IMPORT_SETTINGS are still disabled since it is a non-normal UI.
1793 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1794 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1799 void OnZoomLevelChanged(const base::Closure
& callback
,
1800 const HostZoomMap::ZoomLevelChange
& host
) {
1807 // Flakes regularly on Windows XP
1808 // http://crbug.com/146040
1809 #define MAYBE_PageZoom DISABLED_PageZoom
1811 #define MAYBE_PageZoom PageZoom
1816 int GetZoomPercent(const content::WebContents
* contents
,
1818 bool* enable_minus
) {
1819 int percent
= ZoomController::FromWebContents(contents
)->GetZoomPercent();
1820 *enable_plus
= percent
< contents
->GetMaximumZoomPercent();
1821 *enable_minus
= percent
> contents
->GetMinimumZoomPercent();
1827 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_PageZoom
) {
1828 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1829 bool enable_plus
, enable_minus
;
1832 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1833 new content::MessageLoopRunner
);
1834 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1835 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1836 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1837 content::HostZoomMap::GetForBrowserContext(
1838 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1839 chrome::Zoom(browser(), content::PAGE_ZOOM_IN
);
1842 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 110);
1843 EXPECT_TRUE(enable_plus
);
1844 EXPECT_TRUE(enable_minus
);
1848 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1849 new content::MessageLoopRunner
);
1850 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1851 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1852 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1853 content::HostZoomMap::GetForBrowserContext(
1854 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1855 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1858 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 100);
1859 EXPECT_TRUE(enable_plus
);
1860 EXPECT_TRUE(enable_minus
);
1864 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1865 new content::MessageLoopRunner
);
1866 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1867 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1868 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1869 content::HostZoomMap::GetForBrowserContext(
1870 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1871 chrome::Zoom(browser(), content::PAGE_ZOOM_OUT
);
1874 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 90);
1875 EXPECT_TRUE(enable_plus
);
1876 EXPECT_TRUE(enable_minus
);
1879 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1882 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCommandDisable
) {
1883 ASSERT_TRUE(test_server()->Start());
1884 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1885 GURL
url(test_server()->GetURL("empty.html"));
1886 ui_test_utils::NavigateToURL(browser(), url
);
1888 CommandUpdater
* command_updater
=
1889 browser()->command_controller()->command_updater();
1890 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1891 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1892 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1893 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1895 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1897 TestInterstitialPage
* interstitial
= NULL
;
1899 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1900 new content::MessageLoopRunner
);
1902 InterstitialObserver
observer(contents
,
1903 loop_runner
->QuitClosure(),
1905 interstitial
= new TestInterstitialPage(contents
, false, GURL());
1909 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1911 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1912 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1913 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1914 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1917 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1918 new content::MessageLoopRunner
);
1920 InterstitialObserver
observer(contents
,
1922 loop_runner
->QuitClosure());
1923 interstitial
->Proceed();
1925 // interstitial is deleted now.
1928 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1929 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1930 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1931 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1934 // Ensure that creating an interstitial page closes any JavaScript dialogs
1935 // that were present on the previous page. See http://crbug.com/295695.
1936 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialClosesDialogs
) {
1937 ASSERT_TRUE(test_server()->Start());
1938 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1939 GURL
url(test_server()->GetURL("empty.html"));
1940 ui_test_utils::NavigateToURL(browser(), url
);
1942 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1943 contents
->GetMainFrame()->ExecuteJavaScript(
1944 ASCIIToUTF16("alert('Dialog showing!');"));
1945 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
1946 EXPECT_TRUE(alert
->IsValid());
1947 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
1948 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
1950 TestInterstitialPage
* interstitial
= NULL
;
1952 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1953 new content::MessageLoopRunner
);
1955 InterstitialObserver
observer(contents
,
1956 loop_runner
->QuitClosure(),
1958 interstitial
= new TestInterstitialPage(contents
, false, GURL());
1962 // The interstitial should have closed the dialog.
1963 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1964 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
1967 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1968 new content::MessageLoopRunner
);
1970 InterstitialObserver
observer(contents
,
1972 loop_runner
->QuitClosure());
1973 interstitial
->DontProceed();
1975 // interstitial is deleted now.
1978 // Make sure input events still work in the renderer process.
1979 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
1983 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCloseTab
) {
1984 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1987 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1988 new content::MessageLoopRunner
);
1990 InterstitialObserver
observer(contents
,
1991 loop_runner
->QuitClosure(),
1993 // Interstitial will delete itself when we close the tab.
1994 new TestInterstitialPage(contents
, false, GURL());
1998 EXPECT_TRUE(contents
->ShowingInterstitialPage());
2001 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
2002 new content::MessageLoopRunner
);
2004 InterstitialObserver
observer(contents
,
2006 loop_runner
->QuitClosure());
2007 chrome::CloseTab(browser());
2009 // interstitial is deleted now.
2013 class MockWebContentsObserver
: public WebContentsObserver
{
2015 explicit MockWebContentsObserver(WebContents
* web_contents
)
2016 : WebContentsObserver(web_contents
),
2017 got_user_gesture_(false) {
2020 virtual void DidGetUserGesture() OVERRIDE
{
2021 got_user_gesture_
= true;
2024 bool got_user_gesture() const {
2025 return got_user_gesture_
;
2028 void set_got_user_gesture(bool got_it
) {
2029 got_user_gesture_
= got_it
;
2033 bool got_user_gesture_
;
2035 DISALLOW_COPY_AND_ASSIGN(MockWebContentsObserver
);
2038 IN_PROC_BROWSER_TEST_F(BrowserTest
, UserGesturesReported
) {
2039 // Regression test for http://crbug.com/110707. Also tests that a user
2040 // gesture is sent when a normal navigation (via e.g. the omnibox) is
2042 WebContents
* web_contents
=
2043 browser()->tab_strip_model()->GetActiveWebContents();
2044 MockWebContentsObserver
mock_observer(web_contents
);
2046 ASSERT_TRUE(test_server()->Start());
2047 GURL
url(test_server()->GetURL("empty.html"));
2049 ui_test_utils::NavigateToURL(browser(), url
);
2050 EXPECT_TRUE(mock_observer
.got_user_gesture());
2052 mock_observer
.set_got_user_gesture(false);
2053 chrome::Reload(browser(), CURRENT_TAB
);
2054 EXPECT_TRUE(mock_observer
.got_user_gesture());
2057 // TODO(ben): this test was never enabled. It has bit-rotted since being added.
2058 // It originally lived in browser_unittest.cc, but has been moved here to make
2059 // room for real browser unit tests.
2061 class BrowserTest2
: public InProcessBrowserTest
{
2064 host_resolver_proc_
= new net::RuleBasedHostResolverProc(NULL
);
2065 // Avoid making external DNS lookups. In this test we don't need this
2067 host_resolver_proc_
->AddSimulatedFailure("*.google.com");
2068 scoped_host_resolver_proc_
.Init(host_resolver_proc_
.get());
2072 scoped_refptr
<net::RuleBasedHostResolverProc
> host_resolver_proc_
;
2073 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_
;
2076 IN_PROC_BROWSER_TEST_F(BrowserTest2
, NoTabsInPopups
) {
2077 chrome::RegisterAppPrefs(L
"Test");
2079 // We start with a normal browser with one tab.
2080 EXPECT_EQ(1, browser()->tab_strip_model()->count());
2082 // Open a popup browser with a single blank foreground tab.
2083 Browser
* popup_browser
= new Browser(
2084 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile()));
2085 chrome::AddTabAt(popup_browser
, GURL(), -1, true);
2086 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2088 // Now try opening another tab in the popup browser.
2089 AddTabWithURLParams
params1(url
, content::PAGE_TRANSITION_TYPED
);
2090 popup_browser
->AddTabWithURL(¶ms1
);
2091 EXPECT_EQ(popup_browser
, params1
.target
);
2093 // The popup should still only have one tab.
2094 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2096 // The normal browser should now have two.
2097 EXPECT_EQ(2, browser()->tab_strip_model()->count());
2099 // Open an app frame browser with a single blank foreground tab.
2100 Browser
* app_browser
= new Browser(Browser::CreateParams::CreateForApp(
2101 L
"Test", browser()->profile(), false));
2102 chrome::AddTabAt(app_browser
, GURL(), -1, true);
2103 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2105 // Now try opening another tab in the app browser.
2106 AddTabWithURLParams
params2(GURL(url::kAboutBlankURL
),
2107 content::PAGE_TRANSITION_TYPED
);
2108 app_browser
->AddTabWithURL(¶ms2
);
2109 EXPECT_EQ(app_browser
, params2
.target
);
2111 // The popup should still only have one tab.
2112 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2114 // The normal browser should now have three.
2115 EXPECT_EQ(3, browser()->tab_strip_model()->count());
2117 // Open an app frame popup browser with a single blank foreground tab.
2118 Browser
* app_popup_browser
= new Browser(Browser::CreateParams::CreateForApp(
2119 L
"Test", browser()->profile(), false));
2120 chrome::AddTabAt(app_popup_browser
, GURL(), -1, true);
2121 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2123 // Now try opening another tab in the app popup browser.
2124 AddTabWithURLParams
params3(GURL(url::kAboutBlankURL
),
2125 content::PAGE_TRANSITION_TYPED
);
2126 app_popup_browser
->AddTabWithURL(¶ms3
);
2127 EXPECT_EQ(app_popup_browser
, params3
.target
);
2129 // The popup should still only have one tab.
2130 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2132 // The normal browser should now have four.
2133 EXPECT_EQ(4, browser()->tab_strip_model()->count());
2135 // Close the additional browsers.
2136 popup_browser
->tab_strip_model()->CloseAllTabs();
2137 app_browser
->tab_strip_model()->CloseAllTabs();
2138 app_popup_browser
->tab_strip_model()->CloseAllTabs();
2142 IN_PROC_BROWSER_TEST_F(BrowserTest
, WindowOpenClose
) {
2143 CommandLine::ForCurrentProcess()->AppendSwitch(
2144 switches::kDisablePopupBlocking
);
2145 GURL url
= ui_test_utils::GetTestUrl(
2146 base::FilePath(), base::FilePath().AppendASCII("window.close.html"));
2148 base::string16 title
= ASCIIToUTF16("Title Of Awesomeness");
2149 content::TitleWatcher
title_watcher(
2150 browser()->tab_strip_model()->GetActiveWebContents(), title
);
2151 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url
, 2);
2152 EXPECT_EQ(title
, title_watcher
.WaitAndGetTitle());
2155 // TODO(linux_aura) http://crbug.com/163931
2156 // Mac disabled: http://crbug.com/169820
2157 #if !defined(OS_MACOSX) && \
2158 !(defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
2159 IN_PROC_BROWSER_TEST_F(BrowserTest
, FullscreenBookmarkBar
) {
2160 #if defined(OS_WIN) && defined(USE_ASH)
2161 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2162 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2166 chrome::ToggleBookmarkBar(browser());
2167 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2168 chrome::ToggleFullscreenMode(browser());
2169 EXPECT_TRUE(browser()->window()->IsFullscreen());
2170 #if defined(OS_MACOSX)
2171 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2172 #elif defined(OS_CHROMEOS)
2173 // TODO(jamescook): If immersive fullscreen is disabled by default, test
2174 // for BookmarkBar::HIDDEN.
2175 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2177 EXPECT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2182 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisallowFileUrlUniversalAccessTest
) {
2183 GURL url
= ui_test_utils::GetTestUrl(
2185 base::FilePath().AppendASCII("fileurl_universalaccess.html"));
2187 base::string16
expected_title(ASCIIToUTF16("Disallowed"));
2188 content::TitleWatcher
title_watcher(
2189 browser()->tab_strip_model()->GetActiveWebContents(), expected_title
);
2190 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("Allowed"));
2191 ui_test_utils::NavigateToURL(browser(), url
);
2192 ASSERT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
2195 class KioskModeTest
: public BrowserTest
{
2199 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2200 command_line
->AppendSwitch(switches::kKioskMode
);
2204 #if defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
2205 // Mac: http://crbug.com/103912
2206 // Linux: http://crbug.com/163931
2207 #define MAYBE_EnableKioskModeTest DISABLED_EnableKioskModeTest
2209 #define MAYBE_EnableKioskModeTest EnableKioskModeTest
2211 IN_PROC_BROWSER_TEST_F(KioskModeTest
, MAYBE_EnableKioskModeTest
) {
2212 // Check if browser is in fullscreen mode.
2213 ASSERT_TRUE(browser()->window()->IsFullscreen());
2214 ASSERT_FALSE(browser()->window()->IsFullscreenBubbleVisible());
2218 // This test verifies that Chrome can be launched with a user-data-dir path
2219 // which contains non ASCII characters.
2220 class LaunchBrowserWithNonAsciiUserDatadir
: public BrowserTest
{
2222 LaunchBrowserWithNonAsciiUserDatadir() {}
2224 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2225 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
2226 base::FilePath tmp_profile
= temp_dir_
.path().AppendASCII("tmp_profile");
2227 tmp_profile
= tmp_profile
.Append(L
"Test Chrome G\u00E9raldine");
2229 ASSERT_TRUE(base::CreateDirectory(tmp_profile
));
2230 command_line
->AppendSwitchPath(switches::kUserDataDir
, tmp_profile
);
2233 base::ScopedTempDir temp_dir_
;
2236 IN_PROC_BROWSER_TEST_F(LaunchBrowserWithNonAsciiUserDatadir
,
2237 TestNonAsciiUserDataDir
) {
2238 // Verify that the window is present.
2239 ASSERT_TRUE(browser());
2241 #endif // defined(OS_WIN)
2243 // Tests to ensure that the browser continues running in the background after
2244 // the last window closes.
2245 class RunInBackgroundTest
: public BrowserTest
{
2247 RunInBackgroundTest() {}
2249 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2250 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2254 IN_PROC_BROWSER_TEST_F(RunInBackgroundTest
, RunInBackgroundBasicTest
) {
2255 // Close the browser window, then open a new one - the browser should keep
2257 Profile
* profile
= browser()->profile();
2258 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2259 content::WindowedNotificationObserver
observer(
2260 chrome::NOTIFICATION_BROWSER_CLOSED
,
2261 content::Source
<Browser
>(browser()));
2262 chrome::CloseWindow(browser());
2264 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2266 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2267 chrome::NewEmptyWindow(profile
, chrome::GetActiveDesktop());
2268 browser_added_observer
.WaitForSingleNewBrowser();
2270 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2273 // Tests to ensure that the browser continues running in the background after
2274 // the last window closes.
2275 class NoStartupWindowTest
: public BrowserTest
{
2277 NoStartupWindowTest() {}
2279 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2280 command_line
->AppendSwitch(switches::kNoStartupWindow
);
2281 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2285 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, NoStartupWindowBasicTest
) {
2286 #if defined(OS_WIN) && defined(USE_ASH)
2287 // kNoStartupWindow doesn't make sense in Metro+Ash.
2288 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2292 // No browser window should be started by default.
2293 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2295 // Starting a browser window should work just fine.
2296 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2297 CreateBrowser(ProfileManager::GetActiveUserProfile());
2298 browser_added_observer
.WaitForSingleNewBrowser();
2300 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2303 // Chromeos needs to track app windows because it considers them to be part of
2305 #if !defined(OS_CHROMEOS)
2306 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, DontInitSessionServiceForApps
) {
2307 #if defined(OS_WIN) && defined(USE_ASH)
2308 // kNoStartupWindow doesn't make sense in Metro+Ash.
2309 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2313 Profile
* profile
= ProfileManager::GetActiveUserProfile();
2315 SessionService
* session_service
=
2316 SessionServiceFactory::GetForProfile(profile
);
2317 ASSERT_FALSE(session_service
->processed_any_commands());
2319 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2320 CreateBrowserForApp("blah", profile
);
2321 browser_added_observer
.WaitForSingleNewBrowser();
2323 ASSERT_FALSE(session_service
->processed_any_commands());
2325 #endif // !defined(OS_CHROMEOS)
2327 // This test needs to be placed outside the anonymous namespace because we
2328 // need to access private type of Browser.
2329 class AppModeTest
: public BrowserTest
{
2333 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
2334 GURL url
= ui_test_utils::GetTestUrl(
2335 base::FilePath(), base::FilePath().AppendASCII("title1.html"));
2336 command_line
->AppendSwitchASCII(switches::kApp
, url
.spec());
2340 IN_PROC_BROWSER_TEST_F(AppModeTest
, EnableAppModeTest
) {
2341 #if defined(OS_WIN) && defined(USE_ASH)
2342 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2343 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
2347 // Test that an application browser window loads correctly.
2349 // Verify the browser is in application mode.
2350 EXPECT_TRUE(browser()->is_app());
2353 // Confirm chrome://version contains some expected content.
2354 IN_PROC_BROWSER_TEST_F(BrowserTest
, AboutVersion
) {
2355 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL
));
2356 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
2357 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("WebKit"), true, true,
2360 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("OS"), true, true,
2363 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("JavaScript"), true,
2368 static const base::FilePath::CharType
* kTestDir
=
2369 FILE_PATH_LITERAL("click_modifier");
2370 static const char kFirstPageTitle
[] = "First window";
2371 static const char kSecondPageTitle
[] = "New window!";
2373 class ClickModifierTest
: public InProcessBrowserTest
{
2375 ClickModifierTest() {
2378 // Returns a url that opens a new window or tab when clicked, via javascript.
2379 GURL
GetWindowOpenURL() {
2380 return ui_test_utils::GetTestUrl(
2381 base::FilePath(kTestDir
),
2382 base::FilePath(FILE_PATH_LITERAL("window_open.html")));
2385 // Returns a url that follows a simple link when clicked, unless affected by
2388 return ui_test_utils::GetTestUrl(
2389 base::FilePath(kTestDir
),
2390 base::FilePath(FILE_PATH_LITERAL("href.html")));
2393 base::string16
getFirstPageTitle() {
2394 return ASCIIToUTF16(kFirstPageTitle
);
2397 base::string16
getSecondPageTitle() {
2398 return ASCIIToUTF16(kSecondPageTitle
);
2401 // Loads our test page and simulates a single click using the supplied button
2402 // and modifiers. The click will cause either a navigation or the creation of
2403 // a new window or foreground or background tab. We verify that the expected
2404 // disposition occurs.
2405 void RunTest(Browser
* browser
,
2408 blink::WebMouseEvent::Button button
,
2409 WindowOpenDisposition disposition
) {
2410 ui_test_utils::NavigateToURL(browser
, url
);
2411 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2412 browser
->host_desktop_type()));
2413 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2414 content::WebContents
* web_contents
=
2415 browser
->tab_strip_model()->GetActiveWebContents();
2416 EXPECT_EQ(url
, web_contents
->GetURL());
2418 if (disposition
== CURRENT_TAB
) {
2419 content::WebContents
* web_contents
=
2420 browser
->tab_strip_model()->GetActiveWebContents();
2421 content::TestNavigationObserver
same_tab_observer(web_contents
);
2422 SimulateMouseClick(web_contents
, modifiers
, button
);
2423 same_tab_observer
.Wait();
2424 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2425 browser
->host_desktop_type()));
2426 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2427 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2431 content::WindowedNotificationObserver
observer(
2432 chrome::NOTIFICATION_TAB_ADDED
,
2433 content::NotificationService::AllSources());
2434 SimulateMouseClick(web_contents
, modifiers
, button
);
2437 if (disposition
== NEW_WINDOW
) {
2438 EXPECT_EQ(2u, chrome::GetBrowserCount(browser
->profile(),
2439 browser
->host_desktop_type()));
2443 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2444 browser
->host_desktop_type()));
2445 EXPECT_EQ(2, browser
->tab_strip_model()->count());
2446 web_contents
= browser
->tab_strip_model()->GetActiveWebContents();
2447 WaitForLoadStop(web_contents
);
2448 if (disposition
== NEW_FOREGROUND_TAB
) {
2449 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2451 ASSERT_EQ(NEW_BACKGROUND_TAB
, disposition
);
2452 EXPECT_EQ(getFirstPageTitle(), web_contents
->GetTitle());
2457 DISALLOW_COPY_AND_ASSIGN(ClickModifierTest
);
2460 // Tests for clicking on elements with handlers that run window.open.
2462 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenBasicClickTest
) {
2464 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2465 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2466 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2469 // TODO(ericu): Alt-click behavior on window.open is platform-dependent and not
2470 // well defined. Should we add tests so we know if it changes?
2472 // Shift-clicks open in a new window.
2473 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftClickTest
) {
2474 int modifiers
= blink::WebInputEvent::ShiftKey
;
2475 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2476 WindowOpenDisposition disposition
= NEW_WINDOW
;
2477 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2480 // Control-clicks open in a background tab.
2481 // On OSX meta [the command key] takes the place of control.
2482 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlClickTest
) {
2483 #if defined(OS_MACOSX)
2484 int modifiers
= blink::WebInputEvent::MetaKey
;
2486 int modifiers
= blink::WebInputEvent::ControlKey
;
2488 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2489 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2490 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2493 // Control-shift-clicks open in a foreground tab.
2494 // On OSX meta [the command key] takes the place of control.
2495 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlShiftClickTest
) {
2496 #if defined(OS_MACOSX)
2497 int modifiers
= blink::WebInputEvent::MetaKey
;
2499 int modifiers
= blink::WebInputEvent::ControlKey
;
2501 modifiers
|= blink::WebInputEvent::ShiftKey
;
2502 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2503 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2504 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2507 // Middle-clicks open in a background tab.
2508 #if defined(OS_LINUX)
2509 // http://crbug.com/396347
2510 #define MAYBE_WindowOpenMiddleClickTest DISABLED_WindowOpenMiddleClickTest
2512 #define MAYBE_WindowOpenMiddleClickTest WindowOpenMiddleClickTest
2514 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, MAYBE_WindowOpenMiddleClickTest
) {
2516 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2517 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2518 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2521 // Shift-middle-clicks open in a foreground tab.
2522 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftMiddleClickTest
) {
2523 int modifiers
= blink::WebInputEvent::ShiftKey
;
2524 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2525 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2526 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2529 // Tests for clicking on normal links.
2531 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefBasicClickTest
) {
2533 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2534 WindowOpenDisposition disposition
= CURRENT_TAB
;
2535 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2538 // TODO(ericu): Alt-click behavior on links is platform-dependent and not well
2539 // defined. Should we add tests so we know if it changes?
2541 // Shift-clicks open in a new window.
2542 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefShiftClickTest
) {
2543 int modifiers
= blink::WebInputEvent::ShiftKey
;
2544 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2545 WindowOpenDisposition disposition
= NEW_WINDOW
;
2546 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2549 // Control-clicks open in a background tab.
2550 // On OSX meta [the command key] takes the place of control.
2551 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefControlClickTest
) {
2552 #if defined(OS_MACOSX)
2553 int modifiers
= blink::WebInputEvent::MetaKey
;
2555 int modifiers
= blink::WebInputEvent::ControlKey
;
2557 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2558 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2559 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2562 // Control-shift-clicks open in a foreground tab.
2563 // On OSX meta [the command key] takes the place of control.
2564 // http://crbug.com/396347
2565 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, DISABLED_HrefControlShiftClickTest
) {
2566 #if defined(OS_MACOSX)
2567 int modifiers
= blink::WebInputEvent::MetaKey
;
2569 int modifiers
= blink::WebInputEvent::ControlKey
;
2571 modifiers
|= blink::WebInputEvent::ShiftKey
;
2572 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2573 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2574 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2577 // Middle-clicks open in a background tab.
2578 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefMiddleClickTest
) {
2580 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2581 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2582 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2585 // Shift-middle-clicks open in a foreground tab.
2586 // http://crbug.com/396347
2587 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, DISABLED_HrefShiftMiddleClickTest
) {
2588 int modifiers
= blink::WebInputEvent::ShiftKey
;
2589 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2590 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2591 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2594 IN_PROC_BROWSER_TEST_F(BrowserTest
, GetSizeForNewRenderView
) {
2595 // The instant extended NTP has javascript that does not work with
2596 // ui_test_utils::NavigateToURL. The NTP rvh reloads when the browser tries
2597 // to navigate away from the page, which causes the WebContents to end up in
2598 // an inconsistent state. (is_loaded = true, last_commited_url=ntp,
2599 // visible_url=title1.html)
2600 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled
,
2602 ASSERT_TRUE(test_server()->Start());
2603 // Create an HTTPS server for cross-site transition.
2604 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
2605 net::SpawnedTestServer::kLocalhost
,
2606 base::FilePath(kDocRoot
));
2607 ASSERT_TRUE(https_test_server
.Start());
2610 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2611 ASSERT_EQ(BookmarkBar::DETACHED
, browser()->bookmark_bar_state());
2612 WebContents
* web_contents
=
2613 browser()->tab_strip_model()->GetActiveWebContents();
2614 content::RenderViewHost
* prev_rvh
= web_contents
->GetRenderViewHost();
2615 const int height_inset
=
2616 browser()->window()->GetRenderViewHeightInsetWithDetachedBookmarkBar();
2617 const gfx::Size initial_wcv_size
=
2618 web_contents
->GetContainerBounds().size();
2619 RenderViewSizeObserver
observer(web_contents
, browser()->window());
2621 // Navigate to a non-NTP page, without resizing WebContentsView.
2622 ui_test_utils::NavigateToURL(browser(),
2623 test_server()->GetURL("files/title1.html"));
2624 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2625 // A new RenderViewHost should be created.
2626 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2627 prev_rvh
= web_contents
->GetRenderViewHost();
2628 gfx::Size rwhv_create_size0
, rwhv_commit_size0
, wcv_commit_size0
;
2629 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2633 // The create height of RenderWidgetHostView should include the height inset.
2634 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2635 initial_wcv_size
.height() + height_inset
),
2637 // When a navigation entry is committed, the size of RenderWidgetHostView
2638 // should be the same as when it was first created.
2639 EXPECT_EQ(rwhv_create_size0
, rwhv_commit_size0
);
2640 // Sizes of the current RenderWidgetHostView and WebContentsView should not
2641 // change before and after WebContentsDelegate::DidNavigateMainFramePostCommit
2642 // (implemented by Browser); we obtain the sizes before PostCommit via
2643 // WebContentsObserver::NavigationEntryCommitted (implemented by
2644 // RenderViewSizeObserver).
2645 EXPECT_EQ(rwhv_commit_size0
,
2646 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2647 // The behavior differs between OSX and views.
2648 // In OSX, the wcv does not change size until after the commit, when the
2649 // bookmark bar disappears (correct).
2650 // In views, the wcv changes size at commit time.
2651 #if defined(OS_MACOSX)
2652 EXPECT_EQ(gfx::Size(wcv_commit_size0
.width(),
2653 wcv_commit_size0
.height() + height_inset
),
2654 web_contents
->GetContainerBounds().size());
2656 EXPECT_EQ(wcv_commit_size0
, web_contents
->GetContainerBounds().size());
2659 // Navigate to another non-NTP page, without resizing WebContentsView.
2660 ui_test_utils::NavigateToURL(browser(),
2661 https_test_server
.GetURL("files/title2.html"));
2662 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2663 // A new RenderVieHost should be created.
2664 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2665 gfx::Size rwhv_create_size1
, rwhv_commit_size1
, wcv_commit_size1
;
2666 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2670 EXPECT_EQ(rwhv_create_size1
, rwhv_commit_size1
);
2671 EXPECT_EQ(rwhv_commit_size1
,
2672 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2673 EXPECT_EQ(wcv_commit_size1
, web_contents
->GetContainerBounds().size());
2675 // Navigate from NTP to a non-NTP page, resizing WebContentsView while
2676 // navigation entry is pending.
2677 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2678 gfx::Size
wcv_resize_insets(1, 1);
2679 observer
.set_wcv_resize_insets(wcv_resize_insets
);
2680 ui_test_utils::NavigateToURL(browser(),
2681 test_server()->GetURL("files/title2.html"));
2682 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2683 gfx::Size rwhv_create_size2
, rwhv_commit_size2
, wcv_commit_size2
;
2684 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2689 // The behavior on OSX and Views is incorrect in this edge case, but they are
2690 // differently incorrect.
2691 // The behavior should be:
2692 // initial wcv size: (100,100) (to choose random numbers)
2693 // initial rwhv size: (100,140)
2694 // commit wcv size: (101, 101)
2695 // commit rwhv size: (101, 141)
2696 // final wcv size: (101, 141)
2697 // final rwhv size: (101, 141)
2699 // On OSX, the commit rwhv size is (101, 101)
2700 // On views, the commit wcv size is (101, 141)
2701 // All other sizes are correct.
2703 // The create height of RenderWidgetHostView should include the height inset.
2704 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2705 initial_wcv_size
.height() + height_inset
),
2707 gfx::Size
exp_commit_size(initial_wcv_size
);
2709 #if defined(OS_MACOSX)
2710 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2711 wcv_resize_insets
.height());
2713 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2714 wcv_resize_insets
.height() + height_inset
);
2716 EXPECT_EQ(exp_commit_size
, rwhv_commit_size2
);
2717 EXPECT_EQ(exp_commit_size
, wcv_commit_size2
);
2718 gfx::Size
exp_final_size(initial_wcv_size
);
2719 exp_final_size
.Enlarge(wcv_resize_insets
.width(),
2720 wcv_resize_insets
.height() + height_inset
);
2721 EXPECT_EQ(exp_final_size
,
2722 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2723 EXPECT_EQ(exp_final_size
, web_contents
->GetContainerBounds().size());