Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(951)

Side by Side Diff: chrome/browser/dom_ui/html_dialog_tab_contents_delegate_unittest.cc

Issue 441011: Created HtmlDialogTabContentsDelegate, which encapsulates the (Closed)
Patch Set: Synced to head. Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2009 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/dom_ui/html_dialog_tab_contents_delegate.h"
6
7 #include <vector>
8
9 #include "base/gfx/rect.h"
10 #include "base/logging.h"
11 #include "base/scoped_ptr.h"
12 #include "chrome/browser/browser.h"
13 #include "chrome/browser/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 "googleurl/src/gurl.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace {
22
23 class MockTestBrowserWindow : public TestBrowserWindow {
24 public:
25 // TestBrowserWindow() doesn't actually use its browser argument so we just
26 // pass NULL.
27 MockTestBrowserWindow() : TestBrowserWindow(NULL) {}
28
29 virtual ~MockTestBrowserWindow() {}
30
31 MOCK_METHOD0(Show, void());
32
33 private:
34 DISALLOW_COPY_AND_ASSIGN(MockTestBrowserWindow);
35 };
36
37 class TestTabContentsDelegate : public HtmlDialogTabContentsDelegate {
38 public:
39 explicit TestTabContentsDelegate(Profile* profile)
40 : HtmlDialogTabContentsDelegate(profile),
41 window_for_next_created_browser_(NULL) {}
42
43 virtual ~TestTabContentsDelegate() {
44 CHECK(!window_for_next_created_browser_);
45 for (std::vector<Browser*>::iterator i = created_browsers_.begin();
46 i != created_browsers_.end(); ++i) {
47 (*i)->CloseAllTabs();
48 // We need to explicitly cast this since BrowserWindow does *not*
49 // have a virtual destructor.
50 delete static_cast<MockTestBrowserWindow*>((*i)->window());
51 delete *i;
52 }
53 }
54
55 virtual void MoveContents(TabContents* source, const gfx::Rect& pos) {}
56 virtual void ToolbarSizeChanged(TabContents* source, bool is_animating) {}
57
58 // Takes ownership of window.
59 void SetWindowForNextCreatedBrowser(BrowserWindow* window) {
60 CHECK(window);
61 window_for_next_created_browser_ = window;
62 }
63
64 protected:
65 // CreateBrowser() is called by OpenURLFromTab() and AddNewContents().
66 virtual Browser* CreateBrowser() {
67 DCHECK(profile());
68 DCHECK(window_for_next_created_browser_);
69 Browser* browser = new Browser(Browser::TYPE_NORMAL, profile());
70 browser->set_window(window_for_next_created_browser_);
71 window_for_next_created_browser_ = NULL;
72 created_browsers_.push_back(browser);
73 return browser;
74 }
75
76 private:
77 BrowserWindow* window_for_next_created_browser_;
78 std::vector<Browser*> created_browsers_;
79
80 DISALLOW_COPY_AND_ASSIGN(TestTabContentsDelegate);
81 };
82
83 class HtmlDialogTabContentsDelegateTest : public BrowserWithTestWindowTest {
84 public:
85 virtual void SetUp() {
86 BrowserWithTestWindowTest::SetUp();
87 test_tab_contents_delegate_.reset(new TestTabContentsDelegate(profile()));
88 }
89
90 virtual void TearDown() {
91 test_tab_contents_delegate_.reset(NULL);
92 BrowserWithTestWindowTest::TearDown();
93 }
94
95 protected:
96 scoped_ptr<TestTabContentsDelegate> test_tab_contents_delegate_;
97 };
98
99 TEST_F(HtmlDialogTabContentsDelegateTest, DoNothingMethodsTest) {
100 // None of the following calls should do anything.
101 EXPECT_TRUE(test_tab_contents_delegate_->IsPopup(NULL));
102 EXPECT_FALSE(test_tab_contents_delegate_->ShouldAddNavigationToHistory());
103 test_tab_contents_delegate_->NavigationStateChanged(NULL, 0);
104 test_tab_contents_delegate_->ActivateContents(NULL);
105 test_tab_contents_delegate_->LoadingStateChanged(NULL);
106 test_tab_contents_delegate_->CloseContents(NULL);
107 // URLStarredChanged() calls NOTREACHED() which immediately crashes.
108 // Death tests take too long for unit_test tests on OS X and
109 // there's http://code.google.com/p/chromium/issues/detail?id=24885 on
110 // death tests on Windows so we simply don't call it.
111 test_tab_contents_delegate_->UpdateTargetURL(NULL, GURL());
112 test_tab_contents_delegate_->MoveContents(NULL, gfx::Rect());
113 test_tab_contents_delegate_->ToolbarSizeChanged(NULL, false);
114 EXPECT_EQ(0, browser()->tab_count());
115 EXPECT_EQ(1U, BrowserList::size());
116 }
117
118 TEST_F(HtmlDialogTabContentsDelegateTest, OpenURLFromTabTest) {
119 MockTestBrowserWindow* window = new MockTestBrowserWindow();
120 EXPECT_CALL(*window, Show()).Times(1);
121 test_tab_contents_delegate_->SetWindowForNextCreatedBrowser(window);
122
123 test_tab_contents_delegate_->OpenURLFromTab(
124 NULL, GURL(chrome::kAboutBlankURL), GURL(),
125 NEW_FOREGROUND_TAB, PageTransition::LINK);
126 EXPECT_EQ(0, browser()->tab_count());
127 EXPECT_EQ(2U, BrowserList::size());
128 }
129
130 TEST_F(HtmlDialogTabContentsDelegateTest, AddNewContentsTest) {
131 MockTestBrowserWindow* window = new MockTestBrowserWindow();
132 EXPECT_CALL(*window, Show()).Times(1);
133 test_tab_contents_delegate_->SetWindowForNextCreatedBrowser(window);
134
135 TabContents* contents =
136 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
137 test_tab_contents_delegate_->AddNewContents(
138 NULL, contents, NEW_FOREGROUND_TAB, gfx::Rect(), false);
139 EXPECT_EQ(0, browser()->tab_count());
140 EXPECT_EQ(2U, BrowserList::size());
141 }
142
143 TEST_F(HtmlDialogTabContentsDelegateTest, DetachTest) {
144 EXPECT_EQ(profile(), test_tab_contents_delegate_->profile());
145 test_tab_contents_delegate_->Detach();
146 EXPECT_EQ(NULL, test_tab_contents_delegate_->profile());
147 // Now, none of the following calls should do anything.
148 test_tab_contents_delegate_->OpenURLFromTab(
149 NULL, GURL(chrome::kAboutBlankURL), GURL(),
150 NEW_FOREGROUND_TAB, PageTransition::LINK);
151 test_tab_contents_delegate_->AddNewContents(NULL, NULL, NEW_FOREGROUND_TAB,
152 gfx::Rect(), false);
153 EXPECT_EQ(0, browser()->tab_count());
154 EXPECT_EQ(1U, BrowserList::size());
155 }
156
157 } // namespace
158
OLDNEW
« no previous file with comments | « chrome/browser/dom_ui/html_dialog_tab_contents_delegate.cc ('k') | chrome/browser/gtk/browser_window_gtk.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698