5 var mobilePage = undefined,
6 libName = 'jquery.mobile.init.js',
7 coreLib = 'jquery.mobile.core.js',
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);
20 // NOTE reset for gradeA tests
21 $('html').removeClass('ui-mobile');
23 // TODO add post reload callback
24 $('.ui-loader').remove();
29 // NOTE reset for showPageLoadingMsg/hidePageLoadingMsg tests
30 $('.ui-loader').remove();
32 // clear the classes added by reloading the init
33 $("html").attr('class', '');
35 $.mobile.loadingMessage = originalLoadingMessage;
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;
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" );
50 $.testHelper.excludeFileProtocol(function(){
51 asyncTest( "loading the init library triggers mobilinit on the document", function(){
52 var initFired = false;
55 $(window.document).one('mobileinit', function(event){
59 $.testHelper.reloadLib(libName);
61 setTimeout(function(){
62 ok(initFired, "init fired");
67 test( "enhancments are skipped when the browser is not grade A", function(){
69 $.testHelper.reloadLib(libName);
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");
75 test( "enhancments are added when the browser is grade A", function(){
77 $.testHelper.reloadLib(libName);
79 ok($("html").hasClass("ui-mobile"), "html elem has class mobile");
82 asyncTest( "useFastClick is configurable via mobileinit", function(){
83 $(document).one( "mobileinit", function(){
84 $.mobile.useFastClick = false;
88 $.testHelper.reloadLib(libName);
90 same( $.mobile.useFastClick, false , "fast click is set to false after init" );
91 $.mobile.useFastClick = true;
96 var findFirstPage = function() {
97 return $(":jqmData(role='page')").first();
100 test( "active page and start page should be set to the fist page in the selected set", function(){
102 $.testHelper.reloadLib(libName);
103 var firstPage = findFirstPage();
105 same($.mobile.firstPage[0], firstPage[0]);
106 same($.mobile.activePage[0], firstPage[0]);
109 test( "mobile viewport class is defined on the first page's parent", function(){
111 $.testHelper.reloadLib(libName);
112 var firstPage = findFirstPage();
114 ok(firstPage.parent().hasClass("ui-mobile-viewport"), "first page has viewport");
117 test( "mobile page container is the first page's parent", function(){
119 $.testHelper.reloadLib(libName);
120 var firstPage = findFirstPage();
122 same($.mobile.pageContainer[0], firstPage.parent()[0]);
125 asyncTest( "hashchange triggered on document ready with single argument: true", function(){
126 $.testHelper.sequence([
128 location.hash = "#foo";
131 // delay the bind until the first hashchange
133 $(window).one("hashchange", function(ev, arg){
140 $.testHelper.reloadLib(libName);
145 test( "pages without a data-url attribute have it set to their id", function(){
146 same($("#foo").jqmData('url'), "foo");
149 test( "pages with a data-url attribute are left with the original value", function(){
150 same($("#bar").jqmData('url'), "bak");
153 asyncTest( "showPageLoadingMsg doesn't add the dialog to the page when loading message is false", function(){
155 $.mobile.loadingMessage = false;
156 $.mobile.showPageLoadingMsg();
158 setTimeout(function(){
159 ok(!$(".ui-loader").length, "no ui-loader element");
164 asyncTest( "hidePageLoadingMsg doesn't add the dialog to the page when loading message is false", function(){
166 $.mobile.loadingMessage = true;
167 $.mobile.hidePageLoadingMsg();
169 setTimeout(function(){
170 same($(".ui-loading").length, 0, "page should not be in the loading state");
175 asyncTest( "showPageLoadingMsg adds the dialog to the page when loadingMessage is true", function(){
177 $.mobile.loadingMessage = true;
178 $.mobile.showPageLoadingMsg();
180 setTimeout(function(){
181 same($(".ui-loading").length, 1, "page should be in the loading state");
186 asyncTest( "page loading should contain default loading message", function(){
188 reloadCoreNSandInit();
189 $.mobile.showPageLoadingMsg();
191 setTimeout(function(){
192 same($(".ui-loader h1").text(), "loading");
197 asyncTest( "page loading should contain custom loading message", function(){
198 $.mobile.loadingMessage = "foo";
199 $.testHelper.reloadLib(libName);
200 $.mobile.showPageLoadingMsg();
202 setTimeout(function(){
203 same($(".ui-loader h1").text(), "foo");
208 asyncTest( "page loading should contain custom loading message when set during runtime", function(){
209 $.mobile.loadingMessage = "bar";
210 $.mobile.showPageLoadingMsg();
212 setTimeout(function(){
213 same($(".ui-loader h1").text(), "bar");
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
223 asyncTest( "page does auto-initialize at domready when autoinitialize option is true (default) ", function(){
225 $( "<div />", { "data-nstest-role": "page", "id": "autoinit-on" } ).prependTo( "body" )
227 $(document).one("mobileinit", function(){
228 $.mobile.autoInitializePage = true;
233 reloadCoreNSandInit();
235 setTimeout(function(){
236 same( $( "#autoinit-on.ui-page" ).length, 1 );
243 asyncTest( "page does not initialize at domready when autoinitialize option is false ", function(){
244 $(document).one("mobileinit", function(){
245 $.mobile.autoInitializePage = false;
248 $( "<div />", { "data-nstest-role": "page", "id": "autoinit-off" } ).prependTo( "body" )
253 reloadCoreNSandInit();
255 setTimeout(function(){
256 same( $( "#autoinit-off.ui-page" ).length, 0 );
258 $(document).bind("mobileinit", function(){
259 $.mobile.autoInitializePage = true;
262 reloadCoreNSandInit();