4 <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
6 <link href="qunit/qunit/qunit.css" rel="stylesheet" type="text/css" media="screen" />
7 <script src="qunit/qunit/qunit.js" type="text/javascript"></script>
9 <script src="../client/fileuploader.js" type="text/javascript" ></script>
16 test("contains", function(){
17 var el1 = document.createElement('div');
18 var el2 = document.createElement('div');
19 var el3 = document.createElement('div');
23 var el4 = document.createElement('div');
25 ok(qq.contains(el1,el1));
26 ok(qq.contains(el1,el2));
27 ok(qq.contains(el1,el3));
28 ok(!qq.contains(el1,el4));
29 ok(!qq.contains(el3,el2));
32 test("hasClass, addClass, removeClass", function(){
33 var element = document.createElement('div');
34 qq.addClass(element, 'some-class');
35 ok(!qq.hasClass(element, 'some'), 'dash in class name');
38 test("children", function(){
39 same(qq.children(document.createElement('div')), [], 'empty');
40 var element = document.createElement('div');
41 element.innerHTML = 'asdasd<div>text</div><span>asdas</span>asdasd';
42 same(qq.children(element).length, 2);
45 test("getByClass", function(){
46 var element = document.createElement('div');
47 element.style.display = 'none';
48 element.innerHTML = '<div class="class"><div class="someclass class"></div></div><span></span><div class="test"></div><div class="class"></div>';
49 document.body.appendChild(element);
51 var outside = document.createElement('div');
52 outside.className = 'outside class';
53 document.body.appendChild(outside);
55 same(qq.getByClass(document, 'class').length, 4, 'in document');
56 same(qq.getByClass(element, 'class').length, 3, 'in test div');
62 test("obj2url", function(){
64 {title:'empty', obj:{}, expect:''},
65 {title:'general json', obj:{a:'b',c:'d',e:'f'}, expect:'a=b&c=d&e=f'},
66 {title:'general json', obj:{a:1,b:2,c:3,d:4}, expect:'a=1&b=2&c=3&d=4'},
67 {title:'general json array', obj:{a:[1,2,3,4]}, expect:'a[0]=1&a[1]=2&a[2]=3&a[3]=4'},
68 {title:'complex json', obj:{a:'b',c:'d',e:['f',{g:'h',i:['j',{k:'l',m:'n'}],
69 o:undefined,p:true,q:false}]},
79 {title:'function', obj:{a:function(){return 'b';}}, expect:'a=b'},
80 {title:'function no return', obj:{a:function(){},undefined:'b'}, expect:'a=undefined'},
81 {title:'null', obj:{a:null}, expect:'a=null'},
82 {title:'prevent double encoding', obj:{a:[1,2,3],'b[]':[4,5,6],'c[d]':[7,8,9]},
83 expect:'a[0]=1&a[1]=2&a[2]=3&'
85 +'c[d][0]=7&c[d][1]=8&c[d][2]=9'},
86 {title:'spaces', obj:{a:'All your base are belong to us'},
87 expect:'a=All+your+base+are+belong+to+us'},
88 {title:'undefined simple', obj:{undefined:undefined}, expect:''},
89 {title:'undefined complex', obj:{undefined:undefined,
90 a:{undefined:undefined},
91 b:[{undefined:'c'},undefined,{d:'e'}]},
92 expect:'b[1]=undefined&b[2][d]=e'},
93 {title:'prefix url no params', obj:{a:'b'},
94 prefix:'http://any.url',
95 expect:'http://any.url?a=b'},
96 {title:'prefix url trailing ?', obj:{a:'b'},
97 prefix:'http://any.url?',
98 expect:'http://any.url?a=b'},
99 {title:'prefix url param', obj:{a:'b'}, prefix:'http://any.url?foo',
100 expect:'http://any.url?foo&a=b'},
101 {title:'prefix url param=value', obj:{a:'b'},
102 prefix:'http://any.url?foo=bar',
103 expect:'http://any.url?foo=bar&a=b'},
104 {title:'prefix url multi param', obj:{a:'b'},
105 prefix:'http://any.url?foo=bar&bla=blub',
106 expect:'http://any.url?foo=bar&bla=blub&a=b'},
107 {title:'prefix url array param', obj:{a:'b',c:'d'},
108 prefix:'http://any.url?foo[0]=bla&foo[1]=blub',
109 expect:'http://any.url?foo[0]=bla&foo[1]=blub&a=b&c=d'}
112 for (var i = 0, l = tests.length; i<l; i++) {
113 //console.log('------------------ obj2url-test'+(i+1)+': '+tests[i].title);
114 //console.log('object: '+tests[i].obj);
115 //console.log('prefix: '+tests[i].prefix);
117 if (tests[i].prefix) {
118 result = qq.obj2url(tests[i].obj, tests[i].prefix);
120 result = qq.obj2url(tests[i].obj);
122 //console.log('result: '+result);
123 same(decodeURIComponent(result), tests[i].expect, tests[i].title);
128 var uploadTimeout = 700,
131 if (qq.UploadHandlerXhr.isSupported()){
132 $('.handlerform').remove();
135 module('qq.UploadHandlerForm');
138 asyncTest("_getIframeContentJSON", function() {
140 setTimeout(start, loadTimeout);
142 var exampleObject = {
143 "example" : "&a<computer networks>, to download means to receive data to a local system from a remote system, or to initiate such a data transfer. Examples of a remote system from which a download might be performed include a webserver, FTP server, email server, or other similar systems. A download can mean either any file that is offered for downloading or that has been downloaded, or the process of receiving such a file.The inverse operation, uploading, can refer to the sending of data from a local system to a remote system such as a server or another client with the intent that the remote system should store a copy of the data being transferred, or the initiation of such a process. The words first came into popular usage among computer users with the increased popularity of Bulletin Board Systems (BBSs), facilitated by the widespread distribution and implementation of dial-up access the in the 1970s",
150 var testedFn = qq.UploadHandlerForm.prototype._getIframeContentJSON;
152 // IE 7,8 doesn't support application-javascript, application-json, text-javascript, text-plain
153 // as a response type, it also doesn't file load event when iframe loads page with 404 header
154 createIframe('iframe-content-tests/somepage.php', function(iframe){
155 same(testedFn(iframe), {}, "Server didn't return valid JSON object");
157 createIframe('iframe-content-tests/text-html.php', function(iframe){
158 same(testedFn(iframe), exampleObject, "text-html");
161 // test larger response (>4k)
162 //http://www.coderholic.com/firefox-4k-xml-node-limit/
163 createIframe('iframe-content-tests/text-html-large.php', function(iframe){
164 same(testedFn(iframe).length, 5000, ">4k");
168 function createIframe(src, onLoad){
169 var iframe = document.createElement('iframe');
170 qq.attach(iframe, 'load', function(){
173 setTimeout(function(){
178 document.body.appendChild(iframe);
182 test("upload, cancel with empty input", function(){
185 var uploadHandlerForm = new qq.UploadHandlerForm({
186 action: 'action-slow-response.php',
187 onComplete: function(id, fileName, response){
188 ok(false, 'onComplete should not run, the request should be cancelled');
192 var input = document.createElement('input');
193 var id = uploadHandlerForm.add(input);
194 uploadHandlerForm.cancel(id);
198 uploadHandlerForm.upload(id);
200 ok(true, "wasn't uploaded after cancelling")
205 asyncTest("upload", function() {
208 var data = {stringOne: 'rtdfghdfhfh',stringTwo: 'dfsgsdfgsdg',stringThree: 'dfsgfhdfhdg'};
211 var uploadHandler = new qq.UploadHandlerForm({
212 action: 'action-handler-test.php',
213 onComplete: function(id, fileName, response){
214 ok(savedId == id, 'proper id in callback');
215 same(fileName, uploadHandler.getName(id), 'getName method');
217 data.fileName = fileName;
218 same(response, data, 'server received file and correct params, filenames match');
222 var input = document.getElementById('testinput1');
223 qq.attach(input, 'change', function(){
224 setTimeout(start, uploadTimeout);
226 savedId = uploadHandler.add(input);
227 ok(savedId != null, 'id returned by add');
229 uploadHandler.upload(savedId, data);
233 asyncTest("cancel", function() {
234 var uploadHandlerForm = new qq.UploadHandlerForm({
235 action: 'action-slow-response.php',
236 onComplete: function(id, fileName, response){
237 ok(false, 'onComplete should not run, the request should be cancelled');
241 var input = document.getElementById('testinput2');
242 qq.attach(input, 'change', function(){
243 var id = uploadHandlerForm.add(input);
244 uploadHandlerForm.upload(id);
245 uploadHandlerForm.cancel(id);
250 test("check that forms and iframes were removed after use", function(){
251 same($('form,iframe').length, 0, 'check that forms and iframes were removed after use');
254 asyncTest('going back', function(){
255 setTimeout(function(){
256 var goBack = confirm("checking that the history wasn't changed, the page will go back to previous now");
258 window.history.back();
261 ok(false, "the page didn't change (fails in Opera)");
268 if (!qq.UploadHandlerXhr.isSupported()){
269 $('.handlerxhr').remove();
272 module('qq.UploadHandlerXhr');
275 asyncTest("upload", function() {
278 var data = {stringOne: 'rtdfghdfhfh',stringTwo: 'dfsgsdfgsdg',stringThree: 'dfsgfhdfhdg'};
279 var onProgressCalled = false;
280 var expectedId, expectedName;
282 var uploadHandler = new qq.UploadHandlerXhr({
283 action: 'action-handler-test.php',
284 onProgress: function(id, fileName, loaded, total){
285 if (!onProgressCalled) {
286 onProgressCalled = true;
288 same(id, expectedId, 'progress event fired with correct id param');
289 same(fileName, expectedName, 'progress event fired with correct fileName param')
290 ok(loaded <= total && total > 0, 'progress event fired with possible loaded and total');
292 same(total, uploadHandler.getSize(id), 'getSize method');
295 onComplete: function(id, fileName, response){
296 same(id, expectedId, 'progress event fired with correct id param');
297 same(fileName, expectedName, 'progress event fired with correct fileName param')
299 data.fileName = fileName;
300 data.qqfile = fileName;
302 same(response, data, 'server received passed params, filenames match');
304 same(fileName, uploadHandler.getName(id), 'getName method');
308 var input = document.getElementById('handlerxhr1');
310 qq.attach(input, 'change', function(){
311 setTimeout(start, uploadTimeout);
313 var id = uploadHandler.add(input.files[0]);
314 ok(id != null, 'id returned by add');
317 expectedName = input.files[0].name || input.files[0].fileName;
319 ok(false, 'false value as a file name used');
322 uploadHandler.upload(id, data);
328 asyncTest("cancel", function() {
329 var uploadHandler = new qq.UploadHandlerXhr({
330 action: 'action-slow-response.php',
331 onComplete: function(id, fileName, response){
332 ok(false, 'onComplete should not run, the request should be cancelled');
336 var input = document.getElementById('handlerxhr2');
338 // input removed because uploading via XHR is not supported
342 qq.attach(input, 'change', function(){
343 var id = uploadHandler.add(input.files[0]);
344 uploadHandler.upload(id);
345 uploadHandler.cancel(id);
356 <h1 id="qunit-header">File uploader tests</h1>
357 <h2 id="qunit-banner"></h2>
358 <h2 id="qunit-userAgent"></h2>
359 <ol id="qunit-tests"></ol>
362 Open this page via https connection, and make sure that loading bar is not acting strange after all tests are run.
363 Back button test fails in Opera.
366 <p>Please select a file for each input below (in order)</p>
368 <p>qq.FileUploader</p>
369 <div id="fileuploader1"></div>
371 <p>qq.UploadHandlerForm</p>
372 <input class="handlerform" id="testinput1" type="file">
373 <input class="handlerform" id="testinput2" type="file">
375 <p>qq.UploadHandlerXhr</p>
376 <input class="handlerxhr" id="handlerxhr1" type="file">
377 <input class="handlerxhr" id="handlerxhr2" type="file">