2017-11-25 22:22:36 +01:00
|
|
|
set_global('$', global.make_zjquery());
|
|
|
|
set_global('document', {
|
|
|
|
location: { },
|
|
|
|
});
|
2018-02-13 23:29:17 +01:00
|
|
|
set_global('navigator', {
|
|
|
|
userAgent: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
|
|
|
|
});
|
2017-11-25 22:22:36 +01:00
|
|
|
set_global('i18n', global.stub_i18n);
|
|
|
|
set_global('page_params', { });
|
|
|
|
set_global('csrf_token', { });
|
2018-05-31 23:55:28 +02:00
|
|
|
global.patch_builtin('window', {
|
2017-11-28 22:36:58 +01:00
|
|
|
bridge: false,
|
|
|
|
});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
|
|
|
// Setting these up so that we can test that links to uploads within messages are
|
|
|
|
// automatically converted to server relative links.
|
|
|
|
global.document.location.protocol = 'https:';
|
|
|
|
global.document.location.host = 'foo.com';
|
|
|
|
|
|
|
|
zrequire('compose_ui');
|
|
|
|
zrequire('compose_state');
|
|
|
|
zrequire('compose');
|
|
|
|
zrequire('upload');
|
|
|
|
|
2018-02-13 22:48:48 +01:00
|
|
|
var upload_opts = upload.options({ mode: "compose" });
|
2018-02-13 22:39:51 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('upload_started', () => {
|
2017-11-25 22:22:36 +01:00
|
|
|
$("#compose-send-button").prop('disabled', false);
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").removeClass("alert-info").hide();
|
2017-11-26 20:01:37 +01:00
|
|
|
$(".compose-send-status-close").one = function (ev_name, handler) {
|
2017-11-25 22:22:36 +01:00
|
|
|
assert.equal(ev_name, 'click');
|
|
|
|
assert(handler);
|
|
|
|
};
|
2017-11-26 20:03:46 +01:00
|
|
|
$("#compose-error-msg").html('');
|
2018-04-10 23:36:07 +02:00
|
|
|
var test_html = '<div class="progress active">' +
|
2018-04-14 11:48:22 +02:00
|
|
|
'<div class="bar" id="compose-upload-bar-1549958107000" style="width: 0"></div>' +
|
2018-04-05 04:29:21 +02:00
|
|
|
'</div>';
|
|
|
|
$("#compose-send-status").append = function (html) {
|
2017-11-25 22:22:36 +01:00
|
|
|
assert.equal(html, test_html);
|
|
|
|
};
|
2018-08-15 03:57:35 +02:00
|
|
|
$('#compose-textarea').caret = function () {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
document.execCommand = function (command, show_default, value) {
|
|
|
|
assert.equal(value, "[Uploading some-file…]() ");
|
|
|
|
};
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2018-02-13 22:39:51 +01:00
|
|
|
upload_opts.drop();
|
2018-08-15 03:57:35 +02:00
|
|
|
upload_opts.uploadStarted(0, {
|
|
|
|
lastModified: 1549958107000,
|
|
|
|
name: 'some-file',
|
|
|
|
}, 1);
|
2017-11-25 22:22:36 +01:00
|
|
|
|
|
|
|
assert.equal($("#compose-send-button").attr("disabled"), '');
|
2017-11-26 19:58:36 +01:00
|
|
|
assert($("#compose-send-status").hasClass("alert-info"));
|
|
|
|
assert($("#compose-send-status").visible());
|
2017-11-25 22:22:36 +01:00
|
|
|
assert.equal($("<p>").text(), 'translated: Uploading…');
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('progress_updated', () => {
|
2017-11-25 22:22:36 +01:00
|
|
|
var width_update_checked = false;
|
2018-04-14 11:48:22 +02:00
|
|
|
$("#compose-upload-bar-1549958107000").width = function (width_percent) {
|
2017-11-25 22:22:36 +01:00
|
|
|
assert.equal(width_percent, '39%');
|
|
|
|
width_update_checked = true;
|
|
|
|
};
|
2018-04-14 11:48:22 +02:00
|
|
|
upload_opts.progressUpdated(1, {lastModified: 1549958107000}, 39);
|
2017-11-25 22:22:36 +01:00
|
|
|
assert(width_update_checked);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('upload_error', () => {
|
2017-11-25 22:22:36 +01:00
|
|
|
function setup_test() {
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").removeClass("alert-error");
|
|
|
|
$("#compose-send-status").addClass("alert-info");
|
2017-11-25 22:22:36 +01:00
|
|
|
$("#compose-send-button").attr("disabled", 'disabled');
|
2017-11-26 20:03:46 +01:00
|
|
|
$("#compose-error-msg").text('');
|
2018-04-05 04:29:21 +02:00
|
|
|
|
2018-04-14 11:48:22 +02:00
|
|
|
$("#compose-upload-bar-1549958107000").parent = function () {
|
2018-04-05 04:29:21 +02:00
|
|
|
return { remove: function () {} };
|
|
|
|
};
|
2017-11-25 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2018-01-27 03:30:26 +01:00
|
|
|
function assert_side_effects(msg) {
|
2017-11-26 19:58:36 +01:00
|
|
|
assert($("#compose-send-status").hasClass("alert-error"));
|
|
|
|
assert(!$("#compose-send-status").hasClass("alert-info"));
|
2017-11-25 22:22:36 +01:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
2018-01-27 03:30:26 +01:00
|
|
|
assert.equal($("#compose-error-msg").text(), msg);
|
2017-11-25 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
function test(err, msg, server_response = null, file = {}) {
|
2017-11-25 22:22:36 +01:00
|
|
|
setup_test();
|
2018-04-14 11:48:22 +02:00
|
|
|
file.lastModified = 1549958107000;
|
2018-02-13 22:39:51 +01:00
|
|
|
upload_opts.error(err, server_response, file);
|
2018-01-27 03:30:26 +01:00
|
|
|
assert_side_effects(msg);
|
2017-11-25 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var msg_prefix = 'translated: ';
|
|
|
|
var msg_1 = 'File upload is not yet available for your browser.';
|
|
|
|
var msg_2 = 'Unable to upload that many files at once.';
|
|
|
|
var msg_3 = '"foobar.txt" was too large; the maximum file size is 25MiB.';
|
|
|
|
var msg_4 = 'Sorry, the file was too large.';
|
2018-01-27 03:30:26 +01:00
|
|
|
var msg_5 = 'An unknown error occurred.';
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2018-01-27 04:00:32 +01:00
|
|
|
test('BrowserNotSupported', msg_prefix + msg_1);
|
|
|
|
test('TooManyFiles', msg_prefix + msg_2);
|
|
|
|
test('FileTooLarge', msg_prefix + msg_3, null, {name: 'foobar.txt'});
|
|
|
|
test(413, msg_prefix + msg_4);
|
|
|
|
test(400, 'ちょっと…', {msg: 'ちょっと…'});
|
|
|
|
test('Do-not-match-any-case', msg_prefix + msg_5);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('upload_finish', () => {
|
2017-11-25 22:22:36 +01:00
|
|
|
function test(i, response, textbox_val) {
|
|
|
|
var compose_ui_autosize_textarea_checked = false;
|
|
|
|
var compose_actions_start_checked = false;
|
2018-08-15 03:57:35 +02:00
|
|
|
var syntax_to_replace;
|
|
|
|
var syntax_to_replace_with;
|
2017-11-28 22:36:58 +01:00
|
|
|
var file_input_clear = false;
|
2017-11-25 22:22:36 +01:00
|
|
|
|
|
|
|
function setup() {
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").val('');
|
2017-11-25 22:22:36 +01:00
|
|
|
compose_ui.autosize_textarea = function () {
|
|
|
|
compose_ui_autosize_textarea_checked = true;
|
|
|
|
};
|
2018-08-15 03:57:35 +02:00
|
|
|
compose_ui.replace_syntax = function (old_syntax, new_syntax) {
|
|
|
|
syntax_to_replace = old_syntax;
|
|
|
|
syntax_to_replace_with = new_syntax;
|
2017-11-02 22:01:50 +01:00
|
|
|
};
|
2017-11-25 22:22:36 +01:00
|
|
|
compose_state.set_message_type();
|
|
|
|
global.compose_actions = {
|
|
|
|
start: function (msg_type) {
|
|
|
|
assert.equal(msg_type, 'stream');
|
|
|
|
compose_actions_start_checked = true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
$("#compose-send-button").attr('disabled', 'disabled');
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").addClass("alert-info");
|
|
|
|
$("#compose-send-status").show();
|
2017-11-28 22:36:58 +01:00
|
|
|
|
2017-11-25 22:22:36 +01:00
|
|
|
$('#file_input').clone = function (param) {
|
|
|
|
assert(param);
|
|
|
|
return $('#file_input');
|
|
|
|
};
|
2018-02-13 23:29:17 +01:00
|
|
|
|
|
|
|
$('#file_input').replaceWith = function (elem) {
|
|
|
|
assert.equal(elem, $('#file_input'));
|
|
|
|
file_input_clear = true;
|
|
|
|
};
|
2018-04-14 11:48:22 +02:00
|
|
|
|
|
|
|
$("#compose-upload-bar-1549958107000").parent = function () {
|
|
|
|
return { remove: function () {$('div.progress.active').length = 0;} };
|
|
|
|
};
|
2017-11-25 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function assert_side_effects() {
|
|
|
|
if (response.uri) {
|
2018-08-15 03:57:35 +02:00
|
|
|
assert.equal(syntax_to_replace, '[Uploading some-file…]()');
|
|
|
|
assert.equal(syntax_to_replace_with, textbox_val);
|
2017-11-25 22:22:36 +01:00
|
|
|
assert(compose_actions_start_checked);
|
|
|
|
assert(compose_ui_autosize_textarea_checked);
|
|
|
|
assert.equal($("#compose-send-button").prop('disabled'), false);
|
2017-11-26 19:58:36 +01:00
|
|
|
assert(!$('#compose-send-status').hasClass('alert-info'));
|
|
|
|
assert(!$('#compose-send-status').visible());
|
2017-11-28 22:36:58 +01:00
|
|
|
assert(file_input_clear);
|
2017-11-25 22:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 04:29:21 +02:00
|
|
|
global.patch_builtin('setTimeout', function (func) {
|
|
|
|
func();
|
|
|
|
});
|
|
|
|
|
2018-04-14 11:48:22 +02:00
|
|
|
$("#compose-upload-bar-1549958107000").width = function (width_percent) {
|
2018-04-05 04:29:21 +02:00
|
|
|
assert.equal(width_percent, '100%');
|
|
|
|
};
|
|
|
|
|
2017-11-25 22:22:36 +01:00
|
|
|
setup();
|
2018-08-15 03:57:35 +02:00
|
|
|
upload_opts.uploadFinished(i, {
|
|
|
|
lastModified: 1549958107000,
|
|
|
|
name: 'some-file',
|
|
|
|
}, response);
|
2018-04-14 11:48:22 +02:00
|
|
|
upload_opts.progressUpdated(1, {lastModified: 1549958107000}, 100);
|
2017-11-25 22:22:36 +01:00
|
|
|
assert_side_effects();
|
|
|
|
}
|
|
|
|
|
2017-11-02 22:01:50 +01:00
|
|
|
var msg_1 = '[pasted image](https://foo.com/uploads/122456)';
|
|
|
|
var msg_2 = '[foobar.jpeg](https://foo.com/user_uploads/foobar.jpeg)';
|
2017-11-25 22:22:36 +01:00
|
|
|
|
|
|
|
test(-1, {}, '');
|
|
|
|
test(-1, {uri: 'https://foo.com/uploads/122456'}, msg_1);
|
|
|
|
test(1, {uri: '/user_uploads/foobar.jpeg'}, msg_2);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|