Revert "Export"
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.1.0 / tests / unit / init / init_core.js
1 /*
2  * mobile init tests
3  */
4 (function($){
5         var mobilePage = undefined,
6                         libName = 'jquery.mobile.init.js',
7                         coreLib = 'jquery.mobile.core.js',
8                         extendFn = $.extend,
9                         originalLoadingMessage = $.mobile.loadingMessage,
10                         setGradeA = function(value) { $.mobile.gradeA = function(){ return value; }; },
11                         reloadCoreNSandInit = function(){
12                                 $.testHelper.reloadLib(coreLib);
13                                 $.testHelper.reloadLib("jquery.setNamespace.js");
14                                 $.testHelper.reloadLib(libName);
15                         };
16
17
18         module(libName, {
19                 setup: function(){
20                         // NOTE reset for gradeA tests
21                         $('html').removeClass('ui-mobile');
22
23                         // TODO add post reload callback
24                         $('.ui-loader').remove();
25                 },
26                 teardown: function(){
27                         $.extend = extendFn;
28
29                         // NOTE reset for showPageLoadingMsg/hidePageLoadingMsg tests
30                         $('.ui-loader').remove();
31
32                         // clear the classes added by reloading the init
33                         $("html").attr('class', '');
34
35                         $.mobile.loadingMessage = originalLoadingMessage;
36                 }
37         });
38
39         // NOTE important to use $.fn.one here to make sure library reloads don't fire
40         //      the event before the test check below
41         $(document).one("mobileinit", function(){
42                 mobilePage = $.mobile.page;
43         });
44
45         // NOTE for the following two tests see index html for the binding
46         test( "mobile.page is available when mobile init is fired", function(){
47                 ok( mobilePage !== undefined, "$.mobile.page is defined" );
48         });
49
50         $.testHelper.excludeFileProtocol(function(){
51                 asyncTest( "loading the init library triggers mobilinit on the document", function(){
52                         var initFired = false;
53                         expect( 1 );
54
55                         $(window.document).one('mobileinit', function(event){
56                                 initFired = true;
57                         });
58
59                         $.testHelper.reloadLib(libName);
60
61                         setTimeout(function(){
62                                 ok(initFired, "init fired");
63                                 start();
64                         }, 1000);
65                 });
66
67                 test( "enhancments are skipped when the browser is not grade A", function(){
68                         setGradeA(false);
69                         $.testHelper.reloadLib(libName);
70
71                         //NOTE easiest way to check for enhancements, not the most obvious
72                         ok(!$("html").hasClass("ui-mobile"), "html elem doesn't have class ui-mobile");
73                 });
74
75                 test( "enhancments are added when the browser is grade A", function(){
76                         setGradeA(true);
77                         $.testHelper.reloadLib(libName);
78
79                         ok($("html").hasClass("ui-mobile"), "html elem has class mobile");
80                 });
81
82                 asyncTest( "useFastClick is configurable via mobileinit", function(){
83                         $(document).one( "mobileinit", function(){
84                                 $.mobile.useFastClick = false;
85                                 start();
86                         });
87
88                         $.testHelper.reloadLib(libName);
89
90                         same( $.mobile.useFastClick, false , "fast click is set to false after init" );
91                         $.mobile.useFastClick = true;
92                 });
93
94
95
96                 var findFirstPage = function() {
97                         return $(":jqmData(role='page')").first();
98                 };
99
100                 test( "active page and start page should be set to the fist page in the selected set", function(){
101                         expect( 2 );
102                         $.testHelper.reloadLib(libName);
103                         var firstPage = findFirstPage();
104
105                         same($.mobile.firstPage[0], firstPage[0]);
106                         same($.mobile.activePage[0], firstPage[0]);
107                 });
108
109                 test( "mobile viewport class is defined on the first page's parent", function(){
110                         expect( 1 );
111                         $.testHelper.reloadLib(libName);
112                         var firstPage = findFirstPage();
113
114                         ok(firstPage.parent().hasClass("ui-mobile-viewport"), "first page has viewport");
115                 });
116
117                 test( "mobile page container is the first page's parent", function(){
118                         expect( 1 );
119                         $.testHelper.reloadLib(libName);
120                         var firstPage = findFirstPage();
121
122                         same($.mobile.pageContainer[0], firstPage.parent()[0]);
123                 });
124
125                 asyncTest( "hashchange triggered on document ready with single argument: true", function(){
126                         $.testHelper.sequence([
127                                 function(){
128                                         location.hash = "#foo";
129                                 },
130
131                                 // delay the bind until the first hashchange
132                                 function(){
133                                         $(window).one("hashchange", function(ev, arg){
134                                                 same(arg, true);
135                                                 start();
136                                         });
137                                 },
138
139                                 function(){
140                                         $.testHelper.reloadLib(libName);
141                                 }
142                         ], 1000);
143                 });
144
145                 test( "pages without a data-url attribute have it set to their id", function(){
146                         same($("#foo").jqmData('url'), "foo");
147                 });
148
149                 test( "pages with a data-url attribute are left with the original value", function(){
150                         same($("#bar").jqmData('url'), "bak");
151                 });
152
153                 asyncTest( "showPageLoadingMsg doesn't add the dialog to the page when loading message is false", function(){
154                         expect( 1 );
155                         $.mobile.loadingMessage = false;
156                         $.mobile.showPageLoadingMsg();
157
158                         setTimeout(function(){
159                                 ok(!$(".ui-loader").length, "no ui-loader element");
160                                 start();
161                         }, 500);
162                 });
163
164                 asyncTest( "hidePageLoadingMsg doesn't add the dialog to the page when loading message is false", function(){
165                         expect( 1 );
166                         $.mobile.loadingMessage = true;
167                         $.mobile.hidePageLoadingMsg();
168
169                         setTimeout(function(){
170                                 same($(".ui-loading").length, 0, "page should not be in the loading state");
171                                 start();
172                         }, 500);
173                 });
174
175                 asyncTest( "showPageLoadingMsg adds the dialog to the page when loadingMessage is true", function(){
176                         expect( 1 );
177                         $.mobile.loadingMessage = true;
178                         $.mobile.showPageLoadingMsg();
179
180                         setTimeout(function(){
181                                 same($(".ui-loading").length, 1, "page should be in the loading state");
182                                 start();
183                         }, 500);
184                 });
185
186                 asyncTest( "page loading should contain default loading message", function(){
187                         expect( 1 );
188                         reloadCoreNSandInit();
189                         $.mobile.showPageLoadingMsg();
190
191                         setTimeout(function(){
192                                 same($(".ui-loader h1").text(), "loading");
193                                 start();
194                         }, 500);
195                 });
196
197                 asyncTest( "page loading should contain custom loading message", function(){
198                         $.mobile.loadingMessage = "foo";
199                         $.testHelper.reloadLib(libName);
200                         $.mobile.showPageLoadingMsg();
201
202                         setTimeout(function(){
203                                 same($(".ui-loader h1").text(), "foo");
204                                 start();
205                         }, 500);
206                 });
207
208                 asyncTest( "page loading should contain custom loading message when set during runtime", function(){
209                         $.mobile.loadingMessage = "bar";
210                         $.mobile.showPageLoadingMsg();
211
212                         setTimeout(function(){
213                                 same($(".ui-loader h1").text(), "bar");
214                                 start();
215                         }, 500);
216                 });
217
218
219
220                 // NOTE: the next two tests work on timeouts that assume a page will be created within 2 seconds
221                 // it'd be great to get these using a more reliable callback or event
222
223                 asyncTest( "page does auto-initialize at domready when autoinitialize option is true (default) ", function(){
224
225                         $( "<div />", { "data-nstest-role": "page", "id": "autoinit-on" } ).prependTo( "body" )
226
227                         $(document).one("mobileinit", function(){
228                                 $.mobile.autoInitializePage = true;
229                         });
230
231                         location.hash = "";
232
233                         reloadCoreNSandInit();
234
235                         setTimeout(function(){
236                                 same( $( "#autoinit-on.ui-page" ).length, 1 );
237
238                                 start();
239                         }, 2000);
240                 });
241
242
243                 asyncTest( "page does not initialize at domready when autoinitialize option is false ", function(){
244                         $(document).one("mobileinit", function(){
245                                 $.mobile.autoInitializePage = false;
246                         });
247
248                         $( "<div />", { "data-nstest-role": "page", "id": "autoinit-off" } ).prependTo( "body" )
249
250                         location.hash = "";
251
252
253                         reloadCoreNSandInit();
254
255                         setTimeout(function(){
256                                 same( $( "#autoinit-off.ui-page" ).length, 0 );
257
258                                 $(document).bind("mobileinit", function(){
259                                         $.mobile.autoInitializePage = true;
260                                 });
261
262                                 reloadCoreNSandInit();
263
264                                 start();
265                         }, 2000);
266                 });
267
268
269
270         });
271 })(jQuery);