Home | History | Annotate | Download | only in base
      1 <!DOCTYPE html>
      2 <!--
      3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
      4 Use of this source code is governed by a BSD-style license that can be
      5 found in the LICENSE file.
      6 -->
      7 
      8 <link rel="import" href="/tracing/base/event.html">
      9 <link rel="import" href="/tracing/ui/base/hotkey_controller.html">
     10 
     11 <script>
     12 'use strict';
     13 
     14 tr.b.unittest.testSuite(function() {
     15   var KeyEventManager = tr.b.KeyEventManager;
     16 
     17   function newKeyEvent(eventType, dict) {
     18     var e = new tr.b.Event(eventType, true, true);
     19     if (dict.keyCode === undefined)
     20       throw new Error('keyCode required');
     21     e.keyCode = dict.keyCode;
     22     return e;
     23   }
     24 
     25   test('simpleHotkeyManager', function() {
     26     var rootElement = document.createElement('div');
     27     document.body.appendChild(rootElement);
     28     try {
     29       var elementShadow = rootElement.createShadowRoot();
     30 
     31       var hkc = document.createElement('tv-ui-b-hotkey-controller');
     32       elementShadow.appendChild(hkc);
     33 
     34       var subElement = document.createElement('div');
     35       elementShadow.appendChild(subElement);
     36 
     37       assert.equal(tr.b.getHotkeyControllerForElement(subElement), hkc);
     38 
     39       var didGetCalled = false;
     40       hkc.addHotKey(new tr.ui.b.HotKey({
     41         eventType: 'keydown',
     42         keyCode: 73, useCapture: true,
     43         callback: function() {
     44           didGetCalled = true;
     45         }
     46       }));
     47 
     48       // Ensure it is called when events target the root element.
     49       var e = newKeyEvent('keydown', {keyCode: 73});
     50       rootElement.dispatchEvent(e);
     51       assert.isTrue(didGetCalled);
     52 
     53       // Ensure it is still called when we target the sub element.
     54       didGetCalled = false;
     55       var e = newKeyEvent('keydown', {keyCode: 73});
     56       subElement.dispatchEvent(e);
     57       assert.isTrue(didGetCalled);
     58 
     59     } finally {
     60       document.body.removeChild(rootElement);
     61     }
     62   });
     63 
     64   test('nestedHotkeyController', function() {
     65     var rootElement = document.createElement('div');
     66     document.body.appendChild(rootElement);
     67     try {
     68       var elementShadow = rootElement.createShadowRoot();
     69 
     70       var hkc = document.createElement('tv-ui-b-hotkey-controller');
     71       elementShadow.appendChild(hkc);
     72 
     73       var subElement = document.createElement('div');
     74       elementShadow.appendChild(subElement);
     75       assert.equal(tr.b.getHotkeyControllerForElement(elementShadow), hkc);
     76 
     77       var subHKC = document.createElement('tv-ui-b-hotkey-controller');
     78       subElement.appendChild(subHKC);
     79 
     80       assert.equal(tr.b.getHotkeyControllerForElement(subElement), subHKC);
     81 
     82       var didGetCalled = false;
     83       subHKC.addHotKey(new tr.ui.b.HotKey({
     84         eventType: 'keydown',
     85         keyCode: 73, useCapture: true,
     86         callback: function() {
     87           didGetCalled = true;
     88         }
     89       }));
     90 
     91       // Ensure it is called when events target the root element.
     92       var e = newKeyEvent('keydown', {keyCode: 73});
     93       rootElement.dispatchEvent(e);
     94       assert.isTrue(didGetCalled);
     95     } finally {
     96       document.body.removeChild(rootElement);
     97     }
     98   });
     99 
    100   test('inputInsideHKC', function() {
    101     var rootElement = document.createElement('div');
    102     document.body.appendChild(rootElement);
    103     try {
    104       var elementShadow = rootElement.createShadowRoot();
    105 
    106       var hkc = document.createElement('tv-ui-b-hotkey-controller');
    107       elementShadow.appendChild(hkc);
    108 
    109       var inputEl = document.createElement('input');
    110       elementShadow.appendChild(inputEl);
    111 
    112       var didGetCalled = false;
    113       hkc.addHotKey(new tr.ui.b.HotKey({
    114         eventType: 'keypress',
    115         keyCode: 'a'.charCodeAt(0), useCapture: false,
    116         callback: function() {
    117           didGetCalled = true;
    118         }
    119       }));
    120 
    121       // Ensure it is called when events target the root element.
    122       didGetCalled = false;
    123       var e = newKeyEvent('keypress', {keyCode: 'a'.charCodeAt(0)});
    124       rootElement.dispatchEvent(e);
    125       assert.isTrue(didGetCalled);
    126 
    127       // Handler should NOT be called when events target the input element.
    128       didGetCalled = false;
    129       var e = newKeyEvent('keypress', {keyCode: 'a'.charCodeAt(0)});
    130       inputEl.dispatchEvent(e);
    131       assert.isFalse(didGetCalled);
    132     } finally {
    133       document.body.removeChild(rootElement);
    134     }
    135   });
    136 
    137 });
    138 </script>
    139