| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** @fileoverview Suite of tests for cr-shared-menu. */ | 5 /** @fileoverview Suite of tests for cr-shared-menu. */ |
| 6 suite('cr-shared-menu', function() { | 6 suite('cr-shared-menu', function() { |
| 7 var menu; | 7 var menu; |
| 8 | 8 |
| 9 var button; | 9 var button; |
| 10 var button2; |
| 10 | 11 |
| 11 var item1; | 12 var item1; |
| 12 var item2; | 13 var item2; |
| 13 var item3; | 14 var item3; |
| 14 | 15 |
| 15 function afterOpen(callback) { | 16 function afterOpen(callback) { |
| 16 menu.addEventListener('iron-overlay-opened', function f() { | 17 menu.addEventListener('iron-overlay-opened', function f() { |
| 17 menu.removeEventListener('iron-overlay-opened', f); | 18 menu.removeEventListener('iron-overlay-opened', f); |
| 18 callback(); | 19 callback(); |
| 19 }); | 20 }); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 35 item2 = document.createElement('paper-item'); | 36 item2 = document.createElement('paper-item'); |
| 36 menu.appendChild(item2); | 37 menu.appendChild(item2); |
| 37 item3 = document.createElement('paper-item'); | 38 item3 = document.createElement('paper-item'); |
| 38 menu.appendChild(item3); | 39 menu.appendChild(item3); |
| 39 | 40 |
| 40 button = document.createElement('button'); | 41 button = document.createElement('button'); |
| 41 button.addEventListener('tap', function() { | 42 button.addEventListener('tap', function() { |
| 42 menu.toggleMenu(button, {}); | 43 menu.toggleMenu(button, {}); |
| 43 }); | 44 }); |
| 44 | 45 |
| 46 button2 = document.createElement('button'); |
| 47 button2.addEventListener('tap', function() { |
| 48 menu.toggleMenu(button2, {}); |
| 49 }); |
| 50 |
| 45 document.body.appendChild(menu); | 51 document.body.appendChild(menu); |
| 46 document.body.appendChild(button); | 52 document.body.appendChild(button); |
| 53 document.body.appendChild(button2); |
| 47 }); | 54 }); |
| 48 | 55 |
| 49 test('opening and closing menu', function(done) { | 56 test('opening and closing menu', function(done) { |
| 50 MockInteractions.tap(button); | 57 MockInteractions.tap(button); |
| 51 assertTrue(menu.menuOpen); | 58 assertTrue(menu.menuOpen); |
| 52 | 59 |
| 53 afterOpen(function() { | 60 afterOpen(function() { |
| 54 // Using tap to close the menu requires that the iron-overlay-behavior | 61 // Using tap to close the menu requires that the iron-overlay-behavior |
| 55 // has finished initializing, which happens asynchronously between | 62 // has finished initializing, which happens asynchronously between |
| 56 // tapping the button and firing iron-overlay-opened. | 63 // tapping the button and firing iron-overlay-opened. |
| 57 MockInteractions.tap(document.body); | 64 MockInteractions.tap(document.body); |
| 58 assertFalse(menu.menuOpen); | 65 assertFalse(menu.menuOpen); |
| 59 | 66 |
| 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 done(); |
| 67 }); | 68 }); |
| 68 }); | 69 }); |
| 70 |
| 71 test('open and close menu with escape', function(done) { |
| 72 MockInteractions.tap(button); |
| 73 assertTrue(menu.menuOpen); |
| 74 afterOpen(function() { |
| 75 // Pressing escape should close the menu. |
| 76 MockInteractions.pressAndReleaseKeyOn(menu, 27); |
| 77 assertFalse(menu.menuOpen); |
| 78 done(); |
| 79 }); |
| 80 }); |
| 69 | 81 |
| 70 test('refocus button on close', function(done) { | 82 test('refocus button on close', function(done) { |
| 71 button.focus(); | 83 button.focus(); |
| 72 MockInteractions.tap(button); | 84 MockInteractions.tap(button); |
| 73 | 85 |
| 74 afterOpen(function() { | 86 afterOpen(function() { |
| 75 assertTrue(menu.menuOpen); | 87 assertTrue(menu.menuOpen); |
| 76 // Focus is applied asynchronously after the menu is opened. | 88 // Focus is applied asynchronously after the menu is opened. |
| 77 assertEquals(item1, menu.shadowRoot.activeElement); | 89 assertEquals(item1, menu.shadowRoot.activeElement); |
| 78 | 90 |
| 79 menu.closeMenu(); | 91 menu.closeMenu(); |
| 80 assertFalse(menu.menuOpen); | 92 assertFalse(menu.menuOpen); |
| 81 | 93 |
| 82 // Button should regain focus after closing the menu. | 94 // Button should regain focus after closing the menu. |
| 83 assertEquals(button, document.activeElement); | 95 assertEquals(button, document.activeElement); |
| 84 | 96 |
| 85 done(); | 97 done(); |
| 86 }); | 98 }); |
| 87 }); | 99 }); |
| 88 | 100 |
| 101 test('refocus latest button on close', function(done) { |
| 102 // Regression test for crbug.com/628080. |
| 103 button.focus(); |
| 104 MockInteractions.tap(button); |
| 105 |
| 106 afterOpen(function() { |
| 107 button2.focus(); |
| 108 MockInteractions.tap(button2); |
| 109 |
| 110 afterOpen(function() { |
| 111 menu.closeMenu(); |
| 112 assertEquals(button2, document.activeElement); |
| 113 done(); |
| 114 }); |
| 115 }); |
| 116 }); |
| 117 |
| 89 test('closeMenu does not refocus button when focus moves', function(done) { | 118 test('closeMenu does not refocus button when focus moves', function(done) { |
| 90 var input = document.createElement('input'); | 119 var input = document.createElement('input'); |
| 91 document.body.appendChild(input); | 120 document.body.appendChild(input); |
| 92 | 121 |
| 93 button.focus(); | 122 button.focus(); |
| 94 MockInteractions.tap(button); | 123 MockInteractions.tap(button); |
| 95 | 124 |
| 96 afterOpen(function() { | 125 afterOpen(function() { |
| 97 input.focus(); | 126 input.focus(); |
| 98 menu.closeMenu(); | 127 menu.closeMenu(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 113 assertEquals(item3, menu.shadowRoot.activeElement); | 142 assertEquals(item3, menu.shadowRoot.activeElement); |
| 114 | 143 |
| 115 // Simulate tab on last element. | 144 // Simulate tab on last element. |
| 116 MockInteractions.pressAndReleaseKeyOn(item3, 9); | 145 MockInteractions.pressAndReleaseKeyOn(item3, 9); |
| 117 assertEquals(item1, menu.shadowRoot.activeElement); | 146 assertEquals(item1, menu.shadowRoot.activeElement); |
| 118 | 147 |
| 119 done(); | 148 done(); |
| 120 }); | 149 }); |
| 121 }); | 150 }); |
| 122 }); | 151 }); |
| OLD | NEW |