henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/dtmf_sender.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/fake_clock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/gunit.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/ref_counted_object.h" |
| 22 | #include "rtc_base/time_utils.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "test/gtest.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 24 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | using webrtc::DtmfProviderInterface; |
| 26 | using webrtc::DtmfSender; |
| 27 | using webrtc::DtmfSenderObserverInterface; |
| 28 | |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 29 | // TODO(deadbeef): Even though this test now uses a fake clock, it has a |
| 30 | // generous 3-second timeout for every test case. The timeout could be tuned |
| 31 | // to each test based on the tones sent, instead. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | static const int kMaxWaitMs = 3000; |
| 33 | |
Magnus Jedvert | fc95084 | 2015-10-12 16:10:43 +0200 | [diff] [blame] | 34 | class FakeDtmfObserver : public DtmfSenderObserverInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | public: |
| 36 | FakeDtmfObserver() : completed_(false) {} |
| 37 | |
| 38 | // Implements DtmfSenderObserverInterface. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 39 | void OnToneChange(const std::string& tone) override { |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 40 | tones_from_single_argument_callback_.push_back(tone); |
| 41 | if (tone.empty()) { |
| 42 | completed_ = true; |
| 43 | } |
| 44 | } |
| 45 | void OnToneChange(const std::string& tone, |
| 46 | const std::string& tone_buffer) override { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | tones_.push_back(tone); |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 48 | tones_remaining_ = tone_buffer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | if (tone.empty()) { |
| 50 | completed_ = true; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // getters |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 55 | const std::vector<std::string>& tones() const { return tones_; } |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 56 | const std::vector<std::string>& tones_from_single_argument_callback() const { |
| 57 | return tones_from_single_argument_callback_; |
| 58 | } |
| 59 | const std::string tones_remaining() { return tones_remaining_; } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | bool completed() const { return completed_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | std::vector<std::string> tones_; |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 64 | std::vector<std::string> tones_from_single_argument_callback_; |
| 65 | std::string tones_remaining_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | bool completed_; |
| 67 | }; |
| 68 | |
| 69 | class FakeDtmfProvider : public DtmfProviderInterface { |
| 70 | public: |
| 71 | struct DtmfInfo { |
| 72 | DtmfInfo(int code, int duration, int gap) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | : code(code), duration(duration), gap(gap) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | int code; |
| 75 | int duration; |
| 76 | int gap; |
| 77 | }; |
| 78 | |
| 79 | FakeDtmfProvider() : last_insert_dtmf_call_(0) {} |
| 80 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 81 | ~FakeDtmfProvider() { SignalDestroyed(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | |
| 83 | // Implements DtmfProviderInterface. |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 84 | bool CanInsertDtmf() override { return can_insert_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 86 | bool InsertDtmf(int code, int duration) override { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | int gap = 0; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 88 | // TODO(ronghuawu): Make the timer (basically the rtc::TimeNanos) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | // mockable and use a fake timer in the unit tests. |
| 90 | if (last_insert_dtmf_call_ > 0) { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 91 | gap = static_cast<int>(rtc::TimeMillis() - last_insert_dtmf_call_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | } |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 93 | last_insert_dtmf_call_ = rtc::TimeMillis(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | dtmf_info_queue_.push_back(DtmfInfo(code, duration, gap)); |
| 96 | return true; |
| 97 | } |
| 98 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 99 | sigslot::signal0<>* GetOnDestroyedSignal() override { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | return &SignalDestroyed; |
| 101 | } |
| 102 | |
| 103 | // getter and setter |
| 104 | const std::vector<DtmfInfo>& dtmf_info_queue() const { |
| 105 | return dtmf_info_queue_; |
| 106 | } |
| 107 | |
| 108 | // helper functions |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 109 | void SetCanInsertDtmf(bool can_insert) { can_insert_ = can_insert; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | |
| 111 | private: |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 112 | bool can_insert_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | std::vector<DtmfInfo> dtmf_info_queue_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 114 | int64_t last_insert_dtmf_call_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | sigslot::signal0<> SignalDestroyed; |
| 116 | }; |
| 117 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 118 | class DtmfSenderTest : public ::testing::Test { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | protected: |
| 120 | DtmfSenderTest() |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 121 | : observer_(new rtc::RefCountedObject<FakeDtmfObserver>()), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | provider_(new FakeDtmfProvider()) { |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 123 | provider_->SetCanInsertDtmf(true); |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 124 | dtmf_ = DtmfSender::Create(rtc::Thread::Current(), provider_.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | dtmf_->RegisterObserver(observer_.get()); |
| 126 | } |
| 127 | |
| 128 | ~DtmfSenderTest() { |
| 129 | if (dtmf_.get()) { |
| 130 | dtmf_->UnregisterObserver(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Constructs a list of DtmfInfo from |tones|, |duration| and |
| 135 | // |inter_tone_gap|. |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 136 | void GetDtmfInfoFromString( |
| 137 | const std::string& tones, |
| 138 | int duration, |
| 139 | int inter_tone_gap, |
| 140 | std::vector<FakeDtmfProvider::DtmfInfo>* dtmfs, |
| 141 | int comma_delay = webrtc::DtmfSender::kDtmfDefaultCommaDelayMs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 142 | // Init extra_delay as -inter_tone_gap - duration to ensure the first |
| 143 | // DtmfInfo's gap field will be 0. |
| 144 | int extra_delay = -1 * (inter_tone_gap + duration); |
| 145 | |
| 146 | std::string::const_iterator it = tones.begin(); |
| 147 | for (; it != tones.end(); ++it) { |
| 148 | char tone = *it; |
| 149 | int code = 0; |
| 150 | webrtc::GetDtmfCode(tone, &code); |
| 151 | if (tone == ',') { |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 152 | extra_delay = comma_delay; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 154 | dtmfs->push_back(FakeDtmfProvider::DtmfInfo( |
| 155 | code, duration, duration + inter_tone_gap + extra_delay)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 156 | extra_delay = 0; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 161 | void VerifyExpectedState(const std::string& tones, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 162 | int duration, |
| 163 | int inter_tone_gap) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | EXPECT_EQ(tones, dtmf_->tones()); |
| 165 | EXPECT_EQ(duration, dtmf_->duration()); |
| 166 | EXPECT_EQ(inter_tone_gap, dtmf_->inter_tone_gap()); |
| 167 | } |
| 168 | |
| 169 | // Verify the provider got all the expected calls. |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 170 | void VerifyOnProvider( |
| 171 | const std::string& tones, |
| 172 | int duration, |
| 173 | int inter_tone_gap, |
| 174 | int comma_delay = webrtc::DtmfSender::kDtmfDefaultCommaDelayMs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 175 | std::vector<FakeDtmfProvider::DtmfInfo> dtmf_queue_ref; |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 176 | GetDtmfInfoFromString(tones, duration, inter_tone_gap, &dtmf_queue_ref, |
| 177 | comma_delay); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | VerifyOnProvider(dtmf_queue_ref); |
| 179 | } |
| 180 | |
| 181 | void VerifyOnProvider( |
| 182 | const std::vector<FakeDtmfProvider::DtmfInfo>& dtmf_queue_ref) { |
| 183 | const std::vector<FakeDtmfProvider::DtmfInfo>& dtmf_queue = |
| 184 | provider_->dtmf_info_queue(); |
| 185 | ASSERT_EQ(dtmf_queue_ref.size(), dtmf_queue.size()); |
| 186 | std::vector<FakeDtmfProvider::DtmfInfo>::const_iterator it_ref = |
| 187 | dtmf_queue_ref.begin(); |
| 188 | std::vector<FakeDtmfProvider::DtmfInfo>::const_iterator it = |
| 189 | dtmf_queue.begin(); |
| 190 | while (it_ref != dtmf_queue_ref.end() && it != dtmf_queue.end()) { |
| 191 | EXPECT_EQ(it_ref->code, it->code); |
| 192 | EXPECT_EQ(it_ref->duration, it->duration); |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 193 | // Allow ~10ms error (can be small since we're using a fake clock). |
| 194 | EXPECT_GE(it_ref->gap, it->gap - 10); |
| 195 | EXPECT_LE(it_ref->gap, it->gap + 10); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 196 | ++it_ref; |
| 197 | ++it; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Verify the observer got all the expected callbacks. |
| 202 | void VerifyOnObserver(const std::string& tones_ref) { |
| 203 | const std::vector<std::string>& tones = observer_->tones(); |
| 204 | // The observer will get an empty string at the end. |
| 205 | EXPECT_EQ(tones_ref.size() + 1, tones.size()); |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 206 | EXPECT_EQ(observer_->tones(), |
| 207 | observer_->tones_from_single_argument_callback()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 208 | EXPECT_TRUE(tones.back().empty()); |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 209 | EXPECT_TRUE(observer_->tones_remaining().empty()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 210 | std::string::const_iterator it_ref = tones_ref.begin(); |
| 211 | std::vector<std::string>::const_iterator it = tones.begin(); |
| 212 | while (it_ref != tones_ref.end() && it != tones.end()) { |
| 213 | EXPECT_EQ(*it_ref, it->at(0)); |
| 214 | ++it_ref; |
| 215 | ++it; |
| 216 | } |
| 217 | } |
| 218 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 219 | std::unique_ptr<FakeDtmfObserver> observer_; |
| 220 | std::unique_ptr<FakeDtmfProvider> provider_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 221 | rtc::scoped_refptr<DtmfSender> dtmf_; |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 222 | rtc::ScopedFakeClock fake_clock_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | TEST_F(DtmfSenderTest, CanInsertDtmf) { |
| 226 | EXPECT_TRUE(dtmf_->CanInsertDtmf()); |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 227 | provider_->SetCanInsertDtmf(false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 228 | EXPECT_FALSE(dtmf_->CanInsertDtmf()); |
| 229 | } |
| 230 | |
| 231 | TEST_F(DtmfSenderTest, InsertDtmf) { |
| 232 | std::string tones = "@1%a&*$"; |
| 233 | int duration = 100; |
| 234 | int inter_tone_gap = 50; |
| 235 | EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 236 | EXPECT_TRUE_SIMULATED_WAIT(observer_->completed(), kMaxWaitMs, fake_clock_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 237 | |
| 238 | // The unrecognized characters should be ignored. |
| 239 | std::string known_tones = "1a*"; |
| 240 | VerifyOnProvider(known_tones, duration, inter_tone_gap); |
| 241 | VerifyOnObserver(known_tones); |
| 242 | } |
| 243 | |
| 244 | TEST_F(DtmfSenderTest, InsertDtmfTwice) { |
| 245 | std::string tones1 = "12"; |
| 246 | std::string tones2 = "ab"; |
| 247 | int duration = 100; |
| 248 | int inter_tone_gap = 50; |
| 249 | EXPECT_TRUE(dtmf_->InsertDtmf(tones1, duration, inter_tone_gap)); |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 250 | VerifyExpectedState(tones1, duration, inter_tone_gap); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 251 | // Wait until the first tone got sent. |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 252 | EXPECT_TRUE_SIMULATED_WAIT(observer_->tones().size() == 1, kMaxWaitMs, |
| 253 | fake_clock_); |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 254 | VerifyExpectedState("2", duration, inter_tone_gap); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 255 | // Insert with another tone buffer. |
| 256 | EXPECT_TRUE(dtmf_->InsertDtmf(tones2, duration, inter_tone_gap)); |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 257 | VerifyExpectedState(tones2, duration, inter_tone_gap); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 258 | // Wait until it's completed. |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 259 | EXPECT_TRUE_SIMULATED_WAIT(observer_->completed(), kMaxWaitMs, fake_clock_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 260 | |
| 261 | std::vector<FakeDtmfProvider::DtmfInfo> dtmf_queue_ref; |
| 262 | GetDtmfInfoFromString("1", duration, inter_tone_gap, &dtmf_queue_ref); |
| 263 | GetDtmfInfoFromString("ab", duration, inter_tone_gap, &dtmf_queue_ref); |
| 264 | VerifyOnProvider(dtmf_queue_ref); |
| 265 | VerifyOnObserver("1ab"); |
| 266 | } |
| 267 | |
| 268 | TEST_F(DtmfSenderTest, InsertDtmfWhileProviderIsDeleted) { |
| 269 | std::string tones = "@1%a&*$"; |
| 270 | int duration = 100; |
| 271 | int inter_tone_gap = 50; |
| 272 | EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
| 273 | // Wait until the first tone got sent. |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 274 | EXPECT_TRUE_SIMULATED_WAIT(observer_->tones().size() == 1, kMaxWaitMs, |
| 275 | fake_clock_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 276 | // Delete provider. |
| 277 | provider_.reset(); |
| 278 | // The queue should be discontinued so no more tone callbacks. |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 279 | SIMULATED_WAIT(false, 200, fake_clock_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 280 | EXPECT_EQ(1U, observer_->tones().size()); |
| 281 | } |
| 282 | |
| 283 | TEST_F(DtmfSenderTest, InsertDtmfWhileSenderIsDeleted) { |
| 284 | std::string tones = "@1%a&*$"; |
| 285 | int duration = 100; |
| 286 | int inter_tone_gap = 50; |
| 287 | EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
| 288 | // Wait until the first tone got sent. |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 289 | EXPECT_TRUE_SIMULATED_WAIT(observer_->tones().size() == 1, kMaxWaitMs, |
| 290 | fake_clock_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 291 | // Delete the sender. |
| 292 | dtmf_ = NULL; |
| 293 | // The queue should be discontinued so no more tone callbacks. |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 294 | SIMULATED_WAIT(false, 200, fake_clock_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 295 | EXPECT_EQ(1U, observer_->tones().size()); |
| 296 | } |
| 297 | |
| 298 | TEST_F(DtmfSenderTest, InsertEmptyTonesToCancelPreviousTask) { |
| 299 | std::string tones1 = "12"; |
| 300 | std::string tones2 = ""; |
| 301 | int duration = 100; |
| 302 | int inter_tone_gap = 50; |
| 303 | EXPECT_TRUE(dtmf_->InsertDtmf(tones1, duration, inter_tone_gap)); |
| 304 | // Wait until the first tone got sent. |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 305 | EXPECT_TRUE_SIMULATED_WAIT(observer_->tones().size() == 1, kMaxWaitMs, |
| 306 | fake_clock_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 307 | // Insert with another tone buffer. |
| 308 | EXPECT_TRUE(dtmf_->InsertDtmf(tones2, duration, inter_tone_gap)); |
| 309 | // Wait until it's completed. |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 310 | EXPECT_TRUE_SIMULATED_WAIT(observer_->completed(), kMaxWaitMs, fake_clock_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 311 | |
| 312 | std::vector<FakeDtmfProvider::DtmfInfo> dtmf_queue_ref; |
| 313 | GetDtmfInfoFromString("1", duration, inter_tone_gap, &dtmf_queue_ref); |
| 314 | VerifyOnProvider(dtmf_queue_ref); |
| 315 | VerifyOnObserver("1"); |
| 316 | } |
| 317 | |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 318 | TEST_F(DtmfSenderTest, InsertDtmfWithDefaultCommaDelay) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 319 | std::string tones = "3,4"; |
| 320 | int duration = 100; |
| 321 | int inter_tone_gap = 50; |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 322 | int default_comma_delay = webrtc::DtmfSender::kDtmfDefaultCommaDelayMs; |
| 323 | EXPECT_EQ(dtmf_->comma_delay(), default_comma_delay); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 324 | EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
deadbeef | e7fc7d5 | 2016-10-28 13:53:08 -0700 | [diff] [blame] | 325 | EXPECT_TRUE_SIMULATED_WAIT(observer_->completed(), kMaxWaitMs, fake_clock_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 326 | |
| 327 | VerifyOnProvider(tones, duration, inter_tone_gap); |
| 328 | VerifyOnObserver(tones); |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 329 | EXPECT_EQ(dtmf_->comma_delay(), default_comma_delay); |
| 330 | } |
| 331 | |
| 332 | TEST_F(DtmfSenderTest, InsertDtmfWithNonDefaultCommaDelay) { |
| 333 | std::string tones = "3,4"; |
| 334 | int duration = 100; |
| 335 | int inter_tone_gap = 50; |
| 336 | int default_comma_delay = webrtc::DtmfSender::kDtmfDefaultCommaDelayMs; |
| 337 | int comma_delay = 500; |
| 338 | EXPECT_EQ(dtmf_->comma_delay(), default_comma_delay); |
| 339 | EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap, comma_delay)); |
| 340 | EXPECT_TRUE_SIMULATED_WAIT(observer_->completed(), kMaxWaitMs, fake_clock_); |
| 341 | |
| 342 | VerifyOnProvider(tones, duration, inter_tone_gap, comma_delay); |
| 343 | VerifyOnObserver(tones); |
| 344 | EXPECT_EQ(dtmf_->comma_delay(), comma_delay); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | TEST_F(DtmfSenderTest, TryInsertDtmfWhenItDoesNotWork) { |
| 348 | std::string tones = "3,4"; |
| 349 | int duration = 100; |
| 350 | int inter_tone_gap = 50; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 351 | provider_->SetCanInsertDtmf(false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 352 | EXPECT_FALSE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
| 353 | } |
| 354 | |
| 355 | TEST_F(DtmfSenderTest, InsertDtmfWithInvalidDurationOrGap) { |
| 356 | std::string tones = "3,4"; |
dminor | 588101c | 2017-03-28 11:18:32 -0700 | [diff] [blame] | 357 | int duration = 40; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 358 | int inter_tone_gap = 50; |
| 359 | |
| 360 | EXPECT_FALSE(dtmf_->InsertDtmf(tones, 6001, inter_tone_gap)); |
dminor | 588101c | 2017-03-28 11:18:32 -0700 | [diff] [blame] | 361 | EXPECT_FALSE(dtmf_->InsertDtmf(tones, 39, inter_tone_gap)); |
Harald Alvestrand | 52e5852 | 2018-02-20 08:15:36 +0100 | [diff] [blame] | 362 | EXPECT_FALSE(dtmf_->InsertDtmf(tones, duration, 29)); |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 363 | EXPECT_FALSE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap, 29)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 364 | |
| 365 | EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
| 366 | } |
Harald Alvestrand | 1e0c804 | 2018-03-06 10:51:27 +0100 | [diff] [blame] | 367 | |
| 368 | TEST_F(DtmfSenderTest, InsertDtmfSendsAfterWait) { |
| 369 | std::string tones = "ABC"; |
| 370 | int duration = 100; |
| 371 | int inter_tone_gap = 50; |
| 372 | EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 373 | VerifyExpectedState("ABC", duration, inter_tone_gap); |
Harald Alvestrand | 1e0c804 | 2018-03-06 10:51:27 +0100 | [diff] [blame] | 374 | // Wait until the first tone got sent. |
| 375 | EXPECT_TRUE_SIMULATED_WAIT(observer_->tones().size() == 1, kMaxWaitMs, |
| 376 | fake_clock_); |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 377 | VerifyExpectedState("BC", duration, inter_tone_gap); |
Harald Alvestrand | 1e0c804 | 2018-03-06 10:51:27 +0100 | [diff] [blame] | 378 | } |