OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/copresence/handlers/audio/audio_directive_handler.h" | 5 #include "components/copresence/handlers/audio/audio_directive_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/test/simple_test_tick_clock.h" |
| 11 #include "base/timer/mock_timer.h" |
| 12 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" |
10 #include "components/copresence/mediums/audio/audio_manager.h" | 13 #include "components/copresence/mediums/audio/audio_manager.h" |
11 #include "components/copresence/test/audio_test_support.h" | 14 #include "components/copresence/test/audio_test_support.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
13 | 16 |
14 namespace copresence { | 17 namespace copresence { |
15 | 18 |
16 namespace { | 19 namespace { |
17 | 20 |
18 class TestAudioManager final : public AudioManager { | 21 class TestAudioManager final : public AudioManager { |
19 public: | 22 public: |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 140 |
138 directive_handler_->RemoveInstructions("op_id2"); | 141 directive_handler_->RemoveInstructions("op_id2"); |
139 EXPECT_FALSE(IsPlaying(INAUDIBLE)); | 142 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
140 EXPECT_FALSE(IsRecording(AUDIBLE)); | 143 EXPECT_FALSE(IsRecording(AUDIBLE)); |
141 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 144 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
142 | 145 |
143 directive_handler_->RemoveInstructions("op_id3"); | 146 directive_handler_->RemoveInstructions("op_id3"); |
144 EXPECT_FALSE(IsRecording(INAUDIBLE)); | 147 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
145 } | 148 } |
146 | 149 |
| 150 TEST_F(AudioDirectiveHandlerTest, Timed) { |
| 151 base::SimpleTestTickClock* clock = new base::SimpleTestTickClock(); |
| 152 scoped_refptr<TickClockRefCounted> clock_proxy = |
| 153 new TickClockRefCounted(clock); |
| 154 directive_handler_->set_clock_for_testing(clock_proxy); |
| 155 |
| 156 scoped_ptr<base::Timer> timer(new base::MockTimer(false, false)); |
| 157 base::MockTimer* timer_ptr = static_cast<base::MockTimer*>(timer.get()); |
| 158 directive_handler_->set_timer_for_testing(timer.Pass()); |
| 159 |
| 160 const base::TimeDelta kTtl1 = base::TimeDelta::FromMilliseconds(1337); |
| 161 directive_handler_->AddInstruction( |
| 162 CreateTransmitInstruction("token", true), "op_id1", kTtl1); |
| 163 |
| 164 const base::TimeDelta kTtl2 = base::TimeDelta::FromMilliseconds(1338); |
| 165 directive_handler_->AddInstruction( |
| 166 CreateTransmitInstruction("token", false), "op_id1", kTtl2); |
| 167 |
| 168 const base::TimeDelta kTtl3 = base::TimeDelta::FromMilliseconds(1336); |
| 169 directive_handler_->AddInstruction( |
| 170 CreateReceiveInstruction(false), "op_id3", kTtl3); |
| 171 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 172 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 173 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 174 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
| 175 |
| 176 // We *have* to call an operation on the directive handler after we advance |
| 177 // time to trigger the next set of operations, so ensure that after calling |
| 178 // advance, we are also calling another operation. |
| 179 clock->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1)); |
| 180 |
| 181 // We are now at base + 1337ms. |
| 182 // This instruction expires at base + (1337 + 1337 = 2674) |
| 183 directive_handler_->AddInstruction( |
| 184 CreateReceiveInstruction(true), "op_id4", kTtl1); |
| 185 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 186 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 187 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 188 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
| 189 |
| 190 clock->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 191 |
| 192 // We are now at base + 1338ms. |
| 193 timer_ptr->Fire(); |
| 194 EXPECT_FALSE(IsPlaying(AUDIBLE)); |
| 195 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 196 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 197 |
| 198 clock->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 199 |
| 200 // We are now at base + 1339ms. |
| 201 timer_ptr->Fire(); |
| 202 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
| 203 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 204 |
| 205 clock->Advance(kTtl3); |
| 206 |
| 207 // We are now at base + 2676ms. |
| 208 timer_ptr->Fire(); |
| 209 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 210 } |
| 211 |
147 // TODO(rkc): Write more tests that check more convoluted sequences of | 212 // TODO(rkc): Write more tests that check more convoluted sequences of |
148 // transmits/receives. | 213 // transmits/receives. |
149 // TODO(rkc): Write tests to move time forward and test functionality. | |
150 | 214 |
151 } // namespace copresence | 215 } // namespace copresence |
OLD | NEW |