html5history_test.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. goog.provide('goog.history.Html5HistoryTest');
  15. goog.setTestOnly('goog.history.Html5HistoryTest');
  16. goog.require('goog.Timer');
  17. goog.require('goog.events');
  18. goog.require('goog.events.EventType');
  19. goog.require('goog.history.EventType');
  20. goog.require('goog.history.Html5History');
  21. goog.require('goog.testing.MockControl');
  22. goog.require('goog.testing.jsunit');
  23. goog.require('goog.testing.mockmatchers');
  24. goog.require('goog.testing.recordFunction');
  25. var mockControl;
  26. var mockWindow;
  27. var html5History;
  28. function setUp() {
  29. mockControl = new goog.testing.MockControl();
  30. mockWindow = {location: {}};
  31. mockWindow.attachEvent = mockControl.createFunctionMock();
  32. mockWindow
  33. .attachEvent(
  34. goog.testing.mockmatchers.ignoreArgument,
  35. goog.testing.mockmatchers.ignoreArgument)
  36. .$anyTimes();
  37. var mockHistoryIsSupportedMethod =
  38. mockControl.createMethodMock(goog.history.Html5History, 'isSupported');
  39. mockHistoryIsSupportedMethod(mockWindow).$returns(true).$anyTimes();
  40. }
  41. function tearDown() {
  42. if (html5History) {
  43. html5History.dispose();
  44. html5History = null;
  45. }
  46. mockControl.$tearDown();
  47. }
  48. function testGetTokenWithoutUsingFragment() {
  49. mockWindow.location.pathname = '/test/something';
  50. mockControl.$replayAll();
  51. html5History = new goog.history.Html5History(mockWindow);
  52. html5History.setUseFragment(false);
  53. assertEquals('test/something', html5History.getToken());
  54. mockControl.$verifyAll();
  55. }
  56. function testGetTokenWithoutUsingFragmentWithCustomPathPrefix() {
  57. mockWindow.location.pathname = '/test/something';
  58. mockControl.$replayAll();
  59. html5History = new goog.history.Html5History(mockWindow);
  60. html5History.setUseFragment(false);
  61. html5History.setPathPrefix('/test/');
  62. assertEquals('something', html5History.getToken());
  63. mockControl.$verifyAll();
  64. }
  65. function testGetTokenWithoutUsingFragmentWithCustomTransformer() {
  66. mockWindow.location.pathname = '/test/something';
  67. var mockTransformer =
  68. mockControl.createLooseMock(goog.history.Html5History.TokenTransformer);
  69. mockTransformer.retrieveToken('/', mockWindow.location).$returns('abc/1');
  70. mockControl.$replayAll();
  71. html5History = new goog.history.Html5History(mockWindow, mockTransformer);
  72. html5History.setUseFragment(false);
  73. assertEquals('abc/1', html5History.getToken());
  74. mockControl.$verifyAll();
  75. }
  76. function testGetTokenWithoutUsingFragmentWithCustomTransformerAndPrefix() {
  77. mockWindow.location.pathname = '/test/something';
  78. var mockTransformer =
  79. mockControl.createLooseMock(goog.history.Html5History.TokenTransformer);
  80. mockTransformer.retrieveToken('/test/', mockWindow.location)
  81. .$returns('abc/1');
  82. mockControl.$replayAll();
  83. html5History = new goog.history.Html5History(mockWindow, mockTransformer);
  84. html5History.setUseFragment(false);
  85. html5History.setPathPrefix('/test/');
  86. assertEquals('abc/1', html5History.getToken());
  87. mockControl.$verifyAll();
  88. }
  89. function testGetUrlWithoutUsingFragment() {
  90. mockWindow.location.search = '?q=something';
  91. mockControl.$replayAll();
  92. html5History = new goog.history.Html5History(mockWindow);
  93. html5History.setUseFragment(false);
  94. assertEquals('/some/token?q=something', html5History.getUrl_('some/token'));
  95. mockControl.$verifyAll();
  96. }
  97. function testGetUrlWithoutUsingFragmentWithCustomPathPrefix() {
  98. mockWindow.location.search = '?q=something';
  99. mockControl.$replayAll();
  100. html5History = new goog.history.Html5History(mockWindow);
  101. html5History.setUseFragment(false);
  102. html5History.setPathPrefix('/test/');
  103. assertEquals(
  104. '/test/some/token?q=something', html5History.getUrl_('some/token'));
  105. mockControl.$verifyAll();
  106. }
  107. function testGetUrlWithoutUsingFragmentWithCustomTransformer() {
  108. mockWindow.location.search = '?q=something';
  109. var mockTransformer =
  110. mockControl.createLooseMock(goog.history.Html5History.TokenTransformer);
  111. mockTransformer.createUrl('some/token', '/', mockWindow.location)
  112. .$returns('/something/else/?different');
  113. mockControl.$replayAll();
  114. html5History = new goog.history.Html5History(mockWindow, mockTransformer);
  115. html5History.setUseFragment(false);
  116. assertEquals(
  117. '/something/else/?different', html5History.getUrl_('some/token'));
  118. mockControl.$verifyAll();
  119. }
  120. function testGetUrlWithoutUsingFragmentWithCustomTransformerAndPrefix() {
  121. mockWindow.location.search = '?q=something';
  122. var mockTransformer =
  123. mockControl.createLooseMock(goog.history.Html5History.TokenTransformer);
  124. mockTransformer.createUrl('some/token', '/test/', mockWindow.location)
  125. .$returns('/something/else/?different');
  126. mockControl.$replayAll();
  127. html5History = new goog.history.Html5History(mockWindow, mockTransformer);
  128. html5History.setUseFragment(false);
  129. html5History.setPathPrefix('/test/');
  130. assertEquals(
  131. '/something/else/?different', html5History.getUrl_('some/token'));
  132. mockControl.$verifyAll();
  133. }
  134. // Regression test for b/18663922.
  135. function testNavigateFiresOnceOnNavigation() {
  136. var history = new goog.history.Html5History;
  137. var onNavigate = goog.testing.recordFunction();
  138. history.setEnabled(true);
  139. history.listen(goog.history.EventType.NAVIGATE, onNavigate);
  140. // Simulate that the user navigates in the history.
  141. location = '#' + goog.now();
  142. return goog.Timer.promise(0).then(function() {
  143. // NAVIGATE should fire once with isNavigation=true.
  144. onNavigate.assertCallCount(1);
  145. assertTrue(onNavigate.getLastCall().getArgument(0).isNavigation);
  146. return goog.Timer.promise(0).then(function() {
  147. // NAVIGATE should not fire again after the current JS execution context.
  148. onNavigate.assertCallCount(1);
  149. });
  150. });
  151. }
  152. // Regression test for b/18663922.
  153. function testNavigateFiresOnceWithoutPopstate() {
  154. var history = new goog.history.Html5History;
  155. var onNavigate = goog.testing.recordFunction();
  156. history.setEnabled(true);
  157. history.listen(goog.history.EventType.NAVIGATE, onNavigate);
  158. // Removing POPSTATE to ensure NAVIGATE is triggered in browsers that don't
  159. // support it.
  160. assertTrue(
  161. goog.events.unlisten(
  162. window, goog.events.EventType.POPSTATE, history.onHistoryEvent_,
  163. false, history));
  164. // Simulate that the user navigates in the history.
  165. location = '#' + goog.now();
  166. return goog.Timer.promise(0).then(function() {
  167. // NAVIGATE should fire once with isNavigation=true.
  168. onNavigate.assertCallCount(1);
  169. assertTrue(onNavigate.getLastCall().getArgument(0).isNavigation);
  170. return goog.Timer.promise(0).then(function() {
  171. // NAVIGATE should not fire again after the current JS execution context.
  172. onNavigate.assertCallCount(1);
  173. });
  174. });
  175. }