OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/debug/stack_trace.h" | 6 #include "base/debug/stack_trace.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "media/base/pipeline_status.h" | 8 #include "media/base/pipeline_status.h" |
9 #include "media/base/serial_runner.h" | 9 #include "media/base/serial_runner.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 // |status|. called(i) returns whether the i'th bound function pushed to the | 28 // |status|. called(i) returns whether the i'th bound function pushed to the |
29 // queue was called while running the SerialRunner. | 29 // queue was called while running the SerialRunner. |
30 void PushBoundFunction(PipelineStatus status) { | 30 void PushBoundFunction(PipelineStatus status) { |
31 bound_fns_.Push(base::Bind(&SerialRunnerTest::RunBoundFunction, | 31 bound_fns_.Push(base::Bind(&SerialRunnerTest::RunBoundFunction, |
32 base::Unretained(this), | 32 base::Unretained(this), |
33 status, | 33 status, |
34 called_.size())); | 34 called_.size())); |
35 called_.push_back(false); | 35 called_.push_back(false); |
36 } | 36 } |
37 | 37 |
| 38 void PushBoundClosure() { |
| 39 bound_fns_.Push(base::Bind(&SerialRunnerTest::RunBoundClosure, |
| 40 base::Unretained(this), |
| 41 called_.size())); |
| 42 called_.push_back(false); |
| 43 } |
| 44 |
| 45 void PushClosure() { |
| 46 bound_fns_.Push(base::Bind(&SerialRunnerTest::RunClosure, |
| 47 base::Unretained(this), |
| 48 called_.size())); |
| 49 called_.push_back(false); |
| 50 } |
| 51 |
38 // Push a bound function to the queue that will delete the SerialRunner, | 52 // Push a bound function to the queue that will delete the SerialRunner, |
39 // which should cancel all remaining queued work. | 53 // which should cancel all remaining queued work. |
40 void PushCancellation() { | 54 void PushCancellation() { |
41 bound_fns_.Push(base::Bind(&SerialRunnerTest::CancelSerialRunner, | 55 bound_fns_.Push(base::Bind(&SerialRunnerTest::CancelSerialRunner, |
42 base::Unretained(this))); | 56 base::Unretained(this))); |
43 } | 57 } |
44 | 58 |
45 // Queries final status of pushed functions and done callback. Valid only | 59 // Queries final status of pushed functions and done callback. Valid only |
46 // after calling RunSerialRunner(). | 60 // after calling RunSerialRunner(). |
47 bool called(size_t index) { return called_[index]; } | 61 bool called(size_t index) { return called_[index]; } |
48 bool done_called() { return done_called_; } | 62 bool done_called() { return done_called_; } |
49 PipelineStatus done_status() { return done_status_; } | 63 PipelineStatus done_status() { return done_status_; } |
50 | 64 |
51 private: | 65 private: |
52 void RunBoundFunction(PipelineStatus status, | 66 void RunBoundFunction(PipelineStatus status, |
53 size_t index, | 67 size_t index, |
54 const PipelineStatusCB& status_cb) { | 68 const PipelineStatusCB& status_cb) { |
55 EXPECT_EQ(index == 0u, inside_start_) | 69 EXPECT_EQ(index == 0u, inside_start_) |
56 << "First bound function should run on same stack as " | 70 << "First bound function should run on same stack as " |
57 << "SerialRunner::Run() while all others should not\n" | 71 << "SerialRunner::Run() while all others should not\n" |
58 << base::debug::StackTrace().ToString(); | 72 << base::debug::StackTrace().ToString(); |
59 | 73 |
60 called_[index] = true; | 74 called_[index] = true; |
61 status_cb.Run(status); | 75 status_cb.Run(status); |
62 } | 76 } |
63 | 77 |
| 78 void RunBoundClosure(size_t index, |
| 79 const base::Closure& done_cb) { |
| 80 EXPECT_EQ(index == 0u, inside_start_) |
| 81 << "First bound function should run on same stack as " |
| 82 << "SerialRunner::Run() while all others should not\n" |
| 83 << base::debug::StackTrace().ToString(); |
| 84 |
| 85 called_[index] = true; |
| 86 done_cb.Run(); |
| 87 } |
| 88 |
| 89 void RunClosure(size_t index) { |
| 90 EXPECT_EQ(index == 0u, inside_start_) |
| 91 << "First bound function should run on same stack as " |
| 92 << "SerialRunner::Run() while all others should not\n" |
| 93 << base::debug::StackTrace().ToString(); |
| 94 |
| 95 called_[index] = true; |
| 96 } |
| 97 |
64 void StartRunnerInternal(const SerialRunner::Queue& bound_fns) { | 98 void StartRunnerInternal(const SerialRunner::Queue& bound_fns) { |
65 inside_start_ = true; | 99 inside_start_ = true; |
66 runner_ = SerialRunner::Run(bound_fns_, base::Bind( | 100 runner_ = SerialRunner::Run(bound_fns_, base::Bind( |
67 &SerialRunnerTest::DoneCallback, base::Unretained(this))); | 101 &SerialRunnerTest::DoneCallback, base::Unretained(this))); |
68 inside_start_ = false; | 102 inside_start_ = false; |
69 } | 103 } |
70 | 104 |
71 void DoneCallback(PipelineStatus status) { | 105 void DoneCallback(PipelineStatus status) { |
72 EXPECT_FALSE(inside_start_) | 106 EXPECT_FALSE(inside_start_) |
73 << "Done callback should not run on same stack as SerialRunner::Run()\n" | 107 << "Done callback should not run on same stack as SerialRunner::Run()\n" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 PushBoundFunction(PIPELINE_OK); | 200 PushBoundFunction(PIPELINE_OK); |
167 PushCancellation(); | 201 PushCancellation(); |
168 PushBoundFunction(PIPELINE_OK); | 202 PushBoundFunction(PIPELINE_OK); |
169 RunSerialRunner(); | 203 RunSerialRunner(); |
170 | 204 |
171 EXPECT_TRUE(called(0)); | 205 EXPECT_TRUE(called(0)); |
172 EXPECT_FALSE(called(1)); | 206 EXPECT_FALSE(called(1)); |
173 EXPECT_FALSE(done_called()); | 207 EXPECT_FALSE(done_called()); |
174 } | 208 } |
175 | 209 |
| 210 TEST_F(SerialRunnerTest, BoundClosure) { |
| 211 PushBoundClosure(); |
| 212 RunSerialRunner(); |
| 213 |
| 214 EXPECT_TRUE(called(0)); |
| 215 EXPECT_TRUE(done_called()); |
| 216 EXPECT_EQ(PIPELINE_OK, done_status()); |
| 217 } |
| 218 |
| 219 TEST_F(SerialRunnerTest, Closure) { |
| 220 PushClosure(); |
| 221 RunSerialRunner(); |
| 222 |
| 223 EXPECT_TRUE(called(0)); |
| 224 EXPECT_TRUE(done_called()); |
| 225 EXPECT_EQ(PIPELINE_OK, done_status()); |
| 226 } |
| 227 |
176 } // namespace media | 228 } // namespace media |
OLD | NEW |