Index: components/copresence/handlers/audio/audio_directive_handler_unittest.cc |
diff --git a/components/copresence/handlers/audio/audio_directive_handler_unittest.cc b/components/copresence/handlers/audio/audio_directive_handler_unittest.cc |
index 93b352927a76ac8ef7c81b41f5c80685524a7b4e..bb847c38a439dbc27dea7bebc9a330ecef1d715f 100644 |
--- a/components/copresence/handlers/audio/audio_directive_handler_unittest.cc |
+++ b/components/copresence/handlers/audio/audio_directive_handler_unittest.cc |
@@ -7,6 +7,8 @@ |
#include "base/bind.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/message_loop/message_loop.h" |
+#include "base/test/simple_test_tick_clock.h" |
+#include "base/timer/mock_timer.h" |
#include "components/copresence/mediums/audio/audio_manager.h" |
#include "components/copresence/test/audio_test_support.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -112,8 +114,68 @@ TEST_F(AudioDirectiveHandlerTest, Basic) { |
EXPECT_FALSE(IsRecording(INAUDIBLE)); |
} |
+TEST_F(AudioDirectiveHandlerTest, Timed) { |
+ scoped_refptr<base::SimpleTestTickClock> clock = |
+ new base::SimpleTestTickClock; |
+ directive_handler_->set_clock_for_testing(clock); |
+ |
+ scoped_ptr<base::Timer> timer(new base::MockTimer(false, false)); |
+ base::MockTimer* timer_ptr = static_cast<base::MockTimer*>(timer.get()); |
+ directive_handler_->set_timer_for_testing(timer.Pass()); |
+ |
+ const base::TimeDelta kTtl1 = base::TimeDelta::FromMilliseconds(1337); |
+ directive_handler_->AddInstruction( |
+ CreateTransmitInstruction("token", true), "op_id1", kTtl1); |
+ |
+ const base::TimeDelta kTtl2 = base::TimeDelta::FromMilliseconds(1338); |
+ directive_handler_->AddInstruction( |
+ CreateTransmitInstruction("token", false), "op_id1", kTtl2); |
+ |
+ const base::TimeDelta kTtl3 = base::TimeDelta::FromMilliseconds(1336); |
+ directive_handler_->AddInstruction( |
+ CreateReceiveInstruction(false), "op_id3", kTtl3); |
+ EXPECT_TRUE(IsPlaying(AUDIBLE)); |
+ EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
+ EXPECT_FALSE(IsRecording(AUDIBLE)); |
+ EXPECT_TRUE(IsRecording(INAUDIBLE)); |
+ |
+ // We *have* to call an operation on the directive handler after we advance |
+ // time to trigger the next set of operations, so ensure that after calling |
+ // advance, we are also calling another operation. |
+ clock->Advance(kTtl3 + base::TimeDelta::FromMilliseconds(1)); |
+ |
+ // We are now at base + 1337ms. |
+ // This instruction expires at base + (1337 + 1337 = 2674) |
+ directive_handler_->AddInstruction( |
+ CreateReceiveInstruction(true), "op_id4", kTtl1); |
+ EXPECT_TRUE(IsPlaying(AUDIBLE)); |
+ EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
+ EXPECT_TRUE(IsRecording(AUDIBLE)); |
+ EXPECT_FALSE(IsRecording(INAUDIBLE)); |
+ |
+ clock->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ |
+ // We are now at base + 1338ms. |
+ timer_ptr->Fire(); |
+ EXPECT_FALSE(IsPlaying(AUDIBLE)); |
+ EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
+ EXPECT_TRUE(IsRecording(AUDIBLE)); |
+ |
+ clock->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ |
+ // We are now at base + 1339ms. |
+ timer_ptr->Fire(); |
+ EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
+ EXPECT_TRUE(IsRecording(AUDIBLE)); |
+ |
+ clock->Advance(kTtl3); |
+ |
+ // We are now at base + 2676ms. |
+ timer_ptr->Fire(); |
+ EXPECT_FALSE(IsRecording(AUDIBLE)); |
+} |
+ |
// TODO(rkc): Write more tests that check more convoluted sequences of |
// transmits/receives. |
-// TODO(rkc): Write tests to move time forward and test functionality. |
} // namespace copresence |