Commit 66cd3419 authored by harkness's avatar harkness Committed by Commit bot

Convert budget LayoutTests to use promise_test.

The infrastructure of async_test LayoutTests was changed recently, and
one effect is that an assert inside an event listener will cause the
test to time out instead of returning the error. This makes the test
much more difficult to debug failures. Additionally, promise_test is just
nicer when it's easy to return a promise.

This CL updates the existing budget tests which run in service workers to
use promise_test. It also cleans up a bit of style.

BUG=617971

Review-Url: https://codereview.chromium.org/2600733002
Cr-Commit-Position: refs/heads/master@{#441931}
parent 6edea0df
......@@ -11,32 +11,30 @@
</head>
<body>
<script>
async_test(function(test) {
var script = 'resources/instrumentation-service-worker.js';
var scope = 'resources/scope/' + location.pathname;
var port;
promise_test(function(test) {
const script = 'resources/instrumentation-service-worker.js';
const scope = 'resources/scope/' + location.pathname;
let port;
budgetServiceMock.then(mock => {
return budgetServiceMock.then(mock => {
mock.addBudget(TEST_BUDGET_TIME, TEST_BUDGET_AT);
getActiveServiceWorkerWithMessagePort(test, script, scope)
.then(function(workerInfo) {
port = workerInfo.port;
port.postMessage({command: 'getBudget'});
return getActiveServiceWorkerWithMessagePort(test, script, scope);
}).then(function(workerInfo) {
port = workerInfo.port;
port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command)
assert_unreached('Invalid message from the service worker');
port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command)
assert_unreached('Invalid message from the service worker');
assert_equals(event.data.command, 'getBudget');
assert_true(event.data.success,
'getBudget should succeed. Error message: ' + event.data.errorMessage);
assert_equals(event.data.budgetAt, TEST_BUDGET_AT);
assert_equals(event.data.time, TEST_BUDGET_TIME);
test.done();
});
})
.catch(unreached_rejection(test));
});
assert_equals(event.data.command, 'getBudget');
assert_true(event.data.success,
'getBudget should succeed. Error message: ' + event.data.errorMessage);
assert_equals(event.data.budgetAt, TEST_BUDGET_AT);
assert_equals(event.data.time, TEST_BUDGET_TIME);
});
return sendCommand(port, { command: 'getBudget' });
});
}, 'getBudget should succeed from Service Worker');
</script>
</body>
......
......@@ -11,29 +11,27 @@
</head>
<body>
<script>
async_test(function(test) {
var script = 'resources/instrumentation-service-worker.js';
var scope = 'resources/scope/' + location.pathname;
var port;
promise_test(function(test) {
const script = 'resources/instrumentation-service-worker.js';
const scope = 'resources/scope/' + location.pathname;
let port;
budgetServiceMock.then(mock => {
getActiveServiceWorkerWithMessagePort(test, script, scope)
.then(function(workerInfo) {
port = workerInfo.port;
port.postMessage({ command: 'getCostInvalidType' });
return budgetServiceMock.then(mock => {
return getActiveServiceWorkerWithMessagePort(test, script, scope);
}).then(function(workerInfo) {
port = workerInfo.port;
port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command)
assert_unreached('Invalid message from the service worker');
port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command)
assert_unreached('Invalid message from the service worker');
assert_equals(event.data.command, 'getCostInvalidType');
assert_false(event.data.success,
'getCost should fail with invalid arguments.');
assert_equals(event.data.errorMessage, "Failed to execute 'getCost' on 'BudgetService': The provided value 'frobinator' is not a valid enum value of type OperationType.");
test.done();
});
})
.catch(unreached_rejection(test));
assert_equals(event.data.command, 'getCostInvalidType');
assert_false(event.data.success,
'getCost should fail with invalid arguments.');
assert_equals(event.data.errorMessage, "Failed to execute 'getCost' on 'BudgetService': The provided value 'frobinator' is not a valid enum value of type OperationType.");
});
return sendCommand(port, { command: 'getCostInvalidType' });
});
}, 'getCost with invalid arugment should fail from Service Worker');
</script>
......
......@@ -11,30 +11,28 @@
</head>
<body>
<script>
async_test(function(test) {
var script = 'resources/instrumentation-service-worker.js';
var scope = 'resources/scope/' + location.pathname;
var port;
promise_test(function(test) {
const script = 'resources/instrumentation-service-worker.js';
const scope = 'resources/scope/' + location.pathname;
let port;
budgetServiceMock.then(mock => {
return budgetServiceMock.then(mock => {
mock.setCost("silent-push", TEST_BUDGET_COST);
getActiveServiceWorkerWithMessagePort(test, script, scope)
.then(function(workerInfo) {
port = workerInfo.port;
port.postMessage({ command: 'getCost' });
return getActiveServiceWorkerWithMessagePort(test, script, scope);
}).then(function(workerInfo) {
port = workerInfo.port;
port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command)
assert_unreached('Invalid message from the service worker');
port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command)
assert_unreached('Invalid message from the service worker');
assert_equals(event.data.command, 'getCost');
assert_true(event.data.success,
'getCost should succeed. Error message: ' + event.data.errorMessage);
assert_equals(event.data.cost, TEST_BUDGET_COST);
test.done();
});
})
.catch(unreached_rejection(test));
assert_equals(event.data.command, 'getCost');
assert_true(event.data.success,
'getCost should succeed. Error message: ' + event.data.errorMessage);
assert_equals(event.data.cost, TEST_BUDGET_COST);
});
return sendCommand(port, { command: 'getCost' });
});
}, 'getCost should succeed from Service Worker');
</script>
......
......@@ -30,7 +30,6 @@
return sendCommand(port, { command: 'checkInterfaces' });
})
.catch(unreached_rejection(test));
}, 'Budget interfaces should be available in a Service Worker');
</script>
</body>
......
......@@ -11,29 +11,27 @@
</head>
<body>
<script>
async_test(function(test) {
var script = 'resources/instrumentation-service-worker.js';
var scope = 'resources/scope/' + location.pathname;
var port;
promise_test(function(test) {
const script = 'resources/instrumentation-service-worker.js';
const scope = 'resources/scope/' + location.pathname;
let port;
budgetServiceMock.then(mock => {
return budgetServiceMock.then(mock => {
mock.setReserveSuccess(true);
getActiveServiceWorkerWithMessagePort(test, script, scope)
.then(function(workerInfo) {
port = workerInfo.port;
port.postMessage({ command: 'reserve' });
return getActiveServiceWorkerWithMessagePort(test, script, scope);
}).then(function(workerInfo) {
port = workerInfo.port;
port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command)
assert_unreached('Invalid message from the service worker');
port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command)
assert_unreached('Invalid message from the service worker');
assert_equals(event.data.command, 'reserve');
assert_true(event.data.success,
'reserve should succeed. Error message: ' + event.data.errorMessage);
test.done();
});
})
.catch(unreached_rejection(test));
assert_equals(event.data.command, 'reserve');
assert_true(event.data.success,
'reserve should succeed. Error message: ' + event.data.errorMessage);
});
return sendCommand(port, { command: 'reserve' });
});
}, 'reserve should succeed from Service Worker');
</script>
......
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