| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // A new breed of mock media filters, this time using gmock! Feel free to add | 5 // A new breed of mock media filters, this time using gmock! Feel free to add |
| 6 // actions if you need interesting side-effects (i.e., copying data to the | 6 // actions if you need interesting side-effects (i.e., copying data to the |
| 7 // buffer passed into MockDataSource::Read()). | 7 // buffer passed into MockDataSource::Read()). |
| 8 // | 8 // |
| 9 // Don't forget you can use StrictMock<> and NiceMock<> if you want the mock | 9 // Don't forget you can use StrictMock<> and NiceMock<> if you want the mock |
| 10 // filters to fail the test or do nothing when an unexpected method is called. | 10 // filters to fail the test or do nothing when an unexpected method is called. |
| 11 // http://code.google.com/p/googlemock/wiki/CookBook#Nice_Mocks_and_Strict_Mocks | 11 // http://code.google.com/p/googlemock/wiki/CookBook#Nice_Mocks_and_Strict_Mocks |
| 12 | 12 |
| 13 #ifndef MEDIA_BASE_MOCK_FILTERS_H_ | 13 #ifndef MEDIA_BASE_MOCK_FILTERS_H_ |
| 14 #define MEDIA_BASE_MOCK_FILTERS_H_ | 14 #define MEDIA_BASE_MOCK_FILTERS_H_ |
| 15 | 15 |
| 16 #include <string> | 16 #include <string> |
| 17 | 17 |
| 18 #include "base/callback.h" | |
| 19 #include "media/base/filters.h" | 18 #include "media/base/filters.h" |
| 20 #include "media/base/filter_collection.h" | 19 #include "media/base/filter_collection.h" |
| 21 #include "media/base/video_frame.h" | 20 #include "media/base/video_frame.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 23 | 22 |
| 24 namespace media { | 23 namespace media { |
| 25 | 24 |
| 26 // Use this template to test for object destruction by setting expectations on | 25 // Use this template to test for object destruction by setting expectations on |
| 27 // the method OnDestroy(). | 26 // the method OnDestroy(). |
| 28 // | 27 // |
| 29 // TODO(scherkus): not sure about the naming... perhaps contribute this back | 28 // TODO(scherkus): not sure about the naming... perhaps contribute this back |
| 30 // to gmock itself! | 29 // to gmock itself! |
| 31 template<class MockClass> | 30 template<class MockClass> |
| 32 class Destroyable : public MockClass { | 31 class Destroyable : public MockClass { |
| 33 public: | 32 public: |
| 34 Destroyable() {} | 33 Destroyable() {} |
| 35 | 34 |
| 36 MOCK_METHOD0(OnDestroy, void()); | 35 MOCK_METHOD0(OnDestroy, void()); |
| 37 | 36 |
| 38 protected: | 37 protected: |
| 39 virtual ~Destroyable() { | 38 virtual ~Destroyable() { |
| 40 OnDestroy(); | 39 OnDestroy(); |
| 41 } | 40 } |
| 42 | 41 |
| 43 private: | 42 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(Destroyable); | 43 DISALLOW_COPY_AND_ASSIGN(Destroyable); |
| 45 }; | 44 }; |
| 46 | 45 |
| 47 // Helper class used to test that callbacks are executed. It is recommend you | |
| 48 // combine this class with StrictMock<> to verify that the callback is executed. | |
| 49 // You can reuse the same instance of a MockFilterCallback many times since | |
| 50 // gmock will track the number of times the methods are executed. | |
| 51 class MockFilterCallback { | |
| 52 public: | |
| 53 MockFilterCallback(); | |
| 54 MockFilterCallback(bool run_destroy_callback); | |
| 55 virtual ~MockFilterCallback(); | |
| 56 | |
| 57 MOCK_METHOD0(OnCallbackDestroyed, void()); | |
| 58 MOCK_METHOD0(OnFilterCallback, void()); | |
| 59 | |
| 60 // Helper method to create a new callback for this mock. The callback will | |
| 61 // call OnFilterCallback() when executed and OnCallbackDestroyed() when | |
| 62 // destroyed. Clients should use NiceMock<> or StrictMock<> depending on the | |
| 63 // test. | |
| 64 FilterCallback* NewCallback(); | |
| 65 | |
| 66 private: | |
| 67 // Private implementation of CallbackRunner used to trigger expectations on | |
| 68 // MockFilterCallback. | |
| 69 class CallbackImpl : public CallbackRunner<Tuple0> { | |
| 70 public: | |
| 71 explicit CallbackImpl(MockFilterCallback* mock_callback, | |
| 72 bool run_destroy_callback) | |
| 73 : mock_callback_(mock_callback), | |
| 74 run_destroy_callback_(run_destroy_callback) { | |
| 75 } | |
| 76 | |
| 77 virtual ~CallbackImpl() { | |
| 78 if (run_destroy_callback_) | |
| 79 mock_callback_->OnCallbackDestroyed(); | |
| 80 } | |
| 81 | |
| 82 virtual void RunWithParams(const Tuple0& params) { | |
| 83 mock_callback_->OnFilterCallback(); | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 MockFilterCallback* mock_callback_; | |
| 88 bool run_destroy_callback_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(CallbackImpl); | |
| 91 }; | |
| 92 | |
| 93 bool run_destroy_callback_; | |
| 94 DISALLOW_COPY_AND_ASSIGN(MockFilterCallback); | |
| 95 }; | |
| 96 | |
| 97 class MockFilter : public Filter { | 46 class MockFilter : public Filter { |
| 98 public: | 47 public: |
| 99 MockFilter(); | 48 MockFilter(); |
| 100 MockFilter(bool requires_message_loop); | 49 MockFilter(bool requires_message_loop); |
| 101 | 50 |
| 102 // Filter implementation. | 51 // Filter implementation. |
| 103 virtual bool requires_message_loop() const; | 52 virtual bool requires_message_loop() const; |
| 104 virtual const char* message_loop_name() const; | 53 virtual const char* message_loop_name() const; |
| 105 | 54 |
| 106 MOCK_METHOD1(Play, void(FilterCallback* callback)); | 55 MOCK_METHOD1(Play, void(FilterCallback* callback)); |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 | 314 |
| 366 // Helper gmock action that calls DisableAudioRenderer() on behalf of the | 315 // Helper gmock action that calls DisableAudioRenderer() on behalf of the |
| 367 // provided filter. | 316 // provided filter. |
| 368 ACTION_P(DisableAudioRenderer, filter) { | 317 ACTION_P(DisableAudioRenderer, filter) { |
| 369 filter->host()->DisableAudioRenderer(); | 318 filter->host()->DisableAudioRenderer(); |
| 370 } | 319 } |
| 371 | 320 |
| 372 } // namespace media | 321 } // namespace media |
| 373 | 322 |
| 374 #endif // MEDIA_BASE_MOCK_FILTERS_H_ | 323 #endif // MEDIA_BASE_MOCK_FILTERS_H_ |
| OLD | NEW |