Skip to content

Commit

Permalink
Idle Detection API: Prototype
Browse files Browse the repository at this point in the history
https://github.com/inexorabletash/idle-detection

Just a webification of the chrome.idle.* APIs.

Does not yet include:

- permission checks
- android integration
- final API

Intent to implement:

https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/OuwzBmH02M4

Design doc:

https://docs.google.com/document/d/1_XlwY3NuG2HwWEYVrR-MNvw_i4hj5ZNj3gKvxWqO0FU/edit

Bug: 878979
Change-Id: Ie45b34d5c29e04dfe65f8ff5127c6be34850d68f
Reviewed-on: https://chromium-review.googlesource.com/c/1351490
Commit-Queue: Sam Goto <goto@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Reviewed-by: Mike West <mkwst@chromium.org>
Reviewed-by: Pavel Feldman <pfeldman@chromium.org>
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
Auto-Submit: Sam Goto <goto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#624304}
  • Loading branch information
Sam Goto authored and chromium-wpt-export-bot committed Jan 18, 2019
1 parent 97f725c commit f5239a2
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
5 changes: 5 additions & 0 deletions idle-detection/META.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spec: https://github.com/inexorabletash/idle-detection
suggested_reviewers:
- goto
- jsbell
- reillyg
69 changes: 69 additions & 0 deletions idle-detection/basics.tentative.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// META: title=Idle Detection API: Basics

'use strict';

promise_test(async t => {
let promise = navigator.idle.query();
assert_equals(promise.constructor, Promise,
'query() returns a promise');

let status = await promise;
assert_true(status instanceof IdleStatus,
'query() promise resolves to an IdleStatus');

assert_true(['active', 'idle', 'locked'].includes(status.state),
'status has a valid state');
}, 'query() basics');

promise_test(async t => {
let used = false;

await navigator.idle.query({
get threshold() {
used = true;
return 1;
}
});

assert_true(used, 'query() options "threshold" member was used');
}, 'query() uses threshold property');

promise_test(async t => {
return promise_rejects(
t,
new TypeError,
navigator.idle.query({threshold: 0}),
'Threshold of 0 should reject');
}, 'query() throws with invalid threshold (0)');

promise_test(async t => {
return promise_rejects(
t,
new TypeError,
navigator.idle.query({threshold: null}),
'Threshold of null should reject');
}, 'query() throws with invalid threshold (null)');

promise_test(async t => {
return promise_rejects(
t,
new TypeError,
navigator.idle.query({threshold: -1}),
'Threshold of negative numbers should reject');
}, 'query() throws with invalid threshold (-1)');

promise_test(async t => {
return promise_rejects(
t,
new TypeError,
navigator.idle.query({threshold: NaN}),
'Threshold of NaN should reject');
}, 'query() throws with invalid threshold (NaN)');

promise_test(async t => {
return navigator.idle.query();
}, 'query() uses a default value for the threshold when none is passed');

promise_test(async t => {
return navigator.idle.query({threshold: undefined});
}, 'query() uses a default value for the threshold');
32 changes: 32 additions & 0 deletions idle-detection/idle-detection.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[SecureContext]
interface mixin NavigatorIdle {
readonly attribute IdleManager idle;
};

Navigator includes NavigatorIdle;
WorkerNavigator includes NavigatorIdle;

[
SecureContext,
Exposed=(Window,Worker)
] interface IdleManager {
Promise<IdleStatus> query(optional IdleOptions options);
};

dictionary IdleOptions {
unsigned long threshold;
};

[
SecureContext,
Exposed=(Window,Worker)
] interface IdleStatus : EventTarget {
readonly attribute IdleState state;
attribute EventHandler onchange;
};

enum IdleState {
"active",
"idle",
"locked"
};
32 changes: 32 additions & 0 deletions idle-detection/idlharness.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// META: script=/resources/WebIDLParser.js
// META: script=/resources/idlharness.js

// https://github.com/inexorabletash/idle-detection

'use strict';

promise_test(async () => {
const srcs = ['./idle-detection.idl',
'/interfaces/dom.idl', '/interfaces/html.idl'];
const [idle, dom, html] = await Promise.all(
srcs.map(i => fetch(i).then(r => r.text())));

const idl_array = new IdlArray();
idl_array.add_idls(idle);
idl_array.add_dependency_idls(dom);
idl_array.add_dependency_idls(html);

self.idle = await navigator.idle.query();

idl_array.add_objects({
IdleManager: ['navigator.idle'],
IdleStatus: ['idle'],
});
if (self.Window) {
idl_array.add_objects({ Navigator: ['navigator'] });
} else {
idl_array.add_objects({ WorkerNavigator: ['navigator'] });
}

idl_array.test();
}, 'Test IDL implementation of Idle Detection API');

0 comments on commit f5239a2

Please sign in to comment.