Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
| 9 #include "media/audio/sounds/test_data.h" | 9 #include "media/audio/sounds/test_data.h" |
| 10 #include "media/audio/sounds/wav_audio_handler.h" | 10 #include "media/audio/sounds/wav_audio_handler.h" |
| 11 #include "media/base/audio_bus.h" | 11 #include "media/base/audio_bus.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace media { | 14 namespace media { |
| 15 namespace { | |
| 16 const size_t kWavHeaderSize = 12; | |
| 17 const size_t kFormatHeaderSize = 8; | |
| 18 | |
| 19 const size_t kChannelIndex = kWavHeaderSize + kFormatHeaderSize + 2; | |
| 20 const size_t kBitsPerSampleIndex = kWavHeaderSize + kFormatHeaderSize + 14; | |
| 21 const size_t kSampleRateIndex = kWavHeaderSize + kFormatHeaderSize + 4; | |
| 22 | |
| 23 void CheckStateIsInvalid(const WavAudioHandler& handler) { | |
| 24 EXPECT_FALSE(handler.is_valid()); | |
| 25 EXPECT_EQ(base::StringPiece(), handler.data()); | |
| 26 EXPECT_EQ(0u, handler.num_channels()); | |
| 27 EXPECT_EQ(0u, handler.sample_rate()); | |
| 28 EXPECT_EQ(0u, handler.bits_per_sample()); | |
| 29 EXPECT_EQ(0u, handler.total_frames()); | |
| 30 | |
| 31 // CopyTo should always return false. | |
| 32 auto bus = media::AudioBus::Create(2, 1024); | |
| 33 size_t written = 0u; | |
| 34 EXPECT_FALSE(handler.CopyTo(bus.get(), 0u, &written)); | |
| 35 | |
| 36 // AtEnd should always return true. | |
| 37 EXPECT_TRUE(handler.AtEnd(0u)); | |
| 38 | |
| 39 // GetDuration should return zero duration. | |
| 40 EXPECT_EQ(base::TimeDelta(), handler.GetDuration()); | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 15 | 44 |
| 16 TEST(WavAudioHandlerTest, SampleDataTest) { | 45 TEST(WavAudioHandlerTest, SampleDataTest) { |
| 17 WavAudioHandler handler(base::StringPiece(kTestAudioData, | 46 WavAudioHandler handler(base::StringPiece(kTestAudioData, |
| 18 arraysize(kTestAudioData))); | 47 arraysize(kTestAudioData))); |
| 48 ASSERT_TRUE(handler.is_valid()); | |
| 19 ASSERT_EQ(2u, handler.num_channels()); | 49 ASSERT_EQ(2u, handler.num_channels()); |
| 20 ASSERT_EQ(16u, handler.bits_per_sample()); | 50 ASSERT_EQ(16u, handler.bits_per_sample()); |
| 21 ASSERT_EQ(48000u, handler.sample_rate()); | 51 ASSERT_EQ(48000u, handler.sample_rate()); |
| 22 ASSERT_EQ(1u, handler.total_frames()); | 52 ASSERT_EQ(1u, handler.total_frames()); |
| 23 ASSERT_EQ(20u, handler.GetDuration().InMicroseconds()); | 53 ASSERT_EQ(20u, handler.GetDuration().InMicroseconds()); |
| 24 | 54 |
| 25 ASSERT_EQ(4U, handler.data().size()); | 55 ASSERT_EQ(4U, handler.data().size()); |
| 26 const char kData[] = "\x01\x00\x01\x00"; | 56 const char kData[] = "\x01\x00\x01\x00"; |
| 27 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler.data()); | 57 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler.data()); |
| 28 | 58 |
| 29 scoped_ptr<AudioBus> bus = AudioBus::Create( | 59 scoped_ptr<AudioBus> bus = AudioBus::Create( |
| 30 handler.num_channels(), handler.data().size() / handler.num_channels()); | 60 handler.num_channels(), handler.data().size() / handler.num_channels()); |
| 31 | 61 |
| 32 size_t bytes_written; | 62 size_t bytes_written; |
| 33 ASSERT_TRUE(handler.CopyTo(bus.get(), 0, &bytes_written)); | 63 ASSERT_TRUE(handler.CopyTo(bus.get(), 0, &bytes_written)); |
| 34 ASSERT_EQ(static_cast<size_t>(handler.data().size()), bytes_written); | 64 ASSERT_EQ(static_cast<size_t>(handler.data().size()), bytes_written); |
| 35 } | 65 } |
| 36 | 66 |
| 67 TEST(WavAudioHandlerTest, TestZeroChannelsIsNotValid) { | |
| 68 // Read in the sample data and modify the channel field to hold |00|00|. | |
| 69 auto data = std::string(kTestAudioData, arraysize(kTestAudioData)); | |
|
tommi (sloooow) - chröme
2015/11/17 20:48:39
std::string data(kTestAudioData, arraysize(kTestAu
| |
| 70 data[kChannelIndex] = '\x00'; | |
| 71 data[kChannelIndex + 1] = '\x00'; | |
| 72 WavAudioHandler handler(base::StringPiece(data.c_str(), data.size())); | |
| 73 EXPECT_FALSE(handler.is_valid()); | |
| 74 CheckStateIsInvalid(handler); | |
| 75 } | |
| 76 | |
| 77 TEST(WavAudioHandlerTest, TestZeroBitsPerSampleIsNotValid) { | |
| 78 // Read in the sample data and modify the bits_per_sample field to hold | |
| 79 // |00|00|. | |
| 80 auto data = std::string(kTestAudioData, arraysize(kTestAudioData)); | |
| 81 data[kBitsPerSampleIndex] = '\x00'; | |
| 82 data[kBitsPerSampleIndex + 1] = '\x00'; | |
| 83 WavAudioHandler handler(base::StringPiece(data.c_str(), data.size())); | |
| 84 EXPECT_FALSE(handler.is_valid()); | |
| 85 CheckStateIsInvalid(handler); | |
| 86 } | |
| 87 | |
| 88 TEST(WavAudioHandlerTest, TestZeroSamplesPerSecondIsNotValid) { | |
| 89 // Read in the sample data and modify the bits_per_sample field to hold | |
| 90 // |00|00|. | |
| 91 auto data = std::string(kTestAudioData, arraysize(kTestAudioData)); | |
| 92 data[kSampleRateIndex] = '\x00'; | |
| 93 data[kSampleRateIndex + 1] = '\x00'; | |
| 94 data[kSampleRateIndex + 2] = '\x00'; | |
| 95 data[kSampleRateIndex + 3] = '\x00'; | |
| 96 WavAudioHandler handler(base::StringPiece(data.c_str(), data.size())); | |
| 97 EXPECT_FALSE(handler.is_valid()); | |
| 98 CheckStateIsInvalid(handler); | |
| 99 } | |
| 100 | |
| 37 } // namespace media | 101 } // namespace media |
| OLD | NEW |