Chromium Code Reviews| 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" | |
|
msw
2015/08/27 18:41:13
You can also remove this now.
hcarmona
2015/08/27 20:25:42
Done.
| |
| 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 | |
| 106 BubbleManagerTest::BubbleManagerTest() {} | |
| 107 | |
| 108 void BubbleManagerTest::SetUp() { | |
| 109 testing::Test::SetUp(); | |
| 110 manager_.reset(new BubbleManager); | |
| 111 } | |
| 112 | |
| 113 void BubbleManagerTest::TearDown() { | |
| 114 manager_.reset(); | |
| 115 testing::Test::TearDown(); | |
| 116 } | |
| 117 | |
| 118 TEST_F(BubbleManagerTest, ManagerShowsBubbleUI) { | |
| 119 // Manager will delete bubble_ui. | |
| 120 MockBubbleUI* bubble_ui = new MockBubbleUI; | |
| 121 EXPECT_CALL(*bubble_ui, Destroyed()); | |
| 122 EXPECT_CALL(*bubble_ui, Show()); | |
| 123 EXPECT_CALL(*bubble_ui, Close()); | |
| 124 EXPECT_CALL(*bubble_ui, UpdateAnchorPosition()).Times(0); | |
| 125 | |
| 126 // Manager will delete delegate. | |
| 127 MockBubbleDelegate* delegate = new MockBubbleDelegate; | |
| 128 EXPECT_CALL(*delegate, Destroyed()); | |
| 129 EXPECT_CALL(*delegate, BuildBubbleUIMock()) | |
| 130 .WillOnce(testing::Return(bubble_ui)); | |
| 131 EXPECT_CALL(*delegate, ShouldClose(testing::_)) | |
| 132 .WillOnce(testing::Return(true)); | |
| 133 | |
| 134 manager_->ShowBubble(make_scoped_ptr(delegate)); | |
| 135 } | |
| 136 | |
| 137 TEST_F(BubbleManagerTest, ManagerUpdatesBubbleUI) { | |
| 138 // Manager will delete bubble_ui. | |
| 139 MockBubbleUI* bubble_ui = new MockBubbleUI; | |
| 140 EXPECT_CALL(*bubble_ui, Destroyed()); | |
| 141 EXPECT_CALL(*bubble_ui, Show()); | |
| 142 EXPECT_CALL(*bubble_ui, Close()); | |
| 143 EXPECT_CALL(*bubble_ui, UpdateAnchorPosition()); | |
| 144 | |
| 145 // Manager will delete delegate. | |
| 146 MockBubbleDelegate* delegate = new MockBubbleDelegate; | |
| 147 EXPECT_CALL(*delegate, Destroyed()); | |
| 148 EXPECT_CALL(*delegate, BuildBubbleUIMock()) | |
| 149 .WillOnce(testing::Return(bubble_ui)); | |
| 150 EXPECT_CALL(*delegate, ShouldClose(testing::_)) | |
| 151 .WillOnce(testing::Return(true)); | |
| 152 | |
| 153 manager_->ShowBubble(make_scoped_ptr(delegate)); | |
| 154 manager_->UpdateAllBubbleAnchors(); | |
| 155 } | |
| 156 | |
| 157 TEST_F(BubbleManagerTest, CloseOnReferenceInvalidatesReference) { | |
| 158 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); | |
| 159 | |
| 160 ASSERT_TRUE(ref->CloseBubble(BUBBLE_CLOSE_FOCUS_LOST)); | |
| 161 | |
| 162 ASSERT_FALSE(ref); | |
| 163 } | |
| 164 | |
| 165 TEST_F(BubbleManagerTest, CloseOnStubbornReferenceDoesNotInvalidate) { | |
| 166 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); | |
| 167 | |
| 168 ASSERT_FALSE(ref->CloseBubble(BUBBLE_CLOSE_FOCUS_LOST)); | |
| 169 | |
| 170 ASSERT_TRUE(ref); | |
| 171 } | |
| 172 | |
| 173 TEST_F(BubbleManagerTest, CloseInvalidatesReference) { | |
| 174 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); | |
| 175 | |
| 176 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FOCUS_LOST)); | |
| 177 | |
| 178 ASSERT_FALSE(ref); | |
| 179 } | |
| 180 | |
| 181 TEST_F(BubbleManagerTest, CloseAllInvalidatesReference) { | |
| 182 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); | |
| 183 | |
| 184 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); | |
| 185 | |
| 186 ASSERT_FALSE(ref); | |
| 187 } | |
| 188 | |
| 189 TEST_F(BubbleManagerTest, DestroyInvalidatesReference) { | |
| 190 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); | |
| 191 | |
| 192 manager_.reset(); | |
| 193 | |
| 194 ASSERT_FALSE(ref); | |
| 195 } | |
| 196 | |
| 197 TEST_F(BubbleManagerTest, CloseInvalidatesStubbornReference) { | |
| 198 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); | |
| 199 | |
| 200 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED)); | |
| 201 | |
| 202 ASSERT_FALSE(ref); | |
| 203 } | |
| 204 | |
| 205 TEST_F(BubbleManagerTest, CloseAllInvalidatesStubbornReference) { | |
| 206 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); | |
| 207 | |
| 208 manager_->CloseAllBubbles(BUBBLE_CLOSE_FORCED); | |
| 209 | |
| 210 ASSERT_FALSE(ref); | |
| 211 } | |
| 212 | |
| 213 TEST_F(BubbleManagerTest, DestroyInvalidatesStubbornReference) { | |
| 214 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); | |
| 215 | |
| 216 manager_.reset(); | |
| 217 | |
| 218 ASSERT_FALSE(ref); | |
| 219 } | |
| 220 | |
| 221 TEST_F(BubbleManagerTest, CloseDoesNotInvalidateStubbornReference) { | |
| 222 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); | |
| 223 | |
| 224 ASSERT_FALSE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FOCUS_LOST)); | |
| 225 | |
| 226 ASSERT_TRUE(ref); | |
| 227 } | |
| 228 | |
| 229 TEST_F(BubbleManagerTest, CloseAllDoesNotInvalidateStubbornReference) { | |
| 230 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn()); | |
| 231 | |
| 232 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); | |
| 233 | |
| 234 ASSERT_TRUE(ref); | |
| 235 } | |
| 236 | |
| 237 TEST_F(BubbleManagerTest, CloseAllInvalidatesMixAppropriately) { | |
| 238 BubbleReference stubborn_ref1 = | |
| 239 manager_->ShowBubble(MockBubbleDelegate::Stubborn()); | |
| 240 BubbleReference normal_ref1 = | |
| 241 manager_->ShowBubble(MockBubbleDelegate::Default()); | |
| 242 BubbleReference stubborn_ref2 = | |
| 243 manager_->ShowBubble(MockBubbleDelegate::Stubborn()); | |
| 244 BubbleReference normal_ref2 = | |
| 245 manager_->ShowBubble(MockBubbleDelegate::Default()); | |
| 246 BubbleReference stubborn_ref3 = | |
| 247 manager_->ShowBubble(MockBubbleDelegate::Stubborn()); | |
| 248 BubbleReference normal_ref3 = | |
| 249 manager_->ShowBubble(MockBubbleDelegate::Default()); | |
| 250 | |
| 251 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); | |
| 252 | |
| 253 ASSERT_TRUE(stubborn_ref1); | |
| 254 ASSERT_TRUE(stubborn_ref2); | |
| 255 ASSERT_TRUE(stubborn_ref3); | |
| 256 ASSERT_FALSE(normal_ref1); | |
| 257 ASSERT_FALSE(normal_ref2); | |
| 258 ASSERT_FALSE(normal_ref3); | |
| 259 } | |
| 260 | |
| 261 TEST_F(BubbleManagerTest, UpdateAllShouldWorkWithoutBubbles) { | |
| 262 // Manager shouldn't crash if bubbles have never been added. | |
| 263 manager_->UpdateAllBubbleAnchors(); | |
| 264 | |
| 265 // Add a bubble and close it. | |
| 266 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); | |
| 267 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED)); | |
| 268 | |
| 269 // Bubble should NOT get an update event because it's already closed. | |
| 270 manager_->UpdateAllBubbleAnchors(); | |
| 271 } | |
| 272 | |
| 273 TEST_F(BubbleManagerTest, CloseAllShouldWorkWithoutBubbles) { | |
| 274 // Manager shouldn't crash if bubbles have never been added. | |
| 275 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); | |
| 276 | |
| 277 // Add a bubble and close it. | |
| 278 BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default()); | |
| 279 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED)); | |
| 280 | |
| 281 // Bubble should NOT get a close event because it's already closed. | |
| 282 manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST); | |
| 283 } | |
| 284 | |
| 285 TEST_F(BubbleManagerTest, AllowBubbleChainingOnClose) { | |
| 286 scoped_ptr<BubbleDelegate> chained_delegate = MockBubbleDelegate::Default(); | |
| 287 DelegateChainHelper chain_helper(manager_.get(), chained_delegate.Pass()); | |
| 288 | |
| 289 // Manager will delete delegate. | |
| 290 MockBubbleDelegate* delegate = new MockBubbleDelegate; | |
| 291 EXPECT_CALL(*delegate, BuildBubbleUIMock()) | |
| 292 .WillOnce(testing::Return(new MockBubbleUI)); | |
| 293 EXPECT_CALL(*delegate, ShouldClose(testing::_)) | |
| 294 .WillOnce(testing::DoAll(testing::InvokeWithoutArgs( | |
| 295 &chain_helper, &DelegateChainHelper::Chain), | |
| 296 testing::Return(true))); | |
| 297 | |
| 298 BubbleReference ref = manager_->ShowBubble(make_scoped_ptr(delegate)); | |
| 299 ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED)); | |
| 300 | |
| 301 ASSERT_TRUE(chain_helper.BubbleWasTaken()); | |
| 302 } | |
| 303 | |
| 304 TEST_F(BubbleManagerTest, AllowBubbleChainingOnCloseAll) { | |
| 305 scoped_ptr<BubbleDelegate> chained_delegate = MockBubbleDelegate::Default(); | |
| 306 DelegateChainHelper chain_helper(manager_.get(), chained_delegate.Pass()); | |
| 307 | |
| 308 // Manager will delete delegate. | |
| 309 MockBubbleDelegate* delegate = new MockBubbleDelegate; | |
| 310 EXPECT_CALL(*delegate, BuildBubbleUIMock()) | |
| 311 .WillOnce(testing::Return(new MockBubbleUI)); | |
| 312 EXPECT_CALL(*delegate, ShouldClose(testing::_)) | |
| 313 .WillOnce(testing::DoAll(testing::InvokeWithoutArgs( | |
| 314 &chain_helper, &DelegateChainHelper::Chain), | |
| 315 testing::Return(true))); | |
| 316 | |
| 317 manager_->ShowBubble(make_scoped_ptr(delegate)); | |
| 318 manager_->CloseAllBubbles(BUBBLE_CLOSE_FORCED); | |
| 319 | |
| 320 ASSERT_TRUE(chain_helper.BubbleWasTaken()); | |
| 321 } | |
| 322 | |
| 323 TEST_F(BubbleManagerTest, BubblesDoNotChainOnDestroy) { | |
| 324 // Manager will delete delegate. | |
| 325 MockBubbleDelegate* chained_delegate = new MockBubbleDelegate; | |
| 326 EXPECT_CALL(*chained_delegate, BuildBubbleUIMock()).Times(0); | |
| 327 EXPECT_CALL(*chained_delegate, ShouldClose(testing::_)).Times(0); | |
| 328 | |
| 329 DelegateChainHelper chain_helper(manager_.get(), | |
| 330 make_scoped_ptr(chained_delegate)); | |
| 331 | |
| 332 // Manager will delete delegate. | |
| 333 MockBubbleDelegate* delegate = new MockBubbleDelegate; | |
| 334 EXPECT_CALL(*delegate, BuildBubbleUIMock()) | |
| 335 .WillOnce(testing::Return(new MockBubbleUI)); | |
| 336 EXPECT_CALL(*delegate, ShouldClose(testing::_)) | |
| 337 .WillOnce(testing::DoAll(testing::InvokeWithoutArgs( | |
| 338 &chain_helper, &DelegateChainHelper::Chain), | |
| 339 testing::Return(true))); | |
| 340 | |
| 341 manager_->ShowBubble(make_scoped_ptr(delegate)); | |
| 342 manager_.reset(); | |
| 343 | |
| 344 // The manager will take the bubble, but not show it. | |
| 345 ASSERT_TRUE(chain_helper.BubbleWasTaken()); | |
| 346 } | |
| 347 | |
| 348 } // namespace | |
| OLD | NEW |