Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / browser_navigator_browsertest.cc
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.
4
5 #include "chrome/browser/ui/browser_navigator_browsertest.h"
6
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/prefs/incognito_mode_prefs.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_navigator.h"
18 #include "chrome/browser/ui/browser_tabstrip.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/chrome_pages.h"
21 #include "chrome/browser/ui/location_bar/location_bar.h"
22 #include "chrome/browser/ui/omnibox/omnibox_view.h"
23 #include "chrome/browser/ui/singleton_tabs.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/url_constants.h"
28 #include "chrome/test/base/ui_test_utils.h"
29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/notification_types.h"
31 #include "content/public/browser/web_contents.h"
32
33 using content::WebContents;
34
35 namespace {
36
37 const char kExpectedTitle[] = "PASSED!";
38 const char kEchoTitleCommand[] = "echotitle";
39
40 GURL GetGoogleURL() {
41   return GURL("http://www.google.com/");
42 }
43
44 GURL GetSettingsURL() {
45   return GURL(chrome::kChromeUISettingsURL);
46 }
47
48 GURL GetContentSettingsURL() {
49   return GetSettingsURL().Resolve(chrome::kContentSettingsExceptionsSubPage);
50 }
51
52 GURL GetClearBrowsingDataURL() {
53   return GetSettingsURL().Resolve(chrome::kClearBrowserDataSubPage);
54 }
55
56 // Converts long uber URLs ("chrome://chrome/foo/") to short (virtual) URLs
57 // ("chrome://foo/"). This should be used to convert the return value of
58 // WebContentsImpl::GetURL before comparison because it can return either the
59 // real URL or the virtual URL.
60 GURL ShortenUberURL(const GURL& url) {
61   std::string url_string = url.spec();
62   const std::string long_prefix = "chrome://chrome/";
63   const std::string short_prefix = "chrome://";
64   if (url_string.find(long_prefix) != 0)
65     return url;
66   url_string.replace(0, long_prefix.length(), short_prefix);
67   return GURL(url_string);
68 }
69
70 }  // namespace
71
72 chrome::NavigateParams BrowserNavigatorTest::MakeNavigateParams() const {
73   return MakeNavigateParams(browser());
74 }
75
76 chrome::NavigateParams BrowserNavigatorTest::MakeNavigateParams(
77     Browser* browser) const {
78   chrome::NavigateParams params(browser, GetGoogleURL(),
79                                 ui::PAGE_TRANSITION_LINK);
80   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
81   return params;
82 }
83
84 bool BrowserNavigatorTest::OpenPOSTURLInNewForegroundTabAndGetTitle(
85     const GURL& url, const std::string& post_data, bool is_browser_initiated,
86     base::string16* title) {
87   chrome::NavigateParams param(MakeNavigateParams());
88   param.disposition = NEW_FOREGROUND_TAB;
89   param.url = url;
90   param.is_renderer_initiated = !is_browser_initiated;
91   param.uses_post = true;
92   param.browser_initiated_post_data = new base::RefCountedStaticMemory(
93       post_data.data(), post_data.size());
94
95   ui_test_utils::NavigateToURL(&param);
96   if (!param.target_contents)
97     return false;
98
99   // Navigate() should have opened the contents in new foreground tab in the
100   // current Browser.
101   EXPECT_EQ(browser(), param.browser);
102   EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(),
103             param.target_contents);
104   // We should have one window, with one tab.
105   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
106   EXPECT_EQ(2, browser()->tab_strip_model()->count());
107
108   *title = param.target_contents->GetTitle();
109   return true;
110 }
111
112 Browser* BrowserNavigatorTest::CreateEmptyBrowserForType(Browser::Type type,
113                                                          Profile* profile) {
114   Browser* browser = new Browser(
115       Browser::CreateParams(type, profile, chrome::GetActiveDesktop()));
116   chrome::AddTabAt(browser, GURL(), -1, true);
117   return browser;
118 }
119
120 Browser* BrowserNavigatorTest::CreateEmptyBrowserForApp(Profile* profile) {
121   Browser* browser = new Browser(
122       Browser::CreateParams::CreateForApp(
123           "Test", false /* trusted_source */, gfx::Rect(), profile,
124           chrome::GetActiveDesktop()));
125   chrome::AddTabAt(browser, GURL(), -1, true);
126   return browser;
127 }
128
129 WebContents* BrowserNavigatorTest::CreateWebContents() {
130   content::WebContents::CreateParams create_params(browser()->profile());
131   content::WebContents* base_web_contents =
132       browser()->tab_strip_model()->GetActiveWebContents();
133   if (base_web_contents) {
134     create_params.initial_size =
135         base_web_contents->GetContainerBounds().size();
136   }
137   return WebContents::Create(create_params);
138 }
139
140 void BrowserNavigatorTest::RunSuppressTest(WindowOpenDisposition disposition) {
141   GURL old_url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
142   chrome::NavigateParams params(MakeNavigateParams());
143   params.disposition = disposition;
144   chrome::Navigate(&params);
145
146   // Nothing should have happened as a result of Navigate();
147   EXPECT_EQ(1, browser()->tab_strip_model()->count());
148   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
149   EXPECT_EQ(old_url,
150             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
151 }
152
153 void BrowserNavigatorTest::RunUseNonIncognitoWindowTest(const GURL& url) {
154   Browser* incognito_browser = CreateIncognitoBrowser();
155
156   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
157   EXPECT_EQ(1, browser()->tab_strip_model()->count());
158   EXPECT_EQ(1, incognito_browser->tab_strip_model()->count());
159
160   // Navigate to the page.
161   chrome::NavigateParams params(MakeNavigateParams(incognito_browser));
162   params.disposition = SINGLETON_TAB;
163   params.url = url;
164   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
165   chrome::Navigate(&params);
166
167   // This page should be opened in browser() window.
168   EXPECT_NE(incognito_browser, params.browser);
169   EXPECT_EQ(browser(), params.browser);
170   EXPECT_EQ(2, browser()->tab_strip_model()->count());
171   EXPECT_EQ(url,
172             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
173 }
174
175 void BrowserNavigatorTest::RunDoNothingIfIncognitoIsForcedTest(
176     const GURL& url) {
177   Browser* browser = CreateIncognitoBrowser();
178
179   // Set kIncognitoModeAvailability to FORCED.
180   PrefService* prefs1 = browser->profile()->GetPrefs();
181   prefs1->SetInteger(prefs::kIncognitoModeAvailability,
182                      IncognitoModePrefs::FORCED);
183   PrefService* prefs2 = browser->profile()->GetOriginalProfile()->GetPrefs();
184   prefs2->SetInteger(prefs::kIncognitoModeAvailability,
185                      IncognitoModePrefs::FORCED);
186
187   // Navigate to the page.
188   chrome::NavigateParams params(MakeNavigateParams(browser));
189   params.disposition = OFF_THE_RECORD;
190   params.url = url;
191   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
192   chrome::Navigate(&params);
193
194   // The page should not be opened.
195   EXPECT_EQ(browser, params.browser);
196   EXPECT_EQ(1, browser->tab_strip_model()->count());
197   EXPECT_EQ(GURL(url::kAboutBlankURL),
198             browser->tab_strip_model()->GetActiveWebContents()->GetURL());
199 }
200
201 void BrowserNavigatorTest::SetUpCommandLine(base::CommandLine* command_line) {
202   // Disable settings-in-a-window so that we can use the settings page and
203   // sub-pages to test browser navigation.
204   command_line->AppendSwitch(::switches::kDisableSettingsWindow);
205 }
206
207 void BrowserNavigatorTest::Observe(
208     int type,
209     const content::NotificationSource& source,
210     const content::NotificationDetails& details) {
211   switch (type) {
212     case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: {
213       ++this->created_tab_contents_count_;
214       break;
215     }
216     default:
217       break;
218   }
219 }
220
221
222 namespace {
223
224 // This test verifies that when a navigation occurs within a tab, the tab count
225 // of the Browser remains the same and the current tab bears the loaded URL.
226 // Note that network URLs are not actually loaded in tests, so this also tests
227 // that error pages leave the intended URL in the address bar.
228 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_CurrentTab) {
229   ui_test_utils::NavigateToURL(browser(), GetGoogleURL());
230   EXPECT_EQ(GetGoogleURL(),
231             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
232   // We should have one window with one tab.
233   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
234   EXPECT_EQ(1, browser()->tab_strip_model()->count());
235 }
236
237 // This test verifies that a singleton tab is refocused if one is already opened
238 // in another or an existing window, or added if it is not.
239 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_SingletonTabExisting) {
240   GURL singleton_url1("http://maps.google.com/");
241
242   // Register for a notification if an additional WebContents was instantiated.
243   // Opening a Singleton tab that is already opened should not be opening a new
244   // tab nor be creating a new WebContents object.
245   content::NotificationRegistrar registrar;
246
247   // As the registrar object goes out of scope, this will get unregistered
248   registrar.Add(this,
249                 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
250                 content::NotificationService::AllSources());
251
252   chrome::AddSelectedTabWithURL(browser(), singleton_url1,
253                                 ui::PAGE_TRANSITION_LINK);
254   chrome::AddSelectedTabWithURL(browser(), GetGoogleURL(),
255                                 ui::PAGE_TRANSITION_LINK);
256
257   // We should have one browser with 3 tabs, the 3rd selected.
258   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
259   EXPECT_EQ(2, browser()->tab_strip_model()->active_index());
260
261   unsigned int previous_tab_contents_count =
262       created_tab_contents_count_ = 0;
263
264   // Navigate to singleton_url1.
265   chrome::NavigateParams params(MakeNavigateParams());
266   params.disposition = SINGLETON_TAB;
267   params.url = singleton_url1;
268   chrome::Navigate(&params);
269
270   // The middle tab should now be selected.
271   EXPECT_EQ(browser(), params.browser);
272   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
273
274   // No tab contents should have been created
275   EXPECT_EQ(previous_tab_contents_count,
276             created_tab_contents_count_);
277 }
278
279 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
280                        Disposition_SingletonTabRespectingRef) {
281   GURL singleton_ref_url1("http://maps.google.com/#a");
282   GURL singleton_ref_url2("http://maps.google.com/#b");
283   GURL singleton_ref_url3("http://maps.google.com/");
284
285   chrome::AddSelectedTabWithURL(browser(), singleton_ref_url1,
286                                 ui::PAGE_TRANSITION_LINK);
287
288   // We should have one browser with 2 tabs, 2nd selected.
289   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
290   EXPECT_EQ(2, browser()->tab_strip_model()->count());
291   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
292
293   // Navigate to singleton_url2.
294   chrome::NavigateParams params(MakeNavigateParams());
295   params.disposition = SINGLETON_TAB;
296   params.url = singleton_ref_url2;
297   chrome::Navigate(&params);
298
299   // We should now have 2 tabs, the 2nd one selected.
300   EXPECT_EQ(browser(), params.browser);
301   EXPECT_EQ(2, browser()->tab_strip_model()->count());
302   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
303
304   // Navigate to singleton_url2, but with respect ref set.
305   params = MakeNavigateParams();
306   params.disposition = SINGLETON_TAB;
307   params.url = singleton_ref_url2;
308   params.ref_behavior = chrome::NavigateParams::RESPECT_REF;
309   chrome::Navigate(&params);
310
311   // We should now have 3 tabs, the 3th one selected.
312   EXPECT_EQ(browser(), params.browser);
313   EXPECT_EQ(3, browser()->tab_strip_model()->count());
314   EXPECT_EQ(2, browser()->tab_strip_model()->active_index());
315
316   // Navigate to singleton_url3.
317   params = MakeNavigateParams();
318   params.disposition = SINGLETON_TAB;
319   params.url = singleton_ref_url3;
320   params.ref_behavior = chrome::NavigateParams::RESPECT_REF;
321   chrome::Navigate(&params);
322
323   // We should now have 4 tabs, the 4th one selected.
324   EXPECT_EQ(browser(), params.browser);
325   EXPECT_EQ(4, browser()->tab_strip_model()->count());
326   EXPECT_EQ(3, browser()->tab_strip_model()->active_index());
327 }
328
329 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
330                        Disposition_SingletonTabNoneExisting) {
331   GURL singleton_url1("http://maps.google.com/");
332
333   // We should have one browser with 1 tab.
334   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
335   EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
336
337   // Navigate to singleton_url1.
338   chrome::NavigateParams params(MakeNavigateParams());
339   params.disposition = SINGLETON_TAB;
340   params.url = singleton_url1;
341   chrome::Navigate(&params);
342
343   // We should now have 2 tabs, the 2nd one selected.
344   EXPECT_EQ(browser(), params.browser);
345   EXPECT_EQ(2, browser()->tab_strip_model()->count());
346   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
347 }
348
349 // This test verifies that when a navigation results in a foreground tab, the
350 // tab count of the Browser increases and the selected tab shifts to the new
351 // foreground tab.
352 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewForegroundTab) {
353   WebContents* old_contents =
354       browser()->tab_strip_model()->GetActiveWebContents();
355   chrome::NavigateParams params(MakeNavigateParams());
356   params.disposition = NEW_FOREGROUND_TAB;
357   chrome::Navigate(&params);
358   EXPECT_NE(old_contents,
359             browser()->tab_strip_model()->GetActiveWebContents());
360   EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(),
361             params.target_contents);
362   EXPECT_EQ(2, browser()->tab_strip_model()->count());
363 }
364
365 // This test verifies that when a navigation results in a background tab, the
366 // tab count of the Browser increases but the selected tab remains the same.
367 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewBackgroundTab) {
368   WebContents* old_contents =
369       browser()->tab_strip_model()->GetActiveWebContents();
370   chrome::NavigateParams params(MakeNavigateParams());
371   params.disposition = NEW_BACKGROUND_TAB;
372   chrome::Navigate(&params);
373   WebContents* new_contents =
374       browser()->tab_strip_model()->GetActiveWebContents();
375   // The selected tab should have remained unchanged, since the new tab was
376   // opened in the background.
377   EXPECT_EQ(old_contents, new_contents);
378   EXPECT_EQ(2, browser()->tab_strip_model()->count());
379 }
380
381 // This test verifies that when a navigation requiring a new foreground tab
382 // occurs in a Browser that cannot host multiple tabs, the new foreground tab
383 // is created in an existing compatible Browser.
384 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
385                        Disposition_IncompatibleWindow_Existing) {
386   // Open a foreground tab in a window that cannot open popups when there is an
387   // existing compatible window somewhere else that they can be opened within.
388   Browser* popup = CreateEmptyBrowserForType(Browser::TYPE_POPUP,
389                                              browser()->profile());
390   chrome::NavigateParams params(MakeNavigateParams(popup));
391   params.disposition = NEW_FOREGROUND_TAB;
392   chrome::Navigate(&params);
393
394   // Navigate() should have opened the tab in a different browser since the
395   // one we supplied didn't support additional tabs.
396   EXPECT_NE(popup, params.browser);
397
398   // Since browser() is an existing compatible tabbed browser, it should have
399   // opened the tab there.
400   EXPECT_EQ(browser(), params.browser);
401
402   // We should be left with 2 windows, the popup with one tab and the browser()
403   // provided by the framework with two.
404   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
405   EXPECT_EQ(1, popup->tab_strip_model()->count());
406   EXPECT_EQ(2, browser()->tab_strip_model()->count());
407 }
408
409 // This test verifies that when a navigation requiring a new foreground tab
410 // occurs in a Browser that cannot host multiple tabs and no compatible Browser
411 // that can is open, a compatible Browser is created.
412 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
413                        Disposition_IncompatibleWindow_NoExisting) {
414   // We want to simulate not being able to find an existing window compatible
415   // with our non-tabbed browser window so Navigate() is forced to create a
416   // new compatible window. Because browser() supplied by the in-process
417   // browser testing framework is compatible with browser()->profile(), we
418   // need a different profile, and creating a popup window with an incognito
419   // profile is a quick and dirty way of achieving this.
420   Browser* popup = CreateEmptyBrowserForType(
421       Browser::TYPE_POPUP,
422       browser()->profile()->GetOffTheRecordProfile());
423   chrome::NavigateParams params(MakeNavigateParams(popup));
424   params.disposition = NEW_FOREGROUND_TAB;
425   chrome::Navigate(&params);
426
427   // Navigate() should have opened the tab in a different browser since the
428   // one we supplied didn't support additional tabs.
429   EXPECT_NE(popup, params.browser);
430
431   // This time, browser() is _not_ compatible with popup since it is not an
432   // incognito window.
433   EXPECT_NE(browser(), params.browser);
434
435   // We should have three windows, each with one tab:
436   // 1. the browser() provided by the framework (unchanged in this test)
437   // 2. the incognito popup we created originally
438   // 3. the new incognito tabbed browser that was created by Navigate().
439   EXPECT_EQ(3u, chrome::GetTotalBrowserCount());
440   EXPECT_EQ(1, browser()->tab_strip_model()->count());
441   EXPECT_EQ(1, popup->tab_strip_model()->count());
442   EXPECT_EQ(1, params.browser->tab_strip_model()->count());
443   EXPECT_TRUE(params.browser->is_type_tabbed());
444 }
445
446 // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP
447 // from a normal Browser results in a new Browser with TYPE_POPUP.
448 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewPopup) {
449   chrome::NavigateParams params(MakeNavigateParams());
450   params.disposition = NEW_POPUP;
451   params.window_bounds = gfx::Rect(0, 0, 200, 200);
452   // Wait for new popup to to load and gain focus.
453   ui_test_utils::NavigateToURL(&params);
454
455   // Navigate() should have opened a new, focused popup window.
456   EXPECT_NE(browser(), params.browser);
457 #if 0
458   // TODO(stevenjb): Enable this test. See: crbug.com/79493
459   EXPECT_TRUE(browser->window()->IsActive());
460 #endif
461   EXPECT_TRUE(params.browser->is_type_popup());
462   EXPECT_FALSE(params.browser->is_app());
463
464   // We should have two windows, the browser() provided by the framework and the
465   // new popup window.
466   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
467   EXPECT_EQ(1, browser()->tab_strip_model()->count());
468   EXPECT_EQ(1, params.browser->tab_strip_model()->count());
469 }
470
471 // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP
472 // from a normal Browser results in a new Browser with is_app() true.
473 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewPopup_ExtensionId) {
474   chrome::NavigateParams params(MakeNavigateParams());
475   params.disposition = NEW_POPUP;
476   params.extension_app_id = "extensionappid";
477   params.window_bounds = gfx::Rect(0, 0, 200, 200);
478   // Wait for new popup to to load and gain focus.
479   ui_test_utils::NavigateToURL(&params);
480
481   // Navigate() should have opened a new, focused popup window.
482   EXPECT_NE(browser(), params.browser);
483   EXPECT_TRUE(params.browser->is_type_popup());
484   EXPECT_TRUE(params.browser->is_app());
485
486   // We should have two windows, the browser() provided by the framework and the
487   // new popup window.
488   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
489   EXPECT_EQ(1, browser()->tab_strip_model()->count());
490   EXPECT_EQ(1, params.browser->tab_strip_model()->count());
491 }
492
493 // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP
494 // from a normal popup results in a new Browser with TYPE_POPUP.
495 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewPopupFromPopup) {
496   // Open a popup.
497   chrome::NavigateParams params1(MakeNavigateParams());
498   params1.disposition = NEW_POPUP;
499   params1.window_bounds = gfx::Rect(0, 0, 200, 200);
500   chrome::Navigate(&params1);
501   // Open another popup.
502   chrome::NavigateParams params2(MakeNavigateParams(params1.browser));
503   params2.disposition = NEW_POPUP;
504   params2.window_bounds = gfx::Rect(0, 0, 200, 200);
505   chrome::Navigate(&params2);
506
507   // Navigate() should have opened a new normal popup window.
508   EXPECT_NE(params1.browser, params2.browser);
509   EXPECT_TRUE(params2.browser->is_type_popup());
510   EXPECT_FALSE(params2.browser->is_app());
511
512   // We should have three windows, the browser() provided by the framework,
513   // the first popup window, and the second popup window.
514   EXPECT_EQ(3u, chrome::GetTotalBrowserCount());
515   EXPECT_EQ(1, browser()->tab_strip_model()->count());
516   EXPECT_EQ(1, params1.browser->tab_strip_model()->count());
517   EXPECT_EQ(1, params2.browser->tab_strip_model()->count());
518 }
519
520 // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP
521 // from an app frame results in a new Browser with TYPE_POPUP.
522 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
523                        Disposition_NewPopupFromAppWindow) {
524   Browser* app_browser = CreateEmptyBrowserForApp(browser()->profile());
525   chrome::NavigateParams params(MakeNavigateParams(app_browser));
526   params.disposition = NEW_POPUP;
527   params.window_bounds = gfx::Rect(0, 0, 200, 200);
528   chrome::Navigate(&params);
529
530   // Navigate() should have opened a new popup app window.
531   EXPECT_NE(app_browser, params.browser);
532   EXPECT_NE(browser(), params.browser);
533   EXPECT_TRUE(params.browser->is_type_popup());
534   EXPECT_TRUE(params.browser->is_app());
535
536   // We should now have three windows, the app window, the app popup it created,
537   // and the original browser() provided by the framework.
538   EXPECT_EQ(3u, chrome::GetTotalBrowserCount());
539   EXPECT_EQ(1, browser()->tab_strip_model()->count());
540   EXPECT_EQ(1, app_browser->tab_strip_model()->count());
541   EXPECT_EQ(1, params.browser->tab_strip_model()->count());
542 }
543
544 // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP
545 // from an app popup results in a new Browser also of TYPE_POPUP.
546 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
547                        Disposition_NewPopupFromAppPopup) {
548   Browser* app_browser = CreateEmptyBrowserForApp(browser()->profile());
549   // Open an app popup.
550   chrome::NavigateParams params1(MakeNavigateParams(app_browser));
551   params1.disposition = NEW_POPUP;
552   params1.window_bounds = gfx::Rect(0, 0, 200, 200);
553   chrome::Navigate(&params1);
554   // Now open another app popup.
555   chrome::NavigateParams params2(MakeNavigateParams(params1.browser));
556   params2.disposition = NEW_POPUP;
557   params2.window_bounds = gfx::Rect(0, 0, 200, 200);
558   chrome::Navigate(&params2);
559
560   // Navigate() should have opened a new popup app window.
561   EXPECT_NE(browser(), params1.browser);
562   EXPECT_NE(params1.browser, params2.browser);
563   EXPECT_TRUE(params2.browser->is_type_popup());
564   EXPECT_TRUE(params2.browser->is_app());
565
566   // We should now have four windows, the app window, the first app popup,
567   // the second app popup, and the original browser() provided by the framework.
568   EXPECT_EQ(4u, chrome::GetTotalBrowserCount());
569   EXPECT_EQ(1, browser()->tab_strip_model()->count());
570   EXPECT_EQ(1, app_browser->tab_strip_model()->count());
571   EXPECT_EQ(1, params1.browser->tab_strip_model()->count());
572   EXPECT_EQ(1, params2.browser->tab_strip_model()->count());
573 }
574
575 // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP
576 // from an extension app tab results in a new Browser with TYPE_APP_POPUP.
577 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
578                        Disposition_NewPopupFromExtensionApp) {
579   // TODO(beng): TBD.
580 }
581
582 // This test verifies that navigating with window_action = SHOW_WINDOW_INACTIVE
583 // does not focus a new new popup window.
584 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewPopupUnfocused) {
585   chrome::NavigateParams params(MakeNavigateParams());
586   params.disposition = NEW_POPUP;
587   params.window_bounds = gfx::Rect(0, 0, 200, 200);
588   params.window_action = chrome::NavigateParams::SHOW_WINDOW_INACTIVE;
589   // Wait for new popup to load (and gain focus if the test fails).
590   ui_test_utils::NavigateToURL(&params);
591
592   // Navigate() should have opened a new, unfocused, popup window.
593   EXPECT_NE(browser(), params.browser);
594   EXPECT_EQ(Browser::TYPE_POPUP, params.browser->type());
595 #if 0
596 // TODO(stevenjb): Enable this test. See: crbug.com/79493
597   EXPECT_FALSE(p.browser->window()->IsActive());
598 #endif
599 }
600
601 // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP
602 // and trusted_source = true results in a new Browser where is_trusted_source()
603 // is true.
604 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewPopupTrusted) {
605   chrome::NavigateParams params(MakeNavigateParams());
606   params.disposition = NEW_POPUP;
607   params.trusted_source = true;
608   params.window_bounds = gfx::Rect(0, 0, 200, 200);
609   // Wait for new popup to to load and gain focus.
610   ui_test_utils::NavigateToURL(&params);
611
612   // Navigate() should have opened a new popup window of TYPE_TRUSTED_POPUP.
613   EXPECT_NE(browser(), params.browser);
614   EXPECT_TRUE(params.browser->is_type_popup());
615   EXPECT_TRUE(params.browser->is_trusted_source());
616 }
617
618
619 // This test verifies that navigating with WindowOpenDisposition = NEW_WINDOW
620 // always opens a new window.
621 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewWindow) {
622   chrome::NavigateParams params(MakeNavigateParams());
623   params.disposition = NEW_WINDOW;
624   chrome::Navigate(&params);
625
626   // Navigate() should have opened a new toplevel window.
627   EXPECT_NE(browser(), params.browser);
628   EXPECT_TRUE(params.browser->is_type_tabbed());
629
630   // We should now have two windows, the browser() provided by the framework and
631   // the new normal window.
632   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
633   EXPECT_EQ(1, browser()->tab_strip_model()->count());
634   EXPECT_EQ(1, params.browser->tab_strip_model()->count());
635 }
636
637 // This test verifies that navigating with WindowOpenDisposition = INCOGNITO
638 // opens a new incognito window if no existing incognito window is present.
639 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_Incognito) {
640   chrome::NavigateParams params(MakeNavigateParams());
641   params.disposition = OFF_THE_RECORD;
642   chrome::Navigate(&params);
643
644   // Navigate() should have opened a new toplevel incognito window.
645   EXPECT_NE(browser(), params.browser);
646   EXPECT_EQ(browser()->profile()->GetOffTheRecordProfile(),
647             params.browser->profile());
648
649   // |source_contents| should be set to NULL because the profile for the new
650   // page is different from the originating page.
651   EXPECT_EQ(NULL, params.source_contents);
652
653   // We should now have two windows, the browser() provided by the framework and
654   // the new incognito window.
655   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
656   EXPECT_EQ(1, browser()->tab_strip_model()->count());
657   EXPECT_EQ(1, params.browser->tab_strip_model()->count());
658 }
659
660 // This test verifies that navigating with WindowOpenDisposition = INCOGNITO
661 // reuses an existing incognito window when possible.
662 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_IncognitoRefocus) {
663   Browser* incognito_browser =
664       CreateEmptyBrowserForType(Browser::TYPE_TABBED,
665                                 browser()->profile()->GetOffTheRecordProfile());
666   chrome::NavigateParams params(MakeNavigateParams());
667   params.disposition = OFF_THE_RECORD;
668   chrome::Navigate(&params);
669
670   // Navigate() should have opened a new tab in the existing incognito window.
671   EXPECT_NE(browser(), params.browser);
672   EXPECT_EQ(params.browser, incognito_browser);
673
674   // We should now have two windows, the browser() provided by the framework and
675   // the incognito window we opened earlier.
676   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
677   EXPECT_EQ(1, browser()->tab_strip_model()->count());
678   EXPECT_EQ(2, incognito_browser->tab_strip_model()->count());
679 }
680
681 // This test verifies that no navigation action occurs when
682 // WindowOpenDisposition = SUPPRESS_OPEN.
683 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_SuppressOpen) {
684   RunSuppressTest(SUPPRESS_OPEN);
685 }
686
687 // This test verifies that no navigation action occurs when
688 // WindowOpenDisposition = SAVE_TO_DISK.
689 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_SaveToDisk) {
690   RunSuppressTest(SAVE_TO_DISK);
691 }
692
693 // This test verifies that no navigation action occurs when
694 // WindowOpenDisposition = IGNORE_ACTION.
695 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_IgnoreAction) {
696   RunSuppressTest(IGNORE_ACTION);
697 }
698
699 // This tests adding a foreground tab with a predefined WebContents.
700 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, TargetContents_ForegroundTab) {
701   chrome::NavigateParams params(MakeNavigateParams());
702   params.disposition = NEW_FOREGROUND_TAB;
703   params.target_contents = CreateWebContents();
704   chrome::Navigate(&params);
705
706   // Navigate() should have opened the contents in a new foreground in the
707   // current Browser.
708   EXPECT_EQ(browser(), params.browser);
709   EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(),
710             params.target_contents);
711
712   // We should have one window, with two tabs.
713   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
714   EXPECT_EQ(2, browser()->tab_strip_model()->count());
715 }
716
717 #if defined(OS_WIN)
718 // This tests adding a popup with a predefined WebContents.
719 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, DISABLED_TargetContents_Popup) {
720   chrome::NavigateParams params(MakeNavigateParams());
721   params.disposition = NEW_POPUP;
722   params.target_contents = CreateWebContents();
723   params.window_bounds = gfx::Rect(10, 10, 500, 500);
724   chrome::Navigate(&params);
725
726   // Navigate() should have opened a new popup window.
727   EXPECT_NE(browser(), params.browser);
728   EXPECT_TRUE(params.browser->is_type_popup());
729   EXPECT_FALSE(params.browser->is_app());
730
731   // The web platform is weird. The window bounds specified in
732   // |params.window_bounds| are used as follows:
733   // - the origin is used to position the window
734   // - the size is used to size the WebContents of the window.
735   // As such the position of the resulting window will always match
736   // params.window_bounds.origin(), but its size will not. We need to match
737   // the size against the selected tab's view's container size.
738   // Only Windows positions the window according to
739   // |params.window_bounds.origin()| - on Mac the window is offset from the
740   // opener and on Linux it always opens at 0,0.
741   EXPECT_EQ(params.window_bounds.origin(),
742             params.browser->window()->GetRestoredBounds().origin());
743   // All platforms should respect size however provided width > 400 (Mac has a
744   // minimum window width of 400).
745   EXPECT_EQ(params.window_bounds.size(),
746             params.target_contents->GetContainerBounds().size());
747
748   // We should have two windows, the new popup and the browser() provided by the
749   // framework.
750   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
751   EXPECT_EQ(1, browser()->tab_strip_model()->count());
752   EXPECT_EQ(1, params.browser->tab_strip_model()->count());
753 }
754 #endif
755
756 // This tests adding a tab at a specific index.
757 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Tabstrip_InsertAtIndex) {
758   // This is not meant to be a comprehensive test of whether or not the tab
759   // implementation of the browser observes the insertion index. That is
760   // covered by the unit tests for TabStripModel. This merely verifies that
761   // insertion index preference is reflected in common cases.
762   chrome::NavigateParams params(MakeNavigateParams());
763   params.disposition = NEW_FOREGROUND_TAB;
764   params.tabstrip_index = 0;
765   params.tabstrip_add_types = TabStripModel::ADD_FORCE_INDEX;
766   chrome::Navigate(&params);
767
768   // Navigate() should have inserted a new tab at slot 0 in the tabstrip.
769   EXPECT_EQ(browser(), params.browser);
770   EXPECT_EQ(0, browser()->tab_strip_model()->GetIndexOfWebContents(
771       static_cast<const WebContents*>(params.target_contents)));
772
773   // We should have one window - the browser() provided by the framework.
774   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
775   EXPECT_EQ(2, browser()->tab_strip_model()->count());
776 }
777
778 // This test verifies that constructing params with disposition = SINGLETON_TAB
779 // and IGNORE_AND_NAVIGATE opens a new tab navigated to the specified URL if
780 // no previous tab with that URL (minus the path) exists.
781 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
782                        Disposition_SingletonTabNew_IgnorePath) {
783   chrome::AddSelectedTabWithURL(browser(), GetGoogleURL(),
784                                 ui::PAGE_TRANSITION_LINK);
785
786   // We should have one browser with 2 tabs, the 2nd selected.
787   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
788   EXPECT_EQ(2, browser()->tab_strip_model()->count());
789   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
790
791   // Navigate to a new singleton tab with a sub-page.
792   chrome::NavigateParams params(MakeNavigateParams());
793   params.disposition = SINGLETON_TAB;
794   params.url = GetContentSettingsURL();
795   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
796   params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
797   chrome::Navigate(&params);
798
799   // The last tab should now be selected and navigated to the sub-page of the
800   // URL.
801   EXPECT_EQ(browser(), params.browser);
802   EXPECT_EQ(3, browser()->tab_strip_model()->count());
803   EXPECT_EQ(2, browser()->tab_strip_model()->active_index());
804   EXPECT_EQ(GetContentSettingsURL(),
805             ShortenUberURL(browser()->tab_strip_model()->
806                 GetActiveWebContents()->GetURL()));
807 }
808
809 // This test verifies that constructing params with disposition = SINGLETON_TAB
810 // and IGNORE_AND_NAVIGATE opens an existing tab with the matching URL (minus
811 // the path) which is navigated to the specified URL.
812 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
813                        Disposition_SingletonTabExisting_IgnorePath) {
814   GURL singleton_url1(GetSettingsURL());
815   chrome::AddSelectedTabWithURL(browser(), singleton_url1,
816                                 ui::PAGE_TRANSITION_LINK);
817   chrome::AddSelectedTabWithURL(browser(), GetGoogleURL(),
818                                 ui::PAGE_TRANSITION_LINK);
819
820   // We should have one browser with 3 tabs, the 3rd selected.
821   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
822   EXPECT_EQ(3, browser()->tab_strip_model()->count());
823   EXPECT_EQ(2, browser()->tab_strip_model()->active_index());
824
825   // Navigate to singleton_url1.
826   chrome::NavigateParams params(MakeNavigateParams());
827   params.disposition = SINGLETON_TAB;
828   params.url = GetContentSettingsURL();
829   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
830   params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
831   chrome::Navigate(&params);
832
833   // The middle tab should now be selected and navigated to the sub-page of the
834   // URL.
835   EXPECT_EQ(browser(), params.browser);
836   EXPECT_EQ(3, browser()->tab_strip_model()->count());
837   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
838   EXPECT_EQ(GetContentSettingsURL(),
839             ShortenUberURL(browser()->tab_strip_model()->
840                 GetActiveWebContents()->GetURL()));
841 }
842
843 // This test verifies that constructing params with disposition = SINGLETON_TAB
844 // and IGNORE_AND_NAVIGATE opens an existing tab with the matching URL (minus
845 // the path) which is navigated to the specified URL.
846 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
847                        Disposition_SingletonTabExistingSubPath_IgnorePath) {
848   GURL singleton_url1(GetContentSettingsURL());
849   chrome::AddSelectedTabWithURL(browser(), singleton_url1,
850                                 ui::PAGE_TRANSITION_LINK);
851   chrome::AddSelectedTabWithURL(browser(), GetGoogleURL(),
852                                 ui::PAGE_TRANSITION_LINK);
853
854   // We should have one browser with 3 tabs, the 3rd selected.
855   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
856   EXPECT_EQ(3, browser()->tab_strip_model()->count());
857   EXPECT_EQ(2, browser()->tab_strip_model()->active_index());
858
859   // Navigate to singleton_url1.
860   chrome::NavigateParams params(MakeNavigateParams());
861   params.disposition = SINGLETON_TAB;
862   params.url = GetClearBrowsingDataURL();
863   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
864   params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
865   chrome::Navigate(&params);
866
867   // The middle tab should now be selected and navigated to the sub-page of the
868   // URL.
869   EXPECT_EQ(browser(), params.browser);
870   EXPECT_EQ(3, browser()->tab_strip_model()->count());
871   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
872   EXPECT_EQ(GetClearBrowsingDataURL(),
873             ShortenUberURL(browser()->tab_strip_model()->
874                 GetActiveWebContents()->GetURL()));
875 }
876
877 // This test verifies that constructing params with disposition = SINGLETON_TAB
878 // and IGNORE_AND_STAY_PUT opens an existing tab with the matching URL (minus
879 // the path).
880 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
881                        Disposition_SingletonTabExistingSubPath_IgnorePath2) {
882   GURL singleton_url1(GetContentSettingsURL());
883   chrome::AddSelectedTabWithURL(browser(), singleton_url1,
884                                 ui::PAGE_TRANSITION_LINK);
885   chrome::AddSelectedTabWithURL(browser(), GetGoogleURL(),
886                                 ui::PAGE_TRANSITION_LINK);
887
888   // We should have one browser with 3 tabs, the 3rd selected.
889   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
890   EXPECT_EQ(3, browser()->tab_strip_model()->count());
891   EXPECT_EQ(2, browser()->tab_strip_model()->active_index());
892
893   // Navigate to singleton_url1.
894   chrome::NavigateParams params(MakeNavigateParams());
895   params.disposition = SINGLETON_TAB;
896   params.url = GetClearBrowsingDataURL();
897   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
898   params.path_behavior = chrome::NavigateParams::IGNORE_AND_STAY_PUT;
899   chrome::Navigate(&params);
900
901   // The middle tab should now be selected.
902   EXPECT_EQ(browser(), params.browser);
903   EXPECT_EQ(3, browser()->tab_strip_model()->count());
904   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
905   EXPECT_EQ(singleton_url1,
906             ShortenUberURL(browser()->tab_strip_model()->
907                 GetActiveWebContents()->GetURL()));
908 }
909
910 // This test verifies that constructing params with disposition = SINGLETON_TAB
911 // and IGNORE_AND_NAVIGATE will update the current tab's URL if the currently
912 // selected tab is a match but has a different path.
913 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
914                        Disposition_SingletonTabFocused_IgnorePath) {
915   GURL singleton_url_current(GetContentSettingsURL());
916   chrome::AddSelectedTabWithURL(browser(), singleton_url_current,
917                                 ui::PAGE_TRANSITION_LINK);
918
919   // We should have one browser with 2 tabs, the 2nd selected.
920   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
921   EXPECT_EQ(2, browser()->tab_strip_model()->count());
922   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
923
924   // Navigate to a different settings path.
925   GURL singleton_url_target(GetClearBrowsingDataURL());
926   chrome::NavigateParams params(MakeNavigateParams());
927   params.disposition = SINGLETON_TAB;
928   params.url = singleton_url_target;
929   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
930   params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
931   chrome::Navigate(&params);
932
933   // The second tab should still be selected, but navigated to the new path.
934   EXPECT_EQ(browser(), params.browser);
935   EXPECT_EQ(2, browser()->tab_strip_model()->count());
936   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
937   EXPECT_EQ(singleton_url_target,
938             ShortenUberURL(browser()->tab_strip_model()->
939                 GetActiveWebContents()->GetURL()));
940 }
941
942 // This test verifies that constructing params with disposition = SINGLETON_TAB
943 // and IGNORE_AND_NAVIGATE will open an existing matching tab with a different
944 // query.
945 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
946                        Disposition_SingletonTabExisting_IgnoreQuery) {
947   int initial_tab_count = browser()->tab_strip_model()->count();
948   GURL singleton_url_current("chrome://settings/internet");
949   chrome::AddSelectedTabWithURL(browser(), singleton_url_current,
950                                 ui::PAGE_TRANSITION_LINK);
951
952   EXPECT_EQ(initial_tab_count + 1, browser()->tab_strip_model()->count());
953   EXPECT_EQ(initial_tab_count, browser()->tab_strip_model()->active_index());
954
955   // Navigate to a different settings path.
956   GURL singleton_url_target(
957       "chrome://settings/internet?"
958       "servicePath=/profile/ethernet_00aa00aa00aa&networkType=1");
959   chrome::NavigateParams params(MakeNavigateParams());
960   params.disposition = SINGLETON_TAB;
961   params.url = singleton_url_target;
962   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
963   params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
964   chrome::Navigate(&params);
965
966   // Last tab should still be selected.
967   EXPECT_EQ(browser(), params.browser);
968   EXPECT_EQ(initial_tab_count + 1, browser()->tab_strip_model()->count());
969   EXPECT_EQ(initial_tab_count, browser()->tab_strip_model()->active_index());
970 }
971
972 // This test verifies that the settings page isn't opened in the incognito
973 // window.
974 // Disabled until fixed for uber settings: http://crbug.com/111243
975 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
976                        DISABLED_Disposition_Settings_UseNonIncognitoWindow) {
977   RunUseNonIncognitoWindowTest(GetSettingsURL());
978 }
979
980 // This test verifies that the view-source settings page isn't opened in the
981 // incognito window.
982 IN_PROC_BROWSER_TEST_F(
983     BrowserNavigatorTest,
984     Disposition_ViewSource_Settings_DoNothingIfIncognitoForced) {
985   std::string view_source(content::kViewSourceScheme);
986   view_source.append(":");
987   view_source.append(chrome::kChromeUISettingsURL);
988   RunDoNothingIfIncognitoIsForcedTest(GURL(view_source));
989 }
990
991 // This test verifies that the view-source settings page isn't opened in the
992 // incognito window even if incognito mode is forced (does nothing in that
993 // case).
994 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
995                        Disposition_ViewSource_Settings_UseNonIncognitoWindow) {
996   std::string view_source(content::kViewSourceScheme);
997   view_source.append(":");
998   view_source.append(chrome::kChromeUISettingsURL);
999   RunUseNonIncognitoWindowTest(GURL(view_source));
1000 }
1001
1002 // This test verifies that the settings page isn't opened in the incognito
1003 // window from a non-incognito window (bookmark open-in-incognito trigger).
1004 // Disabled until fixed for uber settings: http://crbug.com/111243
1005 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1006     DISABLED_Disposition_Settings_UseNonIncognitoWindowForBookmark) {
1007   chrome::NavigateParams params(browser(), GetSettingsURL(),
1008                                 ui::PAGE_TRANSITION_AUTO_BOOKMARK);
1009   params.disposition = OFF_THE_RECORD;
1010   {
1011     content::WindowedNotificationObserver observer(
1012         content::NOTIFICATION_LOAD_STOP,
1013         content::NotificationService::AllSources());
1014     chrome::Navigate(&params);
1015     observer.Wait();
1016   }
1017
1018   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1019   EXPECT_EQ(GetSettingsURL(),
1020             ShortenUberURL(browser()->tab_strip_model()->
1021                 GetActiveWebContents()->GetURL()));
1022 }
1023
1024 // Settings page is expected to always open in normal mode regardless
1025 // of whether the user is trying to open it in incognito mode or not.
1026 // This test verifies that if incognito mode is forced (by policy), settings
1027 // page doesn't open at all.
1028 // Disabled until fixed for uber settings: http://crbug.com/111243
1029 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1030     DISABLED_Disposition_Settings_DoNothingIfIncognitoIsForced) {
1031   RunDoNothingIfIncognitoIsForcedTest(GetSettingsURL());
1032 }
1033
1034 // This test verifies that the bookmarks page isn't opened in the incognito
1035 // window.
1036 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1037                        Disposition_Bookmarks_UseNonIncognitoWindow) {
1038   RunUseNonIncognitoWindowTest(GURL(chrome::kChromeUIBookmarksURL));
1039 }
1040
1041 // Bookmark manager is expected to always open in normal mode regardless
1042 // of whether the user is trying to open it in incognito mode or not.
1043 // This test verifies that if incognito mode is forced (by policy), bookmark
1044 // manager doesn't open at all.
1045 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1046                        Disposition_Bookmarks_DoNothingIfIncognitoIsForced) {
1047   RunDoNothingIfIncognitoIsForcedTest(GURL(chrome::kChromeUIBookmarksURL));
1048 }
1049
1050 // This test makes sure a crashed singleton tab reloads from a new navigation.
1051 // http://crbug.com/396371
1052 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1053                        DISABLED_NavigateToCrashedSingletonTab) {
1054   GURL singleton_url(GetContentSettingsURL());
1055   WebContents* web_contents = chrome::AddSelectedTabWithURL(
1056       browser(), singleton_url, ui::PAGE_TRANSITION_LINK);
1057
1058   // We should have one browser with 2 tabs, the 2nd selected.
1059   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1060   EXPECT_EQ(2, browser()->tab_strip_model()->count());
1061   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
1062
1063   // Kill the singleton tab.
1064   web_contents->SetIsCrashed(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
1065   EXPECT_TRUE(web_contents->IsCrashed());
1066
1067   chrome::NavigateParams params(MakeNavigateParams());
1068   params.disposition = SINGLETON_TAB;
1069   params.url = singleton_url;
1070   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
1071   params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
1072   ui_test_utils::NavigateToURL(&params);
1073
1074   // The tab should not be sad anymore.
1075   EXPECT_FALSE(web_contents->IsCrashed());
1076 }
1077
1078 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1079                        NavigateFromDefaultToOptionsInSameTab) {
1080   {
1081     content::WindowedNotificationObserver observer(
1082         content::NOTIFICATION_LOAD_STOP,
1083         content::NotificationService::AllSources());
1084     chrome::ShowSettings(browser());
1085     observer.Wait();
1086   }
1087   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1088   EXPECT_EQ(GetSettingsURL(),
1089             ShortenUberURL(browser()->tab_strip_model()->
1090                 GetActiveWebContents()->GetURL()));
1091 }
1092
1093 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1094                        NavigateFromBlankToOptionsInSameTab) {
1095   chrome::NavigateParams params(MakeNavigateParams());
1096   params.url = GURL(url::kAboutBlankURL);
1097   ui_test_utils::NavigateToURL(&params);
1098
1099   {
1100     content::WindowedNotificationObserver observer(
1101         content::NOTIFICATION_LOAD_STOP,
1102         content::NotificationService::AllSources());
1103     chrome::ShowSettings(browser());
1104     observer.Wait();
1105   }
1106   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1107   EXPECT_EQ(GetSettingsURL(),
1108             ShortenUberURL(browser()->tab_strip_model()->
1109                 GetActiveWebContents()->GetURL()));
1110 }
1111
1112 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1113                        NavigateFromNTPToOptionsInSameTab) {
1114   chrome::NavigateParams params(MakeNavigateParams());
1115   params.url = GURL(chrome::kChromeUINewTabURL);
1116   ui_test_utils::NavigateToURL(&params);
1117   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1118   EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
1119             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1120
1121   {
1122     content::WindowedNotificationObserver observer(
1123         content::NOTIFICATION_LOAD_STOP,
1124         content::NotificationService::AllSources());
1125     chrome::ShowSettings(browser());
1126     observer.Wait();
1127   }
1128   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1129   EXPECT_EQ(GetSettingsURL(),
1130             ShortenUberURL(browser()->tab_strip_model()->
1131                 GetActiveWebContents()->GetURL()));
1132 }
1133
1134 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1135                        NavigateFromPageToOptionsInNewTab) {
1136   chrome::NavigateParams params(MakeNavigateParams());
1137   ui_test_utils::NavigateToURL(&params);
1138   EXPECT_EQ(GetGoogleURL(),
1139             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1140   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1141   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1142
1143   {
1144     content::WindowedNotificationObserver observer(
1145         content::NOTIFICATION_LOAD_STOP,
1146         content::NotificationService::AllSources());
1147     chrome::ShowSettings(browser());
1148     observer.Wait();
1149   }
1150   EXPECT_EQ(2, browser()->tab_strip_model()->count());
1151   EXPECT_EQ(GetSettingsURL(),
1152             ShortenUberURL(browser()->tab_strip_model()->
1153                 GetActiveWebContents()->GetURL()));
1154 }
1155
1156 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1157                        NavigateFromNTPToOptionsSingleton) {
1158   {
1159     content::WindowedNotificationObserver observer(
1160         content::NOTIFICATION_LOAD_STOP,
1161         content::NotificationService::AllSources());
1162     chrome::ShowSettings(browser());
1163     observer.Wait();
1164   }
1165   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1166
1167   chrome::NewTab(browser());
1168   EXPECT_EQ(2, browser()->tab_strip_model()->count());
1169
1170   {
1171     content::WindowedNotificationObserver observer(
1172         content::NOTIFICATION_LOAD_STOP,
1173         content::NotificationService::AllSources());
1174     chrome::ShowSettings(browser());
1175     observer.Wait();
1176   }
1177   EXPECT_EQ(2, browser()->tab_strip_model()->count());
1178   EXPECT_EQ(GetSettingsURL(),
1179             ShortenUberURL(browser()->tab_strip_model()->
1180                 GetActiveWebContents()->GetURL()));
1181 }
1182
1183 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1184                        NavigateFromNTPToOptionsPageInSameTab) {
1185   {
1186     content::WindowedNotificationObserver observer(
1187         content::NOTIFICATION_LOAD_STOP,
1188         content::NotificationService::AllSources());
1189     chrome::ShowClearBrowsingDataDialog(browser());
1190     observer.Wait();
1191   }
1192   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1193   EXPECT_EQ(GetClearBrowsingDataURL(),
1194             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1195
1196   chrome::NewTab(browser());
1197   EXPECT_EQ(2, browser()->tab_strip_model()->count());
1198
1199   {
1200     content::WindowedNotificationObserver observer(
1201         content::NOTIFICATION_LOAD_STOP,
1202         content::NotificationService::AllSources());
1203     chrome::ShowClearBrowsingDataDialog(browser());
1204     observer.Wait();
1205   }
1206   EXPECT_EQ(2, browser()->tab_strip_model()->count());
1207   EXPECT_EQ(GetClearBrowsingDataURL(),
1208             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1209 }
1210
1211 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1212                        NavigateFromOtherTabToSingletonOptions) {
1213   {
1214     content::WindowedNotificationObserver observer(
1215         content::NOTIFICATION_LOAD_STOP,
1216         content::NotificationService::AllSources());
1217     chrome::ShowSettings(browser());
1218     observer.Wait();
1219   }
1220   {
1221     content::WindowedNotificationObserver observer(
1222         content::NOTIFICATION_LOAD_STOP,
1223         content::NotificationService::AllSources());
1224     chrome::AddSelectedTabWithURL(browser(), GetGoogleURL(),
1225                                   ui::PAGE_TRANSITION_LINK);
1226     observer.Wait();
1227   }
1228
1229   // This load should simply cause a tab switch.
1230   chrome::ShowSettings(browser());
1231
1232   EXPECT_EQ(2, browser()->tab_strip_model()->count());
1233   EXPECT_EQ(GetSettingsURL(),
1234             ShortenUberURL(browser()->tab_strip_model()->
1235                 GetActiveWebContents()->GetURL()));
1236 }
1237
1238 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, CloseSingletonTab) {
1239   for (int i = 0; i < 2; ++i) {
1240     content::WindowedNotificationObserver observer(
1241         content::NOTIFICATION_LOAD_STOP,
1242         content::NotificationService::AllSources());
1243     chrome::AddSelectedTabWithURL(browser(), GetGoogleURL(),
1244                                   ui::PAGE_TRANSITION_TYPED);
1245     observer.Wait();
1246   }
1247
1248   browser()->tab_strip_model()->ActivateTabAt(0, true);
1249
1250   {
1251     content::WindowedNotificationObserver observer(
1252         content::NOTIFICATION_LOAD_STOP,
1253         content::NotificationService::AllSources());
1254     chrome::ShowSettings(browser());
1255     observer.Wait();
1256   }
1257
1258   EXPECT_TRUE(browser()->tab_strip_model()->CloseWebContentsAt(
1259       2, TabStripModel::CLOSE_USER_GESTURE));
1260   EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
1261 }
1262
1263 // TODO(csilv): Update this for uber page. http://crbug.com/111579.
1264 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1265                        DISABLED_NavigateFromDefaultToHistoryInSameTab) {
1266   {
1267     content::WindowedNotificationObserver observer(
1268         content::NOTIFICATION_LOAD_STOP,
1269         content::NotificationService::AllSources());
1270     chrome::ShowHistory(browser());
1271     observer.Wait();
1272   }
1273   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1274   EXPECT_EQ(GURL(chrome::kChromeUIHistoryFrameURL),
1275             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1276 }
1277
1278 // TODO(linux_aura) http://crbug.com/163931
1279 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
1280 #define MAYBE_NavigateFromDefaultToBookmarksInSameTab DISABLED_NavigateFromDefaultToBookmarksInSameTab
1281 #else
1282 #define MAYBE_NavigateFromDefaultToBookmarksInSameTab NavigateFromDefaultToBookmarksInSameTab
1283 #endif
1284 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1285                        MAYBE_NavigateFromDefaultToBookmarksInSameTab) {
1286   {
1287     content::WindowedNotificationObserver observer(
1288         content::NOTIFICATION_LOAD_STOP,
1289         content::NotificationService::AllSources());
1290     chrome::ShowBookmarkManager(browser());
1291     observer.Wait();
1292   }
1293   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1294   EXPECT_TRUE(StartsWithASCII(
1295       browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec(),
1296       chrome::kChromeUIBookmarksURL,
1297       true));
1298 }
1299
1300 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1301                        NavigateFromDefaultToDownloadsInSameTab) {
1302   {
1303     content::WindowedNotificationObserver observer(
1304         content::NOTIFICATION_LOAD_STOP,
1305         content::NotificationService::AllSources());
1306     chrome::ShowDownloads(browser());
1307     observer.Wait();
1308   }
1309   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1310   EXPECT_EQ(GURL(chrome::kChromeUIDownloadsURL),
1311             browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
1312 }
1313
1314 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1315                        NavigateWithoutBrowser) {
1316   // First navigate using the profile of the existing browser window, and
1317   // check that the window is reused.
1318   chrome::NavigateParams params(browser()->profile(), GetGoogleURL(),
1319                                 ui::PAGE_TRANSITION_LINK);
1320   ui_test_utils::NavigateToURL(&params);
1321   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1322
1323   // Now navigate using the incognito profile and check that a new window
1324   // is created.
1325   chrome::NavigateParams params_incognito(
1326       browser()->profile()->GetOffTheRecordProfile(),
1327       GetGoogleURL(), ui::PAGE_TRANSITION_LINK);
1328   ui_test_utils::NavigateToURL(&params_incognito);
1329   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1330 }
1331
1332 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, ViewSourceIsntSingleton) {
1333   const std::string viewsource_ntp_url =
1334       std::string(content::kViewSourceScheme) + ":" +
1335       chrome::kChromeUIVersionURL;
1336
1337   chrome::NavigateParams viewsource_params(browser(),
1338                                            GURL(viewsource_ntp_url),
1339                                            ui::PAGE_TRANSITION_LINK);
1340   ui_test_utils::NavigateToURL(&viewsource_params);
1341
1342   chrome::NavigateParams singleton_params(browser(),
1343                                           GURL(chrome::kChromeUIVersionURL),
1344                                           ui::PAGE_TRANSITION_LINK);
1345   singleton_params.disposition = SINGLETON_TAB;
1346   EXPECT_EQ(-1, chrome::GetIndexOfSingletonTab(&singleton_params));
1347 }
1348
1349 // This test verifies that browser initiated navigations can send requests
1350 // using POST.
1351 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1352                        SendBrowserInitiatedRequestUsingPOST) {
1353   // Uses a test sever to verify POST request.
1354   ASSERT_TRUE(test_server()->Start());
1355
1356   // Open a browser initiated POST request in new foreground tab.
1357   base::string16 expected_title(base::ASCIIToUTF16(kExpectedTitle));
1358   std::string post_data = kExpectedTitle;
1359   base::string16 title;
1360   ASSERT_TRUE(OpenPOSTURLInNewForegroundTabAndGetTitle(
1361       test_server()->GetURL(kEchoTitleCommand), post_data, true, &title));
1362   EXPECT_EQ(expected_title, title);
1363 }
1364
1365 // This test verifies that renderer initiated navigations can NOT send requests
1366 // using POST.
1367 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1368                        SendRendererInitiatedRequestUsingPOST) {
1369   // Uses a test sever to verify POST request.
1370   ASSERT_TRUE(test_server()->Start());
1371
1372   // Open a renderer initiated POST request in new foreground tab.
1373   base::string16 expected_title(base::ASCIIToUTF16(kExpectedTitle));
1374   std::string post_data = kExpectedTitle;
1375   base::string16 title;
1376   ASSERT_TRUE(OpenPOSTURLInNewForegroundTabAndGetTitle(
1377       test_server()->GetURL(kEchoTitleCommand), post_data, false, &title));
1378   EXPECT_NE(expected_title, title);
1379 }
1380
1381 // This test navigates to a data URL that contains BiDi control
1382 // characters. For security reasons, BiDi control chars should always be
1383 // escaped in the URL but they should be unescaped in the loaded HTML.
1384 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest,
1385                        NavigateToDataURLWithBiDiControlChars) {
1386   // Text in Arabic.
1387   std::string text = "\xD8\xA7\xD8\xAE\xD8\xAA\xD8\xA8\xD8\xA7\xD8\xB1";
1388   // Page title starts with RTL mark.
1389   std::string unescaped_title = "\xE2\x80\x8F" + text;
1390   std::string data_url = "data:text/html;charset=utf-8,<html><title>" +
1391                          unescaped_title + "</title></html>";
1392   // BiDi control chars in URLs are always escaped, so the expected URL should
1393   // have the title with the escaped RTL mark.
1394   std::string escaped_title = "%E2%80%8F" + text;
1395   std::string expected_url = "data:text/html;charset=utf-8,<html><title>" +
1396                              escaped_title + "</title></html>";
1397
1398   // Navigate to the page.
1399   chrome::NavigateParams params(MakeNavigateParams());
1400   params.disposition = NEW_FOREGROUND_TAB;
1401   params.url = GURL(data_url);
1402   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
1403   ui_test_utils::NavigateToURL(&params);
1404
1405   base::string16 expected_title(base::UTF8ToUTF16(unescaped_title));
1406   EXPECT_TRUE(params.target_contents);
1407   EXPECT_EQ(expected_title, params.target_contents->GetTitle());
1408   // GURL always keeps non-ASCII characters escaped, but check them anyways.
1409   EXPECT_EQ(GURL(expected_url).spec(), params.target_contents->GetURL().spec());
1410   // Check the omnibox text. It should have escaped RTL with unescaped text.
1411   LocationBar* location_bar = browser()->window()->GetLocationBar();
1412   OmniboxView* omnibox_view = location_bar->GetOmniboxView();
1413   EXPECT_EQ(base::UTF8ToUTF16(expected_url), omnibox_view->GetText());
1414 }
1415
1416 }  // namespace