aboutsummaryrefslogtreecommitdiffstats
path: root/addon/js_upload/file-uploader/tests/test-upload-handlers.htm
blob: 9cf74fe7ed542445f0c78478cf4da6ca9dbc4f0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<!DOCTYPE HTML>
<html>
<head>	
	<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
	
	<link href="qunit/qunit/qunit.css" rel="stylesheet" type="text/css" media="screen" />
	<script src="qunit/qunit/qunit.js" type="text/javascript"></script>
	
	<script src="../client/fileuploader.js" type="text/javascript" ></script>
	<script>

jQuery(function(){

    module('qq');

    test("contains", function(){
        var el1 = document.createElement('div');
        var el2 = document.createElement('div');
        var el3 = document.createElement('div');
        el1.appendChild(el2);
        el2.appendChild(el3);
        
        var el4 = document.createElement('div');
        
        ok(qq.contains(el1,el1));
        ok(qq.contains(el1,el2));
        ok(qq.contains(el1,el3));
        ok(!qq.contains(el1,el4));
        ok(!qq.contains(el3,el2));
    }); 
        
    test("hasClass, addClass, removeClass", function(){
        var element = document.createElement('div');
        qq.addClass(element, 'some-class');
        ok(!qq.hasClass(element, 'some'), 'dash in class name');
    });

    test("children", function(){
        same(qq.children(document.createElement('div')), [], 'empty');
        var element = document.createElement('div');
        element.innerHTML = 'asdasd<div>text</div><span>asdas</span>asdasd';
        same(qq.children(element).length, 2);
    });
        
    test("getByClass", function(){
        var element = document.createElement('div');
        element.style.display = 'none';
        element.innerHTML = '<div class="class"><div class="someclass class"></div></div><span></span><div class="test"></div><div class="class"></div>';
        document.body.appendChild(element);
        
        var outside = document.createElement('div');
        outside.className = 'outside class';
        document.body.appendChild(outside);
        
        same(qq.getByClass(document, 'class').length, 4, 'in document');
        same(qq.getByClass(element, 'class').length, 3, 'in test div');
        
        qq.remove(element);
        qq.remove(outside);     
    }); 

    test("obj2url", function(){
        var tests = [
          {title:'empty',                   obj:{}, expect:''},
          {title:'general json',            obj:{a:'b',c:'d',e:'f'}, expect:'a=b&c=d&e=f'},
          {title:'general json',            obj:{a:1,b:2,c:3,d:4}, expect:'a=1&b=2&c=3&d=4'},
          {title:'general json array',      obj:{a:[1,2,3,4]}, expect:'a[0]=1&a[1]=2&a[2]=3&a[3]=4'},
          {title:'complex json',            obj:{a:'b',c:'d',e:['f',{g:'h',i:['j',{k:'l',m:'n'}],
                                                 o:undefined,p:true,q:false}]}, 
                                            expect:'a=b&c=d&'
                                                  +'e[0]=f&'
                                                  +'e[1][g]=h&'
                                                  +'e[1][i][0]=j&'
                                                  +'e[1][i][1][k]=l&'
                                                  +'e[1][i][1][m]=n&'
                                                  +'e[1][o]=undefined&'
                                                  +'e[1][p]=true&'
                                                  +'e[1][q]=false'},
          {title:'function',                obj:{a:function(){return 'b';}}, expect:'a=b'},
          {title:'function no return',      obj:{a:function(){},undefined:'b'}, expect:'a=undefined'},
          {title:'null',                    obj:{a:null}, expect:'a=null'},
          {title:'prevent double encoding', obj:{a:[1,2,3],'b[]':[4,5,6],'c[d]':[7,8,9]}, 
                                            expect:'a[0]=1&a[1]=2&a[2]=3&'
                                                  +'b[]=4&b[]=5&b[]=6&'
                                                  +'c[d][0]=7&c[d][1]=8&c[d][2]=9'},
          {title:'spaces',                  obj:{a:'All your base are belong to us'}, 
                                            expect:'a=All+your+base+are+belong+to+us'},
          {title:'undefined simple',        obj:{undefined:undefined}, expect:''},
          {title:'undefined complex',       obj:{undefined:undefined,
                                                 a:{undefined:undefined},
                                                 b:[{undefined:'c'},undefined,{d:'e'}]}, 
                                            expect:'b[1]=undefined&b[2][d]=e'},
          {title:'prefix url no params',    obj:{a:'b'}, 
                                            prefix:'http://any.url', 
                                            expect:'http://any.url?a=b'},
          {title:'prefix url trailing ?',   obj:{a:'b'}, 
                                            prefix:'http://any.url?', 
                                            expect:'http://any.url?a=b'},
          {title:'prefix url param',        obj:{a:'b'}, prefix:'http://any.url?foo', 
                                            expect:'http://any.url?foo&a=b'},
          {title:'prefix url param=value',  obj:{a:'b'}, 
                                            prefix:'http://any.url?foo=bar', 
                                            expect:'http://any.url?foo=bar&a=b'},
          {title:'prefix url multi param',  obj:{a:'b'}, 
                                            prefix:'http://any.url?foo=bar&bla=blub', 
                                            expect:'http://any.url?foo=bar&bla=blub&a=b'},
          {title:'prefix url array param',  obj:{a:'b',c:'d'}, 
                                            prefix:'http://any.url?foo[0]=bla&foo[1]=blub', 
                                            expect:'http://any.url?foo[0]=bla&foo[1]=blub&a=b&c=d'} 
        ];
        
        for (var i = 0, l = tests.length; i<l; i++) {
          //console.log('------------------ obj2url-test'+(i+1)+': '+tests[i].title);
          //console.log('object: '+tests[i].obj);
          //console.log('prefix: '+tests[i].prefix); 
          var result = '';
          if (tests[i].prefix) {
            result = qq.obj2url(tests[i].obj, tests[i].prefix);
          } else {
            result = qq.obj2url(tests[i].obj);
          }
          //console.log('result: '+result);
          same(decodeURIComponent(result), tests[i].expect, tests[i].title);
        }
    }); 
    
    	
	var uploadTimeout = 700,
		loadTimeout = 300;
	
    if (qq.UploadHandlerXhr.isSupported()){
        $('.handlerform').remove();    
    } else {            	
    	//
    	module('qq.UploadHandlerForm');
    	//
    	
    	asyncTest("_getIframeContentJSON", function() {						
    		expect(3);
    		setTimeout(start, loadTimeout);		
    		
    		var exampleObject = {
    		  "example" : "&amp;a&lt;computer networks&gt;, 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",
    		  "sub" : {
    		    "arr": [10,20,30],
    		    "boo": false    
    		  }
    		};
    	
    		var testedFn = qq.UploadHandlerForm.prototype._getIframeContentJSON;		
    		
    		// IE 7,8 doesn't support application-javascript, application-json, text-javascript, text-plain
    		// as a response type, it also doesn't file load event when iframe loads page with 404 header		
    		createIframe('iframe-content-tests/somepage.php', function(iframe){
    			same(testedFn(iframe), {}, "Server didn't return valid JSON object");
    		});		
    		createIframe('iframe-content-tests/text-html.php', function(iframe){
    			same(testedFn(iframe), exampleObject, "text-html");
    		});			
    		
    		// test larger response (>4k)
    		//http://www.coderholic.com/firefox-4k-xml-node-limit/
            createIframe('iframe-content-tests/text-html-large.php', function(iframe){
                same(testedFn(iframe).length, 5000, ">4k");
            });         
    				
    		
    		function createIframe(src, onLoad){
    			var iframe = document.createElement('iframe');			
    			qq.attach(iframe, 'load', function(){
    				onLoad(iframe);
    				
    				setTimeout(function(){
    					qq.remove(iframe);	
    				}, 1);						
    			});
    			iframe.src = src;
    			document.body.appendChild(iframe);				
    		}		
    	});
    
        test("upload, cancel with empty input", function(){
            expect(1);
    
            var uploadHandlerForm = new qq.UploadHandlerForm({
                action: 'action-slow-response.php',
                onComplete: function(id, fileName, response){
                    ok(false, 'onComplete should not run, the request should be cancelled');
                }
            });
                
            var input = document.createElement('input');
            var id = uploadHandlerForm.add(input);        
            uploadHandlerForm.cancel(id);
            
            try {
                // should fail
                uploadHandlerForm.upload(id);
            } catch (err){
                ok(true, "wasn't uploaded after cancelling")            
            };        
            
        });
        
    	asyncTest("upload", function() {				
    		expect(4);		
    				
    		var data = {stringOne: 'rtdfghdfhfh',stringTwo: 'dfsgsdfgsdg',stringThree: 'dfsgfhdfhdg'};
    		var savedId;
    												
    		var uploadHandler = new qq.UploadHandlerForm({
    			action: 'action-handler-test.php',
    			onComplete: function(id, fileName, response){		    			    			    			    
    			    ok(savedId == id, 'proper id in callback');			    
    			    same(fileName, uploadHandler.getName(id), 'getName method');
    			    
    			    data.fileName = fileName;			    			
    				same(response, data, 'server received file and correct params, filenames match');
    			}
    		});
    			
    		var input = document.getElementById('testinput1');
    		qq.attach(input, 'change', function(){
    			setTimeout(start, uploadTimeout);
    										
    			savedId = uploadHandler.add(input);			
    			ok(savedId != null, 'id returned by add');
    			
    			uploadHandler.upload(savedId, data);
    		});
    	});
    	
    	asyncTest("cancel", function() {				
    		var uploadHandlerForm = new qq.UploadHandlerForm({
    			action: 'action-slow-response.php',
    			onComplete: function(id, fileName, response){
    				ok(false, 'onComplete should not run, the request should be cancelled');
    			}
    		});
    			
    		var input = document.getElementById('testinput2');
    		qq.attach(input, 'change', function(){
    			var id = uploadHandlerForm.add(input);
    			uploadHandlerForm.upload(id);
    			uploadHandlerForm.cancel(id);
    			start();
    		});
    	});	
    			
    	test("check that forms and iframes were removed after use", function(){
    		same($('form,iframe').length, 0, 'check that forms and iframes were removed after use');
    	});
    	
        asyncTest('going back', function(){
            setTimeout(function(){
                var goBack = confirm("checking that the history wasn't changed, the page will go back to previous now");
                if (goBack){
                   window.history.back();
                   
                   start();
                   ok(false, "the page didn't change (fails in Opera)");
                }              
            }, 1000);               
        });    	
    	
	}
	
    if (!qq.UploadHandlerXhr.isSupported()){
        $('.handlerxhr').remove();    
    } else {
        //      
        module('qq.UploadHandlerXhr');
        //
    	
    	asyncTest("upload", function() {		
    	    expect(9);
    	    	
    		var data = {stringOne: 'rtdfghdfhfh',stringTwo: 'dfsgsdfgsdg',stringThree: 'dfsgfhdfhdg'};
    		var onProgressCalled = false;
    		var expectedId, expectedName;
    														
    		var uploadHandler = new qq.UploadHandlerXhr({
    			action: 'action-handler-test.php',
    			onProgress: function(id, fileName, loaded, total){
    				if (!onProgressCalled) {
    					onProgressCalled = true;
    					
    					same(id, expectedId, 'progress event fired with correct id param');
    					same(fileName, expectedName, 'progress event fired with correct fileName param')					
    					ok(loaded <= total && total > 0, 'progress event fired with possible loaded and total');
    					
    					same(total, uploadHandler.getSize(id), 'getSize method');					
    				}
    			},
    			onComplete: function(id, fileName, response){    			    
                    same(id, expectedId, 'progress event fired with correct id param');
                    same(fileName, expectedName, 'progress event fired with correct fileName param')
                                    				
    				data.fileName = fileName;
                    data.qqfile = fileName;

    				same(response, data, 'server received passed params, filenames match');
    				
    				same(fileName, uploadHandler.getName(id), 'getName method');    				 
    			}
    		});
    			
    		var input = document.getElementById('handlerxhr1');
    		
    		qq.attach(input, 'change', function(){
    			setTimeout(start, uploadTimeout);
    										
    			var id = uploadHandler.add(input.files[0]);
    			ok(id != null, 'id returned by add');
    			
    			expectedId = id;
    			expectedName = input.files[0].name || input.files[0].fileName;    			
    			if (!expectedName){
    			    ok(false, 'false value as a file name used');
    			}
    			
    			uploadHandler.upload(id, data);
    			
    			qq.remove(input);			
    		});
    	});	    
        	
    	asyncTest("cancel", function() {						
    		var uploadHandler = new qq.UploadHandlerXhr({
    			action: 'action-slow-response.php',
    			onComplete: function(id, fileName, response){
    				ok(false, 'onComplete should not run, the request should be cancelled');
    			}
    		});
    			
    		var input = document.getElementById('handlerxhr2');
    		if (!input){
    			// input removed because uploading via XHR is not supported
    			return;
    		}
    		
    		qq.attach(input, 'change', function(){						
    			var id = uploadHandler.add(input.files[0]);
    			uploadHandler.upload(id);
    			uploadHandler.cancel(id);
    			
    			start();			
    			qq.remove(input);								
    		});
    	});		
	}	
});
	</script>  
</head>
<body> 
	<h1 id="qunit-header">File uploader tests</h1> 
	<h2 id="qunit-banner"></h2> 
	<h2 id="qunit-userAgent"></h2> 
	<ol id="qunit-tests"></ol>
	
	<p>
	    Open this page via https connection, and make sure that loading bar is not acting strange after all tests are run.
	    Back button test fails in Opera. 
	</p>
	
	<p>Please select a file for each input below (in order)</p>

    <p>qq.FileUploader</p>
    <div id="fileuploader1"></div>
    	
	<p>qq.UploadHandlerForm</p>
	<input class="handlerform" id="testinput1" type="file">
	<input class="handlerform" id="testinput2" type="file">

	<p>qq.UploadHandlerXhr</p>	
	<input class="handlerxhr" id="handlerxhr1" type="file">
	<input class="handlerxhr" id="handlerxhr2" type="file">	 
		
</body> 
</html>