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