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 var waitForNextPop = getOnPopState(); |
| 61 history.back(); |
| 62 return waitForNextPop; |
| 63 }).then(() => { |
| 64 mock.verifyMock(); |
| 65 }); |
| 66 }); |
| 67 |
| 68 test(assert(TestNames.Conversions), function() { |
| 69 var id = 'a'.repeat(32); |
| 70 var stateUrlPairs = { |
| 71 list: { |
| 72 url: 'chrome://extensions/', |
| 73 state: {page: Page.LIST}, |
| 74 }, |
| 75 details: { |
| 76 url: 'chrome://extensions/?id=' + id, |
| 77 state: {page: Page.DETAILS, extensionId: id}, |
| 78 }, |
| 79 options: { |
| 80 url: 'chrome://extensions/?options=' + id, |
| 81 state: { |
| 82 page: Page.DETAILS, |
| 83 extensionId: id, |
| 84 subpage: Dialog.OPTIONS, |
| 85 }, |
| 86 }, |
| 87 errors: { |
| 88 url: 'chrome://extensions/?errors=' + id, |
| 89 state: {page: Page.ERRORS, extensionId: id}, |
| 90 }, |
| 91 shortcuts: { |
| 92 url: 'chrome://extensions/shortcuts', |
| 93 state: {page: Page.SHORTCUTS}, |
| 94 }, |
| 95 }; |
| 96 |
| 97 var navigationHelper = new extensions.NavigationHelper(function() {}); |
| 98 |
| 99 // Test url -> state. |
| 100 for (let key in stateUrlPairs) { |
| 101 let entry = stateUrlPairs[key]; |
| 102 history.pushState({}, '', entry.url); |
| 103 expectDeepEquals(entry.state, navigationHelper.getCurrentPage(), key); |
| 104 } |
| 105 |
| 106 // Test state -> url. |
| 107 for (let key in stateUrlPairs) { |
| 108 let entry = stateUrlPairs[key]; |
| 109 navigationHelper.updateHistory(entry.state); |
| 110 expectEquals(entry.url, location.href, key); |
| 111 } |
| 112 }); |
| 113 |
| 114 test(assert(TestNames.PushAndReplaceState), function() { |
| 115 var id1 = 'a'.repeat(32); |
| 116 var id2 = 'b'.repeat(32); |
| 117 var navigationHelper = new extensions.NavigationHelper(function() {}); |
| 118 |
| 119 history.pushState({}, '', 'chrome://extensions/'); |
| 120 expectDeepEquals({page: Page.LIST}, navigationHelper.getCurrentPage()); |
| 121 |
| 122 var expectedLength = history.length; |
| 123 |
| 124 // Navigating to a new page pushes new state. |
| 125 navigationHelper.updateHistory({page: Page.DETAILS, extensionId: id1}); |
| 126 expectEquals(++expectedLength, history.length); |
| 127 |
| 128 // Navigating to a subpage (like the options page) just opens a dialog, |
| 129 // and shouldn't push new state. |
| 130 navigationHelper.updateHistory( |
| 131 {page: Page.DETAILS, extensionId: id1, subpage: Dialog.OPTIONS}); |
| 132 expectEquals(expectedLength, history.length); |
| 133 |
| 134 // Navigating away from a subpage also shouldn't push state (it just |
| 135 // closes the dialog). |
| 136 navigationHelper.updateHistory({page: Page.DETAILS, extensionId: id1}); |
| 137 expectEquals(expectedLength, history.length); |
| 138 |
| 139 // Navigating away should push new state. |
| 140 navigationHelper.updateHistory({page: Page.LIST}); |
| 141 expectEquals(++expectedLength, history.length); |
| 142 |
| 143 // Navigating to a subpage of a different page should push state. |
| 144 navigationHelper.updateHistory( |
| 145 {page: Page.DETAILS, extensionId: id1, subpage: Dialog.OPTIONS}); |
| 146 expectEquals(++expectedLength, history.length); |
| 147 |
| 148 // Navigating away from a subpage to a page for a different item should |
| 149 // push state. |
| 150 navigationHelper.updateHistory({page: Page.DETAILS, extensionId: id2}); |
| 151 expectEquals(++expectedLength, history.length); |
| 152 }); |
| 153 }); |
| 154 } |
| 155 |
| 156 return { |
| 157 registerTests: registerTests, |
| 158 TestNames: TestNames, |
| 159 }; |
| 160 }); |
OLD | NEW |