Commit defb343e authored by Devlin Cronin's avatar Devlin Cronin Committed by Commit Bot

[Extensions] Move some chrome.test APIs to just be normal functions

Currently, there there exist chrome.test.runNextTest() and
chrome.test.assertBool(). However, these are implementation details of
the chrome.test API, and should not be used directly.

Instead of exposing them on the API (which is unnecessary), just have
them be normal functions in the custom bindings.

Bug: None
Change-Id: Ia5fc47fe5b357ac9507aed52c76b132ea582a0af
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2391925Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804433}
parent 98f42b7c
......@@ -32,8 +32,8 @@ chrome.app.runtime.onLaunched.addListener(function() {
e.message);
failedToAddLast = true;
}
chrome.test.assertBool(
true, failedToAddLast,
chrome.test.assertTrue(
failedToAddLast,
"there should be an error when adding the 11th listener");
listeners.forEach(function(x) {
......
......@@ -131,12 +131,6 @@
"nocompile": true,
"parameters": []
},
{
"name": "runNextTest",
"type": "function",
"nocompile": true,
"parameters": []
},
{
"name": "fail",
"type": "function",
......@@ -200,22 +194,6 @@
{"type": "string", "name": "message", "optional": true}
]
},
{
"name": "assertBool",
"type": "function",
"nocompile": true,
"parameters": [
{
"name": "test",
"choices": [
{"type": "string"},
{"type": "boolean"}
]
},
{"type": "boolean", "name": "expected"},
{"type": "string", "name": "message", "optional": true}
]
},
{
"name": "checkDeepEq",
"type": "function",
......
......@@ -29,6 +29,43 @@ apiBridge.registerCustomHook(function(api) {
var testsFailed = 0;
var testCount = 1;
var failureException = 'chrome.test.failure';
var pendingCallbacks = 0;
function safeFunctionApply(func, args) {
try {
if (func)
return $Function.apply(func, undefined, args);
} catch (e) {
if (e === failureException)
throw e;
handleException(e.message, e);
}
}
function runNextTest() {
// There may have been callbacks which were interrupted by failure
// exceptions.
pendingCallbacks = 0;
lastTest = currentTest;
currentTest = $Array.shift(chromeTest.tests);
if (!currentTest) {
allTestsDone();
return;
}
try {
chromeTest.log("( RUN ) " + testName(currentTest));
bindingUtil.setExceptionHandler(function(message, e) {
if (e !== failureException)
chromeTest.fail('uncaught exception: ' + message);
});
$Function.call(currentTest);
} catch (e) {
handleException(e.message, e);
}
}
// Helper function to get around the fact that function names in javascript
// are read-only, and you can't assign one to anonymous functions.
......@@ -37,7 +74,7 @@ apiBridge.registerCustomHook(function(api) {
}
function testDone() {
environmentSpecificBindings.testDone(chromeTest.runNextTest);
environmentSpecificBindings.testDone(runNextTest);
}
function allTestsDone() {
......@@ -49,7 +86,18 @@ apiBridge.registerCustomHook(function(api) {
}
}
var pendingCallbacks = 0;
// Helper function for boolean asserts. Compares |test| to |expected|.
function assertBool(test, expected, message) {
if (test !== expected) {
if (typeof(test) == "string") {
if (message)
message = test + "\n" + message;
else
message = test;
}
chromeTest.fail(message);
}
}
apiFunctions.setHandleRequest('callbackAdded', function() {
pendingCallbacks++;
......@@ -74,31 +122,6 @@ apiBridge.registerCustomHook(function(api) {
};
});
apiFunctions.setHandleRequest('runNextTest', function() {
// There may have been callbacks which were interrupted by failure
// exceptions.
pendingCallbacks = 0;
lastTest = currentTest;
currentTest = $Array.shift(chromeTest.tests);
if (!currentTest) {
allTestsDone();
return;
}
try {
chromeTest.log("( RUN ) " + testName(currentTest));
bindingUtil.setExceptionHandler(function(message, e) {
if (e !== failureException)
chromeTest.fail('uncaught exception: ' + message);
});
$Function.call(currentTest);
} catch (e) {
handleException(e.message, e);
}
});
apiFunctions.setHandleRequest('fail', function failHandler(message) {
chromeTest.log("( FAILED ) " + testName(currentTest));
......@@ -135,24 +158,11 @@ apiBridge.registerCustomHook(function(api) {
});
apiFunctions.setHandleRequest('assertTrue', function(test, message) {
chromeTest.assertBool(test, true, message);
assertBool(test, true, message);
});
apiFunctions.setHandleRequest('assertFalse', function(test, message) {
chromeTest.assertBool(test, false, message);
});
apiFunctions.setHandleRequest('assertBool',
function(test, expected, message) {
if (test !== expected) {
if (typeof(test) == "string") {
if (message)
message = test + "\n" + message;
else
message = test;
}
chromeTest.fail(message);
}
assertBool(test, false, message);
});
apiFunctions.setHandleRequest('checkDeepEq', function(expected, actual) {
......@@ -274,17 +284,6 @@ apiBridge.registerCustomHook(function(api) {
}
});
function safeFunctionApply(func, args) {
try {
if (func)
return $Function.apply(func, undefined, args);
} catch (e) {
if (e === failureException)
throw e;
handleException(e.message, e);
}
};
// Wrapper for generating test functions, that takes care of calling
// assertNoLastError() and (optionally) succeed() for you.
apiFunctions.setHandleRequest('callback', function(func, expectedError) {
......@@ -347,7 +346,7 @@ apiBridge.registerCustomHook(function(api) {
apiFunctions.setHandleRequest('runTests', function(tests) {
chromeTest.tests = tests;
testCount = chromeTest.tests.length;
chromeTest.runNextTest();
runNextTest();
});
apiFunctions.setHandleRequest('getApiDefinitions', function() {
......
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