|
| 1 | +// Copyright 2018 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 "content/public/browser/web_contents_observer.h" |
| 6 | +#include "content/public/test/browser_test_utils.h" |
| 7 | +#include "content/public/test/content_browser_test.h" |
| 8 | +#include "content/public/test/content_browser_test_utils.h" |
| 9 | +#include "content/shell/browser/shell.h" |
| 10 | + |
| 11 | +namespace content { |
| 12 | + |
| 13 | +namespace { |
| 14 | +// Test for audible playback message. |
| 15 | +class WaitForAudioContextAudible : WebContentsObserver { |
| 16 | + public: |
| 17 | + explicit WaitForAudioContextAudible(WebContents* web_contents) |
| 18 | + : WebContentsObserver(web_contents) { |
| 19 | + run_loop_.Run(); |
| 20 | + } |
| 21 | + |
| 22 | + void AudioContextPlaybackStarted(const AudioContextId&) final { |
| 23 | + // Stop the run loop when we get the message |
| 24 | + run_loop_.Quit(); |
| 25 | + } |
| 26 | + |
| 27 | + private: |
| 28 | + base::RunLoop run_loop_; |
| 29 | + |
| 30 | + DISALLOW_COPY_AND_ASSIGN(WaitForAudioContextAudible); |
| 31 | +}; |
| 32 | + |
| 33 | +// Test for silent playback started (audible playback stopped). |
| 34 | +class WaitForAudioContextSilent : WebContentsObserver { |
| 35 | + public: |
| 36 | + explicit WaitForAudioContextSilent(WebContents* web_contents) |
| 37 | + : WebContentsObserver(web_contents) { |
| 38 | + run_loop_.Run(); |
| 39 | + } |
| 40 | + |
| 41 | + void AudioContextPlaybackStopped(const AudioContextId&) final { |
| 42 | + // Stop the run loop when we get the message |
| 43 | + run_loop_.Quit(); |
| 44 | + } |
| 45 | + |
| 46 | + private: |
| 47 | + base::RunLoop run_loop_; |
| 48 | + |
| 49 | + DISALLOW_COPY_AND_ASSIGN(WaitForAudioContextSilent); |
| 50 | +}; |
| 51 | + |
| 52 | +} // namespace |
| 53 | + |
| 54 | +class AudioContextManagerTest : public ContentBrowserTest {}; |
| 55 | + |
| 56 | +IN_PROC_BROWSER_TEST_F(AudioContextManagerTest, AudioContextPlaybackRecorded) { |
| 57 | + NavigateToURL(shell(), |
| 58 | + content::GetTestUrl("media/webaudio/", "playback-test.html")); |
| 59 | + |
| 60 | + // Set gain to 1 to start audible audio and verify we got the |
| 61 | + // playback started message. |
| 62 | + { |
| 63 | + ASSERT_TRUE(ExecuteScript(shell()->web_contents(), "gain.gain.value = 1;")); |
| 64 | + WaitForAudioContextAudible wait(shell()->web_contents()); |
| 65 | + } |
| 66 | + |
| 67 | + // Set gain to 0 to stop audible audio and verify we got the |
| 68 | + // playback stopped message. |
| 69 | + { |
| 70 | + ASSERT_TRUE(ExecuteScript(shell()->web_contents(), "gain.gain.value = 0;")); |
| 71 | + WaitForAudioContextSilent wait(shell()->web_contents()); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace content |
0 commit comments