Index: media/audio/audio_output_controller_unittest.cc |
diff --git a/media/audio/audio_output_controller_unittest.cc b/media/audio/audio_output_controller_unittest.cc |
index a68571a640d326fa8c7a9534d0b5b8c620991312..df96f4ad72613a0078bc27cc2926a23c676c6e8f 100644 |
--- a/media/audio/audio_output_controller_unittest.cc |
+++ b/media/audio/audio_output_controller_unittest.cc |
@@ -14,6 +14,7 @@ using ::testing::AtLeast; |
using ::testing::Exactly; |
using ::testing::InvokeWithoutArgs; |
using ::testing::NotNull; |
+using ::testing::Return; |
static const int kSampleRate = AudioManager::kAudioCDSampleRate; |
static const int kBitsPerSample = 16; |
@@ -68,6 +69,10 @@ static bool IsRunningHeadless() { |
return false; |
} |
+ACTION_P(SignalEvent, event) { |
+ event->Signal(); |
+} |
+ |
ACTION_P3(SignalEvent, event, count, limit) { |
if (++*count >= limit) { |
event->Signal(); |
@@ -86,7 +91,10 @@ TEST(AudioOutputControllerTest, CreateAndClose) { |
kHardwareBufferSize, kBufferCapacity); |
ASSERT_TRUE(controller.get()); |
- // Close the controller immediately. |
+ // Close the controller immediately. At this point, chances are that |
+ // DoCreate() hasn't been called yet. In any case, it should be safe to call |
+ // Close() and it should not try to call |event_handler| later (the test |
+ // would crash otherwise). |
controller->Close(); |
} |
@@ -100,7 +108,7 @@ TEST(AudioOutputControllerTest, PlayAndClose) { |
// If OnCreated is called then signal the event. |
EXPECT_CALL(event_handler, OnCreated(NotNull())) |
- .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal)); |
+ .WillOnce(SignalEvent(&event)); |
// OnPlaying() will be called only once. |
EXPECT_CALL(event_handler, OnPlaying(NotNull())) |
@@ -120,14 +128,13 @@ TEST(AudioOutputControllerTest, PlayAndClose) { |
// Wait for OnCreated() to be called. |
event.Wait(); |
- event.Reset(); |
// Play and then wait for the event to be signaled. |
controller->Play(); |
event.Wait(); |
- // Now stop the controller. This should shutdown the internal |
- // thread and we hold the only reference to it. |
+ // Now stop the controller. The object is freed later after DoClose() is |
+ // executed. |
controller->Close(); |
} |
@@ -178,8 +185,8 @@ TEST(AudioOutputControllerTest, PlayPauseClose) { |
controller->Pause(); |
event.Wait(); |
- // Now stop the controller. This should shutdown the internal |
- // thread and we hold the only reference to it. |
+ // Now stop the controller. The object is freed later after DoClose() is |
+ // executed. |
controller->Close(); |
} |
@@ -242,8 +249,8 @@ TEST(AudioOutputControllerTest, PlayPausePlay) { |
controller->Play(); |
event.Wait(); |
- // Now stop the controller. This should shutdown the internal |
- // thread and we hold the only reference to it. |
+ // Now stop the controller. The object is freed later after DoClose() is |
+ // executed. |
controller->Close(); |
} |
@@ -270,6 +277,17 @@ TEST(AudioOutputControllerTest, CloseTwice) { |
return; |
MockAudioOutputControllerEventHandler event_handler; |
+ base::WaitableEvent event(false, false); |
+ |
+ // If OnCreated is called then signal the event. |
+ EXPECT_CALL(event_handler, OnCreated(NotNull())) |
+ .WillOnce(SignalEvent(&event)); |
+ |
+ // One OnMoreData() is expected. |
+ EXPECT_CALL(event_handler, OnMoreData(NotNull(), _, 0)) |
+ .Times(AtLeast(1)) |
+ .WillRepeatedly(SignalEvent(&event)); |
+ |
scoped_refptr<AudioOutputController> controller = |
AudioOutputController::Create(&event_handler, |
AudioManager::AUDIO_PCM_LINEAR, kChannels, |
@@ -277,7 +295,12 @@ TEST(AudioOutputControllerTest, CloseTwice) { |
kHardwareBufferSize, kBufferCapacity); |
ASSERT_TRUE(controller.get()); |
- // Close the controller immediately. |
+ // Wait for OnCreated() to be called. |
+ event.Wait(); |
+ |
+ // Wait for OnMoreData() to be called. |
+ event.Wait(); |
+ |
controller->Close(); |
controller->Close(); |
} |