Commit 86c8d526 authored by Noel Gordon's avatar Noel Gordon Committed by Commit Bot

Integration tests: add sendBrowserTestCommand helper

Add sendBrowserTestCommand that expects a 'string' result: this helper
retries until a 'string' result is received, and logs errors.

Update sendTestMessage docs (change message -> command) and update the
test extension harness to use sendBrowserTestCommand.

Bug: 891148
Change-Id: I5840668919672e68787eccd8dece3eae5f2d8c45
Reviewed-on: https://chromium-review.googlesource.com/1256482Reviewed-by: default avatarJoel Hockey <joelhockey@chromium.org>
Commit-Queue: Noel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595733}
parent 4b965235
......@@ -437,15 +437,13 @@ window.addEventListener('load', function() {
var steps = [
// Request the guest mode state.
function() {
chrome.test.sendMessage(
JSON.stringify({name: 'isInGuestMode'}), steps.shift());
sendBrowserTestCommand({name: 'isInGuestMode'}, steps.shift());
},
// Request the root entry paths.
function(mode) {
if (JSON.parse(mode) != chrome.extension.inIncognitoContext)
return;
chrome.test.sendMessage(
JSON.stringify({name: 'getRootPaths'}), steps.shift());
sendBrowserTestCommand({name: 'getRootPaths'}, steps.shift());
},
// Request the test case name.
function(paths) {
......@@ -453,15 +451,14 @@ window.addEventListener('load', function() {
RootPath.DOWNLOADS = roots.downloads;
RootPath.DRIVE = roots.drive;
RootPath.ANDROID_FILES = roots.android_files;
chrome.test.sendMessage(
JSON.stringify({name: 'getTestName'}), steps.shift());
sendBrowserTestCommand({name: 'getTestName'}, steps.shift());
},
// Run the test case.
function(testCaseName) {
// Get the test function from testcase namespace testCaseName.
var test = testcase[testCaseName];
// Verify test is an unnamed (aka 'anonymous') Function.
if (!test instanceof Function || test.name) {
if (!(test instanceof Function) || test.name) {
chrome.test.fail('[' + testCaseName + '] not found.');
return;
}
......
......@@ -5,8 +5,8 @@
'use strict';
/**
* Sends a message to the controlling test harness, namely and usually, the
* chrome FileManagerBrowserTest harness: it expects the message to contain
* Sends a command to the controlling test harness, namely and usually, the
* chrome FileManagerBrowserTest harness: it expects the command to contain
* the 'name' of the command, and any required or optional arguments of the
* command, e.g.,
*
......@@ -16,18 +16,18 @@
* entries: entries
* }).then(...);
*
* @param {Object} message Message object to be sent. The object is converted
* to a JSON string prior to sending.
* @param {Object} command Test command to send. The object is converted to
* a JSON string prior to sending.
* @return {Promise} Promise to be fulfilled with the value returned by the
* chrome.test.sendMessage callback.
*/
function sendTestMessage(message) {
if (typeof message.name === 'string') {
function sendTestMessage(command) {
if (typeof command.name === 'string') {
return new Promise(function(fulfill) {
chrome.test.sendMessage(JSON.stringify(message), fulfill);
chrome.test.sendMessage(JSON.stringify(command), fulfill);
});
} else {
let error = 'sendTestMessage requires a message.name <string>';
const error = 'sendTestMessage requires a command.name <string>';
throw new Error(error);
}
}
......@@ -165,6 +165,33 @@ function repeatUntil(checkFunction) {
return step();
}
/**
* Sends the test |command| to the browser test harness and awaits a 'string'
* result. Calls |callback| with that result.
* @param {Object} command Test command to send. Refer to sendTestMessage()
* above for the expected format of a test |command| object.
* @param {function(string)} callback Completion callback.
* @param {Object=} opt_debug If truthy, log the result.
*/
function sendBrowserTestCommand(command, callback, opt_debug) {
const caller = getCaller();
if (typeof command.name !== 'string')
chrome.test.fail('Invalid test command: ' + JSON.stringify(command));
repeatUntil(function sendTestCommand() {
const tryAgain = pending(caller, 'Sent BrowserTest ' + command.name);
return sendTestMessage(command).then((result) => {
if (typeof result !== 'string')
return tryAgain;
if (opt_debug)
console.log('BrowserTest ' + command.name + ': ' + result);
callback(result);
}).catch((error) => {
console.log(error.stack || error);
return tryAgain;
});
});
}
/**
* Adds the givin entries to the target volume(s).
* @param {Array<string>} volumeNames Names of target volumes.
......
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