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

Side by Side Diff: ceee/ie/plugin/bho/infobar_manager_unittest.cc

Issue 4991002: New unittests for infobar. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 10 years, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
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 //
5 // Infobar manager implementation unit tests.
6
7 // MockWin32 must not be included after atlwin, which is included by some
8 // headers in here, so we need to put it at the top:
9 #include "ceee/testing/utils/mock_win32.h" // NOLINT
10
11 #include "ceee/ie/plugin/bho/infobar_manager.h"
12 #include "ceee/ie/plugin/bho/infobar_window.h"
13 #include "ceee/testing/utils/instance_count_mixin.h"
14 #include "ceee/testing/utils/test_utils.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
MAD 2010/11/16 12:58:12 Just a note that if you don't submit before Jói do
vadimb 2010/11/16 22:46:01 I will sync before submitting. Or maybe you will h
17
18 #include "broker_lib.h" // NOLINT
19
20 namespace {
21
22 using testing::_;
23 using testing::Invoke;
24 using testing::InvokeWithoutArgs;
25 using testing::NotNull;
26 using testing::Return;
27 using testing::SetArgumentPointee;
28 using testing::StrictMock;
29
30 const HWND kGoodWindow = reinterpret_cast<HWND>(42);
31 const HWND kParentWindow = reinterpret_cast<HWND>(77);
32 const HWND kInfobarWindow = reinterpret_cast<HWND>(91);
33 const wchar_t* kUrl1 = L"/infobar/test.html";
34 const int kMaxHeight = 55;
35
36 class MockInfobarDelegate : public infobar_api::InfobarWindow::Delegate {
37 public:
38 MOCK_METHOD0(GetContainerWindow, HWND());
39 MOCK_METHOD1(OnWindowClose, void(infobar_api::InfobarType));
40 };
41
42 class MockInfobarWindow : public infobar_api::InfobarWindow {
43 public:
44 MockInfobarWindow(infobar_api::InfobarType type, Delegate* delegate)
45 : infobar_api::InfobarWindow(type, delegate) {}
46 MOCK_METHOD2(InternalCreate, HWND(HWND, DWORD));
47 MOCK_METHOD2(Show, HRESULT(int, bool));
48 MOCK_METHOD0(Hide, HRESULT());
49 MOCK_METHOD1(Navigate, HRESULT(const std::wstring& url));
50 MOCK_METHOD0(Reset, void());
51
52 HWND SetInfobarHwnd() {
53 m_hWnd = kInfobarWindow;
54 return m_hWnd;
55 }
56 };
57
58 class TestingInfobarManager : public infobar_api::InfobarManager {
59 public:
60 class MockContainerWindow : public ContainerWindowInterface {
61 public:
62 virtual bool IsDestroyed() const { return false; }
MAD 2010/11/16 12:58:12 Would be nice to mock those two and test the diffe
vadimb 2010/11/16 22:46:01 Done.
63 virtual HWND GetWindowHandle() const { return kParentWindow; }
64 MOCK_METHOD3(PostWindowsMessage, bool(UINT, WPARAM, LPARAM));
65 /*
MAD 2010/11/16 12:58:12 Why this commented out code?
vadimb 2010/11/16 22:46:01 Removed. Sorry, leftover from the previous version
66 virtual bool PostWindowsMessage(UINT msg, WPARAM wparam, LPARAM lparam) {
67 return true;
68 }
69 */
70 };
71
72 explicit TestingInfobarManager(HWND tab_window)
73 : infobar_api::InfobarManager(tab_window) {
74 }
75
76 MockInfobarWindow* GetInfobarWindow(infobar_api::InfobarType type) {
77 if (type < infobar_api::FIRST_INFOBAR_TYPE ||
78 type >= infobar_api::END_OF_INFOBAR_TYPE) {
79 return NULL;
80 }
81 return reinterpret_cast<MockInfobarWindow*>(infobars_[type].get());
82 }
83
84 void InitInfobarWindow(infobar_api::InfobarType type, Delegate* delegate) {
85 ASSERT_TRUE(type >= infobar_api::FIRST_INFOBAR_TYPE &&
86 type < infobar_api::END_OF_INFOBAR_TYPE);
87 ASSERT_TRUE(infobars_[type] == NULL);
88 infobars_[type].reset(new MockInfobarWindow(type, delegate));
89 }
90
91 virtual ContainerWindowInterface* CreateContainerWindow(
92 HWND container, InfobarManager* manager) {
93 return new MockContainerWindow;
94 }
95
96 void SetContainerWindow(ContainerWindowInterface* window) {
97 container_window_.reset(window);
98 }
99
100 void EmulateOnClose(infobar_api::InfobarType type) {
101 OnWindowClose(type);
102 OnContainerWindowDelayedCloseInfobar(type);
103 }
104 };
105
106 class InfobarManagerTests : public testing::Test {
107 public:
108 virtual void SetUp() {
109 infobar_delegate_.reset(new MockInfobarDelegate);
110 infobar_manager_.reset(new TestingInfobarManager(kGoodWindow));
111
112 EXPECT_CALL(user32_, IsWindow(kParentWindow)).WillRepeatedly(Return(TRUE));
113 EXPECT_CALL(user32_, IsWindow(kGoodWindow)).WillRepeatedly(Return(TRUE));
114 EXPECT_CALL(user32_, IsWindow(kInfobarWindow)).WillRepeatedly(Return(TRUE));
115 EXPECT_CALL(user32_, IsWindow(NULL)).WillRepeatedly(Return(FALSE));
116 EXPECT_CALL(user32_, GetParent(_)).WillRepeatedly(Return(kGoodWindow));
117
118 RECT window_rect = {131, 213, 831, 1013};
MAD 2010/11/16 12:58:12 Where do these numbers come from? Comments please.
vadimb 2010/11/16 22:46:01 Done.
119 EXPECT_CALL(user32_, GetWindowRect(_, NotNull())).
120 WillRepeatedly(DoAll(SetArgumentPointee<1>(window_rect), Return(TRUE)));
121 }
122 virtual void TearDown() {
123 testing::LogDisabler no_dchecks;
124 infobar_manager_.reset(NULL);
125 infobar_delegate_.reset(NULL);
126
127 // Everything should have been relinquished.
128 ASSERT_EQ(0, testing::InstanceCountMixinBase::all_instance_count());
129 }
130
131 protected:
132 static BOOL MockEnumChildWindows(HWND, WNDENUMPROC, LPARAM lparam) {
133 HWND* window_handle = reinterpret_cast<HWND*>(lparam);
134 if (window_handle)
135 *window_handle = kParentWindow;
136 return TRUE;
137 }
138
139 StrictMock<testing::MockUser32> user32_;
140 scoped_ptr<MockInfobarDelegate> infobar_delegate_;
141 scoped_ptr<TestingInfobarManager> infobar_manager_;
142 };
143
144 TEST_F(InfobarManagerTests, GetContainerWindow) {
145 EXPECT_CALL(user32_, EnumChildWindows(kGoodWindow, _, _)).
146 WillOnce(Invoke(MockEnumChildWindows));
147
148 ASSERT_EQ(kParentWindow, infobar_manager_->GetContainerWindow());
149 // Next time it should not call EnumChildWindows.
150 ASSERT_EQ(kParentWindow, infobar_manager_->GetContainerWindow());
151 }
152
153 TEST_F(InfobarManagerTests, ShowHide) {
154 infobar_manager_->InitInfobarWindow(infobar_api::TOP_INFOBAR,
155 infobar_delegate_.get());
156
157 // Test Show first.
158 MockInfobarWindow* infobar_window =
159 infobar_manager_->GetInfobarWindow(infobar_api::TOP_INFOBAR);
160 const std::wstring url(kUrl1);
161 EXPECT_CALL(*infobar_window, Navigate(url)).WillOnce(Return(S_OK));
162 EXPECT_CALL(*infobar_window, InternalCreate(kGoodWindow, _)).
163 WillOnce(WithoutArgs(Invoke(infobar_window,
164 &MockInfobarWindow::SetInfobarHwnd)));
165 EXPECT_CALL(*infobar_window, Show(kMaxHeight, false));
166 EXPECT_CALL(*infobar_delegate_, GetContainerWindow()).
167 WillRepeatedly(Return(kParentWindow));
168 EXPECT_CALL(user32_, SetWindowPos(_, _, _, _, _, _, _)).
169 WillRepeatedly(Return(TRUE));
170
171 EXPECT_HRESULT_SUCCEEDED(infobar_manager_->Show(infobar_api::TOP_INFOBAR,
172 kMaxHeight, kUrl1, false));
173
174 // Test Hide for this infobar two times. The second Hide should still be OK
175 // even though the infobar is already hidden.
176 EXPECT_CALL(*infobar_window, Reset()).Times(2);
177 EXPECT_HRESULT_SUCCEEDED(infobar_manager_->Hide(infobar_api::TOP_INFOBAR));
178 EXPECT_HRESULT_SUCCEEDED(infobar_manager_->Hide(infobar_api::TOP_INFOBAR));
179
180 // However Hide to non-existent infobar should result in an error.
181 EXPECT_HRESULT_FAILED(infobar_manager_->Hide(infobar_api::BOTTOM_INFOBAR));
182 EXPECT_HRESULT_FAILED(infobar_manager_->Hide(
183 static_cast<infobar_api::InfobarType>(55)));
184
185 // HideAll hides only the existing infobar.
186 EXPECT_CALL(*infobar_window, Reset());
187 infobar_manager_->HideAll();
188 }
189
190 TEST_F(InfobarManagerTests, OnClose) {
191 infobar_manager_->InitInfobarWindow(infobar_api::TOP_INFOBAR,
192 infobar_delegate_.get());
193 TestingInfobarManager::MockContainerWindow* container_window =
194 new TestingInfobarManager::MockContainerWindow;
195 infobar_manager_->SetContainerWindow(container_window);
196 EXPECT_CALL(*container_window, PostWindowsMessage(_, _, _));
197 MockInfobarWindow* infobar_window =
198 infobar_manager_->GetInfobarWindow(infobar_api::TOP_INFOBAR);
199 EXPECT_CALL(*infobar_window, Reset());
200 infobar_manager_->EmulateOnClose(infobar_api::TOP_INFOBAR);
201 }
202
203 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698