File: scriptloader.js

package info (click to toggle)
ckeditor 4.16.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 258,804 kB
  • sloc: javascript: 239,590; sh: 184; makefile: 64; python: 37; php: 15; xml: 5
file content (128 lines) | stat: -rw-r--r-- 2,962 bytes parent folder | download | duplicates (3)
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
/* bender-tags: editor */
/* global testVar, testVar1, testVar2, testVar3 */

var tests = {
//		test_loadCode : function()
//		{
//			CKEDITOR.scriptLoader.loadCode( 'var test="Testing!";' );
//
//			/*jsl:ignore
//			assert.areEqual( 'Testing!', test );
//			/*jsl:end
//		},

	'test load': function() {
		var tc = this;

		function callback() {
			tc.resume( function() {
				assert.areSame( 'Test!', testVar );
			} );
		}

		CKEDITOR.scriptLoader.load( '../_assets/sample.js', callback );

		this.wait();
	},

	'test load event handling': function() {
		var tc = this;

		function callback( success ) {
			tc.resume( function( ) {
				assert.isTrue( success );
			} );
		}

		CKEDITOR.scriptLoader.load( '../_assets/sample.js', callback );

		this.wait();
	},

	'test error event handling': function() {
		// Ignore for both IE8 and IE9.
		if ( CKEDITOR.env.ie && CKEDITOR.env.version <= 9 ) {
			assert.ignore();
		}

		var tc = this;

		function callback( success ) {
			tc.resume( function( ) {
				assert.isFalse( success );
			} );
		}

		CKEDITOR.scriptLoader.load( '../_assets/non-existing-sample.js', callback );

		this.wait();
	},

	'test queue': function() {
		var tc = this;

		function testQueue( scripts ) {
			var loaded = [],
				script;

			for ( var i = 0; i < scripts.length; i++ ) {
				// Mark this script as loaded.
				// This should happen before scriptLoader.queue() loads
				// the next pending script. It allows the final load order
				// assertion.
				script = scripts[ i ];

				( function( script ) {
					function callback( success ) {
						loaded.push( script );

						tc.resume( function() {
							assert.areSame( true, success, 'Successful load.' );
							wait();
						} );

						if ( loaded.length == scripts.length ) {
							tc.resume( function() {
								assert.areSame( 'Foo', testVar1, 'Script has been loaded.' );
								assert.areSame( 'Bar', testVar2, 'Script has been loaded.' );
								assert.areSame( 'Bam', testVar3, 'Script has been loaded.' );

								arrayAssert.itemsAreSame( scripts, loaded, 'Scripts loaded in queue order.' );
							} );
						}
					}

					CKEDITOR.scriptLoader.queue( script, callback );
				} )( script );
			}
		}

		testQueue( [
			'../_assets/queue1.js',
			'../_assets/queue2.js',
			'../_assets/queue3.js'
		] );

		this.wait();
	},

	// Script listeners should be removed because it might cause memory leaks (#589).
	'test script listeners removed when loaded': function() {
		CKEDITOR.scriptLoader.load( '../_assets/sample.js', function() {
			resume( function() {
				var script = CKEDITOR.document.findOne( 'script[src="../_assets/sample.js"]' );

				assert.isFalse( !!script.$.onreadystatechange );
				assert.isFalse( !!script.$.onload );
				assert.isFalse( !!script.$.onerror );
			} );
		} );

		wait();
	}
};

// Repeat the queue tests, to be sure that it is not getting stuck.
tests[ 'test queue again' ] = tests[ 'test queue' ];

bender.test( tests );