OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "media/base/android/test_destruction_observable.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace media { |
| 11 |
| 12 DestructionObservable::DestructionObservable() {} |
| 13 |
| 14 DestructionObservable::~DestructionObservable() { |
| 15 if (!destruction_cb_.is_null()) |
| 16 destruction_cb_.Run(); |
| 17 } |
| 18 |
| 19 DestructionObservable::DestructionObserver::DestructionObserver() |
| 20 : weak_factory_(this) { |
| 21 // By default, destruction is okay, but not required. |
| 22 DestructionIsOptional(); |
| 23 } |
| 24 |
| 25 DestructionObservable::DestructionObserver::~DestructionObserver() {} |
| 26 |
| 27 base::Closure DestructionObservable::DestructionObserver::GetCallback() { |
| 28 return base::Bind(&DestructionObservable::DestructionObserver::OnDestroyed, |
| 29 weak_factory_.GetWeakPtr()); |
| 30 } |
| 31 |
| 32 void DestructionObservable::DestructionObserver::OnDestroyed() { |
| 33 destroyed_ = true; |
| 34 // Notify the mock. |
| 35 MockOnDestroyed(); |
| 36 } |
| 37 |
| 38 void DestructionObservable::DestructionObserver::DestructionIsOptional() { |
| 39 testing::Mock::VerifyAndClearExpectations(this); |
| 40 // We're a NiceMock, so we don't need to set any expectations. |
| 41 } |
| 42 |
| 43 void DestructionObservable::DestructionObserver::ExpectDestruction() { |
| 44 // Fail if the observerable has already been destroyed. This may seem a |
| 45 // little odd, but our semantics are "destroyed in the future". If it's |
| 46 // already gone at this point, then it's likely that the test didn't set |
| 47 // expectations properly before, unless some previous expectation of ours |
| 48 // already failed. (e.g., if the test previous told us DoNotAllowDestruction |
| 49 // but the object was destroyed, and we will fail anyway). |
| 50 ASSERT_TRUE(!destroyed_); |
| 51 testing::Mock::VerifyAndClearExpectations(this); |
| 52 EXPECT_CALL(*this, MockOnDestroyed()).Times(1); |
| 53 } |
| 54 |
| 55 void DestructionObservable::DestructionObserver::DoNotAllowDestruction() { |
| 56 testing::Mock::VerifyAndClearExpectations(this); |
| 57 // Fail if the observerable has already been destroyed. |
| 58 ASSERT_TRUE(!destroyed_); |
| 59 EXPECT_CALL(*this, MockOnDestroyed()).Times(0); |
| 60 } |
| 61 |
| 62 std::unique_ptr<DestructionObservable::DestructionObserver> |
| 63 DestructionObservable::CreateDestructionObserver() { |
| 64 DCHECK(destruction_cb_.is_null()); |
| 65 |
| 66 std::unique_ptr<DestructionObservable::DestructionObserver> observer( |
| 67 new DestructionObserver()); |
| 68 |
| 69 destruction_cb_ = observer->GetCallback(); |
| 70 |
| 71 return observer; |
| 72 } |
| 73 |
| 74 } // namespace media |
OLD | NEW |