2 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * The callback to send test messages to. By default we will assume that we
9 * are being run by an automated test case such as a browser test, but this can
13 var gReturnCallback
= sendToTest
;
16 * The callback to send debug messages to. By default we assume console.log.
19 var gDebugCallback
= consoleLog_
;
22 * Replaces the test message callback. Test messages are messages sent by the
23 * returnToTest function.
25 * @param callback A function that takes a single string (the message).
27 function replaceReturnCallback(callback
) {
28 gReturnCallback
= callback
;
32 * Replaces the debug message callback. Debug messages are messages sent by the
35 * @param callback A function that takes a single string (the message).
37 function replaceDebugCallback(callback
) {
38 gDebugCallback
= callback
;
41 // Helper / error handling functions.
44 * Prints a debug message on the webpage itself.
47 if (gOurClientName
== null)
50 prefix
= gOurClientName
+ ' says: ';
52 gDebugCallback(prefix
+ txt
);
56 * Sends a value back to the test.
58 * @param {string} message The message to return.
60 function returnToTest(message
) {
61 gReturnCallback(message
);
65 * Sends a message to the test case. Requires that this javascript was
66 * loaded by the test. This will make the test proceed if it is blocked in a
67 * ExecuteJavascript call.
69 * @param {string} message The message to send.
71 function sendToTest(message
) {
72 debug('Returning ' + message
+ ' to test.');
73 window
.domAutomationController
.send(message
);
77 * Fails the test by generating an exception. If the test automation is calling
78 * into us, make sure to fail the test as fast as possible. You must use this
81 * throw failTest('my reason');
85 function failTest(reason
) {
86 console
.error(reason
);
87 returnToTest('Test failed: ' + reason
);
88 return new Error(reason
);
92 function consoleLog_(message
) {
93 // It is not legal to treat console.log as a first-class object, so wrap it.