upload tizen1.0 source
[framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.0.1pre / tests / jquery.testHelper.js
1 /*
2  * mobile support unit tests
3  */
4
5 (function( $ ) {
6         $.testHelper = {
7                 excludeFileProtocol: function(callback){
8                         var message = "Tests require script reload and cannot be run via file: protocol";
9
10                         if (location.protocol == "file:") {
11                                 test(message, function(){
12                                         ok(false, message);
13                                 });
14                         } else {
15                                 callback();
16                         }
17                 },
18
19                 // TODO prevent test suite loads when the browser doesn't support push state
20                 // and push-state false is defined.
21                 setPushStateFor: function( libs ) {
22                         if( $.support.pushState && location.search.indexOf( "push-state" ) >= 0 ) {
23                                 $.support.pushState = false;
24                         }
25
26                         $.each(libs, function(i, l) {
27                                 $( "<script>", { src: l }).appendTo("head");
28                         });
29                 },
30
31                 reloads: {},
32
33                 reloadLib: function(libName){
34                         if(this.reloads[libName] === undefined) {
35                                 this.reloads[libName] = {
36                                         lib: $("script[src$='" + libName + "']"),
37                                         count: 0
38                                 };
39                         }
40
41                         var lib = this.reloads[libName].lib.clone(),
42                                 src = lib.attr('src');
43
44                         //NOTE append "cache breaker" to force reload
45                         lib.attr('src', src + "?" + this.reloads[libName].count++);
46                         $("body").append(lib);
47                 },
48
49                 rerunQunit: function(){
50                         var self = this;
51                         QUnit.init();
52                         $("script:not([src*='.\/'])").each(function(i, elem){
53                                 var src = elem.src.split("/");
54                                 self.reloadLib(src[src.length - 1]);
55                         });
56                         QUnit.start();
57                 },
58
59                 alterExtend: function(extraExtension){
60                         var extendFn = $.extend;
61
62                         $.extend = function(object, extension){
63                                 // NOTE extend the object as normal
64                                 var result = extendFn.apply(this, arguments);
65
66                                 // NOTE add custom extensions
67                                 result = extendFn(result, extraExtension);
68                                 return result;
69                         };
70                 },
71
72                 hideActivePageWhenComplete: function() {
73                         if( $('#qunit-testresult').length > 0 ) {
74                                 $('.ui-page-active').css('display', 'none');
75                         } else {
76                                 setTimeout($.testHelper.hideActivePageWhenComplete, 500);
77                         }
78                 },
79
80                 openPage: function(hash){
81                         location.href = location.href.split('#')[0] + hash;
82                 },
83
84                 sequence: function(fns, interval){
85                         $.each(fns, function(i, fn){
86                                 setTimeout(fn, i * interval);
87                         });
88                 },
89
90                 pageSequence: function(fns){
91                         this.eventSequence("pagechange", fns);
92                 },
93
94                 eventSequence: function(event, fns, timedOut){
95                         var fn = fns.shift(),
96                                         self = this;
97
98                         if( fn === undefined ) return;
99
100                         // if a pagechange or defined event is never triggered
101                         // continue in the sequence to alert possible failures
102                         var warnTimer = setTimeout(function(){
103                                 self.eventSequence(event, fns, true);
104                         }, 2000);
105
106                         // bind the recursive call to the event
107                         $.mobile.pageContainer.one(event, function(){
108                                 clearTimeout(warnTimer);
109
110                                 // Let the current stack unwind before we fire off the next item in the sequence.
111                                 // TODO setTimeout(self.pageSequence, 0, [fns, event]);
112                                 setTimeout(function(){ self.eventSequence(event, fns); }, 0);
113                         });
114
115                         // invoke the function which should, in some fashion,
116                         // trigger the defined event
117                         fn(timedOut);
118                 },
119
120                 decorate: function(opts){
121                         var thisVal = opts.self || window;
122
123                         return function(){
124                                 var returnVal;
125                                 opts.before && opts.before.apply(thisVal, arguments);
126                                 returnVal = opts.fn.apply(thisVal, arguments);
127                                 opts.after && opts.after.apply(thisVal, arguments);
128
129                                 return returnVal;
130                         };
131                 },
132
133                 assertUrlLocation: function( args ) {
134                         var parts = $.mobile.path.parseUrl( location.href ),
135                                 pathnameOnward = location.href.replace( parts.domain, "" );
136
137                         if( $.support.pushState ) {
138                                 same( pathnameOnward, args.hashOrPush || args.push, args.report );
139                         } else {
140                                 same( parts.hash, "#" + (args.hashOrPush || args.hash), args.report );
141                         }
142                 }
143         };
144 })(jQuery);