Coverage Report - org.eclipse.swtbot.swt.finder.keyboard.MockKeyboardStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
MockKeyboardStrategy
4%
1/23
0%
0/4
1.333
MockKeyboardStrategy$MyWidget
0%
0/4
N/A
1.333
 
 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 org.eclipse.jface.bindings.keys.KeyStroke;
 14  
 import org.eclipse.swt.SWT;
 15  
 import org.eclipse.swt.widgets.Event;
 16  
 import org.eclipse.swt.widgets.Widget;
 17  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 18  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 19  
 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
 20  
 import org.hamcrest.SelfDescribing;
 21  
 
 22  
 /**
 23  
  * Sends keyboard notifications using {@link Widget#notifyListeners(int, Event)}. Note that this may not work in all
 24  
  * cases.
 25  
  * 
 26  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 27  
  * @version $Id$
 28  
  */
 29  1
 class MockKeyboardStrategy extends AbstractKeyboardStrategy {
 30  
 
 31  
         private MyWidget        widget;
 32  
 
 33  
         public void init(Widget widget, SelfDescribing description) {
 34  0
                 this.widget = new MyWidget(widget, description);
 35  0
         }
 36  
 
 37  
         public void pressKeys(KeyStroke... keys) {
 38  0
                 assertNotDisposed();
 39  0
                 widget.notify(SWT.KeyDown, event(keys));
 40  0
         }
 41  
 
 42  
         public void releaseKeys(KeyStroke... keys) {
 43  0
                 assertNotDisposed();
 44  0
                 widget.notify(SWT.KeyUp, event(keys));
 45  0
         }
 46  
 
 47  
         private Event event(KeyStroke... keys) {
 48  0
                 int modifiers = SWT.NONE;
 49  0
                 int ch = 0;
 50  
 
 51  0
                 for (KeyStroke key : keys) {
 52  0
                         modifiers |= key.getModifierKeys();
 53  
                 }
 54  
 
 55  0
                 Event e = new Event();
 56  0
                 e.character = Keystrokes.toCharacter(keys);
 57  0
                 e.widget = widget.widget;
 58  0
                 e.keyCode = ch;
 59  0
                 e.stateMask = modifiers;
 60  0
                 return e;
 61  
         }
 62  
 
 63  
         private void assertNotDisposed() {
 64  0
                 Assert.isTrue(!widget.widget.isDisposed(), "The widget has been disposed.");
 65  0
         }
 66  
 
 67  
         public void pressKey(KeyStroke key) {
 68  0
                 throw new UnsupportedOperationException("This operation is not supported");
 69  
         }
 70  
 
 71  
         public void releaseKey(KeyStroke key) {
 72  0
                 throw new UnsupportedOperationException("This operation is not supported");
 73  
         }
 74  
 
 75  
         private static class MyWidget extends AbstractSWTBot<Widget> {
 76  
 
 77  
                 public MyWidget(Widget w, SelfDescribing description) throws WidgetNotFoundException {
 78  0
                         super(w, description);
 79  0
                 }
 80  
 
 81  
                 public void notify(int eventType, Event createEvent) {
 82  0
                         super.notify(eventType, createEvent);
 83  0
                 }
 84  
         }
 85  
 
 86  
 }