NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / browser_browsertest.cc
blob198f56dbc5e42fdcd6556a8deb9b903a66891a38
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/files/file_path.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/sys_info.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/chrome_content_browser_client.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/command_updater.h"
18 #include "chrome/browser/content_settings/host_content_settings_map.h"
19 #include "chrome/browser/defaults.h"
20 #include "chrome/browser/extensions/extension_browsertest.h"
21 #include "chrome/browser/extensions/extension_service.h"
22 #include "chrome/browser/extensions/tab_helper.h"
23 #include "chrome/browser/first_run/first_run.h"
24 #include "chrome/browser/lifetime/application_lifetime.h"
25 #include "chrome/browser/prefs/incognito_mode_prefs.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/browser/profiles/profile_manager.h"
28 #include "chrome/browser/sessions/session_backend.h"
29 #include "chrome/browser/sessions/session_service_factory.h"
30 #include "chrome/browser/translate/translate_tab_helper.h"
31 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h"
32 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h"
33 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h"
34 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h"
35 #include "chrome/browser/ui/browser.h"
36 #include "chrome/browser/ui/browser_command_controller.h"
37 #include "chrome/browser/ui/browser_commands.h"
38 #include "chrome/browser/ui/browser_finder.h"
39 #include "chrome/browser/ui/browser_iterator.h"
40 #include "chrome/browser/ui/browser_navigator.h"
41 #include "chrome/browser/ui/browser_tabstrip.h"
42 #include "chrome/browser/ui/browser_ui_prefs.h"
43 #include "chrome/browser/ui/browser_window.h"
44 #include "chrome/browser/ui/extensions/application_launch.h"
45 #include "chrome/browser/ui/host_desktop.h"
46 #include "chrome/browser/ui/startup/startup_browser_creator.h"
47 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
48 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
49 #include "chrome/browser/ui/tabs/tab_strip_model.h"
50 #include "chrome/common/chrome_switches.h"
51 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
52 #include "chrome/common/url_constants.h"
53 #include "chrome/test/base/in_process_browser_test.h"
54 #include "chrome/test/base/test_switches.h"
55 #include "chrome/test/base/ui_test_utils.h"
56 #include "components/translate/core/common/language_detection_details.h"
57 #include "content/public/browser/favicon_status.h"
58 #include "content/public/browser/host_zoom_map.h"
59 #include "content/public/browser/interstitial_page.h"
60 #include "content/public/browser/interstitial_page_delegate.h"
61 #include "content/public/browser/navigation_entry.h"
62 #include "content/public/browser/notification_service.h"
63 #include "content/public/browser/render_process_host.h"
64 #include "content/public/browser/render_view_host.h"
65 #include "content/public/browser/render_widget_host_view.h"
66 #include "content/public/browser/resource_context.h"
67 #include "content/public/browser/web_contents.h"
68 #include "content/public/browser/web_contents_observer.h"
69 #include "content/public/browser/web_contents_view.h"
70 #include "content/public/common/frame_navigate_params.h"
71 #include "content/public/common/page_transition_types.h"
72 #include "content/public/common/renderer_preferences.h"
73 #include "content/public/common/url_constants.h"
74 #include "content/public/test/browser_test_utils.h"
75 #include "content/public/test/test_navigation_observer.h"
76 #include "extensions/browser/extension_system.h"
77 #include "extensions/common/extension.h"
78 #include "extensions/common/extension_set.h"
79 #include "grit/chromium_strings.h"
80 #include "grit/generated_resources.h"
81 #include "net/dns/mock_host_resolver.h"
82 #include "net/test/spawned_test_server/spawned_test_server.h"
83 #include "ui/base/l10n/l10n_util.h"
85 #if defined(OS_MACOSX)
86 #include "base/mac/mac_util.h"
87 #include "base/mac/scoped_nsautorelease_pool.h"
88 #include "chrome/browser/ui/cocoa/run_loop_testing.h"
89 #endif
91 #if defined(OS_WIN)
92 #include "base/i18n/rtl.h"
93 #include "chrome/browser/browser_process.h"
94 #endif
96 using base::ASCIIToUTF16;
97 using content::InterstitialPage;
98 using content::HostZoomMap;
99 using content::NavigationController;
100 using content::NavigationEntry;
101 using content::OpenURLParams;
102 using content::Referrer;
103 using content::WebContents;
104 using content::WebContentsObserver;
105 using extensions::Extension;
107 namespace {
109 const char* kBeforeUnloadHTML =
110 "<html><head><title>beforeunload</title></head><body>"
111 "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
112 "</body></html>";
114 const char* kOpenNewBeforeUnloadPage =
115 "w=window.open(); w.onbeforeunload=function(e){return 'foo'};";
117 const base::FilePath::CharType* kBeforeUnloadFile =
118 FILE_PATH_LITERAL("beforeunload.html");
120 const base::FilePath::CharType* kTitle1File = FILE_PATH_LITERAL("title1.html");
121 const base::FilePath::CharType* kTitle2File = FILE_PATH_LITERAL("title2.html");
123 const base::FilePath::CharType kDocRoot[] =
124 FILE_PATH_LITERAL("chrome/test/data");
126 // Given a page title, returns the expected window caption string.
127 base::string16 WindowCaptionFromPageTitle(const base::string16& page_title) {
128 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
129 // On Mac or ChromeOS, we don't want to suffix the page title with
130 // the application name.
131 if (page_title.empty())
132 return l10n_util::GetStringUTF16(IDS_BROWSER_WINDOW_MAC_TAB_UNTITLED);
133 return page_title;
134 #else
135 if (page_title.empty())
136 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
138 return l10n_util::GetStringFUTF16(IDS_BROWSER_WINDOW_TITLE_FORMAT,
139 page_title);
140 #endif
143 // Returns the number of active RenderProcessHosts.
144 int CountRenderProcessHosts() {
145 int result = 0;
146 for (content::RenderProcessHost::iterator i(
147 content::RenderProcessHost::AllHostsIterator());
148 !i.IsAtEnd(); i.Advance())
149 ++result;
150 return result;
153 class MockTabStripModelObserver : public TabStripModelObserver {
154 public:
155 MockTabStripModelObserver() : closing_count_(0) {}
157 virtual void TabClosingAt(TabStripModel* tab_strip_model,
158 WebContents* contents,
159 int index) OVERRIDE {
160 ++closing_count_;
163 int closing_count() const { return closing_count_; }
165 private:
166 int closing_count_;
168 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver);
171 class InterstitialObserver : public content::WebContentsObserver {
172 public:
173 InterstitialObserver(content::WebContents* web_contents,
174 const base::Closure& attach_callback,
175 const base::Closure& detach_callback)
176 : WebContentsObserver(web_contents),
177 attach_callback_(attach_callback),
178 detach_callback_(detach_callback) {
181 virtual void DidAttachInterstitialPage() OVERRIDE {
182 attach_callback_.Run();
185 virtual void DidDetachInterstitialPage() OVERRIDE {
186 detach_callback_.Run();
189 private:
190 base::Closure attach_callback_;
191 base::Closure detach_callback_;
193 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
196 // Causes the browser to swap processes on a redirect to an HTTPS URL.
197 class TransferHttpsRedirectsContentBrowserClient
198 : public chrome::ChromeContentBrowserClient {
199 public:
200 virtual bool ShouldSwapProcessesForRedirect(
201 content::ResourceContext* resource_context,
202 const GURL& current_url,
203 const GURL& new_url) OVERRIDE {
204 return new_url.SchemeIs(content::kHttpsScheme);
208 // Used by CloseWithAppMenuOpen. Invokes CloseWindow on the supplied browser.
209 void CloseWindowCallback(Browser* browser) {
210 chrome::CloseWindow(browser);
213 // Used by CloseWithAppMenuOpen. Posts a CloseWindowCallback and shows the app
214 // menu.
215 void RunCloseWithAppMenuCallback(Browser* browser) {
216 // ShowAppMenu is modal under views. Schedule a task that closes the window.
217 base::MessageLoop::current()->PostTask(
218 FROM_HERE, base::Bind(&CloseWindowCallback, browser));
219 chrome::ShowAppMenu(browser);
222 // Displays "INTERSTITIAL" while the interstitial is attached.
223 // (InterstitialPage can be used in a test directly, but there would be no way
224 // to visually tell if it is showing or not.)
225 class TestInterstitialPage : public content::InterstitialPageDelegate {
226 public:
227 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) {
228 interstitial_page_ = InterstitialPage::Create(
229 tab, new_navigation, url , this);
230 interstitial_page_->Show();
232 virtual ~TestInterstitialPage() { }
233 void Proceed() {
234 interstitial_page_->Proceed();
236 void DontProceed() {
237 interstitial_page_->DontProceed();
240 virtual std::string GetHTMLContents() OVERRIDE {
241 return "<h1>INTERSTITIAL</h1>";
244 private:
245 InterstitialPage* interstitial_page_; // Owns us.
248 class RenderViewSizeObserver : public content::WebContentsObserver {
249 public:
250 RenderViewSizeObserver(content::WebContents* web_contents,
251 BrowserWindow* browser_window)
252 : WebContentsObserver(web_contents),
253 browser_window_(browser_window) {
256 void GetSizeForRenderViewHost(
257 content::RenderViewHost* render_view_host,
258 gfx::Size* rwhv_create_size,
259 gfx::Size* rwhv_commit_size,
260 gfx::Size* wcv_commit_size) {
261 RenderViewSizes::const_iterator result = render_view_sizes_.end();
262 result = render_view_sizes_.find(render_view_host);
263 if (result != render_view_sizes_.end()) {
264 *rwhv_create_size = result->second.rwhv_create_size;
265 *rwhv_commit_size = result->second.rwhv_commit_size;
266 *wcv_commit_size = result->second.wcv_commit_size;
270 void set_wcv_resize_insets(const gfx::Size& wcv_resize_insets) {
271 wcv_resize_insets_ = wcv_resize_insets;
274 // Cache the size when RenderViewHost is first created.
275 virtual void RenderViewCreated(
276 content::RenderViewHost* render_view_host) OVERRIDE {
277 render_view_sizes_[render_view_host].rwhv_create_size =
278 render_view_host->GetView()->GetViewBounds().size();
281 // Enlarge WebContentsView by |wcv_resize_insets_| while the navigation entry
282 // is pending.
283 virtual void DidStartNavigationToPendingEntry(
284 const GURL& url,
285 NavigationController::ReloadType reload_type) OVERRIDE {
286 if (wcv_resize_insets_.IsEmpty())
287 return;
288 // Resizing the main browser window by |wcv_resize_insets_| will
289 // automatically resize the WebContentsView by the same amount.
290 // Just resizing WebContentsView directly doesn't work on Linux, because the
291 // next automatic layout of the browser window will resize WebContentsView
292 // back to the previous size. To make it consistent, resize main browser
293 // window on all platforms.
294 gfx::Rect bounds(browser_window_->GetBounds());
295 gfx::Size size(bounds.size());
296 size.Enlarge(wcv_resize_insets_.width(), wcv_resize_insets_.height());
297 bounds.set_size(size);
298 browser_window_->SetBounds(bounds);
299 // Let the message loop run so that resize actually takes effect.
300 content::RunAllPendingInMessageLoop();
303 // Cache the sizes of RenderWidgetHostView and WebContentsView when the
304 // navigation entry is committed, which is before
305 // WebContentsDelegate::DidNavigateMainFramePostCommit is called.
306 virtual void NavigationEntryCommitted(
307 const content::LoadCommittedDetails& details) OVERRIDE {
308 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
309 render_view_sizes_[rvh].rwhv_commit_size =
310 web_contents()->GetRenderWidgetHostView()->GetViewBounds().size();
311 render_view_sizes_[rvh].wcv_commit_size =
312 web_contents()->GetView()->GetContainerSize();
315 private:
316 struct Sizes {
317 gfx::Size rwhv_create_size; // Size of RenderWidgetHostView when created.
318 gfx::Size rwhv_commit_size; // Size of RenderWidgetHostView when committed.
319 gfx::Size wcv_commit_size; // Size of WebContentsView when committed.
322 typedef std::map<content::RenderViewHost*, Sizes> RenderViewSizes;
323 RenderViewSizes render_view_sizes_;
324 // Enlarge WebContentsView by this size insets in
325 // DidStartNavigationToPendingEntry.
326 gfx::Size wcv_resize_insets_;
327 BrowserWindow* browser_window_; // Weak ptr.
329 DISALLOW_COPY_AND_ASSIGN(RenderViewSizeObserver);
332 } // namespace
334 class BrowserTest : public ExtensionBrowserTest {
335 protected:
336 // In RTL locales wrap the page title with RTL embedding characters so that it
337 // matches the value returned by GetWindowTitle().
338 base::string16 LocaleWindowCaptionFromPageTitle(
339 const base::string16& expected_title) {
340 base::string16 page_title = WindowCaptionFromPageTitle(expected_title);
341 #if defined(OS_WIN)
342 std::string locale = g_browser_process->GetApplicationLocale();
343 if (base::i18n::GetTextDirectionForLocale(locale.c_str()) ==
344 base::i18n::RIGHT_TO_LEFT) {
345 base::i18n::WrapStringWithLTRFormatting(&page_title);
348 return page_title;
349 #else
350 // Do we need to use the above code on POSIX as well?
351 return page_title;
352 #endif
355 // Returns the app extension aptly named "App Test".
356 const Extension* GetExtension() {
357 const extensions::ExtensionSet* extensions =
358 extensions::ExtensionSystem::Get(
359 browser()->profile())->extension_service()->extensions();
360 for (extensions::ExtensionSet::const_iterator it = extensions->begin();
361 it != extensions->end(); ++it) {
362 if ((*it)->name() == "App Test")
363 return it->get();
365 NOTREACHED();
366 return NULL;
370 // Launch the app on a page with no title, check that the app title was set
371 // correctly.
372 IN_PROC_BROWSER_TEST_F(BrowserTest, NoTitle) {
373 #if defined(OS_WIN) && defined(USE_ASH)
374 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
375 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
376 return;
377 #endif
379 ui_test_utils::NavigateToURL(
380 browser(), ui_test_utils::GetTestUrl(
381 base::FilePath(base::FilePath::kCurrentDirectory),
382 base::FilePath(kTitle1File)));
383 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(ASCIIToUTF16("title1.html")),
384 browser()->GetWindowTitleForCurrentTab());
385 base::string16 tab_title;
386 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title));
387 EXPECT_EQ(ASCIIToUTF16("title1.html"), tab_title);
390 // Launch the app, navigate to a page with a title, check that the app title
391 // was set correctly.
392 IN_PROC_BROWSER_TEST_F(BrowserTest, Title) {
393 #if defined(OS_WIN) && defined(USE_ASH)
394 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
395 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
396 return;
397 #endif
399 ui_test_utils::NavigateToURL(
400 browser(), ui_test_utils::GetTestUrl(
401 base::FilePath(base::FilePath::kCurrentDirectory),
402 base::FilePath(kTitle2File)));
403 const base::string16 test_title(ASCIIToUTF16("Title Of Awesomeness"));
404 EXPECT_EQ(LocaleWindowCaptionFromPageTitle(test_title),
405 browser()->GetWindowTitleForCurrentTab());
406 base::string16 tab_title;
407 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title));
408 EXPECT_EQ(test_title, tab_title);
411 IN_PROC_BROWSER_TEST_F(BrowserTest, JavascriptAlertActivatesTab) {
412 GURL url(ui_test_utils::GetTestUrl(base::FilePath(
413 base::FilePath::kCurrentDirectory), base::FilePath(kTitle1File)));
414 ui_test_utils::NavigateToURL(browser(), url);
415 AddTabAtIndex(0, url, content::PAGE_TRANSITION_TYPED);
416 EXPECT_EQ(2, browser()->tab_strip_model()->count());
417 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
418 WebContents* second_tab = browser()->tab_strip_model()->GetWebContentsAt(1);
419 ASSERT_TRUE(second_tab);
420 second_tab->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
421 base::string16(),
422 ASCIIToUTF16("alert('Activate!');"));
423 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
424 alert->CloseModalDialog();
425 EXPECT_EQ(2, browser()->tab_strip_model()->count());
426 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
430 #if defined(OS_WIN) && !defined(NDEBUG)
431 // http://crbug.com/114859. Times out frequently on Windows.
432 #define MAYBE_ThirtyFourTabs DISABLED_ThirtyFourTabs
433 #else
434 #define MAYBE_ThirtyFourTabs ThirtyFourTabs
435 #endif
437 // Create 34 tabs and verify that a lot of processes have been created. The
438 // exact number of processes depends on the amount of memory. Previously we
439 // had a hard limit of 31 processes and this test is mainly directed at
440 // verifying that we don't crash when we pass this limit.
441 // Warning: this test can take >30 seconds when running on a slow (low
442 // memory?) Mac builder.
443 IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_ThirtyFourTabs) {
444 GURL url(ui_test_utils::GetTestUrl(base::FilePath(
445 base::FilePath::kCurrentDirectory), base::FilePath(kTitle2File)));
447 // There is one initial tab.
448 const int kTabCount = 34;
449 for (int ix = 0; ix != (kTabCount - 1); ++ix) {
450 chrome::AddSelectedTabWithURL(browser(), url,
451 content::PAGE_TRANSITION_TYPED);
453 EXPECT_EQ(kTabCount, browser()->tab_strip_model()->count());
455 // See GetMaxRendererProcessCount() in
456 // content/browser/renderer_host/render_process_host_impl.cc
457 // for the algorithm to decide how many processes to create.
458 const int kExpectedProcessCount =
459 #if defined(ARCH_CPU_64_BITS)
461 #else
463 #endif
464 if (base::SysInfo::AmountOfPhysicalMemoryMB() >= 2048) {
465 EXPECT_GE(CountRenderProcessHosts(), kExpectedProcessCount);
466 } else {
467 EXPECT_LT(CountRenderProcessHosts(), kExpectedProcessCount);
471 // Test for crbug.com/297289. Ensure that modal dialogs are closed when a
472 // cross-process navigation is ready to commit.
473 IN_PROC_BROWSER_TEST_F(BrowserTest, CrossProcessNavCancelsDialogs) {
474 ASSERT_TRUE(test_server()->Start());
475 host_resolver()->AddRule("www.example.com", "127.0.0.1");
476 GURL url(test_server()->GetURL("empty.html"));
477 ui_test_utils::NavigateToURL(browser(), url);
479 // Test this with multiple alert dialogs to ensure that we can navigate away
480 // even if the renderer tries to synchronously create more.
481 // See http://crbug.com/312490.
482 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
483 contents->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
484 base::string16(),
485 ASCIIToUTF16("alert('one'); alert('two');"));
486 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
487 EXPECT_TRUE(alert->IsValid());
488 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance();
489 EXPECT_TRUE(dialog_queue->HasActiveDialog());
491 // A cross-site navigation should force the dialog to close.
492 GURL url2("http://www.example.com/empty.html");
493 ui_test_utils::NavigateToURL(browser(), url2);
494 EXPECT_FALSE(dialog_queue->HasActiveDialog());
496 // Make sure input events still work in the renderer process.
497 EXPECT_FALSE(contents->GetRenderProcessHost()->IgnoreInputEvents());
500 // Test for crbug.com/22004. Reloading a page with a before unload handler and
501 // then canceling the dialog should not leave the throbber spinning.
502 IN_PROC_BROWSER_TEST_F(BrowserTest, ReloadThenCancelBeforeUnload) {
503 GURL url(std::string("data:text/html,") + kBeforeUnloadHTML);
504 ui_test_utils::NavigateToURL(browser(), url);
506 // Navigate to another page, but click cancel in the dialog. Make sure that
507 // the throbber stops spinning.
508 chrome::Reload(browser(), CURRENT_TAB);
509 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
510 alert->CloseModalDialog();
511 EXPECT_FALSE(
512 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
514 // Clear the beforeunload handler so the test can easily exit.
515 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost()->
516 ExecuteJavascriptInWebFrame(base::string16(),
517 ASCIIToUTF16("onbeforeunload=null;"));
520 class RedirectObserver : public content::WebContentsObserver {
521 public:
522 explicit RedirectObserver(content::WebContents* web_contents)
523 : WebContentsObserver(web_contents) {
526 virtual void DidNavigateAnyFrame(
527 const content::LoadCommittedDetails& details,
528 const content::FrameNavigateParams& params) OVERRIDE {
529 params_ = params;
532 virtual void WebContentsDestroyed(WebContents* contents) OVERRIDE {
533 // Make sure we don't close the tab while the observer is in scope.
534 // See http://crbug.com/314036.
535 FAIL() << "WebContents closed during navigation (http://crbug.com/314036).";
538 const content::FrameNavigateParams& params() const {
539 return params_;
542 private:
543 content::FrameNavigateParams params_;
545 DISALLOW_COPY_AND_ASSIGN(RedirectObserver);
548 // Ensure that a transferred cross-process navigation does not generate
549 // DidStopLoading events until the navigation commits. If it did, then
550 // ui_test_utils::NavigateToURL would proceed before the URL had committed.
551 // http://crbug.com/243957.
552 IN_PROC_BROWSER_TEST_F(BrowserTest, NoStopDuringTransferUntilCommit) {
553 // Create HTTP and HTTPS servers for a cross-site transition.
554 ASSERT_TRUE(test_server()->Start());
555 net::SpawnedTestServer https_test_server(net::SpawnedTestServer::TYPE_HTTPS,
556 net::SpawnedTestServer::kLocalhost,
557 base::FilePath(kDocRoot));
558 ASSERT_TRUE(https_test_server.Start());
560 // Temporarily replace ContentBrowserClient with one that will cause a
561 // process swap on all redirects to HTTPS URLs.
562 TransferHttpsRedirectsContentBrowserClient new_client;
563 content::ContentBrowserClient* old_client =
564 SetBrowserClientForTesting(&new_client);
566 GURL init_url(test_server()->GetURL("files/title1.html"));
567 ui_test_utils::NavigateToURL(browser(), init_url);
569 // Navigate to a same-site page that redirects, causing a transfer.
570 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
572 // Create a RedirectObserver that goes away before we close the tab.
574 RedirectObserver redirect_observer(contents);
575 GURL dest_url(https_test_server.GetURL("files/title2.html"));
576 GURL redirect_url(test_server()->GetURL("server-redirect?" +
577 dest_url.spec()));
578 ui_test_utils::NavigateToURL(browser(), redirect_url);
580 // We should immediately see the new committed entry.
581 EXPECT_FALSE(contents->GetController().GetPendingEntry());
582 EXPECT_EQ(dest_url,
583 contents->GetController().GetLastCommittedEntry()->GetURL());
585 // We should keep track of the original request URL, redirect chain, and
586 // page transition type during a transfer, since these are necessary for
587 // history autocomplete to work.
588 EXPECT_EQ(redirect_url, contents->GetController().GetLastCommittedEntry()->
589 GetOriginalRequestURL());
590 EXPECT_EQ(2U, redirect_observer.params().redirects.size());
591 EXPECT_EQ(redirect_url, redirect_observer.params().redirects.at(0));
592 EXPECT_EQ(dest_url, redirect_observer.params().redirects.at(1));
593 EXPECT_TRUE(PageTransitionCoreTypeIs(redirect_observer.params().transition,
594 content::PAGE_TRANSITION_TYPED));
597 // Restore previous browser client.
598 SetBrowserClientForTesting(old_client);
601 // Tests that a cross-process redirect will only cause the beforeunload
602 // handler to run once.
603 IN_PROC_BROWSER_TEST_F(BrowserTest, SingleBeforeUnloadAfterRedirect) {
604 // Create HTTP and HTTPS servers for a cross-site transition.
605 ASSERT_TRUE(test_server()->Start());
606 net::SpawnedTestServer https_test_server(net::SpawnedTestServer::TYPE_HTTPS,
607 net::SpawnedTestServer::kLocalhost,
608 base::FilePath(kDocRoot));
609 ASSERT_TRUE(https_test_server.Start());
611 // Temporarily replace ContentBrowserClient with one that will cause a
612 // process swap on all redirects to HTTPS URLs.
613 TransferHttpsRedirectsContentBrowserClient new_client;
614 content::ContentBrowserClient* old_client =
615 SetBrowserClientForTesting(&new_client);
617 // Navigate to a page with a beforeunload handler.
618 GURL url(test_server()->GetURL("files/beforeunload.html"));
619 ui_test_utils::NavigateToURL(browser(), url);
621 // Navigate to a URL that redirects to another process and approve the
622 // beforeunload dialog that pops up.
623 content::WindowedNotificationObserver nav_observer(
624 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
625 content::NotificationService::AllSources());
626 GURL https_url(https_test_server.GetURL("files/title1.html"));
627 GURL redirect_url(test_server()->GetURL("server-redirect?" +
628 https_url.spec()));
629 browser()->OpenURL(OpenURLParams(redirect_url, Referrer(), CURRENT_TAB,
630 content::PAGE_TRANSITION_TYPED, false));
631 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
632 EXPECT_TRUE(
633 static_cast<JavaScriptAppModalDialog*>(alert)->is_before_unload_dialog());
634 alert->native_dialog()->AcceptAppModalDialog();
635 nav_observer.Wait();
637 // Restore previous browser client.
638 SetBrowserClientForTesting(old_client);
641 // Test for crbug.com/80401. Canceling a before unload dialog should reset
642 // the URL to the previous page's URL.
643 IN_PROC_BROWSER_TEST_F(BrowserTest, CancelBeforeUnloadResetsURL) {
644 GURL url(ui_test_utils::GetTestUrl(base::FilePath(
645 base::FilePath::kCurrentDirectory), base::FilePath(kBeforeUnloadFile)));
646 ui_test_utils::NavigateToURL(browser(), url);
648 // Navigate to a page that triggers a cross-site transition.
649 ASSERT_TRUE(test_server()->Start());
650 GURL url2(test_server()->GetURL("files/title1.html"));
651 browser()->OpenURL(OpenURLParams(
652 url2, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
654 content::WindowedNotificationObserver host_destroyed_observer(
655 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
656 content::NotificationService::AllSources());
658 // Cancel the dialog.
659 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
660 alert->CloseModalDialog();
661 EXPECT_FALSE(
662 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
664 // Verify there are no pending history items after the dialog is cancelled.
665 // (see crbug.com/93858)
666 NavigationEntry* entry = browser()->tab_strip_model()->
667 GetActiveWebContents()->GetController().GetPendingEntry();
668 EXPECT_EQ(NULL, entry);
670 // Wait for the ShouldClose_ACK to arrive. We can detect it by waiting for
671 // the pending RVH to be destroyed.
672 host_destroyed_observer.Wait();
673 EXPECT_EQ(url, browser()->toolbar_model()->GetURL());
675 // Clear the beforeunload handler so the test can easily exit.
676 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost()->
677 ExecuteJavascriptInWebFrame(base::string16(),
678 ASCIIToUTF16("onbeforeunload=null;"));
681 // Crashy on mac. http://crbug.com/38522 Crashy on win too (after 3 years).
682 #if defined(OS_MACOSX) || defined(OS_WIN)
683 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
684 DISABLED_SingleBeforeUnloadAfterWindowClose
685 #else
686 #define MAYBE_SingleBeforeUnloadAfterWindowClose \
687 SingleBeforeUnloadAfterWindowClose
688 #endif
690 // Test for crbug.com/11647. A page closed with window.close() should not have
691 // two beforeunload dialogs shown.
692 IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_SingleBeforeUnloadAfterWindowClose) {
693 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost()->
694 ExecuteJavascriptInWebFrame(base::string16(),
695 ASCIIToUTF16(kOpenNewBeforeUnloadPage));
697 // Close the new window with JavaScript, which should show a single
698 // beforeunload dialog. Then show another alert, to make it easy to verify
699 // that a second beforeunload dialog isn't shown.
700 browser()->tab_strip_model()->GetWebContentsAt(0)->GetRenderViewHost()->
701 ExecuteJavascriptInWebFrame(base::string16(),
702 ASCIIToUTF16("w.close(); alert('bar');"));
703 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
704 alert->native_dialog()->AcceptAppModalDialog();
706 alert = ui_test_utils::WaitForAppModalDialog();
707 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog*>(alert)->
708 is_before_unload_dialog());
709 alert->native_dialog()->AcceptAppModalDialog();
712 // BrowserTest.BeforeUnloadVsBeforeReload times out on Windows.
713 // http://crbug.com/130411
714 #if defined(OS_WIN)
715 #define MAYBE_BeforeUnloadVsBeforeReload DISABLED_BeforeUnloadVsBeforeReload
716 #else
717 #define MAYBE_BeforeUnloadVsBeforeReload BeforeUnloadVsBeforeReload
718 #endif
720 // Test that when a page has an onunload handler, reloading a page shows a
721 // different dialog than navigating to a different page.
722 IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_BeforeUnloadVsBeforeReload) {
723 GURL url(std::string("data:text/html,") + kBeforeUnloadHTML);
724 ui_test_utils::NavigateToURL(browser(), url);
726 // Reload the page, and check that we get a "before reload" dialog.
727 chrome::Reload(browser(), CURRENT_TAB);
728 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
729 EXPECT_TRUE(static_cast<JavaScriptAppModalDialog*>(alert)->is_reload());
731 // Cancel the reload.
732 alert->native_dialog()->CancelAppModalDialog();
734 // Navigate to another url, and check that we get a "before unload" dialog.
735 GURL url2(std::string("about:blank"));
736 browser()->OpenURL(OpenURLParams(
737 url2, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
739 alert = ui_test_utils::WaitForAppModalDialog();
740 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog*>(alert)->is_reload());
742 // Accept the navigation so we end up on a page without a beforeunload hook.
743 alert->native_dialog()->AcceptAppModalDialog();
746 // BeforeUnloadAtQuitWithTwoWindows is a regression test for
747 // http://crbug.com/11842. It opens two windows, one of which has a
748 // beforeunload handler and attempts to exit cleanly.
749 class BeforeUnloadAtQuitWithTwoWindows : public InProcessBrowserTest {
750 public:
751 // This test is for testing a specific shutdown behavior. This mimics what
752 // happens in InProcessBrowserTest::RunTestOnMainThread and QuitBrowsers, but
753 // ensures that it happens through the single IDC_EXIT of the test.
754 virtual void CleanUpOnMainThread() OVERRIDE {
755 // Cycle both the MessageLoop and the Cocoa runloop twice to flush out any
756 // Chrome work that generates Cocoa work. Do this twice since there are two
757 // Browsers that must be closed.
758 CycleRunLoops();
759 CycleRunLoops();
761 // Run the application event loop to completion, which will cycle the
762 // native MessagePump on all platforms.
763 base::MessageLoop::current()->PostTask(FROM_HERE,
764 base::MessageLoop::QuitClosure());
765 base::MessageLoop::current()->Run();
767 // Take care of any remaining Cocoa work.
768 CycleRunLoops();
770 // At this point, quit should be for real now.
771 ASSERT_EQ(0u, chrome::GetTotalBrowserCount());
774 // A helper function that cycles the MessageLoop, and on Mac, the Cocoa run
775 // loop. It also drains the NSAutoreleasePool.
776 void CycleRunLoops() {
777 content::RunAllPendingInMessageLoop();
778 #if defined(OS_MACOSX)
779 chrome::testing::NSRunLoopRunAllPending();
780 AutoreleasePool()->Recycle();
781 #endif
785 // Disabled, http://crbug.com/159214 .
786 IN_PROC_BROWSER_TEST_F(BeforeUnloadAtQuitWithTwoWindows,
787 DISABLED_IfThisTestTimesOutItIndicatesFAILURE) {
788 // In the first browser, set up a page that has a beforeunload handler.
789 GURL url(std::string("data:text/html,") + kBeforeUnloadHTML);
790 ui_test_utils::NavigateToURL(browser(), url);
792 // Open a second browser window at about:blank.
793 ui_test_utils::BrowserAddedObserver browser_added_observer;
794 chrome::NewEmptyWindow(browser()->profile(), chrome::GetActiveDesktop());
795 Browser* second_window = browser_added_observer.WaitForSingleNewBrowser();
796 ui_test_utils::NavigateToURL(second_window, GURL("about:blank"));
798 // Tell the application to quit. IDC_EXIT calls AttemptUserExit, which on
799 // everything but ChromeOS allows unload handlers to block exit. On that
800 // platform, though, it exits unconditionally. See the comment and bug ID
801 // in AttemptUserExit() in application_lifetime.cc.
802 #if defined(OS_CHROMEOS)
803 chrome::AttemptExit();
804 #else
805 chrome::ExecuteCommand(second_window, IDC_EXIT);
806 #endif
808 // The beforeunload handler will run at exit, ensure it does, and then accept
809 // it to allow shutdown to proceed.
810 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
811 ASSERT_TRUE(alert);
812 EXPECT_TRUE(
813 static_cast<JavaScriptAppModalDialog*>(alert)->is_before_unload_dialog());
814 alert->native_dialog()->AcceptAppModalDialog();
816 // But wait there's more! If this test times out, it likely means that the
817 // browser has not been able to quit correctly, indicating there's a
818 // regression of the bug noted above.
821 // Test that scripts can fork a new renderer process for a cross-site popup,
822 // based on http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab.
823 // The script must open a new tab, set its window.opener to null, and navigate
824 // it to a cross-site URL. It should also work for meta-refreshes.
825 // See http://crbug.com/93517.
826 IN_PROC_BROWSER_TEST_F(BrowserTest, NullOpenerRedirectForksProcess) {
827 CommandLine::ForCurrentProcess()->AppendSwitch(
828 switches::kDisablePopupBlocking);
830 // Create http and https servers for a cross-site transition.
831 ASSERT_TRUE(test_server()->Start());
832 net::SpawnedTestServer https_test_server(net::SpawnedTestServer::TYPE_HTTPS,
833 net::SpawnedTestServer::kLocalhost,
834 base::FilePath(kDocRoot));
835 ASSERT_TRUE(https_test_server.Start());
836 GURL http_url(test_server()->GetURL("files/title1.html"));
837 GURL https_url(https_test_server.GetURL(std::string()));
839 // Start with an http URL.
840 ui_test_utils::NavigateToURL(browser(), http_url);
841 WebContents* oldtab = browser()->tab_strip_model()->GetActiveWebContents();
842 content::RenderProcessHost* process = oldtab->GetRenderProcessHost();
844 // Now open a tab to a blank page, set its opener to null, and redirect it
845 // cross-site.
846 std::string redirect_popup = "w=window.open();";
847 redirect_popup += "w.opener=null;";
848 redirect_popup += "w.document.location=\"";
849 redirect_popup += https_url.spec();
850 redirect_popup += "\";";
852 content::WindowedNotificationObserver popup_observer(
853 chrome::NOTIFICATION_TAB_ADDED,
854 content::NotificationService::AllSources());
855 content::WindowedNotificationObserver nav_observer(
856 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
857 content::NotificationService::AllSources());
858 oldtab->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
859 base::string16(), ASCIIToUTF16(redirect_popup));
861 // Wait for popup window to appear and finish navigating.
862 popup_observer.Wait();
863 ASSERT_EQ(2, browser()->tab_strip_model()->count());
864 WebContents* newtab = browser()->tab_strip_model()->GetActiveWebContents();
865 EXPECT_TRUE(newtab);
866 EXPECT_NE(oldtab, newtab);
867 nav_observer.Wait();
868 ASSERT_TRUE(newtab->GetController().GetLastCommittedEntry());
869 EXPECT_EQ(https_url.spec(),
870 newtab->GetController().GetLastCommittedEntry()->GetURL().spec());
872 // Popup window should not be in the opener's process.
873 content::RenderProcessHost* popup_process =
874 newtab->GetRenderProcessHost();
875 EXPECT_NE(process, popup_process);
877 // Now open a tab to a blank page, set its opener to null, and use a
878 // meta-refresh to navigate it instead.
879 std::string refresh_popup = "w=window.open();";
880 refresh_popup += "w.opener=null;";
881 refresh_popup += "w.document.write(";
882 refresh_popup += "'<META HTTP-EQUIV=\"refresh\" content=\"0; url=";
883 refresh_popup += https_url.spec();
884 refresh_popup += "\">');w.document.close();";
886 content::WindowedNotificationObserver popup_observer2(
887 chrome::NOTIFICATION_TAB_ADDED,
888 content::NotificationService::AllSources());
889 content::WindowedNotificationObserver nav_observer2(
890 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
891 content::NotificationService::AllSources());
892 oldtab->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
893 base::string16(), ASCIIToUTF16(refresh_popup));
895 // Wait for popup window to appear and finish navigating.
896 popup_observer2.Wait();
897 ASSERT_EQ(3, browser()->tab_strip_model()->count());
898 WebContents* newtab2 = browser()->tab_strip_model()->GetActiveWebContents();
899 EXPECT_TRUE(newtab2);
900 EXPECT_NE(oldtab, newtab2);
901 nav_observer2.Wait();
902 ASSERT_TRUE(newtab2->GetController().GetLastCommittedEntry());
903 EXPECT_EQ(https_url.spec(),
904 newtab2->GetController().GetLastCommittedEntry()->GetURL().spec());
906 // This popup window should also not be in the opener's process.
907 content::RenderProcessHost* popup_process2 =
908 newtab2->GetRenderProcessHost();
909 EXPECT_NE(process, popup_process2);
912 // Tests that other popup navigations that do not follow the steps at
913 // http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab will not
914 // fork a new renderer process.
915 IN_PROC_BROWSER_TEST_F(BrowserTest, OtherRedirectsDontForkProcess) {
916 CommandLine::ForCurrentProcess()->AppendSwitch(
917 switches::kDisablePopupBlocking);
919 // Create http and https servers for a cross-site transition.
920 ASSERT_TRUE(test_server()->Start());
921 net::SpawnedTestServer https_test_server(net::SpawnedTestServer::TYPE_HTTPS,
922 net::SpawnedTestServer::kLocalhost,
923 base::FilePath(kDocRoot));
924 ASSERT_TRUE(https_test_server.Start());
925 GURL http_url(test_server()->GetURL("files/title1.html"));
926 GURL https_url(https_test_server.GetURL(std::string()));
928 // Start with an http URL.
929 ui_test_utils::NavigateToURL(browser(), http_url);
930 WebContents* oldtab = browser()->tab_strip_model()->GetActiveWebContents();
931 content::RenderProcessHost* process = oldtab->GetRenderProcessHost();
933 // Now open a tab to a blank page, set its opener to null, and redirect it
934 // cross-site.
935 std::string dont_fork_popup = "w=window.open();";
936 dont_fork_popup += "w.document.location=\"";
937 dont_fork_popup += https_url.spec();
938 dont_fork_popup += "\";";
940 content::WindowedNotificationObserver popup_observer(
941 chrome::NOTIFICATION_TAB_ADDED,
942 content::NotificationService::AllSources());
943 content::WindowedNotificationObserver nav_observer(
944 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
945 content::NotificationService::AllSources());
946 oldtab->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
947 base::string16(), ASCIIToUTF16(dont_fork_popup));
949 // Wait for popup window to appear and finish navigating.
950 popup_observer.Wait();
951 ASSERT_EQ(2, browser()->tab_strip_model()->count());
952 WebContents* newtab = browser()->tab_strip_model()->GetActiveWebContents();
953 EXPECT_TRUE(newtab);
954 EXPECT_NE(oldtab, newtab);
955 nav_observer.Wait();
956 ASSERT_TRUE(newtab->GetController().GetLastCommittedEntry());
957 EXPECT_EQ(https_url.spec(),
958 newtab->GetController().GetLastCommittedEntry()->GetURL().spec());
960 // Popup window should still be in the opener's process.
961 content::RenderProcessHost* popup_process =
962 newtab->GetRenderProcessHost();
963 EXPECT_EQ(process, popup_process);
965 // Same thing if the current tab tries to navigate itself.
966 std::string navigate_str = "document.location=\"";
967 navigate_str += https_url.spec();
968 navigate_str += "\";";
970 content::WindowedNotificationObserver nav_observer2(
971 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
972 content::NotificationService::AllSources());
973 oldtab->GetRenderViewHost()->
974 ExecuteJavascriptInWebFrame(base::string16(), ASCIIToUTF16(navigate_str));
975 nav_observer2.Wait();
976 ASSERT_TRUE(oldtab->GetController().GetLastCommittedEntry());
977 EXPECT_EQ(https_url.spec(),
978 oldtab->GetController().GetLastCommittedEntry()->GetURL().spec());
980 // Original window should still be in the original process.
981 content::RenderProcessHost* new_process = newtab->GetRenderProcessHost();
982 EXPECT_EQ(process, new_process);
985 // Test that get_process_idle_time() returns reasonable values when compared
986 // with time deltas measured locally.
987 IN_PROC_BROWSER_TEST_F(BrowserTest, RenderIdleTime) {
988 base::TimeTicks start = base::TimeTicks::Now();
989 ui_test_utils::NavigateToURL(
990 browser(), ui_test_utils::GetTestUrl(
991 base::FilePath(base::FilePath::kCurrentDirectory),
992 base::FilePath(kTitle1File)));
993 content::RenderProcessHost::iterator it(
994 content::RenderProcessHost::AllHostsIterator());
995 for (; !it.IsAtEnd(); it.Advance()) {
996 base::TimeDelta renderer_td =
997 it.GetCurrentValue()->GetChildProcessIdleTime();
998 base::TimeDelta browser_td = base::TimeTicks::Now() - start;
999 EXPECT_TRUE(browser_td >= renderer_td);
1003 // Test IDC_CREATE_SHORTCUTS command is enabled for url scheme file, ftp, http
1004 // and https and disabled for chrome://, about:// etc.
1005 // TODO(pinkerton): Disable app-mode in the model until we implement it
1006 // on the Mac. http://crbug.com/13148
1007 #if !defined(OS_MACOSX)
1008 IN_PROC_BROWSER_TEST_F(BrowserTest, CommandCreateAppShortcutFile) {
1009 CommandUpdater* command_updater =
1010 browser()->command_controller()->command_updater();
1012 static const base::FilePath::CharType* kEmptyFile =
1013 FILE_PATH_LITERAL("empty.html");
1014 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
1015 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
1016 ASSERT_TRUE(file_url.SchemeIs(content::kFileScheme));
1017 ui_test_utils::NavigateToURL(browser(), file_url);
1018 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_CREATE_SHORTCUTS));
1021 IN_PROC_BROWSER_TEST_F(BrowserTest, CommandCreateAppShortcutHttp) {
1022 CommandUpdater* command_updater =
1023 browser()->command_controller()->command_updater();
1025 ASSERT_TRUE(test_server()->Start());
1026 GURL http_url(test_server()->GetURL(std::string()));
1027 ASSERT_TRUE(http_url.SchemeIs(content::kHttpScheme));
1028 ui_test_utils::NavigateToURL(browser(), http_url);
1029 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_CREATE_SHORTCUTS));
1032 IN_PROC_BROWSER_TEST_F(BrowserTest, CommandCreateAppShortcutHttps) {
1033 CommandUpdater* command_updater =
1034 browser()->command_controller()->command_updater();
1036 net::SpawnedTestServer test_server(net::SpawnedTestServer::TYPE_HTTPS,
1037 net::SpawnedTestServer::kLocalhost,
1038 base::FilePath(kDocRoot));
1039 ASSERT_TRUE(test_server.Start());
1040 GURL https_url(test_server.GetURL("/"));
1041 ASSERT_TRUE(https_url.SchemeIs(content::kHttpsScheme));
1042 ui_test_utils::NavigateToURL(browser(), https_url);
1043 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_CREATE_SHORTCUTS));
1046 IN_PROC_BROWSER_TEST_F(BrowserTest, CommandCreateAppShortcutFtp) {
1047 CommandUpdater* command_updater =
1048 browser()->command_controller()->command_updater();
1050 net::SpawnedTestServer test_server(net::SpawnedTestServer::TYPE_FTP,
1051 net::SpawnedTestServer::kLocalhost,
1052 base::FilePath(kDocRoot));
1053 ASSERT_TRUE(test_server.Start());
1054 GURL ftp_url(test_server.GetURL(std::string()));
1055 ASSERT_TRUE(ftp_url.SchemeIs(content::kFtpScheme));
1056 ui_test_utils::NavigateToURL(browser(), ftp_url);
1057 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_CREATE_SHORTCUTS));
1060 IN_PROC_BROWSER_TEST_F(BrowserTest, CommandCreateAppShortcutInvalid) {
1061 CommandUpdater* command_updater =
1062 browser()->command_controller()->command_updater();
1064 // Urls that should not have shortcuts.
1065 GURL new_tab_url(chrome::kChromeUINewTabURL);
1066 ui_test_utils::NavigateToURL(browser(), new_tab_url);
1067 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_CREATE_SHORTCUTS));
1069 GURL history_url(chrome::kChromeUIHistoryURL);
1070 ui_test_utils::NavigateToURL(browser(), history_url);
1071 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_CREATE_SHORTCUTS));
1073 GURL downloads_url(chrome::kChromeUIDownloadsURL);
1074 ui_test_utils::NavigateToURL(browser(), downloads_url);
1075 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_CREATE_SHORTCUTS));
1077 GURL blank_url(content::kAboutBlankURL);
1078 ui_test_utils::NavigateToURL(browser(), blank_url);
1079 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_CREATE_SHORTCUTS));
1082 // Change a tab into an application window.
1083 // DISABLED: http://crbug.com/72310
1084 IN_PROC_BROWSER_TEST_F(BrowserTest, DISABLED_ConvertTabToAppShortcut) {
1085 ASSERT_TRUE(test_server()->Start());
1086 GURL http_url(test_server()->GetURL(std::string()));
1087 ASSERT_TRUE(http_url.SchemeIs(content::kHttpScheme));
1089 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1090 WebContents* initial_tab = browser()->tab_strip_model()->GetWebContentsAt(0);
1091 WebContents* app_tab = chrome::AddSelectedTabWithURL(
1092 browser(), http_url, content::PAGE_TRANSITION_TYPED);
1093 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1094 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
1095 browser()->host_desktop_type()));
1097 // Normal tabs should accept load drops.
1098 EXPECT_TRUE(initial_tab->GetMutableRendererPrefs()->can_accept_load_drops);
1099 EXPECT_TRUE(app_tab->GetMutableRendererPrefs()->can_accept_load_drops);
1101 // Turn |app_tab| into a tab in an app panel.
1102 chrome::ConvertTabToAppWindow(browser(), app_tab);
1104 // The launch should have created a new browser.
1105 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1106 browser()->host_desktop_type()));
1108 // Find the new browser.
1109 Browser* app_browser = NULL;
1110 for (chrome::BrowserIterator it; !it.done() && !app_browser; it.Next()) {
1111 if (*it != browser())
1112 app_browser = *it;
1114 ASSERT_TRUE(app_browser);
1116 // Check that the tab contents is in the new browser, and not in the old.
1117 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1118 ASSERT_EQ(initial_tab, browser()->tab_strip_model()->GetWebContentsAt(0));
1120 // Check that the appliaction browser has a single tab, and that tab contains
1121 // the content that we app-ified.
1122 ASSERT_EQ(1, app_browser->tab_strip_model()->count());
1123 ASSERT_EQ(app_tab, app_browser->tab_strip_model()->GetWebContentsAt(0));
1125 // Normal tabs should accept load drops.
1126 EXPECT_TRUE(initial_tab->GetMutableRendererPrefs()->can_accept_load_drops);
1128 // The tab in an app window should not.
1129 EXPECT_FALSE(app_tab->GetMutableRendererPrefs()->can_accept_load_drops);
1132 #endif // !defined(OS_MACOSX)
1134 // Test RenderView correctly send back favicon url for web page that redirects
1135 // to an anchor in javascript body.onload handler.
1136 IN_PROC_BROWSER_TEST_F(BrowserTest,
1137 DISABLED_FaviconOfOnloadRedirectToAnchorPage) {
1138 ASSERT_TRUE(test_server()->Start());
1139 GURL url(test_server()->GetURL("files/onload_redirect_to_anchor.html"));
1140 GURL expected_favicon_url(test_server()->GetURL("files/test.png"));
1142 ui_test_utils::NavigateToURL(browser(), url);
1144 NavigationEntry* entry = browser()->tab_strip_model()->
1145 GetActiveWebContents()->GetController().GetActiveEntry();
1146 EXPECT_EQ(expected_favicon_url.spec(), entry->GetFavicon().url.spec());
1149 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined (OS_WIN)
1150 // http://crbug.com/83828. On Mac 10.6, the failure rate is 14%
1151 #define MAYBE_FaviconChange DISABLED_FaviconChange
1152 #else
1153 #define MAYBE_FaviconChange FaviconChange
1154 #endif
1155 // Test that an icon can be changed from JS.
1156 IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_FaviconChange) {
1157 static const base::FilePath::CharType* kFile =
1158 FILE_PATH_LITERAL("onload_change_favicon.html");
1159 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
1160 base::FilePath::kCurrentDirectory), base::FilePath(kFile)));
1161 ASSERT_TRUE(file_url.SchemeIs(content::kFileScheme));
1162 ui_test_utils::NavigateToURL(browser(), file_url);
1164 NavigationEntry* entry = browser()->tab_strip_model()->
1165 GetActiveWebContents()->GetController().GetActiveEntry();
1166 static const base::FilePath::CharType* kIcon =
1167 FILE_PATH_LITERAL("test1.png");
1168 GURL expected_favicon_url(ui_test_utils::GetTestUrl(base::FilePath(
1169 base::FilePath::kCurrentDirectory), base::FilePath(kIcon)));
1170 EXPECT_EQ(expected_favicon_url.spec(), entry->GetFavicon().url.spec());
1173 // http://crbug.com/172336
1174 #if defined(OS_WIN)
1175 #define MAYBE_TabClosingWhenRemovingExtension \
1176 DISABLED_TabClosingWhenRemovingExtension
1177 #else
1178 #define MAYBE_TabClosingWhenRemovingExtension TabClosingWhenRemovingExtension
1179 #endif
1180 // Makes sure TabClosing is sent when uninstalling an extension that is an app
1181 // tab.
1182 IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_TabClosingWhenRemovingExtension) {
1183 ASSERT_TRUE(test_server()->Start());
1184 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1185 GURL url(test_server()->GetURL("empty.html"));
1186 TabStripModel* model = browser()->tab_strip_model();
1188 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app/")));
1190 const Extension* extension_app = GetExtension();
1192 ui_test_utils::NavigateToURL(browser(), url);
1194 WebContents* app_contents = WebContents::Create(
1195 WebContents::CreateParams(browser()->profile()));
1196 extensions::TabHelper::CreateForWebContents(app_contents);
1197 extensions::TabHelper* extensions_tab_helper =
1198 extensions::TabHelper::FromWebContents(app_contents);
1199 extensions_tab_helper->SetExtensionApp(extension_app);
1201 model->AddWebContents(app_contents, 0, content::PageTransitionFromInt(0),
1202 TabStripModel::ADD_NONE);
1203 model->SetTabPinned(0, true);
1204 ui_test_utils::NavigateToURL(browser(), url);
1206 MockTabStripModelObserver observer;
1207 model->AddObserver(&observer);
1209 // Uninstall the extension and make sure TabClosing is sent.
1210 ExtensionService* service = extensions::ExtensionSystem::Get(
1211 browser()->profile())->extension_service();
1212 service->UninstallExtension(GetExtension()->id(), false, NULL);
1213 EXPECT_EQ(1, observer.closing_count());
1215 model->RemoveObserver(&observer);
1217 // There should only be one tab now.
1218 ASSERT_EQ(1, browser()->tab_strip_model()->count());
1221 #if !defined(OS_MACOSX)
1222 // Open with --app-id=<id>, and see that an app window opens.
1223 IN_PROC_BROWSER_TEST_F(BrowserTest, AppIdSwitch) {
1224 ASSERT_TRUE(test_server()->Start());
1226 // Load an app.
1227 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1228 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app/")));
1229 const Extension* extension_app = GetExtension();
1231 CommandLine command_line(CommandLine::NO_PROGRAM);
1232 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
1234 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
1235 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
1236 StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
1237 ASSERT_TRUE(launch.OpenApplicationWindow(browser()->profile(), NULL));
1239 // Check that the new browser has an app name.
1240 // The launch should have created a new browser.
1241 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1242 browser()->host_desktop_type()));
1244 // Find the new browser.
1245 Browser* new_browser = NULL;
1246 for (chrome::BrowserIterator it; !it.done() && !new_browser; it.Next()) {
1247 if (*it != browser())
1248 new_browser = *it;
1250 ASSERT_TRUE(new_browser);
1251 ASSERT_TRUE(new_browser != browser());
1253 // The browser's app_name should include the app's ID.
1254 ASSERT_NE(
1255 new_browser->app_name_.find(extension_app->id()),
1256 std::string::npos) << new_browser->app_name_;
1258 #endif
1260 // Tests that the CLD (Compact Language Detection) works properly.
1261 IN_PROC_BROWSER_TEST_F(BrowserTest, PageLanguageDetection) {
1262 ASSERT_TRUE(test_server()->Start());
1264 //std::string lang;
1265 LanguageDetectionDetails details;
1267 // Open a new tab with a page in English.
1268 AddTabAtIndex(0, GURL(test_server()->GetURL("files/english_page.html")),
1269 content::PAGE_TRANSITION_TYPED);
1271 WebContents* current_web_contents =
1272 browser()->tab_strip_model()->GetActiveWebContents();
1273 TranslateTabHelper* translate_tab_helper =
1274 TranslateTabHelper::FromWebContents(current_web_contents);
1275 content::Source<WebContents> source(current_web_contents);
1277 ui_test_utils::WindowedNotificationObserverWithDetails<
1278 LanguageDetectionDetails>
1279 en_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1280 source);
1281 EXPECT_EQ("", translate_tab_helper->GetLanguageState().original_language());
1282 en_language_detected_signal.Wait();
1283 EXPECT_TRUE(en_language_detected_signal.GetDetailsFor(
1284 source.map_key(), &details));
1285 EXPECT_EQ("en", details.adopted_language);
1286 EXPECT_EQ("en", translate_tab_helper->GetLanguageState().original_language());
1288 // Now navigate to a page in French.
1289 ui_test_utils::WindowedNotificationObserverWithDetails<
1290 LanguageDetectionDetails>
1291 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1292 source);
1293 ui_test_utils::NavigateToURL(
1294 browser(), GURL(test_server()->GetURL("files/french_page.html")));
1295 fr_language_detected_signal.Wait();
1296 details.adopted_language.clear();
1297 EXPECT_TRUE(fr_language_detected_signal.GetDetailsFor(
1298 source.map_key(), &details));
1299 EXPECT_EQ("fr", details.adopted_language);
1300 EXPECT_EQ("fr", translate_tab_helper->GetLanguageState().original_language());
1303 // Chromeos defaults to restoring the last session, so this test isn't
1304 // applicable.
1305 #if !defined(OS_CHROMEOS)
1306 #if defined(OS_MACOSX)
1307 // Crashy, http://crbug.com/38522
1308 #define RestorePinnedTabs DISABLED_RestorePinnedTabs
1309 #endif
1310 // Makes sure pinned tabs are restored correctly on start.
1311 IN_PROC_BROWSER_TEST_F(BrowserTest, RestorePinnedTabs) {
1312 ASSERT_TRUE(test_server()->Start());
1314 // Add an pinned app tab.
1315 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1316 GURL url(test_server()->GetURL("empty.html"));
1317 TabStripModel* model = browser()->tab_strip_model();
1318 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app/")));
1319 const Extension* extension_app = GetExtension();
1320 ui_test_utils::NavigateToURL(browser(), url);
1321 WebContents* app_contents = WebContents::Create(
1322 WebContents::CreateParams(browser()->profile()));
1323 extensions::TabHelper::CreateForWebContents(app_contents);
1324 extensions::TabHelper* extensions_tab_helper =
1325 extensions::TabHelper::FromWebContents(app_contents);
1326 extensions_tab_helper->SetExtensionApp(extension_app);
1327 model->AddWebContents(app_contents, 0, content::PageTransitionFromInt(0),
1328 TabStripModel::ADD_NONE);
1329 model->SetTabPinned(0, true);
1330 ui_test_utils::NavigateToURL(browser(), url);
1332 // Add a non pinned tab.
1333 chrome::NewTab(browser());
1335 // Add a pinned non-app tab.
1336 chrome::NewTab(browser());
1337 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
1338 model->SetTabPinned(2, true);
1340 // Write out the pinned tabs.
1341 PinnedTabCodec::WritePinnedTabs(browser()->profile());
1343 // Simulate launching again.
1344 CommandLine dummy(CommandLine::NO_PROGRAM);
1345 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
1346 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
1347 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
1348 launch.profile_ = browser()->profile();
1349 launch.ProcessStartupURLs(std::vector<GURL>(),
1350 browser()->host_desktop_type());
1352 // The launch should have created a new browser.
1353 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1354 browser()->host_desktop_type()));
1356 // Find the new browser.
1357 Browser* new_browser = NULL;
1358 for (chrome::BrowserIterator it; !it.done() && !new_browser; it.Next()) {
1359 if (*it != browser())
1360 new_browser = *it;
1362 ASSERT_TRUE(new_browser);
1363 ASSERT_TRUE(new_browser != browser());
1365 // We should get back an additional tab for the app, and another for the
1366 // default home page.
1367 ASSERT_EQ(3, new_browser->tab_strip_model()->count());
1369 // Make sure the state matches.
1370 TabStripModel* new_model = new_browser->tab_strip_model();
1371 EXPECT_TRUE(new_model->IsAppTab(0));
1372 EXPECT_FALSE(new_model->IsAppTab(1));
1373 EXPECT_FALSE(new_model->IsAppTab(2));
1375 EXPECT_TRUE(new_model->IsTabPinned(0));
1376 EXPECT_TRUE(new_model->IsTabPinned(1));
1377 EXPECT_FALSE(new_model->IsTabPinned(2));
1379 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
1380 new_model->GetWebContentsAt(2)->GetURL());
1382 EXPECT_TRUE(
1383 extensions::TabHelper::FromWebContents(
1384 new_model->GetWebContentsAt(0))->extension_app() == extension_app);
1386 #endif // !defined(OS_CHROMEOS)
1388 // This test verifies we don't crash when closing the last window and the app
1389 // menu is showing.
1390 // TODO(linux_aura) http://crbug.com/163931
1391 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
1392 #define MAYBE_CloseWithAppMenuOpen DISABLED_CloseWithAppMenuOpen
1393 #else
1394 #define MAYBE_CloseWithAppMenuOpen CloseWithAppMenuOpen
1395 #endif
1396 IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_CloseWithAppMenuOpen) {
1397 if (browser_defaults::kBrowserAliveWithNoWindows)
1398 return;
1400 // We need a message loop running for menus on windows.
1401 base::MessageLoop::current()->PostTask(
1402 FROM_HERE, base::Bind(&RunCloseWithAppMenuCallback, browser()));
1405 #if !defined(OS_MACOSX)
1406 IN_PROC_BROWSER_TEST_F(BrowserTest, OpenAppWindowLikeNtp) {
1407 ASSERT_TRUE(test_server()->Start());
1409 // Load an app
1410 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1411 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app/")));
1412 const Extension* extension_app = GetExtension();
1414 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
1415 WebContents* app_window = OpenApplication(
1416 AppLaunchParams(browser()->profile(), extension_app,
1417 extensions::LAUNCH_CONTAINER_WINDOW, NEW_WINDOW));
1418 ASSERT_TRUE(app_window);
1420 // Apps launched in a window from the NTP have an extensions tab helper but
1421 // do not have extension_app set in it.
1422 ASSERT_TRUE(extensions::TabHelper::FromWebContents(app_window));
1423 EXPECT_FALSE(
1424 extensions::TabHelper::FromWebContents(app_window)->extension_app());
1425 EXPECT_EQ(extensions::AppLaunchInfo::GetFullLaunchURL(extension_app),
1426 app_window->GetURL());
1428 // The launch should have created a new browser.
1429 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
1430 browser()->host_desktop_type()));
1432 // Find the new browser.
1433 Browser* new_browser = NULL;
1434 for (chrome::BrowserIterator it; !it.done() && !new_browser; it.Next()) {
1435 if (*it != browser())
1436 new_browser = *it;
1438 ASSERT_TRUE(new_browser);
1439 ASSERT_TRUE(new_browser != browser());
1441 EXPECT_TRUE(new_browser->is_app());
1443 // The browser's app name should include the extension's id.
1444 std::string app_name = new_browser->app_name_;
1445 EXPECT_NE(app_name.find(extension_app->id()), std::string::npos)
1446 << "Name " << app_name << " should contain id "<< extension_app->id();
1448 #endif // !defined(OS_MACOSX)
1450 // Makes sure the browser doesn't crash when
1451 // set_show_state(ui::SHOW_STATE_MAXIMIZED) has been invoked.
1452 IN_PROC_BROWSER_TEST_F(BrowserTest, StartMaximized) {
1453 Browser::Type types[] = { Browser::TYPE_TABBED, Browser::TYPE_POPUP };
1454 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(types); ++i) {
1455 Browser::CreateParams params(types[i], browser()->profile(),
1456 browser()->host_desktop_type());
1457 params.initial_show_state = ui::SHOW_STATE_MAXIMIZED;
1458 AddBlankTabAndShow(new Browser(params));
1462 // Aura doesn't support minimized window. crbug.com/104571.
1463 #if defined(USE_AURA)
1464 #define MAYBE_StartMinimized DISABLED_StartMinimized
1465 #else
1466 #define MAYBE_StartMinimized StartMinimized
1467 #endif
1468 // Makes sure the browser doesn't crash when
1469 // set_show_state(ui::SHOW_STATE_MINIMIZED) has been invoked.
1470 IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_StartMinimized) {
1471 Browser::Type types[] = { Browser::TYPE_TABBED, Browser::TYPE_POPUP };
1472 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(types); ++i) {
1473 Browser::CreateParams params(types[i], browser()->profile(),
1474 browser()->host_desktop_type());
1475 params.initial_show_state = ui::SHOW_STATE_MINIMIZED;
1476 AddBlankTabAndShow(new Browser(params));
1480 // Makes sure the forward button is disabled immediately when navigating
1481 // forward to a slow-to-commit page.
1482 IN_PROC_BROWSER_TEST_F(BrowserTest, ForwardDisabledOnForward) {
1483 GURL blank_url(content::kAboutBlankURL);
1484 ui_test_utils::NavigateToURL(browser(), blank_url);
1486 ui_test_utils::NavigateToURL(
1487 browser(), ui_test_utils::GetTestUrl(
1488 base::FilePath(base::FilePath::kCurrentDirectory),
1489 base::FilePath(kTitle1File)));
1491 content::WindowedNotificationObserver back_nav_load_observer(
1492 content::NOTIFICATION_LOAD_STOP,
1493 content::Source<NavigationController>(
1494 &browser()->tab_strip_model()->GetActiveWebContents()->
1495 GetController()));
1496 chrome::GoBack(browser(), CURRENT_TAB);
1497 back_nav_load_observer.Wait();
1498 CommandUpdater* command_updater =
1499 browser()->command_controller()->command_updater();
1500 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_FORWARD));
1502 content::WindowedNotificationObserver forward_nav_load_observer(
1503 content::NOTIFICATION_LOAD_STOP,
1504 content::Source<NavigationController>(
1505 &browser()->tab_strip_model()->GetActiveWebContents()->
1506 GetController()));
1507 chrome::GoForward(browser(), CURRENT_TAB);
1508 // This check will happen before the navigation completes, since the browser
1509 // won't process the renderer's response until the Wait() call below.
1510 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_FORWARD));
1511 forward_nav_load_observer.Wait();
1514 // Makes sure certain commands are disabled when Incognito mode is forced.
1515 IN_PROC_BROWSER_TEST_F(BrowserTest, DisableMenuItemsWhenIncognitoIsForced) {
1516 CommandUpdater* command_updater =
1517 browser()->command_controller()->command_updater();
1518 // At the beginning, all commands are enabled.
1519 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_NEW_WINDOW));
1520 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW));
1521 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER));
1522 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1523 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_MANAGE_EXTENSIONS));
1524 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_OPTIONS));
1526 // Set Incognito to FORCED.
1527 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1528 IncognitoModePrefs::FORCED);
1529 // Bookmarks & Settings commands should get disabled.
1530 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_NEW_WINDOW));
1531 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER));
1532 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1533 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_MANAGE_EXTENSIONS));
1534 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_OPTIONS));
1535 // New Incognito Window command, however, should be enabled.
1536 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW));
1538 // Create a new browser.
1539 Browser* new_browser =
1540 new Browser(Browser::CreateParams(
1541 browser()->profile()->GetOffTheRecordProfile(),
1542 browser()->host_desktop_type()));
1543 CommandUpdater* new_command_updater =
1544 new_browser->command_controller()->command_updater();
1545 // It should have Bookmarks & Settings commands disabled by default.
1546 EXPECT_FALSE(new_command_updater->IsCommandEnabled(IDC_NEW_WINDOW));
1547 EXPECT_FALSE(new_command_updater->IsCommandEnabled(
1548 IDC_SHOW_BOOKMARK_MANAGER));
1549 EXPECT_FALSE(new_command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1550 EXPECT_FALSE(new_command_updater->IsCommandEnabled(IDC_MANAGE_EXTENSIONS));
1551 EXPECT_FALSE(new_command_updater->IsCommandEnabled(IDC_OPTIONS));
1552 EXPECT_TRUE(new_command_updater->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW));
1555 // Makes sure New Incognito Window command is disabled when Incognito mode is
1556 // not available.
1557 IN_PROC_BROWSER_TEST_F(BrowserTest,
1558 NoNewIncognitoWindowWhenIncognitoIsDisabled) {
1559 CommandUpdater* command_updater =
1560 browser()->command_controller()->command_updater();
1561 // Set Incognito to DISABLED.
1562 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1563 IncognitoModePrefs::DISABLED);
1564 // Make sure New Incognito Window command is disabled. All remaining commands
1565 // should be enabled.
1566 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW));
1567 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_NEW_WINDOW));
1568 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER));
1569 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1570 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_MANAGE_EXTENSIONS));
1571 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_OPTIONS));
1573 // Create a new browser.
1574 Browser* new_browser =
1575 new Browser(Browser::CreateParams(browser()->profile(),
1576 browser()->host_desktop_type()));
1577 CommandUpdater* new_command_updater =
1578 new_browser->command_controller()->command_updater();
1579 EXPECT_FALSE(new_command_updater->IsCommandEnabled(IDC_NEW_INCOGNITO_WINDOW));
1580 EXPECT_TRUE(new_command_updater->IsCommandEnabled(IDC_NEW_WINDOW));
1581 EXPECT_TRUE(new_command_updater->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER));
1582 EXPECT_TRUE(new_command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1583 EXPECT_TRUE(new_command_updater->IsCommandEnabled(IDC_MANAGE_EXTENSIONS));
1584 EXPECT_TRUE(new_command_updater->IsCommandEnabled(IDC_OPTIONS));
1587 // Makes sure Extensions and Settings commands are disabled in certain
1588 // circumstances even though normally they should stay enabled.
1589 IN_PROC_BROWSER_TEST_F(BrowserTest,
1590 DisableExtensionsAndSettingsWhenIncognitoIsDisabled) {
1591 CommandUpdater* command_updater =
1592 browser()->command_controller()->command_updater();
1593 // Disable extensions. This should disable Extensions menu.
1594 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
1595 set_extensions_enabled(false);
1596 // Set Incognito to DISABLED.
1597 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
1598 IncognitoModePrefs::DISABLED);
1599 // Make sure Manage Extensions command is disabled.
1600 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_MANAGE_EXTENSIONS));
1601 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_NEW_WINDOW));
1602 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER));
1603 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1604 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_OPTIONS));
1606 // Create a popup (non-main-UI-type) browser. Settings command as well
1607 // as Extensions should be disabled.
1608 Browser* popup_browser = new Browser(
1609 Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(),
1610 browser()->host_desktop_type()));
1611 CommandUpdater* popup_command_updater =
1612 popup_browser->command_controller()->command_updater();
1613 EXPECT_FALSE(popup_command_updater->IsCommandEnabled(IDC_MANAGE_EXTENSIONS));
1614 EXPECT_FALSE(popup_command_updater->IsCommandEnabled(IDC_OPTIONS));
1615 EXPECT_TRUE(popup_command_updater->IsCommandEnabled(
1616 IDC_SHOW_BOOKMARK_MANAGER));
1617 EXPECT_FALSE(popup_command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1620 // Makes sure Extensions and Settings commands are disabled in certain
1621 // circumstances even though normally they should stay enabled.
1622 IN_PROC_BROWSER_TEST_F(BrowserTest,
1623 DisableOptionsAndImportMenuItemsConsistently) {
1624 // Create a popup browser.
1625 Browser* popup_browser = new Browser(
1626 Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(),
1627 browser()->host_desktop_type()));
1628 CommandUpdater* command_updater =
1629 popup_browser->command_controller()->command_updater();
1630 // OPTIONS and IMPORT_SETTINGS are disabled for a non-normal UI.
1631 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_OPTIONS));
1632 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1634 // Set Incognito to FORCED.
1635 IncognitoModePrefs::SetAvailability(popup_browser->profile()->GetPrefs(),
1636 IncognitoModePrefs::FORCED);
1637 // OPTIONS and IMPORT_SETTINGS are disabled when Incognito is forced.
1638 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_OPTIONS));
1639 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1640 // Set Incognito to AVAILABLE.
1641 IncognitoModePrefs::SetAvailability(popup_browser->profile()->GetPrefs(),
1642 IncognitoModePrefs::ENABLED);
1643 // OPTIONS and IMPORT_SETTINGS are still disabled since it is a non-normal UI.
1644 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_OPTIONS));
1645 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_IMPORT_SETTINGS));
1648 namespace {
1650 void OnZoomLevelChanged(const base::Closure& callback,
1651 const HostZoomMap::ZoomLevelChange& host) {
1652 callback.Run();
1655 } // namespace
1657 #if defined(OS_WIN)
1658 // Flakes regularly on Windows XP
1659 // http://crbug.com/146040
1660 #define MAYBE_PageZoom DISABLED_PageZoom
1661 #else
1662 #define MAYBE_PageZoom PageZoom
1663 #endif
1664 IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_PageZoom) {
1665 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
1666 bool enable_plus, enable_minus;
1669 scoped_refptr<content::MessageLoopRunner> loop_runner(
1670 new content::MessageLoopRunner);
1671 content::HostZoomMap::ZoomLevelChangedCallback callback(
1672 base::Bind(&OnZoomLevelChanged, loop_runner->QuitClosure()));
1673 scoped_ptr<content::HostZoomMap::Subscription> sub =
1674 content::HostZoomMap::GetForBrowserContext(
1675 browser()->profile())->AddZoomLevelChangedCallback(callback);
1676 chrome::Zoom(browser(), content::PAGE_ZOOM_IN);
1677 loop_runner->Run();
1678 sub.reset();
1679 EXPECT_EQ(contents->GetZoomPercent(&enable_plus, &enable_minus), 110);
1680 EXPECT_TRUE(enable_plus);
1681 EXPECT_TRUE(enable_minus);
1685 scoped_refptr<content::MessageLoopRunner> loop_runner(
1686 new content::MessageLoopRunner);
1687 content::HostZoomMap::ZoomLevelChangedCallback callback(
1688 base::Bind(&OnZoomLevelChanged, loop_runner->QuitClosure()));
1689 scoped_ptr<content::HostZoomMap::Subscription> sub =
1690 content::HostZoomMap::GetForBrowserContext(
1691 browser()->profile())->AddZoomLevelChangedCallback(callback);
1692 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET);
1693 loop_runner->Run();
1694 sub.reset();
1695 EXPECT_EQ(contents->GetZoomPercent(&enable_plus, &enable_minus), 100);
1696 EXPECT_TRUE(enable_plus);
1697 EXPECT_TRUE(enable_minus);
1701 scoped_refptr<content::MessageLoopRunner> loop_runner(
1702 new content::MessageLoopRunner);
1703 content::HostZoomMap::ZoomLevelChangedCallback callback(
1704 base::Bind(&OnZoomLevelChanged, loop_runner->QuitClosure()));
1705 scoped_ptr<content::HostZoomMap::Subscription> sub =
1706 content::HostZoomMap::GetForBrowserContext(
1707 browser()->profile())->AddZoomLevelChangedCallback(callback);
1708 chrome::Zoom(browser(), content::PAGE_ZOOM_OUT);
1709 loop_runner->Run();
1710 sub.reset();
1711 EXPECT_EQ(contents->GetZoomPercent(&enable_plus, &enable_minus), 90);
1712 EXPECT_TRUE(enable_plus);
1713 EXPECT_TRUE(enable_minus);
1716 chrome::Zoom(browser(), content::PAGE_ZOOM_RESET);
1719 IN_PROC_BROWSER_TEST_F(BrowserTest, InterstitialCommandDisable) {
1720 ASSERT_TRUE(test_server()->Start());
1721 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1722 GURL url(test_server()->GetURL("empty.html"));
1723 ui_test_utils::NavigateToURL(browser(), url);
1725 CommandUpdater* command_updater =
1726 browser()->command_controller()->command_updater();
1727 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_VIEW_SOURCE));
1728 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_PRINT));
1729 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SAVE_PAGE));
1730 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_ENCODING_MENU));
1732 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
1734 TestInterstitialPage* interstitial = NULL;
1736 scoped_refptr<content::MessageLoopRunner> loop_runner(
1737 new content::MessageLoopRunner);
1739 InterstitialObserver observer(contents,
1740 loop_runner->QuitClosure(),
1741 base::Closure());
1742 interstitial = new TestInterstitialPage(contents, false, GURL());
1743 loop_runner->Run();
1746 EXPECT_TRUE(contents->ShowingInterstitialPage());
1748 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_VIEW_SOURCE));
1749 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_PRINT));
1750 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_SAVE_PAGE));
1751 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_ENCODING_MENU));
1754 scoped_refptr<content::MessageLoopRunner> loop_runner(
1755 new content::MessageLoopRunner);
1757 InterstitialObserver observer(contents,
1758 base::Closure(),
1759 loop_runner->QuitClosure());
1760 interstitial->Proceed();
1761 loop_runner->Run();
1762 // interstitial is deleted now.
1765 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_VIEW_SOURCE));
1766 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_PRINT));
1767 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SAVE_PAGE));
1768 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_ENCODING_MENU));
1771 // Ensure that creating an interstitial page closes any JavaScript dialogs
1772 // that were present on the previous page. See http://crbug.com/295695.
1773 IN_PROC_BROWSER_TEST_F(BrowserTest, InterstitialClosesDialogs) {
1774 ASSERT_TRUE(test_server()->Start());
1775 host_resolver()->AddRule("www.example.com", "127.0.0.1");
1776 GURL url(test_server()->GetURL("empty.html"));
1777 ui_test_utils::NavigateToURL(browser(), url);
1779 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
1780 contents->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
1781 base::string16(),
1782 ASCIIToUTF16("alert('Dialog showing!');"));
1783 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
1784 EXPECT_TRUE(alert->IsValid());
1785 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance();
1786 EXPECT_TRUE(dialog_queue->HasActiveDialog());
1788 TestInterstitialPage* interstitial = NULL;
1790 scoped_refptr<content::MessageLoopRunner> loop_runner(
1791 new content::MessageLoopRunner);
1793 InterstitialObserver observer(contents,
1794 loop_runner->QuitClosure(),
1795 base::Closure());
1796 interstitial = new TestInterstitialPage(contents, false, GURL());
1797 loop_runner->Run();
1800 // The interstitial should have closed the dialog.
1801 EXPECT_TRUE(contents->ShowingInterstitialPage());
1802 EXPECT_FALSE(dialog_queue->HasActiveDialog());
1805 scoped_refptr<content::MessageLoopRunner> loop_runner(
1806 new content::MessageLoopRunner);
1808 InterstitialObserver observer(contents,
1809 base::Closure(),
1810 loop_runner->QuitClosure());
1811 interstitial->DontProceed();
1812 loop_runner->Run();
1813 // interstitial is deleted now.
1816 // Make sure input events still work in the renderer process.
1817 EXPECT_FALSE(contents->GetRenderProcessHost()->IgnoreInputEvents());
1821 IN_PROC_BROWSER_TEST_F(BrowserTest, InterstitialCloseTab) {
1822 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
1825 scoped_refptr<content::MessageLoopRunner> loop_runner(
1826 new content::MessageLoopRunner);
1828 InterstitialObserver observer(contents,
1829 loop_runner->QuitClosure(),
1830 base::Closure());
1831 // Interstitial will delete itself when we close the tab.
1832 new TestInterstitialPage(contents, false, GURL());
1833 loop_runner->Run();
1836 EXPECT_TRUE(contents->ShowingInterstitialPage());
1839 scoped_refptr<content::MessageLoopRunner> loop_runner(
1840 new content::MessageLoopRunner);
1842 InterstitialObserver observer(contents,
1843 base::Closure(),
1844 loop_runner->QuitClosure());
1845 chrome::CloseTab(browser());
1846 loop_runner->Run();
1847 // interstitial is deleted now.
1851 class MockWebContentsObserver : public WebContentsObserver {
1852 public:
1853 explicit MockWebContentsObserver(WebContents* web_contents)
1854 : WebContentsObserver(web_contents),
1855 got_user_gesture_(false) {
1858 virtual void DidGetUserGesture() OVERRIDE {
1859 got_user_gesture_ = true;
1862 bool got_user_gesture() const {
1863 return got_user_gesture_;
1866 void set_got_user_gesture(bool got_it) {
1867 got_user_gesture_ = got_it;
1870 private:
1871 bool got_user_gesture_;
1873 DISALLOW_COPY_AND_ASSIGN(MockWebContentsObserver);
1876 IN_PROC_BROWSER_TEST_F(BrowserTest, UserGesturesReported) {
1877 // Regression test for http://crbug.com/110707. Also tests that a user
1878 // gesture is sent when a normal navigation (via e.g. the omnibox) is
1879 // performed.
1880 WebContents* web_contents =
1881 browser()->tab_strip_model()->GetActiveWebContents();
1882 MockWebContentsObserver mock_observer(web_contents);
1884 ASSERT_TRUE(test_server()->Start());
1885 GURL url(test_server()->GetURL("empty.html"));
1887 ui_test_utils::NavigateToURL(browser(), url);
1888 EXPECT_TRUE(mock_observer.got_user_gesture());
1890 mock_observer.set_got_user_gesture(false);
1891 chrome::Reload(browser(), CURRENT_TAB);
1892 EXPECT_TRUE(mock_observer.got_user_gesture());
1895 // TODO(ben): this test was never enabled. It has bit-rotted since being added.
1896 // It originally lived in browser_unittest.cc, but has been moved here to make
1897 // room for real browser unit tests.
1898 #if 0
1899 class BrowserTest2 : public InProcessBrowserTest {
1900 public:
1901 BrowserTest2() {
1902 host_resolver_proc_ = new net::RuleBasedHostResolverProc(NULL);
1903 // Avoid making external DNS lookups. In this test we don't need this
1904 // to succeed.
1905 host_resolver_proc_->AddSimulatedFailure("*.google.com");
1906 scoped_host_resolver_proc_.Init(host_resolver_proc_.get());
1909 private:
1910 scoped_refptr<net::RuleBasedHostResolverProc> host_resolver_proc_;
1911 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_;
1914 IN_PROC_BROWSER_TEST_F(BrowserTest2, NoTabsInPopups) {
1915 chrome::RegisterAppPrefs(L"Test");
1917 // We start with a normal browser with one tab.
1918 EXPECT_EQ(1, browser()->tab_strip_model()->count());
1920 // Open a popup browser with a single blank foreground tab.
1921 Browser* popup_browser = new Browser(
1922 Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile()));
1923 chrome::AddTabAt(popup_browser, GURL(), -1, true);
1924 EXPECT_EQ(1, popup_browser->tab_strip_model()->count());
1926 // Now try opening another tab in the popup browser.
1927 AddTabWithURLParams params1(url, content::PAGE_TRANSITION_TYPED);
1928 popup_browser->AddTabWithURL(&params1);
1929 EXPECT_EQ(popup_browser, params1.target);
1931 // The popup should still only have one tab.
1932 EXPECT_EQ(1, popup_browser->tab_strip_model()->count());
1934 // The normal browser should now have two.
1935 EXPECT_EQ(2, browser()->tab_strip_model()->count());
1937 // Open an app frame browser with a single blank foreground tab.
1938 Browser* app_browser = new Browser(Browser::CreateParams::CreateForApp(
1939 L"Test", browser()->profile(), false));
1940 chrome::AddTabAt(app_browser, GURL(), -1, true);
1941 EXPECT_EQ(1, app_browser->tab_strip_model()->count());
1943 // Now try opening another tab in the app browser.
1944 AddTabWithURLParams params2(GURL(content::kAboutBlankURL),
1945 content::PAGE_TRANSITION_TYPED);
1946 app_browser->AddTabWithURL(&params2);
1947 EXPECT_EQ(app_browser, params2.target);
1949 // The popup should still only have one tab.
1950 EXPECT_EQ(1, app_browser->tab_strip_model()->count());
1952 // The normal browser should now have three.
1953 EXPECT_EQ(3, browser()->tab_strip_model()->count());
1955 // Open an app frame popup browser with a single blank foreground tab.
1956 Browser* app_popup_browser = new Browser(Browser::CreateParams::CreateForApp(
1957 L"Test", browser()->profile(), false));
1958 chrome::AddTabAt(app_popup_browser, GURL(), -1, true);
1959 EXPECT_EQ(1, app_popup_browser->tab_strip_model()->count());
1961 // Now try opening another tab in the app popup browser.
1962 AddTabWithURLParams params3(GURL(content::kAboutBlankURL),
1963 content::PAGE_TRANSITION_TYPED);
1964 app_popup_browser->AddTabWithURL(&params3);
1965 EXPECT_EQ(app_popup_browser, params3.target);
1967 // The popup should still only have one tab.
1968 EXPECT_EQ(1, app_popup_browser->tab_strip_model()->count());
1970 // The normal browser should now have four.
1971 EXPECT_EQ(4, browser()->tab_strip_model()->count());
1973 // Close the additional browsers.
1974 popup_browser->tab_strip_model()->CloseAllTabs();
1975 app_browser->tab_strip_model()->CloseAllTabs();
1976 app_popup_browser->tab_strip_model()->CloseAllTabs();
1978 #endif
1980 IN_PROC_BROWSER_TEST_F(BrowserTest, WindowOpenClose) {
1981 CommandLine::ForCurrentProcess()->AppendSwitch(
1982 switches::kDisablePopupBlocking);
1983 GURL url = ui_test_utils::GetTestUrl(
1984 base::FilePath(), base::FilePath().AppendASCII("window.close.html"));
1986 base::string16 title = ASCIIToUTF16("Title Of Awesomeness");
1987 content::TitleWatcher title_watcher(
1988 browser()->tab_strip_model()->GetActiveWebContents(), title);
1989 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url, 2);
1990 EXPECT_EQ(title, title_watcher.WaitAndGetTitle());
1993 // GTK doesn't use the Browser's fullscreen state.
1994 // TODO(linux_aura) http://crbug.com/163931
1995 // Mac disabled: http://crbug.com/169820
1996 #if !defined(TOOLKIT_GTK) && !defined(OS_MACOSX) && \
1997 !(defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
1998 IN_PROC_BROWSER_TEST_F(BrowserTest, FullscreenBookmarkBar) {
1999 #if defined(OS_WIN) && defined(USE_ASH)
2000 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2001 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
2002 return;
2003 #endif
2005 chrome::ToggleBookmarkBar(browser());
2006 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
2007 chrome::ToggleFullscreenMode(browser());
2008 EXPECT_TRUE(browser()->window()->IsFullscreen());
2009 #if defined(OS_MACOSX)
2010 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
2011 #elif defined(OS_CHROMEOS)
2012 // TODO(jamescook): If immersive fullscreen is disabled by default, test
2013 // for BookmarkBar::HIDDEN.
2014 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
2015 #else
2016 EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
2017 #endif
2019 #endif
2021 class ShowModalDialogTest : public BrowserTest {
2022 public:
2023 ShowModalDialogTest() {}
2025 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
2026 command_line->AppendSwitch(switches::kDisablePopupBlocking);
2030 IN_PROC_BROWSER_TEST_F(ShowModalDialogTest, BasicTest) {
2031 // This navigation should show a modal dialog that will be immediately
2032 // closed, but the fact that it was shown should be recorded.
2033 GURL url = ui_test_utils::GetTestUrl(
2034 base::FilePath(), base::FilePath().AppendASCII("showmodaldialog.html"));
2036 base::string16 expected_title(ASCIIToUTF16("SUCCESS"));
2037 content::TitleWatcher title_watcher(
2038 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
2039 ui_test_utils::NavigateToURL(browser(), url);
2041 // Verify that we set a mark on successful dialog show.
2042 ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
2045 IN_PROC_BROWSER_TEST_F(BrowserTest, DisallowFileUrlUniversalAccessTest) {
2046 GURL url = ui_test_utils::GetTestUrl(
2047 base::FilePath(),
2048 base::FilePath().AppendASCII("fileurl_universalaccess.html"));
2050 base::string16 expected_title(ASCIIToUTF16("Disallowed"));
2051 content::TitleWatcher title_watcher(
2052 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
2053 title_watcher.AlsoWaitForTitle(ASCIIToUTF16("Allowed"));
2054 ui_test_utils::NavigateToURL(browser(), url);
2055 ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
2058 class KioskModeTest : public BrowserTest {
2059 public:
2060 KioskModeTest() {}
2062 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
2063 command_line->AppendSwitch(switches::kKioskMode);
2067 #if defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
2068 // http://crbug.com/103912
2069 // TODO(linux_aura) http://crbug.com/163931
2070 #define MAYBE_EnableKioskModeTest DISABLED_EnableKioskModeTest
2071 #else
2072 #define MAYBE_EnableKioskModeTest EnableKioskModeTest
2073 #endif
2074 IN_PROC_BROWSER_TEST_F(KioskModeTest, MAYBE_EnableKioskModeTest) {
2075 // Check if browser is in fullscreen mode.
2076 ASSERT_TRUE(browser()->window()->IsFullscreen());
2077 ASSERT_FALSE(browser()->window()->IsFullscreenBubbleVisible());
2080 #if defined(OS_WIN)
2081 // This test verifies that Chrome can be launched with a user-data-dir path
2082 // which contains non ASCII characters.
2083 class LaunchBrowserWithNonAsciiUserDatadir : public BrowserTest {
2084 public:
2085 LaunchBrowserWithNonAsciiUserDatadir() {}
2087 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
2088 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
2089 base::FilePath tmp_profile = temp_dir_.path().AppendASCII("tmp_profile");
2090 tmp_profile = tmp_profile.Append(L"Test Chrome G\u00E9raldine");
2092 ASSERT_TRUE(base::CreateDirectory(tmp_profile));
2093 command_line->AppendSwitchPath(switches::kUserDataDir, tmp_profile);
2096 base::ScopedTempDir temp_dir_;
2099 IN_PROC_BROWSER_TEST_F(LaunchBrowserWithNonAsciiUserDatadir,
2100 TestNonAsciiUserDataDir) {
2101 // Verify that the window is present.
2102 ASSERT_TRUE(browser());
2104 #endif // defined(OS_WIN)
2106 // Tests to ensure that the browser continues running in the background after
2107 // the last window closes.
2108 class RunInBackgroundTest : public BrowserTest {
2109 public:
2110 RunInBackgroundTest() {}
2112 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
2113 command_line->AppendSwitch(switches::kKeepAliveForTest);
2117 IN_PROC_BROWSER_TEST_F(RunInBackgroundTest, RunInBackgroundBasicTest) {
2118 // Close the browser window, then open a new one - the browser should keep
2119 // running.
2120 Profile* profile = browser()->profile();
2121 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2122 content::WindowedNotificationObserver observer(
2123 chrome::NOTIFICATION_BROWSER_CLOSED,
2124 content::Source<Browser>(browser()));
2125 chrome::CloseWindow(browser());
2126 observer.Wait();
2127 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2129 ui_test_utils::BrowserAddedObserver browser_added_observer;
2130 chrome::NewEmptyWindow(profile, chrome::GetActiveDesktop());
2131 browser_added_observer.WaitForSingleNewBrowser();
2133 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2136 // Tests to ensure that the browser continues running in the background after
2137 // the last window closes.
2138 class NoStartupWindowTest : public BrowserTest {
2139 public:
2140 NoStartupWindowTest() {}
2142 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
2143 command_line->AppendSwitch(switches::kNoStartupWindow);
2144 command_line->AppendSwitch(switches::kKeepAliveForTest);
2148 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest, NoStartupWindowBasicTest) {
2149 #if defined(OS_WIN) && defined(USE_ASH)
2150 // kNoStartupWindow doesn't make sense in Metro+Ash.
2151 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
2152 return;
2153 #endif
2155 // No browser window should be started by default.
2156 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
2158 // Starting a browser window should work just fine.
2159 ui_test_utils::BrowserAddedObserver browser_added_observer;
2160 CreateBrowser(ProfileManager::GetActiveUserProfile());
2161 browser_added_observer.WaitForSingleNewBrowser();
2163 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2166 // Chromeos needs to track app windows because it considers them to be part of
2167 // session state.
2168 #if !defined(OS_CHROMEOS)
2169 IN_PROC_BROWSER_TEST_F(NoStartupWindowTest, DontInitSessionServiceForApps) {
2170 #if defined(OS_WIN) && defined(USE_ASH)
2171 // kNoStartupWindow doesn't make sense in Metro+Ash.
2172 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
2173 return;
2174 #endif
2176 Profile* profile = ProfileManager::GetActiveUserProfile();
2178 SessionService* session_service =
2179 SessionServiceFactory::GetForProfile(profile);
2180 ASSERT_FALSE(session_service->processed_any_commands());
2182 ui_test_utils::BrowserAddedObserver browser_added_observer;
2183 CreateBrowserForApp("blah", profile);
2184 browser_added_observer.WaitForSingleNewBrowser();
2186 ASSERT_FALSE(session_service->processed_any_commands());
2188 #endif // !defined(OS_CHROMEOS)
2190 // This test needs to be placed outside the anonymous namespace because we
2191 // need to access private type of Browser.
2192 class AppModeTest : public BrowserTest {
2193 public:
2194 AppModeTest() {}
2196 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
2197 GURL url = ui_test_utils::GetTestUrl(
2198 base::FilePath(), base::FilePath().AppendASCII("title1.html"));
2199 command_line->AppendSwitchASCII(switches::kApp, url.spec());
2203 IN_PROC_BROWSER_TEST_F(AppModeTest, EnableAppModeTest) {
2204 #if defined(OS_WIN) && defined(USE_ASH)
2205 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
2206 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
2207 return;
2208 #endif
2210 // Test that an application browser window loads correctly.
2212 // Verify the browser is in application mode.
2213 EXPECT_TRUE(browser()->is_app());
2216 // Confirm chrome://version contains some expected content.
2217 IN_PROC_BROWSER_TEST_F(BrowserTest, AboutVersion) {
2218 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL));
2219 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
2220 ASSERT_GT(ui_test_utils::FindInPage(tab, ASCIIToUTF16("WebKit"), true, true,
2221 NULL, NULL),
2223 ASSERT_GT(ui_test_utils::FindInPage(tab, ASCIIToUTF16("OS"), true, true,
2224 NULL, NULL),
2226 ASSERT_GT(ui_test_utils::FindInPage(tab, ASCIIToUTF16("JavaScript"), true,
2227 true, NULL, NULL),
2231 static const base::FilePath::CharType* kTestDir =
2232 FILE_PATH_LITERAL("click_modifier");
2233 static const char kFirstPageTitle[] = "First window";
2234 static const char kSecondPageTitle[] = "New window!";
2236 class ClickModifierTest : public InProcessBrowserTest {
2237 public:
2238 ClickModifierTest() {
2241 // Returns a url that opens a new window or tab when clicked, via javascript.
2242 GURL GetWindowOpenURL() {
2243 return ui_test_utils::GetTestUrl(
2244 base::FilePath(kTestDir),
2245 base::FilePath(FILE_PATH_LITERAL("window_open.html")));
2248 // Returns a url that follows a simple link when clicked, unless affected by
2249 // modifiers.
2250 GURL GetHrefURL() {
2251 return ui_test_utils::GetTestUrl(
2252 base::FilePath(kTestDir),
2253 base::FilePath(FILE_PATH_LITERAL("href.html")));
2256 base::string16 getFirstPageTitle() {
2257 return ASCIIToUTF16(kFirstPageTitle);
2260 base::string16 getSecondPageTitle() {
2261 return ASCIIToUTF16(kSecondPageTitle);
2264 // Loads our test page and simulates a single click using the supplied button
2265 // and modifiers. The click will cause either a navigation or the creation of
2266 // a new window or foreground or background tab. We verify that the expected
2267 // disposition occurs.
2268 void RunTest(Browser* browser,
2269 const GURL& url,
2270 int modifiers,
2271 blink::WebMouseEvent::Button button,
2272 WindowOpenDisposition disposition) {
2273 ui_test_utils::NavigateToURL(browser, url);
2274 EXPECT_EQ(1u, chrome::GetBrowserCount(browser->profile(),
2275 browser->host_desktop_type()));
2276 EXPECT_EQ(1, browser->tab_strip_model()->count());
2277 content::WebContents* web_contents =
2278 browser->tab_strip_model()->GetActiveWebContents();
2279 EXPECT_EQ(url, web_contents->GetURL());
2281 if (disposition == CURRENT_TAB) {
2282 content::WebContents* web_contents =
2283 browser->tab_strip_model()->GetActiveWebContents();
2284 content::TestNavigationObserver same_tab_observer(web_contents);
2285 SimulateMouseClick(web_contents, modifiers, button);
2286 same_tab_observer.Wait();
2287 EXPECT_EQ(1u, chrome::GetBrowserCount(browser->profile(),
2288 browser->host_desktop_type()));
2289 EXPECT_EQ(1, browser->tab_strip_model()->count());
2290 EXPECT_EQ(getSecondPageTitle(), web_contents->GetTitle());
2291 return;
2294 content::WindowedNotificationObserver observer(
2295 chrome::NOTIFICATION_TAB_ADDED,
2296 content::NotificationService::AllSources());
2297 SimulateMouseClick(web_contents, modifiers, button);
2298 observer.Wait();
2300 if (disposition == NEW_WINDOW) {
2301 EXPECT_EQ(2u, chrome::GetBrowserCount(browser->profile(),
2302 browser->host_desktop_type()));
2303 return;
2306 EXPECT_EQ(1u, chrome::GetBrowserCount(browser->profile(),
2307 browser->host_desktop_type()));
2308 EXPECT_EQ(2, browser->tab_strip_model()->count());
2309 web_contents = browser->tab_strip_model()->GetActiveWebContents();
2310 WaitForLoadStop(web_contents);
2311 if (disposition == NEW_FOREGROUND_TAB) {
2312 EXPECT_EQ(getSecondPageTitle(), web_contents->GetTitle());
2313 } else {
2314 ASSERT_EQ(NEW_BACKGROUND_TAB, disposition);
2315 EXPECT_EQ(getFirstPageTitle(), web_contents->GetTitle());
2319 private:
2320 DISALLOW_COPY_AND_ASSIGN(ClickModifierTest);
2323 // Tests for clicking on elements with handlers that run window.open.
2325 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenBasicClickTest) {
2326 int modifiers = 0;
2327 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonLeft;
2328 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
2329 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
2332 // TODO(ericu): Alt-click behavior on window.open is platform-dependent and not
2333 // well defined. Should we add tests so we know if it changes?
2335 // Shift-clicks open in a new window.
2336 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenShiftClickTest) {
2337 int modifiers = blink::WebInputEvent::ShiftKey;
2338 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonLeft;
2339 WindowOpenDisposition disposition = NEW_WINDOW;
2340 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
2343 // Control-clicks open in a background tab.
2344 // On OSX meta [the command key] takes the place of control.
2345 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenControlClickTest) {
2346 #if defined(OS_MACOSX)
2347 int modifiers = blink::WebInputEvent::MetaKey;
2348 #else
2349 int modifiers = blink::WebInputEvent::ControlKey;
2350 #endif
2351 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonLeft;
2352 WindowOpenDisposition disposition = NEW_BACKGROUND_TAB;
2353 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
2356 // Control-shift-clicks open in a foreground tab.
2357 // On OSX meta [the command key] takes the place of control.
2358 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenControlShiftClickTest) {
2359 #if defined(OS_MACOSX)
2360 int modifiers = blink::WebInputEvent::MetaKey;
2361 #else
2362 int modifiers = blink::WebInputEvent::ControlKey;
2363 #endif
2364 modifiers |= blink::WebInputEvent::ShiftKey;
2365 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonLeft;
2366 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
2367 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
2370 // Middle-clicks open in a background tab.
2371 // TODO(linux_aura) http://crbug.com/163931
2372 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
2373 #define MAYBE_WindowOpenMiddleClickTest DISABLED_WindowOpenMiddleClickTest
2374 #else
2375 #define MAYBE_WindowOpenMiddleClickTest WindowOpenMiddleClickTest
2376 #endif
2377 IN_PROC_BROWSER_TEST_F(ClickModifierTest, MAYBE_WindowOpenMiddleClickTest) {
2378 int modifiers = 0;
2379 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonMiddle;
2380 WindowOpenDisposition disposition = NEW_BACKGROUND_TAB;
2381 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
2384 // Shift-middle-clicks open in a foreground tab.
2385 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenShiftMiddleClickTest) {
2386 int modifiers = blink::WebInputEvent::ShiftKey;
2387 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonMiddle;
2388 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
2389 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
2392 // Tests for clicking on normal links.
2394 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefBasicClickTest) {
2395 int modifiers = 0;
2396 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonLeft;
2397 WindowOpenDisposition disposition = CURRENT_TAB;
2398 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
2401 // TODO(ericu): Alt-click behavior on links is platform-dependent and not well
2402 // defined. Should we add tests so we know if it changes?
2404 // Shift-clicks open in a new window.
2405 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefShiftClickTest) {
2406 int modifiers = blink::WebInputEvent::ShiftKey;
2407 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonLeft;
2408 WindowOpenDisposition disposition = NEW_WINDOW;
2409 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
2412 // Control-clicks open in a background tab.
2413 // On OSX meta [the command key] takes the place of control.
2414 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefControlClickTest) {
2415 #if defined(OS_MACOSX)
2416 int modifiers = blink::WebInputEvent::MetaKey;
2417 #else
2418 int modifiers = blink::WebInputEvent::ControlKey;
2419 #endif
2420 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonLeft;
2421 WindowOpenDisposition disposition = NEW_BACKGROUND_TAB;
2422 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
2425 // Control-shift-clicks open in a foreground tab.
2426 // On OSX meta [the command key] takes the place of control.
2427 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefControlShiftClickTest) {
2428 #if defined(OS_MACOSX)
2429 int modifiers = blink::WebInputEvent::MetaKey;
2430 #else
2431 int modifiers = blink::WebInputEvent::ControlKey;
2432 #endif
2433 modifiers |= blink::WebInputEvent::ShiftKey;
2434 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonLeft;
2435 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
2436 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
2439 // Middle-clicks open in a background tab.
2440 // TODO(linux_aura) http://crbug.com/163931
2441 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
2442 #define MAYBE_HrefMiddleClickTest DISABLED_HrefMiddleClickTest
2443 #else
2444 #define MAYBE_HrefMiddleClickTest HrefMiddleClickTest
2445 #endif
2446 IN_PROC_BROWSER_TEST_F(ClickModifierTest, MAYBE_HrefMiddleClickTest) {
2447 int modifiers = 0;
2448 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonMiddle;
2449 WindowOpenDisposition disposition = NEW_BACKGROUND_TAB;
2450 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
2453 // Shift-middle-clicks open in a foreground tab.
2454 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefShiftMiddleClickTest) {
2455 int modifiers = blink::WebInputEvent::ShiftKey;
2456 blink::WebMouseEvent::Button button = blink::WebMouseEvent::ButtonMiddle;
2457 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
2458 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
2461 // Does not work with Instant Extended. http://crbug.com/317760.
2462 // // TODO(sail): enable this for MAC when
2463 // // BrowserWindowCocoa::GetRenderViewHeightInsetWithDetachedBookmarkBar
2464 // // is fixed.
2465 // #if defined(OS_MACOSX)
2466 // #define MAYBE_GetSizeForNewRenderView DISABLED_GetSizeForNewRenderView
2467 // #else
2468 // #define MAYBE_GetSizeForNewRenderView GetSizeForNewRenderView
2469 // #endif
2470 IN_PROC_BROWSER_TEST_F(BrowserTest, DISABLED_GetSizeForNewRenderView) {
2471 ASSERT_TRUE(test_server()->Start());
2472 // Create an HTTPS server for cross-site transition.
2473 net::SpawnedTestServer https_test_server(net::SpawnedTestServer::TYPE_HTTPS,
2474 net::SpawnedTestServer::kLocalhost,
2475 base::FilePath(kDocRoot));
2476 ASSERT_TRUE(https_test_server.Start());
2478 // Start with NTP.
2479 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2480 ASSERT_EQ(BookmarkBar::DETACHED, browser()->bookmark_bar_state());
2481 WebContents* web_contents =
2482 browser()->tab_strip_model()->GetActiveWebContents();
2483 content::RenderViewHost* prev_rvh = web_contents->GetRenderViewHost();
2484 const int height_inset =
2485 browser()->window()->GetRenderViewHeightInsetWithDetachedBookmarkBar();
2486 const gfx::Size initial_wcv_size =
2487 web_contents->GetView()->GetContainerSize();
2488 RenderViewSizeObserver observer(web_contents, browser()->window());
2490 // Navigate to a non-NTP page, without resizing WebContentsView.
2491 ui_test_utils::NavigateToURL(browser(),
2492 test_server()->GetURL("files/title1.html"));
2493 ASSERT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
2494 // A new RenderViewHost should be created.
2495 EXPECT_NE(prev_rvh, web_contents->GetRenderViewHost());
2496 prev_rvh = web_contents->GetRenderViewHost();
2497 gfx::Size rwhv_create_size0, rwhv_commit_size0, wcv_commit_size0;
2498 observer.GetSizeForRenderViewHost(web_contents->GetRenderViewHost(),
2499 &rwhv_create_size0,
2500 &rwhv_commit_size0,
2501 &wcv_commit_size0);
2502 // The create height of RenderWidgetHostView should include the height inset.
2503 EXPECT_EQ(gfx::Size(initial_wcv_size.width(),
2504 initial_wcv_size.height() + height_inset),
2505 rwhv_create_size0);
2506 // When a navigation entry is committed, the size of RenderWidgetHostView
2507 // should be the same as when it was first created.
2508 EXPECT_EQ(rwhv_create_size0, rwhv_commit_size0);
2509 // Sizes of the current RenderWidgetHostView and WebContentsView should not
2510 // change before and after WebContentsDelegate::DidNavigateMainFramePostCommit
2511 // (implemented by Browser); we obtain the sizes before PostCommit via
2512 // WebContentsObserver::NavigationEntryCommitted (implemented by
2513 // RenderViewSizeObserver).
2514 EXPECT_EQ(rwhv_commit_size0,
2515 web_contents->GetRenderWidgetHostView()->GetViewBounds().size());
2516 EXPECT_EQ(wcv_commit_size0, web_contents->GetView()->GetContainerSize());
2518 // Navigate to another non-NTP page, without resizing WebContentsView.
2519 ui_test_utils::NavigateToURL(browser(),
2520 https_test_server.GetURL("files/title2.html"));
2521 ASSERT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
2522 // A new RenderVieHost should be created.
2523 EXPECT_NE(prev_rvh, web_contents->GetRenderViewHost());
2524 gfx::Size rwhv_create_size1, rwhv_commit_size1, wcv_commit_size1;
2525 observer.GetSizeForRenderViewHost(web_contents->GetRenderViewHost(),
2526 &rwhv_create_size1,
2527 &rwhv_commit_size1,
2528 &wcv_commit_size1);
2529 EXPECT_EQ(rwhv_create_size1, rwhv_commit_size1);
2530 EXPECT_EQ(rwhv_commit_size1,
2531 web_contents->GetRenderWidgetHostView()->GetViewBounds().size());
2532 EXPECT_EQ(wcv_commit_size1, web_contents->GetView()->GetContainerSize());
2534 // Navigate from NTP to a non-NTP page, resizing WebContentsView while
2535 // navigation entry is pending.
2536 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
2537 gfx::Size wcv_resize_insets(-34, -57);
2538 observer.set_wcv_resize_insets(wcv_resize_insets);
2539 ui_test_utils::NavigateToURL(browser(),
2540 test_server()->GetURL("files/title2.html"));
2541 ASSERT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
2542 gfx::Size rwhv_create_size2, rwhv_commit_size2, wcv_commit_size2;
2543 observer.GetSizeForRenderViewHost(web_contents->GetRenderViewHost(),
2544 &rwhv_create_size2,
2545 &rwhv_commit_size2,
2546 &wcv_commit_size2);
2547 // The create height of RenderWidgetHostView should include the height inset.
2548 EXPECT_EQ(gfx::Size(initial_wcv_size.width(),
2549 initial_wcv_size.height() + height_inset),
2550 rwhv_create_size2);
2551 // WebContentsView was resized in
2552 // RenderViewSizeObserver::DidStartNavigationToPendingEntry after
2553 // RenderWidgetHostView was created, so the commit size should be resized
2554 // accordingly.
2555 gfx::Size exp_commit_size(initial_wcv_size);
2556 exp_commit_size.Enlarge(wcv_resize_insets.width(),
2557 wcv_resize_insets.height() + height_inset);
2558 EXPECT_EQ(exp_commit_size, rwhv_commit_size2);
2559 EXPECT_EQ(exp_commit_size, wcv_commit_size2);
2560 // Sizes of RenderWidgetHostView and WebContentsView before and after
2561 // WebContentsDelegate::DidNavigateMainFramePostCommit should be the same.
2562 EXPECT_EQ(rwhv_commit_size2,
2563 web_contents->GetRenderWidgetHostView()->GetViewBounds().size());
2564 EXPECT_EQ(wcv_commit_size2, web_contents->GetView()->GetContainerSize());