plupload
Version:
Plupload is a JavaScript API for dealing with file uploads it supports features like multiple file selection, file type filtering, request chunking, client side image scaling and it uses different runtimes to achieve this such as HTML 5, Silverlight and F
419 lines (331 loc) • 8.86 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Plupload: Test Uploader Initialization</title>
<script src="../loader.js"></script>
<script type="text/javascript">
QUnit.config.reorder = false;
QUnit.config.autostart = false;
QUnit.config.testTimeout = 10000;
FileHash.init({
files: ['files/01.min.js', 'files/02.min.js'],
onready: function() {
QUnit.start();
}
});
o.Env.swf_url = "../../js/Moxie.swf";
o.Env.xap_url = "../../js/Moxie.xap";
module("plupload.Uploader", {
setup: function() {
$('#qunit-fixture').html('<div id="uploader" />');
this.options = {
browse_button: 'uploader',
drop_element: 'uploader',
container: 'qunit-fixture',
url: "Plupload/upload.php"
};
this.up = null;
},
teardown: function() {
if (this.up) {
this.up.destroy();
}
}
});
// test required options
test("'url' is not a required option anymore so uploader shouldn't trigger plupload.INIT_ERROR", function() {
expect(1);
delete this.options['url'];
this.up = new plupload.Uploader(this.options);
this.up.bind("Init", function() {
start();
ok(true, "'url' option is not required.");
});
this.up.bind("Error", function(up, err) {
start();
ok(false, "'url' option is not required.");
});
stop();
this.up.init();
});
test("If both 'browse_button' and 'drop_element' are missing, uploader should trigger plupload.INIT_ERROR", function() {
expect(1);
delete this.options['browse_button'];
delete this.options['drop_element'];
this.up = new plupload.Uploader(this.options);
this.up.bind("Init", function() {
start();
ok(false,
"One must specify either 'browse_button' or 'drop_element'.");
});
this.up.bind("Error", function(up, err) {
start();
equal(err.code, plupload.INIT_ERROR,
"One must specify either 'browse_button' or 'drop_element'.");
});
stop();
this.up.init();
});
// test normalized options after instantiation
test("Normalize required_features when passed as comma-delimited string", function() {
expect(1);
this.up = new plupload.Uploader(o.extend(this.options, {
required_features: 'chunks,jpgresize, progress,dragdrop,multi_selection,triggerDialog,display_media'
}));
deepEqual(this.up.settings.required_features, {
slice_blob: true,
send_binary_string: true,
report_upload_progress: true,
select_multiple: true,
summon_file_dialog: true,
drag_and_drop: true,
display_media: true
}, "required_features properly normalized and translated to caps.");
});
test("Normalize required_features when passed as object", function() {
expect(1);
this.up = new plupload.Uploader(o.extend(this.options, {
required_features: {
chunks: true,
jpgresize: true,
progress: true,
dragdrop: true,
multi_selection: true,
triggerDialog: true,
display_media: true
}
}));
deepEqual(this.up.settings.required_features, {
slice_blob: true,
send_binary_string: true,
report_upload_progress: true,
select_multiple: true,
summon_file_dialog: true,
drag_and_drop: true,
display_media: true
}, "required_features properly normalized and translated to caps.");
});
test("Consider special settings as required_features when required_features is boolean true", function() {
this.up = new plupload.Uploader(o.extend(this.options, {
multi_selection: true,
drop_element: 'uploader',
multipart: false,
chunk_size: '1mb',
max_file_size: '1gb',
resize: {
width: 100,
height: 60
},
headers: {
'X-Test': 1
},
required_features: true
}));
deepEqual(this.up.settings.required_features, {
select_multiple: true,
drag_and_drop: true,
slice_blob: true,
send_binary_string: true,
send_custom_headers: true,
use_http_method: "POST"
}, "required_features properly normalized and translated to caps.");
});
test("When required_features is undefined, consider special settings as optional", function() {
this.up = new plupload.Uploader(o.extend(this.options, {
multi_selection: true,
drop_element: 'uploader',
max_file_size: '1gb',
resize: {
width: 100,
height: 60
},
headers: {
'X-Test': 1
}
}));
deepEqual(this.up.settings.required_features, {}, "required_features must be empty.");
});
test("Size identifiers in string format (e.g. 1mb) converted to numerical format", function() {
this.up = new plupload.Uploader(o.extend(this.options, {
chunk_size: '1mb',
max_file_size: '1gb'
}));
equal(this.up.settings.chunk_size, 1024 * 1024, "'1mb' converted to numerical bytes");
});
test("Test how default values are assigned to missing options", function() {
this.up = new plupload.Uploader(this.options);
deepEqual(this.up.settings, {
browse_button: plupload.getAll('uploader'),
drop_element: plupload.getAll('uploader'),
container: plupload.get('qunit-fixture'),
url: "Plupload/upload.php",
runtimes: o.Runtime.order,
http_method: "POST",
max_retries: 0,
multipart : true,
send_file_name : true,
multi_selection : true,
file_data_name : 'file',
flash_swf_url : 'js/Moxie.swf',
silverlight_xap_url : 'js/Moxie.xap',
filters : {
prevent_duplicates: false,
prevent_empty: true,
max_file_size: 0,
mime_types: []
},
resize: false,
chunk_size: 0,
required_features: {},
send_chunk_number: true
}, "Proper defaults set.");
});
test("Test how defaults are set for resize object", function() {
this.up = new plupload.Uploader(o.extend(this.options, {
resize: {
width: 100,
height: 60
}
}));
deepEqual(this.up.settings.resize, {
width: 100,
height: 60,
crop: false,
preserve_headers: true
}, "Proper defaults set for resize property.");
});
// test init event with restored runtime specifier
test("Test runtime property", function() {
expect(2);
this.up = new plupload.Uploader(o.extend(this.options, {
runtimes: 'html4,test',
required_features: 'multi_selection'
}));
this.up.bind('Init', function(up, info) {
equal(info.runtime, 'test', "Test second argument for Init handler.");
equal(up.runtime, 'test', "Test uploader's 'runtime' property.");
});
this.up.bind('PostInit', function() {
start();
});
stop();
this.up.init();
});
// test preinit and init
test("Test preinit and init callbacks", function() {
var eventOrder = [];
this.up = new plupload.Uploader(o.extend(this.options, {
preinit: function() {
eventOrder.push(1);
},
init: function() {
start();
eventOrder.push(2);
}
}));
this.up.bind('Init', function() {
eventOrder.push(3);
deepEqual(eventOrder, [1, 2, 3], "preinit callback runs first, then init callback and finally Init handler.");
});
stop();
this.up.init();
});
test("Test preinit and init set of event handlers", function() {
var eventOrder = [];
this.up = new plupload.Uploader(o.extend(this.options, {
preinit: {
PostInit: function() {
eventOrder.push(1);
}
},
init: {
PostInit: function() {
eventOrder.push(2);
start();
deepEqual(eventOrder, [1, 2], "preinit handlers run first and and then init handlers.");
}
}
}));
stop();
this.up.init();
});
module("up.features must be set for backward compatibility", {
setup: function() {
$('#qunit-fixture').html('<div id="uploader" />');
this.options = {
runtimes: 'test',
browse_button: 'uploader',
drop_element: 'uploader',
container: 'qunit-fixture',
url: "Plupload/upload.php",
init: {
Error: function(up) {
start();
ok(false, "Runtime failed to initialise.");
}
}
};
this.up = null;
},
teardown: function() {
if (this.up) {
this.up.destroy();
}
}
});
test("with 'drop_element' only", function() {
delete this.options.browse_button;
var up = new plupload.Uploader(this.options);
up.bind('PostInit', function(up) {
start();
deepEqual(up.features, {
dragdrop: true,
chunks: true,
multipart: true
},
"up.features properly set for backward compatibility."
);
});
stop();
up.init();
});
test("with 'browse_button' only", function() {
delete this.options.drop_element;
var up = new plupload.Uploader(this.options);
up.bind('PostInit', function(up) {
start();
deepEqual(up.features, {
multi_selection: true,
chunks: true,
multipart: true
},
"up.features properly set for backward compatibility."
);
});
stop();
up.init();
});
test("with both", function() {
var up = new plupload.Uploader(this.options);
up.bind('PostInit', function(up) {
start();
deepEqual(up.features, {
dragdrop: true,
chunks: true,
multipart: true,
multi_selection: true
},
"up.features properly set for backward compatibility."
);
});
stop();
up.init();
});
</script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture" style="position: relative; top: 0 !important; left: 0 !important; width: 100%; height: 9px;"></div>
</body>
</html>