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

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

Issue 2130803002: Deleting the copresence API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <stdint.h>
6
7 #include <memory>
8 #include <string>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/test/simple_test_tick_clock.h"
16 #include "base/timer/mock_timer.h"
17 #include "components/audio_modem/public/modem.h"
18 #include "components/audio_modem/test/random_samples.h"
19 #include "components/audio_modem/test/stub_modem.h"
20 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h"
21 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h"
22 #include "components/copresence/proto/data.pb.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 using audio_modem::AUDIBLE;
26 using audio_modem::AudioType;
27 using audio_modem::INAUDIBLE;
28 using audio_modem::StubModem;
29
30 namespace copresence {
31
32 namespace {
33
34 const Directive CreateDirective(TokenInstructionType type,
35 bool audible,
36 int64_t ttl) {
37 Directive directive;
38 directive.mutable_token_instruction()->set_token_instruction_type(type);
39 directive.mutable_token_instruction()->set_token_id("token");
40 directive.mutable_token_instruction()->set_medium(audible ?
41 AUDIO_AUDIBLE_DTMF : AUDIO_ULTRASOUND_PASSBAND);
42 directive.set_ttl_millis(ttl);
43 return directive;
44 }
45
46 } // namespace
47
48 class AudioDirectiveHandlerTest : public testing::Test {
49 public:
50 AudioDirectiveHandlerTest() {
51 modem_ptr_ = new StubModem;
52 timer_ptr_ = new base::MockTimer(false, false);
53 clock_ptr_ = new base::SimpleTestTickClock;
54
55 directive_handler_.reset(new AudioDirectiveHandlerImpl(
56 base::Bind(&AudioDirectiveHandlerTest::GetDirectiveUpdates,
57 base::Unretained(this)),
58 base::WrapUnique<audio_modem::Modem>(modem_ptr_),
59 base::WrapUnique<base::Timer>(timer_ptr_),
60 make_scoped_refptr(new TickClockRefCounted(clock_ptr_))));
61 directive_handler_->Initialize(nullptr, audio_modem::TokensCallback());
62 }
63 ~AudioDirectiveHandlerTest() override {}
64
65 protected:
66 const std::vector<Directive>& current_directives() {
67 return current_directives_;
68 }
69
70 bool IsPlaying(AudioType type) { return modem_ptr_->IsPlaying(type); }
71
72 bool IsRecording(AudioType type) { return modem_ptr_->IsRecording(type); }
73
74 // This order is important. We want the message loop to get created before
75 // our the audio directive handler since the directive list ctor (invoked
76 // from the directive handler ctor) will post tasks.
77 base::MessageLoop message_loop_;
78 std::unique_ptr<AudioDirectiveHandler> directive_handler_;
79
80 std::vector<Directive> current_directives_;
81
82 // Unowned.
83 StubModem* modem_ptr_;
84 base::MockTimer* timer_ptr_;
85 base::SimpleTestTickClock* clock_ptr_;
86
87 private:
88 void GetDirectiveUpdates(const std::vector<Directive>& current_directives) {
89 current_directives_ = current_directives;
90 }
91
92 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest);
93 };
94
95 TEST_F(AudioDirectiveHandlerTest, Basic) {
96 const int64_t kTtl = 10;
97 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, true, kTtl),
98 "op_id1");
99 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, false, kTtl),
100 "op_id1");
101 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, false, kTtl),
102 "op_id2");
103 directive_handler_->AddInstruction(CreateDirective(RECEIVE, false, kTtl),
104 "op_id1");
105 directive_handler_->AddInstruction(CreateDirective(RECEIVE, true, kTtl),
106 "op_id2");
107 directive_handler_->AddInstruction(CreateDirective(RECEIVE, false, kTtl),
108 "op_id3");
109
110 EXPECT_TRUE(IsPlaying(AUDIBLE));
111 EXPECT_TRUE(IsPlaying(INAUDIBLE));
112 EXPECT_TRUE(IsRecording(AUDIBLE));
113 EXPECT_TRUE(IsRecording(INAUDIBLE));
114
115 directive_handler_->RemoveInstructions("op_id1");
116 EXPECT_FALSE(IsPlaying(AUDIBLE));
117 EXPECT_TRUE(IsPlaying(INAUDIBLE));
118 EXPECT_TRUE(IsRecording(AUDIBLE));
119 EXPECT_TRUE(IsRecording(INAUDIBLE));
120
121 directive_handler_->RemoveInstructions("op_id2");
122 EXPECT_FALSE(IsPlaying(INAUDIBLE));
123 EXPECT_FALSE(IsRecording(AUDIBLE));
124 EXPECT_TRUE(IsRecording(INAUDIBLE));
125
126 directive_handler_->RemoveInstructions("op_id3");
127 EXPECT_FALSE(IsRecording(INAUDIBLE));
128 }
129
130 TEST_F(AudioDirectiveHandlerTest, Timed) {
131 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, true, 6),
132 "op_id1");
133 directive_handler_->AddInstruction(CreateDirective(TRANSMIT, false, 8),
134 "op_id1");
135 directive_handler_->AddInstruction(CreateDirective(RECEIVE, false, 4),
136 "op_id3");
137
138 EXPECT_TRUE(IsPlaying(AUDIBLE));
139 EXPECT_TRUE(IsPlaying(INAUDIBLE));
140 EXPECT_FALSE(IsRecording(AUDIBLE));
141 EXPECT_TRUE(IsRecording(INAUDIBLE));
142
143 // Every time we advance and a directive expires, the timer should fire also.
144 clock_ptr_->Advance(base::TimeDelta::FromMilliseconds(5));
145 timer_ptr_->Fire();
146
147 // We are now at +5ms. This instruction expires at +10ms.
148 directive_handler_->AddInstruction(CreateDirective(RECEIVE, true, 5),
149 "op_id4");
150 EXPECT_TRUE(IsPlaying(AUDIBLE));
151 EXPECT_TRUE(IsPlaying(INAUDIBLE));
152 EXPECT_TRUE(IsRecording(AUDIBLE));
153 EXPECT_FALSE(IsRecording(INAUDIBLE));
154
155 // Advance to +7ms.
156 const base::TimeDelta twoMs = base::TimeDelta::FromMilliseconds(2);
157 clock_ptr_->Advance(twoMs);
158 timer_ptr_->Fire();
159
160 EXPECT_FALSE(IsPlaying(AUDIBLE));
161 EXPECT_TRUE(IsPlaying(INAUDIBLE));
162 EXPECT_TRUE(IsRecording(AUDIBLE));
163
164 // Advance to +9ms.
165 clock_ptr_->Advance(twoMs);
166 timer_ptr_->Fire();
167 EXPECT_FALSE(IsPlaying(INAUDIBLE));
168 EXPECT_TRUE(IsRecording(AUDIBLE));
169
170 // Advance to +11ms.
171 clock_ptr_->Advance(twoMs);
172 timer_ptr_->Fire();
173 EXPECT_FALSE(IsRecording(AUDIBLE));
174 }
175
176 // TODO(rkc): Write more tests that check more convoluted sequences of
177 // transmits/receives.
178
179 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698