OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 "ppapi/tests/test_audio.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/basictypes.h" // For |arraysize()|. | |
dmichael (off chromium)
2011/11/30 22:53:49
Is it okay to use 'base' in the tests? I've been a
viettrungluu
2011/11/30 23:05:09
Right. I think the answer is we should avoid base.
| |
10 #include "ppapi/c/ppb_audio_config.h" | |
11 #include "ppapi/c/ppb_audio.h" | |
12 #include "ppapi/cpp/module.h" | |
13 #include "ppapi/tests/testing_instance.h" | |
14 | |
15 REGISTER_TEST_CASE(Audio); | |
16 | |
17 bool TestAudio::Init() { | |
18 audio_interface_ = static_cast<PPB_Audio const*>( | |
19 pp::Module::Get()->GetBrowserInterface(PPB_AUDIO_INTERFACE)); | |
20 audio_config_interface_ = static_cast<PPB_AudioConfig const*>( | |
21 pp::Module::Get()->GetBrowserInterface(PPB_AUDIO_CONFIG_INTERFACE)); | |
22 core_interface_ = static_cast<const PPB_Core*>( | |
23 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | |
24 return audio_interface_ && audio_config_interface_ && core_interface_; | |
25 } | |
26 | |
27 void TestAudio::RunTests(const std::string& filter) { | |
28 RUN_TEST(Creation, filter); | |
29 RUN_TEST(DestroyNoStop, filter); | |
30 RUN_TEST(Failures, filter); | |
31 } | |
32 | |
33 // A trivial audio callback to provide when we're no interested in whether it | |
34 // gets called or not. It just clears the buffer so we don't play noise. | |
35 static void TrivialAudioCallback(void* sample_buffer, | |
36 uint32_t buffer_size_in_bytes, | |
37 void* user_data) { | |
38 memset(sample_buffer, 0, buffer_size_in_bytes); | |
39 } | |
40 | |
41 // Test creating audio resources for all guaranteed sample rates and various | |
42 // frame counts. | |
43 std::string TestAudio::TestCreation() { | |
44 static const PP_AudioSampleRate kSampleRates[] = { | |
45 PP_AUDIOSAMPLERATE_44100, | |
46 PP_AUDIOSAMPLERATE_48000 | |
47 }; | |
48 static const uint32_t kRequestFrameCounts[] = { | |
49 PP_AUDIOMINSAMPLEFRAMECOUNT, | |
50 PP_AUDIOMAXSAMPLEFRAMECOUNT, | |
51 // Include some "okay-looking" frame counts; check their validity below. | |
52 1024, | |
53 2048, | |
54 4096 | |
55 }; | |
56 | |
57 for (size_t i = 0; i < arraysize(kSampleRates); i++) { | |
58 PP_AudioSampleRate sample_rate = kSampleRates[i]; | |
59 | |
60 for (size_t j = 0; j < arraysize(kRequestFrameCounts); j++) { | |
61 // Make a config, create the audio resource, and release the config. | |
62 uint32_t request_frame_count = kRequestFrameCounts[j]; | |
63 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount( | |
64 sample_rate, request_frame_count); | |
65 PP_Resource ac = audio_config_interface_->CreateStereo16Bit( | |
66 instance_->pp_instance(), sample_rate, frame_count); | |
67 ASSERT_TRUE(ac); | |
68 PP_Resource audio = audio_interface_->Create( | |
69 instance_->pp_instance(), ac, TrivialAudioCallback, NULL); | |
70 core_interface_->ReleaseResource(ac); | |
71 ac = 0; | |
72 | |
73 ASSERT_TRUE(audio); | |
74 ASSERT_TRUE(audio_interface_->IsAudio(audio)); | |
75 | |
76 // Check that the config returned for |audio| matches what we gave it. | |
77 ac = audio_interface_->GetCurrentConfig(audio); | |
78 ASSERT_TRUE(ac); | |
79 ASSERT_TRUE(audio_config_interface_->IsAudioConfig(ac)); | |
80 ASSERT_EQ(sample_rate, audio_config_interface_->GetSampleRate(ac)); | |
81 ASSERT_EQ(frame_count, audio_config_interface_->GetSampleFrameCount(ac)); | |
82 core_interface_->ReleaseResource(ac); | |
83 ac = 0; | |
84 | |
85 // Start and stop audio playback. The documentation indicates that | |
86 // |StartPlayback()| and |StopPlayback()| may fail, but gives no | |
87 // indication as to why ... so check that they succeed. | |
88 ASSERT_TRUE(audio_interface_->StartPlayback(audio)); | |
89 ASSERT_TRUE(audio_interface_->StopPlayback(audio)); | |
90 | |
91 core_interface_->ReleaseResource(audio); | |
92 } | |
93 } | |
94 | |
95 PASS(); | |
96 } | |
97 | |
98 // Test that releasing the resource without calling |StopPlayback()| "works". | |
99 // TODO(viettrungluu): Figure out how to check that |StopPlayback()| properly | |
100 // waits for in-flight callbacks. | |
101 std::string TestAudio::TestDestroyNoStop() { | |
102 const PP_AudioSampleRate kSampleRate = PP_AUDIOSAMPLERATE_44100; | |
103 const uint32_t kRequestFrameCount = 2048; | |
104 | |
105 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount( | |
106 kSampleRate, kRequestFrameCount); | |
107 PP_Resource ac = audio_config_interface_->CreateStereo16Bit( | |
108 instance_->pp_instance(), kSampleRate, frame_count); | |
109 ASSERT_TRUE(ac); | |
110 PP_Resource audio = audio_interface_->Create( | |
111 instance_->pp_instance(), ac, TrivialAudioCallback, NULL); | |
112 core_interface_->ReleaseResource(ac); | |
113 ac = 0; | |
114 | |
115 ASSERT_TRUE(audio); | |
116 ASSERT_TRUE(audio_interface_->IsAudio(audio)); | |
117 | |
118 // Start playback and release the resource. | |
119 ASSERT_TRUE(audio_interface_->StartPlayback(audio)); | |
120 core_interface_->ReleaseResource(audio); | |
121 | |
122 PASS(); | |
123 } | |
124 | |
125 std::string TestAudio::TestFailures() { | |
126 const PP_AudioSampleRate kSampleRate = PP_AUDIOSAMPLERATE_44100; | |
127 const uint32_t kRequestFrameCount = 2048; | |
128 | |
129 // Test invalid parameters to |Create()|. | |
130 | |
131 // We want a valid config for some of our tests of |Create()|. | |
132 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount( | |
133 kSampleRate, kRequestFrameCount); | |
134 PP_Resource ac = audio_config_interface_->CreateStereo16Bit( | |
135 instance_->pp_instance(), kSampleRate, frame_count); | |
136 ASSERT_TRUE(ac); | |
137 | |
138 // Invalid instance -> failure. | |
139 PP_Resource audio = audio_interface_->Create( | |
140 0, ac, TrivialAudioCallback, NULL); | |
141 ASSERT_EQ(0, audio); | |
142 | |
143 // Invalid config -> failure. | |
144 audio = audio_interface_->Create( | |
145 instance_->pp_instance(), 0, TrivialAudioCallback, NULL); | |
146 ASSERT_EQ(0, audio); | |
147 | |
148 // Null callback -> failure. | |
149 audio = audio_interface_->Create( | |
150 instance_->pp_instance(), ac, NULL, NULL); | |
151 ASSERT_EQ(0, audio); | |
152 | |
153 core_interface_->ReleaseResource(ac); | |
154 ac = 0; | |
155 | |
156 // Test the other functions with an invalid audio resource. | |
157 ASSERT_FALSE(audio_interface_->IsAudio(0)); | |
158 ASSERT_EQ(0, audio_interface_->GetCurrentConfig(0)); | |
159 ASSERT_FALSE(audio_interface_->StartPlayback(0)); | |
160 ASSERT_FALSE(audio_interface_->StopPlayback(0)); | |
161 | |
162 PASS(); | |
163 } | |
164 | |
165 // TODO(viettrungluu): Test that callbacks get called, playback happens, etc. | |
OLD | NEW |