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() { ...@@ -437,15 +437,13 @@ window.addEventListener('load', function() {
var steps = [ var steps = [
// Request the guest mode state. // Request the guest mode state.
function() { function() {
chrome.test.sendMessage( sendBrowserTestCommand({name: 'isInGuestMode'}, steps.shift());
JSON.stringify({name: 'isInGuestMode'}), steps.shift());
}, },
// Request the root entry paths. // Request the root entry paths.
function(mode) { function(mode) {
if (JSON.parse(mode) != chrome.extension.inIncognitoContext) if (JSON.parse(mode) != chrome.extension.inIncognitoContext)
return; return;
chrome.test.sendMessage( sendBrowserTestCommand({name: 'getRootPaths'}, steps.shift());
JSON.stringify({name: 'getRootPaths'}), steps.shift());
}, },
// Request the test case name. // Request the test case name.
function(paths) { function(paths) {
...@@ -453,15 +451,14 @@ window.addEventListener('load', function() { ...@@ -453,15 +451,14 @@ window.addEventListener('load', function() {
RootPath.DOWNLOADS = roots.downloads; RootPath.DOWNLOADS = roots.downloads;
RootPath.DRIVE = roots.drive; RootPath.DRIVE = roots.drive;
RootPath.ANDROID_FILES = roots.android_files; RootPath.ANDROID_FILES = roots.android_files;
chrome.test.sendMessage( sendBrowserTestCommand({name: 'getTestName'}, steps.shift());
JSON.stringify({name: 'getTestName'}), steps.shift());
}, },
// Run the test case. // Run the test case.
function(testCaseName) { function(testCaseName) {
// Get the test function from testcase namespace testCaseName. // Get the test function from testcase namespace testCaseName.
var test = testcase[testCaseName]; var test = testcase[testCaseName];
// Verify test is an unnamed (aka 'anonymous') Function. // 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.'); chrome.test.fail('[' + testCaseName + '] not found.');
return; return;
} }
......
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
'use strict'; 'use strict';
/** /**
* Sends a message to the controlling test harness, namely and usually, the * Sends a command to the controlling test harness, namely and usually, the
* chrome FileManagerBrowserTest harness: it expects the message to contain * chrome FileManagerBrowserTest harness: it expects the command to contain
* the 'name' of the command, and any required or optional arguments of the * the 'name' of the command, and any required or optional arguments of the
* command, e.g., * command, e.g.,
* *
...@@ -16,18 +16,18 @@ ...@@ -16,18 +16,18 @@
* entries: entries * entries: entries
* }).then(...); * }).then(...);
* *
* @param {Object} message Message object to be sent. The object is converted * @param {Object} command Test command to send. The object is converted to
* to a JSON string prior to sending. * a JSON string prior to sending.
* @return {Promise} Promise to be fulfilled with the value returned by the * @return {Promise} Promise to be fulfilled with the value returned by the
* chrome.test.sendMessage callback. * chrome.test.sendMessage callback.
*/ */
function sendTestMessage(message) { function sendTestMessage(command) {
if (typeof message.name === 'string') { if (typeof command.name === 'string') {
return new Promise(function(fulfill) { return new Promise(function(fulfill) {
chrome.test.sendMessage(JSON.stringify(message), fulfill); chrome.test.sendMessage(JSON.stringify(command), fulfill);
}); });
} else { } else {
let error = 'sendTestMessage requires a message.name <string>'; const error = 'sendTestMessage requires a command.name <string>';
throw new Error(error); throw new Error(error);
} }
} }
...@@ -165,6 +165,33 @@ function repeatUntil(checkFunction) { ...@@ -165,6 +165,33 @@ function repeatUntil(checkFunction) {
return step(); 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). * Adds the givin entries to the target volume(s).
* @param {Array<string>} volumeNames Names of target volumes. * @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