Coverage Report - org.eclipse.swtbot.swt.finder.keyboard.SWTKeyboardStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTKeyboardStrategy
57%
34/59
0%
0/6
1.75
 
 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.swt.widgets.Widget;
 21  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 22  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 23  
 import org.hamcrest.SelfDescribing;
 24  
 
 25  
 /**
 26  
  * Sends keyboard notifications using {@link Display#post(Event)}.
 27  
  * 
 28  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 29  
  * @version $Id$
 30  
  * @see Display#KeyTable
 31  
  */
 32  1
 class SWTKeyboardStrategy extends AbstractKeyboardStrategy {
 33  
 
 34  
         private final Display                                display;
 35  
 
 36  
         private Widget        widget;
 37  
 
 38  1
         private static final Set<Integer>        specialKeys        = new HashSet<Integer>();
 39  
 
 40  1
         SWTKeyboardStrategy() {
 41  1
                 this.display = SWTUtils.display();
 42  1
         }
 43  
 
 44  
         @Override
 45  
         public void init(Widget widget, SelfDescribing description) {
 46  0
                 this.widget = widget;
 47  0
         }
 48  
         
 49  
         public void pressKey(KeyStroke key) {
 50  0
                 Assert.isTrue(display.post(keyEvent(key, SWT.KeyDown)), "Could not post keyevent.");
 51  0
                 display.wake();
 52  0
         }
 53  
 
 54  
         public void releaseKey(KeyStroke key) {
 55  0
                 Assert.isTrue(display.post(keyEvent(key, SWT.KeyUp)), "Could not post keyevent.");
 56  0
                 display.wake();
 57  0
         }
 58  
 
 59  
         private Event keyEvent(KeyStroke key, int type) {
 60  0
                 Event e = new Event();
 61  0
                 e.type = type;
 62  0
                 e.keyCode = keycode(key);
 63  0
                 e.character = character(key);
 64  0
                 e.widget = widget;
 65  0
                 return e;
 66  
         }
 67  
 
 68  
         private char character(KeyStroke key) {
 69  0
                 int naturalKey = key.getNaturalKey();
 70  0
                 if (specialKeys.contains(naturalKey))
 71  0
                         return 0;
 72  0
                 return (char) naturalKey;
 73  
         }
 74  
 
 75  
         private int keycode(KeyStroke key) {
 76  0
                 int naturalKey = key.getNaturalKey();
 77  0
                 int modifierKeys = key.getModifierKeys();
 78  
                 // if it is a modifier key
 79  0
                 if (modifierKeys != 0)
 80  0
                         return modifierKeys;
 81  
                 // if it is a special key
 82  0
                 if (specialKeys.contains(naturalKey))
 83  0
                         return naturalKey;
 84  0
                 return 0;
 85  
         }
 86  
 
 87  
         /* these keys are special and translated using Display#KeyTable. */
 88  
         static {
 89  
                 /* function keys */
 90  1
                 addSpecialKeyMapping(SWT.F1);
 91  1
                 addSpecialKeyMapping(SWT.F2);
 92  1
                 addSpecialKeyMapping(SWT.F3);
 93  1
                 addSpecialKeyMapping(SWT.F4);
 94  1
                 addSpecialKeyMapping(SWT.F5);
 95  1
                 addSpecialKeyMapping(SWT.F6);
 96  1
                 addSpecialKeyMapping(SWT.F7);
 97  1
                 addSpecialKeyMapping(SWT.F8);
 98  1
                 addSpecialKeyMapping(SWT.F9);
 99  1
                 addSpecialKeyMapping(SWT.F10);
 100  1
                 addSpecialKeyMapping(SWT.F11);
 101  1
                 addSpecialKeyMapping(SWT.F12);
 102  
 
 103  1
                 addSpecialKeyMapping(SWT.DEL);
 104  
 
 105  
                 /* direction and page navigation keys */
 106  1
                 addSpecialKeyMapping(SWT.HOME);
 107  1
                 addSpecialKeyMapping(SWT.END);
 108  1
                 addSpecialKeyMapping(SWT.PAGE_UP);
 109  1
                 addSpecialKeyMapping(SWT.PAGE_DOWN);
 110  1
                 addSpecialKeyMapping(SWT.ARROW_RIGHT);
 111  1
                 addSpecialKeyMapping(SWT.ARROW_DOWN);
 112  1
                 addSpecialKeyMapping(SWT.ARROW_LEFT);
 113  1
                 addSpecialKeyMapping(SWT.ARROW_UP);
 114  
 
 115  
                 /* whitespace keys */
 116  1
                 addSpecialKeyMapping(SWT.BS);
 117  1
                 addSpecialKeyMapping(SWT.CR);
 118  1
                 addSpecialKeyMapping(SWT.DEL);
 119  1
                 addSpecialKeyMapping(SWT.ESC);
 120  1
                 addSpecialKeyMapping(SWT.LF);
 121  1
                 addSpecialKeyMapping(SWT.TAB);
 122  
         }
 123  
 
 124  
         private static void addSpecialKeyMapping(int swtKey) {
 125  27
                 specialKeys.add(swtKey);
 126  27
         }
 127  
 
 128  
 }