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,17 +11,16 @@ ...@@ -11,17 +11,16 @@
</head> </head>
<body> <body>
<script> <script>
async_test(function(test) { promise_test(function(test) {
var script = 'resources/instrumentation-service-worker.js'; const script = 'resources/instrumentation-service-worker.js';
var scope = 'resources/scope/' + location.pathname; const scope = 'resources/scope/' + location.pathname;
var port; let port;
budgetServiceMock.then(mock => { return budgetServiceMock.then(mock => {
mock.addBudget(TEST_BUDGET_TIME, TEST_BUDGET_AT); mock.addBudget(TEST_BUDGET_TIME, TEST_BUDGET_AT);
getActiveServiceWorkerWithMessagePort(test, script, scope) return getActiveServiceWorkerWithMessagePort(test, script, scope);
.then(function(workerInfo) { }).then(function(workerInfo) {
port = workerInfo.port; port = workerInfo.port;
port.postMessage({command: 'getBudget'});
port.addEventListener('message', function(event) { port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command) if (typeof event.data != 'object' || !event.data.command)
...@@ -32,10 +31,9 @@ ...@@ -32,10 +31,9 @@
'getBudget should succeed. Error message: ' + event.data.errorMessage); 'getBudget should succeed. Error message: ' + event.data.errorMessage);
assert_equals(event.data.budgetAt, TEST_BUDGET_AT); assert_equals(event.data.budgetAt, TEST_BUDGET_AT);
assert_equals(event.data.time, TEST_BUDGET_TIME); assert_equals(event.data.time, TEST_BUDGET_TIME);
test.done();
}); });
})
.catch(unreached_rejection(test)); return sendCommand(port, { command: 'getBudget' });
}); });
}, 'getBudget should succeed from Service Worker'); }, 'getBudget should succeed from Service Worker');
</script> </script>
......
...@@ -11,16 +11,15 @@ ...@@ -11,16 +11,15 @@
</head> </head>
<body> <body>
<script> <script>
async_test(function(test) { promise_test(function(test) {
var script = 'resources/instrumentation-service-worker.js'; const script = 'resources/instrumentation-service-worker.js';
var scope = 'resources/scope/' + location.pathname; const scope = 'resources/scope/' + location.pathname;
var port; let port;
budgetServiceMock.then(mock => { return budgetServiceMock.then(mock => {
getActiveServiceWorkerWithMessagePort(test, script, scope) return getActiveServiceWorkerWithMessagePort(test, script, scope);
.then(function(workerInfo) { }).then(function(workerInfo) {
port = workerInfo.port; port = workerInfo.port;
port.postMessage({ command: 'getCostInvalidType' });
port.addEventListener('message', function(event) { port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command) if (typeof event.data != 'object' || !event.data.command)
...@@ -30,10 +29,9 @@ ...@@ -30,10 +29,9 @@
assert_false(event.data.success, assert_false(event.data.success,
'getCost should fail with invalid arguments.'); '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."); 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)); return sendCommand(port, { command: 'getCostInvalidType' });
}); });
}, 'getCost with invalid arugment should fail from Service Worker'); }, 'getCost with invalid arugment should fail from Service Worker');
</script> </script>
......
...@@ -11,17 +11,16 @@ ...@@ -11,17 +11,16 @@
</head> </head>
<body> <body>
<script> <script>
async_test(function(test) { promise_test(function(test) {
var script = 'resources/instrumentation-service-worker.js'; const script = 'resources/instrumentation-service-worker.js';
var scope = 'resources/scope/' + location.pathname; const scope = 'resources/scope/' + location.pathname;
var port; let port;
budgetServiceMock.then(mock => { return budgetServiceMock.then(mock => {
mock.setCost("silent-push", TEST_BUDGET_COST); mock.setCost("silent-push", TEST_BUDGET_COST);
getActiveServiceWorkerWithMessagePort(test, script, scope) return getActiveServiceWorkerWithMessagePort(test, script, scope);
.then(function(workerInfo) { }).then(function(workerInfo) {
port = workerInfo.port; port = workerInfo.port;
port.postMessage({ command: 'getCost' });
port.addEventListener('message', function(event) { port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command) if (typeof event.data != 'object' || !event.data.command)
...@@ -31,10 +30,9 @@ ...@@ -31,10 +30,9 @@
assert_true(event.data.success, assert_true(event.data.success,
'getCost should succeed. Error message: ' + event.data.errorMessage); 'getCost should succeed. Error message: ' + event.data.errorMessage);
assert_equals(event.data.cost, TEST_BUDGET_COST); assert_equals(event.data.cost, TEST_BUDGET_COST);
test.done();
}); });
})
.catch(unreached_rejection(test)); return sendCommand(port, { command: 'getCost' });
}); });
}, 'getCost should succeed from Service Worker'); }, 'getCost should succeed from Service Worker');
</script> </script>
......
...@@ -30,7 +30,6 @@ ...@@ -30,7 +30,6 @@
return sendCommand(port, { command: 'checkInterfaces' }); return sendCommand(port, { command: 'checkInterfaces' });
}) })
.catch(unreached_rejection(test));
}, 'Budget interfaces should be available in a Service Worker'); }, 'Budget interfaces should be available in a Service Worker');
</script> </script>
</body> </body>
......
...@@ -11,17 +11,16 @@ ...@@ -11,17 +11,16 @@
</head> </head>
<body> <body>
<script> <script>
async_test(function(test) { promise_test(function(test) {
var script = 'resources/instrumentation-service-worker.js'; const script = 'resources/instrumentation-service-worker.js';
var scope = 'resources/scope/' + location.pathname; const scope = 'resources/scope/' + location.pathname;
var port; let port;
budgetServiceMock.then(mock => { return budgetServiceMock.then(mock => {
mock.setReserveSuccess(true); mock.setReserveSuccess(true);
getActiveServiceWorkerWithMessagePort(test, script, scope) return getActiveServiceWorkerWithMessagePort(test, script, scope);
.then(function(workerInfo) { }).then(function(workerInfo) {
port = workerInfo.port; port = workerInfo.port;
port.postMessage({ command: 'reserve' });
port.addEventListener('message', function(event) { port.addEventListener('message', function(event) {
if (typeof event.data != 'object' || !event.data.command) if (typeof event.data != 'object' || !event.data.command)
...@@ -30,10 +29,9 @@ ...@@ -30,10 +29,9 @@
assert_equals(event.data.command, 'reserve'); assert_equals(event.data.command, 'reserve');
assert_true(event.data.success, assert_true(event.data.success,
'reserve should succeed. Error message: ' + event.data.errorMessage); 'reserve should succeed. Error message: ' + event.data.errorMessage);
test.done();
}); });
})
.catch(unreached_rejection(test)); return sendCommand(port, { command: 'reserve' });
}); });
}, 'reserve should succeed from Service Worker'); }, 'reserve should succeed from Service Worker');
</script> </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