1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/bubble/bubble_manager.h"
#include <memory>
#include <utility>
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "components/bubble/bubble_controller.h"
#include "components/bubble/bubble_manager_mocks.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
// A "show chain" happens when a bubble decides to show another bubble on close.
// All bubbles must be iterated to handle a close event. If a bubble shows
// another bubble while it's being closed, the iterator can get messed up.
class ChainShowBubbleDelegate : public MockBubbleDelegate {
public:
// |chained_bubble| can be nullptr if not interested in getting a reference to
// the chained bubble.
ChainShowBubbleDelegate(BubbleManager* manager,
std::unique_ptr<BubbleDelegate> delegate,
BubbleReference* chained_bubble)
: manager_(manager),
delegate_(std::move(delegate)),
chained_bubble_(chained_bubble),
closed_(false) {
EXPECT_CALL(*this, ShouldClose(testing::_)).WillOnce(testing::Return(true));
}
~ChainShowBubbleDelegate() override { EXPECT_TRUE(closed_); }
void DidClose(BubbleCloseReason reason) override {
MockBubbleDelegate::DidClose(reason);
BubbleReference ref = manager_->ShowBubble(std::move(delegate_));
if (chained_bubble_)
*chained_bubble_ = ref;
closed_ = true;
}
private:
BubbleManager* manager_;
std::unique_ptr<BubbleDelegate> delegate_;
BubbleReference* chained_bubble_;
bool closed_;
DISALLOW_COPY_AND_ASSIGN(ChainShowBubbleDelegate);
};
// A "close chain" happens when a close event is received while another close
// event is in progress. Ex: Closing the BubbleUi will hide the bubble, causing
// it to lose focus, which causes another close event. This test simulates this
// by sending a close event during the |DidClose| method of a BubbleDelegate.
// Similarly to the show chain, a close chain can mess up the iterator.
class ChainCloseBubbleDelegate : public MockBubbleDelegate {
public:
ChainCloseBubbleDelegate(BubbleManager* manager) : manager_(manager) {}
~ChainCloseBubbleDelegate() override {}
void DidClose(BubbleCloseReason reason) override {
manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST);
}
private:
BubbleManager* manager_;
DISALLOW_COPY_AND_ASSIGN(ChainCloseBubbleDelegate);
};
class MockBubbleManagerObserver : public BubbleManager::BubbleManagerObserver {
public:
MockBubbleManagerObserver() {}
~MockBubbleManagerObserver() override {}
MOCK_METHOD1(OnBubbleNeverShown, void(BubbleReference));
MOCK_METHOD2(OnBubbleClosed, void(BubbleReference, BubbleCloseReason));
private:
DISALLOW_COPY_AND_ASSIGN(MockBubbleManagerObserver);
};
class BubbleManagerSubclass : public BubbleManager {
public:
using BubbleManager::CloseBubblesOwnedBy;
};
class BubbleManagerTest : public testing::Test {
public:
BubbleManagerTest();
~BubbleManagerTest() override {}
void SetUp() override;
void TearDown() override;
protected:
std::unique_ptr<BubbleManagerSubclass> manager_;
private:
DISALLOW_COPY_AND_ASSIGN(BubbleManagerTest);
};
BubbleManagerTest::BubbleManagerTest() {}
void BubbleManagerTest::SetUp() {
testing::Test::SetUp();
manager_.reset(new BubbleManagerSubclass);
}
void BubbleManagerTest::TearDown() {
manager_.reset();
testing::Test::TearDown();
}
TEST_F(BubbleManagerTest, ManagerShowsBubbleUi) {
std::unique_ptr<MockBubbleDelegate> delegate = MockBubbleDelegate::Default();
MockBubbleUi* bubble_ui = delegate->bubble_ui();
EXPECT_CALL(*bubble_ui, Destroyed());
EXPECT_CALL(*bubble_ui, Show(testing::_));
EXPECT_CALL(*bubble_ui, Close());
EXPECT_CALL(*bubble_ui, UpdateAnchorPosition()).Times(0);
manager_->ShowBubble(std::move(delegate));
}
TEST_F(BubbleManagerTest, ManagerUpdatesBubbleUiAnchor) {
std::unique_ptr<MockBubbleDelegate> delegate = MockBubbleDelegate::Default();
MockBubbleUi* bubble_ui = delegate->bubble_ui();
EXPECT_CALL(*bubble_ui, Destroyed());
EXPECT_CALL(*bubble_ui, Show(testing::_));
EXPECT_CALL(*bubble_ui, Close());
EXPECT_CALL(*bubble_ui, UpdateAnchorPosition());
manager_->ShowBubble(std::move(delegate));
manager_->UpdateAllBubbleAnchors();
}
TEST_F(BubbleManagerTest, CloseOnReferenceInvalidatesReference) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default());
ASSERT_TRUE(ref->CloseBubble(BUBBLE_CLOSE_FOCUS_LOST));
ASSERT_FALSE(ref);
}
TEST_F(BubbleManagerTest, CloseOnStubbornReferenceDoesNotInvalidate) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn());
ASSERT_FALSE(ref->CloseBubble(BUBBLE_CLOSE_FOCUS_LOST));
ASSERT_TRUE(ref);
}
TEST_F(BubbleManagerTest, CloseInvalidatesReference) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default());
ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FOCUS_LOST));
ASSERT_FALSE(ref);
}
TEST_F(BubbleManagerTest, CloseAllInvalidatesReference) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default());
manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST);
ASSERT_FALSE(ref);
}
TEST_F(BubbleManagerTest, DestroyInvalidatesReference) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default());
manager_.reset();
ASSERT_FALSE(ref);
}
TEST_F(BubbleManagerTest, CloseInvalidatesStubbornReference) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn());
ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED));
ASSERT_FALSE(ref);
}
TEST_F(BubbleManagerTest, CloseAllInvalidatesStubbornReference) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn());
manager_->CloseAllBubbles(BUBBLE_CLOSE_FORCED);
ASSERT_FALSE(ref);
}
TEST_F(BubbleManagerTest, DestroyInvalidatesStubbornReference) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn());
manager_.reset();
ASSERT_FALSE(ref);
}
TEST_F(BubbleManagerTest, CloseDoesNotInvalidateStubbornReference) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn());
ASSERT_FALSE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FOCUS_LOST));
ASSERT_TRUE(ref);
}
TEST_F(BubbleManagerTest, CloseAllDoesNotInvalidateStubbornReference) {
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Stubborn());
manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST);
ASSERT_TRUE(ref);
}
TEST_F(BubbleManagerTest, CloseAllInvalidatesMixAppropriately) {
BubbleReference stubborn_ref1 =
manager_->ShowBubble(MockBubbleDelegate::Stubborn());
BubbleReference normal_ref1 =
manager_->ShowBubble(MockBubbleDelegate::Default());
BubbleReference stubborn_ref2 =
manager_->ShowBubble(MockBubbleDelegate::Stubborn());
BubbleReference normal_ref2 =
manager_->ShowBubble(MockBubbleDelegate::Default());
BubbleReference stubborn_ref3 =
manager_->ShowBubble(MockBubbleDelegate::Stubborn());
BubbleReference normal_ref3 =
manager_->ShowBubble(MockBubbleDelegate::Default());
manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST);
ASSERT_TRUE(stubborn_ref1);
ASSERT_TRUE(stubborn_ref2);
ASSERT_TRUE(stubborn_ref3);
ASSERT_FALSE(normal_ref1);
ASSERT_FALSE(normal_ref2);
ASSERT_FALSE(normal_ref3);
}
TEST_F(BubbleManagerTest, CloseBubbleShouldOnlylCloseSelf) {
BubbleReference ref1 = manager_->ShowBubble(MockBubbleDelegate::Default());
BubbleReference ref2 = manager_->ShowBubble(MockBubbleDelegate::Default());
BubbleReference ref3 = manager_->ShowBubble(MockBubbleDelegate::Default());
EXPECT_TRUE(ref1);
EXPECT_TRUE(ref2);
EXPECT_TRUE(ref3);
ref2->CloseBubble(BUBBLE_CLOSE_FOCUS_LOST);
EXPECT_TRUE(ref1);
EXPECT_FALSE(ref2);
EXPECT_TRUE(ref3);
}
TEST_F(BubbleManagerTest, CloseOwnedByShouldLeaveUnowned) {
std::unique_ptr<MockBubbleDelegate> delegate1 = MockBubbleDelegate::Default();
std::unique_ptr<MockBubbleDelegate> delegate2 = MockBubbleDelegate::Default();
std::unique_ptr<MockBubbleDelegate> delegate3 = MockBubbleDelegate::Default();
MockBubbleDelegate& delegate1_ref = *delegate1;
MockBubbleDelegate& delegate2_ref = *delegate2;
MockBubbleDelegate& delegate3_ref = *delegate3;
BubbleReference ref1 = manager_->ShowBubble(std::move(delegate1));
BubbleReference ref2 = manager_->ShowBubble(std::move(delegate2));
BubbleReference ref3 = manager_->ShowBubble(std::move(delegate3));
// These pointers are only compared for equality, not dereferenced.
const content::RenderFrameHost* const frame1 =
reinterpret_cast<const content::RenderFrameHost*>(&ref1);
const content::RenderFrameHost* const frame2 =
reinterpret_cast<const content::RenderFrameHost*>(&ref2);
EXPECT_CALL(delegate1_ref, OwningFrame())
.WillRepeatedly(testing::Return(frame1));
EXPECT_CALL(delegate2_ref, OwningFrame())
.WillRepeatedly(testing::Return(frame2));
EXPECT_CALL(delegate3_ref, OwningFrame())
.WillRepeatedly(testing::Return(nullptr));
EXPECT_CALL(delegate1_ref, ShouldClose(BUBBLE_CLOSE_FRAME_DESTROYED))
.WillOnce(testing::Return(true));
manager_->CloseBubblesOwnedBy(frame1);
EXPECT_FALSE(ref1);
EXPECT_TRUE(ref2);
EXPECT_TRUE(ref3);
}
TEST_F(BubbleManagerTest, UpdateAllShouldWorkWithoutBubbles) {
// Manager shouldn't crash if bubbles have never been added.
manager_->UpdateAllBubbleAnchors();
// Add a bubble and close it.
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default());
ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED));
// Bubble should NOT get an update event because it's already closed.
manager_->UpdateAllBubbleAnchors();
}
TEST_F(BubbleManagerTest, CloseAllShouldWorkWithoutBubbles) {
// Manager shouldn't crash if bubbles have never been added.
manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST);
// Add a bubble and close it.
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default());
ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED));
// Bubble should NOT get a close event because it's already closed.
manager_->CloseAllBubbles(BUBBLE_CLOSE_FOCUS_LOST);
}
// This test validates that it's possible to show another bubble when
// |CloseBubble| is called.
TEST_F(BubbleManagerTest, AllowBubbleChainingOnClose) {
BubbleReference chained_bubble;
BubbleReference ref =
manager_->ShowBubble(base::WrapUnique(new ChainShowBubbleDelegate(
manager_.get(), MockBubbleDelegate::Default(), &chained_bubble)));
ASSERT_FALSE(chained_bubble); // Bubble not yet visible.
ASSERT_TRUE(manager_->CloseBubble(ref, BUBBLE_CLOSE_FORCED));
ASSERT_TRUE(chained_bubble); // Bubble is now visible.
}
// This test validates that it's possible to show another bubble when
// |CloseAllBubbles| is called.
TEST_F(BubbleManagerTest, AllowBubbleChainingOnCloseAll) {
BubbleReference chained_bubble;
BubbleReference ref =
manager_->ShowBubble(base::WrapUnique(new ChainShowBubbleDelegate(
manager_.get(), MockBubbleDelegate::Default(), &chained_bubble)));
ASSERT_FALSE(chained_bubble); // Bubble not yet visible.
manager_->CloseAllBubbles(BUBBLE_CLOSE_FORCED);
ASSERT_TRUE(chained_bubble); // Bubble is now visible.
}
// This test validates that a show chain will not happen in the destructor.
// While chaining is during the normal life span of the manager, it should NOT
// happen when the manager is being destroyed.
TEST_F(BubbleManagerTest, BubblesDoNotChainOnDestroy) {
MockBubbleManagerObserver metrics;
// |chained_delegate| should never be shown.
EXPECT_CALL(metrics, OnBubbleNeverShown(testing::_));
// The ChainShowBubbleDelegate should be closed when the manager is destroyed.
EXPECT_CALL(metrics, OnBubbleClosed(testing::_, BUBBLE_CLOSE_FORCED));
manager_->AddBubbleManagerObserver(&metrics);
std::unique_ptr<MockBubbleDelegate> chained_delegate(new MockBubbleDelegate);
EXPECT_CALL(*chained_delegate->bubble_ui(), Show(testing::_)).Times(0);
EXPECT_CALL(*chained_delegate, ShouldClose(testing::_)).Times(0);
EXPECT_CALL(*chained_delegate, DidClose(testing::_)).Times(0);
manager_->ShowBubble(base::WrapUnique(new ChainShowBubbleDelegate(
manager_.get(), std::move(chained_delegate), nullptr)));
manager_.reset();
}
TEST_F(BubbleManagerTest, BubbleCloseReasonIsCalled) {
MockBubbleManagerObserver metrics;
EXPECT_CALL(metrics, OnBubbleNeverShown(testing::_)).Times(0);
EXPECT_CALL(metrics, OnBubbleClosed(testing::_, BUBBLE_CLOSE_ACCEPTED));
manager_->AddBubbleManagerObserver(&metrics);
BubbleReference ref = manager_->ShowBubble(MockBubbleDelegate::Default());
ref->CloseBubble(BUBBLE_CLOSE_ACCEPTED);
// Destroy to verify no events are sent to |metrics| in destructor.
manager_.reset();
}
// In a close chain, it should be possible for the bubble in the second close
// event to close.
TEST_F(BubbleManagerTest, BubbleCloseChainCloseClose) {
std::unique_ptr<ChainCloseBubbleDelegate> closing_bubble(
new ChainCloseBubbleDelegate(manager_.get()));
EXPECT_CALL(*closing_bubble, ShouldClose(testing::_))
.WillOnce(testing::Return(true));
BubbleReference other_bubble_ref =
manager_->ShowBubble(MockBubbleDelegate::Default());
BubbleReference closing_bubble_ref =
manager_->ShowBubble(std::move(closing_bubble));
EXPECT_TRUE(other_bubble_ref);
EXPECT_TRUE(closing_bubble_ref);
closing_bubble_ref->CloseBubble(BUBBLE_CLOSE_ACCEPTED);
EXPECT_FALSE(other_bubble_ref);
EXPECT_FALSE(closing_bubble_ref);
}
// In a close chain, it should be possible for the bubble in the second close
// event to remain open because close is a request.
TEST_F(BubbleManagerTest, BubbleCloseChainCloseNoClose) {
std::unique_ptr<ChainCloseBubbleDelegate> closing_bubble(
new ChainCloseBubbleDelegate(manager_.get()));
EXPECT_CALL(*closing_bubble, ShouldClose(testing::_))
.WillOnce(testing::Return(true));
BubbleReference other_bubble_ref =
manager_->ShowBubble(MockBubbleDelegate::Stubborn());
BubbleReference closing_bubble_ref =
manager_->ShowBubble(std::move(closing_bubble));
EXPECT_TRUE(other_bubble_ref);
EXPECT_TRUE(closing_bubble_ref);
closing_bubble_ref->CloseBubble(BUBBLE_CLOSE_ACCEPTED);
EXPECT_TRUE(other_bubble_ref);
EXPECT_FALSE(closing_bubble_ref);
}
// This test is a sanity check. |closing_bubble| will attempt to close all other
// bubbles if it's closed, but it doesn't want to close. Sending a close request
// should keep it open without starting a close chain.
TEST_F(BubbleManagerTest, BubbleCloseChainNoCloseNoClose) {
std::unique_ptr<ChainCloseBubbleDelegate> closing_bubble(
new ChainCloseBubbleDelegate(manager_.get()));
EXPECT_CALL(*closing_bubble, ShouldClose(testing::_))
.WillRepeatedly(testing::Return(false));
BubbleReference other_bubble_ref =
manager_->ShowBubble(MockBubbleDelegate::Default());
BubbleReference closing_bubble_ref =
manager_->ShowBubble(std::move(closing_bubble));
EXPECT_TRUE(other_bubble_ref);
EXPECT_TRUE(closing_bubble_ref);
closing_bubble_ref->CloseBubble(BUBBLE_CLOSE_ACCEPTED);
EXPECT_TRUE(other_bubble_ref);
EXPECT_TRUE(closing_bubble_ref);
}
} // namespace
|