Chromium Code Reviews| 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(done) { | |
| 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, id: id}); | |
| 48 expectEquals(++currentLength, history.length); | |
| 49 | |
| 50 navigationHelper.updateHistory({page: Page.ERRORS, id: id}); | |
| 51 expectEquals(++currentLength, history.length); | |
| 52 | |
| 53 mock.addExpectation({page: Page.DETAILS, id: id}); | |
| 54 var waitForPop = getOnPopState(); | |
| 55 history.back(); | |
| 56 waitForPop.then(() => { | |
|
michaelpg
2017/05/01 23:41:26
I think returning this Promise will let you elimin
Devlin
2017/05/02 01:04:07
Done.
| |
| 57 mock.verifyMock(); | |
| 58 | |
| 59 mock.addExpectation({page: Page.LIST}); | |
| 60 waitForPop = getOnPopState(); | |
|
michaelpg
2017/05/01 23:41:26
setting a variable in a function passed as an argu
Devlin
2017/05/02 01:04:07
waitFor*Next*Pop? :)
| |
| 61 history.back(); | |
| 62 return waitForPop; | |
| 63 }).then(() => { | |
| 64 mock.verifyMock(); | |
| 65 done(); | |
| 66 }).catch(done); | |
| 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, id: id}, | |
| 79 }, | |
| 80 options: { | |
| 81 url: 'chrome://extensions/?options=' + id, | |
| 82 state: {page: Page.DETAILS, id: id, subpage: Dialog.OPTIONS}, | |
| 83 }, | |
| 84 errors: { | |
| 85 url: 'chrome://extensions/?errors=' + id, | |
| 86 state: {page: Page.ERRORS, id: id}, | |
| 87 }, | |
| 88 shortcuts: { | |
| 89 url: 'chrome://extensions/shortcuts', | |
| 90 state: {page: Page.SHORTCUTS}, | |
| 91 }, | |
| 92 }; | |
| 93 | |
| 94 var navigationHelper = new extensions.NavigationHelper(function() {}); | |
| 95 | |
| 96 // Test url -> state. | |
| 97 for (let key in stateUrlPairs) { | |
| 98 let entry = stateUrlPairs[key]; | |
| 99 history.pushState({}, '', entry.url); | |
| 100 var currentPage = navigationHelper.getCurrentPage(); | |
|
michaelpg
2017/05/01 23:41:26
unused? also, mixing var/let?
Devlin
2017/05/02 01:04:07
Removed.
| |
| 101 expectDeepEquals(entry.state, navigationHelper.getCurrentPage(), key); | |
| 102 } | |
| 103 | |
| 104 // Test state -> url. | |
| 105 for (let key in stateUrlPairs) { | |
| 106 let entry = stateUrlPairs[key]; | |
| 107 navigationHelper.updateHistory(entry.state); | |
| 108 expectEquals(entry.url, location.href, key); | |
| 109 } | |
| 110 }); | |
| 111 | |
| 112 test(assert(TestNames.PushAndReplaceState), function() { | |
| 113 var id1 = 'a'.repeat(32); | |
| 114 var id2 = 'b'.repeat(32); | |
| 115 var navigationHelper = new extensions.NavigationHelper(function() {}); | |
| 116 | |
| 117 history.pushState({}, '', 'chrome://extensions/'); | |
| 118 expectDeepEquals({page: Page.LIST}, navigationHelper.getCurrentPage()); | |
| 119 | |
| 120 var expectedLength = history.length; | |
| 121 | |
| 122 // Navigating to a new page pushes new state. | |
| 123 navigationHelper.updateHistory({page: Page.DETAILS, id: id1}); | |
| 124 expectEquals(++expectedLength, history.length); | |
| 125 | |
| 126 // Navigating to a subpage (like the options page) just opens a dialog, | |
| 127 // and shouldn't push new state. | |
| 128 navigationHelper.updateHistory( | |
| 129 {page: Page.DETAILS, id: id1, subpage: Dialog.OPTIONS}); | |
| 130 expectEquals(expectedLength, history.length); | |
| 131 | |
| 132 // Navigating away from a subpage also shouldn't push state (it just | |
| 133 // closes the dialog). | |
| 134 navigationHelper.updateHistory({page: Page.DETAILS, id: id1}); | |
| 135 expectEquals(expectedLength, history.length); | |
| 136 | |
| 137 // Navigating away should push new state. | |
| 138 navigationHelper.updateHistory({page: Page.LIST}); | |
| 139 expectEquals(++expectedLength, history.length); | |
| 140 | |
| 141 // Navigating to a subpage of a different page should push state. | |
| 142 navigationHelper.updateHistory( | |
| 143 {page: Page.DETAILS, id: id1, subpage: Dialog.OPTIONS}); | |
| 144 expectEquals(++expectedLength, history.length); | |
| 145 | |
| 146 // Navigating away from a subpage to a page for a different item should | |
| 147 // push state. | |
| 148 navigationHelper.updateHistory({page: Page.DETAILS, id: id2}); | |
| 149 expectEquals(++expectedLength, history.length); | |
| 150 }); | |
| 151 }); | |
| 152 } | |
| 153 | |
| 154 return { | |
| 155 registerTests: registerTests, | |
| 156 TestNames: TestNames, | |
| 157 }; | |
| 158 }); | |
| OLD | NEW |