Commit 6382f72d authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

Switch Budget API LayoutTests to the new Mojo JS bindings

This change rewrites the Budget API layout tests to use the new Mojo
JS bindings.

Bug: 699569
Change-Id: I4af6e3a78c16cf4ecc6aeedc5317eaf1021ca9eb
Reviewed-on: https://chromium-review.googlesource.com/595234
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarYuzhu Shen <yzshen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491073}
parent 81112286
...@@ -8,89 +8,78 @@ const TEST_BUDGET_COST = 1.2; ...@@ -8,89 +8,78 @@ const TEST_BUDGET_COST = 1.2;
const TEST_BUDGET_AT = 2; const TEST_BUDGET_AT = 2;
const TEST_BUDGET_TIME = new Date().getTime(); const TEST_BUDGET_TIME = new Date().getTime();
let budgetServiceMock = loadMojoModules( class BudgetServiceMock {
'budgetServiceMock', constructor() {
['third_party/WebKit/public/platform/modules/budget_service/budget_service.mojom', // Values to return for the next getBudget and getCost calls.
'mojo/public/js/bindings' this.cost_ = {};
]).then(mojo => { this.budget_ = [];
const [budgetService, bindings] = mojo.modules; this.error_ = blink.mojom.BudgetServiceErrorType.NONE;
this.bindingSet_ = new mojo.BindingSet(blink.mojom.BudgetService);
class BudgetServiceMock { this.interceptor_ = new MojoInterfaceInterceptor(
constructor() { blink.mojom.BudgetService.name);
// Register process-wide (worker) interface override. this.interceptor_.oninterfacerequest =
mojo.interfaces.addInterfaceOverrideForTesting( e => this.bindingSet_.addBinding(this, e.handle);
budgetService.BudgetService.name, this.interceptor_.start();
handle => this.bindingSet_.addBinding(this, handle)); }
// Register frame interface override.
mojo.frameInterfaces.addInterfaceOverrideForTesting(
budgetService.BudgetService.name,
handle => this.bindingSet_.addBinding(this, handle));
// Values to return for the next getBudget and getCost calls.
this.cost_ = {};
this.budget_ = [];
this.error_ = budgetService.BudgetServiceErrorType.NONE;
this.bindingSet_ = new bindings.BindingSet(budgetService.BudgetService);
}
// This is called directly from test JavaScript to set up the return value // This is called directly from test JavaScript to set up the return value
// for the getCost Mojo call. The operationType mapping is needed to mirror // for the getCost Mojo call. The operationType mapping is needed to mirror
// the mapping that happens in the Mojo layer. // the mapping that happens in the Mojo layer.
setCost(operationType, cost) { setCost(operationType, cost) {
let mojoOperationType = budgetService.BudgetOperationType.INVALID_OPERATION; let mojoOperationType = blink.mojom.BudgetOperationType.INVALID_OPERATION;
if (operationType == "silent-push") if (operationType == "silent-push")
mojoOperationType = budgetService.BudgetOperationType.SILENT_PUSH; mojoOperationType = blink.mojom.BudgetOperationType.SILENT_PUSH;
this.cost_[mojoOperationType] = cost; this.cost_[mojoOperationType] = cost;
} }
// This is called directly from test JavaScript to set up the budget that is // This is called directly from test JavaScript to set up the budget that is
// returned from a later getBudget Mojo call. This adds an entry into the // returned from a later getBudget Mojo call. This adds an entry into the
// budget array. // budget array.
addBudget(addTime, addBudget) { addBudget(addTime, addBudget) {
this.budget_.push({ time: addTime, budget_at: addBudget }); this.budget_.push({ time: addTime, budgetAt: addBudget });
} }
// This is called from test JavaScript. It sets whether the next reserve // This is called from test JavaScript. It sets whether the next reserve
// call should return success or not. // call should return success or not.
setReserveSuccess(success) { setReserveSuccess(success) {
this.success_ = success; this.success_ = success;
} }
// Called from test JavaScript, this sets the error to be returned by the // Called from test JavaScript, this sets the error to be returned by the
// Mojo service to the BudgetService in Blink. This error is never surfaced // Mojo service to the BudgetService in Blink. This error is never surfaced
// to the test JavaScript, but the test code may get an exception if one of // to the test JavaScript, but the test code may get an exception if one of
// these is set. // these is set.
setError(errorName) { setError(errorName) {
switch (errorName) { switch (errorName) {
case "database-error": case "database-error":
this.error_ = budgetService.BudgetServiceErrorType.DATABASE_ERROR; this.error_ = blink.mojom.BudgetServiceErrorType.DATABASE_ERROR;
break; break;
case "not-supported": case "not-supported":
this.error_ = budgetService.BudgetServiceErrorType.NOT_SUPPORTED; this.error_ = blink.mojom.BudgetServiceErrorType.NOT_SUPPORTED;
break; break;
case "no-error": case "no-error":
this.error_ = budgetService.BudgetServiceErrorType.NONE; this.error_ = blink.mojom.BudgetServiceErrorType.NONE;
break; break;
}
} }
}
// This provides an implementation for the Mojo serivce getCost call. // This provides an implementation for the Mojo serivce getCost call.
getCost(operationType) { async getCost(operationType) {
return Promise.resolve({ cost: this.cost_[operationType] }); return { cost: this.cost_[operationType] };
} }
// This provides an implementation for the Mojo serivce getBudget call. // This provides an implementation for the Mojo serivce getBudget call.
getBudget() { async getBudget() {
return Promise.resolve({ error_type: this.error_, budget: this.budget_ }); return { errorType: this.error_, budget: this.budget_ };
} }
// This provides an implementation for the Mojo serivce reserve call. // This provides an implementation for the Mojo serivce reserve call.
reserve() { async reserve() {
return Promise.resolve({ error_type: this.error_, success: this.success_ }); return { errorType: this.error_, success: this.success_ };
}
} }
return new BudgetServiceMock(); }
});
let budgetServiceMock = new BudgetServiceMock();
This is a testharness.js-based test.
Harness Error. harness_status.status = 1 , harness_status.message = 1 duplicate test name: "BudgetService.GetBudget should return correct results."
PASS BudgetService.GetBudget should return correct results.
PASS BudgetService.GetBudget should return correct results.
Harness: the test ran to completion.
...@@ -6,35 +6,34 @@ ...@@ -6,35 +6,34 @@
<script src="../resources/testharnessreport.js"></script> <script src="../resources/testharnessreport.js"></script>
<script src="../serviceworker/resources/test-helpers.js"></script> <script src="../serviceworker/resources/test-helpers.js"></script>
<script src="../notifications/resources/test-helpers.js"></script> <script src="../notifications/resources/test-helpers.js"></script>
<script src="/js-test-resources/mojo-helpers.js"></script>
<script src="budget-service-mock.js"></script>
</head> </head>
<body> <body>
<script> <script>
promise_test(function(test) { const TEST_BUDGET_AT = 2;
const TEST_BUDGET_TIME = new Date().getTime();
promise_test(async function(test) {
const script = 'resources/instrumentation-service-worker.js'; const script = 'resources/instrumentation-service-worker.js';
const scope = 'resources/scope/' + location.pathname; const scope = 'resources/scope/' + location.pathname;
let port;
return budgetServiceMock.then(mock => {
mock.addBudget(TEST_BUDGET_TIME, TEST_BUDGET_AT);
return getActiveServiceWorkerWithMessagePort(test, script, scope);
}).then(function(workerInfo) {
port = workerInfo.port;
port.addEventListener('message', function(event) { let workerInfo =
if (typeof event.data != 'object' || !event.data.command) await getActiveServiceWorkerWithMessagePort(test, script, scope);
assert_unreached('Invalid message from the service worker'); let port = workerInfo.port;
assert_equals(event.data.command, 'getBudget'); let data = await sendCommand(port, {
assert_true(event.data.success, command: 'mockAddBudget',
'getBudget should succeed. Error message: ' + event.data.errorMessage); budgetAt: TEST_BUDGET_AT,
assert_equals(event.data.budgetAt, TEST_BUDGET_AT); time: TEST_BUDGET_TIME
assert_equals(event.data.time, TEST_BUDGET_TIME); });
}); assert_equals(data.command, 'mockAddBudget');
assert_true(data.success);
return sendCommand(port, { command: 'getBudget' }); data = await sendCommand(port, { command: 'getBudget' });
}); assert_equals(data.command, 'getBudget');
assert_true(data.success,
'getBudget should succeed. Error message: ' + data.errorMessage);
assert_equals(data.budgetAt, TEST_BUDGET_AT);
assert_equals(data.time, TEST_BUDGET_TIME);
}, 'getBudget should succeed from Service Worker'); }, 'getBudget should succeed from Service Worker');
</script> </script>
</body> </body>
......
...@@ -4,35 +4,34 @@ ...@@ -4,35 +4,34 @@
<title>Budget API: Make basic getBudget call.</title> <title>Budget API: Make basic getBudget call.</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="/js-test-resources/mojo-helpers.js"></script> <script src="/gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="/gen/third_party/WebKit/public/platform/modules/budget_service/budget_service.mojom.js"></script>
<script src="budget-service-mock.js"></script> <script src="budget-service-mock.js"></script>
</head> </head>
<body> <body>
<script> <script>
promise_test(function() { promise_test(async function() {
return budgetServiceMock.then(mock => { assert_own_property(Navigator.prototype, 'budget');
assert_own_property(Navigator.prototype, 'budget'); budgetServiceMock.addBudget(TEST_BUDGET_TIME, TEST_BUDGET_AT);
mock.addBudget(TEST_BUDGET_TIME, TEST_BUDGET_AT);
return navigator.budget.getBudget(); let budget = await navigator.budget.getBudget();
}).then(budget => { assert_equals(budget.length, 1);
assert_equals(budget.length, 1); assert_equals(budget[0].budgetAt, TEST_BUDGET_AT);
assert_equals(budget[0].budgetAt, TEST_BUDGET_AT); assert_equals(budget[0].time, TEST_BUDGET_TIME);
assert_equals(budget[0].time, TEST_BUDGET_TIME);
});
}, 'BudgetService.GetBudget should return correct results.'); }, 'BudgetService.GetBudget should return correct results.');
promise_test(function() { promise_test(async function() {
return budgetServiceMock.then(mock => { assert_own_property(Navigator.prototype, 'budget');
assert_own_property(Navigator.prototype, 'budget'); budgetServiceMock.setError("database-error");
mock.setError("database-error");
return navigator.budget.getBudget(); try {
}).then(budget => { let budget = await navigator.budget.getBudget();
assert_unreached('getBudget should have failed.'); assert_unreached('getBudget should have failed.');
}, function(error) { } catch (error) {
assert_equals(error.name, 'DataError'); assert_equals(error.name, 'DataError');
assert_equals(error.message, "Error reading the budget database."); assert_equals(error.message, "Error reading the budget database.");
}); };
}, 'BudgetService.GetBudget should return correct results.'); }, 'BudgetService.GetBudget should return database error.');
</script> </script>
</body> </body>
</html> </html>
...@@ -6,33 +6,22 @@ ...@@ -6,33 +6,22 @@
<script src="../resources/testharnessreport.js"></script> <script src="../resources/testharnessreport.js"></script>
<script src="../serviceworker/resources/test-helpers.js"></script> <script src="../serviceworker/resources/test-helpers.js"></script>
<script src="../notifications/resources/test-helpers.js"></script> <script src="../notifications/resources/test-helpers.js"></script>
<script src="/js-test-resources/mojo-helpers.js"></script>
<script src="budget-service-mock.js"></script>
</head> </head>
<body> <body>
<script> <script>
promise_test(function(test) { promise_test(async function(test) {
const script = 'resources/instrumentation-service-worker.js'; const script = 'resources/instrumentation-service-worker.js';
const scope = 'resources/scope/' + location.pathname; const scope = 'resources/scope/' + location.pathname;
let port;
return budgetServiceMock.then(mock => { let workerInfo =
return getActiveServiceWorkerWithMessagePort(test, script, scope); await getActiveServiceWorkerWithMessagePort(test, script, scope);
}).then(function(workerInfo) { let port = workerInfo.port;
port = workerInfo.port;
port.addEventListener('message', function(event) { let data = await sendCommand(port, { command: 'getCostInvalidType' });
if (typeof event.data != 'object' || !event.data.command) assert_equals(event.data.command, 'getCostInvalidType');
assert_unreached('Invalid message from the service worker'); assert_false(event.data.success,
'getCost should fail with invalid arguments.');
assert_equals(event.data.command, 'getCostInvalidType'); 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_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'); }, 'getCost with invalid arugment should fail from Service Worker');
</script> </script>
</body> </body>
......
...@@ -6,34 +6,32 @@ ...@@ -6,34 +6,32 @@
<script src="../resources/testharnessreport.js"></script> <script src="../resources/testharnessreport.js"></script>
<script src="../serviceworker/resources/test-helpers.js"></script> <script src="../serviceworker/resources/test-helpers.js"></script>
<script src="../notifications/resources/test-helpers.js"></script> <script src="../notifications/resources/test-helpers.js"></script>
<script src="/js-test-resources/mojo-helpers.js"></script>
<script src="budget-service-mock.js"></script>
</head> </head>
<body> <body>
<script> <script>
promise_test(function(test) { const TEST_BUDGET_COST = 1.2;
promise_test(async function(test) {
const script = 'resources/instrumentation-service-worker.js'; const script = 'resources/instrumentation-service-worker.js';
const scope = 'resources/scope/' + location.pathname; const scope = 'resources/scope/' + location.pathname;
let port;
return budgetServiceMock.then(mock => {
mock.setCost("silent-push", TEST_BUDGET_COST);
return getActiveServiceWorkerWithMessagePort(test, script, scope);
}).then(function(workerInfo) {
port = workerInfo.port;
port.addEventListener('message', function(event) { let workerInfo =
if (typeof event.data != 'object' || !event.data.command) await getActiveServiceWorkerWithMessagePort(test, script, scope);
assert_unreached('Invalid message from the service worker'); let port = workerInfo.port;
assert_equals(event.data.command, 'getCost'); let data = await sendCommand(port, {
assert_true(event.data.success, command: 'mockSetCost',
'getCost should succeed. Error message: ' + event.data.errorMessage); operation: 'silent-push',
assert_equals(event.data.cost, TEST_BUDGET_COST); cost: TEST_BUDGET_COST
});
return sendCommand(port, { command: 'getCost' });
}); });
assert_equals(data.command, 'mockSetCost');
assert_true(data.success);
data = await sendCommand(port, { command: 'getCost' });
assert_equals(data.command, 'getCost');
assert_true(data.success,
'getCost should succeed. Error message: ' + data.errorMessage);
assert_equals(data.cost, TEST_BUDGET_COST);
}, 'getCost should succeed from Service Worker'); }, 'getCost should succeed from Service Worker');
</script> </script>
</body> </body>
......
...@@ -4,31 +4,30 @@ ...@@ -4,31 +4,30 @@
<title>Budget API: Make basic getCost calls.</title> <title>Budget API: Make basic getCost calls.</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="/js-test-resources/mojo-helpers.js"></script> <script src="/gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="/gen/third_party/WebKit/public/platform/modules/budget_service/budget_service.mojom.js"></script>
<script src="budget-service-mock.js"></script> <script src="budget-service-mock.js"></script>
</head> </head>
<body> <body>
<script> <script>
promise_test(function() { promise_test(async function() {
return budgetServiceMock.then(mock => { assert_own_property(Navigator.prototype, 'budget');
assert_own_property(Navigator.prototype, 'budget'); budgetServiceMock.setCost("silent-push", TEST_BUDGET_COST);
mock.setCost("silent-push", TEST_BUDGET_COST);
return navigator.budget.getCost("silent-push"); let cost = await navigator.budget.getCost("silent-push");
}).then(cost => { assert_equals(cost, TEST_BUDGET_COST);
assert_equals(cost, TEST_BUDGET_COST);
});
}, 'BudgetService.GetCost should return correct results.'); }, 'BudgetService.GetCost should return correct results.');
promise_test(function() { promise_test(async function() {
return budgetServiceMock.then(mock => { assert_own_property(Navigator.prototype, 'budget');
assert_own_property(Navigator.prototype, 'budget');
return navigator.budget.getCost("frobinator"); try {
}).then(cost => { let cost = await navigator.budget.getCost("frobinator");
assert_unreached('getCost should have failed with an invalid argument.'); assert_unreached('getCost should have failed with an invalid argument.');
}, function(error) { } catch (error) {
assert_equals(error.name, 'TypeError'); assert_equals(error.name, 'TypeError');
assert_equals(error.message, "Failed to execute 'getCost' on 'BudgetService': The provided value 'frobinator' is not a valid enum value of type OperationType."); assert_equals(error.message, "Failed to execute 'getCost' on 'BudgetService': The provided value 'frobinator' is not a valid enum value of type OperationType.");
}); }
}, 'BudgetService.GetCost should return Type Error if an invalid argument is provided.'); }, 'BudgetService.GetCost should return Type Error if an invalid argument is provided.');
</script> </script>
</body> </body>
......
...@@ -6,33 +6,28 @@ ...@@ -6,33 +6,28 @@
<script src="../resources/testharnessreport.js"></script> <script src="../resources/testharnessreport.js"></script>
<script src="../serviceworker/resources/test-helpers.js"></script> <script src="../serviceworker/resources/test-helpers.js"></script>
<script src="../notifications/resources/test-helpers.js"></script> <script src="../notifications/resources/test-helpers.js"></script>
<script src="/js-test-resources/mojo-helpers.js"></script>
<script src="budget-service-mock.js"></script>
</head> </head>
<body> <body>
<script> <script>
promise_test(function(test) { promise_test(async function(test) {
const script = 'resources/instrumentation-service-worker.js'; const script = 'resources/instrumentation-service-worker.js';
const scope = 'resources/scope/' + location.pathname; const scope = 'resources/scope/' + location.pathname;
let port;
return budgetServiceMock.then(mock => { let workerInfo =
mock.setReserveSuccess(true); await getActiveServiceWorkerWithMessagePort(test, script, scope);
return getActiveServiceWorkerWithMessagePort(test, script, scope); let port = workerInfo.port;
}).then(function(workerInfo) {
port = workerInfo.port;
port.addEventListener('message', function(event) { let data = await sendCommand(port, {
if (typeof event.data != 'object' || !event.data.command) command: 'mockSetReserveSuccess',
assert_unreached('Invalid message from the service worker'); success: true
assert_equals(event.data.command, 'reserve');
assert_true(event.data.success,
'reserve should succeed. Error message: ' + event.data.errorMessage);
});
return sendCommand(port, { command: 'reserve' });
}); });
assert_equals(data.command, 'mockSetReserveSuccess');
assert_true(data.success);
data = await sendCommand(port, { command: 'reserve' });
assert_equals(data.command, 'reserve');
assert_true(data.success,
'reserve should succeed. Error message: ' + data.errorMessage);
}, 'reserve should succeed from Service Worker'); }, 'reserve should succeed from Service Worker');
</script> </script>
</body> </body>
......
...@@ -4,31 +4,30 @@ ...@@ -4,31 +4,30 @@
<title>Budget API: Make basic reserve calls.</title> <title>Budget API: Make basic reserve calls.</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="/js-test-resources/mojo-helpers.js"></script> <script src="/gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="/gen/third_party/WebKit/public/platform/modules/budget_service/budget_service.mojom.js"></script>
<script src="budget-service-mock.js"></script> <script src="budget-service-mock.js"></script>
</head> </head>
<body> <body>
<script> <script>
promise_test(function() { promise_test(async function() {
return budgetServiceMock.then(mock => { assert_own_property(Navigator.prototype, 'budget');
assert_own_property(Navigator.prototype, 'budget'); budgetServiceMock.setReserveSuccess(true);
mock.setReserveSuccess(true);
return navigator.budget.reserve("silent-push"); let success = await navigator.budget.reserve("silent-push");
}).then(success => { assert_equals(success, true);
assert_equals(success, true);
});
}, 'BudgetService.Reserve should return correct results.'); }, 'BudgetService.Reserve should return correct results.');
promise_test(function() { promise_test(async function() {
return budgetServiceMock.then(mock => { assert_own_property(Navigator.prototype, 'budget');
assert_own_property(Navigator.prototype, 'budget');
return navigator.budget.reserve("frobinator"); try {
}).then(success => { let success = await navigator.budget.reserve("frobinator");
assert_unreached('reserve should have failed with an invalid argument.'); assert_unreached('reserve should have failed with an invalid argument.');
}, function(error) { } catch (error) {
assert_equals(error.name, 'TypeError'); assert_equals(error.name, 'TypeError');
assert_equals(error.message, "Failed to execute 'reserve' on 'BudgetService': The provided value 'frobinator' is not a valid enum value of type OperationType."); assert_equals(error.message, "Failed to execute 'reserve' on 'BudgetService': The provided value 'frobinator' is not a valid enum value of type OperationType.");
}); }
}, 'BudgetService.Reserve should return Type Error if an invalid argument is provided.'); }, 'BudgetService.Reserve should return Type Error if an invalid argument is provided.');
</script> </script>
</body> </body>
......
importScripts('/gen/layout_test_data/mojo/public/js/mojo_bindings.js');
importScripts('/gen/url/mojo/origin.mojom.js');
importScripts('/gen/third_party/WebKit/public/platform/modules/budget_service/budget_service.mojom.js');
importScripts('../budget-service-mock.js');
// Allows a document to exercise the Budget API within a service worker by sending commands. // Allows a document to exercise the Budget API within a service worker by sending commands.
self.addEventListener('message', function(workerEvent) { self.addEventListener('message', function(workerEvent) {
...@@ -10,6 +15,30 @@ self.addEventListener('message', function(workerEvent) { ...@@ -10,6 +15,30 @@ self.addEventListener('message', function(workerEvent) {
var options = event.data.options || {}; var options = event.data.options || {};
switch (event.data.command) { switch (event.data.command) {
case 'mockAddBudget':
budgetServiceMock.addBudget(event.data.time, event.data.budgetAt);
port.postMessage({
command: event.data.command,
success: true
});
break;
case 'mockSetCost':
budgetServiceMock.setCost(event.data.operation, event.data.cost);
port.postMessage({
command: event.data.command,
success: true
});
break;
case 'mockSetReserveSuccess':
budgetServiceMock.setReserveSuccess(event.data.success);
port.postMessage({
command: event.data.command,
success: true
});
break;
case 'getCost': case 'getCost':
navigator.budget.getCost('silent-push').then(function(cost) { navigator.budget.getCost('silent-push').then(function(cost) {
port.postMessage({ port.postMessage({
......
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