OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 cr.define('extension_navigation_helper_tests', function() { | |
6 /** @enum {string} */ | |
7 var TestNames = { | |
8 Basic: 'basic', | |
9 Conversions: 'conversions', | |
10 PushAndReplaceState: 'push and replace state', | |
11 }; | |
12 | |
13 /** | |
14 * @return {!Promise<void>} A promise that resolves after the next popstate | |
15 * event. | |
16 */ | |
17 function getOnPopState() { | |
18 return new Promise(function(resolve, reject) { | |
19 window.addEventListener('popstate', function listener() { | |
20 window.removeEventListener('popstate', listener); | |
21 // Resolve asynchronously to allow all other listeners to run. | |
22 window.setTimeout(resolve, 0); | |
23 }); | |
24 }); | |
25 } | |
26 | |
27 function registerTests() { | |
28 suite('ExtensionNavigationHelperTest', function() { | |
29 setup(function() { | |
30 PolymerTest.clearBody(); | |
31 Polymer.dom.flush(); | |
32 }); | |
33 | |
34 test(assert(TestNames.Basic), function() { | |
35 var id = 'a'.repeat(32); | |
36 var mock = new MockMethod(); | |
37 var changePage = function(state) { | |
38 mock.recordCall([state]); | |
39 }; | |
40 var navigationHelper = new extensions.NavigationHelper(changePage); | |
41 | |
42 expectEquals('chrome://extensions/navigation_helper.html', | |
43 location.href); | |
44 expectDeepEquals({page: Page.LIST}, navigationHelper.getCurrentPage()); | |
45 | |
46 var currentLength = history.length; | |
47 navigationHelper.updateHistory({page: Page.DETAILS, extensionId: id}); | |
48 expectEquals(++currentLength, history.length); | |
49 | |
50 navigationHelper.updateHistory({page: Page.ERRORS, extensionId: id}); | |
51 expectEquals(++currentLength, history.length); | |
52 | |
53 mock.addExpectation({page: Page.DETAILS, extensionId: id}); | |
54 var waitForPop = getOnPopState(); | |
55 history.back(); | |
56 return waitForPop.then(() => { | |
57 mock.verifyMock(); | |
58 | |
59 mock.addExpectation({page: Page.LIST}); | |
60 waitForNextPop = getOnPopState(); | |
michaelpg
2017/05/02 20:58:10
missing "var"
Devlin
2017/05/04 00:15:54
Done.
| |
61 history.back(); | |
62 return waitForNextPop; | |
63 }).then(() => { | |
64 console.warn('VERIFYING MOCK'); | |
michaelpg
2017/05/02 20:58:10
yes, I promise it runs :-P
Devlin
2017/05/04 00:15:54
Grr... I wonder if there's a good enough heuristic
| |
65 mock.verifyMock(); | |
66 }); | |
67 }); | |
68 | |
69 test(assert(TestNames.Conversions), function() { | |
70 var id = 'a'.repeat(32); | |
71 var stateUrlPairs = { | |
72 list: { | |
73 url: 'chrome://extensions/', | |
74 state: {page: Page.LIST}, | |
75 }, | |
76 details: { | |
77 url: 'chrome://extensions/?id=' + id, | |
78 state: {page: Page.DETAILS, extensionId: id}, | |
79 }, | |
80 options: { | |
81 url: 'chrome://extensions/?options=' + id, | |
82 state: { | |
83 page: Page.DETAILS, | |
84 extensionId: id, | |
85 subpage: Dialog.OPTIONS, | |
86 }, | |
87 }, | |
88 errors: { | |
89 url: 'chrome://extensions/?errors=' + id, | |
90 state: {page: Page.ERRORS, extensionId: id}, | |
91 }, | |
92 shortcuts: { | |
93 url: 'chrome://extensions/shortcuts', | |
94 state: {page: Page.SHORTCUTS}, | |
95 }, | |
96 }; | |
97 | |
98 var navigationHelper = new extensions.NavigationHelper(function() {}); | |
99 | |
100 // Test url -> state. | |
101 for (let key in stateUrlPairs) { | |
102 let entry = stateUrlPairs[key]; | |
103 history.pushState({}, '', entry.url); | |
104 expectDeepEquals(entry.state, navigationHelper.getCurrentPage(), key); | |
105 } | |
106 | |
107 // Test state -> url. | |
108 for (let key in stateUrlPairs) { | |
109 let entry = stateUrlPairs[key]; | |
110 navigationHelper.updateHistory(entry.state); | |
111 expectEquals(entry.url, location.href, key); | |
112 } | |
113 }); | |
114 | |
115 test(assert(TestNames.PushAndReplaceState), function() { | |
116 var id1 = 'a'.repeat(32); | |
117 var id2 = 'b'.repeat(32); | |
118 var navigationHelper = new extensions.NavigationHelper(function() {}); | |
119 | |
120 history.pushState({}, '', 'chrome://extensions/'); | |
121 expectDeepEquals({page: Page.LIST}, navigationHelper.getCurrentPage()); | |
122 | |
123 var expectedLength = history.length; | |
124 | |
125 // Navigating to a new page pushes new state. | |
126 navigationHelper.updateHistory({page: Page.DETAILS, extensionId: id1}); | |
127 expectEquals(++expectedLength, history.length); | |
128 | |
129 // Navigating to a subpage (like the options page) just opens a dialog, | |
130 // and shouldn't push new state. | |
131 navigationHelper.updateHistory( | |
132 {page: Page.DETAILS, extensionId: id1, subpage: Dialog.OPTIONS}); | |
133 expectEquals(expectedLength, history.length); | |
134 | |
135 // Navigating away from a subpage also shouldn't push state (it just | |
136 // closes the dialog). | |
137 navigationHelper.updateHistory({page: Page.DETAILS, extensionId: id1}); | |
138 expectEquals(expectedLength, history.length); | |
139 | |
140 // Navigating away should push new state. | |
141 navigationHelper.updateHistory({page: Page.LIST}); | |
142 expectEquals(++expectedLength, history.length); | |
143 | |
144 // Navigating to a subpage of a different page should push state. | |
145 navigationHelper.updateHistory( | |
146 {page: Page.DETAILS, extensionId: id1, subpage: Dialog.OPTIONS}); | |
147 expectEquals(++expectedLength, history.length); | |
148 | |
149 // Navigating away from a subpage to a page for a different item should | |
150 // push state. | |
151 navigationHelper.updateHistory({page: Page.DETAILS, extensionId: id2}); | |
152 expectEquals(++expectedLength, history.length); | |
153 }); | |
154 }); | |
155 } | |
156 | |
157 return { | |
158 registerTests: registerTests, | |
159 TestNames: TestNames, | |
160 }; | |
161 }); | |
OLD | NEW |