diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-10 00:05:39 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-10 00:05:39 +0000 |
commit | e7a557c64a933835da445cda865a8f81bd92b8b0 (patch) | |
tree | e5fdf02f91d06f17aa34e32c6a94c82703e4c374 /media/base/mock_task.h | |
parent | e3149dbeafdcb286e24aa676b125eb9b0134ab11 (diff) | |
download | chromium_src-e7a557c64a933835da445cda865a8f81bd92b8b0.zip chromium_src-e7a557c64a933835da445cda865a8f81bd92b8b0.tar.gz chromium_src-e7a557c64a933835da445cda865a8f81bd92b8b0.tar.bz2 |
Refactor FFmpegVideoDecoder to try and generalize code common to all video decoders.
This changes the DecoderBase API to be fully asynchronous when invoking its subclass's actions.
Review URL: http://codereview.chromium.org/465044
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34208 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/base/mock_task.h')
-rw-r--r-- | media/base/mock_task.h | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/media/base/mock_task.h b/media/base/mock_task.h new file mode 100644 index 0000000..cb6e0de --- /dev/null +++ b/media/base/mock_task.h @@ -0,0 +1,110 @@ +// Copyright (c) 2009 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. + +// This file provides some utility classes that help with testing APIs which use +// callbacks. +// +// -- InvokeRunnable -- +// The InvokeRunnable is an action that can be used a gMock mock object to +// invoke the Run() method on mock argument. Example: +// +// class MockFoo : public Foo { +// public: +// MOCK_METHOD0(DoSomething, void(Task* done_cb)); +// }; +// +// EXPECT_CALL(foo, DoSomething(_)).WillOnce(WithArg<0>(InvokeRunnable())); +// +// Then you pass "foo" to something that will eventually call DoSomething(). +// The mock action will ensure that passed in done_cb is invoked. +// +// +// -- TaskMocker -- +// The TaskMocker class lets you create mock callbacks. Callbacks are +// difficult to mock because ownership of the callback object is often passed +// to the funciton being invoked. TaskMocker solves this by providing a +// GetTask() function that creates a new, single-use task that delegates to +// the originating TaskMocker object. Expectations are placed on the +// originating TaskMocker object. Each callback retrieved by GetTask() is +// tracked to ensure that it is properly deleted. The TaskMocker expects to +// outlive all the callbacks retrieved by GetTask(). +// +// Example: +// +// TaskMocker done_cb; +// EXPECT_CALL(done_cb, Run()).Times(3); +// +// func1(done_cb.GetTask()); +// func2(done_cb.GetTask()); +// func3(done_cb.GetTask()); +// +// // All 3 callbacks from GetTask() should be deleted before done_cb goes out +// // of scope. +// +// This class is not threadsafe. +// +// TODO(ajwong): Is it even worth bothering with gmock here? +// TODO(ajwong): Move MockFilterCallback here and merge the implementation +// differences. + +#ifndef MEDIA_BASE_MOCK_TASK_H_ +#define MEDIA_BASE_MOCK_TASK_H_ + +#include "base/task.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace media { + +ACTION(InvokeRunnable) { + arg0->Run(); + delete arg0; +} + +class TaskMocker { + public: + TaskMocker() + : outstanding_tasks_(0) { + } + ~TaskMocker() { + CHECK(outstanding_tasks_ == 0) + << "If outstanding_tasks_ is not zero, tasks have been leaked."; + } + + Task* CreateTask() { + return new CountingTask(this); + } + + MOCK_METHOD0(Run, void()); + + private: + friend class CountingTask; + class CountingTask : public Task { + public: + CountingTask(TaskMocker* origin) + : origin_(origin) { + origin_->outstanding_tasks_++; + } + + virtual void Run() { + origin_->Run(); + } + + virtual ~CountingTask() { + origin_->outstanding_tasks_--; + } + + private: + TaskMocker* origin_; + + DISALLOW_COPY_AND_ASSIGN(CountingTask); + }; + + int outstanding_tasks_; + + DISALLOW_COPY_AND_ASSIGN(TaskMocker); +}; + +} // namespace media + +#endif //MEDIA_BASE_MOCK_TASK_H_ |