Commit 65ac09b1 authored by Matt Giuca's avatar Matt Giuca Committed by Commit Bot

Improve Badge API IDL tests.

- Added tests for scope parameter.
- Added lots of different types and values for inputs.
- Tests which value is set, not just the type of value.
- Fixed assert argument order and fix failure messages.

Explainer (current, but vague):
https://github.com/WICG/badging/blob/master/explainer.md
Spec (out of date):
https://wicg.github.io/badging/

Bug: 987514, 1000968, 1001404, 1001405
Change-Id: I518ff82a81c9a56bd45421cf8b7b16b9855a7ba6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1788966
Commit-Queue: Matt Giuca <mgiuca@chromium.org>
Reviewed-by: default avatarEric Willigers <ericwilligers@chromium.org>
Cr-Commit-Position: refs/heads/master@{#696695}
parent 41edd5e1
...@@ -11,9 +11,40 @@ ...@@ -11,9 +11,40 @@
<body> <body>
<script> <script>
badge_test(() => { ExperimentalBadge.set(-1); }, undefined, 'TypeError'); // Negative value not allowed.
badge_test(() => { ExperimentalBadge.set(-1); }, undefined, undefined,
'TypeError');
badge_test(() => { ExperimentalBadge.set("Foo"); }, undefined, 'TypeError'); // Value too large (2^53).
badge_test(() => { ExperimentalBadge.set(9007199254740992); }, undefined,
undefined, 'TypeError');
// Illegal numeric values.
badge_test(() => { ExperimentalBadge.set(Infinity); }, undefined, undefined,
'TypeError');
badge_test(() => { ExperimentalBadge.set(-Infinity); }, undefined, undefined,
'TypeError');
badge_test(() => { ExperimentalBadge.set(NaN); }, undefined, undefined,
'TypeError');
// Other values that can't convert to a long.
badge_test(() => { ExperimentalBadge.set("Foo"); }, undefined, undefined,
'TypeError');
badge_test(() => { ExperimentalBadge.set({}); }, undefined, undefined,
'TypeError');
// Wrong origin.
// TODO(mgiuca): Currently succeeds (failing in the browser).
// https://crbug.com/1001404
// badge_test(() => {
// ExperimentalBadge.set(1, {scope: 'https://wrongorigin.com/scope'});
// }, undefined, undefined, 'SecurityError');
// Invalid URL.
// TODO(mgiuca): Currently succeeds with scope = "". https://crbug.com/1001405
// badge_test(() => {
// ExperimentalBadge.set(1, {scope: 'https://example.com:99999'});
// }, undefined, undefined, 'TypeError');
</script> </script>
</body> </body>
......
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Test that Badge API accepts expected types.</title> <title>Correct interpretation of valid badge values.</title>
<script src="../resources/testharness.js"></script> <script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script> <script src="../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script> <script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
...@@ -11,16 +11,51 @@ ...@@ -11,16 +11,51 @@
<body> <body>
<script> <script>
badge_test(() => { ExperimentalBadge.set(); }, 'flag'); // The default scope URL is the current origin.
const defaultScope = new URL('/', location).href;
badge_test(() => { ExperimentalBadge.set(undefined); }, 'flag'); badge_test(() => { ExperimentalBadge.set(); }, 'flag', defaultScope);
badge_test(() => { ExperimentalBadge.set(1); }, 'number'); badge_test(() => { ExperimentalBadge.set(undefined); }, 'flag', defaultScope);
badge_test(() => { ExperimentalBadge.set(1); }, 'number:1', defaultScope);
// Non-whole number should round down to nearest integer.
badge_test(() => { ExperimentalBadge.set(10.6); }, 'number:10', defaultScope);
// Maximum allowed value (2^53 - 1).
badge_test(() => { ExperimentalBadge.set(9007199254740991); },
'number:9007199254740991', defaultScope);
// Setting the Badge to 0 should be equivalent to clearing the badge. // Setting the Badge to 0 should be equivalent to clearing the badge.
badge_test(() => { ExperimentalBadge.set(0); }, 'clear'); badge_test(() => { ExperimentalBadge.set(0); }, 'clear', defaultScope);
badge_test(() => { ExperimentalBadge.clear(); }, 'clear', defaultScope);
// Non-numeric values that convert to integer.
badge_test(() => { ExperimentalBadge.set(null); }, 'clear', defaultScope);
badge_test(() => { ExperimentalBadge.set(false); }, 'clear', defaultScope);
badge_test(() => { ExperimentalBadge.set(true); }, 'number:1', defaultScope);
badge_test(() => { ExperimentalBadge.set('3'); }, 'number:3', defaultScope);
// Test options dictionary.
badge_test(() => { ExperimentalBadge.set(1, {}); }, 'number:1', defaultScope);
badge_test(() => { ExperimentalBadge.set(1, {foo: 4}); }, 'number:1',
defaultScope);
badge_test(() => {
ExperimentalBadge.set(1, {scope: new URL('/scope', location).href});
}, 'number:1', new URL('/scope', location).href);
// Scope URL resolved against document.
badge_test(() => { ExperimentalBadge.set(1, {scope: '/scope'}); },
'number:1', new URL('/scope', location).href);
badge_test(() => { ExperimentalBadge.clear(); }, 'clear'); // Explicit undefined to set a flag with a scope.
// TODO(mgiuca): Currently fails with TypeError. https://crbug.com/1001411
// badge_test(() => {
// ExperimentalBadge.set(undefined, {scope: 'https://example.com/scope'});
// }, 'number:1', 'https://example.com/scope');
</script> </script>
</body> </body>
......
...@@ -10,8 +10,9 @@ class MockBadgeService { ...@@ -10,8 +10,9 @@ class MockBadgeService {
this.interceptor_.start(); this.interceptor_.start();
} }
init_(expectedAction) { init_(expectedAction, expectedScope) {
this.expectedAction = expectedAction; this.expectedAction = expectedAction;
this.expectedScope = expectedScope;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.reject_ = reject; this.reject_ = reject;
this.resolve_ = resolve; this.resolve_ = resolve;
...@@ -29,17 +30,19 @@ class MockBadgeService { ...@@ -29,17 +30,19 @@ class MockBadgeService {
} }
try { try {
const action = number === undefined ? 'flag' : 'number'; const action = number === undefined ? 'flag' : 'number:' + number;
assert_equals(this.expectedAction, action); assert_equals(action, this.expectedAction);
assert_equals(scope.url, this.expectedScope);
this.resolve_(); this.resolve_();
} catch (error) { } catch (error) {
this.reject_(); this.reject_(error);
} }
} }
clearBadge(scope) { clearBadge(scope) {
try { try {
assert_equals(this.expectedAction, 'clear'); assert_equals('clear', this.expectedAction);
assert_equals(scope.url, this.expectedScope);
this.resolve_(); this.resolve_();
} catch (error) { } catch (error) {
this.reject_(error); this.reject_(error);
...@@ -64,9 +67,10 @@ function callAndObserveErrors(func, expectedErrorName) { ...@@ -64,9 +67,10 @@ function callAndObserveErrors(func, expectedErrorName) {
}); });
} }
function badge_test(func, expectedAction, expectError) { function badge_test(func, expectedAction, expectedScope, expectedError) {
promise_test(() => { promise_test(() => {
let mockPromise = mockBadgeService.init_(expectedAction); let mockPromise = mockBadgeService.init_(expectedAction, expectedScope);
return Promise.race([callAndObserveErrors(func, expectError), mockPromise]); return Promise.race(
[callAndObserveErrors(func, expectedError), mockPromise]);
}); });
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment