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