gerrit/polygerrit-ui/app/behaviors/keyboard-shortcut-behavior_test.html
Logan Hanks 610f80d3ec Add namespacing for keyboard shortcut disabling
The gr-overlay element attempts to manage disabling and enabling
keyboard shortcuts. When multiple gr-overlay elements are available
on a page and one of them opens immediately, that overlay tries to
disable keyboard shortcuts, but the other elements initialize as
closed and enable them.

This change offers a new method for disabling keyboard shortcuts.
The caller can pass in an identifier to enable or disable. If keyboard
shortcuts are disabled by one or more identifiers, then they are
suppressed.

Change-Id: I82fe6efd922f09279e76a2f2c8cb5781f3afe395
2016-10-14 14:05:43 -07:00

79 lines
2.7 KiB
HTML

<!DOCTYPE html>
<!--
Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>keyboard-shortcut-behavior</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="keyboard-shortcut-behavior.html">
<test-fixture id="basic">
<template>
<test-element></test-element>
</template>
</test-fixture>
<script>
suite('keyboard-shortcut-behavior tests', function() {
var element;
suiteSetup(function() {
// Define a Polymer element that uses this behavior.
Polymer({
is: 'test-element',
behaviors: [Gerrit.KeyboardShortcutBehavior],
properties: {
keyEventTarget: {
value: function() { return document.body; },
},
log: {
value: function() { return []; },
},
},
_handleKey: function(e) {
if (!this.shouldSuppressKeyboardShortcut(e)) {
this.log.push(e.keyCode);
}
},
});
});
setup(function() {
element = fixture('basic');
});
test('blocks keydown events iff one or more disablers', function() {
MockInteractions.pressAndReleaseKeyOn(document.body, 97); // 'a'
Gerrit.KeyboardShortcutBehavior.enable('x'); // should have no effect
MockInteractions.pressAndReleaseKeyOn(document.body, 98); // 'b'
Gerrit.KeyboardShortcutBehavior.disable('x'); // blocking starts here
MockInteractions.pressAndReleaseKeyOn(document.body, 99); // 'c'
Gerrit.KeyboardShortcutBehavior.disable('y');
MockInteractions.pressAndReleaseKeyOn(document.body, 100); // 'd'
Gerrit.KeyboardShortcutBehavior.enable('x');
MockInteractions.pressAndReleaseKeyOn(document.body, 101); // 'e'
Gerrit.KeyboardShortcutBehavior.enable('y'); // blocking ends here
MockInteractions.pressAndReleaseKeyOn(document.body, 102); // 'f'
assert.deepEqual(element.log, [97, 98, 102]);
});
});
</script>