Newer
Older
Shane Tomlinson
committed
/*jshint browsers: true laxbreak: true, expr: true */
/*global BrowserID: true, ok: true, equal: true, start: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Shane Tomlinson
committed
BrowserID.TestHelpers = (function() {
"use strict";
var bid = BrowserID,
mediator = bid.Mediator,
network = bid.Network,
storage = bid.Storage,
xhr = bid.XHR,
transport = bid.Mocks.xhr,
provisioning = bid.Mocks.Provisioning,
screens = bid.Screens,
Shane Tomlinson
committed
registrations = [],
calls = {},
testOrigin = "https://browserid.org";
function register(message, cb) {
registrations.push(mediator.subscribe(message, function(msg, info) {
if(calls[msg]) {
throw msg + " triggered more than once";
}
calls[msg] = true;
cb && cb.apply(null, arguments);
}));
}
function unregisterAll() {
for(var i = 0, registration; registration = registrations[i]; ++i) {
mediator.unsubscribe(registration);
}
registrations = [];
calls = {};
}
function checkNetworkError() {
ok($("#error .contents").text().length, "contents have been written");
ok($("#error #action").text().length, "action contents have been written");
ok($("#error #network").text().length, "network contents have been written");
}
function clearStorage() {
for(var key in localStorage) {
localStorage.removeItem(key);
}
}
Shane Tomlinson
committed
var TestHelpers = {
XHR_TIME_UNTIL_DELAY: 100,
setup: function() {
unregisterAll();
mediator.reset();
xhr.init({
transport: transport,
Shane Tomlinson
committed
time_until_delay: TestHelpers.XHR_TIME_UNTIL_DELAY
});
transport.setDelay(0);
transport.setContextInfo("auth_level", undefined);
transport.setContextInfo("cookies_enabled", true);
transport.useResult("valid");
network.init();
Shane Tomlinson
committed
$("body").stop().show();
$("body")[0].className = "";
$(".error").removeClass("error");
$("#error").hide();
$(".notification").stop().hide();
screens.wait.hide();
screens.error.hide();
screens.delay.hide();
provisioning.setStatus(provisioning.NOT_AUTHENTICATED);
Shane Tomlinson
committed
user.reset();
user.init({
provisioning: provisioning
});
user.setOrigin(testOrigin);
Shane Tomlinson
committed
},
teardown: function() {
unregisterAll();
mediator.reset();
xhr.init({
transport: $,
Shane Tomlinson
committed
time_until_delay: 10 * 1000
});
network.init();
screens.wait.hide();
screens.error.hide();
screens.delay.hide();
provisioning.setStatus(provisioning.NOT_AUTHENTICATED);
testOrigin: testOrigin,
register: register,
Shane Tomlinson
committed
isTriggered: function(message) {
return calls[message];
},
Shane Tomlinson
committed
testTriggered: function(message) {
equal(calls[message], true, message + " was triggered");
},
errorVisible: function() {
return screens.error.visible;
},
Shane Tomlinson
committed
testErrorVisible: function() {
Shane Tomlinson
committed
equal(TestHelpers.errorVisible(), true, "error screen is visible");
Shane Tomlinson
committed
waitVisible: function() {
return screens.wait.visible;
},
testWaitVisible: function() {
equal(TestHelpers.waitVisible(), true, "wait screen is visible");
},
Shane Tomlinson
committed
delayVisible: function() {
return screens.delay.visible;
},
testDelayVisible: function() {
equal(TestHelpers.delayVisible(), true, "delay screen is visible");
},
checkNetworkError: checkNetworkError,
unexpectedSuccess: function() {
ok(false, "unexpected success");
start();
},
expectedXHRFailure: function() {
ok(true, "expected XHR failure");
start();
},
unexpectedXHRFailure: function() {
ok(false, "unexpected XHR failure");
start();
},
testTooltipVisible: function() {
equal(tooltip.shown, true, "tooltip is visible");
},
failureCheck: function failureCheck(cb) {
// Take the original arguments, take off the function. Add any additional
// arguments that were passed in, and then tack on the onSuccess and
// onFailure to the end. Then call the callback.
var args = [].slice.call(arguments, 1);
var errorInfo;
args.push(bid.TestHelpers.unexpectedSuccess, function onFailure(info) {
ok(true, "XHR failure should never pass");
ok(info.network.url, "url is in network info");
ok(info.network.type, "request type is in network info");
equal(info.network.textStatus, "errorStatus", "textStatus is in network info");
equal(info.network.errorThrown, "errorThrown", "errorThrown is in response info");
start();
});
if(transport.resultType === "valid") {
transport.useResult("ajaxError");
Shane Tomlinson
committed
},
/**
* Generate a long string
*/
generateString: function(length) {
var str = "";
for(var i = 0; i < length; i++) {
str += (i % 10);
}
return str;
Shane Tomlinson
committed
return TestHelpers;