OLD | NEW |
(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" |
| 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 // TODO(vadimb@google.com): Mock these two methods and test different |
| 63 // behaviors when they return different values. |
| 64 virtual bool IsDestroyed() const { return false; } |
| 65 virtual HWND GetWindowHandle() const { return kParentWindow; } |
| 66 MOCK_METHOD3(PostWindowsMessage, bool(UINT, WPARAM, LPARAM)); |
| 67 }; |
| 68 |
| 69 explicit TestingInfobarManager(HWND tab_window) |
| 70 : infobar_api::InfobarManager(tab_window) { |
| 71 } |
| 72 |
| 73 MockInfobarWindow* GetInfobarWindow(infobar_api::InfobarType type) { |
| 74 if (type < infobar_api::FIRST_INFOBAR_TYPE || |
| 75 type >= infobar_api::END_OF_INFOBAR_TYPE) { |
| 76 return NULL; |
| 77 } |
| 78 return reinterpret_cast<MockInfobarWindow*>(infobars_[type].get()); |
| 79 } |
| 80 |
| 81 void InitInfobarWindow(infobar_api::InfobarType type, Delegate* delegate) { |
| 82 ASSERT_TRUE(type >= infobar_api::FIRST_INFOBAR_TYPE && |
| 83 type < infobar_api::END_OF_INFOBAR_TYPE); |
| 84 ASSERT_TRUE(infobars_[type] == NULL); |
| 85 infobars_[type].reset(new MockInfobarWindow(type, delegate)); |
| 86 } |
| 87 |
| 88 virtual ContainerWindowInterface* CreateContainerWindow( |
| 89 HWND container, InfobarManager* manager) { |
| 90 return new MockContainerWindow; |
| 91 } |
| 92 |
| 93 void SetContainerWindow(ContainerWindowInterface* window) { |
| 94 container_window_.reset(window); |
| 95 } |
| 96 |
| 97 void EmulateOnClose(infobar_api::InfobarType type) { |
| 98 OnWindowClose(type); |
| 99 OnContainerWindowDelayedCloseInfobar(type); |
| 100 } |
| 101 }; |
| 102 |
| 103 class InfobarManagerTests : public testing::Test { |
| 104 public: |
| 105 virtual void SetUp() { |
| 106 infobar_delegate_.reset(new MockInfobarDelegate); |
| 107 infobar_manager_.reset(new TestingInfobarManager(kGoodWindow)); |
| 108 |
| 109 EXPECT_CALL(user32_, IsWindow(kParentWindow)).WillRepeatedly(Return(TRUE)); |
| 110 EXPECT_CALL(user32_, IsWindow(kGoodWindow)).WillRepeatedly(Return(TRUE)); |
| 111 EXPECT_CALL(user32_, IsWindow(kInfobarWindow)).WillRepeatedly(Return(TRUE)); |
| 112 EXPECT_CALL(user32_, IsWindow(NULL)).WillRepeatedly(Return(FALSE)); |
| 113 EXPECT_CALL(user32_, GetParent(_)).WillRepeatedly(Return(kGoodWindow)); |
| 114 |
| 115 // Arbitrary number to return from GetWindowRect. Unique to make it easier |
| 116 // to trace in the debugger or to add in future tests dependent on return |
| 117 // values. |
| 118 RECT window_rect = {131, 213, 831, 1013}; |
| 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 |
OLD | NEW |