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" |
10 #include "components/copresence/mediums/audio/audio_manager.h" | 12 #include "components/copresence/mediums/audio/audio_manager.h" |
11 #include "components/copresence/test/audio_test_support.h" | 13 #include "components/copresence/test/audio_test_support.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 namespace copresence { | 16 namespace copresence { |
15 | 17 |
16 class AudioDirectiveHandlerTest : public testing::Test { | 18 class AudioDirectiveHandlerTest : public testing::Test { |
17 public: | 19 public: |
18 AudioDirectiveHandlerTest() | 20 AudioDirectiveHandlerTest() |
19 : directive_handler_(new AudioDirectiveHandler()) { | 21 : directive_handler_(new AudioDirectiveHandler()) { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 107 |
106 directive_handler_->RemoveInstructions("op_id2"); | 108 directive_handler_->RemoveInstructions("op_id2"); |
107 EXPECT_FALSE(IsPlaying(INAUDIBLE)); | 109 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
108 EXPECT_FALSE(IsRecording(AUDIBLE)); | 110 EXPECT_FALSE(IsRecording(AUDIBLE)); |
109 EXPECT_TRUE(IsRecording(INAUDIBLE)); | 111 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
110 | 112 |
111 directive_handler_->RemoveInstructions("op_id3"); | 113 directive_handler_->RemoveInstructions("op_id3"); |
112 EXPECT_FALSE(IsRecording(INAUDIBLE)); | 114 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
113 } | 115 } |
114 | 116 |
| 117 TEST_F(AudioDirectiveHandlerTest, Timed) { |
| 118 scoped_refptr<base::SimpleTestTickClock> clock = |
| 119 new base::SimpleTestTickClock; |
| 120 directive_handler_->set_clock_for_testing(clock); |
| 121 |
| 122 scoped_ptr<base::Timer> timer(new base::MockTimer(false, false)); |
| 123 base::MockTimer* timer_ptr = static_cast<base::MockTimer*>(timer.get()); |
| 124 directive_handler_->set_timer_for_testing(timer.Pass()); |
| 125 |
| 126 const base::TimeDelta kTtl1 = base::TimeDelta::FromMilliseconds(1337); |
| 127 directive_handler_->AddInstruction( |
| 128 CreateTransmitInstruction("token", true), "op_id1", kTtl1); |
| 129 |
| 130 const base::TimeDelta kTtl2 = base::TimeDelta::FromMilliseconds(1338); |
| 131 directive_handler_->AddInstruction( |
| 132 CreateTransmitInstruction("token", false), "op_id1", kTtl2); |
| 133 |
| 134 const base::TimeDelta kTtl3 = base::TimeDelta::FromMilliseconds(1336); |
| 135 directive_handler_->AddInstruction( |
| 136 CreateReceiveInstruction(false), "op_id3", kTtl3); |
| 137 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 138 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 139 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 140 EXPECT_TRUE(IsRecording(INAUDIBLE)); |
| 141 |
| 142 // We *have* to call an operation on the directive handler after we advance |
| 143 // time to trigger the next set of operations, so ensure that after calling |
| 144 // advance, we are also calling another operation. |
| 145 clock->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1)); |
| 146 |
| 147 // We are now at base + 1337ms. |
| 148 // This instruction expires at base + (1337 + 1337 = 2674) |
| 149 directive_handler_->AddInstruction( |
| 150 CreateReceiveInstruction(true), "op_id4", kTtl1); |
| 151 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 152 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 153 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 154 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
| 155 |
| 156 clock->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 157 |
| 158 // We are now at base + 1338ms. |
| 159 timer_ptr->Fire(); |
| 160 EXPECT_FALSE(IsPlaying(AUDIBLE)); |
| 161 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 162 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 163 |
| 164 clock->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 165 |
| 166 // We are now at base + 1339ms. |
| 167 timer_ptr->Fire(); |
| 168 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
| 169 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 170 |
| 171 clock->Advance(kTtl3); |
| 172 |
| 173 // We are now at base + 2676ms. |
| 174 timer_ptr->Fire(); |
| 175 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 176 } |
| 177 |
115 // TODO(rkc): Write more tests that check more convoluted sequences of | 178 // TODO(rkc): Write more tests that check more convoluted sequences of |
116 // transmits/receives. | 179 // transmits/receives. |
117 // TODO(rkc): Write tests to move time forward and test functionality. | |
118 | 180 |
119 } // namespace copresence | 181 } // namespace copresence |
OLD | NEW |