web_dialog_web_contents_delegate_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 "ui/web_dialogs/web_dialog_web_contents_delegate.h"
6
7#include <vector>
8
9#include "base/logging.h"
10#include "base/memory/scoped_ptr.h"
11#include "chrome/browser/ui/browser.h"
12#include "chrome/browser/ui/browser_finder.h"
13#include "chrome/browser/ui/tabs/tab_strip_model.h"
14#include "chrome/browser/ui/webui/chrome_web_contents_handler.h"
15#include "chrome/common/url_constants.h"
16#include "chrome/test/base/browser_with_test_window_test.h"
17#include "chrome/test/base/test_browser_window.h"
18#include "chrome/test/base/testing_profile.h"
19#include "components/history/core/browser/history_types.h"
20#include "content/public/browser/web_contents.h"
21#include "content/public/test/web_contents_tester.h"
22#include "testing/gmock/include/gmock/gmock.h"
23#include "testing/gtest/include/gtest/gtest.h"
24#include "ui/gfx/rect.h"
25#include "url/gurl.h"
26
27using content::OpenURLParams;
28using content::Referrer;
29using content::BrowserContext;
30using content::WebContents;
31using content::WebContentsTester;
32using ui::WebDialogWebContentsDelegate;
33
34namespace {
35
36class TestWebContentsDelegate : public WebDialogWebContentsDelegate {
37 public:
38  explicit TestWebContentsDelegate(content::BrowserContext* context)
39      : WebDialogWebContentsDelegate(context, new ChromeWebContentsHandler) {
40  }
41  virtual ~TestWebContentsDelegate() {
42  }
43
44 private:
45  DISALLOW_COPY_AND_ASSIGN(TestWebContentsDelegate);
46};
47
48class WebDialogWebContentsDelegateTest : public BrowserWithTestWindowTest {
49 public:
50  virtual void SetUp() {
51    BrowserWithTestWindowTest::SetUp();
52    test_web_contents_delegate_.reset(new TestWebContentsDelegate(profile()));
53  }
54
55  virtual void TearDown() {
56    test_web_contents_delegate_.reset(NULL);
57    BrowserWithTestWindowTest::TearDown();
58  }
59
60 protected:
61  scoped_ptr<TestWebContentsDelegate> test_web_contents_delegate_;
62};
63
64TEST_F(WebDialogWebContentsDelegateTest, DoNothingMethodsTest) {
65  // None of the following calls should do anything.
66  EXPECT_TRUE(test_web_contents_delegate_->IsPopupOrPanel(NULL));
67  history::HistoryAddPageArgs should_add_args(
68      GURL(), base::Time::Now(), 0, 0, GURL(), history::RedirectList(),
69      ui::PAGE_TRANSITION_TYPED, history::SOURCE_SYNCED, false);
70  test_web_contents_delegate_->NavigationStateChanged(
71      NULL, content::InvalidateTypes(0));
72  test_web_contents_delegate_->ActivateContents(NULL);
73  test_web_contents_delegate_->LoadingStateChanged(NULL, true);
74  test_web_contents_delegate_->CloseContents(NULL);
75  test_web_contents_delegate_->UpdateTargetURL(NULL, GURL());
76  test_web_contents_delegate_->MoveContents(NULL, gfx::Rect());
77  EXPECT_EQ(0, browser()->tab_strip_model()->count());
78  EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
79}
80
81TEST_F(WebDialogWebContentsDelegateTest, OpenURLFromTabTest) {
82  test_web_contents_delegate_->OpenURLFromTab(
83      NULL,
84      OpenURLParams(GURL(url::kAboutBlankURL),
85                    Referrer(),
86                    NEW_FOREGROUND_TAB,
87                    ui::PAGE_TRANSITION_LINK,
88                    false));
89  // This should create a new foreground tab in the existing browser.
90  EXPECT_EQ(1, browser()->tab_strip_model()->count());
91  EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
92}
93
94TEST_F(WebDialogWebContentsDelegateTest, AddNewContentsForegroundTabTest) {
95  WebContents* contents =
96      WebContentsTester::CreateTestWebContents(profile(), NULL);
97  test_web_contents_delegate_->AddNewContents(
98      NULL, contents, NEW_FOREGROUND_TAB, gfx::Rect(), false, NULL);
99  // This should create a new foreground tab in the existing browser.
100  EXPECT_EQ(1, browser()->tab_strip_model()->count());
101  EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
102}
103
104TEST_F(WebDialogWebContentsDelegateTest, DetachTest) {
105  EXPECT_EQ(profile(), test_web_contents_delegate_->browser_context());
106  test_web_contents_delegate_->Detach();
107  EXPECT_EQ(NULL, test_web_contents_delegate_->browser_context());
108  // Now, none of the following calls should do anything.
109  test_web_contents_delegate_->OpenURLFromTab(
110      NULL,
111      OpenURLParams(GURL(url::kAboutBlankURL),
112                    Referrer(),
113                    NEW_FOREGROUND_TAB,
114                    ui::PAGE_TRANSITION_LINK,
115                    false));
116  test_web_contents_delegate_->AddNewContents(NULL, NULL, NEW_FOREGROUND_TAB,
117                                              gfx::Rect(), false, NULL);
118  EXPECT_EQ(0, browser()->tab_strip_model()->count());
119  EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
120}
121
122}  // namespace
123