| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 8 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 9 #include "media/audio/fake_audio_worker.h" | 11 #include "media/audio/fake_audio_worker.h" |
| 10 #include "media/audio/simple_sources.h" | 12 #include "media/audio/simple_sources.h" |
| 11 #include "media/base/audio_parameters.h" | 13 #include "media/base/audio_parameters.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 15 |
| 14 namespace media { | 16 namespace media { |
| 15 | 17 |
| 16 static const int kTestCallbacks = 5; | 18 static const int kTestCallbacks = 5; |
| 17 | 19 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 37 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); | 39 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); |
| 38 fake_worker_.Start(base::Bind( | 40 fake_worker_.Start(base::Bind( |
| 39 &FakeAudioWorkerTest::CalledByFakeWorker, base::Unretained(this))); | 41 &FakeAudioWorkerTest::CalledByFakeWorker, base::Unretained(this))); |
| 40 } | 42 } |
| 41 | 43 |
| 42 void RunOnceOnAudioThread() { | 44 void RunOnceOnAudioThread() { |
| 43 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); | 45 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); |
| 44 RunOnAudioThread(); | 46 RunOnAudioThread(); |
| 45 // Start() should immediately post a task to run the callback, so we | 47 // Start() should immediately post a task to run the callback, so we |
| 46 // should end up with only a single callback being run. | 48 // should end up with only a single callback being run. |
| 47 message_loop_.PostTask(FROM_HERE, base::Bind( | 49 message_loop_.task_runner()->PostTask( |
| 48 &FakeAudioWorkerTest::EndTest, base::Unretained(this), 1)); | 50 FROM_HERE, |
| 51 base::Bind(&FakeAudioWorkerTest::EndTest, base::Unretained(this), 1)); |
| 49 } | 52 } |
| 50 | 53 |
| 51 void StopStartOnAudioThread() { | 54 void StopStartOnAudioThread() { |
| 52 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); | 55 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); |
| 53 fake_worker_.Stop(); | 56 fake_worker_.Stop(); |
| 54 RunOnAudioThread(); | 57 RunOnAudioThread(); |
| 55 } | 58 } |
| 56 | 59 |
| 57 void TimeCallbacksOnAudioThread(int callbacks) { | 60 void TimeCallbacksOnAudioThread(int callbacks) { |
| 58 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); | 61 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); |
| 59 | 62 |
| 60 if (seen_callbacks_ == 0) { | 63 if (seen_callbacks_ == 0) { |
| 61 RunOnAudioThread(); | 64 RunOnAudioThread(); |
| 62 start_time_ = base::TimeTicks::Now(); | 65 start_time_ = base::TimeTicks::Now(); |
| 63 } | 66 } |
| 64 | 67 |
| 65 // Keep going until we've seen the requested number of callbacks. | 68 // Keep going until we've seen the requested number of callbacks. |
| 66 if (seen_callbacks_ < callbacks) { | 69 if (seen_callbacks_ < callbacks) { |
| 67 message_loop_.PostDelayedTask(FROM_HERE, base::Bind( | 70 message_loop_.task_runner()->PostDelayedTask( |
| 68 &FakeAudioWorkerTest::TimeCallbacksOnAudioThread, | 71 FROM_HERE, |
| 69 base::Unretained(this), callbacks), time_between_callbacks_ / 2); | 72 base::Bind(&FakeAudioWorkerTest::TimeCallbacksOnAudioThread, |
| 73 base::Unretained(this), callbacks), |
| 74 time_between_callbacks_ / 2); |
| 70 } else { | 75 } else { |
| 71 end_time_ = base::TimeTicks::Now(); | 76 end_time_ = base::TimeTicks::Now(); |
| 72 EndTest(callbacks); | 77 EndTest(callbacks); |
| 73 } | 78 } |
| 74 } | 79 } |
| 75 | 80 |
| 76 void EndTest(int callbacks) { | 81 void EndTest(int callbacks) { |
| 77 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); | 82 ASSERT_TRUE(message_loop_.task_runner()->BelongsToCurrentThread()); |
| 78 fake_worker_.Stop(); | 83 fake_worker_.Stop(); |
| 79 EXPECT_LE(callbacks, seen_callbacks_); | 84 EXPECT_LE(callbacks, seen_callbacks_); |
| 80 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 85 message_loop_.task_runner()->PostTask( |
| 86 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 81 } | 87 } |
| 82 | 88 |
| 83 protected: | 89 protected: |
| 84 base::MessageLoop message_loop_; | 90 base::MessageLoop message_loop_; |
| 85 AudioParameters params_; | 91 AudioParameters params_; |
| 86 FakeAudioWorker fake_worker_; | 92 FakeAudioWorker fake_worker_; |
| 87 base::TimeTicks start_time_; | 93 base::TimeTicks start_time_; |
| 88 base::TimeTicks end_time_; | 94 base::TimeTicks end_time_; |
| 89 base::TimeDelta time_between_callbacks_; | 95 base::TimeDelta time_between_callbacks_; |
| 90 int seen_callbacks_; | 96 int seen_callbacks_; |
| 91 | 97 |
| 92 private: | 98 private: |
| 93 DISALLOW_COPY_AND_ASSIGN(FakeAudioWorkerTest); | 99 DISALLOW_COPY_AND_ASSIGN(FakeAudioWorkerTest); |
| 94 }; | 100 }; |
| 95 | 101 |
| 96 // Ensure the worker runs on the audio thread and fires callbacks. | 102 // Ensure the worker runs on the audio thread and fires callbacks. |
| 97 TEST_F(FakeAudioWorkerTest, FakeBasicCallback) { | 103 TEST_F(FakeAudioWorkerTest, FakeBasicCallback) { |
| 98 message_loop_.PostTask(FROM_HERE, base::Bind( | 104 message_loop_.task_runner()->PostTask( |
| 99 &FakeAudioWorkerTest::RunOnceOnAudioThread, | 105 FROM_HERE, base::Bind(&FakeAudioWorkerTest::RunOnceOnAudioThread, |
| 100 base::Unretained(this))); | 106 base::Unretained(this))); |
| 101 message_loop_.Run(); | 107 base::RunLoop().Run(); |
| 102 } | 108 } |
| 103 | 109 |
| 104 // Ensure the time between callbacks is sane. | 110 // Ensure the time between callbacks is sane. |
| 105 TEST_F(FakeAudioWorkerTest, TimeBetweenCallbacks) { | 111 TEST_F(FakeAudioWorkerTest, TimeBetweenCallbacks) { |
| 106 message_loop_.PostTask(FROM_HERE, base::Bind( | 112 message_loop_.task_runner()->PostTask( |
| 107 &FakeAudioWorkerTest::TimeCallbacksOnAudioThread, | 113 FROM_HERE, base::Bind(&FakeAudioWorkerTest::TimeCallbacksOnAudioThread, |
| 108 base::Unretained(this), kTestCallbacks)); | 114 base::Unretained(this), kTestCallbacks)); |
| 109 message_loop_.Run(); | 115 base::RunLoop().Run(); |
| 110 | 116 |
| 111 // There are only (kTestCallbacks - 1) intervals between kTestCallbacks. | 117 // There are only (kTestCallbacks - 1) intervals between kTestCallbacks. |
| 112 base::TimeDelta actual_time_between_callbacks = | 118 base::TimeDelta actual_time_between_callbacks = |
| 113 (end_time_ - start_time_) / (seen_callbacks_ - 1); | 119 (end_time_ - start_time_) / (seen_callbacks_ - 1); |
| 114 | 120 |
| 115 // Ensure callback time is no faster than the expected time between callbacks. | 121 // Ensure callback time is no faster than the expected time between callbacks. |
| 116 EXPECT_GE(actual_time_between_callbacks, time_between_callbacks_); | 122 EXPECT_GE(actual_time_between_callbacks, time_between_callbacks_); |
| 117 | 123 |
| 118 // Softly check if the callback time is no slower than twice the expected time | 124 // Softly check if the callback time is no slower than twice the expected time |
| 119 // between callbacks. Since this test runs on the bots we can't be too strict | 125 // between callbacks. Since this test runs on the bots we can't be too strict |
| 120 // with the bounds. | 126 // with the bounds. |
| 121 if (actual_time_between_callbacks > 2 * time_between_callbacks_) | 127 if (actual_time_between_callbacks > 2 * time_between_callbacks_) |
| 122 LOG(ERROR) << "Time between fake audio callbacks is too large!"; | 128 LOG(ERROR) << "Time between fake audio callbacks is too large!"; |
| 123 } | 129 } |
| 124 | 130 |
| 125 // Ensure Start()/Stop() on the worker doesn't generate too many callbacks. See | 131 // Ensure Start()/Stop() on the worker doesn't generate too many callbacks. See |
| 126 // http://crbug.com/159049. | 132 // http://crbug.com/159049. |
| 127 TEST_F(FakeAudioWorkerTest, StartStopClearsCallbacks) { | 133 TEST_F(FakeAudioWorkerTest, StartStopClearsCallbacks) { |
| 128 message_loop_.PostTask(FROM_HERE, base::Bind( | 134 message_loop_.task_runner()->PostTask( |
| 129 &FakeAudioWorkerTest::TimeCallbacksOnAudioThread, | 135 FROM_HERE, base::Bind(&FakeAudioWorkerTest::TimeCallbacksOnAudioThread, |
| 130 base::Unretained(this), kTestCallbacks)); | 136 base::Unretained(this), kTestCallbacks)); |
| 131 | 137 |
| 132 // Issue a Stop() / Start() in between expected callbacks to maximize the | 138 // Issue a Stop() / Start() in between expected callbacks to maximize the |
| 133 // chance of catching the worker doing the wrong thing. | 139 // chance of catching the worker doing the wrong thing. |
| 134 message_loop_.PostDelayedTask(FROM_HERE, base::Bind( | 140 message_loop_.task_runner()->PostDelayedTask( |
| 135 &FakeAudioWorkerTest::StopStartOnAudioThread, | 141 FROM_HERE, base::Bind(&FakeAudioWorkerTest::StopStartOnAudioThread, |
| 136 base::Unretained(this)), time_between_callbacks_ / 2); | 142 base::Unretained(this)), |
| 143 time_between_callbacks_ / 2); |
| 137 | 144 |
| 138 // EndTest() will ensure the proper number of callbacks have occurred. | 145 // EndTest() will ensure the proper number of callbacks have occurred. |
| 139 message_loop_.Run(); | 146 base::RunLoop().Run(); |
| 140 } | 147 } |
| 141 | 148 |
| 142 } // namespace media | 149 } // namespace media |
| OLD | NEW |