Tizen 2.1 base
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.0.1pre / tests / unit / navigation / navigation_helpers.js
1 /*
2  * mobile navigation unit tests
3  */
4 (function($){
5         var siteDirectory = location.pathname.replace(/[^/]+$/, "");
6
7         module('jquery.mobile.navigation.js', {
8                 setup: function(){
9                         if ( location.hash ) {
10                                 stop();
11                                 $(document).one("pagechange", function() {
12                                         start();
13                                 } );
14                                 location.hash = "";
15                         }
16                 }
17         });
18
19         test( "path.get method is working properly", function(){
20                 window.location.hash = "foo";
21                 same($.mobile.path.get(), "foo", "get method returns location.hash minus hash character");
22                 same($.mobile.path.get( "#foo/bar/baz.html" ), "foo/bar/", "get method with hash arg returns path with no filename or hash prefix");
23                 same($.mobile.path.get( "#foo/bar/baz.html/" ), "foo/bar/baz.html/", "last segment of hash is retained if followed by a trailing slash");
24         });
25
26         test( "path.isPath method is working properly", function(){
27                 ok(!$.mobile.path.isPath('bar'), "anything without a slash is not a path");
28                 ok($.mobile.path.isPath('bar/'), "anything with a slash is a path");
29                 ok($.mobile.path.isPath('/bar'), "anything with a slash is a path");
30                 ok($.mobile.path.isPath('a/r'), "anything with a slash is a path");
31                 ok($.mobile.path.isPath('/'), "anything with a slash is a path");
32         });
33
34         test( "path.getFilePath method is working properly", function(){
35                 same($.mobile.path.getFilePath("foo.html" + "&" + $.mobile.subPageUrlKey ), "foo.html", "returns path without sub page key");
36         });
37
38         test( "path.set method is working properly", function(){
39                 $.mobile.urlHistory.ignoreNextHashChange = false;
40                 $.mobile.path.set("foo");
41                 same("foo", window.location.hash.replace(/^#/,""), "sets location.hash properly");
42         });
43
44         test( "path.makeUrlAbsolute is working properly", function(){
45                 var mua = $.mobile.path.makeUrlAbsolute,
46                         p1 = "http://jqm.com/",
47                         p2 = "http://jqm.com/?foo=1&bar=2",
48                         p3 = "http://jqm.com/#spaz",
49                         p4 = "http://jqm.com/?foo=1&bar=2#spaz",
50
51                         p5 = "http://jqm.com/test.php",
52                         p6 = "http://jqm.com/test.php?foo=1&bar=2",
53                         p7 = "http://jqm.com/test.php#spaz",
54                         p8 = "http://jqm.com/test.php?foo=1&bar=2#spaz",
55
56                         p9 = "http://jqm.com/dir1/dir2/",
57                         p10 = "http://jqm.com/dir1/dir2/?foo=1&bar=2",
58                         p11 = "http://jqm.com/dir1/dir2/#spaz",
59                         p12 = "http://jqm.com/dir1/dir2/?foo=1&bar=2#spaz",
60
61                         p13 = "http://jqm.com/dir1/dir2/test.php",
62                         p14 = "http://jqm.com/dir1/dir2/test.php?foo=1&bar=2",
63                         p15 = "http://jqm.com/dir1/dir2/test.php#spaz",
64                         p16 = "http://jqm.com/dir1/dir2/test.php?foo=1&bar=2#spaz";
65
66                 // Test URL conversion against an absolute URL to the site root.
67                 // directory tests
68                 same( mua( "http://jqm.com/",                            p1 ), "http://jqm.com/", "absolute root - absolute root" );
69                 same( mua( "//jqm.com/",                                 p1 ), "http://jqm.com/", "protocol relative root - absolute root" );
70                 same( mua( "/",                                          p1 ), "http://jqm.com/", "site relative root - absolute root" );
71
72                 same( mua( "http://jqm.com/?foo=1&bar=2",                p1 ), "http://jqm.com/?foo=1&bar=2", "absolute root with query - absolute root" );
73                 same( mua( "//jqm.com/?foo=1&bar=2",                     p1 ), "http://jqm.com/?foo=1&bar=2", "protocol relative root with query - absolute root" );
74                 same( mua( "/?foo=1&bar=2",                              p1 ), "http://jqm.com/?foo=1&bar=2", "site relative root with query - absolute root" );
75                 same( mua( "?foo=1&bar=2",                               p1 ), "http://jqm.com/?foo=1&bar=2", "query relative - absolute root" );
76
77                 same( mua( "http://jqm.com/#spaz",                       p1 ), "http://jqm.com/#spaz", "absolute root with fragment - absolute root" );
78                 same( mua( "//jqm.com/#spaz",                            p1 ), "http://jqm.com/#spaz", "protocol relative root with fragment - absolute root" );
79                 same( mua( "/#spaz",                                     p1 ), "http://jqm.com/#spaz", "site relative root with fragment - absolute root" );
80                 same( mua( "#spaz",                                      p1 ), "http://jqm.com/#spaz", "fragment relative - absolute root" );
81
82                 same( mua( "http://jqm.com/?foo=1&bar=2#spaz",           p1 ), "http://jqm.com/?foo=1&bar=2#spaz", "absolute root with query and fragment - absolute root" );
83                 same( mua( "//jqm.com/?foo=1&bar=2#spaz",                p1 ), "http://jqm.com/?foo=1&bar=2#spaz", "protocol relative root with query and fragment - absolute root" );
84                 same( mua( "/?foo=1&bar=2#spaz",                         p1 ), "http://jqm.com/?foo=1&bar=2#spaz", "site relative root with query and fragment - absolute root" );
85                 same( mua( "?foo=1&bar=2#spaz",                          p1 ), "http://jqm.com/?foo=1&bar=2#spaz", "query relative and fragment - absolute root" );
86
87                 // file tests
88                 same( mua( "http://jqm.com/test.php",                    p1 ), "http://jqm.com/test.php", "absolute file at root - absolute root" );
89                 same( mua( "//jqm.com/test.php",                         p1 ), "http://jqm.com/test.php", "protocol relative file at root - absolute root" );
90                 same( mua( "/test.php",                                  p1 ), "http://jqm.com/test.php", "site relative file at root - absolute root" );
91                 same( mua( "test.php",                                   p1 ), "http://jqm.com/test.php", "document relative file at root - absolute root" );
92
93                 same( mua( "http://jqm.com/test.php?foo=1&bar=2",        p1 ), "http://jqm.com/test.php?foo=1&bar=2", "absolute file at root with query - absolute root" );
94                 same( mua( "//jqm.com/test.php?foo=1&bar=2",             p1 ), "http://jqm.com/test.php?foo=1&bar=2", "protocol relative file at root with query - absolute root" );
95                 same( mua( "/test.php?foo=1&bar=2",                      p1 ), "http://jqm.com/test.php?foo=1&bar=2", "site relative file at root with query - absolute root" );
96                 same( mua( "test.php?foo=1&bar=2",                       p1 ), "http://jqm.com/test.php?foo=1&bar=2", "document relative file at root with query - absolute root" );
97
98                 same( mua( "http://jqm.com/test.php#spaz",               p1 ), "http://jqm.com/test.php#spaz", "absolute file at root with fragment - absolute root" );
99                 same( mua( "//jqm.com/test.php#spaz",                    p1 ), "http://jqm.com/test.php#spaz", "protocol relative file at root with fragment - absolute root" );
100                 same( mua( "/test.php#spaz",                             p1 ), "http://jqm.com/test.php#spaz", "site relative file at root with fragment - absolute root" );
101                 same( mua( "test.php#spaz",                              p1 ), "http://jqm.com/test.php#spaz", "file at root with fragment - absolute root" );
102
103                 same( mua( "http://jqm.com/test.php?foo=1&bar=2#spaz",   p1 ), "http://jqm.com/test.php?foo=1&bar=2#spaz", "absolute file at root with query and fragment - absolute root" );
104                 same( mua( "//jqm.com/test.php?foo=1&bar=2#spaz",        p1 ), "http://jqm.com/test.php?foo=1&bar=2#spaz", "protocol relative file at root with query and fragment - absolute root" );
105                 same( mua( "/test.php?foo=1&bar=2#spaz",                 p1 ), "http://jqm.com/test.php?foo=1&bar=2#spaz", "site relative file at root with query and fragment - absolute root" );
106                 same( mua( "test.php?foo=1&bar=2#spaz",                  p1 ), "http://jqm.com/test.php?foo=1&bar=2#spaz", "query relative file at root fragment - absolute root" );
107
108                 // Test URL conversion against an absolute URL to a file at the site root.
109
110                 same( mua( "http://jqm.com/",                            p5 ), "http://jqm.com/", "absolute root - absolute root" );
111                 same( mua( "//jqm.com/",                                 p5 ), "http://jqm.com/", "protocol relative root - absolute root" );
112                 same( mua( "/",                                          p5 ), "http://jqm.com/", "site relative root - absolute root" );
113
114                 same( mua( "http://jqm.com/?foo=1&bar=2",                p5 ), "http://jqm.com/?foo=1&bar=2", "absolute root with query - absolute root" );
115                 same( mua( "//jqm.com/?foo=1&bar=2",                     p5 ), "http://jqm.com/?foo=1&bar=2", "protocol relative root with query - absolute root" );
116                 same( mua( "/?foo=1&bar=2",                              p5 ), "http://jqm.com/?foo=1&bar=2", "site relative root with query - absolute root" );
117                 same( mua( "?foo=1&bar=2",                               p5 ), "http://jqm.com/test.php?foo=1&bar=2", "query relative - absolute root" );
118
119                 same( mua( "http://jqm.com/#spaz",                       p5 ), "http://jqm.com/#spaz", "absolute root with fragment - absolute root" );
120                 same( mua( "//jqm.com/#spaz",                            p5 ), "http://jqm.com/#spaz", "protocol relative root with fragment - absolute root" );
121                 same( mua( "/#spaz",                                     p5 ), "http://jqm.com/#spaz", "site relative root with fragment - absolute root" );
122                 same( mua( "#spaz",                                      p5 ), "http://jqm.com/test.php#spaz", "fragment relative - absolute root" );
123
124                 same( mua( "http://jqm.com/?foo=1&bar=2#spaz",           p5 ), "http://jqm.com/?foo=1&bar=2#spaz", "absolute root with query and fragment - absolute root" );
125                 same( mua( "//jqm.com/?foo=1&bar=2#spaz",                p5 ), "http://jqm.com/?foo=1&bar=2#spaz", "protocol relative root with query and fragment - absolute root" );
126                 same( mua( "/?foo=1&bar=2#spaz",                         p5 ), "http://jqm.com/?foo=1&bar=2#spaz", "site relative root with query and fragment - absolute root" );
127                 same( mua( "?foo=1&bar=2#spaz",                          p5 ), "http://jqm.com/test.php?foo=1&bar=2#spaz", "query relative and fragment - absolute root" );
128         });
129
130         // https://github.com/jquery/jquery-mobile/issues/2362
131         test( "ipv6 host support", function(){
132                 // http://www.ietf.org/rfc/rfc2732.txt ipv6 examples for tests
133                 // most definitely not comprehensive
134                 var     ipv6_1 = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html",
135                         ipv6_2 = "http://[1080:0:0:0:8:800:200C:417A]/index.html",
136                         ipv6_3 = "http://[3ffe:2a00:100:7031::1]",
137                         ipv6_4 = "http://[1080::8:800:200C:417A]/foo",
138                         ipv6_5 = "http://[::192.9.5.5]/ipng",
139                         ipv6_6 = "http://[::FFFF:129.144.52.38]:80/index.html",
140                         ipv6_7 = "http://[2010:836B:4179::836B:4179]",
141                   fromIssue = "http://[3fff:cafe:babe::]:443/foo";
142
143                 same( $.mobile.path.parseUrl(ipv6_1).host, "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80");
144                 same( $.mobile.path.parseUrl(ipv6_1).hostname, "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]");
145                 same( $.mobile.path.parseUrl(ipv6_2).host, "[1080:0:0:0:8:800:200C:417A]");
146                 same( $.mobile.path.parseUrl(ipv6_3).host, "[3ffe:2a00:100:7031::1]");
147                 same( $.mobile.path.parseUrl(ipv6_4).host, "[1080::8:800:200C:417A]");
148                 same( $.mobile.path.parseUrl(ipv6_5).host, "[::192.9.5.5]");
149                 same( $.mobile.path.parseUrl(ipv6_6).host, "[::FFFF:129.144.52.38]:80");
150                 same( $.mobile.path.parseUrl(ipv6_6).hostname, "[::FFFF:129.144.52.38]");
151                 same( $.mobile.path.parseUrl(ipv6_7).host, "[2010:836B:4179::836B:4179]");
152                 same( $.mobile.path.parseUrl(fromIssue).host, "[3fff:cafe:babe::]:443");
153                 same( $.mobile.path.parseUrl(fromIssue).hostname, "[3fff:cafe:babe::]");
154         });
155
156         test( "path.clean is working properly", function(){
157                 var localroot = location.protocol + "//" + location.host + location.pathname,
158                         remoteroot = "http://google.com/",
159                         fakepath = "#foo/bar/baz.html",
160                         pathWithParam = localroot + "bar?baz=" + localroot,
161                         localpath = localroot + fakepath,
162                         remotepath = remoteroot + fakepath;
163
164                 same( $.mobile.path.clean( localpath ), location.pathname + fakepath, "removes location protocol, host, and portfrom same-domain path");
165                 same( $.mobile.path.clean( remotepath ), remotepath, "does nothing to an external domain path");
166                 same( $.mobile.path.clean( pathWithParam ), location.pathname + "bar?baz=" + localroot, "doesn't remove params with localroot value");
167         });
168
169         test( "path.stripHash is working properly", function(){
170                 same( $.mobile.path.stripHash( "#bar" ), "bar", "returns a hash without the # prefix");
171         });
172
173         test( "path.hasProtocol is working properly", function(){
174                 same( $.mobile.path.hasProtocol( "tel:5559999" ), true, "value in tel protocol format has protocol" );
175                 same( $.mobile.path.hasProtocol( location.href ), true, "location href has protocol" );
176                 same( $.mobile.path.hasProtocol( "foo/bar/baz.html" ), false, "simple directory path has no protocol" );
177                 same( $.mobile.path.hasProtocol( "file://foo/bar/baz.html" ), true, "simple directory path with file:// has protocol" );
178         });
179
180         test( "path.isRelativeUrl is working properly", function(){
181                 same( $.mobile.path.isRelativeUrl("http://company.com/"), false, "absolute url is not relative" );
182                 same( $.mobile.path.isRelativeUrl("//company.com/"), true, "protocol relative url is relative" );
183                 same( $.mobile.path.isRelativeUrl("/"), true, "site relative url is relative" );
184
185                 same( $.mobile.path.isRelativeUrl("http://company.com/test.php"), false, "absolute url is not relative" );
186                 same( $.mobile.path.isRelativeUrl("//company.com/test.php"), true, "protocol relative url is relative" );
187                 same( $.mobile.path.isRelativeUrl("/test.php"), true, "site relative url is relative" );
188                 same( $.mobile.path.isRelativeUrl("test.php"), true, "document relative url is relative" );
189
190                 same( $.mobile.path.isRelativeUrl("http://company.com/dir1/dir2/test.php?foo=1&bar=2#frag"), false, "absolute url is not relative" );
191                 same( $.mobile.path.isRelativeUrl("//company.com/dir1/dir2/test.php?foo=1&bar=2#frag"), true, "protocol relative url is relative" );
192                 same( $.mobile.path.isRelativeUrl("/dir1/dir2/test.php?foo=1&bar=2#frag"), true, "site relative url is relative" );
193                 same( $.mobile.path.isRelativeUrl("dir1/dir2/test.php?foo=1&bar=2#frag"), true, "document relative path url is relative" );
194                 same( $.mobile.path.isRelativeUrl("test.php?foo=1&bar=2#frag"), true, "document relative file url is relative" );
195                 same( $.mobile.path.isRelativeUrl("?foo=1&bar=2#frag"), true, "query relative url is relative" );
196                 same( $.mobile.path.isRelativeUrl("#frag"), true, "fragments are relative" );
197         });
198
199         test( "path.isExternal is working properly", function(){
200                 same( $.mobile.path.isExternal( location.href ), false, "same domain is not external" );
201                 same( $.mobile.path.isExternal( "http://example.com" ), true, "example.com is external" );
202                 same($.mobile.path.isExternal("mailto:"), true, "mailto protocol");
203                 same($.mobile.path.isExternal("http://foo.com"), true, "http protocol");
204                 same($.mobile.path.isExternal("http://www.foo.com"), true, "http protocol with www");
205                 same($.mobile.path.isExternal("tel:16178675309"), true, "tel protocol");
206                 same($.mobile.path.isExternal("foo.html"), false, "filename");
207                 same($.mobile.path.isExternal("foo/foo/foo.html"), false, "file path");
208                 same($.mobile.path.isExternal("../../index.html"), false, "relative parent path");
209                 same($.mobile.path.isExternal("/foo"), false, "root-relative path");
210                 same($.mobile.path.isExternal("foo"), false, "simple string");
211                 same($.mobile.path.isExternal("#foo"), false, "local id reference");
212         });
213
214         test( "path.cleanHash", function(){
215                 same( $.mobile.path.cleanHash( "#anything/atall?akjfdjjf" ), "anything/atall", "removes query param");
216                 same( $.mobile.path.cleanHash( "#nothing/atall" ), "nothing/atall", "removes query param");
217         });
218 })(jQuery);