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_page_state_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('ExtensionPageStateTest', 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 pageState = new extensions.PageState(changePage); |
| 41 |
| 42 expectEquals('chrome://extensions/page_state.html', location.href); |
| 43 expectDeepEquals({page: Page.LIST}, pageState.getCurrentPage()); |
| 44 |
| 45 var currentLength = history.length; |
| 46 pageState.onPageChange({page: Page.DETAILS, id: id}); |
| 47 expectEquals(++currentLength, history.length); |
| 48 |
| 49 pageState.onPageChange({page: Page.ERRORS, id: id}); |
| 50 expectEquals(++currentLength, history.length); |
| 51 |
| 52 mock.addExpectation({page: Page.DETAILS, id: id}); |
| 53 var waitForPop = getOnPopState(); |
| 54 history.back(); |
| 55 waitForPop.then(() => { |
| 56 mock.verifyMock(); |
| 57 |
| 58 mock.addExpectation({page: Page.LIST}); |
| 59 waitForPop = getOnPopState(); |
| 60 history.back(); |
| 61 return waitForPop; |
| 62 }).then(() => { |
| 63 mock.verifyMock(); |
| 64 done(); |
| 65 }).catch(done); |
| 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, id: id}, |
| 78 }, |
| 79 options: { |
| 80 url: 'chrome://extensions/?options=' + id, |
| 81 state: {page: Page.DETAILS, id: id, subpage: Subpage.OPTIONS}, |
| 82 }, |
| 83 errors: { |
| 84 url: 'chrome://extensions/?errors=' + id, |
| 85 state: {page: Page.ERRORS, id: id}, |
| 86 }, |
| 87 shortcuts: { |
| 88 url: 'chrome://extensions/shortcuts', |
| 89 state: {page: Page.SHORTCUTS}, |
| 90 }, |
| 91 }; |
| 92 |
| 93 var pageState = new extensions.PageState(function() {}); |
| 94 |
| 95 // Test url -> state. |
| 96 for (let key in stateUrlPairs) { |
| 97 let entry = stateUrlPairs[key]; |
| 98 history.pushState({}, '', entry.url); |
| 99 var currentPage = pageState.getCurrentPage(); |
| 100 expectDeepEquals(entry.state, pageState.getCurrentPage(), key); |
| 101 } |
| 102 |
| 103 // Test state -> url. |
| 104 for (let key in stateUrlPairs) { |
| 105 let entry = stateUrlPairs[key]; |
| 106 pageState.onPageChange(entry.state); |
| 107 expectEquals(entry.url, location.href, key); |
| 108 } |
| 109 }); |
| 110 |
| 111 test(assert(TestNames.PushAndReplaceState), function() { |
| 112 var id1 = 'a'.repeat(32); |
| 113 var id2 = 'b'.repeat(32); |
| 114 var pageState = new extensions.PageState(function() {}); |
| 115 |
| 116 history.pushState({}, '', 'chrome://extensions/'); |
| 117 expectDeepEquals({page: Page.LIST}, pageState.getCurrentPage()); |
| 118 |
| 119 var expectedLength = history.length; |
| 120 |
| 121 // Navigating to a new page pushes new state. |
| 122 pageState.onPageChange({page: Page.DETAILS, id: id1}); |
| 123 expectEquals(++expectedLength, history.length); |
| 124 |
| 125 // Navigating to a subpage (like the options page) just opens a dialog, |
| 126 // and shouldn't push new state. |
| 127 pageState.onPageChange( |
| 128 {page: Page.DETAILS, id: id1, subpage: Subpage.OPTIONS}); |
| 129 expectEquals(expectedLength, history.length); |
| 130 |
| 131 // Navigating away from a subpage also shouldn't push state (it just |
| 132 // closes the dialog). |
| 133 pageState.onPageChange({page: Page.DETAILS, id: id1}); |
| 134 expectEquals(expectedLength, history.length); |
| 135 |
| 136 // Navigating away should push new state. |
| 137 pageState.onPageChange({page: Page.LIST}); |
| 138 expectEquals(++expectedLength, history.length); |
| 139 |
| 140 // Navigating to a subpage of a different page should push state. |
| 141 pageState.onPageChange( |
| 142 {page: Page.DETAILS, id: id1, subpage: Subpage.OPTIONS}); |
| 143 expectEquals(++expectedLength, history.length); |
| 144 |
| 145 // Navigating away from a subpage to a page for a different item should |
| 146 // push state. |
| 147 pageState.onPageChange({page: Page.DETAILS, id: id2}); |
| 148 expectEquals(++expectedLength, history.length); |
| 149 }); |
| 150 }); |
| 151 } |
| 152 |
| 153 return { |
| 154 registerTests: registerTests, |
| 155 TestNames: TestNames, |
| 156 }; |
| 157 }); |
OLD | NEW |