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 #ifndef MEDIA_BASE_ANDROID_TEST_DESTRUCTION_OBSERVABLE_H_ |
| 6 #define MEDIA_BASE_ANDROID_TEST_DESTRUCTION_OBSERVABLE_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 |
| 12 namespace media { |
| 13 |
| 14 // Utility class to help tests set expectations about the lifetime of mocks that |
| 15 // they don't own. The mocks in question must derive from |
| 16 // DestructionObservable. The test can call CreateDestructionObserver() to |
| 17 // create the observer on which it may set expectations. By default, is is okay |
| 18 // but not required to destroy the observable. |
| 19 class DestructionObservable { |
| 20 private: |
| 21 class DestructionObserverInterface { |
| 22 public: |
| 23 DestructionObserverInterface() = default; |
| 24 virtual ~DestructionObserverInterface() {} |
| 25 |
| 26 virtual void OnDestroyed() = 0; |
| 27 |
| 28 private: |
| 29 DISALLOW_COPY_AND_ASSIGN(DestructionObserverInterface); |
| 30 }; |
| 31 |
| 32 public: |
| 33 // Mock that calls OnDestroyed when the observable is destroyed. This |
| 34 // will replace the destruction callback on the mock. |
| 35 class DestructionObserver : testing::NiceMock<DestructionObserverInterface> { |
| 36 protected: |
| 37 DestructionObserver(); |
| 38 |
| 39 public: |
| 40 ~DestructionObserver(); |
| 41 |
| 42 // Make destruction optional. This will not fail even if destruction has |
| 43 // already occured. Useful if you want to verify that ExpectDestruction() |
| 44 // has succeeded by the time this is called. |
| 45 void DestructionIsOptional(); |
| 46 |
| 47 // Indicate that the object must be destroyed before the observer is, or |
| 48 // before the expectation is changed. Normally, the only reasonable next |
| 49 // expecation is DestructionIsOptional, to verify that the codec has been |
| 50 // destroyed by a certain point in the test. |
| 51 // |
| 52 // Note that if the object has already been destroyed, then this call will |
| 53 // fail the test; either some previous expectation of ours should have |
| 54 // failed (e.g., DoNotAllowDestruction, but the object was destroyed), in |
| 55 // which case us failing is fine. Or, if no previous expectation failed, |
| 56 // then it's likely that the test didn't set expectations properly. |
| 57 void ExpectDestruction(); |
| 58 |
| 59 // Indicate that the observable may not be destroyed before the observer |
| 60 // is destroyed, or the expection is changed. This will fail immediately |
| 61 // if the observable has already been destroyed. |
| 62 void DoNotAllowDestruction(); |
| 63 |
| 64 private: |
| 65 friend class DestructionObservable; |
| 66 |
| 67 // Return a callback gthat will notify us about destruction. |
| 68 base::Closure GetCallback(); |
| 69 |
| 70 // Called by the callback when the observable is destroyed. |
| 71 void OnDestroyed(); |
| 72 |
| 73 // Called by OnDestroyed, so that we can set expectations on it. |
| 74 MOCK_METHOD0(MockOnDestroyed, void()); |
| 75 |
| 76 // Has the observable been destroyed already? |
| 77 bool destroyed_ = false; |
| 78 |
| 79 base::WeakPtrFactory<DestructionObserver> weak_factory_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(DestructionObserver); |
| 82 }; |
| 83 |
| 84 DestructionObservable(); |
| 85 virtual ~DestructionObservable(); |
| 86 |
| 87 // Create a DestructionObserver for us. There can be only one. |
| 88 std::unique_ptr<DestructionObserver> CreateDestructionObserver(); |
| 89 |
| 90 private: |
| 91 // Callback that we'll call when we're destroyed. |
| 92 base::Closure destruction_cb_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(DestructionObservable); |
| 95 }; |
| 96 |
| 97 } // namespace media |
| 98 |
| 99 #endif // MEDIA_BASE_ANDROID_TEST_DESTRUCTION_OBSERVABLE_H_ |
OLD | NEW |