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