Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: components/copresence/handlers/audio/audio_directive_handler_unittest.cc

Issue 461803003: Stop playing/recording when not needed. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "components/copresence/mediums/audio/audio_player.h"
10 #include "components/copresence/mediums/audio/audio_recorder.h"
9 #include "components/copresence/test/audio_test_support.h" 11 #include "components/copresence/test/audio_test_support.h"
10 #include "media/base/audio_bus.h" 12 #include "media/base/audio_bus.h"
11 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
13 15
14 using ::testing::_; 16 using ::testing::_;
15 using ::testing::Le; 17 using ::testing::Le;
16 18
17 namespace copresence { 19 namespace copresence {
18 20
19 class MockAudioDirectiveHandler : public AudioDirectiveHandler { 21 class TestAudioPlayer : public AudioPlayer {
20 public: 22 public:
21 MockAudioDirectiveHandler( 23 TestAudioPlayer() {}
22 const AudioDirectiveList::EncodeTokenCallback& encode_cb) 24 virtual ~TestAudioPlayer() {}
23 : AudioDirectiveHandler(AudioRecorder::DecodeSamplesCallback(),
24 encode_cb) {}
25 virtual ~MockAudioDirectiveHandler() {}
26 25
27 // Mock out the play/record methods. 26 // AudioPlayer overrides:
28 MOCK_METHOD2(PlayAudio, 27 virtual void Initialize() OVERRIDE {}
29 void(const scoped_refptr<media::AudioBusRefCounted>&, 28 virtual void Play(
30 base::TimeDelta)); 29 const scoped_refptr<media::AudioBusRefCounted>& /* samples */) OVERRIDE {
31 MOCK_METHOD1(RecordAudio, void(base::TimeDelta)); 30 is_playing_ = true;
31 }
32 virtual void Stop() OVERRIDE { is_playing_ = false; }
33 virtual void Finalize() OVERRIDE { delete this; }
32 34
33 private: 35 private:
34 DISALLOW_COPY_AND_ASSIGN(MockAudioDirectiveHandler); 36 DISALLOW_COPY_AND_ASSIGN(TestAudioPlayer);
37 };
38
39 class TestAudioRecorder : public AudioRecorder {
40 public:
41 TestAudioRecorder() : AudioRecorder(AudioRecorder::DecodeSamplesCallback()) {}
42 virtual ~TestAudioRecorder() {}
43
44 // AudioRecorder overrides:
45 virtual void Initialize() OVERRIDE {}
46 virtual void Record() OVERRIDE { is_recording_ = true; }
47 virtual void Stop() OVERRIDE { is_recording_ = false; }
48 virtual void Finalize() OVERRIDE { delete this; }
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(TestAudioRecorder);
35 }; 52 };
36 53
37 class AudioDirectiveHandlerTest : public testing::Test { 54 class AudioDirectiveHandlerTest : public testing::Test {
38 public: 55 public:
39 AudioDirectiveHandlerTest() 56 AudioDirectiveHandlerTest()
40 : directive_handler_(new MockAudioDirectiveHandler( 57 : directive_handler_(new AudioDirectiveHandler(
58 AudioRecorder::DecodeSamplesCallback(),
41 base::Bind(&AudioDirectiveHandlerTest::EncodeToken, 59 base::Bind(&AudioDirectiveHandlerTest::EncodeToken,
42 base::Unretained(this)))) {} 60 base::Unretained(this)))) {
43 61 directive_handler_->set_player_audible_for_testing(new TestAudioPlayer());
62 directive_handler_->set_player_inaudible_for_testing(new TestAudioPlayer());
63 directive_handler_->set_recorder_for_testing(new TestAudioRecorder());
64 }
44 virtual ~AudioDirectiveHandlerTest() {} 65 virtual ~AudioDirectiveHandlerTest() {}
45 66
46 void DirectiveAdded() {} 67 void DirectiveAdded() {}
47 68
48 protected: 69 protected:
49 void EncodeToken(const std::string& token, 70 void EncodeToken(const std::string& token,
50 bool audible, 71 bool audible,
51 const AudioDirectiveList::SamplesCallback& callback) { 72 const AudioDirectiveHandler::SamplesCallback& callback) {
52 callback.Run( 73 callback.Run(
53 token, audible, CreateRandomAudioRefCounted(0x1337, 1, 0x7331)); 74 token, audible, CreateRandomAudioRefCounted(0x1337, 1, 0x7331));
54 } 75 }
55 76
56 copresence::TokenInstruction CreateTransmitInstruction( 77 copresence::TokenInstruction CreateTransmitInstruction(
57 const std::string& token) { 78 const std::string& token,
79 bool audible) {
58 copresence::TokenInstruction instruction; 80 copresence::TokenInstruction instruction;
59 instruction.set_token_instruction_type(copresence::TRANSMIT); 81 instruction.set_token_instruction_type(copresence::TRANSMIT);
60 instruction.set_token_id(token); 82 instruction.set_token_id(token);
83 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF
84 : AUDIO_ULTRASOUND_PASSBAND);
61 return instruction; 85 return instruction;
62 } 86 }
63 87
64 copresence::TokenInstruction CreateReceiveInstruction() { 88 copresence::TokenInstruction CreateReceiveInstruction() {
65 copresence::TokenInstruction instruction; 89 copresence::TokenInstruction instruction;
66 instruction.set_token_instruction_type(copresence::RECEIVE); 90 instruction.set_token_instruction_type(copresence::RECEIVE);
67 return instruction; 91 return instruction;
68 } 92 }
69 93
70 // This order is important. We want the message loop to get created before 94 // This order is important. We want the message loop to get created before
71 // our the audio directive handler since the directive list ctor (invoked 95 // our the audio directive handler since the directive list ctor (invoked
72 // from the directive handler ctor) will post tasks. 96 // from the directive handler ctor) will post tasks.
73 base::MessageLoop message_loop_; 97 base::MessageLoop message_loop_;
74 scoped_ptr<MockAudioDirectiveHandler> directive_handler_; 98 scoped_ptr<AudioDirectiveHandler> directive_handler_;
75 99
76 private: 100 private:
77 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); 101 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest);
78 }; 102 };
79 103
80 // TODO(rkc): This test is broken, possibly due to the changes for audible. 104 TEST_F(AudioDirectiveHandlerTest, Basic) {
81 TEST_F(AudioDirectiveHandlerTest, DISABLED_Basic) { 105 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999);
82 const base::TimeDelta kSmallTtl = base::TimeDelta::FromMilliseconds(0x1337); 106 directive_handler_->AddInstruction(
83 const base::TimeDelta kLargeTtl = base::TimeDelta::FromSeconds(0x7331); 107 CreateTransmitInstruction("token", true), "op_id1", kTtl);
108 directive_handler_->AddInstruction(
109 CreateTransmitInstruction("token", false), "op_id1", kTtl);
110 directive_handler_->AddInstruction(
111 CreateTransmitInstruction("token", false), "op_id2", kTtl);
112 directive_handler_->AddInstruction(
113 CreateReceiveInstruction(), "op_id1", kTtl);
114 directive_handler_->AddInstruction(
115 CreateReceiveInstruction(), "op_id2", kTtl);
116 directive_handler_->AddInstruction(
117 CreateReceiveInstruction(), "op_id3", kTtl);
84 118
85 // Expect to play and record instructions for 'less' than the TTL specified, 119 EXPECT_EQ(true, directive_handler_->player_audible_->IsPlaying());
86 // since by the time that the token would have gotten encoded, we would 120 EXPECT_EQ(true, directive_handler_->player_inaudible_->IsPlaying());
87 // have (TTL - time_to_encode) left to play on that instruction. 121 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording());
88 EXPECT_CALL(*directive_handler_, PlayAudio(_, testing::Le(kLargeTtl)))
89 .Times(3);
90 directive_handler_->AddInstruction(CreateTransmitInstruction("token1"),
91 kLargeTtl);
92 directive_handler_->AddInstruction(CreateTransmitInstruction("token2"),
93 kLargeTtl);
94 directive_handler_->AddInstruction(CreateTransmitInstruction("token3"),
95 kSmallTtl);
96 122
97 EXPECT_CALL(*directive_handler_, RecordAudio(Le(kLargeTtl))).Times(3); 123 directive_handler_->RemoveInstructions("op_id1");
98 directive_handler_->AddInstruction(CreateReceiveInstruction(), kLargeTtl); 124 EXPECT_EQ(false, directive_handler_->player_audible_->IsPlaying());
99 directive_handler_->AddInstruction(CreateReceiveInstruction(), kSmallTtl); 125 EXPECT_EQ(true, directive_handler_->player_inaudible_->IsPlaying());
100 directive_handler_->AddInstruction(CreateReceiveInstruction(), kLargeTtl); 126 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording());
127
128 directive_handler_->RemoveInstructions("op_id2");
129 EXPECT_EQ(false, directive_handler_->player_inaudible_->IsPlaying());
130 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording());
131
132 directive_handler_->RemoveInstructions("op_id3");
133 EXPECT_EQ(false, directive_handler_->recorder_->IsRecording());
101 } 134 }
102 135
103 // TODO(rkc): When we are keeping track of which token we're currently playing, 136 // TODO(rkc): Write more tests that check more convoluted sequences of
104 // add tests to make sure we don't replay if we get a token with a lower ttl 137 // transmits/receives.
105 // than the current active.
106 138
107 } // namespace copresence 139 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698