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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
|
// Copyright 2018 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 <memory>
#include <utility>
#include "base/bind.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/interface_ptr.h"
#include "mojo/public/interfaces/bindings/tests/ping_service.mojom-blink.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/mojo/interface_invalidator.h"
#include "third_party/blink/renderer/platform/mojo/revocable_binding.h"
#include "third_party/blink/renderer/platform/mojo/revocable_interface_ptr.h"
#include "third_party/blink/renderer/platform/mojo/revocable_strong_binding.h"
namespace blink {
namespace {
void DoSetFlag(bool* flag) {
*flag = true;
}
class PingServiceImplBase : public mojo::test::blink::PingService {
public:
PingServiceImplBase(bool send_response = true)
: send_response_(send_response) {}
~PingServiceImplBase() override {}
// mojo::test::blink::PingService:
void Ping(PingCallback callback) override {
if (ping_handler_)
ping_handler_.Run();
if (send_response_) {
std::move(callback).Run();
} else {
saved_callback_ = std::move(callback);
}
if (post_ping_handler_)
post_ping_handler_.Run();
}
void set_ping_handler(const base::RepeatingClosure& handler) {
ping_handler_ = handler;
}
void set_post_ping_handler(const base::RepeatingClosure& handler) {
post_ping_handler_ = handler;
}
private:
bool send_response_;
PingCallback saved_callback_;
base::RepeatingClosure ping_handler_;
base::RepeatingClosure post_ping_handler_;
DISALLOW_COPY_AND_ASSIGN(PingServiceImplBase);
};
class PingServiceImpl : public PingServiceImplBase {
public:
PingServiceImpl(
mojo::InterfaceRequest<mojo::test::blink::PingService> request,
bool send_response = true)
: PingServiceImplBase(send_response),
error_handler_called_(false),
binding_(this, std::move(request)) {
binding_.set_connection_error_handler(
base::BindOnce(DoSetFlag, &error_handler_called_));
}
~PingServiceImpl() override {}
bool error_handler_called() { return error_handler_called_; }
mojo::Binding<mojo::test::blink::PingService>* binding() { return &binding_; }
private:
bool error_handler_called_;
mojo::Binding<mojo::test::blink::PingService> binding_;
DISALLOW_COPY_AND_ASSIGN(PingServiceImpl);
};
class InterfaceInvalidatorTest : public testing::Test {
public:
InterfaceInvalidatorTest() {}
~InterfaceInvalidatorTest() override {}
private:
base::MessageLoop message_loop_;
DISALLOW_COPY_AND_ASSIGN(InterfaceInvalidatorTest);
};
class InterfaceInvalidatorObserver : public InterfaceInvalidator::Observer {
public:
InterfaceInvalidatorObserver(const base::RepeatingClosure& handler) {
invalidate_handler_ = handler;
}
~InterfaceInvalidatorObserver() {}
void OnInvalidate() override { invalidate_handler_.Run(); }
private:
base::RepeatingClosure invalidate_handler_;
DISALLOW_COPY_AND_ASSIGN(InterfaceInvalidatorObserver);
};
TEST_F(InterfaceInvalidatorTest, DestroyNotifiesObservers) {
int called = 0;
auto inc_called_cb = base::BindLambdaForTesting([&] { ++called; });
InterfaceInvalidatorObserver observer1(inc_called_cb);
InterfaceInvalidatorObserver observer2(inc_called_cb);
{
InterfaceInvalidator invalidator;
invalidator.AddObserver(&observer1);
invalidator.AddObserver(&observer2);
EXPECT_EQ(called, 0);
}
EXPECT_EQ(called, 2);
}
TEST_F(InterfaceInvalidatorTest, DestroyInvalidatesRevocableInterfacePtr) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
bool ping_called = false;
wptr->Ping(base::BindRepeating(DoSetFlag, &ping_called));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(ping_called);
bool error_handler_called = false;
wptr.set_connection_error_handler(
base::BindOnce(DoSetFlag, &error_handler_called));
invalidator.reset();
impl.set_ping_handler(base::BindRepeating([] { FAIL(); }));
wptr->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(error_handler_called);
EXPECT_TRUE(impl.error_handler_called());
EXPECT_TRUE(wptr.encountered_error());
EXPECT_TRUE(wptr);
}
TEST_F(InterfaceInvalidatorTest, InvalidateAfterMessageSent) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
bool called = false;
impl.set_ping_handler(base::BindRepeating(DoSetFlag, &called));
// The passed in callback will not be called as the interface is invalidated
// before a response can come back.
wptr->Ping(base::BindRepeating([] { FAIL(); }));
invalidator.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
EXPECT_TRUE(impl.error_handler_called());
}
TEST_F(InterfaceInvalidatorTest, PassInterfaceThenInvalidate) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
bool impl_called = false;
impl.set_ping_handler(base::BindRepeating(DoSetFlag, &impl_called));
wptr.set_connection_error_handler(base::BindOnce([] { FAIL(); }));
mojo::test::blink::PingServicePtr ptr(wptr.PassInterface());
invalidator.reset();
bool ping_called = false;
ptr->Ping(base::BindRepeating(DoSetFlag, &ping_called));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(ping_called);
EXPECT_TRUE(impl_called);
EXPECT_FALSE(impl.error_handler_called());
}
TEST_F(InterfaceInvalidatorTest, PassInterfaceOfInvalidatedPtr) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
impl.set_ping_handler(base::BindRepeating([] { FAIL(); }));
bool error_handler_called = false;
wptr.set_connection_error_handler(
base::BindOnce(DoSetFlag, &error_handler_called));
// This also destroys the original invalidator.
invalidator = std::make_unique<InterfaceInvalidator>();
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(error_handler_called);
ASSERT_TRUE(impl.error_handler_called());
mojo::test::blink::RevocablePingServicePtr wptr2(wptr.PassInterface(),
invalidator.get());
wptr2->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
}
TEST_F(InterfaceInvalidatorTest,
PassInterfaceBeforeConnectionErrorNotification) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
impl.set_ping_handler(base::BindRepeating([] { FAIL(); }));
wptr.set_connection_error_handler(base::BindOnce([] { FAIL(); }));
// This also destroys the original invalidator.
invalidator = std::make_unique<InterfaceInvalidator>();
mojo::test::blink::RevocablePingServicePtr wptr2(wptr.PassInterface(),
invalidator.get());
wptr2->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(impl.error_handler_called());
}
TEST_F(InterfaceInvalidatorTest, InvalidateAfterReset) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
wptr.set_connection_error_handler(base::BindOnce([] { FAIL(); }));
wptr.reset();
invalidator.reset();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(wptr);
}
TEST_F(InterfaceInvalidatorTest, ResetInvalidatedRevocableInterfacePtr) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
wptr.set_connection_error_handler(base::BindOnce([] { FAIL(); }));
invalidator.reset();
wptr.reset();
base::RunLoop().RunUntilIdle();
}
TEST_F(InterfaceInvalidatorTest, InvalidateErroredPtr) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
int called = 0;
wptr.set_connection_error_handler(
base::BindLambdaForTesting([&] { called++; }));
impl.binding()->Close();
base::RunLoop().RunUntilIdle();
invalidator.reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(called, 1);
EXPECT_FALSE(impl.error_handler_called());
}
// InterfacePtrs do not set up a proxy until they are used for the first
// time.
TEST_F(InterfaceInvalidatorTest, InvalidateBeforeProxyConfigured) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
invalidator.reset();
wptr->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(impl.error_handler_called());
}
TEST_F(InterfaceInvalidatorTest, MoveChangesInvalidatorObserver) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
auto wptr2(std::move(wptr));
bool called = false;
wptr2.set_connection_error_handler(base::BindOnce(DoSetFlag, &called));
invalidator.reset();
wptr2->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
EXPECT_TRUE(impl.error_handler_called());
}
TEST_F(InterfaceInvalidatorTest, MoveInvalidatedPointer) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
invalidator.reset();
auto wptr2(std::move(wptr));
wptr2->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
}
TEST_F(InterfaceInvalidatorTest, InvalidateRevocableInterfacePtrDuringSyncIPC) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()));
impl.set_ping_handler(
base::BindLambdaForTesting([&]() { invalidator.reset(); }));
bool result = wptr->Ping();
EXPECT_FALSE(result);
}
TEST_F(InterfaceInvalidatorTest,
InvalidateRevocableInterfacePtrDuringSyncIPCWithoutResponse) {
mojo::test::blink::RevocablePingServicePtr wptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
PingServiceImpl impl(MakeRequest(&wptr, invalidator.get()),
false /* send_response */);
impl.set_ping_handler(
base::BindLambdaForTesting([&]() { invalidator.reset(); }));
bool result = wptr->Ping();
EXPECT_FALSE(result);
}
class RevocablePingServiceImpl : public PingServiceImplBase {
public:
RevocablePingServiceImpl(
mojo::InterfaceRequest<mojo::test::blink::PingService> request,
InterfaceInvalidator* invalidator,
bool send_response = true)
: PingServiceImplBase(send_response),
error_handler_called_(false),
binding_(this, std::move(request), invalidator) {
binding_.set_connection_error_handler(
base::BindOnce(DoSetFlag, &error_handler_called_));
}
~RevocablePingServiceImpl() override {}
bool error_handler_called() { return error_handler_called_; }
RevocableBinding<mojo::test::blink::PingService>* binding() {
return &binding_;
}
private:
bool error_handler_called_;
RevocableBinding<mojo::test::blink::PingService> binding_;
DISALLOW_COPY_AND_ASSIGN(RevocablePingServiceImpl);
};
TEST_F(InterfaceInvalidatorTest, DestroyInvalidatesRevocableBinding) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
bool ping_called = false;
ptr->Ping(base::BindRepeating(DoSetFlag, &ping_called));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(ping_called);
bool error_handler_called = false;
ptr.set_connection_error_handler(
base::BindOnce(DoSetFlag, &error_handler_called));
invalidator.reset();
impl.set_ping_handler(base::BindRepeating([] { FAIL(); }));
ptr->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(error_handler_called);
EXPECT_TRUE(impl.error_handler_called());
EXPECT_TRUE(ptr.encountered_error());
EXPECT_TRUE(ptr);
EXPECT_TRUE(*impl.binding());
}
TEST_F(InterfaceInvalidatorTest, InvalidateBindingBeforeResponse) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
impl.set_ping_handler(
base::BindLambdaForTesting([&] { invalidator.reset(); }));
bool ptr_error_handler_called = false;
ptr.set_connection_error_handler(
base::BindOnce(DoSetFlag, &ptr_error_handler_called));
ptr->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(ptr_error_handler_called);
EXPECT_TRUE(impl.error_handler_called());
EXPECT_TRUE(*impl.binding());
}
TEST_F(InterfaceInvalidatorTest, InvalidateBindingAfterResponse) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
impl.set_post_ping_handler(base::BindLambdaForTesting([&] {
invalidator.reset();
impl.set_ping_handler(base::BindRepeating([] { FAIL(); }));
}));
bool ptr_error_handler_called = false;
ptr.set_connection_error_handler(
base::BindOnce(DoSetFlag, &ptr_error_handler_called));
bool ping_called = false;
ptr->Ping(base::BindRepeating(DoSetFlag, &ping_called));
ptr->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(ping_called);
EXPECT_TRUE(ptr_error_handler_called);
EXPECT_TRUE(impl.error_handler_called());
EXPECT_TRUE(*impl.binding());
}
TEST_F(InterfaceInvalidatorTest, UnbindThenInvalidate) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
ptr.set_connection_error_handler(base::BindOnce([] { FAIL(); }));
PingServiceImpl impl2(impl.binding()->Unbind());
invalidator.reset();
bool ping_called = false;
ptr->Ping(base::BindRepeating(DoSetFlag, &ping_called));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(ping_called);
EXPECT_FALSE(impl.error_handler_called());
}
TEST_F(InterfaceInvalidatorTest, UnbindInvalidatedRevocableBinding) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
bool ptr_error_handler_called = false;
ptr.set_connection_error_handler(
base::BindOnce(DoSetFlag, &ptr_error_handler_called));
invalidator.reset();
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(ptr_error_handler_called);
ASSERT_TRUE(impl.error_handler_called());
PingServiceImpl impl2(impl.binding()->Unbind());
impl2.set_ping_handler(base::BindRepeating([] { FAIL(); }));
ptr->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(impl2.error_handler_called());
}
TEST_F(InterfaceInvalidatorTest, UnbindBeforeConnectionErrorNotification) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
bool ptr_error_handler_called = false;
ptr.set_connection_error_handler(
base::BindOnce(DoSetFlag, &ptr_error_handler_called));
invalidator.reset();
PingServiceImpl impl2(impl.binding()->Unbind());
impl2.set_ping_handler(base::BindRepeating([] { FAIL(); }));
ptr->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(impl.error_handler_called());
EXPECT_TRUE(impl2.error_handler_called());
EXPECT_TRUE(ptr_error_handler_called);
}
TEST_F(InterfaceInvalidatorTest, InvalidateClosedRevocableBinding) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
impl.binding()->Close();
invalidator.reset();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(impl.error_handler_called());
EXPECT_FALSE(*impl.binding());
}
TEST_F(InterfaceInvalidatorTest, CloseInvalidatedRevocableBinding) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
invalidator.reset();
impl.binding()->Close();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(impl.error_handler_called());
EXPECT_FALSE(*impl.binding());
}
TEST_F(InterfaceInvalidatorTest, InvalidateErroredRevocableBinding) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
int called = 0;
impl.binding()->set_connection_error_handler(
base::BindLambdaForTesting([&] { called++; }));
ptr.reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, called);
invalidator.reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, called);
}
TEST_F(InterfaceInvalidatorTest, InvalidateWhileRevocableBindingPaused) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
impl.binding()->PauseIncomingMethodCallProcessing();
invalidator.reset();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(impl.error_handler_called());
impl.binding()->ResumeIncomingMethodCallProcessing();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(impl.error_handler_called());
}
TEST_F(InterfaceInvalidatorTest, InvalidateRevocableBindingDuringSyncIPC) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get());
impl.set_ping_handler(
base::BindLambdaForTesting([&] { invalidator.reset(); }));
bool result = ptr->Ping();
EXPECT_FALSE(result);
}
TEST_F(InterfaceInvalidatorTest,
InvalidateRevocableBindingDuringSyncIPCWithoutResponse) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
RevocablePingServiceImpl impl(MakeRequest(&ptr), invalidator.get(),
false /* send_response */);
impl.set_ping_handler(
base::BindLambdaForTesting([&] { invalidator.reset(); }));
bool result = ptr->Ping();
EXPECT_FALSE(result);
}
TEST_F(InterfaceInvalidatorTest, InvalidateStrongBinding) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
auto impl_ptr =
MakeRevocableStrongBinding(std::make_unique<PingServiceImplBase>(),
MakeRequest(&ptr), invalidator.get());
auto* impl = reinterpret_cast<PingServiceImplBase*>(impl_ptr->impl());
bool impl_called = false;
impl->set_ping_handler(base::BindRepeating(DoSetFlag, &impl_called));
bool ping_called = false;
ptr->Ping(base::BindRepeating(DoSetFlag, &ping_called));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(impl_called);
ASSERT_TRUE(ping_called);
impl->set_ping_handler(base::BindRepeating([] { FAIL(); }));
invalidator.reset();
ptr->Ping(base::BindRepeating([] { FAIL(); }));
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(impl_ptr);
}
TEST_F(InterfaceInvalidatorTest, InvalidateStrongBindingAfterError) {
mojo::test::blink::PingServicePtr ptr;
auto invalidator = std::make_unique<InterfaceInvalidator>();
auto impl_ptr =
MakeRevocableStrongBinding(std::make_unique<PingServiceImplBase>(),
MakeRequest(&ptr), invalidator.get());
ptr.set_connection_error_handler(base::BindOnce([] { FAIL(); }));
ptr.reset();
base::RunLoop().RunUntilIdle();
invalidator.reset();
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(impl_ptr);
}
} // namespace
} // namespace blink
|