5 var mobilePage = undefined,
6 libName = 'jquery.mobile.init.js',
7 coreLib = 'jquery.mobile.core.js',
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);
19 // NOTE reset for gradeA tests
20 $('html').removeClass('ui-mobile');
22 // TODO add post reload callback
23 $('.ui-loader').remove();
28 // NOTE reset for showPageLoadingMsg/hidePageLoadingMsg tests
29 $('.ui-loader').remove();
31 // clear the classes added by reloading the init
32 $("html").attr('class', '');
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;
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" );
47 $.testHelper.excludeFileProtocol(function(){
48 asyncTest( "loading the init library triggers mobilinit on the document", function(){
49 var initFired = false;
52 $(window.document).one('mobileinit', function(event){
56 $.testHelper.reloadLib(libName);
58 setTimeout(function(){
59 ok(initFired, "init fired");
64 test( "enhancments are skipped when the browser is not grade A", function(){
66 $.testHelper.reloadLib(libName);
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");
72 test( "enhancments are added when the browser is grade A", function(){
74 $.testHelper.reloadLib(libName);
76 ok($("html").hasClass("ui-mobile"), "html elem has class mobile");
79 asyncTest( "useFastClick is configurable via mobileinit", function(){
80 $(document).one( "mobileinit", function(){
81 $.mobile.useFastClick = false;
85 $.testHelper.reloadLib(libName);
87 same( $.mobile.useFastClick, false , "fast click is set to false after init" );
88 $.mobile.useFastClick = true;
93 var findFirstPage = function() {
94 return $(":jqmData(role='page')").first();
97 test( "active page and start page should be set to the fist page in the selected set", function(){
99 $.testHelper.reloadLib(libName);
100 var firstPage = findFirstPage();
102 same($.mobile.firstPage[0], firstPage[0]);
103 same($.mobile.activePage[0], firstPage[0]);
106 test( "mobile viewport class is defined on the first page's parent", function(){
108 $.testHelper.reloadLib(libName);
109 var firstPage = findFirstPage();
111 ok(firstPage.parent().hasClass("ui-mobile-viewport"), "first page has viewport");
114 test( "mobile page container is the first page's parent", function(){
116 $.testHelper.reloadLib(libName);
117 var firstPage = findFirstPage();
119 same($.mobile.pageContainer[0], firstPage.parent()[0]);
122 asyncTest( "hashchange triggered on document ready with single argument: true", function(){
123 $.testHelper.sequence([
125 location.hash = "#foo";
128 // delay the bind until the first hashchange
130 $(window).one("hashchange", function(ev, arg){
137 $.testHelper.reloadLib(libName);
142 test( "pages without a data-url attribute have it set to their id", function(){
143 same($("#foo").jqmData('url'), "foo");
146 test( "pages with a data-url attribute are left with the original value", function(){
147 same($("#bar").jqmData('url'), "bak");
150 asyncTest( "showPageLoadingMsg doesn't add the dialog to the page when loading message is false", function(){
152 $.mobile.loadingMessage = false;
153 $.mobile.showPageLoadingMsg();
155 setTimeout(function(){
156 ok(!$(".ui-loader").length, "no ui-loader element");
161 asyncTest( "hidePageLoadingMsg doesn't add the dialog to the page when loading message is false", function(){
163 $.mobile.loadingMessage = true;
164 $.mobile.hidePageLoadingMsg();
166 setTimeout(function(){
167 same($(".ui-loading").length, 0, "page should not be in the loading state");
172 asyncTest( "showPageLoadingMsg adds the dialog to the page when loadingMessage is true", function(){
174 $.mobile.loadingMessage = true;
175 $.mobile.showPageLoadingMsg();
177 setTimeout(function(){
178 same($(".ui-loading").length, 1, "page should be in the loading state");
183 asyncTest( "page loading should contain default loading message", function(){
185 reloadCoreNSandInit();
186 $.mobile.showPageLoadingMsg();
188 setTimeout(function(){
189 same($(".ui-loader h1").text(), "loading");
194 asyncTest( "page loading should contain custom loading message", function(){
195 $.mobile.loadingMessage = "foo";
196 $.testHelper.reloadLib(libName);
197 $.mobile.showPageLoadingMsg();
199 setTimeout(function(){
200 same($(".ui-loader h1").text(), "foo");
205 asyncTest( "page loading should contain custom loading message when set during runtime", function(){
206 $.mobile.loadingMessage = "bar";
207 $.mobile.showPageLoadingMsg();
209 setTimeout(function(){
210 same($(".ui-loader h1").text(), "bar");
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
220 asyncTest( "page does auto-initialize at domready when autoinitialize option is true (default) ", function(){
222 $( "<div />", { "data-nstest-role": "page", "id": "autoinit-on" } ).prependTo( "body" )
224 $(document).one("mobileinit", function(){
225 $.mobile.autoInitializePage = true;
230 reloadCoreNSandInit();
232 setTimeout(function(){
233 same( $( "#autoinit-on.ui-page" ).length, 1 );
240 asyncTest( "page does not initialize at domready when autoinitialize option is false ", function(){
241 $(document).one("mobileinit", function(){
242 $.mobile.autoInitializePage = false;
245 $( "<div />", { "data-nstest-role": "page", "id": "autoinit-off" } ).prependTo( "body" )
250 reloadCoreNSandInit();
252 setTimeout(function(){
253 same( $( "#autoinit-off.ui-page" ).length, 0 );
255 $(document).bind("mobileinit", function(){
256 $.mobile.autoInitializePage = true;
259 reloadCoreNSandInit();