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

Side by Side Diff: media/audio/sounds/wav_audio_handler_unittest.cc

Issue 1453233002: Improve input handling for WaveAudioHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Remove VLOG()s Created 5 years, 1 month 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
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 } // namespace
15 23
16 TEST(WavAudioHandlerTest, SampleDataTest) { 24 TEST(WavAudioHandlerTest, SampleDataTest) {
17 WavAudioHandler handler(base::StringPiece(kTestAudioData, 25 auto handler = WavAudioHandler::Create(
18 arraysize(kTestAudioData))); 26 base::StringPiece(kTestAudioData, arraysize(kTestAudioData)));
19 ASSERT_EQ(2u, handler.num_channels()); 27 ASSERT_TRUE(handler);
20 ASSERT_EQ(16u, handler.bits_per_sample()); 28 ASSERT_EQ(2u, handler->num_channels());
21 ASSERT_EQ(48000u, handler.sample_rate()); 29 ASSERT_EQ(16u, handler->bits_per_sample());
22 ASSERT_EQ(1u, handler.total_frames()); 30 ASSERT_EQ(48000u, handler->sample_rate());
23 ASSERT_EQ(20u, handler.GetDuration().InMicroseconds()); 31 ASSERT_EQ(1u, handler->total_frames());
32 ASSERT_EQ(20u, handler->GetDuration().InMicroseconds());
24 33
25 ASSERT_EQ(4U, handler.data().size()); 34 ASSERT_EQ(4U, handler->data().size());
26 const char kData[] = "\x01\x00\x01\x00"; 35 const char kData[] = "\x01\x00\x01\x00";
27 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler.data()); 36 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler->data());
28 37
29 scoped_ptr<AudioBus> bus = AudioBus::Create( 38 scoped_ptr<AudioBus> bus =
30 handler.num_channels(), handler.data().size() / handler.num_channels()); 39 AudioBus::Create(handler->num_channels(),
40 handler->data().size() / handler->num_channels());
31 41
32 size_t bytes_written; 42 size_t bytes_written = 0u;
33 ASSERT_TRUE(handler.CopyTo(bus.get(), 0, &bytes_written)); 43 ASSERT_TRUE(handler->CopyTo(bus.get(), 0, &bytes_written));
34 ASSERT_EQ(static_cast<size_t>(handler.data().size()), bytes_written); 44 ASSERT_EQ(static_cast<size_t>(handler->data().size()), bytes_written);
45 }
46
47 TEST(WavAudioHandlerTest, TestZeroChannelsIsNotValid) {
48 // Read in the sample data and modify the channel field to hold |00|00|.
49 auto data = std::string(kTestAudioData, arraysize(kTestAudioData));
50 data[kChannelIndex] = '\x00';
51 data[kChannelIndex + 1] = '\x00';
52 auto handler =
53 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
54 EXPECT_FALSE(handler);
55 }
56
57 TEST(WavAudioHandlerTest, TestZeroBitsPerSampleIsNotValid) {
58 // Read in the sample data and modify the bits_per_sample field to hold
59 // |00|00|.
60 auto data = std::string(kTestAudioData, arraysize(kTestAudioData));
61 data[kBitsPerSampleIndex] = '\x00';
62 data[kBitsPerSampleIndex + 1] = '\x00';
63 auto handler =
64 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
65 EXPECT_FALSE(handler);
66 }
67
68 TEST(WavAudioHandlerTest, TestZeroSamplesPerSecondIsNotValid) {
69 // Read in the sample data and modify the bits_per_sample field to hold
70 // |00|00|.
71 auto data = std::string(kTestAudioData, arraysize(kTestAudioData));
72 data[kSampleRateIndex] = '\x00';
73 data[kSampleRateIndex + 1] = '\x00';
74 data[kSampleRateIndex + 2] = '\x00';
75 data[kSampleRateIndex + 3] = '\x00';
76 auto handler =
77 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
78 EXPECT_FALSE(handler);
79 ;
35 } 80 }
36 81
37 } // namespace media 82 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698