Revert "Export"
[framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.1.0 / tests / jquery.testHelper.js
1 /*
2  * mobile support unit tests
3  */
4
5 (function( $ ) {
6         $.testHelper = {
7                 // This function takes sets of files to load asynchronously. Each set will be loaded after
8                 // the previous set has completed loading. That is, each require and it's dependencies in a
9                 // set will be loaded asynchronously, but each set will be run in serial.
10                 asyncLoad: function( seq ) {
11                         require({
12                                 baseUrl: "../../../js"
13                         });
14
15                         function loadSeq( seq, i ){
16                                 if( !seq[i] ){
17                                         $( document ).ready( function() {
18                                                 var $fixture = $( '#qunit-fixture' );
19                                                 if ( $fixture.length ) {
20                                                         QUnit.config.fixture = $fixture.html();
21                                                 }
22                                                 QUnit.start();
23                                         });
24                                         return;
25                                 }
26
27                                 require( seq[i], function() {
28                                         loadSeq(seq, i + 1);
29                                 });
30                         }
31
32                         // stop qunit from running the tests until everything is in the page
33                         QUnit.config.autostart = false;
34
35                         loadSeq( seq, 0 );
36                 },
37
38                 excludeFileProtocol: function(callback){
39                         var message = "Tests require script reload and cannot be run via file: protocol";
40
41                         if (location.protocol == "file:") {
42                                 test(message, function(){
43                                         ok(false, message);
44                                 });
45                         } else {
46                                 callback();
47                         }
48                 },
49
50                 // TODO prevent test suite loads when the browser doesn't support push state
51                 // and push-state false is defined.
52                 setPushState: function() {
53                         if( $.support.pushState && location.search.indexOf( "push-state" ) >= 0 ) {
54                                 $.support.pushState = false;
55                         }
56                 },
57
58                 reloads: {},
59
60                 reloadModule: function(libName){
61                         var deferred = $.Deferred(),
62                                 context;
63
64                         // where a module loader isn't defined use the old way
65                         if( !window.require ) {
66                                 this.reloadLib( libName );
67                                 deferred.resolve();
68                                 return deferred;
69                         }
70
71                         if(this.reloads[libName] === undefined) {
72                                 this.reloads[libName] = {
73                                         count: 0
74                                 };
75                         }
76
77                         //Clear internal cache of module inside of require
78                         context = require.s.contexts._;
79                         delete context.defined[libName];
80                         delete context.specified[libName];
81                         delete context.loaded[libName];
82                         delete context.urlFetched[require.toUrl(libName + '.js')];
83
84                         require(
85                                 {
86                                         baseUrl: "../../../js"
87                                 }, [libName],
88                                 function() {
89                                         deferred.resolve();
90                                 }
91                         );
92
93                         return deferred;
94                 },
95
96                 reloadLib: function(libName){
97                         if(this.reloads[libName] === undefined) {
98                                 this.reloads[libName] = {
99                                         lib: $("script[src$='" + libName + "']"),
100                                         count: 0
101                                 };
102                         }
103
104                         var lib = this.reloads[libName].lib.clone(),
105                                 src = lib.attr('src');
106
107                         //NOTE append "cache breaker" to force reload
108                         lib.attr('src', src + "?" + this.reloads[libName].count++);
109                         $("body").append(lib);
110                 },
111
112                 rerunQunit: function(){
113                         var self = this;
114                         QUnit.init();
115                         $("script:not([src*='.\/'])").each(function(i, elem){
116                                 var src = elem.src.split("/");
117                                 self.reloadLib(src[src.length - 1]);
118                         });
119                         QUnit.start();
120                 },
121
122                 alterExtend: function(extraExtension){
123                         var extendFn = $.extend;
124
125                         $.extend = function(object, extension){
126                                 // NOTE extend the object as normal
127                                 var result = extendFn.apply(this, arguments);
128
129                                 // NOTE add custom extensions
130                                 result = extendFn(result, extraExtension);
131                                 return result;
132                         };
133                 },
134
135                 hideActivePageWhenComplete: function() {
136                         if( $('#qunit-testresult').length > 0 ) {
137                                 $('.ui-page-active').css('display', 'none');
138                         } else {
139                                 setTimeout($.testHelper.hideActivePageWhenComplete, 500);
140                         }
141                 },
142
143                 openPage: function(hash){
144                         location.href = location.href.split('#')[0] + hash;
145                 },
146
147                 sequence: function(fns, interval){
148                         $.each(fns, function(i, fn){
149                                 setTimeout(fn, i * interval);
150                         });
151                 },
152
153                 pageSequence: function( fns ){
154                         this.eventSequence( "pagechange", fns );
155                 },
156
157                 eventSequence: function( event, fns, timedOut ){
158                         var seq = [];
159                         $.each(fns, function( i, fn ) {
160                                 seq.push( fn );
161                                 if( i !== fns.length - 1) seq.push( event );
162                         });
163
164                         this.eventCascade( seq );
165                 },
166
167                 eventCascade: function( sequence, timedOut ) {
168                         var fn = sequence.shift(),
169                                 event = sequence.shift(),
170                                 self = this;
171
172                         if( fn === undefined ) {
173                                 return;
174                         }
175
176                         if( event ){
177                                 // if a pagechange or defined event is never triggered
178                                 // continue in the sequence to alert possible failures
179                                 var warnTimer = setTimeout(function() {
180                                         self.eventCascade( sequence, true );
181                                 }, 2000);
182
183                                 // bind the recursive call to the event
184                                 $.mobile.pageContainer.one(event, function() {
185                                         clearTimeout( warnTimer );
186
187                                         // Let the current stack unwind before we fire off the next item in the sequence.
188                                         // TODO setTimeout(self.pageSequence, 0, sequence);
189                                         setTimeout(function(){ self.eventCascade(sequence); }, 0);
190                                 });
191                         }
192
193                         // invoke the function which should, in some fashion,
194                         // trigger the next event
195                         fn( timedOut );
196                 },
197
198                 deferredSequence: function(fns) {
199                         var fn = fns.shift(),
200                                 deferred = $.Deferred(),
201                                 self = this, res;
202
203                         if (fn) {
204                                 res = fn();
205                                 if ( res && $.type( res.done ) === "function" ) {
206                                         res.done(function() {
207                                                 self.deferredSequence( fns ).done(function() {
208                                                         deferred.resolve();
209                                                 });
210                                         });
211                                 } else {
212                                         self.deferredSequence( fns ).done(function() {
213                                                 deferred.resolve();
214                                         });
215                                 }
216                         } else {
217                                 deferred.resolve();
218                         }
219                         return deferred;
220                 },
221
222                 decorate: function(opts){
223                         var thisVal = opts.self || window;
224
225                         return function(){
226                                 var returnVal;
227                                 opts.before && opts.before.apply(thisVal, arguments);
228                                 returnVal = opts.fn.apply(thisVal, arguments);
229                                 opts.after && opts.after.apply(thisVal, arguments);
230
231                                 return returnVal;
232                         };
233                 },
234
235                 assertUrlLocation: function( args ) {
236                         var parts = $.mobile.path.parseUrl( location.href ),
237                                 pathnameOnward = location.href.replace( parts.domain, "" );
238
239                         if( $.support.pushState ) {
240                                 same( pathnameOnward, args.hashOrPush || args.push, args.report );
241                         } else {
242                                 same( parts.hash, "#" + (args.hashOrPush || args.hash), args.report );
243                         }
244                 }
245         };
246 })(jQuery);