1 // Copyright 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/defaults.h"
20 #include "chrome/browser/devtools/devtools_window_testing.h"
21 #include "chrome/browser/extensions/extension_browsertest.h"
22 #include "chrome/browser/extensions/extension_service.h"
23 #include "chrome/browser/extensions/extension_util.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_info_cache.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/browser/search/search.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/translate/cld_data_harness_factory.h"
36 #include "chrome/browser/ui/browser.h"
37 #include "chrome/browser/ui/browser_command_controller.h"
38 #include "chrome/browser/ui/browser_commands.h"
39 #include "chrome/browser/ui/browser_finder.h"
40 #include "chrome/browser/ui/browser_iterator.h"
41 #include "chrome/browser/ui/browser_navigator.h"
42 #include "chrome/browser/ui/browser_tabstrip.h"
43 #include "chrome/browser/ui/browser_ui_prefs.h"
44 #include "chrome/browser/ui/browser_window.h"
45 #include "chrome/browser/ui/extensions/app_launch_params.h"
46 #include "chrome/browser/ui/extensions/application_launch.h"
47 #include "chrome/browser/ui/host_desktop.h"
48 #include "chrome/browser/ui/startup/startup_browser_creator.h"
49 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
50 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
51 #include "chrome/browser/ui/tabs/tab_strip_model.h"
52 #include "chrome/common/chrome_switches.h"
53 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
54 #include "chrome/common/pref_names.h"
55 #include "chrome/common/url_constants.h"
56 #include "chrome/grit/chromium_strings.h"
57 #include "chrome/grit/generated_resources.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/app_modal/app_modal_dialog.h"
62 #include "components/app_modal/app_modal_dialog_queue.h"
63 #include "components/app_modal/javascript_app_modal_dialog.h"
64 #include "components/app_modal/native_app_modal_dialog.h"
65 #include "components/content_settings/core/browser/host_content_settings_map.h"
66 #include "components/sessions/base_session_service_test_helper.h"
67 #include "components/translate/core/browser/language_state.h"
68 #include "components/translate/core/common/language_detection_details.h"
69 #include "content/public/browser/favicon_status.h"
70 #include "content/public/browser/host_zoom_map.h"
71 #include "content/public/browser/interstitial_page.h"
72 #include "content/public/browser/interstitial_page_delegate.h"
73 #include "content/public/browser/navigation_entry.h"
74 #include "content/public/browser/notification_service.h"
75 #include "content/public/browser/render_frame_host.h"
76 #include "content/public/browser/render_process_host.h"
77 #include "content/public/browser/render_view_host.h"
78 #include "content/public/browser/render_widget_host_view.h"
79 #include "content/public/browser/resource_context.h"
80 #include "content/public/browser/web_contents.h"
81 #include "content/public/browser/web_contents_observer.h"
82 #include "content/public/common/frame_navigate_params.h"
83 #include "content/public/common/renderer_preferences.h"
84 #include "content/public/common/url_constants.h"
85 #include "content/public/test/browser_test_utils.h"
86 #include "content/public/test/test_navigation_observer.h"
87 #include "extensions/browser/extension_registry.h"
88 #include "extensions/browser/extension_system.h"
89 #include "extensions/browser/uninstall_reason.h"
90 #include "extensions/common/constants.h"
91 #include "extensions/common/extension.h"
92 #include "extensions/common/extension_set.h"
93 #include "net/dns/mock_host_resolver.h"
94 #include "net/test/spawned_test_server/spawned_test_server.h"
95 #include "ui/base/l10n/l10n_util.h"
96 #include "ui/base/page_transition_types.h"
98 #if defined(OS_MACOSX)
99 #include "base/mac/scoped_nsautorelease_pool.h"
100 #include "chrome/browser/ui/cocoa/run_loop_testing.h"
104 #include "base/i18n/rtl.h"
105 #include "chrome/browser/browser_process.h"
108 using app_modal::AppModalDialog
;
109 using app_modal::AppModalDialogQueue
;
110 using app_modal::JavaScriptAppModalDialog
;
111 using base::ASCIIToUTF16
;
112 using content::InterstitialPage
;
113 using content::HostZoomMap
;
114 using content::NavigationController
;
115 using content::NavigationEntry
;
116 using content::OpenURLParams
;
117 using content::Referrer
;
118 using content::WebContents
;
119 using content::WebContentsObserver
;
120 using extensions::Extension
;
124 const char* kBeforeUnloadHTML
=
125 "<html><head><title>beforeunload</title></head><body>"
126 "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
129 const char* kOpenNewBeforeUnloadPage
=
130 "w=window.open(); w.onbeforeunload=function(e){return 'foo'};";
132 const base::FilePath::CharType
* kBeforeUnloadFile
=
133 FILE_PATH_LITERAL("beforeunload.html");
135 const base::FilePath::CharType
* kTitle1File
= FILE_PATH_LITERAL("title1.html");
136 const base::FilePath::CharType
* kTitle2File
= FILE_PATH_LITERAL("title2.html");
138 const base::FilePath::CharType kDocRoot
[] =
139 FILE_PATH_LITERAL("chrome/test/data");
141 // Given a page title, returns the expected window caption string.
142 base::string16
WindowCaptionFromPageTitle(const base::string16
& page_title
) {
143 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
144 // On Mac or ChromeOS, we don't want to suffix the page title with
145 // the application name.
146 if (page_title
.empty())
147 return l10n_util::GetStringUTF16(IDS_BROWSER_WINDOW_MAC_TAB_UNTITLED
);
150 if (page_title
.empty())
151 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
);
153 return l10n_util::GetStringFUTF16(IDS_BROWSER_WINDOW_TITLE_FORMAT
,
158 // Returns the number of active RenderProcessHosts.
159 int CountRenderProcessHosts() {
161 for (content::RenderProcessHost::iterator
i(
162 content::RenderProcessHost::AllHostsIterator());
163 !i
.IsAtEnd(); i
.Advance())
168 class MockTabStripModelObserver
: public TabStripModelObserver
{
170 MockTabStripModelObserver() : closing_count_(0) {}
172 void TabClosingAt(TabStripModel
* tab_strip_model
,
173 WebContents
* contents
,
174 int index
) override
{
178 int closing_count() const { return closing_count_
; }
183 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver
);
186 // Causes the browser to swap processes on a redirect to an HTTPS URL.
187 class TransferHttpsRedirectsContentBrowserClient
188 : public chrome::ChromeContentBrowserClient
{
190 bool ShouldSwapProcessesForRedirect(
191 content::ResourceContext
* resource_context
,
192 const GURL
& current_url
,
193 const GURL
& new_url
) override
{
194 return new_url
.SchemeIs(url::kHttpsScheme
);
198 // Used by CloseWithAppMenuOpen. Invokes CloseWindow on the supplied browser.
199 void CloseWindowCallback(Browser
* browser
) {
200 chrome::CloseWindow(browser
);
203 // Used by CloseWithAppMenuOpen. Posts a CloseWindowCallback and shows the app
205 void RunCloseWithAppMenuCallback(Browser
* browser
) {
206 // ShowAppMenu is modal under views. Schedule a task that closes the window.
207 base::MessageLoop::current()->PostTask(
208 FROM_HERE
, base::Bind(&CloseWindowCallback
, browser
));
209 chrome::ShowAppMenu(browser
);
212 // Displays "INTERSTITIAL" while the interstitial is attached.
213 // (InterstitialPage can be used in a test directly, but there would be no way
214 // to visually tell if it is showing or not.)
215 class TestInterstitialPage
: public content::InterstitialPageDelegate
{
217 TestInterstitialPage(WebContents
* tab
, bool new_navigation
, const GURL
& url
) {
218 interstitial_page_
= InterstitialPage::Create(
219 tab
, new_navigation
, url
, this);
220 interstitial_page_
->Show();
222 ~TestInterstitialPage() override
{}
224 interstitial_page_
->Proceed();
227 interstitial_page_
->DontProceed();
230 std::string
GetHTMLContents() override
{ return "<h1>INTERSTITIAL</h1>"; }
233 InterstitialPage
* interstitial_page_
; // Owns us.
236 class RenderViewSizeObserver
: public content::WebContentsObserver
{
238 RenderViewSizeObserver(content::WebContents
* web_contents
,
239 BrowserWindow
* browser_window
)
240 : WebContentsObserver(web_contents
),
241 browser_window_(browser_window
) {
244 void GetSizeForRenderViewHost(
245 content::RenderViewHost
* render_view_host
,
246 gfx::Size
* rwhv_create_size
,
247 gfx::Size
* rwhv_commit_size
,
248 gfx::Size
* wcv_commit_size
) {
249 RenderViewSizes::const_iterator result
= render_view_sizes_
.end();
250 result
= render_view_sizes_
.find(render_view_host
);
251 if (result
!= render_view_sizes_
.end()) {
252 *rwhv_create_size
= result
->second
.rwhv_create_size
;
253 *rwhv_commit_size
= result
->second
.rwhv_commit_size
;
254 *wcv_commit_size
= result
->second
.wcv_commit_size
;
258 void set_wcv_resize_insets(const gfx::Size
& wcv_resize_insets
) {
259 wcv_resize_insets_
= wcv_resize_insets
;
262 // Cache the size when RenderViewHost is first created.
263 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
{
264 render_view_sizes_
[render_view_host
].rwhv_create_size
=
265 render_view_host
->GetView()->GetViewBounds().size();
268 // Enlarge WebContentsView by |wcv_resize_insets_| while the navigation entry
270 void DidStartNavigationToPendingEntry(
272 NavigationController::ReloadType reload_type
) override
{
273 if (wcv_resize_insets_
.IsEmpty())
275 // Resizing the main browser window by |wcv_resize_insets_| will
276 // automatically resize the WebContentsView by the same amount.
277 // Just resizing WebContentsView directly doesn't work on Linux, because the
278 // next automatic layout of the browser window will resize WebContentsView
279 // back to the previous size. To make it consistent, resize main browser
280 // window on all platforms.
281 gfx::Rect
bounds(browser_window_
->GetBounds());
282 gfx::Size
size(bounds
.size());
283 size
.Enlarge(wcv_resize_insets_
.width(), wcv_resize_insets_
.height());
284 bounds
.set_size(size
);
285 browser_window_
->SetBounds(bounds
);
286 // Let the message loop run so that resize actually takes effect.
287 content::RunAllPendingInMessageLoop();
290 // Cache the sizes of RenderWidgetHostView and WebContentsView when the
291 // navigation entry is committed, which is before
292 // WebContentsDelegate::DidNavigateMainFramePostCommit is called.
293 void NavigationEntryCommitted(
294 const content::LoadCommittedDetails
& details
) override
{
295 content::RenderViewHost
* rvh
= web_contents()->GetRenderViewHost();
296 render_view_sizes_
[rvh
].rwhv_commit_size
=
297 web_contents()->GetRenderWidgetHostView()->GetViewBounds().size();
298 render_view_sizes_
[rvh
].wcv_commit_size
=
299 web_contents()->GetContainerBounds().size();
304 gfx::Size rwhv_create_size
; // Size of RenderWidgetHostView when created.
305 gfx::Size rwhv_commit_size
; // Size of RenderWidgetHostView when committed.
306 gfx::Size wcv_commit_size
; // Size of WebContentsView when committed.
309 typedef std::map
<content::RenderViewHost
*, Sizes
> RenderViewSizes
;
310 RenderViewSizes render_view_sizes_
;
311 // Enlarge WebContentsView by this size insets in
312 // DidStartNavigationToPendingEntry.
313 gfx::Size wcv_resize_insets_
;
314 BrowserWindow
* browser_window_
; // Weak ptr.
316 DISALLOW_COPY_AND_ASSIGN(RenderViewSizeObserver
);
321 class BrowserTest
: public ExtensionBrowserTest
{
323 // In RTL locales wrap the page title with RTL embedding characters so that it
324 // matches the value returned by GetWindowTitle().
325 base::string16
LocaleWindowCaptionFromPageTitle(
326 const base::string16
& expected_title
) {
327 base::string16 page_title
= WindowCaptionFromPageTitle(expected_title
);
329 std::string locale
= g_browser_process
->GetApplicationLocale();
330 if (base::i18n::GetTextDirectionForLocale(locale
.c_str()) ==
331 base::i18n::RIGHT_TO_LEFT
) {
332 base::i18n::WrapStringWithLTRFormatting(&page_title
);
337 // Do we need to use the above code on POSIX as well?
342 // Returns the app extension aptly named "App Test".
343 const Extension
* GetExtension() {
344 extensions::ExtensionRegistry
* registry
=
345 extensions::ExtensionRegistry::Get(browser()->profile());
346 for (const scoped_refptr
<const extensions::Extension
>& extension
:
347 registry
->enabled_extensions()) {
348 if (extension
->name() == "App Test")
349 return extension
.get();
356 // Launch the app on a page with no title, check that the app title was set
358 IN_PROC_BROWSER_TEST_F(BrowserTest
, NoTitle
) {
359 #if defined(OS_WIN) && defined(USE_ASH)
360 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
361 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
362 switches::kAshBrowserTests
))
366 ui_test_utils::NavigateToURL(
367 browser(), ui_test_utils::GetTestUrl(
368 base::FilePath(base::FilePath::kCurrentDirectory
),
369 base::FilePath(kTitle1File
)));
370 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(ASCIIToUTF16("title1.html")),
371 browser()->GetWindowTitleForCurrentTab());
372 base::string16 tab_title
;
373 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title
));
374 EXPECT_EQ(ASCIIToUTF16("title1.html"), tab_title
);
377 // Launch the app, navigate to a page with a title, check that the app title
378 // was set correctly.
379 IN_PROC_BROWSER_TEST_F(BrowserTest
, Title
) {
380 #if defined(OS_WIN) && defined(USE_ASH)
381 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
382 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
383 switches::kAshBrowserTests
))
387 ui_test_utils::NavigateToURL(
388 browser(), ui_test_utils::GetTestUrl(
389 base::FilePath(base::FilePath::kCurrentDirectory
),
390 base::FilePath(kTitle2File
)));
391 const base::string16
test_title(ASCIIToUTF16("Title Of Awesomeness"));
392 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(test_title
),
393 browser()->GetWindowTitleForCurrentTab());
394 base::string16 tab_title
;
395 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title
));
396 EXPECT_EQ(test_title
, tab_title
);
399 IN_PROC_BROWSER_TEST_F(BrowserTest
, JavascriptAlertActivatesTab
) {
400 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
401 base::FilePath::kCurrentDirectory
), base::FilePath(kTitle1File
)));
402 ui_test_utils::NavigateToURL(browser(), url
);
403 AddTabAtIndex(0, url
, ui::PAGE_TRANSITION_TYPED
);
404 EXPECT_EQ(2, browser()->tab_strip_model()->count());
405 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
406 WebContents
* second_tab
= browser()->tab_strip_model()->GetWebContentsAt(1);
407 ASSERT_TRUE(second_tab
);
408 second_tab
->GetMainFrame()->ExecuteJavaScript(
409 ASCIIToUTF16("alert('Activate!');"));
410 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
411 alert
->CloseModalDialog();
412 EXPECT_EQ(2, browser()->tab_strip_model()->count());
413 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
417 #if defined(OS_WIN) && !defined(NDEBUG)
418 // http://crbug.com/114859. Times out frequently on Windows.
419 #define MAYBE_ThirtyFourTabs DISABLED_ThirtyFourTabs
421 #define MAYBE_ThirtyFourTabs ThirtyFourTabs
424 // Create 34 tabs and verify that a lot of processes have been created. The
425 // exact number of processes depends on the amount of memory. Previously we
426 // had a hard limit of 31 processes and this test is mainly directed at
427 // verifying that we don't crash when we pass this limit.
428 // Warning: this test can take >30 seconds when running on a slow (low
429 // memory?) Mac builder.
430 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_ThirtyFourTabs
) {
431 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
432 base::FilePath::kCurrentDirectory
), base::FilePath(kTitle2File
)));
434 // There is one initial tab.
435 const int kTabCount
= 34;
436 for (int ix
= 0; ix
!= (kTabCount
- 1); ++ix
) {
437 chrome::AddSelectedTabWithURL(browser(), url
,
438 ui::PAGE_TRANSITION_TYPED
);
440 EXPECT_EQ(kTabCount
, browser()->tab_strip_model()->count());
442 // See GetMaxRendererProcessCount() in
443 // content/browser/renderer_host/render_process_host_impl.cc
444 // for the algorithm to decide how many processes to create.
445 const int kExpectedProcessCount
=
446 #if defined(ARCH_CPU_64_BITS)
451 if (base::SysInfo::AmountOfPhysicalMemoryMB() >= 2048) {
452 EXPECT_GE(CountRenderProcessHosts(), kExpectedProcessCount
);
454 EXPECT_LT(CountRenderProcessHosts(), kExpectedProcessCount
);
458 // Test that a browser-initiated navigation to an aborted URL load leaves around
459 // a pending entry if we start from the NTP but not from a normal page.
460 // See http://crbug.com/355537.
461 IN_PROC_BROWSER_TEST_F(BrowserTest
, ClearPendingOnFailUnlessNTP
) {
462 ASSERT_TRUE(test_server()->Start());
463 WebContents
* web_contents
=
464 browser()->tab_strip_model()->GetActiveWebContents();
465 GURL
ntp_url(chrome::GetNewTabPageURL(browser()->profile()));
466 ui_test_utils::NavigateToURL(browser(), ntp_url
);
468 // Navigate to a 204 URL (aborts with no content) on the NTP and make sure it
469 // sticks around so that the user can edit it.
470 GURL
abort_url(test_server()->GetURL("nocontent"));
472 content::WindowedNotificationObserver
stop_observer(
473 content::NOTIFICATION_LOAD_STOP
,
474 content::Source
<NavigationController
>(
475 &web_contents
->GetController()));
476 browser()->OpenURL(OpenURLParams(abort_url
, Referrer(), CURRENT_TAB
,
477 ui::PAGE_TRANSITION_TYPED
, false));
478 stop_observer
.Wait();
479 EXPECT_TRUE(web_contents
->GetController().GetPendingEntry());
480 EXPECT_EQ(abort_url
, web_contents
->GetVisibleURL());
483 // Navigate to a real URL.
484 GURL
real_url(test_server()->GetURL("title1.html"));
485 ui_test_utils::NavigateToURL(browser(), real_url
);
486 EXPECT_EQ(real_url
, web_contents
->GetVisibleURL());
488 // Now navigating to a 204 URL should clear the pending entry.
490 content::WindowedNotificationObserver
stop_observer(
491 content::NOTIFICATION_LOAD_STOP
,
492 content::Source
<NavigationController
>(
493 &web_contents
->GetController()));
494 browser()->OpenURL(OpenURLParams(abort_url
, Referrer(), CURRENT_TAB
,
495 ui::PAGE_TRANSITION_TYPED
, false));
496 stop_observer
.Wait();
497 EXPECT_FALSE(web_contents
->GetController().GetPendingEntry());
498 EXPECT_EQ(real_url
, web_contents
->GetVisibleURL());
502 // Test for crbug.com/297289. Ensure that modal dialogs are closed when a
503 // cross-process navigation is ready to commit.
504 // Flaky test, see https://crbug.com/445155.
505 IN_PROC_BROWSER_TEST_F(BrowserTest
, DISABLED_CrossProcessNavCancelsDialogs
) {
506 ASSERT_TRUE(test_server()->Start());
507 host_resolver()->AddRule("www.example.com", "127.0.0.1");
508 GURL
url(test_server()->GetURL("empty.html"));
509 ui_test_utils::NavigateToURL(browser(), url
);
511 // Test this with multiple alert dialogs to ensure that we can navigate away
512 // even if the renderer tries to synchronously create more.
513 // See http://crbug.com/312490.
514 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
515 contents
->GetMainFrame()->ExecuteJavaScript(
516 ASCIIToUTF16("alert('one'); alert('two');"));
517 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
518 EXPECT_TRUE(alert
->IsValid());
519 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
520 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
522 // A cross-site navigation should force the dialog to close.
523 GURL
url2("http://www.example.com/empty.html");
524 ui_test_utils::NavigateToURL(browser(), url2
);
525 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
527 // Make sure input events still work in the renderer process.
528 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
531 // Make sure that dialogs are closed after a renderer process dies, and that
532 // subsequent navigations work. See http://crbug/com/343265.
533 IN_PROC_BROWSER_TEST_F(BrowserTest
, SadTabCancelsDialogs
) {
534 ASSERT_TRUE(test_server()->Start());
535 host_resolver()->AddRule("www.example.com", "127.0.0.1");
536 GURL
beforeunload_url(test_server()->GetURL("files/beforeunload.html"));
537 ui_test_utils::NavigateToURL(browser(), beforeunload_url
);
539 // Start a navigation to trigger the beforeunload dialog.
540 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
541 contents
->GetMainFrame()->ExecuteJavaScript(
542 ASCIIToUTF16("window.location.href = 'data:text/html,foo'"));
543 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
544 EXPECT_TRUE(alert
->IsValid());
545 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
546 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
548 // Crash the renderer process and ensure the dialog is gone.
549 content::RenderProcessHost
* child_process
= contents
->GetRenderProcessHost();
550 content::RenderProcessHostWatcher
crash_observer(
552 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
553 child_process
->Shutdown(0, false);
554 crash_observer
.Wait();
555 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
557 // Make sure subsequent navigations work.
558 GURL
url2("http://www.example.com/files/empty.html");
559 ui_test_utils::NavigateToURL(browser(), url2
);
562 // Make sure that dialogs opened by subframes are closed when the process dies.
563 // See http://crbug.com/366510.
564 IN_PROC_BROWSER_TEST_F(BrowserTest
, SadTabCancelsSubframeDialogs
) {
565 // Navigate to an iframe that opens an alert dialog.
566 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
567 contents
->GetMainFrame()->ExecuteJavaScript(
568 ASCIIToUTF16("window.location.href = 'data:text/html,"
569 "<iframe srcdoc=\"<script>alert(1)</script>\">'"));
570 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
571 EXPECT_TRUE(alert
->IsValid());
572 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
573 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
575 // Crash the renderer process and ensure the dialog is gone.
576 content::RenderProcessHost
* child_process
= contents
->GetRenderProcessHost();
577 content::RenderProcessHostWatcher
crash_observer(
579 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
580 child_process
->Shutdown(0, false);
581 crash_observer
.Wait();
582 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
584 // Make sure subsequent navigations work.
585 GURL
url2("data:text/html,foo");
586 ui_test_utils::NavigateToURL(browser(), url2
);
589 // Make sure modal dialogs within a guestview are closed when an interstitial
590 // page is showing. See crbug.com/482380.
591 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCancelsGuestViewDialogs
) {
592 // Navigate to a PDF, which is loaded within a guestview.
593 ASSERT_TRUE(test_server()->Start());
594 GURL
pdf_with_dialog(test_server()->GetURL("files/alert_dialog.pdf"));
595 ui_test_utils::NavigateToURL(browser(), pdf_with_dialog
);
597 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
598 EXPECT_TRUE(alert
->IsValid());
599 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
600 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
602 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
604 TestInterstitialPage
* interstitial
=
605 new TestInterstitialPage(contents
, false, GURL());
606 content::WaitForInterstitialAttach(contents
);
608 // The interstitial should have closed the dialog.
609 EXPECT_TRUE(contents
->ShowingInterstitialPage());
610 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
612 interstitial
->DontProceed();
615 // Test for crbug.com/22004. Reloading a page with a before unload handler and
616 // then canceling the dialog should not leave the throbber spinning.
617 IN_PROC_BROWSER_TEST_F(BrowserTest
, ReloadThenCancelBeforeUnload
) {
618 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
619 ui_test_utils::NavigateToURL(browser(), url
);
621 // Navigate to another page, but click cancel in the dialog. Make sure that
622 // the throbber stops spinning.
623 chrome::Reload(browser(), CURRENT_TAB
);
624 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
625 alert
->CloseModalDialog();
627 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
629 // Clear the beforeunload handler so the test can easily exit.
630 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
631 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
634 class RedirectObserver
: public content::WebContentsObserver
{
636 explicit RedirectObserver(content::WebContents
* web_contents
)
637 : WebContentsObserver(web_contents
) {
640 void DidNavigateAnyFrame(
641 content::RenderFrameHost
* render_frame_host
,
642 const content::LoadCommittedDetails
& details
,
643 const content::FrameNavigateParams
& params
) override
{
647 void WebContentsDestroyed() override
{
648 // Make sure we don't close the tab while the observer is in scope.
649 // See http://crbug.com/314036.
650 FAIL() << "WebContents closed during navigation (http://crbug.com/314036).";
653 const content::FrameNavigateParams
& params() const {
658 content::FrameNavigateParams params_
;
660 DISALLOW_COPY_AND_ASSIGN(RedirectObserver
);
663 // Ensure that a transferred cross-process navigation does not generate
664 // DidStopLoading events until the navigation commits. If it did, then
665 // ui_test_utils::NavigateToURL would proceed before the URL had committed.
666 // http://crbug.com/243957.
667 IN_PROC_BROWSER_TEST_F(BrowserTest
, NoStopDuringTransferUntilCommit
) {
668 // Create HTTP and HTTPS servers for a cross-site transition.
669 ASSERT_TRUE(test_server()->Start());
670 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
671 net::SpawnedTestServer::kLocalhost
,
672 base::FilePath(kDocRoot
));
673 ASSERT_TRUE(https_test_server
.Start());
675 // Temporarily replace ContentBrowserClient with one that will cause a
676 // process swap on all redirects to HTTPS URLs.
677 TransferHttpsRedirectsContentBrowserClient new_client
;
678 content::ContentBrowserClient
* old_client
=
679 SetBrowserClientForTesting(&new_client
);
681 GURL
init_url(test_server()->GetURL("files/title1.html"));
682 ui_test_utils::NavigateToURL(browser(), init_url
);
684 // Navigate to a same-site page that redirects, causing a transfer.
685 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
687 // Create a RedirectObserver that goes away before we close the tab.
689 RedirectObserver
redirect_observer(contents
);
690 GURL
dest_url(https_test_server
.GetURL("files/title2.html"));
691 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
693 ui_test_utils::NavigateToURL(browser(), redirect_url
);
695 // We should immediately see the new committed entry.
696 EXPECT_FALSE(contents
->GetController().GetPendingEntry());
698 contents
->GetController().GetLastCommittedEntry()->GetURL());
700 // We should keep track of the original request URL, redirect chain, and
701 // page transition type during a transfer, since these are necessary for
702 // history autocomplete to work.
703 EXPECT_EQ(redirect_url
, contents
->GetController().GetLastCommittedEntry()->
704 GetOriginalRequestURL());
705 EXPECT_EQ(2U, redirect_observer
.params().redirects
.size());
706 EXPECT_EQ(redirect_url
, redirect_observer
.params().redirects
.at(0));
707 EXPECT_EQ(dest_url
, redirect_observer
.params().redirects
.at(1));
708 EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
709 redirect_observer
.params().transition
, ui::PAGE_TRANSITION_TYPED
));
712 // Restore previous browser client.
713 SetBrowserClientForTesting(old_client
);
716 // Tests that a cross-process redirect will only cause the beforeunload
717 // handler to run once.
718 IN_PROC_BROWSER_TEST_F(BrowserTest
, SingleBeforeUnloadAfterRedirect
) {
719 // Create HTTP and HTTPS servers for a cross-site transition.
720 ASSERT_TRUE(test_server()->Start());
721 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
722 net::SpawnedTestServer::kLocalhost
,
723 base::FilePath(kDocRoot
));
724 ASSERT_TRUE(https_test_server
.Start());
726 // Temporarily replace ContentBrowserClient with one that will cause a
727 // process swap on all redirects to HTTPS URLs.
728 TransferHttpsRedirectsContentBrowserClient new_client
;
729 content::ContentBrowserClient
* old_client
=
730 SetBrowserClientForTesting(&new_client
);
732 // Navigate to a page with a beforeunload handler.
733 GURL
url(test_server()->GetURL("files/beforeunload.html"));
734 ui_test_utils::NavigateToURL(browser(), url
);
736 // Navigate to a URL that redirects to another process and approve the
737 // beforeunload dialog that pops up.
738 content::WindowedNotificationObserver
nav_observer(
739 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
740 content::NotificationService::AllSources());
741 GURL
https_url(https_test_server
.GetURL("files/title1.html"));
742 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
744 browser()->OpenURL(OpenURLParams(redirect_url
, Referrer(), CURRENT_TAB
,
745 ui::PAGE_TRANSITION_TYPED
, false));
746 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
748 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
749 alert
->native_dialog()->AcceptAppModalDialog();
752 // Restore previous browser client.
753 SetBrowserClientForTesting(old_client
);
756 // Test for crbug.com/80401. Canceling a before unload dialog should reset
757 // the URL to the previous page's URL.
758 IN_PROC_BROWSER_TEST_F(BrowserTest
, CancelBeforeUnloadResetsURL
) {
759 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
760 base::FilePath::kCurrentDirectory
), base::FilePath(kBeforeUnloadFile
)));
761 ui_test_utils::NavigateToURL(browser(), url
);
763 // Navigate to a page that triggers a cross-site transition.
764 ASSERT_TRUE(test_server()->Start());
765 GURL
url2(test_server()->GetURL("files/title1.html"));
766 browser()->OpenURL(OpenURLParams(
767 url2
, Referrer(), CURRENT_TAB
, ui::PAGE_TRANSITION_TYPED
, false));
769 content::WindowedNotificationObserver
host_destroyed_observer(
770 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED
,
771 content::NotificationService::AllSources());
773 // Cancel the dialog.
774 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
775 alert
->CloseModalDialog();
777 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
779 // Verify there are no pending history items after the dialog is cancelled.
780 // (see crbug.com/93858)
781 NavigationEntry
* entry
= browser()->tab_strip_model()->
782 GetActiveWebContents()->GetController().GetPendingEntry();
783 EXPECT_EQ(NULL
, entry
);
785 // Wait for the ShouldClose_ACK to arrive. We can detect it by waiting for
786 // the pending RVH to be destroyed.
787 host_destroyed_observer
.Wait();
788 EXPECT_EQ(url
, browser()->toolbar_model()->GetURL());
790 // Clear the beforeunload handler so the test can easily exit.
791 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
792 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
795 // Test for crbug.com/11647. A page closed with window.close() should not have
796 // two beforeunload dialogs shown.
797 // http://crbug.com/410891
798 IN_PROC_BROWSER_TEST_F(BrowserTest
,
799 DISABLED_SingleBeforeUnloadAfterWindowClose
) {
802 ->GetActiveWebContents()
804 ->ExecuteJavaScriptWithUserGestureForTests(
805 ASCIIToUTF16(kOpenNewBeforeUnloadPage
));
807 // Close the new window with JavaScript, which should show a single
808 // beforeunload dialog. Then show another alert, to make it easy to verify
809 // that a second beforeunload dialog isn't shown.
812 ->GetWebContentsAt(0)
814 ->ExecuteJavaScriptWithUserGestureForTests(
815 ASCIIToUTF16("w.close(); alert('bar');"));
816 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
817 alert
->native_dialog()->AcceptAppModalDialog();
819 alert
= ui_test_utils::WaitForAppModalDialog();
820 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->
821 is_before_unload_dialog());
822 alert
->native_dialog()->AcceptAppModalDialog();
825 // BrowserTest.BeforeUnloadVsBeforeReload times out on Windows.
826 // http://crbug.com/130411
828 #define MAYBE_BeforeUnloadVsBeforeReload DISABLED_BeforeUnloadVsBeforeReload
830 #define MAYBE_BeforeUnloadVsBeforeReload BeforeUnloadVsBeforeReload
833 // Test that when a page has an onunload handler, reloading a page shows a
834 // different dialog than navigating to a different page.
835 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_BeforeUnloadVsBeforeReload
) {
836 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
837 ui_test_utils::NavigateToURL(browser(), url
);
839 // Reload the page, and check that we get a "before reload" dialog.
840 chrome::Reload(browser(), CURRENT_TAB
);
841 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
842 EXPECT_TRUE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
844 // Cancel the reload.
845 alert
->native_dialog()->CancelAppModalDialog();
847 // Navigate to another url, and check that we get a "before unload" dialog.
848 GURL
url2(url::kAboutBlankURL
);
849 browser()->OpenURL(OpenURLParams(
850 url2
, Referrer(), CURRENT_TAB
, ui::PAGE_TRANSITION_TYPED
, false));
852 alert
= ui_test_utils::WaitForAppModalDialog();
853 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
855 // Accept the navigation so we end up on a page without a beforeunload hook.
856 alert
->native_dialog()->AcceptAppModalDialog();
859 // BeforeUnloadAtQuitWithTwoWindows is a regression test for
860 // http://crbug.com/11842. It opens two windows, one of which has a
861 // beforeunload handler and attempts to exit cleanly.
862 class BeforeUnloadAtQuitWithTwoWindows
: public InProcessBrowserTest
{
864 // This test is for testing a specific shutdown behavior. This mimics what
865 // happens in InProcessBrowserTest::RunTestOnMainThread and QuitBrowsers, but
866 // ensures that it happens through the single IDC_EXIT of the test.
867 void TearDownOnMainThread() override
{
868 // Cycle both the MessageLoop and the Cocoa runloop twice to flush out any
869 // Chrome work that generates Cocoa work. Do this twice since there are two
870 // Browsers that must be closed.
874 // Run the application event loop to completion, which will cycle the
875 // native MessagePump on all platforms.
876 base::MessageLoop::current()->PostTask(FROM_HERE
,
877 base::MessageLoop::QuitClosure());
878 base::MessageLoop::current()->Run();
880 // Take care of any remaining Cocoa work.
883 // At this point, quit should be for real now.
884 ASSERT_EQ(0u, chrome::GetTotalBrowserCount());
887 // A helper function that cycles the MessageLoop, and on Mac, the Cocoa run
888 // loop. It also drains the NSAutoreleasePool.
889 void CycleRunLoops() {
890 content::RunAllPendingInMessageLoop();
891 #if defined(OS_MACOSX)
892 chrome::testing::NSRunLoopRunAllPending();
893 AutoreleasePool()->Recycle();
898 // Disabled, http://crbug.com/159214 .
899 IN_PROC_BROWSER_TEST_F(BeforeUnloadAtQuitWithTwoWindows
,
900 DISABLED_IfThisTestTimesOutItIndicatesFAILURE
) {
901 // In the first browser, set up a page that has a beforeunload handler.
902 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
903 ui_test_utils::NavigateToURL(browser(), url
);
905 // Open a second browser window at about:blank.
906 ui_test_utils::BrowserAddedObserver browser_added_observer
;
907 chrome::NewEmptyWindow(browser()->profile(), chrome::GetActiveDesktop());
908 Browser
* second_window
= browser_added_observer
.WaitForSingleNewBrowser();
909 ui_test_utils::NavigateToURL(second_window
, GURL(url::kAboutBlankURL
));
911 // Tell the application to quit. IDC_EXIT calls AttemptUserExit, which on
912 // everything but ChromeOS allows unload handlers to block exit. On that
913 // platform, though, it exits unconditionally. See the comment and bug ID
914 // in AttemptUserExit() in application_lifetime.cc.
915 #if defined(OS_CHROMEOS)
916 chrome::AttemptExit();
918 chrome::ExecuteCommand(second_window
, IDC_EXIT
);
921 // The beforeunload handler will run at exit, ensure it does, and then accept
922 // it to allow shutdown to proceed.
923 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
926 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
927 alert
->native_dialog()->AcceptAppModalDialog();
929 // But wait there's more! If this test times out, it likely means that the
930 // browser has not been able to quit correctly, indicating there's a
931 // regression of the bug noted above.
934 // Test that scripts can fork a new renderer process for a cross-site popup,
935 // based on http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab.
936 // The script must open a new tab, set its window.opener to null, and navigate
937 // it to a cross-site URL. It should also work for meta-refreshes.
938 // See http://crbug.com/93517.
939 IN_PROC_BROWSER_TEST_F(BrowserTest
, NullOpenerRedirectForksProcess
) {
940 base::CommandLine::ForCurrentProcess()->AppendSwitch(
941 switches::kDisablePopupBlocking
);
943 // Create http and https servers for a cross-site transition.
944 ASSERT_TRUE(test_server()->Start());
945 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
946 net::SpawnedTestServer::kLocalhost
,
947 base::FilePath(kDocRoot
));
948 ASSERT_TRUE(https_test_server
.Start());
949 GURL
http_url(test_server()->GetURL("files/title1.html"));
950 GURL
https_url(https_test_server
.GetURL(std::string()));
952 // Start with an http URL.
953 ui_test_utils::NavigateToURL(browser(), http_url
);
954 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
955 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
957 // Now open a tab to a blank page, set its opener to null, and redirect it
959 std::string redirect_popup
= "w=window.open();";
960 redirect_popup
+= "w.opener=null;";
961 redirect_popup
+= "w.document.location=\"";
962 redirect_popup
+= https_url
.spec();
963 redirect_popup
+= "\";";
965 content::WindowedNotificationObserver
popup_observer(
966 chrome::NOTIFICATION_TAB_ADDED
,
967 content::NotificationService::AllSources());
968 content::WindowedNotificationObserver
nav_observer(
969 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
970 content::NotificationService::AllSources());
971 oldtab
->GetMainFrame()->
972 ExecuteJavaScriptWithUserGestureForTests(ASCIIToUTF16(redirect_popup
));
974 // Wait for popup window to appear and finish navigating.
975 popup_observer
.Wait();
976 ASSERT_EQ(2, browser()->tab_strip_model()->count());
977 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
979 EXPECT_NE(oldtab
, newtab
);
981 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
982 EXPECT_EQ(https_url
.spec(),
983 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
985 // Popup window should not be in the opener's process.
986 content::RenderProcessHost
* popup_process
=
987 newtab
->GetRenderProcessHost();
988 EXPECT_NE(process
, popup_process
);
990 // Now open a tab to a blank page, set its opener to null, and use a
991 // meta-refresh to navigate it instead.
992 std::string refresh_popup
= "w=window.open();";
993 refresh_popup
+= "w.opener=null;";
994 refresh_popup
+= "w.document.write(";
995 refresh_popup
+= "'<META HTTP-EQUIV=\"refresh\" content=\"0; url=";
996 refresh_popup
+= https_url
.spec();
997 refresh_popup
+= "\">');w.document.close();";
999 content::WindowedNotificationObserver
popup_observer2(
1000 chrome::NOTIFICATION_TAB_ADDED
,
1001 content::NotificationService::AllSources());
1002 content::WindowedNotificationObserver
nav_observer2(
1003 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1004 content::NotificationService::AllSources());
1005 oldtab
->GetMainFrame()->
1006 ExecuteJavaScriptWithUserGestureForTests(ASCIIToUTF16(refresh_popup
));
1008 // Wait for popup window to appear and finish navigating.
1009 popup_observer2
.Wait();
1010 ASSERT_EQ(3, browser()->tab_strip_model()->count());
1011 WebContents
* newtab2
= browser()->tab_strip_model()->GetActiveWebContents();
1012 EXPECT_TRUE(newtab2
);
1013 EXPECT_NE(oldtab
, newtab2
);
1014 nav_observer2
.Wait();
1015 ASSERT_TRUE(newtab2
->GetController().GetLastCommittedEntry());
1016 EXPECT_EQ(https_url
.spec(),
1017 newtab2
->GetController().GetLastCommittedEntry()->GetURL().spec());
1019 // This popup window should also not be in the opener's process.
1020 content::RenderProcessHost
* popup_process2
=
1021 newtab2
->GetRenderProcessHost();
1022 EXPECT_NE(process
, popup_process2
);
1025 // Tests that other popup navigations that do not follow the steps at
1026 // http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab will not
1027 // fork a new renderer process.
1028 IN_PROC_BROWSER_TEST_F(BrowserTest
, OtherRedirectsDontForkProcess
) {
1029 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1030 switches::kDisablePopupBlocking
);
1032 // Create http and https servers for a cross-site transition.
1033 ASSERT_TRUE(test_server()->Start());
1034 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1035 net::SpawnedTestServer::kLocalhost
,
1036 base::FilePath(kDocRoot
));
1037 ASSERT_TRUE(https_test_server
.Start());
1038 GURL
http_url(test_server()->GetURL("files/title1.html"));
1039 GURL
https_url(https_test_server
.GetURL(std::string()));
1041 // Start with an http URL.
1042 ui_test_utils::NavigateToURL(browser(), http_url
);
1043 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
1044 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
1046 // Now open a tab to a blank page, set its opener to null, and redirect it
1048 std::string dont_fork_popup
= "w=window.open();";
1049 dont_fork_popup
+= "w.document.location=\"";
1050 dont_fork_popup
+= https_url
.spec();
1051 dont_fork_popup
+= "\";";
1053 content::WindowedNotificationObserver
popup_observer(
1054 chrome::NOTIFICATION_TAB_ADDED
,
1055 content::NotificationService::AllSources());
1056 content::WindowedNotificationObserver
nav_observer(
1057 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1058 content::NotificationService::AllSources());
1059 oldtab
->GetMainFrame()->
1060 ExecuteJavaScriptWithUserGestureForTests(ASCIIToUTF16(dont_fork_popup
));
1062 // Wait for popup window to appear and finish navigating.
1063 popup_observer
.Wait();
1064 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1065 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
1066 EXPECT_TRUE(newtab
);
1067 EXPECT_NE(oldtab
, newtab
);
1068 nav_observer
.Wait();
1069 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
1070 EXPECT_EQ(https_url
.spec(),
1071 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1073 // Popup window should still be in the opener's process.
1074 content::RenderProcessHost
* popup_process
=
1075 newtab
->GetRenderProcessHost();
1076 EXPECT_EQ(process
, popup_process
);
1078 // Same thing if the current tab tries to navigate itself.
1079 std::string navigate_str
= "document.location=\"";
1080 navigate_str
+= https_url
.spec();
1081 navigate_str
+= "\";";
1083 content::WindowedNotificationObserver
nav_observer2(
1084 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1085 content::NotificationService::AllSources());
1086 oldtab
->GetMainFrame()->ExecuteJavaScriptWithUserGestureForTests(
1087 ASCIIToUTF16(navigate_str
));
1088 nav_observer2
.Wait();
1089 ASSERT_TRUE(oldtab
->GetController().GetLastCommittedEntry());
1090 EXPECT_EQ(https_url
.spec(),
1091 oldtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1093 // Original window should still be in the original process.
1094 content::RenderProcessHost
* new_process
= newtab
->GetRenderProcessHost();
1095 EXPECT_EQ(process
, new_process
);
1098 // Test that get_process_idle_time() returns reasonable values when compared
1099 // with time deltas measured locally.
1100 IN_PROC_BROWSER_TEST_F(BrowserTest
, RenderIdleTime
) {
1101 base::TimeTicks start
= base::TimeTicks::Now();
1102 ui_test_utils::NavigateToURL(
1103 browser(), ui_test_utils::GetTestUrl(
1104 base::FilePath(base::FilePath::kCurrentDirectory
),
1105 base::FilePath(kTitle1File
)));
1106 content::RenderProcessHost::iterator
it(
1107 content::RenderProcessHost::AllHostsIterator());
1108 for (; !it
.IsAtEnd(); it
.Advance()) {
1109 base::TimeDelta renderer_td
=
1110 it
.GetCurrentValue()->GetChildProcessIdleTime();
1111 base::TimeDelta browser_td
= base::TimeTicks::Now() - start
;
1112 EXPECT_TRUE(browser_td
>= renderer_td
);
1116 // Test IDC_CREATE_SHORTCUTS command is enabled for url scheme file, ftp, http
1117 // and https and disabled for chrome://, about:// etc.
1118 // TODO(pinkerton): Disable app-mode in the model until we implement it
1119 // on the Mac. http://crbug.com/13148
1120 #if !defined(OS_MACOSX)
1121 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFile
) {
1122 CommandUpdater
* command_updater
=
1123 browser()->command_controller()->command_updater();
1125 static const base::FilePath::CharType
* kEmptyFile
=
1126 FILE_PATH_LITERAL("empty.html");
1127 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1128 base::FilePath::kCurrentDirectory
), base::FilePath(kEmptyFile
)));
1129 ASSERT_TRUE(file_url
.SchemeIs(url::kFileScheme
));
1130 ui_test_utils::NavigateToURL(browser(), file_url
);
1131 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1134 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttp
) {
1135 CommandUpdater
* command_updater
=
1136 browser()->command_controller()->command_updater();
1138 ASSERT_TRUE(test_server()->Start());
1139 GURL
http_url(test_server()->GetURL(std::string()));
1140 ASSERT_TRUE(http_url
.SchemeIs(url::kHttpScheme
));
1141 ui_test_utils::NavigateToURL(browser(), http_url
);
1142 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1145 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttps
) {
1146 CommandUpdater
* command_updater
=
1147 browser()->command_controller()->command_updater();
1149 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1150 net::SpawnedTestServer::kLocalhost
,
1151 base::FilePath(kDocRoot
));
1152 ASSERT_TRUE(test_server
.Start());
1153 GURL
https_url(test_server
.GetURL("/"));
1154 ASSERT_TRUE(https_url
.SchemeIs(url::kHttpsScheme
));
1155 ui_test_utils::NavigateToURL(browser(), https_url
);
1156 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1159 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFtp
) {
1160 CommandUpdater
* command_updater
=
1161 browser()->command_controller()->command_updater();
1163 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_FTP
,
1164 net::SpawnedTestServer::kLocalhost
,
1165 base::FilePath(kDocRoot
));
1166 ASSERT_TRUE(test_server
.Start());
1167 GURL
ftp_url(test_server
.GetURL(std::string()));
1168 ASSERT_TRUE(ftp_url
.SchemeIs(url::kFtpScheme
));
1169 ui_test_utils::NavigateToURL(browser(), ftp_url
);
1170 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1173 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutInvalid
) {
1174 CommandUpdater
* command_updater
=
1175 browser()->command_controller()->command_updater();
1177 // Urls that should not have shortcuts.
1178 GURL
new_tab_url(chrome::kChromeUINewTabURL
);
1179 ui_test_utils::NavigateToURL(browser(), new_tab_url
);
1180 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1182 GURL
history_url(chrome::kChromeUIHistoryURL
);
1183 ui_test_utils::NavigateToURL(browser(), history_url
);
1184 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1186 GURL
downloads_url(chrome::kChromeUIDownloadsURL
);
1187 ui_test_utils::NavigateToURL(browser(), downloads_url
);
1188 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1190 GURL
blank_url(url::kAboutBlankURL
);
1191 ui_test_utils::NavigateToURL(browser(), blank_url
);
1192 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1195 // Change a tab into an application window.
1196 // DISABLED: http://crbug.com/72310
1197 IN_PROC_BROWSER_TEST_F(BrowserTest
, DISABLED_ConvertTabToAppShortcut
) {
1198 ASSERT_TRUE(test_server()->Start());
1199 GURL
http_url(test_server()->GetURL(std::string()));
1200 ASSERT_TRUE(http_url
.SchemeIs(url::kHttpScheme
));
1202 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1203 WebContents
* initial_tab
= browser()->tab_strip_model()->GetWebContentsAt(0);
1204 WebContents
* app_tab
= chrome::AddSelectedTabWithURL(
1205 browser(), http_url
, ui::PAGE_TRANSITION_TYPED
);
1206 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1207 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
1208 browser()->host_desktop_type()));
1210 // Normal tabs should accept load drops.
1211 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1212 EXPECT_TRUE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1214 // Turn |app_tab| into a tab in an app panel.
1215 chrome::ConvertTabToAppWindow(browser(), app_tab
);
1217 // The launch should have created a new browser.
1218 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1219 browser()->host_desktop_type()));
1221 // Find the new browser.
1222 Browser
* app_browser
= NULL
;
1223 for (chrome::BrowserIterator it
; !it
.done() && !app_browser
; it
.Next()) {
1224 if (*it
!= browser())
1227 ASSERT_TRUE(app_browser
);
1229 // Check that the tab contents is in the new browser, and not in the old.
1230 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1231 ASSERT_EQ(initial_tab
, browser()->tab_strip_model()->GetWebContentsAt(0));
1233 // Check that the appliaction browser has a single tab, and that tab contains
1234 // the content that we app-ified.
1235 ASSERT_EQ(1, app_browser
->tab_strip_model()->count());
1236 ASSERT_EQ(app_tab
, app_browser
->tab_strip_model()->GetWebContentsAt(0));
1238 // Normal tabs should accept load drops.
1239 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1241 // The tab in an app window should not.
1242 EXPECT_FALSE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1245 #endif // !defined(OS_MACOSX)
1247 // Test RenderView correctly send back favicon url for web page that redirects
1248 // to an anchor in javascript body.onload handler.
1249 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1250 DISABLED_FaviconOfOnloadRedirectToAnchorPage
) {
1251 ASSERT_TRUE(test_server()->Start());
1252 GURL
url(test_server()->GetURL("files/onload_redirect_to_anchor.html"));
1253 GURL
expected_favicon_url(test_server()->GetURL("files/test.png"));
1255 ui_test_utils::NavigateToURL(browser(), url
);
1257 NavigationEntry
* entry
= browser()->tab_strip_model()->
1258 GetActiveWebContents()->GetController().GetLastCommittedEntry();
1259 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1262 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined (OS_WIN)
1263 // http://crbug.com/83828. On Mac 10.6, the failure rate is 14%
1264 #define MAYBE_FaviconChange DISABLED_FaviconChange
1266 #define MAYBE_FaviconChange FaviconChange
1268 // Test that an icon can be changed from JS.
1269 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_FaviconChange
) {
1270 static const base::FilePath::CharType
* kFile
=
1271 FILE_PATH_LITERAL("onload_change_favicon.html");
1272 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1273 base::FilePath::kCurrentDirectory
), base::FilePath(kFile
)));
1274 ASSERT_TRUE(file_url
.SchemeIs(url::kFileScheme
));
1275 ui_test_utils::NavigateToURL(browser(), file_url
);
1277 NavigationEntry
* entry
= browser()->tab_strip_model()->
1278 GetActiveWebContents()->GetController().GetLastCommittedEntry();
1279 static const base::FilePath::CharType
* kIcon
=
1280 FILE_PATH_LITERAL("test1.png");
1281 GURL
expected_favicon_url(ui_test_utils::GetTestUrl(base::FilePath(
1282 base::FilePath::kCurrentDirectory
), base::FilePath(kIcon
)));
1283 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1286 // http://crbug.com/172336
1288 #define MAYBE_TabClosingWhenRemovingExtension \
1289 DISABLED_TabClosingWhenRemovingExtension
1291 #define MAYBE_TabClosingWhenRemovingExtension TabClosingWhenRemovingExtension
1293 // Makes sure TabClosing is sent when uninstalling an extension that is an app
1295 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_TabClosingWhenRemovingExtension
) {
1296 ASSERT_TRUE(test_server()->Start());
1297 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1298 GURL
url(test_server()->GetURL("empty.html"));
1299 TabStripModel
* model
= browser()->tab_strip_model();
1301 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1303 const Extension
* extension_app
= GetExtension();
1305 ui_test_utils::NavigateToURL(browser(), url
);
1307 WebContents
* app_contents
= WebContents::Create(
1308 WebContents::CreateParams(browser()->profile()));
1309 extensions::TabHelper::CreateForWebContents(app_contents
);
1310 extensions::TabHelper
* extensions_tab_helper
=
1311 extensions::TabHelper::FromWebContents(app_contents
);
1312 extensions_tab_helper
->SetExtensionApp(extension_app
);
1314 model
->AddWebContents(app_contents
, 0, ui::PageTransitionFromInt(0),
1315 TabStripModel::ADD_NONE
);
1316 model
->SetTabPinned(0, true);
1317 ui_test_utils::NavigateToURL(browser(), url
);
1319 MockTabStripModelObserver observer
;
1320 model
->AddObserver(&observer
);
1322 // Uninstall the extension and make sure TabClosing is sent.
1323 ExtensionService
* service
= extensions::ExtensionSystem::Get(
1324 browser()->profile())->extension_service();
1325 service
->UninstallExtension(GetExtension()->id(),
1326 extensions::UNINSTALL_REASON_FOR_TESTING
,
1327 base::Bind(&base::DoNothing
),
1329 EXPECT_EQ(1, observer
.closing_count());
1331 model
->RemoveObserver(&observer
);
1333 // There should only be one tab now.
1334 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1337 // Open with --app-id=<id>, and see that an application tab opens by default.
1338 IN_PROC_BROWSER_TEST_F(BrowserTest
, AppIdSwitch
) {
1339 ASSERT_TRUE(test_server()->Start());
1341 // There should be one tab to start with.
1342 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1345 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1346 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1347 const Extension
* extension_app
= GetExtension();
1349 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
1350 command_line
.AppendSwitchASCII(switches::kAppId
, extension_app
->id());
1352 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1353 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1354 StartupBrowserCreatorImpl
launch(base::FilePath(), command_line
, first_run
);
1356 bool new_bookmark_apps_enabled
= extensions::util::IsNewBookmarkAppsEnabled();
1358 // If the new bookmark app flow is enabled, the app should open as an tab.
1359 // Otherwise the app should open as an app window.
1360 EXPECT_EQ(!new_bookmark_apps_enabled
,
1361 launch
.OpenApplicationWindow(browser()->profile(), NULL
));
1362 EXPECT_EQ(new_bookmark_apps_enabled
,
1363 launch
.OpenApplicationTab(browser()->profile()));
1365 // Check that a the number of browsers and tabs is correct.
1366 unsigned int expected_browsers
= 1;
1367 int expected_tabs
= 1;
1368 new_bookmark_apps_enabled
? expected_tabs
++ : expected_browsers
++;
1370 EXPECT_EQ(expected_browsers
,
1371 chrome::GetBrowserCount(browser()->profile(),
1372 browser()->host_desktop_type()));
1373 EXPECT_EQ(expected_tabs
, browser()->tab_strip_model()->count());
1376 // Open an app window and the dev tools window and ensure that the location
1377 // bar settings are correct.
1378 IN_PROC_BROWSER_TEST_F(BrowserTest
, ShouldShowLocationBar
) {
1379 ASSERT_TRUE(test_server()->Start());
1382 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1383 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1384 const Extension
* extension_app
= GetExtension();
1386 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1387 WebContents
* app_window
= OpenApplication(AppLaunchParams(
1388 browser()->profile(), extension_app
, extensions::LAUNCH_CONTAINER_WINDOW
,
1389 NEW_WINDOW
, extensions::SOURCE_TEST
));
1390 ASSERT_TRUE(app_window
);
1392 DevToolsWindow
* devtools_window
=
1393 DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), false);
1395 // The launch should have created a new app browser and a dev tools browser.
1397 chrome::GetBrowserCount(browser()->profile(),
1398 browser()->host_desktop_type()));
1400 // Find the new browsers.
1401 Browser
* app_browser
= NULL
;
1402 Browser
* dev_tools_browser
= NULL
;
1403 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
1404 if (*it
== browser()) {
1406 } else if ((*it
)->app_name() == DevToolsWindow::kDevToolsApp
) {
1407 dev_tools_browser
= *it
;
1412 ASSERT_TRUE(dev_tools_browser
);
1413 ASSERT_TRUE(app_browser
);
1414 ASSERT_TRUE(app_browser
!= browser());
1417 dev_tools_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1419 app_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1421 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_window
);
1424 // Tests that the CLD (Compact Language Detection) works properly.
1425 IN_PROC_BROWSER_TEST_F(BrowserTest
, PageLanguageDetection
) {
1426 scoped_ptr
<test::CldDataHarness
> cld_data_harness
=
1427 test::CldDataHarnessFactory::Get()->CreateCldDataHarness();
1428 ASSERT_NO_FATAL_FAILURE(cld_data_harness
->Init());
1429 ASSERT_TRUE(test_server()->Start());
1431 translate::LanguageDetectionDetails details
;
1433 // Open a new tab with a page in English.
1434 AddTabAtIndex(0, GURL(test_server()->GetURL("files/english_page.html")),
1435 ui::PAGE_TRANSITION_TYPED
);
1437 WebContents
* current_web_contents
=
1438 browser()->tab_strip_model()->GetActiveWebContents();
1439 ChromeTranslateClient
* chrome_translate_client
=
1440 ChromeTranslateClient::FromWebContents(current_web_contents
);
1441 content::Source
<WebContents
> source(current_web_contents
);
1443 ui_test_utils::WindowedNotificationObserverWithDetails
<
1444 translate::LanguageDetectionDetails
>
1445 en_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1448 chrome_translate_client
->GetLanguageState().original_language());
1449 en_language_detected_signal
.Wait();
1450 EXPECT_TRUE(en_language_detected_signal
.GetDetailsFor(
1451 source
.map_key(), &details
));
1452 EXPECT_EQ("en", details
.adopted_language
);
1454 chrome_translate_client
->GetLanguageState().original_language());
1456 // Now navigate to a page in French.
1457 ui_test_utils::WindowedNotificationObserverWithDetails
<
1458 translate::LanguageDetectionDetails
>
1459 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1461 ui_test_utils::NavigateToURL(
1462 browser(), GURL(test_server()->GetURL("files/french_page.html")));
1463 fr_language_detected_signal
.Wait();
1464 details
.adopted_language
.clear();
1465 EXPECT_TRUE(fr_language_detected_signal
.GetDetailsFor(
1466 source
.map_key(), &details
));
1467 EXPECT_EQ("fr", details
.adopted_language
);
1469 chrome_translate_client
->GetLanguageState().original_language());
1472 // Chromeos defaults to restoring the last session, so this test isn't
1474 #if !defined(OS_CHROMEOS)
1475 #if defined(OS_MACOSX)
1476 // Crashy, http://crbug.com/38522
1477 #define RestorePinnedTabs DISABLED_RestorePinnedTabs
1479 // Makes sure pinned tabs are restored correctly on start.
1480 IN_PROC_BROWSER_TEST_F(BrowserTest
, RestorePinnedTabs
) {
1481 ASSERT_TRUE(test_server()->Start());
1483 // Add an pinned app tab.
1484 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1485 GURL
url(test_server()->GetURL("empty.html"));
1486 TabStripModel
* model
= browser()->tab_strip_model();
1487 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1488 const Extension
* extension_app
= GetExtension();
1489 ui_test_utils::NavigateToURL(browser(), url
);
1490 WebContents
* app_contents
= WebContents::Create(
1491 WebContents::CreateParams(browser()->profile()));
1492 extensions::TabHelper::CreateForWebContents(app_contents
);
1493 extensions::TabHelper
* extensions_tab_helper
=
1494 extensions::TabHelper::FromWebContents(app_contents
);
1495 extensions_tab_helper
->SetExtensionApp(extension_app
);
1496 model
->AddWebContents(app_contents
, 0, ui::PageTransitionFromInt(0),
1497 TabStripModel::ADD_NONE
);
1498 model
->SetTabPinned(0, true);
1499 ui_test_utils::NavigateToURL(browser(), url
);
1501 // Add a non pinned tab.
1502 chrome::NewTab(browser());
1504 // Add a pinned non-app tab.
1505 chrome::NewTab(browser());
1506 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL
));
1507 model
->SetTabPinned(2, true);
1509 // Write out the pinned tabs.
1510 PinnedTabCodec::WritePinnedTabs(browser()->profile());
1512 // Simulate launching again.
1513 base::CommandLine
dummy(base::CommandLine::NO_PROGRAM
);
1514 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1515 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1516 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, first_run
);
1517 launch
.profile_
= browser()->profile();
1518 launch
.ProcessStartupURLs(std::vector
<GURL
>(),
1519 browser()->host_desktop_type());
1521 // The launch should have created a new browser.
1522 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1523 browser()->host_desktop_type()));
1525 // Find the new browser.
1526 Browser
* new_browser
= NULL
;
1527 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1528 if (*it
!= browser())
1531 ASSERT_TRUE(new_browser
);
1532 ASSERT_TRUE(new_browser
!= browser());
1534 // We should get back an additional tab for the app, and another for the
1535 // default home page.
1536 ASSERT_EQ(3, new_browser
->tab_strip_model()->count());
1538 // Make sure the state matches.
1539 TabStripModel
* new_model
= new_browser
->tab_strip_model();
1540 EXPECT_TRUE(new_model
->IsAppTab(0));
1541 EXPECT_FALSE(new_model
->IsAppTab(1));
1542 EXPECT_FALSE(new_model
->IsAppTab(2));
1544 EXPECT_TRUE(new_model
->IsTabPinned(0));
1545 EXPECT_TRUE(new_model
->IsTabPinned(1));
1546 EXPECT_FALSE(new_model
->IsTabPinned(2));
1548 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
1549 new_model
->GetWebContentsAt(2)->GetURL());
1552 extensions::TabHelper::FromWebContents(
1553 new_model
->GetWebContentsAt(0))->extension_app() == extension_app
);
1555 #endif // !defined(OS_CHROMEOS)
1557 // This test verifies we don't crash when closing the last window and the app
1559 IN_PROC_BROWSER_TEST_F(BrowserTest
, CloseWithAppMenuOpen
) {
1560 if (browser_defaults::kBrowserAliveWithNoWindows
)
1563 // We need a message loop running for menus on windows.
1564 base::MessageLoop::current()->PostTask(
1565 FROM_HERE
, base::Bind(&RunCloseWithAppMenuCallback
, browser()));
1568 #if !defined(OS_MACOSX)
1569 IN_PROC_BROWSER_TEST_F(BrowserTest
, OpenAppWindowLikeNtp
) {
1570 ASSERT_TRUE(test_server()->Start());
1573 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1574 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1575 const Extension
* extension_app
= GetExtension();
1577 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1578 WebContents
* app_window
= OpenApplication(AppLaunchParams(
1579 browser()->profile(), extension_app
, extensions::LAUNCH_CONTAINER_WINDOW
,
1580 NEW_WINDOW
, extensions::SOURCE_TEST
));
1581 ASSERT_TRUE(app_window
);
1583 // Apps launched in a window from the NTP have an extensions tab helper but
1584 // do not have extension_app set in it.
1585 ASSERT_TRUE(extensions::TabHelper::FromWebContents(app_window
));
1587 extensions::TabHelper::FromWebContents(app_window
)->extension_app());
1588 EXPECT_EQ(extensions::AppLaunchInfo::GetFullLaunchURL(extension_app
),
1589 app_window
->GetURL());
1591 // The launch should have created a new browser.
1592 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1593 browser()->host_desktop_type()));
1595 // Find the new browser.
1596 Browser
* new_browser
= NULL
;
1597 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1598 if (*it
!= browser())
1601 ASSERT_TRUE(new_browser
);
1602 ASSERT_TRUE(new_browser
!= browser());
1604 EXPECT_TRUE(new_browser
->is_app());
1606 // The browser's app name should include the extension's id.
1607 std::string app_name
= new_browser
->app_name_
;
1608 EXPECT_NE(app_name
.find(extension_app
->id()), std::string::npos
)
1609 << "Name " << app_name
<< " should contain id "<< extension_app
->id();
1611 #endif // !defined(OS_MACOSX)
1613 // Makes sure the browser doesn't crash when
1614 // set_show_state(ui::SHOW_STATE_MAXIMIZED) has been invoked.
1615 IN_PROC_BROWSER_TEST_F(BrowserTest
, StartMaximized
) {
1616 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1617 for (size_t i
= 0; i
< arraysize(types
); ++i
) {
1618 Browser::CreateParams
params(types
[i
], browser()->profile(),
1619 browser()->host_desktop_type());
1620 params
.initial_show_state
= ui::SHOW_STATE_MAXIMIZED
;
1621 AddBlankTabAndShow(new Browser(params
));
1625 // Aura doesn't support minimized window. crbug.com/104571.
1626 #if defined(USE_AURA)
1627 #define MAYBE_StartMinimized DISABLED_StartMinimized
1629 #define MAYBE_StartMinimized StartMinimized
1631 // Makes sure the browser doesn't crash when
1632 // set_show_state(ui::SHOW_STATE_MINIMIZED) has been invoked.
1633 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_StartMinimized
) {
1634 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1635 for (size_t i
= 0; i
< arraysize(types
); ++i
) {
1636 Browser::CreateParams
params(types
[i
], browser()->profile(),
1637 browser()->host_desktop_type());
1638 params
.initial_show_state
= ui::SHOW_STATE_MINIMIZED
;
1639 AddBlankTabAndShow(new Browser(params
));
1643 // Makes sure the forward button is disabled immediately when navigating
1644 // forward to a slow-to-commit page.
1645 IN_PROC_BROWSER_TEST_F(BrowserTest
, ForwardDisabledOnForward
) {
1646 GURL
blank_url(url::kAboutBlankURL
);
1647 ui_test_utils::NavigateToURL(browser(), blank_url
);
1649 ui_test_utils::NavigateToURL(
1650 browser(), ui_test_utils::GetTestUrl(
1651 base::FilePath(base::FilePath::kCurrentDirectory
),
1652 base::FilePath(kTitle1File
)));
1654 content::WindowedNotificationObserver
back_nav_load_observer(
1655 content::NOTIFICATION_LOAD_STOP
,
1656 content::Source
<NavigationController
>(
1657 &browser()->tab_strip_model()->GetActiveWebContents()->
1659 chrome::GoBack(browser(), CURRENT_TAB
);
1660 back_nav_load_observer
.Wait();
1661 CommandUpdater
* command_updater
=
1662 browser()->command_controller()->command_updater();
1663 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1665 content::WindowedNotificationObserver
forward_nav_load_observer(
1666 content::NOTIFICATION_LOAD_STOP
,
1667 content::Source
<NavigationController
>(
1668 &browser()->tab_strip_model()->GetActiveWebContents()->
1670 chrome::GoForward(browser(), CURRENT_TAB
);
1671 // This check will happen before the navigation completes, since the browser
1672 // won't process the renderer's response until the Wait() call below.
1673 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1674 forward_nav_load_observer
.Wait();
1677 // Makes sure certain commands are disabled when Incognito mode is forced.
1678 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisableMenuItemsWhenIncognitoIsForced
) {
1679 CommandUpdater
* command_updater
=
1680 browser()->command_controller()->command_updater();
1681 // At the beginning, all commands are enabled.
1682 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1683 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1684 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1685 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1686 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1687 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1689 // Set Incognito to FORCED.
1690 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1691 IncognitoModePrefs::FORCED
);
1692 // Bookmarks & Settings commands should get disabled.
1693 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1694 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1695 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1696 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1697 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1698 // New Incognito Window command, however, should be enabled.
1699 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1701 // Create a new browser.
1702 Browser
* new_browser
=
1703 new Browser(Browser::CreateParams(
1704 browser()->profile()->GetOffTheRecordProfile(),
1705 browser()->host_desktop_type()));
1706 CommandUpdater
* new_command_updater
=
1707 new_browser
->command_controller()->command_updater();
1708 // It should have Bookmarks & Settings commands disabled by default.
1709 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1710 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(
1711 IDC_SHOW_BOOKMARK_MANAGER
));
1712 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1713 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1714 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1715 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1718 // Makes sure New Incognito Window command is disabled when Incognito mode is
1720 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1721 NoNewIncognitoWindowWhenIncognitoIsDisabled
) {
1722 CommandUpdater
* command_updater
=
1723 browser()->command_controller()->command_updater();
1724 // Set Incognito to DISABLED.
1725 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1726 IncognitoModePrefs::DISABLED
);
1727 // Make sure New Incognito Window command is disabled. All remaining commands
1728 // should be enabled.
1729 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1730 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1731 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1732 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1733 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1734 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1736 // Create a new browser.
1737 Browser
* new_browser
=
1738 new Browser(Browser::CreateParams(browser()->profile(),
1739 browser()->host_desktop_type()));
1740 CommandUpdater
* new_command_updater
=
1741 new_browser
->command_controller()->command_updater();
1742 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1743 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1744 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1745 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1746 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1747 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1750 // Makes sure Extensions and Settings commands are disabled in certain
1751 // circumstances even though normally they should stay enabled.
1752 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1753 DisableExtensionsAndSettingsWhenIncognitoIsDisabled
) {
1754 CommandUpdater
* command_updater
=
1755 browser()->command_controller()->command_updater();
1756 // Disable extensions. This should disable Extensions menu.
1757 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
1758 set_extensions_enabled(false);
1759 // Set Incognito to DISABLED.
1760 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1761 IncognitoModePrefs::DISABLED
);
1762 // Make sure Manage Extensions command is disabled.
1763 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1764 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1765 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1766 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1767 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1769 // Create a popup (non-main-UI-type) browser. Settings command as well
1770 // as Extensions should be disabled.
1771 Browser
* popup_browser
= new Browser(
1772 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1773 browser()->host_desktop_type()));
1774 CommandUpdater
* popup_command_updater
=
1775 popup_browser
->command_controller()->command_updater();
1776 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1777 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1778 EXPECT_TRUE(popup_command_updater
->IsCommandEnabled(
1779 IDC_SHOW_BOOKMARK_MANAGER
));
1780 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1783 // Makes sure Extensions and Settings commands are disabled in certain
1784 // circumstances even though normally they should stay enabled.
1785 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1786 DisableOptionsAndImportMenuItemsConsistently
) {
1787 // Create a popup browser.
1788 Browser
* popup_browser
= new Browser(
1789 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1790 browser()->host_desktop_type()));
1791 CommandUpdater
* command_updater
=
1792 popup_browser
->command_controller()->command_updater();
1793 // OPTIONS and IMPORT_SETTINGS are disabled for a non-normal UI.
1794 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1795 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1797 // Set Incognito to FORCED.
1798 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1799 IncognitoModePrefs::FORCED
);
1800 // OPTIONS and IMPORT_SETTINGS are disabled when Incognito is forced.
1801 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1802 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1803 // Set Incognito to AVAILABLE.
1804 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1805 IncognitoModePrefs::ENABLED
);
1806 // OPTIONS and IMPORT_SETTINGS are still disabled since it is a non-normal UI.
1807 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1808 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1813 void OnZoomLevelChanged(const base::Closure
& callback
,
1814 const HostZoomMap::ZoomLevelChange
& host
) {
1821 // Flakes regularly on Windows XP
1822 // http://crbug.com/146040
1823 #define MAYBE_PageZoom DISABLED_PageZoom
1825 #define MAYBE_PageZoom PageZoom
1830 int GetZoomPercent(const content::WebContents
* contents
,
1832 bool* enable_minus
) {
1834 ui_zoom::ZoomController::FromWebContents(contents
)->GetZoomPercent();
1835 *enable_plus
= percent
< contents
->GetMaximumZoomPercent();
1836 *enable_minus
= percent
> contents
->GetMinimumZoomPercent();
1842 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_PageZoom
) {
1843 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1844 bool enable_plus
, enable_minus
;
1847 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1848 new content::MessageLoopRunner
);
1849 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1850 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1851 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1852 content::HostZoomMap::GetDefaultForBrowserContext(
1853 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1854 chrome::Zoom(browser(), content::PAGE_ZOOM_IN
);
1857 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 110);
1858 EXPECT_TRUE(enable_plus
);
1859 EXPECT_TRUE(enable_minus
);
1863 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1864 new content::MessageLoopRunner
);
1865 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1866 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1867 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1868 content::HostZoomMap::GetDefaultForBrowserContext(
1869 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1870 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1873 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 100);
1874 EXPECT_TRUE(enable_plus
);
1875 EXPECT_TRUE(enable_minus
);
1879 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1880 new content::MessageLoopRunner
);
1881 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1882 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1883 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1884 content::HostZoomMap::GetDefaultForBrowserContext(
1885 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1886 chrome::Zoom(browser(), content::PAGE_ZOOM_OUT
);
1889 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 90);
1890 EXPECT_TRUE(enable_plus
);
1891 EXPECT_TRUE(enable_minus
);
1894 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1897 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCommandDisable
) {
1898 ASSERT_TRUE(test_server()->Start());
1899 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1900 GURL
url(test_server()->GetURL("empty.html"));
1901 ui_test_utils::NavigateToURL(browser(), url
);
1903 CommandUpdater
* command_updater
=
1904 browser()->command_controller()->command_updater();
1905 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1906 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1907 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1908 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1910 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1912 TestInterstitialPage
* interstitial
=
1913 new TestInterstitialPage(contents
, false, GURL());
1914 content::WaitForInterstitialAttach(contents
);
1916 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1918 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1919 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1920 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1921 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1923 // Proceed and wait for interstitial to detach. This doesn't destroy
1925 interstitial
->Proceed();
1926 content::WaitForInterstitialDetach(contents
);
1927 // interstitial is deleted now.
1929 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1930 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1931 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1932 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1935 // Ensure that creating an interstitial page closes any JavaScript dialogs
1936 // that were present on the previous page. See http://crbug.com/295695.
1937 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialClosesDialogs
) {
1938 ASSERT_TRUE(test_server()->Start());
1939 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1940 GURL
url(test_server()->GetURL("empty.html"));
1941 ui_test_utils::NavigateToURL(browser(), url
);
1943 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1944 contents
->GetMainFrame()->ExecuteJavaScript(
1945 ASCIIToUTF16("alert('Dialog showing!');"));
1946 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
1947 EXPECT_TRUE(alert
->IsValid());
1948 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
1949 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
1951 TestInterstitialPage
* interstitial
=
1952 new TestInterstitialPage(contents
, false, GURL());
1953 content::WaitForInterstitialAttach(contents
);
1955 // The interstitial should have closed the dialog.
1956 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1957 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
1959 // Don't proceed and wait for interstitial to detach. This doesn't destroy
1961 interstitial
->DontProceed();
1962 content::WaitForInterstitialDetach(contents
);
1963 // interstitial is deleted now.
1965 // Make sure input events still work in the renderer process.
1966 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
1970 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCloseTab
) {
1971 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1973 // Interstitial will delete itself when we close the tab.
1974 new TestInterstitialPage(contents
, false, GURL());
1975 content::WaitForInterstitialAttach(contents
);
1977 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1979 // Close the tab and wait for interstitial detach. This destroys |contents|.
1980 content::RunTaskAndWaitForInterstitialDetach(
1981 contents
, base::Bind(&chrome::CloseTab
, browser()));
1982 // interstitial is deleted now.
1985 class MockWebContentsObserver
: public WebContentsObserver
{
1987 explicit MockWebContentsObserver(WebContents
* web_contents
)
1988 : WebContentsObserver(web_contents
),
1989 got_user_gesture_(false) {
1992 void DidGetUserGesture() override
{ got_user_gesture_
= true; }
1994 bool got_user_gesture() const {
1995 return got_user_gesture_
;
1998 void set_got_user_gesture(bool got_it
) {
1999 got_user_gesture_
= got_it
;
2003 bool got_user_gesture_
;
2005 DISALLOW_COPY_AND_ASSIGN(MockWebContentsObserver
);
2008 IN_PROC_BROWSER_TEST_F(BrowserTest
, UserGesturesReported
) {
2009 // Regression test for http://crbug.com/110707. Also tests that a user
2010 // gesture is sent when a normal navigation (via e.g. the omnibox) is
2012 WebContents
* web_contents
=
2013 browser()->tab_strip_model()->GetActiveWebContents();
2014 MockWebContentsObserver
mock_observer(web_contents
);
2016 ASSERT_TRUE(test_server()->Start());
2017 GURL
url(test_server()->GetURL("empty.html"));
2019 ui_test_utils::NavigateToURL(browser(), url
);
2020 EXPECT_TRUE(mock_observer
.got_user_gesture());
2022 mock_observer
.set_got_user_gesture(false);
2023 chrome::Reload(browser(), CURRENT_TAB
);
2024 EXPECT_TRUE(mock_observer
.got_user_gesture());
2027 // TODO(ben): this test was never enabled. It has bit-rotted since being added.
2028 // It originally lived in browser_unittest.cc, but has been moved here to make
2029 // room for real browser unit tests.
2031 class BrowserTest2
: public InProcessBrowserTest
{
2034 host_resolver_proc_
= new net::RuleBasedHostResolverProc(NULL
);
2035 // Avoid making external DNS lookups. In this test we don't need this
2037 host_resolver_proc_
->AddSimulatedFailure("*.google.com");
2038 scoped_host_resolver_proc_
.Init(host_resolver_proc_
.get());
2042 scoped_refptr
<net::RuleBasedHostResolverProc
> host_resolver_proc_
;
2043 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_
;
2046 IN_PROC_BROWSER_TEST_F(BrowserTest2
, NoTabsInPopups
) {
2047 chrome::RegisterAppPrefs(L
"Test");
2049 // We start with a normal browser with one tab.
2050 EXPECT_EQ(1, browser()->tab_strip_model()->count());
2052 // Open a popup browser with a single blank foreground tab.
2053 Browser
* popup_browser
= new Browser(
2054 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile()));
2055 chrome::AddTabAt(popup_browser
, GURL(), -1, true);
2056 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2058 // Now try opening another tab in the popup browser.
2059 AddTabWithURLParams
params1(url
, ui::PAGE_TRANSITION_TYPED
);
2060 popup_browser
->AddTabWithURL(¶ms1
);
2061 EXPECT_EQ(popup_browser
, params1
.target
);
2063 // The popup should still only have one tab.
2064 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2066 // The normal browser should now have two.
2067 EXPECT_EQ(2, browser()->tab_strip_model()->count());
2069 // Open an app frame browser with a single blank foreground tab.
2070 Browser
* app_browser
= new Browser(Browser::CreateParams::CreateForApp(
2071 L
"Test", browser()->profile(), false));
2072 chrome::AddTabAt(app_browser
, GURL(), -1, true);
2073 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2075 // Now try opening another tab in the app browser.
2076 AddTabWithURLParams
params2(GURL(url::kAboutBlankURL
),
2077 ui::PAGE_TRANSITION_TYPED
);
2078 app_browser
->AddTabWithURL(¶ms2
);
2079 EXPECT_EQ(app_browser
, params2
.target
);
2081 // The popup should still only have one tab.
2082 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2084 // The normal browser should now have three.
2085 EXPECT_EQ(3, browser()->tab_strip_model()->count());
2087 // Open an app frame popup browser with a single blank foreground tab.
2088 Browser
* app_popup_browser
= new Browser(Browser::CreateParams::CreateForApp(
2089 L
"Test", browser()->profile(), false));
2090 chrome::AddTabAt(app_popup_browser
, GURL(), -1, true);
2091 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2093 // Now try opening another tab in the app popup browser.
2094 AddTabWithURLParams
params3(GURL(url::kAboutBlankURL
),
2095 ui::PAGE_TRANSITION_TYPED
);
2096 app_popup_browser
->AddTabWithURL(¶ms3
);
2097 EXPECT_EQ(app_popup_browser
, params3
.target
);
2099 // The popup should still only have one tab.
2100 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2102 // The normal browser should now have four.
2103 EXPECT_EQ(4, browser()->tab_strip_model()->count());
2105 // Close the additional browsers.
2106 popup_browser
->tab_strip_model()->CloseAllTabs();
2107 app_browser
->tab_strip_model()->CloseAllTabs();
2108 app_popup_browser
->tab_strip_model()->CloseAllTabs();
2112 IN_PROC_BROWSER_TEST_F(BrowserTest
, WindowOpenClose
) {
2113 base::CommandLine::ForCurrentProcess()->AppendSwitch(
2114 switches::kDisablePopupBlocking
);
2115 GURL url
= ui_test_utils::GetTestUrl(
2116 base::FilePath(), base::FilePath().AppendASCII("window.close.html"));
2118 base::string16 title
= ASCIIToUTF16("Title Of Awesomeness");
2119 content::TitleWatcher
title_watcher(
2120 browser()->tab_strip_model()->GetActiveWebContents(), title
);
2121 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url
, 2);
2122 EXPECT_EQ(title
, title_watcher
.WaitAndGetTitle());
2125 // TODO(linux_aura) http://crbug.com/163931
2126 // Mac disabled: http://crbug.com/169820
2127 #if !defined(OS_MACOSX) && \
2128 !(defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
2129 IN_PROC_BROWSER_TEST_F(BrowserTest
, FullscreenBookmarkBar
) {
2130 #if defined(OS_WIN) && defined(USE_ASH)
2131 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2132 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2133 switches::kAshBrowserTests
))
2137 chrome::ToggleBookmarkBar(browser());
2138 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2139 chrome::ToggleFullscreenMode(browser());
2140 EXPECT_TRUE(browser()->window()->IsFullscreen());
2141 #if defined(OS_MACOSX)
2142 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2143 #elif defined(OS_CHROMEOS)
2144 // TODO(jamescook): If immersive fullscreen is disabled by default, test
2145 // for BookmarkBar::HIDDEN.
2146 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2148 EXPECT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2153 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisallowFileUrlUniversalAccessTest
) {
2154 GURL url
= ui_test_utils::GetTestUrl(
2156 base::FilePath().AppendASCII("fileurl_universalaccess.html"));
2158 base::string16
expected_title(ASCIIToUTF16("Disallowed"));
2159 content::TitleWatcher
title_watcher(
2160 browser()->tab_strip_model()->GetActiveWebContents(), expected_title
);
2161 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("Allowed"));
2162 ui_test_utils::NavigateToURL(browser(), url
);
2163 ASSERT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
2166 class KioskModeTest
: public BrowserTest
{
2170 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2171 command_line
->AppendSwitch(switches::kKioskMode
);
2175 #if defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
2176 // Mac: http://crbug.com/103912
2177 // Linux: http://crbug.com/163931
2178 #define MAYBE_EnableKioskModeTest DISABLED_EnableKioskModeTest
2180 #define MAYBE_EnableKioskModeTest EnableKioskModeTest
2182 IN_PROC_BROWSER_TEST_F(KioskModeTest
, MAYBE_EnableKioskModeTest
) {
2183 // Check if browser is in fullscreen mode.
2184 ASSERT_TRUE(browser()->window()->IsFullscreen());
2185 ASSERT_FALSE(browser()->window()->IsFullscreenBubbleVisible());
2189 // This test verifies that Chrome can be launched with a user-data-dir path
2190 // which contains non ASCII characters.
2191 class LaunchBrowserWithNonAsciiUserDatadir
: public BrowserTest
{
2193 LaunchBrowserWithNonAsciiUserDatadir() {}
2195 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2196 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
2197 base::FilePath tmp_profile
= temp_dir_
.path().AppendASCII("tmp_profile");
2198 tmp_profile
= tmp_profile
.Append(L
"Test Chrome G\u00E9raldine");
2200 ASSERT_TRUE(base::CreateDirectory(tmp_profile
));
2201 command_line
->AppendSwitchPath(switches::kUserDataDir
, tmp_profile
);
2204 base::ScopedTempDir temp_dir_
;
2207 IN_PROC_BROWSER_TEST_F(LaunchBrowserWithNonAsciiUserDatadir
,
2208 TestNonAsciiUserDataDir
) {
2209 // Verify that the window is present.
2210 ASSERT_TRUE(browser());
2211 ASSERT_TRUE(browser()->profile());
2212 // Verify that the profile has been added correctly to the ProfileInfoCache.
2213 ASSERT_EQ(1u, g_browser_process
->profile_manager()->
2214 GetProfileInfoCache().GetNumberOfProfiles());
2216 #endif // defined(OS_WIN)
2219 // This test verifies that Chrome can be launched with a user-data-dir path
2220 // which trailing slashes.
2221 class LaunchBrowserWithTrailingSlashDatadir
: public BrowserTest
{
2223 LaunchBrowserWithTrailingSlashDatadir() {}
2225 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2226 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
2227 base::FilePath tmp_profile
= temp_dir_
.path().AppendASCII("tmp_profile");
2228 tmp_profile
= tmp_profile
.Append(L
"Test Chrome\\");
2230 ASSERT_TRUE(base::CreateDirectory(tmp_profile
));
2231 command_line
->AppendSwitchPath(switches::kUserDataDir
, tmp_profile
);
2234 base::ScopedTempDir temp_dir_
;
2237 IN_PROC_BROWSER_TEST_F(LaunchBrowserWithTrailingSlashDatadir
,
2238 TestTrailingSlashUserDataDir
) {
2239 // Verify that the window is present.
2240 ASSERT_TRUE(browser());
2241 ASSERT_TRUE(browser()->profile());
2242 // Verify that the profile has been added correctly to the ProfileInfoCache.
2243 ASSERT_EQ(1u, g_browser_process
->profile_manager()->
2244 GetProfileInfoCache().GetNumberOfProfiles());
2246 #endif // defined(OS_WIN)
2248 // Tests to ensure that the browser continues running in the background after
2249 // the last window closes.
2250 class RunInBackgroundTest
: public BrowserTest
{
2252 RunInBackgroundTest() {}
2254 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2255 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2259 IN_PROC_BROWSER_TEST_F(RunInBackgroundTest
, RunInBackgroundBasicTest
) {
2260 // Close the browser window, then open a new one - the browser should keep
2262 Profile
* profile
= browser()->profile();
2263 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2264 content::WindowedNotificationObserver
observer(
2265 chrome::NOTIFICATION_BROWSER_CLOSED
,
2266 content::Source
<Browser
>(browser()));
2267 chrome::CloseWindow(browser());
2269 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2271 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2272 chrome::NewEmptyWindow(profile
, chrome::GetActiveDesktop());
2273 browser_added_observer
.WaitForSingleNewBrowser();
2275 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2278 // Tests to ensure that the browser continues running in the background after
2279 // the last window closes.
2280 class NoStartupWindowTest
: public BrowserTest
{
2282 NoStartupWindowTest() {}
2284 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2285 command_line
->AppendSwitch(switches::kNoStartupWindow
);
2286 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2289 // Returns true if any commands were processed.
2290 bool ProcessedAnyCommands(
2291 sessions::BaseSessionService
* base_session_service
) {
2292 sessions::BaseSessionServiceTestHelper
test_helper(base_session_service
);
2293 return test_helper
.ProcessedAnyCommands();
2297 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, NoStartupWindowBasicTest
) {
2298 #if defined(OS_WIN) && defined(USE_ASH)
2299 // kNoStartupWindow doesn't make sense in Metro+Ash.
2300 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2301 switches::kAshBrowserTests
))
2305 // No browser window should be started by default.
2306 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2308 // Starting a browser window should work just fine.
2309 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2310 CreateBrowser(ProfileManager::GetActiveUserProfile());
2311 browser_added_observer
.WaitForSingleNewBrowser();
2313 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2316 // Chromeos needs to track app windows because it considers them to be part of
2318 #if !defined(OS_CHROMEOS)
2319 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, DontInitSessionServiceForApps
) {
2320 #if defined(OS_WIN) && defined(USE_ASH)
2321 // kNoStartupWindow doesn't make sense in Metro+Ash.
2322 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2323 switches::kAshBrowserTests
))
2327 Profile
* profile
= ProfileManager::GetActiveUserProfile();
2329 SessionService
* session_service
=
2330 SessionServiceFactory::GetForProfile(profile
);
2331 sessions::BaseSessionService
* base_session_service
=
2332 session_service
->GetBaseSessionServiceForTest();
2333 ASSERT_FALSE(ProcessedAnyCommands(base_session_service
));
2335 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2336 CreateBrowserForApp("blah", profile
);
2337 browser_added_observer
.WaitForSingleNewBrowser();
2339 ASSERT_FALSE(ProcessedAnyCommands(base_session_service
));
2341 #endif // !defined(OS_CHROMEOS)
2343 // This test needs to be placed outside the anonymous namespace because we
2344 // need to access private type of Browser.
2345 class AppModeTest
: public BrowserTest
{
2349 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2350 GURL url
= ui_test_utils::GetTestUrl(
2351 base::FilePath(), base::FilePath().AppendASCII("title1.html"));
2352 command_line
->AppendSwitchASCII(switches::kApp
, url
.spec());
2356 IN_PROC_BROWSER_TEST_F(AppModeTest
, EnableAppModeTest
) {
2357 #if defined(OS_WIN) && defined(USE_ASH)
2358 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2359 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2360 switches::kAshBrowserTests
))
2364 // Test that an application browser window loads correctly.
2366 // Verify the browser is in application mode.
2367 EXPECT_TRUE(browser()->is_app());
2370 // Confirm chrome://version contains some expected content.
2371 IN_PROC_BROWSER_TEST_F(BrowserTest
, AboutVersion
) {
2372 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL
));
2373 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
2374 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("WebKit"), true, true,
2377 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("OS"), true, true,
2380 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("JavaScript"), true,
2385 static const base::FilePath::CharType
* kTestDir
=
2386 FILE_PATH_LITERAL("click_modifier");
2387 static const char kFirstPageTitle
[] = "First window";
2388 static const char kSecondPageTitle
[] = "New window!";
2390 class ClickModifierTest
: public InProcessBrowserTest
{
2392 ClickModifierTest() {
2395 // Returns a url that opens a new window or tab when clicked, via javascript.
2396 GURL
GetWindowOpenURL() {
2397 return ui_test_utils::GetTestUrl(
2398 base::FilePath(kTestDir
),
2399 base::FilePath(FILE_PATH_LITERAL("window_open.html")));
2402 // Returns a url that follows a simple link when clicked, unless affected by
2405 return ui_test_utils::GetTestUrl(
2406 base::FilePath(kTestDir
),
2407 base::FilePath(FILE_PATH_LITERAL("href.html")));
2410 base::string16
getFirstPageTitle() {
2411 return ASCIIToUTF16(kFirstPageTitle
);
2414 base::string16
getSecondPageTitle() {
2415 return ASCIIToUTF16(kSecondPageTitle
);
2418 // Loads our test page and simulates a single click using the supplied button
2419 // and modifiers. The click will cause either a navigation or the creation of
2420 // a new window or foreground or background tab. We verify that the expected
2421 // disposition occurs.
2422 void RunTest(Browser
* browser
,
2425 blink::WebMouseEvent::Button button
,
2426 WindowOpenDisposition disposition
) {
2427 ui_test_utils::NavigateToURL(browser
, url
);
2428 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2429 browser
->host_desktop_type()));
2430 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2431 content::WebContents
* web_contents
=
2432 browser
->tab_strip_model()->GetActiveWebContents();
2433 EXPECT_EQ(url
, web_contents
->GetURL());
2435 if (disposition
== CURRENT_TAB
) {
2436 content::WebContents
* web_contents
=
2437 browser
->tab_strip_model()->GetActiveWebContents();
2438 content::TestNavigationObserver
same_tab_observer(web_contents
);
2439 SimulateMouseClick(web_contents
, modifiers
, button
);
2440 same_tab_observer
.Wait();
2441 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2442 browser
->host_desktop_type()));
2443 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2444 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2448 content::WindowedNotificationObserver
observer(
2449 chrome::NOTIFICATION_TAB_ADDED
,
2450 content::NotificationService::AllSources());
2451 SimulateMouseClick(web_contents
, modifiers
, button
);
2454 if (disposition
== NEW_WINDOW
) {
2455 EXPECT_EQ(2u, chrome::GetBrowserCount(browser
->profile(),
2456 browser
->host_desktop_type()));
2460 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2461 browser
->host_desktop_type()));
2462 EXPECT_EQ(2, browser
->tab_strip_model()->count());
2463 web_contents
= browser
->tab_strip_model()->GetActiveWebContents();
2464 WaitForLoadStop(web_contents
);
2465 if (disposition
== NEW_FOREGROUND_TAB
) {
2466 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2468 ASSERT_EQ(NEW_BACKGROUND_TAB
, disposition
);
2469 EXPECT_EQ(getFirstPageTitle(), web_contents
->GetTitle());
2474 DISALLOW_COPY_AND_ASSIGN(ClickModifierTest
);
2477 // Tests for clicking on elements with handlers that run window.open.
2479 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenBasicClickTest
) {
2481 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2482 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2483 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2486 // TODO(ericu): Alt-click behavior on window.open is platform-dependent and not
2487 // well defined. Should we add tests so we know if it changes?
2489 // Shift-clicks open in a new window.
2490 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftClickTest
) {
2491 int modifiers
= blink::WebInputEvent::ShiftKey
;
2492 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2493 WindowOpenDisposition disposition
= NEW_WINDOW
;
2494 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2497 // Control-clicks open in a background tab.
2498 // On OSX meta [the command key] takes the place of control.
2499 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlClickTest
) {
2500 #if defined(OS_MACOSX)
2501 int modifiers
= blink::WebInputEvent::MetaKey
;
2503 int modifiers
= blink::WebInputEvent::ControlKey
;
2505 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2506 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2507 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2510 // Control-shift-clicks open in a foreground tab.
2511 // On OSX meta [the command key] takes the place of control.
2512 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlShiftClickTest
) {
2513 #if defined(OS_MACOSX)
2514 int modifiers
= blink::WebInputEvent::MetaKey
;
2516 int modifiers
= blink::WebInputEvent::ControlKey
;
2518 modifiers
|= blink::WebInputEvent::ShiftKey
;
2519 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2520 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2521 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2524 // Middle-clicks open in a background tab.
2525 #if defined(OS_LINUX)
2526 // http://crbug.com/396347
2527 #define MAYBE_WindowOpenMiddleClickTest DISABLED_WindowOpenMiddleClickTest
2529 #define MAYBE_WindowOpenMiddleClickTest WindowOpenMiddleClickTest
2531 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, MAYBE_WindowOpenMiddleClickTest
) {
2533 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2534 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2535 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2538 // Shift-middle-clicks open in a foreground tab.
2539 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftMiddleClickTest
) {
2540 int modifiers
= blink::WebInputEvent::ShiftKey
;
2541 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2542 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2543 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2546 // Tests for clicking on normal links.
2548 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefBasicClickTest
) {
2550 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2551 WindowOpenDisposition disposition
= CURRENT_TAB
;
2552 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2555 // TODO(ericu): Alt-click behavior on links is platform-dependent and not well
2556 // defined. Should we add tests so we know if it changes?
2558 // Shift-clicks open in a new window.
2559 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefShiftClickTest
) {
2560 int modifiers
= blink::WebInputEvent::ShiftKey
;
2561 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2562 WindowOpenDisposition disposition
= NEW_WINDOW
;
2563 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2566 // Control-clicks open in a background tab.
2567 // On OSX meta [the command key] takes the place of control.
2568 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefControlClickTest
) {
2569 #if defined(OS_MACOSX)
2570 int modifiers
= blink::WebInputEvent::MetaKey
;
2572 int modifiers
= blink::WebInputEvent::ControlKey
;
2574 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2575 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2576 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2579 // Control-shift-clicks open in a foreground tab.
2580 // On OSX meta [the command key] takes the place of control.
2581 // http://crbug.com/396347
2582 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, DISABLED_HrefControlShiftClickTest
) {
2583 #if defined(OS_MACOSX)
2584 int modifiers
= blink::WebInputEvent::MetaKey
;
2586 int modifiers
= blink::WebInputEvent::ControlKey
;
2588 modifiers
|= blink::WebInputEvent::ShiftKey
;
2589 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2590 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2591 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2594 // Middle-clicks open in a background tab.
2595 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefMiddleClickTest
) {
2597 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2598 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2599 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2602 // Shift-middle-clicks open in a foreground tab.
2603 // http://crbug.com/396347
2604 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, DISABLED_HrefShiftMiddleClickTest
) {
2605 int modifiers
= blink::WebInputEvent::ShiftKey
;
2606 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2607 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2608 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2611 IN_PROC_BROWSER_TEST_F(BrowserTest
, GetSizeForNewRenderView
) {
2612 // The instant extended NTP has javascript that does not work with
2613 // ui_test_utils::NavigateToURL. The NTP rvh reloads when the browser tries
2614 // to navigate away from the page, which causes the WebContents to end up in
2615 // an inconsistent state. (is_loaded = true, last_commited_url=ntp,
2616 // visible_url=title1.html)
2617 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled
,
2619 ASSERT_TRUE(test_server()->Start());
2620 // Create an HTTPS server for cross-site transition.
2621 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
2622 net::SpawnedTestServer::kLocalhost
,
2623 base::FilePath(kDocRoot
));
2624 ASSERT_TRUE(https_test_server
.Start());
2627 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2628 ASSERT_EQ(BookmarkBar::DETACHED
, browser()->bookmark_bar_state());
2629 WebContents
* web_contents
=
2630 browser()->tab_strip_model()->GetActiveWebContents();
2631 content::RenderViewHost
* prev_rvh
= web_contents
->GetRenderViewHost();
2632 const int height_inset
=
2633 browser()->window()->GetRenderViewHeightInsetWithDetachedBookmarkBar();
2634 const gfx::Size initial_wcv_size
=
2635 web_contents
->GetContainerBounds().size();
2636 RenderViewSizeObserver
observer(web_contents
, browser()->window());
2638 // Navigate to a non-NTP page, without resizing WebContentsView.
2639 ui_test_utils::NavigateToURL(browser(),
2640 test_server()->GetURL("files/title1.html"));
2641 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2642 // A new RenderViewHost should be created.
2643 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2644 prev_rvh
= web_contents
->GetRenderViewHost();
2645 gfx::Size rwhv_create_size0
, rwhv_commit_size0
, wcv_commit_size0
;
2646 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2650 // The create height of RenderWidgetHostView should include the height inset.
2651 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2652 initial_wcv_size
.height() + height_inset
),
2654 // When a navigation entry is committed, the size of RenderWidgetHostView
2655 // should be the same as when it was first created.
2656 EXPECT_EQ(rwhv_create_size0
, rwhv_commit_size0
);
2657 // Sizes of the current RenderWidgetHostView and WebContentsView should not
2658 // change before and after WebContentsDelegate::DidNavigateMainFramePostCommit
2659 // (implemented by Browser); we obtain the sizes before PostCommit via
2660 // WebContentsObserver::NavigationEntryCommitted (implemented by
2661 // RenderViewSizeObserver).
2662 EXPECT_EQ(rwhv_commit_size0
,
2663 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2664 // The behavior differs between OSX and views.
2665 // In OSX, the wcv does not change size until after the commit, when the
2666 // bookmark bar disappears (correct).
2667 // In views, the wcv changes size at commit time.
2668 #if defined(OS_MACOSX)
2669 EXPECT_EQ(gfx::Size(wcv_commit_size0
.width(),
2670 wcv_commit_size0
.height() + height_inset
),
2671 web_contents
->GetContainerBounds().size());
2673 EXPECT_EQ(wcv_commit_size0
, web_contents
->GetContainerBounds().size());
2676 // Navigate to another non-NTP page, without resizing WebContentsView.
2677 ui_test_utils::NavigateToURL(browser(),
2678 https_test_server
.GetURL("files/title2.html"));
2679 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2680 // A new RenderVieHost should be created.
2681 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2682 gfx::Size rwhv_create_size1
, rwhv_commit_size1
, wcv_commit_size1
;
2683 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2687 EXPECT_EQ(rwhv_create_size1
, rwhv_commit_size1
);
2688 EXPECT_EQ(rwhv_commit_size1
,
2689 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2690 EXPECT_EQ(wcv_commit_size1
, web_contents
->GetContainerBounds().size());
2692 // Navigate from NTP to a non-NTP page, resizing WebContentsView while
2693 // navigation entry is pending.
2694 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2695 gfx::Size
wcv_resize_insets(1, 1);
2696 observer
.set_wcv_resize_insets(wcv_resize_insets
);
2697 ui_test_utils::NavigateToURL(browser(),
2698 test_server()->GetURL("files/title2.html"));
2699 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2700 gfx::Size rwhv_create_size2
, rwhv_commit_size2
, wcv_commit_size2
;
2701 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2706 // The behavior on OSX and Views is incorrect in this edge case, but they are
2707 // differently incorrect.
2708 // The behavior should be:
2709 // initial wcv size: (100,100) (to choose random numbers)
2710 // initial rwhv size: (100,140)
2711 // commit wcv size: (101, 101)
2712 // commit rwhv size: (101, 141)
2713 // final wcv size: (101, 141)
2714 // final rwhv size: (101, 141)
2716 // On OSX, the commit rwhv size is (101, 101)
2717 // On views, the commit wcv size is (101, 141)
2718 // All other sizes are correct.
2720 // The create height of RenderWidgetHostView should include the height inset.
2721 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2722 initial_wcv_size
.height() + height_inset
),
2724 gfx::Size
exp_commit_size(initial_wcv_size
);
2726 #if defined(OS_MACOSX)
2727 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2728 wcv_resize_insets
.height());
2730 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2731 wcv_resize_insets
.height() + height_inset
);
2733 EXPECT_EQ(exp_commit_size
, rwhv_commit_size2
);
2734 EXPECT_EQ(exp_commit_size
, wcv_commit_size2
);
2735 gfx::Size
exp_final_size(initial_wcv_size
);
2736 exp_final_size
.Enlarge(wcv_resize_insets
.width(),
2737 wcv_resize_insets
.height() + height_inset
);
2738 EXPECT_EQ(exp_final_size
,
2739 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2740 EXPECT_EQ(exp_final_size
, web_contents
->GetContainerBounds().size());