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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "base/test/simple_test_tick_clock.h" | |
11 #include "base/win/scoped_com_initializer.h" | 12 #include "base/win/scoped_com_initializer.h" |
12 #include "media/audio/audio_manager.h" | 13 #include "media/audio/audio_manager.h" |
13 #include "media/audio/audio_unittest_util.h" | 14 #include "media/audio/audio_unittest_util.h" |
14 #include "media/audio/win/audio_device_listener_win.h" | 15 #include "media/audio/win/audio_device_listener_win.h" |
15 #include "media/audio/win/core_audio_util_win.h" | 16 #include "media/audio/win/core_audio_util_win.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 using base::win::ScopedCOMInitializer; | 20 using base::win::ScopedCOMInitializer; |
20 | 21 |
21 namespace media { | 22 namespace media { |
22 | 23 |
23 static const char kNoDevice[] = ""; | 24 static const char kNoDevice[] = ""; |
24 static const char kFirstTestDevice[] = "test_device_0"; | 25 static const char kFirstTestDevice[] = "test_device_0"; |
25 static const char kSecondTestDevice[] = "test_device_1"; | 26 static const char kSecondTestDevice[] = "test_device_1"; |
26 | 27 |
27 class AudioDeviceListenerWinTest : public testing::Test { | 28 class AudioDeviceListenerWinTest : public testing::Test { |
28 public: | 29 public: |
29 AudioDeviceListenerWinTest() { | 30 AudioDeviceListenerWinTest() { |
30 DCHECK(com_init_.succeeded()); | 31 DCHECK(com_init_.succeeded()); |
31 } | 32 } |
32 | 33 |
33 virtual void SetUp() { | 34 virtual void SetUp() { |
34 if (!CoreAudioUtil::IsSupported()) | 35 if (!CoreAudioUtil::IsSupported()) |
35 return; | 36 return; |
36 | 37 |
37 output_device_listener_.reset(new AudioDeviceListenerWin(base::Bind( | 38 output_device_listener_.reset(new AudioDeviceListenerWin(base::Bind( |
38 &AudioDeviceListenerWinTest::OnDeviceChange, base::Unretained(this)))); | 39 &AudioDeviceListenerWinTest::OnDeviceChange, base::Unretained(this)))); |
40 | |
41 tick_clock_ = new base::SimpleTestTickClock(); | |
42 tick_clock_->Advance(base::TimeDelta::FromSeconds(12345)); | |
43 output_device_listener_->tick_clock_.reset(tick_clock_); | |
44 } | |
45 | |
46 void AdvanceLastDeviceChangeTime() { | |
henrika (OOO until Aug 14)
2015/07/22 07:58:20
Perhaps add a comment here to explain why you add
| |
47 tick_clock_->Advance(base::TimeDelta::FromMilliseconds( | |
48 AudioDeviceListenerWin::kDeviceChangeLimitMs + 1)); | |
39 } | 49 } |
40 | 50 |
41 // Simulate a device change where no output devices are available. | 51 // Simulate a device change where no output devices are available. |
42 bool SimulateNullDefaultOutputDeviceChange() { | 52 bool SimulateNullDefaultOutputDeviceChange() { |
43 return output_device_listener_->OnDefaultDeviceChanged( | 53 return output_device_listener_->OnDefaultDeviceChanged( |
44 static_cast<EDataFlow>(eConsole), static_cast<ERole>(eRender), | 54 static_cast<EDataFlow>(eConsole), static_cast<ERole>(eRender), |
45 NULL) == S_OK; | 55 NULL) == S_OK; |
46 } | 56 } |
47 | 57 |
48 bool SimulateDefaultOutputDeviceChange(const char* new_device_id) { | 58 bool SimulateDefaultOutputDeviceChange(const char* new_device_id) { |
49 return output_device_listener_->OnDefaultDeviceChanged( | 59 return output_device_listener_->OnDefaultDeviceChanged( |
50 static_cast<EDataFlow>(eConsole), static_cast<ERole>(eRender), | 60 static_cast<EDataFlow>(eConsole), static_cast<ERole>(eRender), |
51 base::ASCIIToUTF16(new_device_id).c_str()) == S_OK; | 61 base::ASCIIToUTF16(new_device_id).c_str()) == S_OK; |
52 } | 62 } |
53 | 63 |
54 void SetOutputDeviceId(std::string new_device_id) { | |
55 output_device_listener_->default_render_device_id_ = new_device_id; | |
56 } | |
57 | 64 |
58 MOCK_METHOD0(OnDeviceChange, void()); | 65 MOCK_METHOD0(OnDeviceChange, void()); |
59 | 66 |
60 private: | 67 private: |
61 ScopedCOMInitializer com_init_; | 68 ScopedCOMInitializer com_init_; |
62 scoped_ptr<AudioDeviceListenerWin> output_device_listener_; | 69 scoped_ptr<AudioDeviceListenerWin> output_device_listener_; |
70 base::SimpleTestTickClock* tick_clock_; | |
63 | 71 |
64 DISALLOW_COPY_AND_ASSIGN(AudioDeviceListenerWinTest); | 72 DISALLOW_COPY_AND_ASSIGN(AudioDeviceListenerWinTest); |
65 }; | 73 }; |
66 | 74 |
67 // Simulate a device change events and ensure we get the right callbacks. | 75 // Simulate a device change events and ensure we get the right callbacks. |
68 TEST_F(AudioDeviceListenerWinTest, OutputDeviceChange) { | 76 TEST_F(AudioDeviceListenerWinTest, OutputDeviceChange) { |
69 ABORT_AUDIO_TEST_IF_NOT(CoreAudioUtil::IsSupported()); | 77 ABORT_AUDIO_TEST_IF_NOT(CoreAudioUtil::IsSupported()); |
70 | 78 |
71 SetOutputDeviceId(kNoDevice); | |
72 EXPECT_CALL(*this, OnDeviceChange()).Times(1); | 79 EXPECT_CALL(*this, OnDeviceChange()).Times(1); |
73 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kFirstTestDevice)); | 80 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kFirstTestDevice)); |
74 | 81 |
75 testing::Mock::VerifyAndClear(this); | 82 testing::Mock::VerifyAndClear(this); |
83 AdvanceLastDeviceChangeTime(); | |
76 EXPECT_CALL(*this, OnDeviceChange()).Times(1); | 84 EXPECT_CALL(*this, OnDeviceChange()).Times(1); |
77 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kSecondTestDevice)); | 85 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kSecondTestDevice)); |
78 | 86 |
79 // The second device event should be ignored since the device id has not | 87 // The second device event should be ignored since it occurs too soon. |
80 // changed. | |
81 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kSecondTestDevice)); | 88 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kSecondTestDevice)); |
82 } | 89 } |
83 | 90 |
84 // Ensure that null output device changes don't crash. Simulates the situation | 91 // Ensure that null output device changes don't crash. Simulates the situation |
85 // where we have no output devices. | 92 // where we have no output devices. |
86 TEST_F(AudioDeviceListenerWinTest, NullOutputDeviceChange) { | 93 TEST_F(AudioDeviceListenerWinTest, NullOutputDeviceChange) { |
87 ABORT_AUDIO_TEST_IF_NOT(CoreAudioUtil::IsSupported()); | 94 ABORT_AUDIO_TEST_IF_NOT(CoreAudioUtil::IsSupported()); |
88 | 95 |
89 SetOutputDeviceId(kNoDevice); | 96 EXPECT_CALL(*this, OnDeviceChange()).Times(1); |
90 EXPECT_CALL(*this, OnDeviceChange()).Times(0); | |
91 ASSERT_TRUE(SimulateNullDefaultOutputDeviceChange()); | 97 ASSERT_TRUE(SimulateNullDefaultOutputDeviceChange()); |
92 | 98 |
93 testing::Mock::VerifyAndClear(this); | 99 testing::Mock::VerifyAndClear(this); |
100 AdvanceLastDeviceChangeTime(); | |
94 EXPECT_CALL(*this, OnDeviceChange()).Times(1); | 101 EXPECT_CALL(*this, OnDeviceChange()).Times(1); |
95 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kFirstTestDevice)); | 102 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kFirstTestDevice)); |
96 | 103 |
97 testing::Mock::VerifyAndClear(this); | 104 testing::Mock::VerifyAndClear(this); |
105 AdvanceLastDeviceChangeTime(); | |
98 EXPECT_CALL(*this, OnDeviceChange()).Times(1); | 106 EXPECT_CALL(*this, OnDeviceChange()).Times(1); |
99 ASSERT_TRUE(SimulateNullDefaultOutputDeviceChange()); | 107 ASSERT_TRUE(SimulateNullDefaultOutputDeviceChange()); |
100 } | 108 } |
101 | 109 |
102 } // namespace media | 110 } // namespace media |
OLD | NEW |