1 // Copyright 2013 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.
6 #include "base/debug/stack_trace.h"
7 #include "base/message_loop/message_loop.h"
8 #include "media/base/pipeline_status.h"
9 #include "media/base/serial_runner.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class SerialRunnerTest
: public ::testing::Test
{
17 : inside_start_(false), done_called_(false), done_status_(PIPELINE_OK
) {}
18 virtual ~SerialRunnerTest() {}
20 void RunSerialRunner() {
21 message_loop_
.PostTask(FROM_HERE
, base::Bind(
22 &SerialRunnerTest::StartRunnerInternal
, base::Unretained(this),
24 message_loop_
.RunUntilIdle();
27 // Pushes a bound function to the queue that will run its callback with
28 // |status|. called(i) returns whether the i'th bound function pushed to the
29 // queue was called while running the SerialRunner.
30 void PushBoundFunction(PipelineStatus status
) {
31 bound_fns_
.Push(base::Bind(&SerialRunnerTest::RunBoundFunction
,
32 base::Unretained(this),
35 called_
.push_back(false);
38 // Push a bound function to the queue that will delete the SerialRunner,
39 // which should cancel all remaining queued work.
40 void PushCancellation() {
41 bound_fns_
.Push(base::Bind(&SerialRunnerTest::CancelSerialRunner
,
42 base::Unretained(this)));
45 // Queries final status of pushed functions and done callback. Valid only
46 // after calling RunSerialRunner().
47 bool called(size_t index
) { return called_
[index
]; }
48 bool done_called() { return done_called_
; }
49 PipelineStatus
done_status() { return done_status_
; }
52 void RunBoundFunction(PipelineStatus status
,
54 const PipelineStatusCB
& status_cb
) {
55 EXPECT_EQ(index
== 0u, inside_start_
)
56 << "First bound function should run on same stack as "
57 << "SerialRunner::Run() while all others should not\n"
58 << base::debug::StackTrace().ToString();
60 called_
[index
] = true;
61 status_cb
.Run(status
);
64 void StartRunnerInternal(const SerialRunner::Queue
& bound_fns
) {
66 runner_
= SerialRunner::Run(bound_fns_
, base::Bind(
67 &SerialRunnerTest::DoneCallback
, base::Unretained(this)));
68 inside_start_
= false;
71 void DoneCallback(PipelineStatus status
) {
72 EXPECT_FALSE(inside_start_
)
73 << "Done callback should not run on same stack as SerialRunner::Run()\n"
74 << base::debug::StackTrace().ToString();
77 done_status_
= status
;
78 message_loop_
.QuitWhenIdle();
81 void CancelSerialRunner(const PipelineStatusCB
& status_cb
) {
82 // Tasks run by |runner_| shouldn't reset it, hence we post a task to do so.
83 message_loop_
.PostTask(FROM_HERE
, base::Bind(
84 &SerialRunnerTest::ResetSerialRunner
, base::Unretained(this)));
85 status_cb
.Run(PIPELINE_OK
);
88 void ResetSerialRunner() {
92 base::MessageLoop message_loop_
;
93 SerialRunner::Queue bound_fns_
;
94 scoped_ptr
<SerialRunner
> runner_
;
96 // Used to enforce calling stack guarantees of the API.
99 // Tracks whether the i'th bound function was called.
100 std::vector
<bool> called_
;
102 // Tracks whether the final done callback was called + resulting status.
104 PipelineStatus done_status_
;
106 DISALLOW_COPY_AND_ASSIGN(SerialRunnerTest
);
109 TEST_F(SerialRunnerTest
, Empty
) {
112 EXPECT_TRUE(done_called());
113 EXPECT_EQ(PIPELINE_OK
, done_status());
116 TEST_F(SerialRunnerTest
, Single
) {
117 PushBoundFunction(PIPELINE_OK
);
120 EXPECT_TRUE(called(0));
121 EXPECT_TRUE(done_called());
122 EXPECT_EQ(PIPELINE_OK
, done_status());
125 TEST_F(SerialRunnerTest
, Single_Error
) {
126 PushBoundFunction(PIPELINE_ERROR_ABORT
);
129 EXPECT_TRUE(called(0));
130 EXPECT_TRUE(done_called());
131 EXPECT_EQ(PIPELINE_ERROR_ABORT
, done_status());
134 TEST_F(SerialRunnerTest
, Single_Cancel
) {
135 PushBoundFunction(PIPELINE_OK
);
139 EXPECT_TRUE(called(0));
140 EXPECT_FALSE(done_called());
143 TEST_F(SerialRunnerTest
, Multiple
) {
144 PushBoundFunction(PIPELINE_OK
);
145 PushBoundFunction(PIPELINE_OK
);
148 EXPECT_TRUE(called(0));
149 EXPECT_TRUE(called(1));
150 EXPECT_TRUE(done_called());
151 EXPECT_EQ(PIPELINE_OK
, done_status());
154 TEST_F(SerialRunnerTest
, Multiple_Error
) {
155 PushBoundFunction(PIPELINE_ERROR_ABORT
);
156 PushBoundFunction(PIPELINE_OK
);
159 EXPECT_TRUE(called(0));
160 EXPECT_FALSE(called(1)); // A bad status cancels remaining work.
161 EXPECT_TRUE(done_called());
162 EXPECT_EQ(PIPELINE_ERROR_ABORT
, done_status());
165 TEST_F(SerialRunnerTest
, Multiple_Cancel
) {
166 PushBoundFunction(PIPELINE_OK
);
168 PushBoundFunction(PIPELINE_OK
);
171 EXPECT_TRUE(called(0));
172 EXPECT_FALSE(called(1));
173 EXPECT_FALSE(done_called());