diff options
author | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-06 06:58:38 +0000 |
---|---|---|
committer | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-06 06:58:38 +0000 |
commit | 1848da07b207f323d31883e565cdf1229137a075 (patch) | |
tree | 5240eecce5de5266f3a70e49fbedaaa49fd130fc /media/base/test_helpers.cc | |
parent | 300ba0c4a5d7fc82c0ab0952ff3d5596f727f611 (diff) | |
download | chromium_src-1848da07b207f323d31883e565cdf1229137a075.zip chromium_src-1848da07b207f323d31883e565cdf1229137a075.tar.gz chromium_src-1848da07b207f323d31883e565cdf1229137a075.tar.bz2 |
Replace WaitableEvents and ConditionalVariables in VideoRendererBase tests with MessageLoop.
Review URL: https://chromiumcodereview.appspot.com/11316293
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171438 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/base/test_helpers.cc')
-rw-r--r-- | media/base/test_helpers.cc | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/media/base/test_helpers.cc b/media/base/test_helpers.cc new file mode 100644 index 0000000..862c9d4 --- /dev/null +++ b/media/base/test_helpers.cc @@ -0,0 +1,99 @@ +// Copyright (c) 2012 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 "media/base/test_helpers.h" + +#include "base/bind.h" +#include "base/message_loop.h" +#include "base/test/test_timeouts.h" +#include "base/timer.h" +#include "media/base/bind_to_loop.h" + +using ::testing::_; +using ::testing::StrictMock; + +namespace media { + +// Utility mock for testing methods expecting Closures and PipelineStatusCBs. +class MockCallback : public base::RefCountedThreadSafe<MockCallback> { + public: + MockCallback(); + MOCK_METHOD0(Run, void()); + MOCK_METHOD1(RunWithStatus, void(PipelineStatus)); + + protected: + friend class base::RefCountedThreadSafe<MockCallback>; + virtual ~MockCallback(); + + private: + DISALLOW_COPY_AND_ASSIGN(MockCallback); +}; + +MockCallback::MockCallback() {} +MockCallback::~MockCallback() {} + +base::Closure NewExpectedClosure() { + StrictMock<MockCallback>* callback = new StrictMock<MockCallback>(); + EXPECT_CALL(*callback, Run()); + return base::Bind(&MockCallback::Run, callback); +} + +PipelineStatusCB NewExpectedStatusCB(PipelineStatus status) { + StrictMock<MockCallback>* callback = new StrictMock<MockCallback>(); + EXPECT_CALL(*callback, RunWithStatus(status)); + return base::Bind(&MockCallback::RunWithStatus, callback); +} + +WaitableMessageLoopEvent::WaitableMessageLoopEvent() + : message_loop_(MessageLoop::current()), + signaled_(false), + status_(PIPELINE_OK) { + DCHECK(message_loop_); +} + +WaitableMessageLoopEvent::~WaitableMessageLoopEvent() {} + +base::Closure WaitableMessageLoopEvent::GetClosure() { + DCHECK_EQ(message_loop_, MessageLoop::current()); + return BindToLoop(message_loop_->message_loop_proxy(), base::Bind( + &WaitableMessageLoopEvent::OnCallback, base::Unretained(this), + PIPELINE_OK)); +} + +PipelineStatusCB WaitableMessageLoopEvent::GetPipelineStatusCB() { + DCHECK_EQ(message_loop_, MessageLoop::current()); + return BindToLoop(message_loop_->message_loop_proxy(), base::Bind( + &WaitableMessageLoopEvent::OnCallback, base::Unretained(this))); +} + +void WaitableMessageLoopEvent::RunAndWait() { + RunAndWaitForStatus(PIPELINE_OK); +} + +void WaitableMessageLoopEvent::RunAndWaitForStatus(PipelineStatus expected) { + DCHECK_EQ(message_loop_, MessageLoop::current()); + base::Timer timer(false, false); + timer.Start(FROM_HERE, TestTimeouts::action_timeout(), base::Bind( + &WaitableMessageLoopEvent::OnTimeout, base::Unretained(this))); + + DCHECK(!signaled_) << "Already signaled"; + message_loop_->Run(); + EXPECT_TRUE(signaled_); + EXPECT_EQ(expected, status_); +} + +void WaitableMessageLoopEvent::OnCallback(PipelineStatus status) { + DCHECK_EQ(message_loop_, MessageLoop::current()); + signaled_ = true; + status_ = status; + message_loop_->QuitWhenIdle(); +} + +void WaitableMessageLoopEvent::OnTimeout() { + DCHECK_EQ(message_loop_, MessageLoop::current()); + ADD_FAILURE() << "Timed out waiting for message loop to quit"; + message_loop_->QuitWhenIdle(); +} + +} // namespace media |