if (window.domAutomationController) {
var ___interactiveUiTestsMode = true;
TestSuite = function()
{
this.controlTaken_ = false;
this.timerId_ = -1;
};
TestSuite.prototype.fail = function(message)
{
if (this.controlTaken_)
this.reportFailure_(message);
else
throw message;
};
TestSuite.prototype.assertEquals = function(expected, actual, opt_message)
{
if (expected !== actual) {
var message = "Expected: '" + expected + "', but was '" + actual + "'";
if (opt_message)
message = opt_message + "(" + message + ")";
this.fail(message);
}
};
TestSuite.prototype.assertTrue = function(value, opt_message)
{
this.assertEquals(true, !!value, opt_message);
};
TestSuite.prototype.assertContains = function(string, substring)
{
if (string.indexOf(substring) === -1)
this.fail("Expected to: '" + string + "' to contain '" + substring + "'");
};
TestSuite.prototype.takeControl = function()
{
this.controlTaken_ = true;
var self = this;
this.timerId_ = setTimeout(function() {
self.reportFailure_("Timeout exceeded: 20 sec");
}, 20000);
};
TestSuite.prototype.releaseControl = function()
{
if (this.timerId_ !== -1) {
clearTimeout(this.timerId_);
this.timerId_ = -1;
}
this.reportOk_();
};
TestSuite.prototype.reportOk_ = function()
{
window.domAutomationController.send("[OK]");
};
TestSuite.prototype.reportFailure_ = function(error)
{
if (this.timerId_ !== -1) {
clearTimeout(this.timerId_);
this.timerId_ = -1;
}
window.domAutomationController.send("[FAILED] " + error);
};
TestSuite.prototype.runTest = function(testName)
{
try {
this[testName]();
if (!this.controlTaken_)
this.reportOk_();
} catch (e) {
this.reportFailure_(e);
}
};
TestSuite.prototype.showPanel = function(panelName)
{
var toolbar = document.getElementById("toolbar");
var button = toolbar.getElementsByClassName(panelName)[0];
button.click();
this.assertEquals(WebInspector.panels[panelName], WebInspector.currentPanel);
};
TestSuite.prototype.addSniffer = function(receiver, methodName, override, opt_sticky)
{
var orig = receiver[methodName];
if (typeof orig !== "function")
this.fail("Cannot find method to override: " + methodName);
var test = this;
receiver[methodName] = function(var_args) {
try {
var result = orig.apply(this, arguments);
} finally {
if (!opt_sticky)
receiver[methodName] = orig;
}
try {
override.apply(this, arguments);
} catch (e) {
test.fail("Exception in overriden method '" + methodName + "': " + e);
}
return result;
};
};
TestSuite.prototype.testEnableResourcesTab = function()
{
}
TestSuite.prototype.testCompletionOnPause = function()
{
}
TestSuite.prototype.testShowScriptsTab = function()
{
this.showPanel("scripts");
var test = this;
this._waitUntilScriptsAreParsed(["debugger_test_page.html"],
function() {
test.releaseControl();
});
this.takeControl();
};
TestSuite.prototype.testScriptsTabIsPopulatedOnInspectedPageRefresh = function()
{
var test = this;
this.assertEquals(WebInspector.panels.elements, WebInspector.currentPanel, "Elements panel should be current one.");
this.addSniffer(WebInspector.panels.scripts, "reset", waitUntilScriptIsParsed);
test.evaluateInConsole_(
"window.location.reload(true);",
function(resultText) {});
function waitUntilScriptIsParsed() {
test.showPanel("scripts");
test._waitUntilScriptsAreParsed(["debugger_test_page.html"],
function() {
test.releaseControl();
});
}
this.takeControl();
};
TestSuite.prototype.testContentScriptIsPresent = function()
{
this.showPanel("scripts");
var test = this;
test._waitUntilScriptsAreParsed(
["page_with_content_script.html", "simple_content_script.js"],
function() {
test.releaseControl();
});
this.takeControl();
};
TestSuite.prototype.testNoScriptDuplicatesOnPanelSwitch = function()
{
var test = this;
var expectedScriptsCount = 2;
var parsedScripts = [];
this.showPanel("scripts");
function switchToElementsTab() {
test.showPanel("elements");
setTimeout(switchToScriptsTab, 0);
}
function switchToScriptsTab() {
test.showPanel("scripts");
setTimeout(checkScriptsPanel, 0);
}
function checkScriptsPanel() {
test.assertTrue(!!WebInspector.panels.scripts.visibleView, "No visible script view.");
test.assertTrue(test._scriptsAreParsed(["debugger_test_page.html"]), "Some scripts are missing.");
checkNoDuplicates();
test.releaseControl();
}
function checkNoDuplicates() {
var scriptSelect = document.getElementById("scripts-files");
var options = scriptSelect.options;
for (var i = 0; i < options.length; i++) {
var scriptName = options[i].text;
for (var j = i + 1; j < options.length; j++)
test.assertTrue(scriptName !== options[j].text, "Found script duplicates: " + test.optionsToString_(options));
}
}
test._waitUntilScriptsAreParsed(
["debugger_test_page.html"],
function() {
checkNoDuplicates();
setTimeout(switchToElementsTab, 0);
});
this.takeControl();
};
TestSuite.prototype.testPauseWhenLoadingDevTools = function()
{
this.showPanel("scripts");
var test = this;
var expectations = {
functionsOnStack: ["callDebugger"],
lineNumber: 8,
lineText: " debugger;"
};
if (WebInspector.currentPanel.paused) {
var callFrame = WebInspector.currentPanel._presentationModel.selectedCallFrame;
this.assertEquals(expectations.functionsOnStack[0], callFrame.functionName);
var callbackInvoked = false;
this._checkSourceFrameWhenLoaded(expectations, function() {
callbackInvoked = true;
if (test.controlTaken_)
test.releaseControl();
});
if (!callbackInvoked) {
test.takeControl();
}
return;
}
this._waitForScriptPause(
{
functionsOnStack: ["callDebugger"],
lineNumber: 8,
lineText: " debugger;"
},
function() {
test.releaseControl();
});
this.takeControl();
};
TestSuite.prototype.testPauseWhenScriptIsRunning = function()
{
this.showPanel("scripts");
var test = this;
test.evaluateInConsole_(
'setTimeout("handleClick()" , 0)',
function(resultText) {
test.assertTrue(!isNaN(resultText), "Failed to get timer id: " + resultText);
testScriptPauseAfterDelay();
});
function testScriptPauseAfterDelay() {
setTimeout(testScriptPause, 300);
}
function testScriptPause() {
WebInspector.panels.scripts.pauseButton.click();
test._waitForScriptPause(
{
functionsOnStack: ["handleClick", ""],
lineNumber: 5,
lineText: " while(true) {"
},
function() {
test.releaseControl();
});
}
this.takeControl();
};
TestSuite.prototype.testNetworkSize = function()
{
var test = this;
function finishResource(resource, finishTime)
{
test.assertEquals(219, resource.transferSize, "Incorrect total encoded data length");
test.assertEquals(25, resource.resourceSize, "Incorrect total data length");
test.releaseControl();
}
this.addSniffer(WebInspector.NetworkDispatcher.prototype, "_finishResource", finishResource);
test.evaluateInConsole_("window.location.reload(true);", function(resultText) {});
this.takeControl();
};
TestSuite.prototype.testNetworkSyncSize = function()
{
var test = this;
function finishResource(resource, finishTime)
{
test.assertEquals(219, resource.transferSize, "Incorrect total encoded data length");
test.assertEquals(25, resource.resourceSize, "Incorrect total data length");
test.releaseControl();
}
this.addSniffer(WebInspector.NetworkDispatcher.prototype, "_finishResource", finishResource);
test.evaluateInConsole_("var xhr = new XMLHttpRequest(); xhr.open(\"GET\", \"chunked\", false); xhr.send(null);", function() {});
this.takeControl();
};
TestSuite.prototype.testNetworkRawHeadersText = function()
{
var test = this;
function finishResource(resource, finishTime)
{
if (!resource.responseHeadersText)
test.fail("Failure: resource does not have response headers text");
test.assertEquals(164, resource.responseHeadersText.length, "Incorrect response headers text length");
test.releaseControl();
}
this.addSniffer(WebInspector.NetworkDispatcher.prototype, "_finishResource", finishResource);
test.evaluateInConsole_("window.location.reload(true);", function(resultText) {});
this.takeControl();
};
TestSuite.prototype.testNetworkTiming = function()
{
var test = this;
function finishResource(resource, finishTime)
{
test.assertTrue(resource.timing.receiveHeadersEnd - resource.timing.connectStart >= 70,
"Time between receiveHeadersEnd and connectStart should be >=70ms, but was " +
"receiveHeadersEnd=" + resource.timing.receiveHeadersEnd + ", connectStart=" + resource.timing.connectStart + ".");
test.assertTrue(resource.responseReceivedTime - resource.startTime >= 0.07,
"Time between responseReceivedTime and startTime should be >=0.07s, but was " +
"responseReceivedTime=" + resource.responseReceivedTime + ", startTime=" + resource.startTime + ".");
test.assertTrue(resource.endTime - resource.startTime >= 0.14,
"Time between endTime and startTime should be >=0.14s, but was " +
"endtime=" + resource.endTime + ", startTime=" + resource.startTime + ".");
test.releaseControl();
}
this.addSniffer(WebInspector.NetworkDispatcher.prototype, "_finishResource", finishResource);
test.evaluateInConsole_("window.location.reload(true);", function(resultText) {});
this.takeControl();
};
TestSuite.prototype.optionsToString_ = function(options)
{
var names = [];
for (var i = 0; i < options.length; i++)
names.push('"' + options[i].text + '"');
return names.join(",");
};
TestSuite.prototype.showMainPageScriptSource_ = function(scriptName, callback)
{
var test = this;
var scriptSelect = document.getElementById("scripts-files");
var options = scriptSelect.options;
test.assertTrue(options.length, "Scripts list is empty");
var scriptResource;
if (options[scriptSelect.selectedIndex].text === scriptName)
scriptResource = options[scriptSelect.selectedIndex].representedObject;
else {
var pageScriptIndex = -1;
for (var i = 0; i < options.length; i++) {
if (options[i].text === scriptName) {
pageScriptIndex = i;
break;
}
}
test.assertTrue(-1 !== pageScriptIndex, "Script with url " + scriptName + " not found among " + test.optionsToString_(options));
scriptResource = options[pageScriptIndex].representedObject;
WebInspector.currentPanel._showScriptOrResource(scriptResource);
test.assertEquals(pageScriptIndex, scriptSelect.selectedIndex, "Unexpected selected option index.");
}
test.assertTrue(scriptResource instanceof WebInspector.Resource,
"Unexpected resource class.");
test.assertTrue(!!scriptResource.url, "Resource URL is null.");
test.assertTrue(scriptResource.url.search(scriptName + "$") !== -1, "Main HTML resource should be selected.");
var scriptsPanel = WebInspector.panels.scripts;
var view = scriptsPanel.visibleView;
test.assertTrue(view instanceof WebInspector.SourceView);
if (!view.sourceFrame._loaded) {
test.addSniffer(view, "_sourceFrameSetupFinished", function(event) {
callback(view, scriptResource.url);
});
} else
callback(view, scriptResource.url);
};
TestSuite.prototype.evaluateInConsole_ = function(code, callback)
{
WebInspector.showConsole();
WebInspector.console.prompt.text = code;
WebInspector.console.promptElement.dispatchEvent( TestSuite.createKeyEvent("Enter"));
this.addSniffer(WebInspector.ConsoleView.prototype, "addMessage",
function(commandResult) {
callback(commandResult.toMessageElement().textContent);
});
};
TestSuite.prototype._checkExecutionLine = function(sourceFrame, lineNumber, lineContent)
{
this.assertEquals(lineNumber, sourceFrame._executionLineNumber + 1, "Unexpected execution line number.");
this.assertEquals(lineContent, sourceFrame._textModel.line(lineNumber - 1), "Unexpected execution line text.");
}
TestSuite.prototype._scriptsAreParsed = function(expected)
{
var scriptSelect = document.getElementById("scripts-files");
var options = scriptSelect.options;
var missing = expected.slice(0);
for (var i = 0 ; i < options.length; i++) {
for (var j = 0; j < missing.length; j++) {
if (options[i].text.search(missing[j]) !== -1) {
missing.splice(j, 1);
break;
}
}
}
return missing.length === 0;
};
TestSuite.prototype._waitForScriptPause = function(expectations, callback)
{
var test = this;
test.addSniffer(
WebInspector.debuggerModel,
"_pausedScript",
function(details) {
var callFrames = details.callFrames;
var functionsOnStack = [];
for (var i = 0; i < callFrames.length; i++)
functionsOnStack.push(callFrames[i].functionName);
test.assertEquals(expectations.functionsOnStack.join(","), functionsOnStack.join(","), "Unexpected stack.");
test._checkSourceFrameWhenLoaded(expectations, callback);
});
};
TestSuite.prototype._checkSourceFrameWhenLoaded = function(expectations, callback)
{
var test = this;
var frame = WebInspector.currentPanel.visibleView;
if (frame._textViewer)
checkExecLine();
else {
setTimeout(function() {
test._checkSourceFrameWhenLoaded(expectations, callback);
}, 100);
}
function checkExecLine() {
test._checkExecutionLine(frame, expectations.lineNumber, expectations.lineText);
callback();
}
};
TestSuite.prototype._executeCodeWhenScriptsAreParsed = function(code, expectedScripts)
{
var test = this;
function executeFunctionInInspectedPage() {
test.evaluateInConsole_(
'setTimeout("' + code + '" , 0)',
function(resultText) {
test.assertTrue(!isNaN(resultText), "Failed to get timer id: " + resultText + ". Code: " + code);
});
}
test._waitUntilScriptsAreParsed(expectedScripts, executeFunctionInInspectedPage);
};
TestSuite.prototype._waitUntilScriptsAreParsed = function(expectedScripts, callback)
{
var test = this;
function waitForAllScripts() {
if (test._scriptsAreParsed(expectedScripts))
callback();
else
test.addSniffer(WebInspector.panels.scripts, "_addOptionToFilesSelect", waitForAllScripts);
}
waitForAllScripts();
};
TestSuite.createKeyEvent = function(keyIdentifier)
{
var evt = document.createEvent("KeyboardEvent");
evt.initKeyboardEvent("keydown", true , true , null , keyIdentifier, "");
return evt;
};
var uiTests = {};
uiTests.runAllTests = function()
{
for (var name in TestSuite.prototype) {
if (name.substring(0, 4) === "test" && typeof TestSuite.prototype[name] === "function")
uiTests.runTest(name);
}
};
uiTests.runTest = function(name)
{
if (uiTests._populatedInterface)
new TestSuite().runTest(name);
else
uiTests._pendingTestName = name;
};
(function() {
function runTests()
{
uiTests._populatedInterface = true;
var name = uiTests._pendingTestName;
delete uiTests._pendingTestName;
if (name)
new TestSuite().runTest(name);
}
var oldShowElementsPanel = WebInspector.showElementsPanel;
WebInspector.showElementsPanel = function()
{
oldShowElementsPanel.call(this);
runTests();
}
var oldShowPanel = WebInspector.showPanel;
WebInspector.showPanel = function(name)
{
oldShowPanel.call(this, name);
runTests();
}
})();
}