xmpp.chapril.org-conversejs/tests/utils.js

205 lines
6.5 KiB
JavaScript
Raw Normal View History

(function (root, factory) {
2014-08-08 22:06:01 +02:00
define("test_utils", [
'jquery',
'mock'
],
function($, mock) {
return factory($, mock);
});
}(this, function ($, mock) {
var Strophe = converse_api.env.Strophe;
var utils = {};
utils.createRequest = function (iq) {
iq = typeof iq.tree == "function" ? iq.tree() : iq;
var req = new Strophe.Request(iq, function() {});
req.getResponse = function () {
var env = new Strophe.Builder('env', {type: 'mock'}).tree();
env.appendChild(iq);
return env;
};
return req;
};
utils.closeAllChatBoxes = function () {
var i, chatbox;
for (i=converse.chatboxes.models.length-1; i>-1; i--) {
chatbox = converse.chatboxes.models[i];
converse.chatboxviews.get(chatbox.get('id')).close();
}
return this;
};
utils.removeAllChatBoxes = function () {
var i, chatbox, num_chatboxes = converse.chatboxes.models.length;
for (i=num_chatboxes-1; i>-1; i--) {
chatbox = converse.chatboxes.models[i];
converse.chatboxviews.get(chatbox.get('id')).close();
2014-03-04 14:54:36 +01:00
converse.chatboxviews.get(chatbox.get('id')).$el.remove();
}
converse.chatboxviews.get('controlbox').close();
2014-03-04 14:54:36 +01:00
converse.chatboxviews.get('controlbox').$el.remove();
return this;
};
utils.initConverse = function () {
2014-09-17 10:35:24 +02:00
converse._tearDown();
2014-05-27 19:18:02 +02:00
converse._initialize();
};
utils.initRoster = function () {
converse.roster.browserStorage._clear();
converse.initRoster();
};
utils.openControlBox = function () {
var toggle = $(".toggle-controlbox");
if (!$("#controlbox").is(':visible')) {
2014-02-28 03:04:52 +01:00
if (!toggle.is(':visible')) {
toggle.show(toggle.click);
} else {
toggle.click();
}
}
return this;
};
utils.closeControlBox = function () {
if ($("#controlbox").is(':visible')) {
2014-02-28 03:04:52 +01:00
$("#controlbox").find(".close-chatbox-button").click();
}
return this;
};
utils.removeControlBox = function () {
2014-02-28 03:04:52 +01:00
converse.controlboxtoggle.show();
$('#controlbox').remove();
};
utils.openContactsPanel = function () {
2014-03-04 14:54:36 +01:00
var cbview = converse.chatboxviews.get('controlbox');
var $tabs = cbview.$el.find('#controlbox-tabs');
$tabs.find('li').first().find('a').click();
};
utils.openRoomsPanel = function () {
2014-03-04 14:54:36 +01:00
var cbview = converse.chatboxviews.get('controlbox');
var $tabs = cbview.$el.find('#controlbox-tabs');
$tabs.find('li').last().find('a').click();
};
utils.openChatBoxes = function (amount) {
var i = 0, jid, views = [];
for (i; i<amount; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
views[i] = converse.roster.get(jid).trigger("open");
}
return views;
};
utils.openChatBoxFor = function (jid) {
return converse.roster.get(jid).trigger("open");
};
utils.openChatRoom = function (room, server, nick) {
// Open a new chatroom
this.openControlBox();
this.openRoomsPanel();
var roomspanel = converse.chatboxviews.get('controlbox').roomspanel;
roomspanel.$el.find('input.new-chatroom-name').val(room);
roomspanel.$el.find('input.new-chatroom-nick').val(nick);
roomspanel.$el.find('input.new-chatroom-server').val(server);
roomspanel.$el.find('form').submit();
this.closeControlBox();
};
utils.removeRosterContacts = function () {
var model;
while (converse.rosterview.model.length) {
model = converse.rosterview.model.pop();
converse.rosterview.model.remove(model);
}
};
2014-06-30 19:55:26 +02:00
utils.clearBrowserStorage = function () {
window.localStorage.clear();
window.sessionStorage.clear();
return this;
2014-06-30 19:55:26 +02:00
};
utils.clearChatBoxMessages = function (jid) {
2014-03-04 14:54:36 +01:00
var view = converse.chatboxviews.get(jid);
view.$el.find('.chat-content').empty();
view.model.messages.reset();
2014-06-30 19:21:16 +02:00
view.model.messages.browserStorage._clear();
};
utils.createContacts = function (type, length) {
/* Create current (as opposed to requesting or pending) contacts
* for the user's roster.
*
* These contacts are not grouped. See below.
*/
var names;
if (type === 'requesting') {
names = mock.req_names;
subscription = 'none';
requesting = true;
ask = null;
} else if (type === 'pending') {
names = mock.pend_names;
subscription = 'none';
requesting = false;
ask = 'subscribe';
} else if (type === 'current') {
names = mock.cur_names;
subscription = 'both';
requesting = false;
ask = null;
} else if (type === 'all') {
this.createContacts('current').createContacts('requesting').createContacts('pending');
return this;
} else {
throw "Need to specify the type of contact to create";
}
if (typeof length === 'undefined') {
length = names.length;
}
for (i=0; i<length; i++) {
converse.roster.create({
ask: ask,
fullname: names[i],
jid: names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
requesting: requesting,
subscription: subscription
});
}
return this;
};
utils.createGroupedContacts = function () {
/* Create grouped contacts
*/
var i=0, j=0;
_.each(_.keys(mock.groups), $.proxy(function (name) {
j = i;
for (i=j; i<j+mock.groups[name]; i++) {
converse.roster.create({
jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
subscription: 'both',
ask: null,
groups: name === 'ungrouped'? [] : [name],
fullname: mock.cur_names[i]
});
}
}, converse));
};
utils.sendMessage = function (chatboxview, message) {
chatboxview.$el.find('.chat-textarea').val(message);
chatboxview.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
};
return utils;
}));