OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 suite('route', function() { | 5 suite('route', function() { |
| 6 /** |
| 7 * Returns a new promise that resolves after a window 'popstate' event. |
| 8 * @return {!Promise} |
| 9 */ |
| 10 function whenPopState(causeEvent) { |
| 11 var promise = new Promise(function(resolve) { |
| 12 window.addEventListener('popstate', function callback() { |
| 13 window.removeEventListener('popstate', callback); |
| 14 resolve(); |
| 15 }); |
| 16 }); |
| 17 |
| 18 causeEvent(); |
| 19 return promise; |
| 20 } |
| 21 |
| 22 /** |
| 23 * Tests a specific navigation situation. |
| 24 * @param {!settings.Route} previousRoute |
| 25 * @param {!settings.Route} currentRoute |
| 26 * @param {!settings.Route} expectedNavigatePreviousResult |
| 27 * @return {!Promise} |
| 28 */ |
| 29 function testNavigateBackUsesHistory(previousRoute, currentRoute, |
| 30 expectedNavigatePreviousResult) { |
| 31 settings.navigateTo(previousRoute); |
| 32 settings.navigateTo(currentRoute); |
| 33 |
| 34 return whenPopState(function() { |
| 35 settings.navigateToPreviousRoute(); |
| 36 }).then(function() { |
| 37 assertEquals(expectedNavigatePreviousResult, |
| 38 settings.getCurrentRoute()); |
| 39 }); |
| 40 }; |
| 41 |
6 test('tree structure', function() { | 42 test('tree structure', function() { |
7 // Set up root page routes. | 43 // Set up root page routes. |
8 var BASIC = new settings.Route('/'); | 44 var BASIC = new settings.Route('/'); |
9 assertEquals(0, BASIC.depth); | 45 assertEquals(0, BASIC.depth); |
10 | 46 |
11 var ADVANCED = new settings.Route('/advanced'); | 47 var ADVANCED = new settings.Route('/advanced'); |
12 assertFalse(ADVANCED.isSubpage()); | 48 assertFalse(ADVANCED.isSubpage()); |
13 assertEquals(0, ADVANCED.depth); | 49 assertEquals(0, ADVANCED.depth); |
14 | 50 |
15 // Test a section route. | 51 // Test a section route. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 }); | 84 }); |
49 | 85 |
50 test('no duplicate routes', function() { | 86 test('no duplicate routes', function() { |
51 var paths = new Set(); | 87 var paths = new Set(); |
52 Object.values(settings.Route).forEach(function(route) { | 88 Object.values(settings.Route).forEach(function(route) { |
53 assertFalse(paths.has(route.path), route.path); | 89 assertFalse(paths.has(route.path), route.path); |
54 paths.add(route.path); | 90 paths.add(route.path); |
55 }); | 91 }); |
56 }); | 92 }); |
57 | 93 |
58 /** | |
59 * Tests a specific navigation situation. | |
60 * @param {!settings.Route} previousRoute | |
61 * @param {!settings.Route} currentRoute | |
62 * @param {!settings.Route} expectedNavigatePreviousResult | |
63 * @return {!Promise} | |
64 */ | |
65 function testNavigateBackUsesHistory(previousRoute, currentRoute, | |
66 expectedNavigatePreviousResult) { | |
67 /** | |
68 * Returns a new promise that resolves after a window 'popstate' event. | |
69 * @return {!Promise} | |
70 */ | |
71 function whenPopState() { | |
72 return new Promise(function(resolve) { | |
73 window.addEventListener('popstate', function callback() { | |
74 window.removeEventListener('popstate', callback); | |
75 resolve(); | |
76 }); | |
77 }); | |
78 } | |
79 | |
80 settings.navigateTo(previousRoute); | |
81 settings.navigateTo(currentRoute); | |
82 settings.navigateToPreviousRoute(); | |
83 | |
84 return whenPopState().then(function() { | |
85 assertEquals(expectedNavigatePreviousResult, | |
86 settings.getCurrentRoute()); | |
87 }); | |
88 }; | |
89 | |
90 test('navigate back to parent previous route', function() { | 94 test('navigate back to parent previous route', function() { |
91 return testNavigateBackUsesHistory( | 95 return testNavigateBackUsesHistory( |
92 settings.Route.BASIC, | 96 settings.Route.BASIC, |
93 settings.Route.PEOPLE, | 97 settings.Route.PEOPLE, |
94 settings.Route.BASIC); | 98 settings.Route.BASIC); |
95 }); | 99 }); |
96 | 100 |
97 test('navigate back to non-ancestor shallower route', function() { | 101 test('navigate back to non-ancestor shallower route', function() { |
98 return testNavigateBackUsesHistory( | 102 return testNavigateBackUsesHistory( |
99 settings.Route.ADVANCED, | 103 settings.Route.ADVANCED, |
(...skipping 14 matching lines...) Expand all Loading... |
114 settings.navigateToPreviousRoute(); | 118 settings.navigateToPreviousRoute(); |
115 assertEquals(settings.Route.BASIC, settings.getCurrentRoute()); | 119 assertEquals(settings.Route.BASIC, settings.getCurrentRoute()); |
116 }); | 120 }); |
117 | 121 |
118 test('navigate back to BASIC when going back from root pages', function() { | 122 test('navigate back to BASIC when going back from root pages', function() { |
119 settings.navigateTo(settings.Route.PEOPLE); | 123 settings.navigateTo(settings.Route.PEOPLE); |
120 settings.navigateTo(settings.Route.ADVANCED); | 124 settings.navigateTo(settings.Route.ADVANCED); |
121 settings.navigateToPreviousRoute(); | 125 settings.navigateToPreviousRoute(); |
122 assertEquals(settings.Route.BASIC, settings.getCurrentRoute()); | 126 assertEquals(settings.Route.BASIC, settings.getCurrentRoute()); |
123 }); | 127 }); |
| 128 |
| 129 test('popstate flag works', function() { |
| 130 settings.navigateTo(settings.Route.BASIC); |
| 131 assertFalse(settings.lastRouteChangeWasPopstate()); |
| 132 |
| 133 settings.navigateTo(settings.Route.PEOPLE); |
| 134 assertFalse(settings.lastRouteChangeWasPopstate()); |
| 135 |
| 136 return whenPopState(function() { |
| 137 window.history.back(); |
| 138 }).then(function() { |
| 139 assertEquals(settings.Route.BASIC, settings.getCurrentRoute()); |
| 140 assertTrue(settings.lastRouteChangeWasPopstate()); |
| 141 |
| 142 settings.navigateTo(settings.Route.ADVANCED); |
| 143 assertFalse(settings.lastRouteChangeWasPopstate()); |
| 144 }); |
| 145 }); |
124 }); | 146 }); |
OLD | NEW |