Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add isCurrentlyAudible() to WebContents #13614

Merged
merged 3 commits into from Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -1434,6 +1434,10 @@ bool WebContents::IsAudioMuted() {
return web_contents()->IsAudioMuted();
}

bool WebContents::IsCurrentlyAudible() {
return web_contents()->IsCurrentlyAudible();
}

void WebContents::Print(mate::Arguments* args) {
PrintSettings settings = {false, false, base::string16()};
if (args->Length() >= 1 && !args->GetNext(&settings)) {
Expand Down Expand Up @@ -2018,6 +2022,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setIgnoreMenuShortcuts", &WebContents::SetIgnoreMenuShortcuts)
.SetMethod("setAudioMuted", &WebContents::SetAudioMuted)
.SetMethod("isAudioMuted", &WebContents::IsAudioMuted)
.SetMethod("isCurrentlyAudible", &WebContents::IsCurrentlyAudible)
.SetMethod("undo", &WebContents::Undo)
.SetMethod("redo", &WebContents::Redo)
.SetMethod("cut", &WebContents::Cut)
Expand Down
1 change: 1 addition & 0 deletions atom/browser/api/atom_api_web_contents.h
Expand Up @@ -143,6 +143,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
void SetIgnoreMenuShortcuts(bool ignore);
void SetAudioMuted(bool muted);
bool IsAudioMuted();
bool IsCurrentlyAudible();
void Print(mate::Arguments* args);
std::vector<printing::PrinterBasicInfo> GetPrinterList();
void SetEmbedder(const WebContents* embedder);
Expand Down
4 changes: 4 additions & 0 deletions docs/api/web-contents.md
Expand Up @@ -832,6 +832,10 @@ Mute the audio on the current web page.

Returns `Boolean` - Whether this page has been muted.

#### `contents.isCurrentlyAudible()`

Returns `Boolean` - Whether audio is currently playing.

#### `contents.setZoomFactor(factor)`

* `factor` Number - Zoom factor.
Expand Down
4 changes: 4 additions & 0 deletions docs/api/webview-tag.md
Expand Up @@ -450,6 +450,10 @@ Set guest page muted.

Returns `Boolean` - Whether guest page has been muted.

#### `<webview>.isCurrentlyAudible()`

Returns `Boolean` - Whether audio is currently playing.

### `<webview>.undo()`

Executes editing command `undo` in page.
Expand Down
1 change: 1 addition & 0 deletions lib/renderer/web-view/web-view.js
Expand Up @@ -344,6 +344,7 @@ const registerWebViewElement = function () {
'inspectElement',
'setAudioMuted',
'isAudioMuted',
'isCurrentlyAudible',
'undo',
'redo',
'cut',
Expand Down
16 changes: 16 additions & 0 deletions spec/api-web-contents-spec.js
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert')
const http = require('http')
const path = require('path')
const {closeWindow} = require('./window-helpers')
const {emittedOnce} = require('./events-helpers')

const {ipcRenderer, remote} = require('electron')
const {BrowserWindow, webContents, ipcMain, session} = remote
Expand Down Expand Up @@ -116,6 +117,21 @@ describe('webContents module', () => {
})
})

describe('isCurrentlyAudible() API', () => {
it('returns whether audio is playing', async () => {
w.loadURL(`file://${path.join(__dirname, '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()
})
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use our event helpers and Chai's expect here:

const chai = require('chai')
const dirtyChai = require('dirty-chai')

const {expect} = chai

chai.use(dirtyChai)

<...>

it('returns whether audio is playing', async () => {
  w.loadURL(`file://${path.join(__dirname, '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()
})


describe('getWebPreferences() API', () => {
it('should not crash when called for devTools webContents', (done) => {
w.webContents.openDevTools()
Expand Down
21 changes: 21 additions & 0 deletions spec/fixtures/api/is-currently-audible.html
@@ -0,0 +1,21 @@
<html>
<body>
<div id="video"></div>
<script type="text/javascript" charset="utf-8">
const {ipcRenderer} = window.top != null ? window.top.require('electron') : require('electron')
ipcRenderer.on('play', (event) => {
const context = new window.AudioContext();
const oscillator = context.createOscillator();

// A beep
oscillator.type = 'sine';
oscillator.frequency.value = 440
oscillator.connect(context.destination)
oscillator.start()

// It'll take a few ms before the beep shows up
setTimeout(() => event.sender.send('playing'), 100)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it will take more than 100ms for the oscillator to become audible?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It never should. In theory, it should start immediately, but the Web Audio API is still a working draft, so I've added the 100ms to ensure that we'll never have a flaky test, even if you're running the test on the slowest of all machines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, if you say so =)

})
</script>
</body>
</html>