| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 "components/bubble/bubble_manager.h" |
| 6 |
| 7 #include "components/bubble/bubble_controller.h" |
| 8 #include "components/bubble/bubble_delegate.h" |
| 9 #include "components/bubble/bubble_ui.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 class MockBubbleUI : public BubbleUI { |
| 17 public: |
| 18 MockBubbleUI() {} |
| 19 ~MockBubbleUI() override { Destroyed(); } |
| 20 |
| 21 MOCK_METHOD0(Show, void()); |
| 22 MOCK_METHOD0(Close, void()); |
| 23 MOCK_METHOD0(UpdateAnchorPosition, void()); |
| 24 |
| 25 // To verify destructor call. |
| 26 MOCK_METHOD0(Destroyed, void()); |
| 27 }; |
| 28 |
| 29 class MockBubbleDelegate : public BubbleDelegate { |
| 30 public: |
| 31 MockBubbleDelegate() {} |
| 32 ~MockBubbleDelegate() override { Destroyed(); } |
| 33 |
| 34 // Default bubble shows UI and closes when asked to close. |
| 35 static scoped_ptr<MockBubbleDelegate> Default(); |
| 36 |
| 37 // Stubborn bubble shows UI and doesn't want to close. |
| 38 static scoped_ptr<MockBubbleDelegate> Stubborn(); |
| 39 |
| 40 MOCK_METHOD1(ShouldClose, bool(BubbleCloseReason reason)); |
| 41 |
| 42 // A scoped_ptr can't be returned in MOCK_METHOD. |
| 43 MOCK_METHOD0(BuildBubbleUIMock, BubbleUI*()); |
| 44 scoped_ptr<BubbleUI> BuildBubbleUI() override { |
| 45 return make_scoped_ptr(BuildBubbleUIMock()); |
| 46 } |
| 47 |
| 48 // To verify destructor call. |
| 49 MOCK_METHOD0(Destroyed, void()); |
| 50 }; |
| 51 |
| 52 // static |
| 53 scoped_ptr<MockBubbleDelegate> MockBubbleDelegate::Default() { |
| 54 MockBubbleDelegate* delegate = new MockBubbleDelegate; |
| 55 EXPECT_CALL(*delegate, BuildBubbleUIMock()) |
| 56 .WillOnce(testing::Return(new MockBubbleUI)); |
| 57 EXPECT_CALL(*delegate, ShouldClose(testing::_)) |
| 58 .WillOnce(testing::Return(true)); |
| 59 return make_scoped_ptr(delegate); |
| 60 } |
| 61 |
| 62 // static |
| 63 scoped_ptr<MockBubbleDelegate> MockBubbleDelegate::Stubborn() { |
| 64 MockBubbleDelegate* delegate = new MockBubbleDelegate; |
| 65 EXPECT_CALL(*delegate, BuildBubbleUIMock()) |
| 66 .WillOnce(testing::Return(new MockBubbleUI)); |
| 67 EXPECT_CALL(*delegate, ShouldClose(testing::_)) |
| 68 .WillRepeatedly(testing::Return(false)); |
| 69 return make_scoped_ptr(delegate); |
| 70 } |
| 71 |
| 72 // Helper class used to test chaining another bubble. |
| 73 class DelegateChainHelper { |
| 74 public: |
| 75 DelegateChainHelper(BubbleManager* manager, |
| 76 scoped_ptr<BubbleDelegate> next_delegate); |
| 77 |
| 78 // Will show the bubble in |next_delegate_|. |
| 79 void Chain() { manager_->ShowBubble(next_delegate_.Pass()); } |
| 80 |
| 81 // True if the bubble was taken by the bubble manager. |
| 82 bool BubbleWasTaken() { return !next_delegate_; } |
| 83 |
| 84 private: |
| 85 BubbleManager* manager_; // Weak. |
| 86 scoped_ptr<BubbleDelegate> next_delegate_; |
| 87 }; |
| 88 |
| 89 DelegateChainHelper::DelegateChainHelper( |
| 90 BubbleManager* manager, |
| 91 scoped_ptr<BubbleDelegate> next_delegate) |
| 92 : manager_(manager), next_delegate_(next_delegate.Pass()) {} |
| 93 |
| 94 class BubbleManagerTest : public testing::Test { |
| 95 public: |
| 96 BubbleManagerTest(); |
| 97 ~BubbleManagerTest() override {} |
| 98 |
| 99 void SetUp() override; |
| 100 void TearDown() override; |
| 101 |
| 102 protected: |
| 103 scoped_ptr<BubbleManager> manager_; |
| 104 |
| 105 private: |
| 106 // The BubbleManager's show/close methods should only be called from the UI |
| 107 // thread. This will allow the UI DCHECK to pass in these tests. |
| 108 content::TestBrowserThreadBundle thread_bundle_; |
| 109 }; |
| 110 |
| 111 BubbleManagerTest::BubbleManagerTest() |
| 112 : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {} |
| 113 |
| 114 void BubbleManagerTest::SetUp() { |
| 115 testing::Test::SetUp(); |
| 116 manager_.reset(new BubbleManager); |
| 117 } |
| 118 |
| 119 void BubbleManagerTest::TearDown() { |
| 120 manager_.reset(); |
| 121 testing::Test::TearDown(); |
| 122 } |
| 123 |
| 124 TEST_F(BubbleManagerTest, ManagerShowsBubbleUI) { |
| 125 // Manager will delete bubble_ui. |
| 126 MockBubbleUI* bubble_ui = new MockBubbleUI; |
| 127 EXPECT_CALL(*bubble_ui, Destroyed()); |
| 128 EXPECT_CALL(*bubble_ui, Show()); |
| 129 EXPECT_CALL(*bubble_ui, Close()); |
| 130 EXPECT_CALL(*bubble_ui, UpdateAnchorPosition()).Times(0); |
| 131 |
| 132 // Manager will delete delegate. |
| 133 MockBubbleDelegate* delegate = new MockBubbleDelegate; |
| 134 EXPECT_CALL(*delegate, Destroyed()); |
| 135 EXPECT_CALL(*delegate, BuildBubbleUIMock()) |
| 136 .WillOnce(testing::Return(bubble_ui)); |
| 137 EXPECT_CALL(*delegate, ShouldClose(testing::_)) |
| 138 .WillOnce(testing::Return(true)); |
| 139 |
| 140 manager_->ShowBubble(make_scoped_ptr(delegate)); |
| 141 } |
| 142 |
| 143 TEST_F(BubbleManagerTest, ManagerUpdatesBubbleUI) { |
| 144 // Manager will delete bubble_ui. |
| 145 MockBubbleUI* bubble_ui = new MockBubbleUI; |
| 146 EXPECT_CALL(*bubble_ui, Destroyed()); |
| 147 EXPECT_CALL(*bubble_ui, Show()); |
| 148 EXPECT_CALL(*bubble_ui, Close()); |
| 149 EXPECT_CALL(*bubble_ui, UpdateAnchorPosition()); |
| 150 |
| 151 // Manager will delete delegate. |
| 152 MockBubbleDelegate* delegate = new MockBubbleDelegate; |
| 153 EXPECT_CALL(*delegate, Destroyed()); |
| 154 EXPECT_CALL(*delegate, BuildBubbleUIMock()) |
| 155 .WillOnce(testing::Return(bubble_ui)); |
| 156 EXPECT_CALL(*delegate, ShouldClose(testing::_)) |
| 157 .WillOnce(testing::Return(true)); |
| 158 |
| 159 manager_->ShowBubble(make_scoped_ptr(delegate)); |
| 160 manager_->UpdateAllBubbleAnchors(); |
| 161 } |
| 162 |
| 163 TEST_F(BubbleManagerTest, CloseOnReferenceInvalidatesReference) { |
| 164 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); |
| 165 |
| 166 ASSERT_TRUE(ref->CloseBubble(BUBBLE_CLOSE_FOCUS_LOST)); |
| 167 |
| 168 ASSERT_FALSE(ref); |
| 169 } |
| 170 |
| 171 TEST_F(BubbleManagerTest, CloseOnStubbornReferenceDoesNotInvalidate) { |
| 172 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); |
| 173 |
| 174 ASSERT_FALSE(ref->CloseBubble(BUBBLE_CLOSE_FOCUS_LOST)); |
| 175 |
| 176 ASSERT_TRUE(ref); |
| 177 } |
| 178 |
| 179 TEST_F(BubbleManagerTest, CloseInvalidatesReference) { |
| 180 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); |
| 181 |
| 182 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FOCUS_LOST)); |
| 183 |
| 184 ASSERT_FALSE(ref); |
| 185 } |
| 186 |
| 187 TEST_F(BubbleManagerTest, CloseAllInvalidatesReference) { |
| 188 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); |
| 189 |
| 190 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); |
| 191 |
| 192 ASSERT_FALSE(ref); |
| 193 } |
| 194 |
| 195 TEST_F(BubbleManagerTest, DestroyInvalidatesReference) { |
| 196 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); |
| 197 |
| 198 manager_.reset(); |
| 199 |
| 200 ASSERT_FALSE(ref); |
| 201 } |
| 202 |
| 203 TEST_F(BubbleManagerTest, CloseInvalidatesStubbornReference) { |
| 204 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); |
| 205 |
| 206 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED)); |
| 207 |
| 208 ASSERT_FALSE(ref); |
| 209 } |
| 210 |
| 211 TEST_F(BubbleManagerTest, CloseAllInvalidatesStubbornReference) { |
| 212 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); |
| 213 |
| 214 manager_->CloseAllBubbles(BUBBLE_CLOSE_FORCED); |
| 215 |
| 216 ASSERT_FALSE(ref); |
| 217 } |
| 218 |
| 219 TEST_F(BubbleManagerTest, DestroyInvalidatesStubbornReference) { |
| 220 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); |
| 221 |
| 222 manager_.reset(); |
| 223 |
| 224 ASSERT_FALSE(ref); |
| 225 } |
| 226 |
| 227 TEST_F(BubbleManagerTest, CloseDoesNotInvalidateStubbornReference) { |
| 228 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); |
| 229 |
| 230 ASSERT_FALSE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FOCUS_LOST)); |
| 231 |
| 232 ASSERT_TRUE(ref); |
| 233 } |
| 234 |
| 235 TEST_F(BubbleManagerTest, CloseAllDoesNotInvalidateStubbornReference) { |
| 236 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); |
| 237 |
| 238 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); |
| 239 |
| 240 ASSERT_TRUE(ref); |
| 241 } |
| 242 |
| 243 TEST_F(BubbleManagerTest, CloseAllInvalidatesMixAppropriately) { |
| 244 BubbleReference stubborn_ref1 = |
| 245 manager_->ShowBubble(MockBubbleDelegate::Stubborn()); |
| 246 BubbleReference normal_ref1 = |
| 247 manager_->ShowBubble(MockBubbleDelegate::Default()); |
| 248 BubbleReference stubborn_ref2 = |
| 249 manager_->ShowBubble(MockBubbleDelegate::Stubborn()); |
| 250 BubbleReference normal_ref2 = |
| 251 manager_->ShowBubble(MockBubbleDelegate::Default()); |
| 252 BubbleReference stubborn_ref3 = |
| 253 manager_->ShowBubble(MockBubbleDelegate::Stubborn()); |
| 254 BubbleReference normal_ref3 = |
| 255 manager_->ShowBubble(MockBubbleDelegate::Default()); |
| 256 |
| 257 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); |
| 258 |
| 259 ASSERT_TRUE(stubborn_ref1); |
| 260 ASSERT_TRUE(stubborn_ref2); |
| 261 ASSERT_TRUE(stubborn_ref3); |
| 262 ASSERT_FALSE(normal_ref1); |
| 263 ASSERT_FALSE(normal_ref2); |
| 264 ASSERT_FALSE(normal_ref3); |
| 265 } |
| 266 |
| 267 TEST_F(BubbleManagerTest, UpdateAllShouldWorkWithoutBubbles) { |
| 268 // Manager shouldn't crash if bubbles have never been added. |
| 269 manager_->UpdateAllBubbleAnchors(); |
| 270 |
| 271 // Add a bubble and close it. |
| 272 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); |
| 273 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED)); |
| 274 |
| 275 // Bubble should NOT get an update event because it's already closed. |
| 276 manager_->UpdateAllBubbleAnchors(); |
| 277 } |
| 278 |
| 279 TEST_F(BubbleManagerTest, CloseAllShouldWorkWithoutBubbles) { |
| 280 // Manager shouldn't crash if bubbles have never been added. |
| 281 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); |
| 282 |
| 283 // Add a bubble and close it. |
| 284 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); |
| 285 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED)); |
| 286 |
| 287 // Bubble should NOT get a close event because it's already closed. |
| 288 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); |
| 289 } |
| 290 |
| 291 TEST_F(BubbleManagerTest, AllowBubbleChainingOnClose) { |
| 292 scoped_ptr<BubbleDelegate> chained_delegate = MockBubbleDelegate::Default(); |
| 293 DelegateChainHelper chain_helper(manager_.get(), chained_delegate.Pass()); |
| 294 |
| 295 // Manager will delete delegate. |
| 296 MockBubbleDelegate* delegate = new MockBubbleDelegate; |
| 297 EXPECT_CALL(*delegate, BuildBubbleUIMock()) |
| 298 .WillOnce(testing::Return(new MockBubbleUI)); |
| 299 EXPECT_CALL(*delegate, ShouldClose(testing::_)) |
| 300 .WillOnce(testing::DoAll(testing::InvokeWithoutArgs( |
| 301 &chain_helper, &DelegateChainHelper::Chain), |
| 302 testing::Return(true))); |
| 303 |
| 304 BubbleReference ref = manager_->ShowBubble(make_scoped_ptr(delegate)); |
| 305 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED)); |
| 306 |
| 307 ASSERT_TRUE(chain_helper.BubbleWasTaken()); |
| 308 } |
| 309 |
| 310 TEST_F(BubbleManagerTest, AllowBubbleChainingOnCloseAll) { |
| 311 scoped_ptr<BubbleDelegate> chained_delegate = MockBubbleDelegate::Default(); |
| 312 DelegateChainHelper chain_helper(manager_.get(), chained_delegate.Pass()); |
| 313 |
| 314 // Manager will delete delegate. |
| 315 MockBubbleDelegate* delegate = new MockBubbleDelegate; |
| 316 EXPECT_CALL(*delegate, BuildBubbleUIMock()) |
| 317 .WillOnce(testing::Return(new MockBubbleUI)); |
| 318 EXPECT_CALL(*delegate, ShouldClose(testing::_)) |
| 319 .WillOnce(testing::DoAll(testing::InvokeWithoutArgs( |
| 320 &chain_helper, &DelegateChainHelper::Chain), |
| 321 testing::Return(true))); |
| 322 |
| 323 manager_->ShowBubble(make_scoped_ptr(delegate)); |
| 324 manager_->CloseAllBubbles(BUBBLE_CLOSE_FORCED); |
| 325 |
| 326 ASSERT_TRUE(chain_helper.BubbleWasTaken()); |
| 327 } |
| 328 |
| 329 TEST_F(BubbleManagerTest, BubblesDoNotChainOnDestroy) { |
| 330 // Manager will delete delegate. |
| 331 MockBubbleDelegate* chained_delegate = new MockBubbleDelegate; |
| 332 EXPECT_CALL(*chained_delegate, BuildBubbleUIMock()).Times(0); |
| 333 EXPECT_CALL(*chained_delegate, ShouldClose(testing::_)).Times(0); |
| 334 |
| 335 DelegateChainHelper chain_helper(manager_.get(), |
| 336 make_scoped_ptr(chained_delegate)); |
| 337 |
| 338 // Manager will delete delegate. |
| 339 MockBubbleDelegate* delegate = new MockBubbleDelegate; |
| 340 EXPECT_CALL(*delegate, BuildBubbleUIMock()) |
| 341 .WillOnce(testing::Return(new MockBubbleUI)); |
| 342 EXPECT_CALL(*delegate, ShouldClose(testing::_)) |
| 343 .WillOnce(testing::DoAll(testing::InvokeWithoutArgs( |
| 344 &chain_helper, &DelegateChainHelper::Chain), |
| 345 testing::Return(true))); |
| 346 |
| 347 manager_->ShowBubble(make_scoped_ptr(delegate)); |
| 348 manager_.reset(); |
| 349 |
| 350 // The manager will take the bubble, but not show it. |
| 351 ASSERT_TRUE(chain_helper.BubbleWasTaken()); |
| 352 } |
| 353 |
| 354 } // namespace |
| OLD | NEW |