Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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/app/chrome_command_ids.h"
      6 #include "chrome/browser/bookmarks/bookmark_model.h"
      7 #include "chrome/browser/ui/browser_list.h"
      8 #include "chrome/common/url_constants.h"
      9 #include "chrome/test/browser_with_test_window_test.h"
     10 #include "chrome/test/testing_profile.h"
     11 #include "content/browser/browser_thread.h"
     12 #include "content/browser/tab_contents/navigation_controller.h"
     13 #include "content/browser/tab_contents/navigation_entry.h"
     14 #include "content/browser/tab_contents/tab_contents.h"
     15 
     16 typedef BrowserWithTestWindowTest BrowserCommandsTest;
     17 
     18 // Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and
     19 // IDC_SELECT_LAST_TAB.
     20 TEST_F(BrowserCommandsTest, TabNavigationAccelerators) {
     21   GURL about_blank(chrome::kAboutBlankURL);
     22 
     23   // Create three tabs.
     24   AddTab(browser(), about_blank);
     25   AddTab(browser(), about_blank);
     26   AddTab(browser(), about_blank);
     27 
     28   // Select the second tab.
     29   browser()->ActivateTabAt(1, false);
     30 
     31   // Navigate to the first tab using an accelerator.
     32   browser()->ExecuteCommand(IDC_SELECT_TAB_0);
     33   ASSERT_EQ(0, browser()->active_index());
     34 
     35   // Navigate to the second tab using the next accelerators.
     36   browser()->ExecuteCommand(IDC_SELECT_NEXT_TAB);
     37   ASSERT_EQ(1, browser()->active_index());
     38 
     39   // Navigate back to the first tab using the previous accelerators.
     40   browser()->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
     41   ASSERT_EQ(0, browser()->active_index());
     42 
     43   // Navigate to the last tab using the select last accelerator.
     44   browser()->ExecuteCommand(IDC_SELECT_LAST_TAB);
     45   ASSERT_EQ(2, browser()->active_index());
     46 }
     47 
     48 // Tests IDC_DUPLICATE_TAB.
     49 TEST_F(BrowserCommandsTest, DuplicateTab) {
     50   GURL url1("http://foo/1");
     51   GURL url2("http://foo/2");
     52   GURL url3("http://foo/3");
     53 
     54   // Navigate to the three urls, then go back.
     55   AddTab(browser(), url1);
     56   NavigateAndCommitActiveTab(url2);
     57   NavigateAndCommitActiveTab(url3);
     58 
     59   size_t initial_window_count = BrowserList::size();
     60 
     61   // Duplicate the tab.
     62   browser()->ExecuteCommand(IDC_DUPLICATE_TAB);
     63 
     64   // The duplicated tab should not end up in a new window.
     65   size_t window_count = BrowserList::size();
     66   ASSERT_EQ(initial_window_count, window_count);
     67 
     68   // And we should have a newly duplicated tab.
     69   ASSERT_EQ(2, browser()->tab_count());
     70 
     71   // Verify the stack of urls.
     72   NavigationController& controller =
     73       browser()->GetTabContentsAt(1)->controller();
     74   ASSERT_EQ(3, controller.entry_count());
     75   ASSERT_EQ(2, controller.GetCurrentEntryIndex());
     76   ASSERT_TRUE(url1 == controller.GetEntryAtIndex(0)->url());
     77   ASSERT_TRUE(url2 == controller.GetEntryAtIndex(1)->url());
     78   ASSERT_TRUE(url3 == controller.GetEntryAtIndex(2)->url());
     79 }
     80 
     81 TEST_F(BrowserCommandsTest, BookmarkCurrentPage) {
     82   // We use profile() here, since it's a TestingProfile.
     83   profile()->CreateBookmarkModel(true);
     84   profile()->BlockUntilBookmarkModelLoaded();
     85 
     86   // Navigate to a url.
     87   GURL url1("http://foo/1");
     88   AddTab(browser(), url1);
     89   browser()->OpenURL(url1, GURL(), CURRENT_TAB, PageTransition::TYPED);
     90 
     91   // TODO(beng): remove this once we can use TabContentses directly in testing
     92   //             instead of the TestTabContents which causes this command not to
     93   //             be enabled when the tab is added (and selected).
     94   browser()->command_updater()->UpdateCommandEnabled(IDC_BOOKMARK_PAGE, true);
     95 
     96   // Star it.
     97   browser()->ExecuteCommand(IDC_BOOKMARK_PAGE);
     98 
     99   // It should now be bookmarked in the bookmark model.
    100   EXPECT_EQ(profile(), browser()->profile());
    101   EXPECT_TRUE(browser()->profile()->GetBookmarkModel()->IsBookmarked(url1));
    102 }
    103 
    104 // Tests back/forward in new tab (Control + Back/Forward button in the UI).
    105 TEST_F(BrowserCommandsTest, BackForwardInNewTab) {
    106   GURL url1("http://foo/1");
    107   GURL url2("http://foo/2");
    108 
    109   // Make a tab with the two pages navigated in it.
    110   AddTab(browser(), url1);
    111   NavigateAndCommitActiveTab(url2);
    112 
    113   // Go back in a new background tab.
    114   browser()->GoBack(NEW_BACKGROUND_TAB);
    115   EXPECT_EQ(0, browser()->active_index());
    116   ASSERT_EQ(2, browser()->tab_count());
    117 
    118   // The original tab should be unchanged.
    119   TabContents* zeroth = browser()->GetTabContentsAt(0);
    120   EXPECT_EQ(url2, zeroth->GetURL());
    121   EXPECT_TRUE(zeroth->controller().CanGoBack());
    122   EXPECT_FALSE(zeroth->controller().CanGoForward());
    123 
    124   // The new tab should be like the first one but navigated back.
    125   TabContents* first = browser()->GetTabContentsAt(1);
    126   EXPECT_EQ(url1, browser()->GetTabContentsAt(1)->GetURL());
    127   EXPECT_FALSE(first->controller().CanGoBack());
    128   EXPECT_TRUE(first->controller().CanGoForward());
    129 
    130   // Select the second tab and make it go forward in a new background tab.
    131   browser()->ActivateTabAt(1, true);
    132   // TODO(brettw) bug 11055: It should not be necessary to commit the load here,
    133   // but because of this bug, it will assert later if we don't. When the bug is
    134   // fixed, one of the three commits here related to this bug should be removed
    135   // (to test both codepaths).
    136   CommitPendingLoad(&first->controller());
    137   EXPECT_EQ(1, browser()->active_index());
    138   browser()->GoForward(NEW_BACKGROUND_TAB);
    139 
    140   // The previous tab should be unchanged and still in the foreground.
    141   EXPECT_EQ(url1, first->GetURL());
    142   EXPECT_FALSE(first->controller().CanGoBack());
    143   EXPECT_TRUE(first->controller().CanGoForward());
    144   EXPECT_EQ(1, browser()->active_index());
    145 
    146   // There should be a new tab navigated forward.
    147   ASSERT_EQ(3, browser()->tab_count());
    148   TabContents* second = browser()->GetTabContentsAt(2);
    149   EXPECT_EQ(url2, second->GetURL());
    150   EXPECT_TRUE(second->controller().CanGoBack());
    151   EXPECT_FALSE(second->controller().CanGoForward());
    152 
    153   // Now do back in a new foreground tab. Don't bother re-checking every sngle
    154   // thing above, just validate that it's opening properly.
    155   browser()->ActivateTabAt(2, true);
    156   // TODO(brettw) bug 11055: see the comment above about why we need this.
    157   CommitPendingLoad(&second->controller());
    158   browser()->GoBack(NEW_FOREGROUND_TAB);
    159   ASSERT_EQ(3, browser()->active_index());
    160   ASSERT_EQ(url1, browser()->GetSelectedTabContents()->GetURL());
    161 
    162   // Same thing again for forward.
    163   // TODO(brettw) bug 11055: see the comment above about why we need this.
    164   CommitPendingLoad(&browser()->GetSelectedTabContents()->controller());
    165   browser()->GoForward(NEW_FOREGROUND_TAB);
    166   ASSERT_EQ(4, browser()->active_index());
    167   ASSERT_EQ(url2, browser()->GetSelectedTabContents()->GetURL());
    168 }
    169