Home | History | Annotate | Download | only in webui
      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/browser/ui/webui/html_dialog_tab_contents_delegate.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/logging.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "chrome/browser/history/history_types.h"
     12 #include "chrome/browser/ui/browser.h"
     13 #include "chrome/browser/ui/browser_list.h"
     14 #include "chrome/common/url_constants.h"
     15 #include "chrome/test/browser_with_test_window_test.h"
     16 #include "chrome/test/test_browser_window.h"
     17 #include "chrome/test/testing_profile.h"
     18 #include "content/browser/tab_contents/test_tab_contents.h"
     19 #include "googleurl/src/gurl.h"
     20 #include "testing/gmock/include/gmock/gmock.h"
     21 #include "testing/gtest/include/gtest/gtest.h"
     22 #include "ui/gfx/rect.h"
     23 
     24 namespace {
     25 
     26 class TestTabContentsDelegate : public HtmlDialogTabContentsDelegate {
     27  public:
     28   explicit TestTabContentsDelegate(Profile* profile)
     29     : HtmlDialogTabContentsDelegate(profile) {}
     30 
     31   virtual ~TestTabContentsDelegate() {
     32   }
     33 
     34   virtual void MoveContents(TabContents* source, const gfx::Rect& pos) {}
     35 
     36  private:
     37   DISALLOW_COPY_AND_ASSIGN(TestTabContentsDelegate);
     38 };
     39 
     40 class HtmlDialogTabContentsDelegateTest : public BrowserWithTestWindowTest {
     41  public:
     42   virtual void SetUp() {
     43     BrowserWithTestWindowTest::SetUp();
     44     test_tab_contents_delegate_.reset(new TestTabContentsDelegate(profile()));
     45   }
     46 
     47   virtual void TearDown() {
     48     test_tab_contents_delegate_.reset(NULL);
     49     BrowserWithTestWindowTest::TearDown();
     50   }
     51 
     52  protected:
     53   scoped_ptr<TestTabContentsDelegate> test_tab_contents_delegate_;
     54 };
     55 
     56 TEST_F(HtmlDialogTabContentsDelegateTest, DoNothingMethodsTest) {
     57   // None of the following calls should do anything.
     58   EXPECT_TRUE(test_tab_contents_delegate_->IsPopup(NULL));
     59   scoped_refptr<history::HistoryAddPageArgs> should_add_args(
     60       new history::HistoryAddPageArgs(
     61           GURL(), base::Time::Now(), 0, 0, GURL(), history::RedirectList(),
     62           PageTransition::TYPED, history::SOURCE_SYNCED, false));
     63   EXPECT_FALSE(test_tab_contents_delegate_->ShouldAddNavigationToHistory(
     64                    *should_add_args, NavigationType::NEW_PAGE));
     65   test_tab_contents_delegate_->NavigationStateChanged(NULL, 0);
     66   test_tab_contents_delegate_->ActivateContents(NULL);
     67   test_tab_contents_delegate_->LoadingStateChanged(NULL);
     68   test_tab_contents_delegate_->CloseContents(NULL);
     69   test_tab_contents_delegate_->UpdateTargetURL(NULL, GURL());
     70   test_tab_contents_delegate_->MoveContents(NULL, gfx::Rect());
     71   EXPECT_EQ(0, browser()->tab_count());
     72   EXPECT_EQ(1U, BrowserList::size());
     73 }
     74 
     75 TEST_F(HtmlDialogTabContentsDelegateTest, OpenURLFromTabTest) {
     76   test_tab_contents_delegate_->OpenURLFromTab(
     77     NULL, GURL(chrome::kAboutBlankURL), GURL(),
     78     NEW_FOREGROUND_TAB, PageTransition::LINK);
     79   // This should create a new foreground tab in the existing browser.
     80   EXPECT_EQ(1, browser()->tab_count());
     81   EXPECT_EQ(1U, BrowserList::size());
     82 }
     83 
     84 TEST_F(HtmlDialogTabContentsDelegateTest, AddNewContentsForegroundTabTest) {
     85   TabContents* contents =
     86       new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL, NULL);
     87   test_tab_contents_delegate_->AddNewContents(
     88       NULL, contents, NEW_FOREGROUND_TAB, gfx::Rect(), false);
     89   // This should create a new foreground tab in the existing browser.
     90   EXPECT_EQ(1, browser()->tab_count());
     91   EXPECT_EQ(1U, BrowserList::size());
     92 }
     93 
     94 TEST_F(HtmlDialogTabContentsDelegateTest, DetachTest) {
     95   EXPECT_EQ(profile(), test_tab_contents_delegate_->profile());
     96   test_tab_contents_delegate_->Detach();
     97   EXPECT_EQ(NULL, test_tab_contents_delegate_->profile());
     98   // Now, none of the following calls should do anything.
     99   test_tab_contents_delegate_->OpenURLFromTab(
    100       NULL, GURL(chrome::kAboutBlankURL), GURL(),
    101       NEW_FOREGROUND_TAB, PageTransition::LINK);
    102   test_tab_contents_delegate_->AddNewContents(NULL, NULL, NEW_FOREGROUND_TAB,
    103                                               gfx::Rect(), false);
    104   EXPECT_EQ(0, browser()->tab_count());
    105   EXPECT_EQ(1U, BrowserList::size());
    106 }
    107 
    108 }  // namespace
    109