Chromium Code Reviews| Index: chrome/test/data/webui/cr_elements/cr_shared_menu_tests.js |
| diff --git a/chrome/test/data/webui/cr_elements/cr_shared_menu_tests.js b/chrome/test/data/webui/cr_elements/cr_shared_menu_tests.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c44a866b8497e77a4d9eff3b2394f08479e51ccb |
| --- /dev/null |
| +++ b/chrome/test/data/webui/cr_elements/cr_shared_menu_tests.js |
| @@ -0,0 +1,128 @@ |
| +// Copyright 2016 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. |
| + |
| +/** @fileoverview Suite of tests for cr-shared-menu. */ |
| +cr.define('cr_shared_menu', function() { |
| + function registerTests() { |
| + suite('cr-shared-menu', function() { |
| + var menu; |
| + |
| + var button; |
| + |
| + var item1; |
| + var item2; |
| + var item3; |
| + |
| + function afterOpen(callback) { |
| + menu.addEventListener('iron-overlay-opened', callback); |
| + } |
| + |
| + suiteSetup(function() { |
| + return Promise.all([ |
| + PolymerTest.importHtml( |
| + 'chrome://resources/polymer/v1_0/paper-item/paper-item.html'), |
| + PolymerTest.importHtml( |
| + 'chrome://resources/cr_elements/icons.html'), |
|
Dan Beam
2016/07/07 17:38:24
why do you need this?
tsergeant
2016/07/11 03:30:48
Oops, leftover from a previous version of the test
|
| + ]); |
| + }); |
| + |
| + setup(function() { |
| + PolymerTest.clearBody(); |
| + // Basic wiring to set up a menu which opens when a button is pressed. |
| + menu = document.createElement('cr-shared-menu'); |
| + menu.$$('#dropdown').noAnimations = true; |
| + |
| + item1 = document.createElement('paper-item'); |
| + menu.appendChild(item1); |
| + item2 = document.createElement('paper-item'); |
| + menu.appendChild(item2); |
| + item3 = document.createElement('paper-item'); |
| + menu.appendChild(item3); |
| + |
| + button = document.createElement('button'); |
| + button.addEventListener('tap', function() { |
| + menu.toggleMenu(button, {}); |
| + }); |
| + |
| + document.body.appendChild(menu); |
| + document.body.appendChild(button); |
| + }); |
| + |
| + test('opening and closing menu', function(done) { |
| + MockInteractions.tap(button); |
| + assertTrue(menu.menuOpen); |
| + |
| + afterOpen(function() { |
| + // Using tap to close the menu requires that the iron-overlay-behavior |
| + // has finished initializing, which happens asynchronously between |
| + // tapping the button and firing iron-overlay-opened. |
| + MockInteractions.tap(document.body); |
| + assertFalse(menu.menuOpen); |
| + |
| + MockInteractions.tap(button); |
| + assertTrue(menu.menuOpen); |
| + |
| + // Pressing escape should close the menu. |
| + MockInteractions.pressAndReleaseKeyOn(menu, 27); |
| + assertTrue(menu.menuOpen); |
| + done(); |
| + }); |
| + }); |
| + |
| + test('refocus button on close', function(done) { |
| + button.focus(); |
| + MockInteractions.tap(button); |
| + |
| + afterOpen(function() { |
| + assertTrue(menu.menuOpen); |
| + // Focus is applied asynchronously after the menu is opened. |
| + assertEquals(item1, menu.shadowRoot.activeElement); |
| + |
| + menu.closeMenu(); |
| + assertFalse(menu.menuOpen); |
| + |
| + // Button should regain focus after closing the menu. |
| + assertEquals(button, document.activeElement); |
| + |
| + done(); |
| + }); |
| + }); |
| + |
| + test('closeMenuNoRefocus does not refocus button', function(done) { |
| + button.focus(); |
| + MockInteractions.tap(button); |
| + |
| + afterOpen(function() { |
| + menu.closeMenuNoRefocus(); |
| + assertNotEquals(button, document.activeElement); |
| + |
| + done(); |
| + }); |
| + }); |
| + |
| + test('focus is trapped inside the menu', function(done) { |
| + button.focus(); |
| + MockInteractions.tap(button); |
| + |
| + afterOpen(function() { |
| + // Simulate shift-tab on first element. |
| + assertEquals(item1, menu.shadowRoot.activeElement); |
| + MockInteractions.pressAndReleaseKeyOn(item1, 9, ['shift']); |
| + assertEquals(item3, menu.shadowRoot.activeElement); |
| + |
| + // Simulate tab on last element. |
| + MockInteractions.pressAndReleaseKeyOn(item3, 9); |
| + assertEquals(item1, menu.shadowRoot.activeElement); |
| + |
| + done(); |
| + }); |
| + }); |
| + |
| + }); |
| + } |
| + |
| + return { |
| + registerTests: registerTests, |
| + }; |
| +}); |