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 // Test for crbug.com/22004. Reloading a page with a before unload handler and
590 // then canceling the dialog should not leave the throbber spinning.
591 IN_PROC_BROWSER_TEST_F(BrowserTest
, ReloadThenCancelBeforeUnload
) {
592 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
593 ui_test_utils::NavigateToURL(browser(), url
);
595 // Navigate to another page, but click cancel in the dialog. Make sure that
596 // the throbber stops spinning.
597 chrome::Reload(browser(), CURRENT_TAB
);
598 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
599 alert
->CloseModalDialog();
601 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
603 // Clear the beforeunload handler so the test can easily exit.
604 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
605 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
608 class RedirectObserver
: public content::WebContentsObserver
{
610 explicit RedirectObserver(content::WebContents
* web_contents
)
611 : WebContentsObserver(web_contents
) {
614 void DidNavigateAnyFrame(
615 content::RenderFrameHost
* render_frame_host
,
616 const content::LoadCommittedDetails
& details
,
617 const content::FrameNavigateParams
& params
) override
{
621 void WebContentsDestroyed() override
{
622 // Make sure we don't close the tab while the observer is in scope.
623 // See http://crbug.com/314036.
624 FAIL() << "WebContents closed during navigation (http://crbug.com/314036).";
627 const content::FrameNavigateParams
& params() const {
632 content::FrameNavigateParams params_
;
634 DISALLOW_COPY_AND_ASSIGN(RedirectObserver
);
637 // Ensure that a transferred cross-process navigation does not generate
638 // DidStopLoading events until the navigation commits. If it did, then
639 // ui_test_utils::NavigateToURL would proceed before the URL had committed.
640 // http://crbug.com/243957.
641 IN_PROC_BROWSER_TEST_F(BrowserTest
, NoStopDuringTransferUntilCommit
) {
642 // Create HTTP and HTTPS servers for a cross-site transition.
643 ASSERT_TRUE(test_server()->Start());
644 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
645 net::SpawnedTestServer::kLocalhost
,
646 base::FilePath(kDocRoot
));
647 ASSERT_TRUE(https_test_server
.Start());
649 // Temporarily replace ContentBrowserClient with one that will cause a
650 // process swap on all redirects to HTTPS URLs.
651 TransferHttpsRedirectsContentBrowserClient new_client
;
652 content::ContentBrowserClient
* old_client
=
653 SetBrowserClientForTesting(&new_client
);
655 GURL
init_url(test_server()->GetURL("files/title1.html"));
656 ui_test_utils::NavigateToURL(browser(), init_url
);
658 // Navigate to a same-site page that redirects, causing a transfer.
659 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
661 // Create a RedirectObserver that goes away before we close the tab.
663 RedirectObserver
redirect_observer(contents
);
664 GURL
dest_url(https_test_server
.GetURL("files/title2.html"));
665 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
667 ui_test_utils::NavigateToURL(browser(), redirect_url
);
669 // We should immediately see the new committed entry.
670 EXPECT_FALSE(contents
->GetController().GetPendingEntry());
672 contents
->GetController().GetLastCommittedEntry()->GetURL());
674 // We should keep track of the original request URL, redirect chain, and
675 // page transition type during a transfer, since these are necessary for
676 // history autocomplete to work.
677 EXPECT_EQ(redirect_url
, contents
->GetController().GetLastCommittedEntry()->
678 GetOriginalRequestURL());
679 EXPECT_EQ(2U, redirect_observer
.params().redirects
.size());
680 EXPECT_EQ(redirect_url
, redirect_observer
.params().redirects
.at(0));
681 EXPECT_EQ(dest_url
, redirect_observer
.params().redirects
.at(1));
682 EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
683 redirect_observer
.params().transition
, ui::PAGE_TRANSITION_TYPED
));
686 // Restore previous browser client.
687 SetBrowserClientForTesting(old_client
);
690 // Tests that a cross-process redirect will only cause the beforeunload
691 // handler to run once.
692 IN_PROC_BROWSER_TEST_F(BrowserTest
, SingleBeforeUnloadAfterRedirect
) {
693 // Create HTTP and HTTPS servers for a cross-site transition.
694 ASSERT_TRUE(test_server()->Start());
695 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
696 net::SpawnedTestServer::kLocalhost
,
697 base::FilePath(kDocRoot
));
698 ASSERT_TRUE(https_test_server
.Start());
700 // Temporarily replace ContentBrowserClient with one that will cause a
701 // process swap on all redirects to HTTPS URLs.
702 TransferHttpsRedirectsContentBrowserClient new_client
;
703 content::ContentBrowserClient
* old_client
=
704 SetBrowserClientForTesting(&new_client
);
706 // Navigate to a page with a beforeunload handler.
707 GURL
url(test_server()->GetURL("files/beforeunload.html"));
708 ui_test_utils::NavigateToURL(browser(), url
);
710 // Navigate to a URL that redirects to another process and approve the
711 // beforeunload dialog that pops up.
712 content::WindowedNotificationObserver
nav_observer(
713 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
714 content::NotificationService::AllSources());
715 GURL
https_url(https_test_server
.GetURL("files/title1.html"));
716 GURL
redirect_url(test_server()->GetURL("server-redirect?" +
718 browser()->OpenURL(OpenURLParams(redirect_url
, Referrer(), CURRENT_TAB
,
719 ui::PAGE_TRANSITION_TYPED
, false));
720 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
722 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
723 alert
->native_dialog()->AcceptAppModalDialog();
726 // Restore previous browser client.
727 SetBrowserClientForTesting(old_client
);
730 // Test for crbug.com/80401. Canceling a before unload dialog should reset
731 // the URL to the previous page's URL.
732 IN_PROC_BROWSER_TEST_F(BrowserTest
, CancelBeforeUnloadResetsURL
) {
733 GURL
url(ui_test_utils::GetTestUrl(base::FilePath(
734 base::FilePath::kCurrentDirectory
), base::FilePath(kBeforeUnloadFile
)));
735 ui_test_utils::NavigateToURL(browser(), url
);
737 // Navigate to a page that triggers a cross-site transition.
738 ASSERT_TRUE(test_server()->Start());
739 GURL
url2(test_server()->GetURL("files/title1.html"));
740 browser()->OpenURL(OpenURLParams(
741 url2
, Referrer(), CURRENT_TAB
, ui::PAGE_TRANSITION_TYPED
, false));
743 content::WindowedNotificationObserver
host_destroyed_observer(
744 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED
,
745 content::NotificationService::AllSources());
747 // Cancel the dialog.
748 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
749 alert
->CloseModalDialog();
751 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
753 // Verify there are no pending history items after the dialog is cancelled.
754 // (see crbug.com/93858)
755 NavigationEntry
* entry
= browser()->tab_strip_model()->
756 GetActiveWebContents()->GetController().GetPendingEntry();
757 EXPECT_EQ(NULL
, entry
);
759 // Wait for the ShouldClose_ACK to arrive. We can detect it by waiting for
760 // the pending RVH to be destroyed.
761 host_destroyed_observer
.Wait();
762 EXPECT_EQ(url
, browser()->toolbar_model()->GetURL());
764 // Clear the beforeunload handler so the test can easily exit.
765 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
766 ExecuteJavaScript(ASCIIToUTF16("onbeforeunload=null;"));
769 // Test for crbug.com/11647. A page closed with window.close() should not have
770 // two beforeunload dialogs shown.
771 // http://crbug.com/410891
772 IN_PROC_BROWSER_TEST_F(BrowserTest
,
773 DISABLED_SingleBeforeUnloadAfterWindowClose
) {
774 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
775 ExecuteJavaScriptForTests(ASCIIToUTF16(kOpenNewBeforeUnloadPage
));
777 // Close the new window with JavaScript, which should show a single
778 // beforeunload dialog. Then show another alert, to make it easy to verify
779 // that a second beforeunload dialog isn't shown.
780 browser()->tab_strip_model()->GetWebContentsAt(0)->GetMainFrame()->
781 ExecuteJavaScriptForTests(ASCIIToUTF16("w.close(); alert('bar');"));
782 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
783 alert
->native_dialog()->AcceptAppModalDialog();
785 alert
= ui_test_utils::WaitForAppModalDialog();
786 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->
787 is_before_unload_dialog());
788 alert
->native_dialog()->AcceptAppModalDialog();
791 // BrowserTest.BeforeUnloadVsBeforeReload times out on Windows.
792 // http://crbug.com/130411
794 #define MAYBE_BeforeUnloadVsBeforeReload DISABLED_BeforeUnloadVsBeforeReload
796 #define MAYBE_BeforeUnloadVsBeforeReload BeforeUnloadVsBeforeReload
799 // Test that when a page has an onunload handler, reloading a page shows a
800 // different dialog than navigating to a different page.
801 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_BeforeUnloadVsBeforeReload
) {
802 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
803 ui_test_utils::NavigateToURL(browser(), url
);
805 // Reload the page, and check that we get a "before reload" dialog.
806 chrome::Reload(browser(), CURRENT_TAB
);
807 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
808 EXPECT_TRUE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
810 // Cancel the reload.
811 alert
->native_dialog()->CancelAppModalDialog();
813 // Navigate to another url, and check that we get a "before unload" dialog.
814 GURL
url2(url::kAboutBlankURL
);
815 browser()->OpenURL(OpenURLParams(
816 url2
, Referrer(), CURRENT_TAB
, ui::PAGE_TRANSITION_TYPED
, false));
818 alert
= ui_test_utils::WaitForAppModalDialog();
819 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog
*>(alert
)->is_reload());
821 // Accept the navigation so we end up on a page without a beforeunload hook.
822 alert
->native_dialog()->AcceptAppModalDialog();
825 // BeforeUnloadAtQuitWithTwoWindows is a regression test for
826 // http://crbug.com/11842. It opens two windows, one of which has a
827 // beforeunload handler and attempts to exit cleanly.
828 class BeforeUnloadAtQuitWithTwoWindows
: public InProcessBrowserTest
{
830 // This test is for testing a specific shutdown behavior. This mimics what
831 // happens in InProcessBrowserTest::RunTestOnMainThread and QuitBrowsers, but
832 // ensures that it happens through the single IDC_EXIT of the test.
833 void TearDownOnMainThread() override
{
834 // Cycle both the MessageLoop and the Cocoa runloop twice to flush out any
835 // Chrome work that generates Cocoa work. Do this twice since there are two
836 // Browsers that must be closed.
840 // Run the application event loop to completion, which will cycle the
841 // native MessagePump on all platforms.
842 base::MessageLoop::current()->PostTask(FROM_HERE
,
843 base::MessageLoop::QuitClosure());
844 base::MessageLoop::current()->Run();
846 // Take care of any remaining Cocoa work.
849 // At this point, quit should be for real now.
850 ASSERT_EQ(0u, chrome::GetTotalBrowserCount());
853 // A helper function that cycles the MessageLoop, and on Mac, the Cocoa run
854 // loop. It also drains the NSAutoreleasePool.
855 void CycleRunLoops() {
856 content::RunAllPendingInMessageLoop();
857 #if defined(OS_MACOSX)
858 chrome::testing::NSRunLoopRunAllPending();
859 AutoreleasePool()->Recycle();
864 // Disabled, http://crbug.com/159214 .
865 IN_PROC_BROWSER_TEST_F(BeforeUnloadAtQuitWithTwoWindows
,
866 DISABLED_IfThisTestTimesOutItIndicatesFAILURE
) {
867 // In the first browser, set up a page that has a beforeunload handler.
868 GURL
url(std::string("data:text/html,") + kBeforeUnloadHTML
);
869 ui_test_utils::NavigateToURL(browser(), url
);
871 // Open a second browser window at about:blank.
872 ui_test_utils::BrowserAddedObserver browser_added_observer
;
873 chrome::NewEmptyWindow(browser()->profile(), chrome::GetActiveDesktop());
874 Browser
* second_window
= browser_added_observer
.WaitForSingleNewBrowser();
875 ui_test_utils::NavigateToURL(second_window
, GURL(url::kAboutBlankURL
));
877 // Tell the application to quit. IDC_EXIT calls AttemptUserExit, which on
878 // everything but ChromeOS allows unload handlers to block exit. On that
879 // platform, though, it exits unconditionally. See the comment and bug ID
880 // in AttemptUserExit() in application_lifetime.cc.
881 #if defined(OS_CHROMEOS)
882 chrome::AttemptExit();
884 chrome::ExecuteCommand(second_window
, IDC_EXIT
);
887 // The beforeunload handler will run at exit, ensure it does, and then accept
888 // it to allow shutdown to proceed.
889 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
892 static_cast<JavaScriptAppModalDialog
*>(alert
)->is_before_unload_dialog());
893 alert
->native_dialog()->AcceptAppModalDialog();
895 // But wait there's more! If this test times out, it likely means that the
896 // browser has not been able to quit correctly, indicating there's a
897 // regression of the bug noted above.
900 // Test that scripts can fork a new renderer process for a cross-site popup,
901 // based on http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab.
902 // The script must open a new tab, set its window.opener to null, and navigate
903 // it to a cross-site URL. It should also work for meta-refreshes.
904 // See http://crbug.com/93517.
905 IN_PROC_BROWSER_TEST_F(BrowserTest
, NullOpenerRedirectForksProcess
) {
906 base::CommandLine::ForCurrentProcess()->AppendSwitch(
907 switches::kDisablePopupBlocking
);
909 // Create http and https servers for a cross-site transition.
910 ASSERT_TRUE(test_server()->Start());
911 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
912 net::SpawnedTestServer::kLocalhost
,
913 base::FilePath(kDocRoot
));
914 ASSERT_TRUE(https_test_server
.Start());
915 GURL
http_url(test_server()->GetURL("files/title1.html"));
916 GURL
https_url(https_test_server
.GetURL(std::string()));
918 // Start with an http URL.
919 ui_test_utils::NavigateToURL(browser(), http_url
);
920 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
921 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
923 // Now open a tab to a blank page, set its opener to null, and redirect it
925 std::string redirect_popup
= "w=window.open();";
926 redirect_popup
+= "w.opener=null;";
927 redirect_popup
+= "w.document.location=\"";
928 redirect_popup
+= https_url
.spec();
929 redirect_popup
+= "\";";
931 content::WindowedNotificationObserver
popup_observer(
932 chrome::NOTIFICATION_TAB_ADDED
,
933 content::NotificationService::AllSources());
934 content::WindowedNotificationObserver
nav_observer(
935 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
936 content::NotificationService::AllSources());
937 oldtab
->GetMainFrame()->
938 ExecuteJavaScriptForTests(ASCIIToUTF16(redirect_popup
));
940 // Wait for popup window to appear and finish navigating.
941 popup_observer
.Wait();
942 ASSERT_EQ(2, browser()->tab_strip_model()->count());
943 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
945 EXPECT_NE(oldtab
, newtab
);
947 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
948 EXPECT_EQ(https_url
.spec(),
949 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
951 // Popup window should not be in the opener's process.
952 content::RenderProcessHost
* popup_process
=
953 newtab
->GetRenderProcessHost();
954 EXPECT_NE(process
, popup_process
);
956 // Now open a tab to a blank page, set its opener to null, and use a
957 // meta-refresh to navigate it instead.
958 std::string refresh_popup
= "w=window.open();";
959 refresh_popup
+= "w.opener=null;";
960 refresh_popup
+= "w.document.write(";
961 refresh_popup
+= "'<META HTTP-EQUIV=\"refresh\" content=\"0; url=";
962 refresh_popup
+= https_url
.spec();
963 refresh_popup
+= "\">');w.document.close();";
965 content::WindowedNotificationObserver
popup_observer2(
966 chrome::NOTIFICATION_TAB_ADDED
,
967 content::NotificationService::AllSources());
968 content::WindowedNotificationObserver
nav_observer2(
969 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
970 content::NotificationService::AllSources());
971 oldtab
->GetMainFrame()->
972 ExecuteJavaScriptForTests(ASCIIToUTF16(refresh_popup
));
974 // Wait for popup window to appear and finish navigating.
975 popup_observer2
.Wait();
976 ASSERT_EQ(3, browser()->tab_strip_model()->count());
977 WebContents
* newtab2
= browser()->tab_strip_model()->GetActiveWebContents();
978 EXPECT_TRUE(newtab2
);
979 EXPECT_NE(oldtab
, newtab2
);
980 nav_observer2
.Wait();
981 ASSERT_TRUE(newtab2
->GetController().GetLastCommittedEntry());
982 EXPECT_EQ(https_url
.spec(),
983 newtab2
->GetController().GetLastCommittedEntry()->GetURL().spec());
985 // This popup window should also not be in the opener's process.
986 content::RenderProcessHost
* popup_process2
=
987 newtab2
->GetRenderProcessHost();
988 EXPECT_NE(process
, popup_process2
);
991 // Tests that other popup navigations that do not follow the steps at
992 // http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab will not
993 // fork a new renderer process.
994 IN_PROC_BROWSER_TEST_F(BrowserTest
, OtherRedirectsDontForkProcess
) {
995 base::CommandLine::ForCurrentProcess()->AppendSwitch(
996 switches::kDisablePopupBlocking
);
998 // Create http and https servers for a cross-site transition.
999 ASSERT_TRUE(test_server()->Start());
1000 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1001 net::SpawnedTestServer::kLocalhost
,
1002 base::FilePath(kDocRoot
));
1003 ASSERT_TRUE(https_test_server
.Start());
1004 GURL
http_url(test_server()->GetURL("files/title1.html"));
1005 GURL
https_url(https_test_server
.GetURL(std::string()));
1007 // Start with an http URL.
1008 ui_test_utils::NavigateToURL(browser(), http_url
);
1009 WebContents
* oldtab
= browser()->tab_strip_model()->GetActiveWebContents();
1010 content::RenderProcessHost
* process
= oldtab
->GetRenderProcessHost();
1012 // Now open a tab to a blank page, set its opener to null, and redirect it
1014 std::string dont_fork_popup
= "w=window.open();";
1015 dont_fork_popup
+= "w.document.location=\"";
1016 dont_fork_popup
+= https_url
.spec();
1017 dont_fork_popup
+= "\";";
1019 content::WindowedNotificationObserver
popup_observer(
1020 chrome::NOTIFICATION_TAB_ADDED
,
1021 content::NotificationService::AllSources());
1022 content::WindowedNotificationObserver
nav_observer(
1023 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1024 content::NotificationService::AllSources());
1025 oldtab
->GetMainFrame()->
1026 ExecuteJavaScriptForTests(ASCIIToUTF16(dont_fork_popup
));
1028 // Wait for popup window to appear and finish navigating.
1029 popup_observer
.Wait();
1030 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1031 WebContents
* newtab
= browser()->tab_strip_model()->GetActiveWebContents();
1032 EXPECT_TRUE(newtab
);
1033 EXPECT_NE(oldtab
, newtab
);
1034 nav_observer
.Wait();
1035 ASSERT_TRUE(newtab
->GetController().GetLastCommittedEntry());
1036 EXPECT_EQ(https_url
.spec(),
1037 newtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1039 // Popup window should still be in the opener's process.
1040 content::RenderProcessHost
* popup_process
=
1041 newtab
->GetRenderProcessHost();
1042 EXPECT_EQ(process
, popup_process
);
1044 // Same thing if the current tab tries to navigate itself.
1045 std::string navigate_str
= "document.location=\"";
1046 navigate_str
+= https_url
.spec();
1047 navigate_str
+= "\";";
1049 content::WindowedNotificationObserver
nav_observer2(
1050 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
1051 content::NotificationService::AllSources());
1052 oldtab
->GetMainFrame()->ExecuteJavaScriptForTests(ASCIIToUTF16(navigate_str
));
1053 nav_observer2
.Wait();
1054 ASSERT_TRUE(oldtab
->GetController().GetLastCommittedEntry());
1055 EXPECT_EQ(https_url
.spec(),
1056 oldtab
->GetController().GetLastCommittedEntry()->GetURL().spec());
1058 // Original window should still be in the original process.
1059 content::RenderProcessHost
* new_process
= newtab
->GetRenderProcessHost();
1060 EXPECT_EQ(process
, new_process
);
1063 // Test that get_process_idle_time() returns reasonable values when compared
1064 // with time deltas measured locally.
1065 IN_PROC_BROWSER_TEST_F(BrowserTest
, RenderIdleTime
) {
1066 base::TimeTicks start
= base::TimeTicks::Now();
1067 ui_test_utils::NavigateToURL(
1068 browser(), ui_test_utils::GetTestUrl(
1069 base::FilePath(base::FilePath::kCurrentDirectory
),
1070 base::FilePath(kTitle1File
)));
1071 content::RenderProcessHost::iterator
it(
1072 content::RenderProcessHost::AllHostsIterator());
1073 for (; !it
.IsAtEnd(); it
.Advance()) {
1074 base::TimeDelta renderer_td
=
1075 it
.GetCurrentValue()->GetChildProcessIdleTime();
1076 base::TimeDelta browser_td
= base::TimeTicks::Now() - start
;
1077 EXPECT_TRUE(browser_td
>= renderer_td
);
1081 // Test IDC_CREATE_SHORTCUTS command is enabled for url scheme file, ftp, http
1082 // and https and disabled for chrome://, about:// etc.
1083 // TODO(pinkerton): Disable app-mode in the model until we implement it
1084 // on the Mac. http://crbug.com/13148
1085 #if !defined(OS_MACOSX)
1086 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFile
) {
1087 CommandUpdater
* command_updater
=
1088 browser()->command_controller()->command_updater();
1090 static const base::FilePath::CharType
* kEmptyFile
=
1091 FILE_PATH_LITERAL("empty.html");
1092 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1093 base::FilePath::kCurrentDirectory
), base::FilePath(kEmptyFile
)));
1094 ASSERT_TRUE(file_url
.SchemeIs(url::kFileScheme
));
1095 ui_test_utils::NavigateToURL(browser(), file_url
);
1096 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1099 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttp
) {
1100 CommandUpdater
* command_updater
=
1101 browser()->command_controller()->command_updater();
1103 ASSERT_TRUE(test_server()->Start());
1104 GURL
http_url(test_server()->GetURL(std::string()));
1105 ASSERT_TRUE(http_url
.SchemeIs(url::kHttpScheme
));
1106 ui_test_utils::NavigateToURL(browser(), http_url
);
1107 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1110 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutHttps
) {
1111 CommandUpdater
* command_updater
=
1112 browser()->command_controller()->command_updater();
1114 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_HTTPS
,
1115 net::SpawnedTestServer::kLocalhost
,
1116 base::FilePath(kDocRoot
));
1117 ASSERT_TRUE(test_server
.Start());
1118 GURL
https_url(test_server
.GetURL("/"));
1119 ASSERT_TRUE(https_url
.SchemeIs(url::kHttpsScheme
));
1120 ui_test_utils::NavigateToURL(browser(), https_url
);
1121 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1124 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutFtp
) {
1125 CommandUpdater
* command_updater
=
1126 browser()->command_controller()->command_updater();
1128 net::SpawnedTestServer
test_server(net::SpawnedTestServer::TYPE_FTP
,
1129 net::SpawnedTestServer::kLocalhost
,
1130 base::FilePath(kDocRoot
));
1131 ASSERT_TRUE(test_server
.Start());
1132 GURL
ftp_url(test_server
.GetURL(std::string()));
1133 ASSERT_TRUE(ftp_url
.SchemeIs(url::kFtpScheme
));
1134 ui_test_utils::NavigateToURL(browser(), ftp_url
);
1135 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1138 IN_PROC_BROWSER_TEST_F(BrowserTest
, CommandCreateAppShortcutInvalid
) {
1139 CommandUpdater
* command_updater
=
1140 browser()->command_controller()->command_updater();
1142 // Urls that should not have shortcuts.
1143 GURL
new_tab_url(chrome::kChromeUINewTabURL
);
1144 ui_test_utils::NavigateToURL(browser(), new_tab_url
);
1145 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1147 GURL
history_url(chrome::kChromeUIHistoryURL
);
1148 ui_test_utils::NavigateToURL(browser(), history_url
);
1149 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1151 GURL
downloads_url(chrome::kChromeUIDownloadsURL
);
1152 ui_test_utils::NavigateToURL(browser(), downloads_url
);
1153 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1155 GURL
blank_url(url::kAboutBlankURL
);
1156 ui_test_utils::NavigateToURL(browser(), blank_url
);
1157 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_CREATE_SHORTCUTS
));
1160 // Change a tab into an application window.
1161 // DISABLED: http://crbug.com/72310
1162 IN_PROC_BROWSER_TEST_F(BrowserTest
, DISABLED_ConvertTabToAppShortcut
) {
1163 ASSERT_TRUE(test_server()->Start());
1164 GURL
http_url(test_server()->GetURL(std::string()));
1165 ASSERT_TRUE(http_url
.SchemeIs(url::kHttpScheme
));
1167 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1168 WebContents
* initial_tab
= browser()->tab_strip_model()->GetWebContentsAt(0);
1169 WebContents
* app_tab
= chrome::AddSelectedTabWithURL(
1170 browser(), http_url
, ui::PAGE_TRANSITION_TYPED
);
1171 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1172 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
1173 browser()->host_desktop_type()));
1175 // Normal tabs should accept load drops.
1176 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1177 EXPECT_TRUE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1179 // Turn |app_tab| into a tab in an app panel.
1180 chrome::ConvertTabToAppWindow(browser(), app_tab
);
1182 // The launch should have created a new browser.
1183 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1184 browser()->host_desktop_type()));
1186 // Find the new browser.
1187 Browser
* app_browser
= NULL
;
1188 for (chrome::BrowserIterator it
; !it
.done() && !app_browser
; it
.Next()) {
1189 if (*it
!= browser())
1192 ASSERT_TRUE(app_browser
);
1194 // Check that the tab contents is in the new browser, and not in the old.
1195 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1196 ASSERT_EQ(initial_tab
, browser()->tab_strip_model()->GetWebContentsAt(0));
1198 // Check that the appliaction browser has a single tab, and that tab contains
1199 // the content that we app-ified.
1200 ASSERT_EQ(1, app_browser
->tab_strip_model()->count());
1201 ASSERT_EQ(app_tab
, app_browser
->tab_strip_model()->GetWebContentsAt(0));
1203 // Normal tabs should accept load drops.
1204 EXPECT_TRUE(initial_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1206 // The tab in an app window should not.
1207 EXPECT_FALSE(app_tab
->GetMutableRendererPrefs()->can_accept_load_drops
);
1210 #endif // !defined(OS_MACOSX)
1212 // Test RenderView correctly send back favicon url for web page that redirects
1213 // to an anchor in javascript body.onload handler.
1214 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1215 DISABLED_FaviconOfOnloadRedirectToAnchorPage
) {
1216 ASSERT_TRUE(test_server()->Start());
1217 GURL
url(test_server()->GetURL("files/onload_redirect_to_anchor.html"));
1218 GURL
expected_favicon_url(test_server()->GetURL("files/test.png"));
1220 ui_test_utils::NavigateToURL(browser(), url
);
1222 NavigationEntry
* entry
= browser()->tab_strip_model()->
1223 GetActiveWebContents()->GetController().GetLastCommittedEntry();
1224 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1227 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined (OS_WIN)
1228 // http://crbug.com/83828. On Mac 10.6, the failure rate is 14%
1229 #define MAYBE_FaviconChange DISABLED_FaviconChange
1231 #define MAYBE_FaviconChange FaviconChange
1233 // Test that an icon can be changed from JS.
1234 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_FaviconChange
) {
1235 static const base::FilePath::CharType
* kFile
=
1236 FILE_PATH_LITERAL("onload_change_favicon.html");
1237 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
1238 base::FilePath::kCurrentDirectory
), base::FilePath(kFile
)));
1239 ASSERT_TRUE(file_url
.SchemeIs(url::kFileScheme
));
1240 ui_test_utils::NavigateToURL(browser(), file_url
);
1242 NavigationEntry
* entry
= browser()->tab_strip_model()->
1243 GetActiveWebContents()->GetController().GetLastCommittedEntry();
1244 static const base::FilePath::CharType
* kIcon
=
1245 FILE_PATH_LITERAL("test1.png");
1246 GURL
expected_favicon_url(ui_test_utils::GetTestUrl(base::FilePath(
1247 base::FilePath::kCurrentDirectory
), base::FilePath(kIcon
)));
1248 EXPECT_EQ(expected_favicon_url
.spec(), entry
->GetFavicon().url
.spec());
1251 // http://crbug.com/172336
1253 #define MAYBE_TabClosingWhenRemovingExtension \
1254 DISABLED_TabClosingWhenRemovingExtension
1256 #define MAYBE_TabClosingWhenRemovingExtension TabClosingWhenRemovingExtension
1258 // Makes sure TabClosing is sent when uninstalling an extension that is an app
1260 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_TabClosingWhenRemovingExtension
) {
1261 ASSERT_TRUE(test_server()->Start());
1262 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1263 GURL
url(test_server()->GetURL("empty.html"));
1264 TabStripModel
* model
= browser()->tab_strip_model();
1266 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1268 const Extension
* extension_app
= GetExtension();
1270 ui_test_utils::NavigateToURL(browser(), url
);
1272 WebContents
* app_contents
= WebContents::Create(
1273 WebContents::CreateParams(browser()->profile()));
1274 extensions::TabHelper::CreateForWebContents(app_contents
);
1275 extensions::TabHelper
* extensions_tab_helper
=
1276 extensions::TabHelper::FromWebContents(app_contents
);
1277 extensions_tab_helper
->SetExtensionApp(extension_app
);
1279 model
->AddWebContents(app_contents
, 0, ui::PageTransitionFromInt(0),
1280 TabStripModel::ADD_NONE
);
1281 model
->SetTabPinned(0, true);
1282 ui_test_utils::NavigateToURL(browser(), url
);
1284 MockTabStripModelObserver observer
;
1285 model
->AddObserver(&observer
);
1287 // Uninstall the extension and make sure TabClosing is sent.
1288 ExtensionService
* service
= extensions::ExtensionSystem::Get(
1289 browser()->profile())->extension_service();
1290 service
->UninstallExtension(GetExtension()->id(),
1291 extensions::UNINSTALL_REASON_FOR_TESTING
,
1292 base::Bind(&base::DoNothing
),
1294 EXPECT_EQ(1, observer
.closing_count());
1296 model
->RemoveObserver(&observer
);
1298 // There should only be one tab now.
1299 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1302 // Open with --app-id=<id>, and see that an application tab opens by default.
1303 IN_PROC_BROWSER_TEST_F(BrowserTest
, AppIdSwitch
) {
1304 ASSERT_TRUE(test_server()->Start());
1306 // There should be one tab to start with.
1307 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1310 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1311 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1312 const Extension
* extension_app
= GetExtension();
1314 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
1315 command_line
.AppendSwitchASCII(switches::kAppId
, extension_app
->id());
1317 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1318 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1319 StartupBrowserCreatorImpl
launch(base::FilePath(), command_line
, first_run
);
1321 bool new_bookmark_apps_enabled
= extensions::util::IsNewBookmarkAppsEnabled();
1323 // If the new bookmark app flow is enabled, the app should open as an tab.
1324 // Otherwise the app should open as an app window.
1325 EXPECT_EQ(!new_bookmark_apps_enabled
,
1326 launch
.OpenApplicationWindow(browser()->profile(), NULL
));
1327 EXPECT_EQ(new_bookmark_apps_enabled
,
1328 launch
.OpenApplicationTab(browser()->profile()));
1330 // Check that a the number of browsers and tabs is correct.
1331 unsigned int expected_browsers
= 1;
1332 int expected_tabs
= 1;
1333 new_bookmark_apps_enabled
? expected_tabs
++ : expected_browsers
++;
1335 EXPECT_EQ(expected_browsers
,
1336 chrome::GetBrowserCount(browser()->profile(),
1337 browser()->host_desktop_type()));
1338 EXPECT_EQ(expected_tabs
, browser()->tab_strip_model()->count());
1341 // Open an app window and the dev tools window and ensure that the location
1342 // bar settings are correct.
1343 IN_PROC_BROWSER_TEST_F(BrowserTest
, ShouldShowLocationBar
) {
1344 ASSERT_TRUE(test_server()->Start());
1347 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1348 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1349 const Extension
* extension_app
= GetExtension();
1351 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1352 WebContents
* app_window
= OpenApplication(AppLaunchParams(
1353 browser()->profile(), extension_app
, extensions::LAUNCH_CONTAINER_WINDOW
,
1354 NEW_WINDOW
, extensions::SOURCE_TEST
));
1355 ASSERT_TRUE(app_window
);
1357 DevToolsWindow
* devtools_window
=
1358 DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), false);
1360 // The launch should have created a new app browser and a dev tools browser.
1362 chrome::GetBrowserCount(browser()->profile(),
1363 browser()->host_desktop_type()));
1365 // Find the new browsers.
1366 Browser
* app_browser
= NULL
;
1367 Browser
* dev_tools_browser
= NULL
;
1368 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
1369 if (*it
== browser()) {
1371 } else if ((*it
)->app_name() == DevToolsWindow::kDevToolsApp
) {
1372 dev_tools_browser
= *it
;
1377 ASSERT_TRUE(dev_tools_browser
);
1378 ASSERT_TRUE(app_browser
);
1379 ASSERT_TRUE(app_browser
!= browser());
1382 dev_tools_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1384 app_browser
->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
));
1386 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_window
);
1389 // Tests that the CLD (Compact Language Detection) works properly.
1390 IN_PROC_BROWSER_TEST_F(BrowserTest
, PageLanguageDetection
) {
1391 scoped_ptr
<test::CldDataHarness
> cld_data_harness
=
1392 test::CldDataHarnessFactory::Get()->CreateCldDataHarness();
1393 ASSERT_NO_FATAL_FAILURE(cld_data_harness
->Init());
1394 ASSERT_TRUE(test_server()->Start());
1396 translate::LanguageDetectionDetails details
;
1398 // Open a new tab with a page in English.
1399 AddTabAtIndex(0, GURL(test_server()->GetURL("files/english_page.html")),
1400 ui::PAGE_TRANSITION_TYPED
);
1402 WebContents
* current_web_contents
=
1403 browser()->tab_strip_model()->GetActiveWebContents();
1404 ChromeTranslateClient
* chrome_translate_client
=
1405 ChromeTranslateClient::FromWebContents(current_web_contents
);
1406 content::Source
<WebContents
> source(current_web_contents
);
1408 ui_test_utils::WindowedNotificationObserverWithDetails
<
1409 translate::LanguageDetectionDetails
>
1410 en_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1413 chrome_translate_client
->GetLanguageState().original_language());
1414 en_language_detected_signal
.Wait();
1415 EXPECT_TRUE(en_language_detected_signal
.GetDetailsFor(
1416 source
.map_key(), &details
));
1417 EXPECT_EQ("en", details
.adopted_language
);
1419 chrome_translate_client
->GetLanguageState().original_language());
1421 // Now navigate to a page in French.
1422 ui_test_utils::WindowedNotificationObserverWithDetails
<
1423 translate::LanguageDetectionDetails
>
1424 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
1426 ui_test_utils::NavigateToURL(
1427 browser(), GURL(test_server()->GetURL("files/french_page.html")));
1428 fr_language_detected_signal
.Wait();
1429 details
.adopted_language
.clear();
1430 EXPECT_TRUE(fr_language_detected_signal
.GetDetailsFor(
1431 source
.map_key(), &details
));
1432 EXPECT_EQ("fr", details
.adopted_language
);
1434 chrome_translate_client
->GetLanguageState().original_language());
1437 // Chromeos defaults to restoring the last session, so this test isn't
1439 #if !defined(OS_CHROMEOS)
1440 #if defined(OS_MACOSX)
1441 // Crashy, http://crbug.com/38522
1442 #define RestorePinnedTabs DISABLED_RestorePinnedTabs
1444 // Makes sure pinned tabs are restored correctly on start.
1445 IN_PROC_BROWSER_TEST_F(BrowserTest
, RestorePinnedTabs
) {
1446 ASSERT_TRUE(test_server()->Start());
1448 // Add an pinned app tab.
1449 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1450 GURL
url(test_server()->GetURL("empty.html"));
1451 TabStripModel
* model
= browser()->tab_strip_model();
1452 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1453 const Extension
* extension_app
= GetExtension();
1454 ui_test_utils::NavigateToURL(browser(), url
);
1455 WebContents
* app_contents
= WebContents::Create(
1456 WebContents::CreateParams(browser()->profile()));
1457 extensions::TabHelper::CreateForWebContents(app_contents
);
1458 extensions::TabHelper
* extensions_tab_helper
=
1459 extensions::TabHelper::FromWebContents(app_contents
);
1460 extensions_tab_helper
->SetExtensionApp(extension_app
);
1461 model
->AddWebContents(app_contents
, 0, ui::PageTransitionFromInt(0),
1462 TabStripModel::ADD_NONE
);
1463 model
->SetTabPinned(0, true);
1464 ui_test_utils::NavigateToURL(browser(), url
);
1466 // Add a non pinned tab.
1467 chrome::NewTab(browser());
1469 // Add a pinned non-app tab.
1470 chrome::NewTab(browser());
1471 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL
));
1472 model
->SetTabPinned(2, true);
1474 // Write out the pinned tabs.
1475 PinnedTabCodec::WritePinnedTabs(browser()->profile());
1477 // Simulate launching again.
1478 base::CommandLine
dummy(base::CommandLine::NO_PROGRAM
);
1479 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
1480 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
1481 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, first_run
);
1482 launch
.profile_
= browser()->profile();
1483 launch
.ProcessStartupURLs(std::vector
<GURL
>(),
1484 browser()->host_desktop_type());
1486 // The launch should have created a new browser.
1487 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1488 browser()->host_desktop_type()));
1490 // Find the new browser.
1491 Browser
* new_browser
= NULL
;
1492 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1493 if (*it
!= browser())
1496 ASSERT_TRUE(new_browser
);
1497 ASSERT_TRUE(new_browser
!= browser());
1499 // We should get back an additional tab for the app, and another for the
1500 // default home page.
1501 ASSERT_EQ(3, new_browser
->tab_strip_model()->count());
1503 // Make sure the state matches.
1504 TabStripModel
* new_model
= new_browser
->tab_strip_model();
1505 EXPECT_TRUE(new_model
->IsAppTab(0));
1506 EXPECT_FALSE(new_model
->IsAppTab(1));
1507 EXPECT_FALSE(new_model
->IsAppTab(2));
1509 EXPECT_TRUE(new_model
->IsTabPinned(0));
1510 EXPECT_TRUE(new_model
->IsTabPinned(1));
1511 EXPECT_FALSE(new_model
->IsTabPinned(2));
1513 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
1514 new_model
->GetWebContentsAt(2)->GetURL());
1517 extensions::TabHelper::FromWebContents(
1518 new_model
->GetWebContentsAt(0))->extension_app() == extension_app
);
1520 #endif // !defined(OS_CHROMEOS)
1522 // This test verifies we don't crash when closing the last window and the app
1524 IN_PROC_BROWSER_TEST_F(BrowserTest
, CloseWithAppMenuOpen
) {
1525 if (browser_defaults::kBrowserAliveWithNoWindows
)
1528 // We need a message loop running for menus on windows.
1529 base::MessageLoop::current()->PostTask(
1530 FROM_HERE
, base::Bind(&RunCloseWithAppMenuCallback
, browser()));
1533 #if !defined(OS_MACOSX)
1534 IN_PROC_BROWSER_TEST_F(BrowserTest
, OpenAppWindowLikeNtp
) {
1535 ASSERT_TRUE(test_server()->Start());
1538 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1539 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("app/")));
1540 const Extension
* extension_app
= GetExtension();
1542 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1543 WebContents
* app_window
= OpenApplication(AppLaunchParams(
1544 browser()->profile(), extension_app
, extensions::LAUNCH_CONTAINER_WINDOW
,
1545 NEW_WINDOW
, extensions::SOURCE_TEST
));
1546 ASSERT_TRUE(app_window
);
1548 // Apps launched in a window from the NTP have an extensions tab helper but
1549 // do not have extension_app set in it.
1550 ASSERT_TRUE(extensions::TabHelper::FromWebContents(app_window
));
1552 extensions::TabHelper::FromWebContents(app_window
)->extension_app());
1553 EXPECT_EQ(extensions::AppLaunchInfo::GetFullLaunchURL(extension_app
),
1554 app_window
->GetURL());
1556 // The launch should have created a new browser.
1557 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1558 browser()->host_desktop_type()));
1560 // Find the new browser.
1561 Browser
* new_browser
= NULL
;
1562 for (chrome::BrowserIterator it
; !it
.done() && !new_browser
; it
.Next()) {
1563 if (*it
!= browser())
1566 ASSERT_TRUE(new_browser
);
1567 ASSERT_TRUE(new_browser
!= browser());
1569 EXPECT_TRUE(new_browser
->is_app());
1571 // The browser's app name should include the extension's id.
1572 std::string app_name
= new_browser
->app_name_
;
1573 EXPECT_NE(app_name
.find(extension_app
->id()), std::string::npos
)
1574 << "Name " << app_name
<< " should contain id "<< extension_app
->id();
1576 #endif // !defined(OS_MACOSX)
1578 // Makes sure the browser doesn't crash when
1579 // set_show_state(ui::SHOW_STATE_MAXIMIZED) has been invoked.
1580 IN_PROC_BROWSER_TEST_F(BrowserTest
, StartMaximized
) {
1581 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1582 for (size_t i
= 0; i
< arraysize(types
); ++i
) {
1583 Browser::CreateParams
params(types
[i
], browser()->profile(),
1584 browser()->host_desktop_type());
1585 params
.initial_show_state
= ui::SHOW_STATE_MAXIMIZED
;
1586 AddBlankTabAndShow(new Browser(params
));
1590 // Aura doesn't support minimized window. crbug.com/104571.
1591 #if defined(USE_AURA)
1592 #define MAYBE_StartMinimized DISABLED_StartMinimized
1594 #define MAYBE_StartMinimized StartMinimized
1596 // Makes sure the browser doesn't crash when
1597 // set_show_state(ui::SHOW_STATE_MINIMIZED) has been invoked.
1598 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_StartMinimized
) {
1599 Browser::Type types
[] = { Browser::TYPE_TABBED
, Browser::TYPE_POPUP
};
1600 for (size_t i
= 0; i
< arraysize(types
); ++i
) {
1601 Browser::CreateParams
params(types
[i
], browser()->profile(),
1602 browser()->host_desktop_type());
1603 params
.initial_show_state
= ui::SHOW_STATE_MINIMIZED
;
1604 AddBlankTabAndShow(new Browser(params
));
1608 // Makes sure the forward button is disabled immediately when navigating
1609 // forward to a slow-to-commit page.
1610 IN_PROC_BROWSER_TEST_F(BrowserTest
, ForwardDisabledOnForward
) {
1611 GURL
blank_url(url::kAboutBlankURL
);
1612 ui_test_utils::NavigateToURL(browser(), blank_url
);
1614 ui_test_utils::NavigateToURL(
1615 browser(), ui_test_utils::GetTestUrl(
1616 base::FilePath(base::FilePath::kCurrentDirectory
),
1617 base::FilePath(kTitle1File
)));
1619 content::WindowedNotificationObserver
back_nav_load_observer(
1620 content::NOTIFICATION_LOAD_STOP
,
1621 content::Source
<NavigationController
>(
1622 &browser()->tab_strip_model()->GetActiveWebContents()->
1624 chrome::GoBack(browser(), CURRENT_TAB
);
1625 back_nav_load_observer
.Wait();
1626 CommandUpdater
* command_updater
=
1627 browser()->command_controller()->command_updater();
1628 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1630 content::WindowedNotificationObserver
forward_nav_load_observer(
1631 content::NOTIFICATION_LOAD_STOP
,
1632 content::Source
<NavigationController
>(
1633 &browser()->tab_strip_model()->GetActiveWebContents()->
1635 chrome::GoForward(browser(), CURRENT_TAB
);
1636 // This check will happen before the navigation completes, since the browser
1637 // won't process the renderer's response until the Wait() call below.
1638 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_FORWARD
));
1639 forward_nav_load_observer
.Wait();
1642 // Makes sure certain commands are disabled when Incognito mode is forced.
1643 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisableMenuItemsWhenIncognitoIsForced
) {
1644 CommandUpdater
* command_updater
=
1645 browser()->command_controller()->command_updater();
1646 // At the beginning, all commands are enabled.
1647 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1648 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1649 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1650 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1651 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1652 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1654 // Set Incognito to FORCED.
1655 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1656 IncognitoModePrefs::FORCED
);
1657 // Bookmarks & Settings commands should get disabled.
1658 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1659 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1660 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1661 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1662 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1663 // New Incognito Window command, however, should be enabled.
1664 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1666 // Create a new browser.
1667 Browser
* new_browser
=
1668 new Browser(Browser::CreateParams(
1669 browser()->profile()->GetOffTheRecordProfile(),
1670 browser()->host_desktop_type()));
1671 CommandUpdater
* new_command_updater
=
1672 new_browser
->command_controller()->command_updater();
1673 // It should have Bookmarks & Settings commands disabled by default.
1674 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1675 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(
1676 IDC_SHOW_BOOKMARK_MANAGER
));
1677 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1678 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1679 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1680 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1683 // Makes sure New Incognito Window command is disabled when Incognito mode is
1685 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1686 NoNewIncognitoWindowWhenIncognitoIsDisabled
) {
1687 CommandUpdater
* command_updater
=
1688 browser()->command_controller()->command_updater();
1689 // Set Incognito to DISABLED.
1690 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1691 IncognitoModePrefs::DISABLED
);
1692 // Make sure New Incognito Window command is disabled. All remaining commands
1693 // should be enabled.
1694 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1695 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1696 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1697 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1698 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1699 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1701 // Create a new browser.
1702 Browser
* new_browser
=
1703 new Browser(Browser::CreateParams(browser()->profile(),
1704 browser()->host_desktop_type()));
1705 CommandUpdater
* new_command_updater
=
1706 new_browser
->command_controller()->command_updater();
1707 EXPECT_FALSE(new_command_updater
->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW
));
1708 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1709 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1710 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1711 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1712 EXPECT_TRUE(new_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1715 // Makes sure Extensions and Settings commands are disabled in certain
1716 // circumstances even though normally they should stay enabled.
1717 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1718 DisableExtensionsAndSettingsWhenIncognitoIsDisabled
) {
1719 CommandUpdater
* command_updater
=
1720 browser()->command_controller()->command_updater();
1721 // Disable extensions. This should disable Extensions menu.
1722 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
1723 set_extensions_enabled(false);
1724 // Set Incognito to DISABLED.
1725 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1726 IncognitoModePrefs::DISABLED
);
1727 // Make sure Manage Extensions command is disabled.
1728 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1729 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_NEW_WINDOW
));
1730 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER
));
1731 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1732 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1734 // Create a popup (non-main-UI-type) browser. Settings command as well
1735 // as Extensions should be disabled.
1736 Browser
* popup_browser
= new Browser(
1737 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1738 browser()->host_desktop_type()));
1739 CommandUpdater
* popup_command_updater
=
1740 popup_browser
->command_controller()->command_updater();
1741 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_MANAGE_EXTENSIONS
));
1742 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1743 EXPECT_TRUE(popup_command_updater
->IsCommandEnabled(
1744 IDC_SHOW_BOOKMARK_MANAGER
));
1745 EXPECT_FALSE(popup_command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1748 // Makes sure Extensions and Settings commands are disabled in certain
1749 // circumstances even though normally they should stay enabled.
1750 IN_PROC_BROWSER_TEST_F(BrowserTest
,
1751 DisableOptionsAndImportMenuItemsConsistently
) {
1752 // Create a popup browser.
1753 Browser
* popup_browser
= new Browser(
1754 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
1755 browser()->host_desktop_type()));
1756 CommandUpdater
* command_updater
=
1757 popup_browser
->command_controller()->command_updater();
1758 // OPTIONS and IMPORT_SETTINGS are disabled for a non-normal UI.
1759 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1760 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1762 // Set Incognito to FORCED.
1763 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1764 IncognitoModePrefs::FORCED
);
1765 // OPTIONS and IMPORT_SETTINGS are disabled when Incognito is forced.
1766 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1767 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1768 // Set Incognito to AVAILABLE.
1769 IncognitoModePrefs::SetAvailability(popup_browser
->profile()->GetPrefs(),
1770 IncognitoModePrefs::ENABLED
);
1771 // OPTIONS and IMPORT_SETTINGS are still disabled since it is a non-normal UI.
1772 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_OPTIONS
));
1773 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_IMPORT_SETTINGS
));
1778 void OnZoomLevelChanged(const base::Closure
& callback
,
1779 const HostZoomMap::ZoomLevelChange
& host
) {
1786 // Flakes regularly on Windows XP
1787 // http://crbug.com/146040
1788 #define MAYBE_PageZoom DISABLED_PageZoom
1790 #define MAYBE_PageZoom PageZoom
1795 int GetZoomPercent(const content::WebContents
* contents
,
1797 bool* enable_minus
) {
1799 ui_zoom::ZoomController::FromWebContents(contents
)->GetZoomPercent();
1800 *enable_plus
= percent
< contents
->GetMaximumZoomPercent();
1801 *enable_minus
= percent
> contents
->GetMinimumZoomPercent();
1807 IN_PROC_BROWSER_TEST_F(BrowserTest
, MAYBE_PageZoom
) {
1808 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1809 bool enable_plus
, enable_minus
;
1812 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1813 new content::MessageLoopRunner
);
1814 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1815 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1816 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1817 content::HostZoomMap::GetDefaultForBrowserContext(
1818 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1819 chrome::Zoom(browser(), content::PAGE_ZOOM_IN
);
1822 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 110);
1823 EXPECT_TRUE(enable_plus
);
1824 EXPECT_TRUE(enable_minus
);
1828 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1829 new content::MessageLoopRunner
);
1830 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1831 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1832 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1833 content::HostZoomMap::GetDefaultForBrowserContext(
1834 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1835 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1838 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 100);
1839 EXPECT_TRUE(enable_plus
);
1840 EXPECT_TRUE(enable_minus
);
1844 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
1845 new content::MessageLoopRunner
);
1846 content::HostZoomMap::ZoomLevelChangedCallback
callback(
1847 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
1848 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
1849 content::HostZoomMap::GetDefaultForBrowserContext(
1850 browser()->profile())->AddZoomLevelChangedCallback(callback
);
1851 chrome::Zoom(browser(), content::PAGE_ZOOM_OUT
);
1854 EXPECT_EQ(GetZoomPercent(contents
, &enable_plus
, &enable_minus
), 90);
1855 EXPECT_TRUE(enable_plus
);
1856 EXPECT_TRUE(enable_minus
);
1859 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET
);
1862 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCommandDisable
) {
1863 ASSERT_TRUE(test_server()->Start());
1864 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1865 GURL
url(test_server()->GetURL("empty.html"));
1866 ui_test_utils::NavigateToURL(browser(), url
);
1868 CommandUpdater
* command_updater
=
1869 browser()->command_controller()->command_updater();
1870 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1871 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1872 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1873 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1875 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1877 TestInterstitialPage
* interstitial
=
1878 new TestInterstitialPage(contents
, false, GURL());
1879 content::WaitForInterstitialAttach(contents
);
1881 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1883 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1884 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1885 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1886 EXPECT_FALSE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1888 // Proceed and wait for interstitial to detach. This doesn't destroy
1890 interstitial
->Proceed();
1891 content::WaitForInterstitialDetach(contents
);
1892 // interstitial is deleted now.
1894 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_VIEW_SOURCE
));
1895 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_PRINT
));
1896 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_SAVE_PAGE
));
1897 EXPECT_TRUE(command_updater
->IsCommandEnabled(IDC_ENCODING_MENU
));
1900 // Ensure that creating an interstitial page closes any JavaScript dialogs
1901 // that were present on the previous page. See http://crbug.com/295695.
1902 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialClosesDialogs
) {
1903 ASSERT_TRUE(test_server()->Start());
1904 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1905 GURL
url(test_server()->GetURL("empty.html"));
1906 ui_test_utils::NavigateToURL(browser(), url
);
1908 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1909 contents
->GetMainFrame()->ExecuteJavaScript(
1910 ASCIIToUTF16("alert('Dialog showing!');"));
1911 AppModalDialog
* alert
= ui_test_utils::WaitForAppModalDialog();
1912 EXPECT_TRUE(alert
->IsValid());
1913 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
1914 EXPECT_TRUE(dialog_queue
->HasActiveDialog());
1916 TestInterstitialPage
* interstitial
=
1917 new TestInterstitialPage(contents
, false, GURL());
1918 content::WaitForInterstitialAttach(contents
);
1920 // The interstitial should have closed the dialog.
1921 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1922 EXPECT_FALSE(dialog_queue
->HasActiveDialog());
1924 // Don't proceed and wait for interstitial to detach. This doesn't destroy
1926 interstitial
->DontProceed();
1927 content::WaitForInterstitialDetach(contents
);
1928 // interstitial is deleted now.
1930 // Make sure input events still work in the renderer process.
1931 EXPECT_FALSE(contents
->GetRenderProcessHost()->IgnoreInputEvents());
1935 IN_PROC_BROWSER_TEST_F(BrowserTest
, InterstitialCloseTab
) {
1936 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
1938 // Interstitial will delete itself when we close the tab.
1939 new TestInterstitialPage(contents
, false, GURL());
1940 content::WaitForInterstitialAttach(contents
);
1942 EXPECT_TRUE(contents
->ShowingInterstitialPage());
1944 // Close the tab and wait for interstitial detach. This destroys |contents|.
1945 content::RunTaskAndWaitForInterstitialDetach(
1946 contents
, base::Bind(&chrome::CloseTab
, browser()));
1947 // interstitial is deleted now.
1950 class MockWebContentsObserver
: public WebContentsObserver
{
1952 explicit MockWebContentsObserver(WebContents
* web_contents
)
1953 : WebContentsObserver(web_contents
),
1954 got_user_gesture_(false) {
1957 void DidGetUserGesture() override
{ got_user_gesture_
= true; }
1959 bool got_user_gesture() const {
1960 return got_user_gesture_
;
1963 void set_got_user_gesture(bool got_it
) {
1964 got_user_gesture_
= got_it
;
1968 bool got_user_gesture_
;
1970 DISALLOW_COPY_AND_ASSIGN(MockWebContentsObserver
);
1973 IN_PROC_BROWSER_TEST_F(BrowserTest
, UserGesturesReported
) {
1974 // Regression test for http://crbug.com/110707. Also tests that a user
1975 // gesture is sent when a normal navigation (via e.g. the omnibox) is
1977 WebContents
* web_contents
=
1978 browser()->tab_strip_model()->GetActiveWebContents();
1979 MockWebContentsObserver
mock_observer(web_contents
);
1981 ASSERT_TRUE(test_server()->Start());
1982 GURL
url(test_server()->GetURL("empty.html"));
1984 ui_test_utils::NavigateToURL(browser(), url
);
1985 EXPECT_TRUE(mock_observer
.got_user_gesture());
1987 mock_observer
.set_got_user_gesture(false);
1988 chrome::Reload(browser(), CURRENT_TAB
);
1989 EXPECT_TRUE(mock_observer
.got_user_gesture());
1992 // TODO(ben): this test was never enabled. It has bit-rotted since being added.
1993 // It originally lived in browser_unittest.cc, but has been moved here to make
1994 // room for real browser unit tests.
1996 class BrowserTest2
: public InProcessBrowserTest
{
1999 host_resolver_proc_
= new net::RuleBasedHostResolverProc(NULL
);
2000 // Avoid making external DNS lookups. In this test we don't need this
2002 host_resolver_proc_
->AddSimulatedFailure("*.google.com");
2003 scoped_host_resolver_proc_
.Init(host_resolver_proc_
.get());
2007 scoped_refptr
<net::RuleBasedHostResolverProc
> host_resolver_proc_
;
2008 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_
;
2011 IN_PROC_BROWSER_TEST_F(BrowserTest2
, NoTabsInPopups
) {
2012 chrome::RegisterAppPrefs(L
"Test");
2014 // We start with a normal browser with one tab.
2015 EXPECT_EQ(1, browser()->tab_strip_model()->count());
2017 // Open a popup browser with a single blank foreground tab.
2018 Browser
* popup_browser
= new Browser(
2019 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile()));
2020 chrome::AddTabAt(popup_browser
, GURL(), -1, true);
2021 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2023 // Now try opening another tab in the popup browser.
2024 AddTabWithURLParams
params1(url
, ui::PAGE_TRANSITION_TYPED
);
2025 popup_browser
->AddTabWithURL(¶ms1
);
2026 EXPECT_EQ(popup_browser
, params1
.target
);
2028 // The popup should still only have one tab.
2029 EXPECT_EQ(1, popup_browser
->tab_strip_model()->count());
2031 // The normal browser should now have two.
2032 EXPECT_EQ(2, browser()->tab_strip_model()->count());
2034 // Open an app frame browser with a single blank foreground tab.
2035 Browser
* app_browser
= new Browser(Browser::CreateParams::CreateForApp(
2036 L
"Test", browser()->profile(), false));
2037 chrome::AddTabAt(app_browser
, GURL(), -1, true);
2038 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2040 // Now try opening another tab in the app browser.
2041 AddTabWithURLParams
params2(GURL(url::kAboutBlankURL
),
2042 ui::PAGE_TRANSITION_TYPED
);
2043 app_browser
->AddTabWithURL(¶ms2
);
2044 EXPECT_EQ(app_browser
, params2
.target
);
2046 // The popup should still only have one tab.
2047 EXPECT_EQ(1, app_browser
->tab_strip_model()->count());
2049 // The normal browser should now have three.
2050 EXPECT_EQ(3, browser()->tab_strip_model()->count());
2052 // Open an app frame popup browser with a single blank foreground tab.
2053 Browser
* app_popup_browser
= new Browser(Browser::CreateParams::CreateForApp(
2054 L
"Test", browser()->profile(), false));
2055 chrome::AddTabAt(app_popup_browser
, GURL(), -1, true);
2056 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2058 // Now try opening another tab in the app popup browser.
2059 AddTabWithURLParams
params3(GURL(url::kAboutBlankURL
),
2060 ui::PAGE_TRANSITION_TYPED
);
2061 app_popup_browser
->AddTabWithURL(¶ms3
);
2062 EXPECT_EQ(app_popup_browser
, params3
.target
);
2064 // The popup should still only have one tab.
2065 EXPECT_EQ(1, app_popup_browser
->tab_strip_model()->count());
2067 // The normal browser should now have four.
2068 EXPECT_EQ(4, browser()->tab_strip_model()->count());
2070 // Close the additional browsers.
2071 popup_browser
->tab_strip_model()->CloseAllTabs();
2072 app_browser
->tab_strip_model()->CloseAllTabs();
2073 app_popup_browser
->tab_strip_model()->CloseAllTabs();
2077 IN_PROC_BROWSER_TEST_F(BrowserTest
, WindowOpenClose
) {
2078 base::CommandLine::ForCurrentProcess()->AppendSwitch(
2079 switches::kDisablePopupBlocking
);
2080 GURL url
= ui_test_utils::GetTestUrl(
2081 base::FilePath(), base::FilePath().AppendASCII("window.close.html"));
2083 base::string16 title
= ASCIIToUTF16("Title Of Awesomeness");
2084 content::TitleWatcher
title_watcher(
2085 browser()->tab_strip_model()->GetActiveWebContents(), title
);
2086 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url
, 2);
2087 EXPECT_EQ(title
, title_watcher
.WaitAndGetTitle());
2090 // TODO(linux_aura) http://crbug.com/163931
2091 // Mac disabled: http://crbug.com/169820
2092 #if !defined(OS_MACOSX) && \
2093 !(defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
2094 IN_PROC_BROWSER_TEST_F(BrowserTest
, FullscreenBookmarkBar
) {
2095 #if defined(OS_WIN) && defined(USE_ASH)
2096 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2097 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2098 switches::kAshBrowserTests
))
2102 chrome::ToggleBookmarkBar(browser());
2103 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2104 chrome::ToggleFullscreenMode(browser());
2105 EXPECT_TRUE(browser()->window()->IsFullscreen());
2106 #if defined(OS_MACOSX)
2107 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2108 #elif defined(OS_CHROMEOS)
2109 // TODO(jamescook): If immersive fullscreen is disabled by default, test
2110 // for BookmarkBar::HIDDEN.
2111 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
2113 EXPECT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2118 IN_PROC_BROWSER_TEST_F(BrowserTest
, DisallowFileUrlUniversalAccessTest
) {
2119 GURL url
= ui_test_utils::GetTestUrl(
2121 base::FilePath().AppendASCII("fileurl_universalaccess.html"));
2123 base::string16
expected_title(ASCIIToUTF16("Disallowed"));
2124 content::TitleWatcher
title_watcher(
2125 browser()->tab_strip_model()->GetActiveWebContents(), expected_title
);
2126 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("Allowed"));
2127 ui_test_utils::NavigateToURL(browser(), url
);
2128 ASSERT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
2131 class KioskModeTest
: public BrowserTest
{
2135 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2136 command_line
->AppendSwitch(switches::kKioskMode
);
2140 #if defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
2141 // Mac: http://crbug.com/103912
2142 // Linux: http://crbug.com/163931
2143 #define MAYBE_EnableKioskModeTest DISABLED_EnableKioskModeTest
2145 #define MAYBE_EnableKioskModeTest EnableKioskModeTest
2147 IN_PROC_BROWSER_TEST_F(KioskModeTest
, MAYBE_EnableKioskModeTest
) {
2148 // Check if browser is in fullscreen mode.
2149 ASSERT_TRUE(browser()->window()->IsFullscreen());
2150 ASSERT_FALSE(browser()->window()->IsFullscreenBubbleVisible());
2154 // This test verifies that Chrome can be launched with a user-data-dir path
2155 // which contains non ASCII characters.
2156 class LaunchBrowserWithNonAsciiUserDatadir
: public BrowserTest
{
2158 LaunchBrowserWithNonAsciiUserDatadir() {}
2160 virtual void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2161 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
2162 base::FilePath tmp_profile
= temp_dir_
.path().AppendASCII("tmp_profile");
2163 tmp_profile
= tmp_profile
.Append(L
"Test Chrome G\u00E9raldine");
2165 ASSERT_TRUE(base::CreateDirectory(tmp_profile
));
2166 command_line
->AppendSwitchPath(switches::kUserDataDir
, tmp_profile
);
2169 base::ScopedTempDir temp_dir_
;
2172 IN_PROC_BROWSER_TEST_F(LaunchBrowserWithNonAsciiUserDatadir
,
2173 TestNonAsciiUserDataDir
) {
2174 // Verify that the window is present.
2175 ASSERT_TRUE(browser());
2176 ASSERT_TRUE(browser()->profile());
2177 // Verify that the profile has been added correctly to the ProfileInfoCache.
2178 ASSERT_EQ(1u, g_browser_process
->profile_manager()->
2179 GetProfileInfoCache().GetNumberOfProfiles());
2181 #endif // defined(OS_WIN)
2184 // This test verifies that Chrome can be launched with a user-data-dir path
2185 // which trailing slashes.
2186 class LaunchBrowserWithTrailingSlashDatadir
: public BrowserTest
{
2188 LaunchBrowserWithTrailingSlashDatadir() {}
2190 virtual void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2191 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
2192 base::FilePath tmp_profile
= temp_dir_
.path().AppendASCII("tmp_profile");
2193 tmp_profile
= tmp_profile
.Append(L
"Test Chrome\\");
2195 ASSERT_TRUE(base::CreateDirectory(tmp_profile
));
2196 command_line
->AppendSwitchPath(switches::kUserDataDir
, tmp_profile
);
2199 base::ScopedTempDir temp_dir_
;
2202 IN_PROC_BROWSER_TEST_F(LaunchBrowserWithTrailingSlashDatadir
,
2203 TestTrailingSlashUserDataDir
) {
2204 // Verify that the window is present.
2205 ASSERT_TRUE(browser());
2206 ASSERT_TRUE(browser()->profile());
2207 // Verify that the profile has been added correctly to the ProfileInfoCache.
2208 ASSERT_EQ(1u, g_browser_process
->profile_manager()->
2209 GetProfileInfoCache().GetNumberOfProfiles());
2211 #endif // defined(OS_WIN)
2213 // Tests to ensure that the browser continues running in the background after
2214 // the last window closes.
2215 class RunInBackgroundTest
: public BrowserTest
{
2217 RunInBackgroundTest() {}
2219 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2220 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2224 IN_PROC_BROWSER_TEST_F(RunInBackgroundTest
, RunInBackgroundBasicTest
) {
2225 // Close the browser window, then open a new one - the browser should keep
2227 Profile
* profile
= browser()->profile();
2228 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2229 content::WindowedNotificationObserver
observer(
2230 chrome::NOTIFICATION_BROWSER_CLOSED
,
2231 content::Source
<Browser
>(browser()));
2232 chrome::CloseWindow(browser());
2234 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2236 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2237 chrome::NewEmptyWindow(profile
, chrome::GetActiveDesktop());
2238 browser_added_observer
.WaitForSingleNewBrowser();
2240 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2243 // Tests to ensure that the browser continues running in the background after
2244 // the last window closes.
2245 class NoStartupWindowTest
: public BrowserTest
{
2247 NoStartupWindowTest() {}
2249 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2250 command_line
->AppendSwitch(switches::kNoStartupWindow
);
2251 command_line
->AppendSwitch(switches::kKeepAliveForTest
);
2254 // Returns true if any commands were processed.
2255 bool ProcessedAnyCommands(
2256 sessions::BaseSessionService
* base_session_service
) {
2257 sessions::BaseSessionServiceTestHelper
test_helper(base_session_service
);
2258 return test_helper
.ProcessedAnyCommands();
2262 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, NoStartupWindowBasicTest
) {
2263 #if defined(OS_WIN) && defined(USE_ASH)
2264 // kNoStartupWindow doesn't make sense in Metro+Ash.
2265 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2266 switches::kAshBrowserTests
))
2270 // No browser window should be started by default.
2271 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2273 // Starting a browser window should work just fine.
2274 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2275 CreateBrowser(ProfileManager::GetActiveUserProfile());
2276 browser_added_observer
.WaitForSingleNewBrowser();
2278 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2281 // Chromeos needs to track app windows because it considers them to be part of
2283 #if !defined(OS_CHROMEOS)
2284 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest
, DontInitSessionServiceForApps
) {
2285 #if defined(OS_WIN) && defined(USE_ASH)
2286 // kNoStartupWindow doesn't make sense in Metro+Ash.
2287 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2288 switches::kAshBrowserTests
))
2292 Profile
* profile
= ProfileManager::GetActiveUserProfile();
2294 SessionService
* session_service
=
2295 SessionServiceFactory::GetForProfile(profile
);
2296 sessions::BaseSessionService
* base_session_service
=
2297 session_service
->GetBaseSessionServiceForTest();
2298 ASSERT_FALSE(ProcessedAnyCommands(base_session_service
));
2300 ui_test_utils::BrowserAddedObserver browser_added_observer
;
2301 CreateBrowserForApp("blah", profile
);
2302 browser_added_observer
.WaitForSingleNewBrowser();
2304 ASSERT_FALSE(ProcessedAnyCommands(base_session_service
));
2306 #endif // !defined(OS_CHROMEOS)
2308 // This test needs to be placed outside the anonymous namespace because we
2309 // need to access private type of Browser.
2310 class AppModeTest
: public BrowserTest
{
2314 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
2315 GURL url
= ui_test_utils::GetTestUrl(
2316 base::FilePath(), base::FilePath().AppendASCII("title1.html"));
2317 command_line
->AppendSwitchASCII(switches::kApp
, url
.spec());
2321 IN_PROC_BROWSER_TEST_F(AppModeTest
, EnableAppModeTest
) {
2322 #if defined(OS_WIN) && defined(USE_ASH)
2323 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2324 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2325 switches::kAshBrowserTests
))
2329 // Test that an application browser window loads correctly.
2331 // Verify the browser is in application mode.
2332 EXPECT_TRUE(browser()->is_app());
2335 // Confirm chrome://version contains some expected content.
2336 IN_PROC_BROWSER_TEST_F(BrowserTest
, AboutVersion
) {
2337 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL
));
2338 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
2339 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("WebKit"), true, true,
2342 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("OS"), true, true,
2345 ASSERT_GT(ui_test_utils::FindInPage(tab
, ASCIIToUTF16("JavaScript"), true,
2350 static const base::FilePath::CharType
* kTestDir
=
2351 FILE_PATH_LITERAL("click_modifier");
2352 static const char kFirstPageTitle
[] = "First window";
2353 static const char kSecondPageTitle
[] = "New window!";
2355 class ClickModifierTest
: public InProcessBrowserTest
{
2357 ClickModifierTest() {
2360 // Returns a url that opens a new window or tab when clicked, via javascript.
2361 GURL
GetWindowOpenURL() {
2362 return ui_test_utils::GetTestUrl(
2363 base::FilePath(kTestDir
),
2364 base::FilePath(FILE_PATH_LITERAL("window_open.html")));
2367 // Returns a url that follows a simple link when clicked, unless affected by
2370 return ui_test_utils::GetTestUrl(
2371 base::FilePath(kTestDir
),
2372 base::FilePath(FILE_PATH_LITERAL("href.html")));
2375 base::string16
getFirstPageTitle() {
2376 return ASCIIToUTF16(kFirstPageTitle
);
2379 base::string16
getSecondPageTitle() {
2380 return ASCIIToUTF16(kSecondPageTitle
);
2383 // Loads our test page and simulates a single click using the supplied button
2384 // and modifiers. The click will cause either a navigation or the creation of
2385 // a new window or foreground or background tab. We verify that the expected
2386 // disposition occurs.
2387 void RunTest(Browser
* browser
,
2390 blink::WebMouseEvent::Button button
,
2391 WindowOpenDisposition disposition
) {
2392 ui_test_utils::NavigateToURL(browser
, url
);
2393 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2394 browser
->host_desktop_type()));
2395 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2396 content::WebContents
* web_contents
=
2397 browser
->tab_strip_model()->GetActiveWebContents();
2398 EXPECT_EQ(url
, web_contents
->GetURL());
2400 if (disposition
== CURRENT_TAB
) {
2401 content::WebContents
* web_contents
=
2402 browser
->tab_strip_model()->GetActiveWebContents();
2403 content::TestNavigationObserver
same_tab_observer(web_contents
);
2404 SimulateMouseClick(web_contents
, modifiers
, button
);
2405 same_tab_observer
.Wait();
2406 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2407 browser
->host_desktop_type()));
2408 EXPECT_EQ(1, browser
->tab_strip_model()->count());
2409 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2413 content::WindowedNotificationObserver
observer(
2414 chrome::NOTIFICATION_TAB_ADDED
,
2415 content::NotificationService::AllSources());
2416 SimulateMouseClick(web_contents
, modifiers
, button
);
2419 if (disposition
== NEW_WINDOW
) {
2420 EXPECT_EQ(2u, chrome::GetBrowserCount(browser
->profile(),
2421 browser
->host_desktop_type()));
2425 EXPECT_EQ(1u, chrome::GetBrowserCount(browser
->profile(),
2426 browser
->host_desktop_type()));
2427 EXPECT_EQ(2, browser
->tab_strip_model()->count());
2428 web_contents
= browser
->tab_strip_model()->GetActiveWebContents();
2429 WaitForLoadStop(web_contents
);
2430 if (disposition
== NEW_FOREGROUND_TAB
) {
2431 EXPECT_EQ(getSecondPageTitle(), web_contents
->GetTitle());
2433 ASSERT_EQ(NEW_BACKGROUND_TAB
, disposition
);
2434 EXPECT_EQ(getFirstPageTitle(), web_contents
->GetTitle());
2439 DISALLOW_COPY_AND_ASSIGN(ClickModifierTest
);
2442 // Tests for clicking on elements with handlers that run window.open.
2444 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenBasicClickTest
) {
2446 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2447 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2448 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2451 // TODO(ericu): Alt-click behavior on window.open is platform-dependent and not
2452 // well defined. Should we add tests so we know if it changes?
2454 // Shift-clicks open in a new window.
2455 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftClickTest
) {
2456 int modifiers
= blink::WebInputEvent::ShiftKey
;
2457 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2458 WindowOpenDisposition disposition
= NEW_WINDOW
;
2459 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2462 // Control-clicks open in a background tab.
2463 // On OSX meta [the command key] takes the place of control.
2464 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlClickTest
) {
2465 #if defined(OS_MACOSX)
2466 int modifiers
= blink::WebInputEvent::MetaKey
;
2468 int modifiers
= blink::WebInputEvent::ControlKey
;
2470 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2471 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2472 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2475 // Control-shift-clicks open in a foreground tab.
2476 // On OSX meta [the command key] takes the place of control.
2477 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenControlShiftClickTest
) {
2478 #if defined(OS_MACOSX)
2479 int modifiers
= blink::WebInputEvent::MetaKey
;
2481 int modifiers
= blink::WebInputEvent::ControlKey
;
2483 modifiers
|= blink::WebInputEvent::ShiftKey
;
2484 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2485 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2486 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2489 // Middle-clicks open in a background tab.
2490 #if defined(OS_LINUX)
2491 // http://crbug.com/396347
2492 #define MAYBE_WindowOpenMiddleClickTest DISABLED_WindowOpenMiddleClickTest
2494 #define MAYBE_WindowOpenMiddleClickTest WindowOpenMiddleClickTest
2496 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, MAYBE_WindowOpenMiddleClickTest
) {
2498 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2499 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2500 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2503 // Shift-middle-clicks open in a foreground tab.
2504 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, WindowOpenShiftMiddleClickTest
) {
2505 int modifiers
= blink::WebInputEvent::ShiftKey
;
2506 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2507 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2508 RunTest(browser(), GetWindowOpenURL(), modifiers
, button
, disposition
);
2511 // Tests for clicking on normal links.
2513 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefBasicClickTest
) {
2515 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2516 WindowOpenDisposition disposition
= CURRENT_TAB
;
2517 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2520 // TODO(ericu): Alt-click behavior on links is platform-dependent and not well
2521 // defined. Should we add tests so we know if it changes?
2523 // Shift-clicks open in a new window.
2524 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefShiftClickTest
) {
2525 int modifiers
= blink::WebInputEvent::ShiftKey
;
2526 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2527 WindowOpenDisposition disposition
= NEW_WINDOW
;
2528 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2531 // Control-clicks open in a background tab.
2532 // On OSX meta [the command key] takes the place of control.
2533 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefControlClickTest
) {
2534 #if defined(OS_MACOSX)
2535 int modifiers
= blink::WebInputEvent::MetaKey
;
2537 int modifiers
= blink::WebInputEvent::ControlKey
;
2539 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2540 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2541 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2544 // Control-shift-clicks open in a foreground tab.
2545 // On OSX meta [the command key] takes the place of control.
2546 // http://crbug.com/396347
2547 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, DISABLED_HrefControlShiftClickTest
) {
2548 #if defined(OS_MACOSX)
2549 int modifiers
= blink::WebInputEvent::MetaKey
;
2551 int modifiers
= blink::WebInputEvent::ControlKey
;
2553 modifiers
|= blink::WebInputEvent::ShiftKey
;
2554 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonLeft
;
2555 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2556 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2559 // Middle-clicks open in a background tab.
2560 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, HrefMiddleClickTest
) {
2562 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2563 WindowOpenDisposition disposition
= NEW_BACKGROUND_TAB
;
2564 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2567 // Shift-middle-clicks open in a foreground tab.
2568 // http://crbug.com/396347
2569 IN_PROC_BROWSER_TEST_F(ClickModifierTest
, DISABLED_HrefShiftMiddleClickTest
) {
2570 int modifiers
= blink::WebInputEvent::ShiftKey
;
2571 blink::WebMouseEvent::Button button
= blink::WebMouseEvent::ButtonMiddle
;
2572 WindowOpenDisposition disposition
= NEW_FOREGROUND_TAB
;
2573 RunTest(browser(), GetHrefURL(), modifiers
, button
, disposition
);
2576 IN_PROC_BROWSER_TEST_F(BrowserTest
, GetSizeForNewRenderView
) {
2577 // The instant extended NTP has javascript that does not work with
2578 // ui_test_utils::NavigateToURL. The NTP rvh reloads when the browser tries
2579 // to navigate away from the page, which causes the WebContents to end up in
2580 // an inconsistent state. (is_loaded = true, last_commited_url=ntp,
2581 // visible_url=title1.html)
2582 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled
,
2584 ASSERT_TRUE(test_server()->Start());
2585 // Create an HTTPS server for cross-site transition.
2586 net::SpawnedTestServer
https_test_server(net::SpawnedTestServer::TYPE_HTTPS
,
2587 net::SpawnedTestServer::kLocalhost
,
2588 base::FilePath(kDocRoot
));
2589 ASSERT_TRUE(https_test_server
.Start());
2592 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2593 ASSERT_EQ(BookmarkBar::DETACHED
, browser()->bookmark_bar_state());
2594 WebContents
* web_contents
=
2595 browser()->tab_strip_model()->GetActiveWebContents();
2596 content::RenderViewHost
* prev_rvh
= web_contents
->GetRenderViewHost();
2597 const int height_inset
=
2598 browser()->window()->GetRenderViewHeightInsetWithDetachedBookmarkBar();
2599 const gfx::Size initial_wcv_size
=
2600 web_contents
->GetContainerBounds().size();
2601 RenderViewSizeObserver
observer(web_contents
, browser()->window());
2603 // Navigate to a non-NTP page, without resizing WebContentsView.
2604 ui_test_utils::NavigateToURL(browser(),
2605 test_server()->GetURL("files/title1.html"));
2606 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2607 // A new RenderViewHost should be created.
2608 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2609 prev_rvh
= web_contents
->GetRenderViewHost();
2610 gfx::Size rwhv_create_size0
, rwhv_commit_size0
, wcv_commit_size0
;
2611 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2615 // The create height of RenderWidgetHostView should include the height inset.
2616 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2617 initial_wcv_size
.height() + height_inset
),
2619 // When a navigation entry is committed, the size of RenderWidgetHostView
2620 // should be the same as when it was first created.
2621 EXPECT_EQ(rwhv_create_size0
, rwhv_commit_size0
);
2622 // Sizes of the current RenderWidgetHostView and WebContentsView should not
2623 // change before and after WebContentsDelegate::DidNavigateMainFramePostCommit
2624 // (implemented by Browser); we obtain the sizes before PostCommit via
2625 // WebContentsObserver::NavigationEntryCommitted (implemented by
2626 // RenderViewSizeObserver).
2627 EXPECT_EQ(rwhv_commit_size0
,
2628 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2629 // The behavior differs between OSX and views.
2630 // In OSX, the wcv does not change size until after the commit, when the
2631 // bookmark bar disappears (correct).
2632 // In views, the wcv changes size at commit time.
2633 #if defined(OS_MACOSX)
2634 EXPECT_EQ(gfx::Size(wcv_commit_size0
.width(),
2635 wcv_commit_size0
.height() + height_inset
),
2636 web_contents
->GetContainerBounds().size());
2638 EXPECT_EQ(wcv_commit_size0
, web_contents
->GetContainerBounds().size());
2641 // Navigate to another non-NTP page, without resizing WebContentsView.
2642 ui_test_utils::NavigateToURL(browser(),
2643 https_test_server
.GetURL("files/title2.html"));
2644 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2645 // A new RenderVieHost should be created.
2646 EXPECT_NE(prev_rvh
, web_contents
->GetRenderViewHost());
2647 gfx::Size rwhv_create_size1
, rwhv_commit_size1
, wcv_commit_size1
;
2648 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2652 EXPECT_EQ(rwhv_create_size1
, rwhv_commit_size1
);
2653 EXPECT_EQ(rwhv_commit_size1
,
2654 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2655 EXPECT_EQ(wcv_commit_size1
, web_contents
->GetContainerBounds().size());
2657 // Navigate from NTP to a non-NTP page, resizing WebContentsView while
2658 // navigation entry is pending.
2659 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2660 gfx::Size
wcv_resize_insets(1, 1);
2661 observer
.set_wcv_resize_insets(wcv_resize_insets
);
2662 ui_test_utils::NavigateToURL(browser(),
2663 test_server()->GetURL("files/title2.html"));
2664 ASSERT_EQ(BookmarkBar::HIDDEN
, browser()->bookmark_bar_state());
2665 gfx::Size rwhv_create_size2
, rwhv_commit_size2
, wcv_commit_size2
;
2666 observer
.GetSizeForRenderViewHost(web_contents
->GetRenderViewHost(),
2671 // The behavior on OSX and Views is incorrect in this edge case, but they are
2672 // differently incorrect.
2673 // The behavior should be:
2674 // initial wcv size: (100,100) (to choose random numbers)
2675 // initial rwhv size: (100,140)
2676 // commit wcv size: (101, 101)
2677 // commit rwhv size: (101, 141)
2678 // final wcv size: (101, 141)
2679 // final rwhv size: (101, 141)
2681 // On OSX, the commit rwhv size is (101, 101)
2682 // On views, the commit wcv size is (101, 141)
2683 // All other sizes are correct.
2685 // The create height of RenderWidgetHostView should include the height inset.
2686 EXPECT_EQ(gfx::Size(initial_wcv_size
.width(),
2687 initial_wcv_size
.height() + height_inset
),
2689 gfx::Size
exp_commit_size(initial_wcv_size
);
2691 #if defined(OS_MACOSX)
2692 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2693 wcv_resize_insets
.height());
2695 exp_commit_size
.Enlarge(wcv_resize_insets
.width(),
2696 wcv_resize_insets
.height() + height_inset
);
2698 EXPECT_EQ(exp_commit_size
, rwhv_commit_size2
);
2699 EXPECT_EQ(exp_commit_size
, wcv_commit_size2
);
2700 gfx::Size
exp_final_size(initial_wcv_size
);
2701 exp_final_size
.Enlarge(wcv_resize_insets
.width(),
2702 wcv_resize_insets
.height() + height_inset
);
2703 EXPECT_EQ(exp_final_size
,
2704 web_contents
->GetRenderWidgetHostView()->GetViewBounds().size());
2705 EXPECT_EQ(exp_final_size
, web_contents
->GetContainerBounds().size());