| Index: chrome/test/data/webui/extensions/extension_page_state_test.js
|
| diff --git a/chrome/test/data/webui/extensions/extension_page_state_test.js b/chrome/test/data/webui/extensions/extension_page_state_test.js
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..21e47e9662fe00e9a2be09fe30cc7e8bfccc0445
|
| --- /dev/null
|
| +++ b/chrome/test/data/webui/extensions/extension_page_state_test.js
|
| @@ -0,0 +1,157 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +cr.define('extension_page_state_tests', function() {
|
| + /** @enum {string} */
|
| + var TestNames = {
|
| + Basic: 'basic',
|
| + Conversions: 'conversions',
|
| + PushAndReplaceState: 'push and replace state',
|
| + };
|
| +
|
| + /**
|
| + * @return {!Promise<void>} A promise that resolves after the next popstate
|
| + * event.
|
| + */
|
| + function getOnPopState() {
|
| + return new Promise(function(resolve, reject) {
|
| + window.addEventListener('popstate', function listener() {
|
| + window.removeEventListener('popstate', listener);
|
| + // Resolve asynchronously to allow all other listeners to run.
|
| + window.setTimeout(resolve, 0);
|
| + });
|
| + });
|
| + }
|
| +
|
| + function registerTests() {
|
| + suite('ExtensionPageStateTest', function() {
|
| + setup(function() {
|
| + PolymerTest.clearBody();
|
| + Polymer.dom.flush();
|
| + });
|
| +
|
| + test(assert(TestNames.Basic), function(done) {
|
| + var id = 'a'.repeat(32);
|
| + var mock = new MockMethod();
|
| + var changePage = function(state) {
|
| + mock.recordCall([state]);
|
| + };
|
| + var pageState = new extensions.PageState(changePage);
|
| +
|
| + expectEquals('chrome://extensions/page_state.html', location.href);
|
| + expectDeepEquals({page: Page.LIST}, pageState.getCurrentPage());
|
| +
|
| + var currentLength = history.length;
|
| + pageState.onPageChange({page: Page.DETAILS, id: id});
|
| + expectEquals(++currentLength, history.length);
|
| +
|
| + pageState.onPageChange({page: Page.ERRORS, id: id});
|
| + expectEquals(++currentLength, history.length);
|
| +
|
| + mock.addExpectation({page: Page.DETAILS, id: id});
|
| + var waitForPop = getOnPopState();
|
| + history.back();
|
| + waitForPop.then(() => {
|
| + mock.verifyMock();
|
| +
|
| + mock.addExpectation({page: Page.LIST});
|
| + waitForPop = getOnPopState();
|
| + history.back();
|
| + return waitForPop;
|
| + }).then(() => {
|
| + mock.verifyMock();
|
| + done();
|
| + }).catch(done);
|
| + });
|
| +
|
| + test(assert(TestNames.Conversions), function() {
|
| + var id = 'a'.repeat(32);
|
| + var stateUrlPairs = {
|
| + list: {
|
| + url: 'chrome://extensions/',
|
| + state: {page: Page.LIST},
|
| + },
|
| + details: {
|
| + url: 'chrome://extensions/?id=' + id,
|
| + state: {page: Page.DETAILS, id: id},
|
| + },
|
| + options: {
|
| + url: 'chrome://extensions/?options=' + id,
|
| + state: {page: Page.DETAILS, id: id, subpage: Subpage.OPTIONS},
|
| + },
|
| + errors: {
|
| + url: 'chrome://extensions/?errors=' + id,
|
| + state: {page: Page.ERRORS, id: id},
|
| + },
|
| + shortcuts: {
|
| + url: 'chrome://extensions/shortcuts',
|
| + state: {page: Page.SHORTCUTS},
|
| + },
|
| + };
|
| +
|
| + var pageState = new extensions.PageState(function() {});
|
| +
|
| + // Test url -> state.
|
| + for (let key in stateUrlPairs) {
|
| + let entry = stateUrlPairs[key];
|
| + history.pushState({}, '', entry.url);
|
| + var currentPage = pageState.getCurrentPage();
|
| + expectDeepEquals(entry.state, pageState.getCurrentPage(), key);
|
| + }
|
| +
|
| + // Test state -> url.
|
| + for (let key in stateUrlPairs) {
|
| + let entry = stateUrlPairs[key];
|
| + pageState.onPageChange(entry.state);
|
| + expectEquals(entry.url, location.href, key);
|
| + }
|
| + });
|
| +
|
| + test(assert(TestNames.PushAndReplaceState), function() {
|
| + var id1 = 'a'.repeat(32);
|
| + var id2 = 'b'.repeat(32);
|
| + var pageState = new extensions.PageState(function() {});
|
| +
|
| + history.pushState({}, '', 'chrome://extensions/');
|
| + expectDeepEquals({page: Page.LIST}, pageState.getCurrentPage());
|
| +
|
| + var expectedLength = history.length;
|
| +
|
| + // Navigating to a new page pushes new state.
|
| + pageState.onPageChange({page: Page.DETAILS, id: id1});
|
| + expectEquals(++expectedLength, history.length);
|
| +
|
| + // Navigating to a subpage (like the options page) just opens a dialog,
|
| + // and shouldn't push new state.
|
| + pageState.onPageChange(
|
| + {page: Page.DETAILS, id: id1, subpage: Subpage.OPTIONS});
|
| + expectEquals(expectedLength, history.length);
|
| +
|
| + // Navigating away from a subpage also shouldn't push state (it just
|
| + // closes the dialog).
|
| + pageState.onPageChange({page: Page.DETAILS, id: id1});
|
| + expectEquals(expectedLength, history.length);
|
| +
|
| + // Navigating away should push new state.
|
| + pageState.onPageChange({page: Page.LIST});
|
| + expectEquals(++expectedLength, history.length);
|
| +
|
| + // Navigating to a subpage of a different page should push state.
|
| + pageState.onPageChange(
|
| + {page: Page.DETAILS, id: id1, subpage: Subpage.OPTIONS});
|
| + expectEquals(++expectedLength, history.length);
|
| +
|
| + // Navigating away from a subpage to a page for a different item should
|
| + // push state.
|
| + pageState.onPageChange({page: Page.DETAILS, id: id2});
|
| + expectEquals(++expectedLength, history.length);
|
| + });
|
| + });
|
| + }
|
| +
|
| + return {
|
| + registerTests: registerTests,
|
| + TestNames: TestNames,
|
| + };
|
| +});
|
|
|