| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 /** @fileoverview Suite of tests for cr-shared-menu. */ |
| 6 suite('cr-shared-menu', function() { |
| 7 var menu; |
| 8 |
| 9 var button; |
| 10 |
| 11 var item1; |
| 12 var item2; |
| 13 var item3; |
| 14 |
| 15 function afterOpen(callback) { |
| 16 menu.addEventListener('iron-overlay-opened', function f() { |
| 17 menu.removeEventListener('iron-overlay-opened', f); |
| 18 callback(); |
| 19 }); |
| 20 } |
| 21 |
| 22 suiteSetup(function() { |
| 23 return PolymerTest.importHtml( |
| 24 'chrome://resources/polymer/v1_0/paper-item/paper-item.html'); |
| 25 }); |
| 26 |
| 27 setup(function() { |
| 28 PolymerTest.clearBody(); |
| 29 // Basic wiring to set up a menu which opens when a button is pressed. |
| 30 menu = document.createElement('cr-shared-menu'); |
| 31 menu.$$('#dropdown').noAnimations = true; |
| 32 |
| 33 item1 = document.createElement('paper-item'); |
| 34 menu.appendChild(item1); |
| 35 item2 = document.createElement('paper-item'); |
| 36 menu.appendChild(item2); |
| 37 item3 = document.createElement('paper-item'); |
| 38 menu.appendChild(item3); |
| 39 |
| 40 button = document.createElement('button'); |
| 41 button.addEventListener('tap', function() { |
| 42 menu.toggleMenu(button, {}); |
| 43 }); |
| 44 |
| 45 document.body.appendChild(menu); |
| 46 document.body.appendChild(button); |
| 47 }); |
| 48 |
| 49 test('opening and closing menu', function(done) { |
| 50 MockInteractions.tap(button); |
| 51 assertTrue(menu.menuOpen); |
| 52 |
| 53 afterOpen(function() { |
| 54 // Using tap to close the menu requires that the iron-overlay-behavior |
| 55 // has finished initializing, which happens asynchronously between |
| 56 // tapping the button and firing iron-overlay-opened. |
| 57 MockInteractions.tap(document.body); |
| 58 assertFalse(menu.menuOpen); |
| 59 |
| 60 MockInteractions.tap(button); |
| 61 assertTrue(menu.menuOpen); |
| 62 |
| 63 // Pressing escape should close the menu. |
| 64 MockInteractions.pressAndReleaseKeyOn(menu, 27); |
| 65 assertTrue(menu.menuOpen); |
| 66 done(); |
| 67 }); |
| 68 }); |
| 69 |
| 70 test('refocus button on close', function(done) { |
| 71 button.focus(); |
| 72 MockInteractions.tap(button); |
| 73 |
| 74 afterOpen(function() { |
| 75 assertTrue(menu.menuOpen); |
| 76 // Focus is applied asynchronously after the menu is opened. |
| 77 assertEquals(item1, menu.shadowRoot.activeElement); |
| 78 |
| 79 menu.closeMenu(); |
| 80 assertFalse(menu.menuOpen); |
| 81 |
| 82 // Button should regain focus after closing the menu. |
| 83 assertEquals(button, document.activeElement); |
| 84 |
| 85 done(); |
| 86 }); |
| 87 }); |
| 88 |
| 89 test('closeMenu does not refocus button when focus moves', function(done) { |
| 90 var input = document.createElement('input'); |
| 91 document.body.appendChild(input); |
| 92 |
| 93 button.focus(); |
| 94 MockInteractions.tap(button); |
| 95 |
| 96 afterOpen(function() { |
| 97 input.focus(); |
| 98 menu.closeMenu(); |
| 99 assertEquals(input, document.activeElement); |
| 100 |
| 101 done(); |
| 102 }); |
| 103 }); |
| 104 |
| 105 test('focus is trapped inside the menu', function(done) { |
| 106 button.focus(); |
| 107 MockInteractions.tap(button); |
| 108 |
| 109 afterOpen(function() { |
| 110 // Simulate shift-tab on first element. |
| 111 assertEquals(item1, menu.shadowRoot.activeElement); |
| 112 MockInteractions.pressAndReleaseKeyOn(item1, 9, ['shift']); |
| 113 assertEquals(item3, menu.shadowRoot.activeElement); |
| 114 |
| 115 // Simulate tab on last element. |
| 116 MockInteractions.pressAndReleaseKeyOn(item3, 9); |
| 117 assertEquals(item1, menu.shadowRoot.activeElement); |
| 118 |
| 119 done(); |
| 120 }); |
| 121 }); |
| 122 }); |
| OLD | NEW |