From 8441d09a1808ee95a71849a141b85e479b2ae68e Mon Sep 17 00:00:00 2001 From: Robo Date: Thu, 6 Sep 2018 02:30:05 +0530 Subject: [PATCH] chore: refactor webContents module isCurrentlyAudible api spec (#14410) --- atom/browser/api/atom_api_web_contents.cc | 5 ++++ atom/browser/api/atom_api_web_contents.h | 2 ++ spec/api-web-contents-spec.js | 30 ++++++++++++--------- spec/fixtures/api/is-currently-audible.html | 21 --------------- 4 files changed, 25 insertions(+), 33 deletions(-) delete mode 100644 spec/fixtures/api/is-currently-audible.html diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index ebf57235333..8c8afe56014 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -754,6 +754,11 @@ content::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager( return dialog_manager_.get(); } +void WebContents::OnAudioStateChanged(content::WebContents* web_contents, + bool audible) { + Emit("-audio-state-changed", audible); +} + void WebContents::BeforeUnloadFired(const base::TimeTicks& proceed_time) { // Do nothing, we override this method just to avoid compilation error since // there are two virtual functions named BeforeUnloadFired. diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index ec5a4a756eb..d7de8f03c97 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -345,6 +345,8 @@ class WebContents : public mate::TrackableObject, const content::BluetoothChooser::EventHandler& handler) override; content::JavaScriptDialogManager* GetJavaScriptDialogManager( content::WebContents* source) override; + void OnAudioStateChanged(content::WebContents* web_contents, + bool audible) override; // content::WebContentsObserver: void BeforeUnloadFired(const base::TimeTicks& proceed_time) override; diff --git a/spec/api-web-contents-spec.js b/spec/api-web-contents-spec.js index c9f454795c3..b6fc840708f 100644 --- a/spec/api-web-contents-spec.js +++ b/spec/api-web-contents-spec.js @@ -122,19 +122,25 @@ describe('webContents module', () => { }) }) - // Disabled because flaky. See #13969 - xdescribe('isCurrentlyAudible() API', () => { + describe('isCurrentlyAudible() API', () => { it('returns whether audio is playing', async () => { - w.loadFile(path.join(fixtures, 'api', 'is-currently-audible.html')) - w.show() - await emittedOnce(w.webContents, 'did-finish-load') - - expect(w.webContents.isCurrentlyAudible()).to.be.false() - - w.webContents.send('play') - await emittedOnce(ipcMain, 'playing') - - expect(w.webContents.isCurrentlyAudible()).to.be.true() + const webContents = remote.getCurrentWebContents() + const context = new window.AudioContext() + // Start in suspended state, because of the + // new web audio api policy. + context.suspend() + const oscillator = context.createOscillator() + oscillator.connect(context.destination) + oscillator.start() + await context.resume() + const [, audible] = await emittedOnce(webContents, '-audio-state-changed') + assert(webContents.isCurrentlyAudible() === audible) + expect(webContents.isCurrentlyAudible()).to.be.true() + oscillator.stop() + await emittedOnce(webContents, '-audio-state-changed') + expect(webContents.isCurrentlyAudible()).to.be.false() + oscillator.disconnect() + context.close() }) }) diff --git a/spec/fixtures/api/is-currently-audible.html b/spec/fixtures/api/is-currently-audible.html deleted file mode 100644 index a2fbe31d3a1..00000000000 --- a/spec/fixtures/api/is-currently-audible.html +++ /dev/null @@ -1,21 +0,0 @@ - - -
- - - \ No newline at end of file