Coverage Report - org.eclipse.swtbot.swt.finder.keyboard.SWTKeyboardStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTKeyboardStrategy
100%
56/56
100%
6/6
1.857
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2009 SWTBot Committers and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  * 
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.keyboard;
 12  
 
 13  
 import java.util.HashSet;
 14  
 import java.util.Set;
 15  
 
 16  
 import org.eclipse.jface.bindings.keys.KeyStroke;
 17  
 import org.eclipse.swt.SWT;
 18  
 import org.eclipse.swt.widgets.Display;
 19  
 import org.eclipse.swt.widgets.Event;
 20  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 21  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 22  
 
 23  
 /**
 24  
  * Sends keyboard notifications using {@link Display#post(Event)}.
 25  
  * 
 26  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 27  
  * @version $Id$
 28  
  * @see Display#KeyTable
 29  
  */
 30  1
 class SWTKeyboardStrategy extends AbstractKeyboardStrategy {
 31  
 
 32  
         private final Display                                display;
 33  
 
 34  1
         private static final Set<Integer>        specialKeys        = new HashSet<Integer>();
 35  
 
 36  51
         SWTKeyboardStrategy() {
 37  51
                 this.display = SWTUtils.display();
 38  51
         }
 39  
 
 40  
         public void pressKey(KeyStroke key) {
 41  50
                 Assert.isTrue(display.post(keyEvent(key, SWT.KeyDown)), "Could not post keyevent.");
 42  50
                 display.wake();
 43  50
         }
 44  
 
 45  
         public void releaseKey(KeyStroke key) {
 46  50
                 Assert.isTrue(display.post(keyEvent(key, SWT.KeyUp)), "Could not post keyevent.");
 47  50
                 display.wake();
 48  50
         }
 49  
 
 50  
         private Event keyEvent(KeyStroke key, int type) {
 51  100
                 Event e = new Event();
 52  100
                 e.type = type;
 53  100
                 e.keyCode = keycode(key);
 54  100
                 e.character = character(key);
 55  100
                 return e;
 56  
         }
 57  
 
 58  
         private char character(KeyStroke key) {
 59  100
                 int naturalKey = key.getNaturalKey();
 60  100
                 if (specialKeys.contains(naturalKey))
 61  12
                         return 0;
 62  88
                 return (char) naturalKey;
 63  
         }
 64  
 
 65  
         private int keycode(KeyStroke key) {
 66  100
                 int naturalKey = key.getNaturalKey();
 67  100
                 int modifierKeys = key.getModifierKeys();
 68  
                 // if it is a modifier key
 69  100
                 if (modifierKeys != 0)
 70  80
                         return modifierKeys;
 71  
                 // if it is a special key
 72  20
                 if (specialKeys.contains(naturalKey))
 73  12
                         return naturalKey;
 74  8
                 return 0;
 75  
         }
 76  
 
 77  
         /* these keys are special and translated using Display#KeyTable. */
 78  
         static {
 79  
                 /* function keys */
 80  1
                 addSpecialKeyMapping(SWT.F1);
 81  1
                 addSpecialKeyMapping(SWT.F2);
 82  1
                 addSpecialKeyMapping(SWT.F3);
 83  1
                 addSpecialKeyMapping(SWT.F4);
 84  1
                 addSpecialKeyMapping(SWT.F5);
 85  1
                 addSpecialKeyMapping(SWT.F6);
 86  1
                 addSpecialKeyMapping(SWT.F7);
 87  1
                 addSpecialKeyMapping(SWT.F8);
 88  1
                 addSpecialKeyMapping(SWT.F9);
 89  1
                 addSpecialKeyMapping(SWT.F10);
 90  1
                 addSpecialKeyMapping(SWT.F11);
 91  1
                 addSpecialKeyMapping(SWT.F12);
 92  
 
 93  1
                 addSpecialKeyMapping(SWT.DEL);
 94  
 
 95  
                 /* direction and page navigation keys */
 96  1
                 addSpecialKeyMapping(SWT.HOME);
 97  1
                 addSpecialKeyMapping(SWT.END);
 98  1
                 addSpecialKeyMapping(SWT.PAGE_UP);
 99  1
                 addSpecialKeyMapping(SWT.PAGE_DOWN);
 100  1
                 addSpecialKeyMapping(SWT.ARROW_RIGHT);
 101  1
                 addSpecialKeyMapping(SWT.ARROW_DOWN);
 102  1
                 addSpecialKeyMapping(SWT.ARROW_LEFT);
 103  1
                 addSpecialKeyMapping(SWT.ARROW_UP);
 104  
 
 105  
                 /* whitespace keys */
 106  1
                 addSpecialKeyMapping(SWT.BS);
 107  1
                 addSpecialKeyMapping(SWT.CR);
 108  1
                 addSpecialKeyMapping(SWT.DEL);
 109  1
                 addSpecialKeyMapping(SWT.ESC);
 110  1
                 addSpecialKeyMapping(SWT.LF);
 111  1
                 addSpecialKeyMapping(SWT.TAB);
 112  
         }
 113  
 
 114  
         private static void addSpecialKeyMapping(int swtKey) {
 115  27
                 specialKeys.add(swtKey);
 116  27
         }
 117  
 
 118  
 }