| 1 |
// Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.history.Html5HistoryTest'); |
| 16 |
goog.setTestOnly('goog.history.Html5HistoryTest'); |
| 17 |
|
| 18 |
// Delay running the tests after page load. This test has some asynchronous |
| 19 |
// behavior that interacts with page load detection. |
| 20 |
goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS = 500; |
| 21 |
|
| 22 |
goog.require('goog.Timer'); |
| 23 |
goog.require('goog.events'); |
| 24 |
goog.require('goog.events.EventType'); |
| 25 |
goog.require('goog.history.EventType'); |
| 26 |
goog.require('goog.history.Html5History'); |
| 27 |
goog.require('goog.testing.MockControl'); |
| 28 |
goog.require('goog.testing.jsunit'); |
| 29 |
goog.require('goog.testing.mockmatchers'); |
| 30 |
goog.require('goog.testing.recordFunction'); |
| 31 |
|
| 32 |
var mockControl; |
| 33 |
var mockWindow; |
| 34 |
|
| 35 |
var html5History; |
| 36 |
|
| 37 |
function setUp() { |
| 38 |
mockControl = new goog.testing.MockControl(); |
| 39 |
|
| 40 |
mockWindow = {location: {}}; |
| 41 |
mockWindow.attachEvent = mockControl.createFunctionMock(); |
| 42 |
mockWindow |
| 43 |
.attachEvent( |
| 44 |
goog.testing.mockmatchers.ignoreArgument, |
| 45 |
goog.testing.mockmatchers.ignoreArgument) |
| 46 |
.$anyTimes(); |
| 47 |
var mockHistoryIsSupportedMethod = |
| 48 |
mockControl.createMethodMock(goog.history.Html5History, 'isSupported'); |
| 49 |
mockHistoryIsSupportedMethod(mockWindow).$returns(true).$anyTimes(); |
| 50 |
} |
| 51 |
|
| 52 |
function tearDown() { |
| 53 |
if (html5History) { |
| 54 |
html5History.dispose(); |
| 55 |
html5History = null; |
| 56 |
} |
| 57 |
mockControl.$tearDown(); |
| 58 |
} |
| 59 |
|
| 60 |
function testGetTokenWithoutUsingFragment() { |
| 61 |
mockWindow.location.pathname = '/test/something'; |
| 62 |
|
| 63 |
mockControl.$replayAll(); |
| 64 |
html5History = new goog.history.Html5History(mockWindow); |
| 65 |
html5History.setUseFragment(false); |
| 66 |
|
| 67 |
assertEquals('test/something', html5History.getToken()); |
| 68 |
mockControl.$verifyAll(); |
| 69 |
} |
| 70 |
|
| 71 |
function testGetTokenWithoutUsingFragmentWithCustomPathPrefix() { |
| 72 |
mockWindow.location.pathname = '/test/something'; |
| 73 |
|
| 74 |
mockControl.$replayAll(); |
| 75 |
html5History = new goog.history.Html5History(mockWindow); |
| 76 |
html5History.setUseFragment(false); |
| 77 |
html5History.setPathPrefix('/test/'); |
| 78 |
|
| 79 |
assertEquals('something', html5History.getToken()); |
| 80 |
mockControl.$verifyAll(); |
| 81 |
} |
| 82 |
|
| 83 |
function testGetTokenWithoutUsingFragmentWithCustomTransformer() { |
| 84 |
mockWindow.location.pathname = '/test/something'; |
| 85 |
var mockTransformer = |
| 86 |
mockControl.createLooseMock(goog.history.Html5History.TokenTransformer); |
| 87 |
mockTransformer.retrieveToken('/', mockWindow.location).$returns('abc/1'); |
| 88 |
|
| 89 |
mockControl.$replayAll(); |
| 90 |
html5History = new goog.history.Html5History(mockWindow, mockTransformer); |
| 91 |
html5History.setUseFragment(false); |
| 92 |
|
| 93 |
assertEquals('abc/1', html5History.getToken()); |
| 94 |
mockControl.$verifyAll(); |
| 95 |
} |
| 96 |
|
| 97 |
function testGetTokenWithoutUsingFragmentWithCustomTransformerAndPrefix() { |
| 98 |
mockWindow.location.pathname = '/test/something'; |
| 99 |
var mockTransformer = |
| 100 |
mockControl.createLooseMock(goog.history.Html5History.TokenTransformer); |
| 101 |
mockTransformer.retrieveToken('/test/', mockWindow.location) |
| 102 |
.$returns('abc/1'); |
| 103 |
|
| 104 |
mockControl.$replayAll(); |
| 105 |
html5History = new goog.history.Html5History(mockWindow, mockTransformer); |
| 106 |
html5History.setUseFragment(false); |
| 107 |
html5History.setPathPrefix('/test/'); |
| 108 |
|
| 109 |
assertEquals('abc/1', html5History.getToken()); |
| 110 |
mockControl.$verifyAll(); |
| 111 |
} |
| 112 |
|
| 113 |
function testGetUrlWithoutUsingFragment() { |
| 114 |
mockWindow.location.search = '?q=something'; |
| 115 |
|
| 116 |
mockControl.$replayAll(); |
| 117 |
html5History = new goog.history.Html5History(mockWindow); |
| 118 |
html5History.setUseFragment(false); |
| 119 |
|
| 120 |
assertEquals('/some/token?q=something', html5History.getUrl_('some/token')); |
| 121 |
mockControl.$verifyAll(); |
| 122 |
} |
| 123 |
|
| 124 |
function testGetUrlWithoutUsingFragmentWithCustomPathPrefix() { |
| 125 |
mockWindow.location.search = '?q=something'; |
| 126 |
|
| 127 |
mockControl.$replayAll(); |
| 128 |
html5History = new goog.history.Html5History(mockWindow); |
| 129 |
html5History.setUseFragment(false); |
| 130 |
html5History.setPathPrefix('/test/'); |
| 131 |
|
| 132 |
assertEquals( |
| 133 |
'/test/some/token?q=something', html5History.getUrl_('some/token')); |
| 134 |
mockControl.$verifyAll(); |
| 135 |
} |
| 136 |
|
| 137 |
function testGetUrlWithoutUsingFragmentWithCustomTransformer() { |
| 138 |
mockWindow.location.search = '?q=something'; |
| 139 |
var mockTransformer = |
| 140 |
mockControl.createLooseMock(goog.history.Html5History.TokenTransformer); |
| 141 |
mockTransformer.createUrl('some/token', '/', mockWindow.location) |
| 142 |
.$returns('/something/else/?different'); |
| 143 |
|
| 144 |
mockControl.$replayAll(); |
| 145 |
html5History = new goog.history.Html5History(mockWindow, mockTransformer); |
| 146 |
html5History.setUseFragment(false); |
| 147 |
|
| 148 |
assertEquals( |
| 149 |
'/something/else/?different', html5History.getUrl_('some/token')); |
| 150 |
mockControl.$verifyAll(); |
| 151 |
} |
| 152 |
|
| 153 |
function testGetUrlWithoutUsingFragmentWithCustomTransformerAndPrefix() { |
| 154 |
mockWindow.location.search = '?q=something'; |
| 155 |
var mockTransformer = |
| 156 |
mockControl.createLooseMock(goog.history.Html5History.TokenTransformer); |
| 157 |
mockTransformer.createUrl('some/token', '/test/', mockWindow.location) |
| 158 |
.$returns('/something/else/?different'); |
| 159 |
|
| 160 |
mockControl.$replayAll(); |
| 161 |
html5History = new goog.history.Html5History(mockWindow, mockTransformer); |
| 162 |
html5History.setUseFragment(false); |
| 163 |
html5History.setPathPrefix('/test/'); |
| 164 |
|
| 165 |
assertEquals( |
| 166 |
'/something/else/?different', html5History.getUrl_('some/token')); |
| 167 |
mockControl.$verifyAll(); |
| 168 |
} |
| 169 |
|
| 170 |
// Regression test for b/18663922. |
| 171 |
function testNavigateFiresOnceOnNavigation() { |
| 172 |
var history = new goog.history.Html5History; |
| 173 |
var onNavigate = goog.testing.recordFunction(); |
| 174 |
history.setEnabled(true); |
| 175 |
history.listen(goog.history.EventType.NAVIGATE, onNavigate); |
| 176 |
|
| 177 |
// Simulate that the user navigates in the history. |
| 178 |
location = '#' + goog.now(); |
| 179 |
|
| 180 |
return goog.Timer.promise(0).then(function() { |
| 181 |
// NAVIGATE should fire once with isNavigation=true. |
| 182 |
onNavigate.assertCallCount(1); |
| 183 |
assertTrue(onNavigate.getLastCall().getArgument(0).isNavigation); |
| 184 |
return goog.Timer.promise(0).then(function() { |
| 185 |
// NAVIGATE should not fire again after the current JS execution context. |
| 186 |
onNavigate.assertCallCount(1); |
| 187 |
}); |
| 188 |
}); |
| 189 |
} |
| 190 |
|
| 191 |
// Regression test for b/18663922. |
| 192 |
function testNavigateFiresOnceWithoutPopstate() { |
| 193 |
var history = new goog.history.Html5History; |
| 194 |
var onNavigate = goog.testing.recordFunction(); |
| 195 |
history.setEnabled(true); |
| 196 |
history.listen(goog.history.EventType.NAVIGATE, onNavigate); |
| 197 |
|
| 198 |
// Removing POPSTATE to ensure NAVIGATE is triggered in browsers that don't |
| 199 |
// support it. |
| 200 |
assertTrue( |
| 201 |
goog.events.unlisten( |
| 202 |
window, goog.events.EventType.POPSTATE, history.onHistoryEvent_, |
| 203 |
false, history)); |
| 204 |
|
| 205 |
// Simulate that the user navigates in the history. |
| 206 |
location = '#' + goog.now(); |
| 207 |
|
| 208 |
return goog.Timer.promise(0).then(function() { |
| 209 |
// NAVIGATE should fire once with isNavigation=true. |
| 210 |
onNavigate.assertCallCount(1); |
| 211 |
assertTrue(onNavigate.getLastCall().getArgument(0).isNavigation); |
| 212 |
return goog.Timer.promise(0).then(function() { |
| 213 |
// NAVIGATE should not fire again after the current JS execution context. |
| 214 |
onNavigate.assertCallCount(1); |
| 215 |
}); |
| 216 |
}); |
| 217 |
} |