[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
[email protected] | bb89e748 | 2010-11-17 18:27:04 | [diff] [blame] | 5 | #include "base/command_line.h" |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 6 | #include "chrome/browser/profile.h" |
| 7 | #include "chrome/browser/tab_contents/tab_contents.h" |
| 8 | #include "chrome/browser/tab_contents/tab_contents_view.h" |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 9 | #include "chrome/browser/tab_contents_wrapper.h" |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 10 | #include "chrome/browser/tabs/tab_strip_model.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser.h" |
[email protected] | bb89e748 | 2010-11-17 18:27:04 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 339d6dd | 2010-11-12 00:41:58 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser_navigator.h" |
[email protected] | bb89e748 | 2010-11-17 18:27:04 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser_window.h" |
| 15 | #include "chrome/common/chrome_switches.h" |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 16 | #include "chrome/test/in_process_browser_test.h" |
| 17 | #include "chrome/test/ui_test_utils.h" |
| 18 | #include "ipc/ipc_message.h" |
| 19 | |
| 20 | namespace { |
| 21 | |
[email protected] | fa7ebe0 | 2010-11-29 23:04:57 | [diff] [blame^] | 22 | class BrowserNavigatorTest : public InProcessBrowserTest, |
| 23 | public NotificationObserver { |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 24 | protected: |
| 25 | GURL GetGoogleURL() const { |
| 26 | return GURL("http://www.google.com/"); |
| 27 | } |
| 28 | |
| 29 | browser::NavigateParams MakeNavigateParams() const { |
| 30 | return MakeNavigateParams(browser()); |
| 31 | } |
| 32 | browser::NavigateParams MakeNavigateParams(Browser* browser) const { |
| 33 | browser::NavigateParams params(browser, GetGoogleURL(), |
| 34 | PageTransition::LINK); |
| 35 | params.show_window = true; |
| 36 | return params; |
| 37 | } |
| 38 | |
| 39 | Browser* CreateEmptyBrowserForType(Browser::Type type, Profile* profile) { |
| 40 | Browser* browser = Browser::CreateForType(type, profile); |
| 41 | browser->AddBlankTab(true); |
| 42 | return browser; |
| 43 | } |
| 44 | |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 45 | TabContentsWrapper* CreateTabContents() { |
| 46 | return Browser::TabContentsFactory( |
| 47 | browser()->profile(), |
| 48 | NULL, |
| 49 | MSG_ROUTING_NONE, |
| 50 | browser()->GetSelectedTabContents(), |
| 51 | NULL); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void RunSuppressTest(WindowOpenDisposition disposition) { |
| 55 | GURL old_url = browser()->GetSelectedTabContents()->GetURL(); |
| 56 | browser::NavigateParams p(MakeNavigateParams()); |
| 57 | p.disposition = disposition; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 58 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 59 | |
| 60 | // Nothing should have happened as a result of Navigate(); |
| 61 | EXPECT_EQ(1, browser()->tab_count()); |
| 62 | EXPECT_EQ(1u, BrowserList::size()); |
| 63 | EXPECT_EQ(old_url, browser()->GetSelectedTabContents()->GetURL()); |
| 64 | } |
[email protected] | bb89e748 | 2010-11-17 18:27:04 | [diff] [blame] | 65 | |
| 66 | // TODO(jhawkins): Remove once tabbed options are enabled by default. |
| 67 | virtual void SetUpCommandLine(CommandLine* command_line) { |
| 68 | command_line->AppendSwitch(switches::kEnableTabbedOptions); |
| 69 | } |
[email protected] | fa7ebe0 | 2010-11-29 23:04:57 | [diff] [blame^] | 70 | |
| 71 | void Observe(NotificationType type, const NotificationSource& source, |
| 72 | const NotificationDetails& details) { |
| 73 | switch (type.value) { |
| 74 | case NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB: { |
| 75 | ++this->created_tab_contents_count_; |
| 76 | break; |
| 77 | } |
| 78 | default: |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | size_t created_tab_contents_count_; |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 84 | }; |
| 85 | |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 86 | // This test verifies that when a navigation occurs within a tab, the tab count |
| 87 | // of the Browser remains the same and the current tab bears the loaded URL. |
| 88 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_CurrentTab) { |
| 89 | browser::NavigateParams p(MakeNavigateParams()); |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 90 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 91 | ui_test_utils::WaitForNavigationInCurrentTab(browser()); |
| 92 | EXPECT_EQ(GetGoogleURL(), browser()->GetSelectedTabContents()->GetURL()); |
| 93 | // We should have one window with one tab. |
| 94 | EXPECT_EQ(1u, BrowserList::size()); |
| 95 | EXPECT_EQ(1, browser()->tab_count()); |
| 96 | } |
| 97 | |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 98 | // This test verifies that a singleton tab is refocused if one is already open |
| 99 | // in another or an existing window, or added if it is not. |
| 100 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_SingletonTabExisting) { |
| 101 | GURL url("http://www.google.com/"); |
| 102 | GURL singleton_url1("http://maps.google.com/"); |
[email protected] | fa7ebe0 | 2010-11-29 23:04:57 | [diff] [blame^] | 103 | |
| 104 | // Register for a notification if an additional tab_contents was instantiated. |
| 105 | // Opening a Singleton tab that is already open should not be opening a new |
| 106 | // tab nor be creating a new TabContents object |
| 107 | NotificationRegistrar registrar; |
| 108 | |
| 109 | // As the registrar object goes out of scope, this will get unregistered |
| 110 | registrar.Add(this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, |
| 111 | NotificationService::AllSources()); |
| 112 | |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 113 | browser()->AddSelectedTabWithURL(singleton_url1, PageTransition::LINK); |
| 114 | browser()->AddSelectedTabWithURL(url, PageTransition::LINK); |
| 115 | |
| 116 | // We should have one browser with 3 tabs, the 3rd selected. |
| 117 | EXPECT_EQ(1u, BrowserList::size()); |
| 118 | EXPECT_EQ(2, browser()->selected_index()); |
| 119 | |
[email protected] | fa7ebe0 | 2010-11-29 23:04:57 | [diff] [blame^] | 120 | unsigned int previous_tab_contents_count = |
| 121 | created_tab_contents_count_ = 0; |
| 122 | |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 123 | // Navigate to singleton_url1. |
| 124 | browser::NavigateParams p(MakeNavigateParams()); |
| 125 | p.disposition = SINGLETON_TAB; |
| 126 | p.url = singleton_url1; |
| 127 | browser::Navigate(&p); |
| 128 | |
| 129 | // The middle tab should now be selected. |
| 130 | EXPECT_EQ(browser(), p.browser); |
| 131 | EXPECT_EQ(1, browser()->selected_index()); |
[email protected] | fa7ebe0 | 2010-11-29 23:04:57 | [diff] [blame^] | 132 | |
| 133 | // No tab contents should have been created |
| 134 | EXPECT_EQ(previous_tab_contents_count, |
| 135 | created_tab_contents_count_); |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 139 | Disposition_SingletonTabNoneExisting) { |
| 140 | GURL url("http://www.google.com/"); |
| 141 | GURL singleton_url1("http://maps.google.com/"); |
| 142 | |
| 143 | // We should have one browser with 3 tabs, the 3rd selected. |
| 144 | EXPECT_EQ(1u, BrowserList::size()); |
| 145 | EXPECT_EQ(0, browser()->selected_index()); |
| 146 | |
| 147 | // Navigate to singleton_url1. |
| 148 | browser::NavigateParams p(MakeNavigateParams()); |
| 149 | p.disposition = SINGLETON_TAB; |
| 150 | p.url = singleton_url1; |
| 151 | browser::Navigate(&p); |
| 152 | |
| 153 | // We should now have 2 tabs, the 2nd one selected. |
| 154 | EXPECT_EQ(browser(), p.browser); |
| 155 | EXPECT_EQ(2, browser()->tab_count()); |
| 156 | EXPECT_EQ(1, browser()->selected_index()); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // This test verifies that when a navigation results in a foreground tab, the |
| 160 | // tab count of the Browser increases and the selected tab shifts to the new |
| 161 | // foreground tab. |
| 162 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewForegroundTab) { |
| 163 | TabContents* old_contents = browser()->GetSelectedTabContents(); |
| 164 | browser::NavigateParams p(MakeNavigateParams()); |
| 165 | p.disposition = NEW_FOREGROUND_TAB; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 166 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 167 | EXPECT_NE(old_contents, browser()->GetSelectedTabContents()); |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 168 | EXPECT_EQ(browser()->GetSelectedTabContentsWrapper(), p.target_contents); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 169 | EXPECT_EQ(2, browser()->tab_count()); |
| 170 | } |
| 171 | |
| 172 | // This test verifies that when a navigation results in a background tab, the |
| 173 | // tab count of the Browser increases but the selected tab remains the same. |
| 174 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewBackgroundTab) { |
| 175 | TabContents* old_contents = browser()->GetSelectedTabContents(); |
| 176 | browser::NavigateParams p(MakeNavigateParams()); |
| 177 | p.disposition = NEW_BACKGROUND_TAB; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 178 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 179 | TabContents* new_contents = browser()->GetSelectedTabContents(); |
| 180 | // The selected tab should have remained unchanged, since the new tab was |
| 181 | // opened in the background. |
| 182 | EXPECT_EQ(old_contents, new_contents); |
| 183 | EXPECT_EQ(2, browser()->tab_count()); |
| 184 | } |
| 185 | |
| 186 | // This test verifies that when a navigation requiring a new foreground tab |
| 187 | // occurs in a Browser that cannot host multiple tabs, the new foreground tab |
| 188 | // is created in an existing compatible Browser. |
| 189 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 190 | Disposition_IncompatibleWindow_Existing) { |
| 191 | // Open a foreground tab in a window that cannot open popups when there is an |
| 192 | // existing compatible window somewhere else that they can be opened within. |
| 193 | Browser* popup = CreateEmptyBrowserForType(Browser::TYPE_POPUP, |
| 194 | browser()->profile()); |
| 195 | browser::NavigateParams p(MakeNavigateParams(popup)); |
| 196 | p.disposition = NEW_FOREGROUND_TAB; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 197 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 198 | |
| 199 | // Navigate() should have opened the tab in a different browser since the |
| 200 | // one we supplied didn't support additional tabs. |
| 201 | EXPECT_NE(popup, p.browser); |
| 202 | |
| 203 | // Since browser() is an existing compatible tabbed browser, it should have |
| 204 | // opened the tab there. |
| 205 | EXPECT_EQ(browser(), p.browser); |
| 206 | |
| 207 | // We should be left with 2 windows, the popup with one tab and the browser() |
| 208 | // provided by the framework with two. |
| 209 | EXPECT_EQ(2u, BrowserList::size()); |
| 210 | EXPECT_EQ(1, popup->tab_count()); |
| 211 | EXPECT_EQ(2, browser()->tab_count()); |
| 212 | } |
| 213 | |
| 214 | // This test verifies that when a navigation requiring a new foreground tab |
| 215 | // occurs in a Browser that cannot host multiple tabs and no compatible Browser |
| 216 | // that can is open, a compatible Browser is created. |
| 217 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 218 | Disposition_IncompatibleWindow_NoExisting) { |
| 219 | // We want to simulate not being able to find an existing window compatible |
| 220 | // with our non-tabbed browser window so Navigate() is forced to create a |
| 221 | // new compatible window. Because browser() supplied by the in-process |
| 222 | // browser testing framework is compatible with browser()->profile(), we |
| 223 | // need a different profile, and creating a popup window with an incognito |
| 224 | // profile is a quick and dirty way of achieving this. |
| 225 | Browser* popup = CreateEmptyBrowserForType( |
| 226 | Browser::TYPE_POPUP, browser()->profile()->GetOffTheRecordProfile()); |
| 227 | browser::NavigateParams p(MakeNavigateParams(popup)); |
| 228 | p.disposition = NEW_FOREGROUND_TAB; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 229 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 230 | |
| 231 | // Navigate() should have opened the tab in a different browser since the |
| 232 | // one we supplied didn't support additional tabs. |
| 233 | EXPECT_NE(popup, p.browser); |
| 234 | |
| 235 | // This time, browser() is _not_ compatible with popup since it is not an |
| 236 | // incognito window. |
| 237 | EXPECT_NE(browser(), p.browser); |
| 238 | |
| 239 | // We should have three windows, each with one tab: |
| 240 | // 1. the browser() provided by the framework (unchanged in this test) |
| 241 | // 2. the incognito popup we created originally |
| 242 | // 3. the new incognito tabbed browser that was created by Navigate(). |
| 243 | EXPECT_EQ(3u, BrowserList::size()); |
| 244 | EXPECT_EQ(1, browser()->tab_count()); |
| 245 | EXPECT_EQ(1, popup->tab_count()); |
| 246 | EXPECT_EQ(1, p.browser->tab_count()); |
| 247 | EXPECT_EQ(Browser::TYPE_NORMAL, p.browser->type()); |
| 248 | } |
| 249 | |
| 250 | // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP |
| 251 | // from a normal Browser results in a new Browser with TYPE_POPUP. |
| 252 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewPopup) { |
| 253 | browser::NavigateParams p(MakeNavigateParams()); |
| 254 | p.disposition = NEW_POPUP; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 255 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 256 | |
| 257 | // Navigate() should have opened a new popup window. |
| 258 | EXPECT_NE(browser(), p.browser); |
| 259 | EXPECT_EQ(Browser::TYPE_POPUP, p.browser->type()); |
| 260 | |
| 261 | // We should have two windows, the browser() provided by the framework and the |
| 262 | // new popup window. |
| 263 | EXPECT_EQ(2u, BrowserList::size()); |
| 264 | EXPECT_EQ(1, browser()->tab_count()); |
| 265 | EXPECT_EQ(1, p.browser->tab_count()); |
| 266 | } |
| 267 | |
| 268 | // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP |
| 269 | // from an app frame results in a new Browser with TYPE_APP_POPUP. |
| 270 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 271 | Disposition_NewPopupFromAppWindow) { |
| 272 | Browser* app_browser = CreateEmptyBrowserForType(Browser::TYPE_APP, |
| 273 | browser()->profile()); |
| 274 | browser::NavigateParams p(MakeNavigateParams(app_browser)); |
| 275 | p.disposition = NEW_POPUP; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 276 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 277 | |
| 278 | // Navigate() should have opened a new popup app window. |
| 279 | EXPECT_NE(app_browser, p.browser); |
| 280 | EXPECT_NE(browser(), p.browser); |
| 281 | EXPECT_EQ(Browser::TYPE_APP_POPUP, p.browser->type()); |
| 282 | |
| 283 | // We should now have three windows, the app window, the app popup it created, |
| 284 | // and the original browser() provided by the framework. |
| 285 | EXPECT_EQ(3u, BrowserList::size()); |
| 286 | EXPECT_EQ(1, browser()->tab_count()); |
| 287 | EXPECT_EQ(1, app_browser->tab_count()); |
| 288 | EXPECT_EQ(1, p.browser->tab_count()); |
| 289 | } |
| 290 | |
| 291 | // This test verifies that navigating with WindowOpenDisposition = NEW_POPUP |
| 292 | // from an extension app tab results in a new Browser with TYPE_APP_POPUP. |
| 293 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 294 | Disposition_NewPopupFromExtensionApp) { |
| 295 | // TODO(beng): TBD. |
| 296 | } |
| 297 | |
| 298 | // This test verifies that navigating with WindowOpenDisposition = NEW_WINDOW |
| 299 | // always opens a new window. |
| 300 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_NewWindow) { |
| 301 | browser::NavigateParams p(MakeNavigateParams()); |
| 302 | p.disposition = NEW_WINDOW; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 303 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 304 | |
| 305 | // Navigate() should have opened a new toplevel window. |
| 306 | EXPECT_NE(browser(), p.browser); |
| 307 | EXPECT_EQ(Browser::TYPE_NORMAL, p.browser->type()); |
| 308 | |
| 309 | // We should now have two windows, the browser() provided by the framework and |
| 310 | // the new normal window. |
| 311 | EXPECT_EQ(2u, BrowserList::size()); |
| 312 | EXPECT_EQ(1, browser()->tab_count()); |
| 313 | EXPECT_EQ(1, p.browser->tab_count()); |
| 314 | } |
| 315 | |
| 316 | // This test verifies that navigating with WindowOpenDisposition = INCOGNITO |
| 317 | // opens a new incognito window if no existing incognito window is present. |
| 318 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_Incognito) { |
| 319 | browser::NavigateParams p(MakeNavigateParams()); |
| 320 | p.disposition = OFF_THE_RECORD; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 321 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 322 | |
| 323 | // Navigate() should have opened a new toplevel incognito window. |
| 324 | EXPECT_NE(browser(), p.browser); |
| 325 | EXPECT_EQ(browser()->profile()->GetOffTheRecordProfile(), |
| 326 | p.browser->profile()); |
| 327 | |
| 328 | // We should now have two windows, the browser() provided by the framework and |
| 329 | // the new incognito window. |
| 330 | EXPECT_EQ(2u, BrowserList::size()); |
| 331 | EXPECT_EQ(1, browser()->tab_count()); |
| 332 | EXPECT_EQ(1, p.browser->tab_count()); |
| 333 | } |
| 334 | |
| 335 | // This test verifies that navigating with WindowOpenDisposition = INCOGNITO |
| 336 | // reuses an existing incognito window when possible. |
| 337 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_IncognitoRefocus) { |
| 338 | Browser* incognito_browser = |
| 339 | CreateEmptyBrowserForType(Browser::TYPE_NORMAL, |
| 340 | browser()->profile()->GetOffTheRecordProfile()); |
| 341 | browser::NavigateParams p(MakeNavigateParams()); |
| 342 | p.disposition = OFF_THE_RECORD; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 343 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 344 | |
| 345 | // Navigate() should have opened a new tab in the existing incognito window. |
| 346 | EXPECT_NE(browser(), p.browser); |
| 347 | EXPECT_EQ(p.browser, incognito_browser); |
| 348 | |
| 349 | // We should now have two windows, the browser() provided by the framework and |
| 350 | // the incognito window we opened earlier. |
| 351 | EXPECT_EQ(2u, BrowserList::size()); |
| 352 | EXPECT_EQ(1, browser()->tab_count()); |
| 353 | EXPECT_EQ(2, incognito_browser->tab_count()); |
| 354 | } |
| 355 | |
| 356 | // This test verifies that no navigation action occurs when |
| 357 | // WindowOpenDisposition = SUPPRESS_OPEN. |
| 358 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_SuppressOpen) { |
| 359 | RunSuppressTest(SUPPRESS_OPEN); |
| 360 | } |
| 361 | |
| 362 | // This test verifies that no navigation action occurs when |
| 363 | // WindowOpenDisposition = SAVE_TO_DISK. |
| 364 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_SaveToDisk) { |
| 365 | RunSuppressTest(SAVE_TO_DISK); |
| 366 | } |
| 367 | |
| 368 | // This test verifies that no navigation action occurs when |
| 369 | // WindowOpenDisposition = IGNORE_ACTION. |
| 370 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_IgnoreAction) { |
| 371 | RunSuppressTest(IGNORE_ACTION); |
| 372 | } |
| 373 | |
| 374 | // This tests adding a foreground tab with a predefined TabContents. |
| 375 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, TargetContents_ForegroundTab) { |
| 376 | browser::NavigateParams p(MakeNavigateParams()); |
| 377 | p.disposition = NEW_FOREGROUND_TAB; |
| 378 | p.target_contents = CreateTabContents(); |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 379 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 380 | |
| 381 | // Navigate() should have opened the contents in a new foreground in the |
| 382 | // current Browser. |
| 383 | EXPECT_EQ(browser(), p.browser); |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 384 | EXPECT_EQ(browser()->GetSelectedTabContentsWrapper(), p.target_contents); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 385 | |
| 386 | // We should have one window, with two tabs. |
| 387 | EXPECT_EQ(1u, BrowserList::size()); |
| 388 | EXPECT_EQ(2, browser()->tab_count()); |
| 389 | } |
| 390 | |
| 391 | #if defined(OS_WIN) |
| 392 | // This tests adding a popup with a predefined TabContents. |
| 393 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, TargetContents_Popup) { |
| 394 | browser::NavigateParams p(MakeNavigateParams()); |
| 395 | p.disposition = NEW_POPUP; |
| 396 | p.target_contents = CreateTabContents(); |
| 397 | p.window_bounds = gfx::Rect(10, 10, 500, 500); |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 398 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 399 | |
| 400 | // Navigate() should have opened a new popup window. |
| 401 | EXPECT_NE(browser(), p.browser); |
| 402 | EXPECT_EQ(Browser::TYPE_POPUP, p.browser->type()); |
| 403 | |
| 404 | // The web platform is weird. The window bounds specified in |
| 405 | // |p.window_bounds| are used as follows: |
| 406 | // - the origin is used to position the window |
| 407 | // - the size is used to size the TabContents of the window. |
| 408 | // As such the position of the resulting window will always match |
| 409 | // p.window_bounds.origin(), but its size will not. We need to match |
| 410 | // the size against the selected tab's view's container size. |
| 411 | // Only Windows positions the window according to |p.window_bounds.origin()| - |
| 412 | // on Mac the window is offset from the opener and on Linux it always opens |
| 413 | // at 0,0. |
| 414 | EXPECT_EQ(p.window_bounds.origin(), |
| 415 | p.browser->window()->GetRestoredBounds().origin()); |
| 416 | // All platforms should respect size however provided width > 400 (Mac has a |
| 417 | // minimum window width of 400). |
| 418 | EXPECT_EQ(p.window_bounds.size(), |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 419 | p.target_contents->tab_contents()->view()->GetContainerSize()); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 420 | |
| 421 | // We should have two windows, the new popup and the browser() provided by the |
| 422 | // framework. |
| 423 | EXPECT_EQ(2u, BrowserList::size()); |
| 424 | EXPECT_EQ(1, browser()->tab_count()); |
| 425 | EXPECT_EQ(1, p.browser->tab_count()); |
| 426 | } |
| 427 | #endif |
| 428 | |
| 429 | // This tests adding a tab at a specific index. |
| 430 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Tabstrip_InsertAtIndex) { |
| 431 | // This is not meant to be a comprehensive test of whether or not the tab |
| 432 | // implementation of the browser observes the insertion index. That is |
| 433 | // covered by the unit tests for TabStripModel. This merely verifies that |
| 434 | // insertion index preference is reflected in common cases. |
| 435 | browser::NavigateParams p(MakeNavigateParams()); |
| 436 | p.disposition = NEW_FOREGROUND_TAB; |
| 437 | p.tabstrip_index = 0; |
| 438 | p.tabstrip_add_types = TabStripModel::ADD_FORCE_INDEX; |
[email protected] | 19d9f3a | 2010-10-14 21:49:36 | [diff] [blame] | 439 | browser::Navigate(&p); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 440 | |
| 441 | // Navigate() should have inserted a new tab at slot 0 in the tabstrip. |
| 442 | EXPECT_EQ(browser(), p.browser); |
| 443 | EXPECT_EQ(0, browser()->tabstrip_model()->GetIndexOfTabContents( |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 444 | static_cast<const TabContentsWrapper*>(p.target_contents))); |
[email protected] | a1feae5 | 2010-10-11 22:14:45 | [diff] [blame] | 445 | |
| 446 | // We should have one window - the browser() provided by the framework. |
| 447 | EXPECT_EQ(1u, BrowserList::size()); |
| 448 | EXPECT_EQ(2, browser()->tab_count()); |
| 449 | } |
| 450 | |
[email protected] | 2dd8548 | 2010-11-06 01:56:47 | [diff] [blame] | 451 | // This test verifies that constructing params with a NULL browser has |
| 452 | // the same result as navigating to a new foreground tab in the (only) |
| 453 | // active browser. Tests are the same as for Disposition_NewForegroundTab. |
| 454 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, NullBrowser_NewForegroundTab) { |
| 455 | TabContents* old_contents = browser()->GetSelectedTabContents(); |
| 456 | // Navigate with a NULL browser. |
| 457 | browser::NavigateParams p(MakeNavigateParams(NULL)); |
| 458 | p.disposition = NEW_FOREGROUND_TAB; |
| 459 | p.profile = browser()->profile(); |
| 460 | browser::Navigate(&p); |
| 461 | |
| 462 | // Navigate() should have found browser() and create a new tab. |
| 463 | EXPECT_EQ(browser(), p.browser); |
| 464 | EXPECT_NE(old_contents, browser()->GetSelectedTabContents()); |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 465 | EXPECT_EQ(browser()->GetSelectedTabContentsWrapper(), p.target_contents); |
[email protected] | 2dd8548 | 2010-11-06 01:56:47 | [diff] [blame] | 466 | EXPECT_EQ(2, browser()->tab_count()); |
| 467 | } |
| 468 | |
| 469 | // This test verifies that constructing params with a NULL browser and |
| 470 | // a specific profile matches the specified profile. |
| 471 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, NullBrowser_MatchProfile) { |
| 472 | // Create a new browser with using the incognito profile. |
| 473 | Browser* incognito = |
| 474 | Browser::Create(browser()->profile()->GetOffTheRecordProfile()); |
| 475 | |
| 476 | // Navigate with a NULL browser and the incognito profile. |
| 477 | browser::NavigateParams p(MakeNavigateParams(NULL)); |
| 478 | p.disposition = NEW_FOREGROUND_TAB; |
| 479 | p.profile = incognito->profile(); |
| 480 | browser::Navigate(&p); |
| 481 | |
| 482 | // Navigate() should have found incognito, not browser(). |
| 483 | EXPECT_EQ(incognito, p.browser); |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 484 | EXPECT_EQ(incognito->GetSelectedTabContentsWrapper(), p.target_contents); |
[email protected] | 2dd8548 | 2010-11-06 01:56:47 | [diff] [blame] | 485 | EXPECT_EQ(1, incognito->tab_count()); |
| 486 | } |
| 487 | |
| 488 | // This test verifies that constructing params with a NULL browser and |
| 489 | // disposition = NEW_WINDOW always opens exactly one new window. |
| 490 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, NullBrowser_NewWindow) { |
| 491 | browser::NavigateParams p(MakeNavigateParams(NULL)); |
| 492 | p.disposition = NEW_WINDOW; |
| 493 | p.profile = browser()->profile(); |
| 494 | browser::Navigate(&p); |
| 495 | |
| 496 | // Navigate() should have created a new browser. |
| 497 | EXPECT_NE(browser(), p.browser); |
| 498 | EXPECT_EQ(Browser::TYPE_NORMAL, p.browser->type()); |
| 499 | |
| 500 | // We should now have two windows, the browser() provided by the framework and |
| 501 | // the new normal window. |
| 502 | EXPECT_EQ(2u, BrowserList::size()); |
| 503 | EXPECT_EQ(1, browser()->tab_count()); |
| 504 | EXPECT_EQ(1, p.browser->tab_count()); |
| 505 | } |
| 506 | |
[email protected] | bb89e748 | 2010-11-17 18:27:04 | [diff] [blame] | 507 | // This test verifies that constructing params with disposition = SINGLETON_TAB |
| 508 | // and |ignore_paths| = true opens a new tab navigated to the specified URL if no |
| 509 | // previous tab with that URL (minus the path) exists. |
| 510 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 511 | Disposition_SingletonTabNew_IgnorePath) { |
| 512 | GURL url("http://www.google.com/"); |
| 513 | browser()->AddSelectedTabWithURL(url, PageTransition::LINK); |
| 514 | |
| 515 | // We should have one browser with 2 tabs, the 2nd selected. |
| 516 | EXPECT_EQ(1u, BrowserList::size()); |
| 517 | EXPECT_EQ(2, browser()->tab_count()); |
| 518 | EXPECT_EQ(1, browser()->selected_index()); |
| 519 | |
| 520 | // Navigate to a new singleton tab with a sub-page. |
| 521 | browser::NavigateParams p(MakeNavigateParams()); |
| 522 | p.disposition = SINGLETON_TAB; |
| 523 | p.url = GURL("chrome://settings/advanced"); |
| 524 | p.show_window = true; |
| 525 | p.ignore_path = true; |
| 526 | browser::Navigate(&p); |
| 527 | |
| 528 | // The last tab should now be selected and navigated to the sub-page of the |
| 529 | // URL. |
| 530 | EXPECT_EQ(browser(), p.browser); |
| 531 | EXPECT_EQ(3, browser()->tab_count()); |
| 532 | EXPECT_EQ(2, browser()->selected_index()); |
| 533 | EXPECT_EQ(GURL("chrome://settings/advanced"), |
| 534 | browser()->GetSelectedTabContents()->GetURL()); |
| 535 | } |
| 536 | |
| 537 | // This test verifies that constructing params with disposition = SINGLETON_TAB |
| 538 | // and |ignore_paths| = true opens an existing tab with the matching URL (minus |
| 539 | // the path) which is navigated to the specified URL. |
| 540 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 541 | Disposition_SingletonTabExisting_IgnorePath) { |
| 542 | GURL singleton_url1("chrome://settings"); |
| 543 | GURL url("http://www.google.com/"); |
| 544 | browser()->AddSelectedTabWithURL(singleton_url1, PageTransition::LINK); |
| 545 | browser()->AddSelectedTabWithURL(url, PageTransition::LINK); |
| 546 | |
| 547 | // We should have one browser with 3 tabs, the 3rd selected. |
| 548 | EXPECT_EQ(1u, BrowserList::size()); |
| 549 | EXPECT_EQ(3, browser()->tab_count()); |
| 550 | EXPECT_EQ(2, browser()->selected_index()); |
| 551 | |
| 552 | // Navigate to singleton_url1. |
| 553 | browser::NavigateParams p(MakeNavigateParams()); |
| 554 | p.disposition = SINGLETON_TAB; |
| 555 | p.url = GURL("chrome://settings/advanced"); |
| 556 | p.show_window = true; |
| 557 | p.ignore_path = true; |
| 558 | browser::Navigate(&p); |
| 559 | |
| 560 | // The middle tab should now be selected and navigated to the sub-page of the |
| 561 | // URL. |
| 562 | EXPECT_EQ(browser(), p.browser); |
| 563 | EXPECT_EQ(3, browser()->tab_count()); |
| 564 | EXPECT_EQ(1, browser()->selected_index()); |
| 565 | EXPECT_EQ(GURL("chrome://settings/advanced"), |
| 566 | browser()->GetSelectedTabContents()->GetURL()); |
| 567 | } |
| 568 | |
| 569 | // This test verifies that constructing params with disposition = SINGLETON_TAB |
| 570 | // and |ignore_paths| = true opens an existing tab with the matching URL (minus |
| 571 | // the path) which is navigated to the specified URL. |
| 572 | IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 573 | Disposition_SingletonTabExistingSubPath_IgnorePath) { |
| 574 | GURL singleton_url1("chrome://settings/advanced"); |
| 575 | GURL url("http://www.google.com/"); |
| 576 | browser()->AddSelectedTabWithURL(singleton_url1, PageTransition::LINK); |
| 577 | browser()->AddSelectedTabWithURL(url, PageTransition::LINK); |
| 578 | |
| 579 | // We should have one browser with 3 tabs, the 3rd selected. |
| 580 | EXPECT_EQ(1u, BrowserList::size()); |
| 581 | EXPECT_EQ(3, browser()->tab_count()); |
| 582 | EXPECT_EQ(2, browser()->selected_index()); |
| 583 | |
| 584 | // Navigate to singleton_url1. |
| 585 | browser::NavigateParams p(MakeNavigateParams()); |
| 586 | p.disposition = SINGLETON_TAB; |
| 587 | p.url = GURL("chrome://settings/personal"); |
| 588 | p.show_window = true; |
| 589 | p.ignore_path = true; |
| 590 | browser::Navigate(&p); |
| 591 | |
| 592 | // The middle tab should now be selected and navigated to the sub-page of the |
| 593 | // URL. |
| 594 | EXPECT_EQ(browser(), p.browser); |
| 595 | EXPECT_EQ(3, browser()->tab_count()); |
| 596 | EXPECT_EQ(1, browser()->selected_index()); |
| 597 | EXPECT_EQ(GURL("chrome://settings/personal"), |
| 598 | browser()->GetSelectedTabContents()->GetURL()); |
| 599 | } |
[email protected] | 2dd8548 | 2010-11-06 01:56:47 | [diff] [blame] | 600 | |
[email protected] | 83de85e | 2010-10-12 23:05:16 | [diff] [blame] | 601 | } // namespace |