Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "components/copresence/handlers/audio/audio_directive_list.h" | |
| 6 | |
| 7 #include <cstdlib> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "media/base/audio_bus.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace copresence { | |
| 15 | |
| 16 class AudioDirectiveListTest : public testing::Test { | |
| 17 public: | |
| 18 AudioDirectiveListTest() | |
| 19 : directive_list_(new AudioDirectiveList( | |
| 20 base::Bind(&AudioDirectiveListTest::EncodeToken, | |
| 21 base::Unretained(this)), | |
| 22 base::Bind(&AudioDirectiveListTest::DirectiveAdded, | |
|
Daniel Erat
2014/07/28 21:18:17
the test doesn't verify that this was called. why
rkc
2014/07/29 00:33:35
Done.
| |
| 23 base::Unretained(this)))) {} | |
| 24 | |
| 25 virtual ~AudioDirectiveListTest() {} | |
| 26 | |
| 27 void DirectiveAdded() {} | |
| 28 | |
| 29 protected: | |
| 30 void EncodeToken(const std::string& token, | |
|
Daniel Erat
2014/07/28 21:18:17
this looks like it's a copy of the code from audio
rkc
2014/07/29 00:33:35
Done.
| |
| 31 const AudioDirectiveList::SamplesCallback& callback) { | |
| 32 const int kSamplesSize = 0x1337; | |
| 33 scoped_refptr<media::AudioBusRefCounted> samples = | |
| 34 media::AudioBusRefCounted::Create(1, kSamplesSize); | |
| 35 PopulateSamples(0x1337, kSamplesSize, samples->channel(0)); | |
| 36 callback.Run(token, samples); | |
| 37 } | |
| 38 | |
| 39 // Populate random samples given a random seed into the samples array. | |
| 40 void PopulateSamples(int random_seed, size_t size, float* samples) { | |
| 41 srand(random_seed); | |
| 42 for (size_t i = 0; i < size; ++i) | |
| 43 samples[i] = (2.0 * rand() / RAND_MAX) - 1; | |
| 44 } | |
| 45 | |
| 46 // This order is important. We want the message loop to get created before | |
| 47 // our directive list, since the directive list constructor will post tasks. | |
| 48 base::MessageLoop message_loop_; | |
| 49 scoped_ptr<AudioDirectiveList> directive_list_; | |
| 50 }; | |
| 51 | |
| 52 TEST_F(AudioDirectiveListTest, Basic) { | |
| 53 const base::TimeDelta zero_ttl = base::TimeDelta::FromMilliseconds(0); | |
| 54 const base::TimeDelta large_ttl = base::TimeDelta::FromSeconds(0x7331); | |
| 55 | |
| 56 directive_list_->AddTransmitDirective("token1", "op_id1", zero_ttl); | |
| 57 directive_list_->AddTransmitDirective("token2", "op_id2", large_ttl); | |
| 58 directive_list_->AddTransmitDirective("token3", "op_id1", zero_ttl); | |
| 59 | |
| 60 EXPECT_EQ("token2", directive_list_->GetNextTransmit()->token); | |
| 61 | |
| 62 directive_list_->AddReceiveDirective("op_id1", zero_ttl); | |
| 63 directive_list_->AddReceiveDirective("op_id3", zero_ttl); | |
| 64 directive_list_->AddReceiveDirective("op_id3", large_ttl); | |
| 65 directive_list_->AddReceiveDirective("op_id7", zero_ttl); | |
| 66 | |
| 67 EXPECT_EQ("op_id3", directive_list_->GetNextReceive()->op_id); | |
| 68 } | |
| 69 | |
| 70 TEST_F(AudioDirectiveListTest, OutOfOrderAndMultiple) { | |
| 71 const base::TimeDelta zero_ttl = base::TimeDelta::FromMilliseconds(0); | |
| 72 const base::TimeDelta large_ttl = base::TimeDelta::FromSeconds(0x7331); | |
| 73 | |
| 74 EXPECT_EQ(NULL, directive_list_->GetNextTransmit().get()); | |
| 75 EXPECT_EQ(NULL, directive_list_->GetNextReceive().get()); | |
| 76 | |
| 77 directive_list_->AddTransmitDirective("token1", "op_id1", zero_ttl); | |
| 78 directive_list_->AddTransmitDirective("token2", "op_id2", large_ttl); | |
| 79 directive_list_->AddTransmitDirective("token3", "op_id1", large_ttl); | |
| 80 | |
| 81 // Should keep getting the directive till it expires or we add a newer one. | |
| 82 EXPECT_EQ("token3", directive_list_->GetNextTransmit()->token); | |
| 83 EXPECT_EQ("token3", directive_list_->GetNextTransmit()->token); | |
| 84 EXPECT_EQ("token3", directive_list_->GetNextTransmit()->token); | |
| 85 EXPECT_EQ(NULL, directive_list_->GetNextReceive().get()); | |
| 86 | |
| 87 directive_list_->AddReceiveDirective("op_id1", large_ttl); | |
| 88 directive_list_->AddReceiveDirective("op_id3", zero_ttl); | |
| 89 directive_list_->AddReceiveDirective("op_id3", large_ttl); | |
| 90 directive_list_->AddReceiveDirective("op_id7", large_ttl); | |
| 91 | |
| 92 // Should keep getting the directive till it expires or we add a newer one. | |
| 93 EXPECT_EQ("op_id7", directive_list_->GetNextReceive()->op_id); | |
| 94 EXPECT_EQ("op_id7", directive_list_->GetNextReceive()->op_id); | |
| 95 EXPECT_EQ("op_id7", directive_list_->GetNextReceive()->op_id); | |
| 96 } | |
| 97 | |
| 98 } // namespace copresence | |
| OLD | NEW |