Coverage Report - org.eclipse.swtbot.swt.finder.keyboard.AbstractKeyboardStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractKeyboardStrategy
100%
21/21
100%
10/10
1.375
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2008 Ketan Padegaonkar 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.apache.log4j.Logger;
 14  
 import org.eclipse.jface.bindings.keys.KeyStroke;
 15  
 import org.eclipse.swt.widgets.Widget;
 16  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 17  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 18  
 import org.hamcrest.SelfDescribing;
 19  
 
 20  
 /**
 21  
  * Implementors must have a default no-args constructor.
 22  
  * 
 23  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 24  
  * @version $Id$
 25  
  */
 26  
 public abstract class AbstractKeyboardStrategy implements KeyboardStrategy {
 27  
 
 28  
         protected final Logger        log;
 29  
 
 30  167
         protected AbstractKeyboardStrategy() {
 31  167
                 this.log = Logger.getLogger(getClass());
 32  167
         }
 33  
 
 34  
         public void init(Widget widget, SelfDescribing description) {
 35  
                 // do nothing
 36  54
         }
 37  
 
 38  
         /**
 39  
          * Presses the specified key.
 40  
          * 
 41  
          * @param key the keystroke to press down
 42  
          */
 43  
         protected abstract void pressKey(KeyStroke key);
 44  
 
 45  
         /**
 46  
          * Releases the specified key.
 47  
          * 
 48  
          * @param key the keystroke to release.
 49  
          */
 50  
         protected abstract void releaseKey(KeyStroke key);
 51  
 
 52  
         public void pressKeys(KeyStroke... keys) {
 53  124
                 assertKeys(keys);
 54  311
                 for (KeyStroke key : keys) {
 55  187
                         log.trace(MessageFormat.format("Pressing down key {0}", key));
 56  187
                         pressKey(key);
 57  
                 }
 58  124
         }
 59  
 
 60  
         public void releaseKeys(KeyStroke... keys) {
 61  124
                 assertKeys(keys);
 62  311
                 for (KeyStroke key : keys) {
 63  187
                         log.trace(MessageFormat.format("Releasing key {0}", key));
 64  187
                         releaseKey(key);
 65  
                 }
 66  124
         }
 67  
 
 68  
         private void assertKeys(KeyStroke... keys) {
 69  622
                 for (KeyStroke key : keys) {
 70  374
                         assertKey(key);
 71  
                 }
 72  248
         }
 73  
 
 74  
         private void assertKey(KeyStroke key) {
 75  374
                 boolean hasNaturalKey = key.getNaturalKey() != KeyStroke.NO_KEY;
 76  374
                 boolean hasModifiers = key.getModifierKeys() != KeyStroke.NO_KEY;
 77  
 
 78  374
                 Assert.isTrue(hasNaturalKey ^ hasModifiers, "You just gave me a complex keystroke. Please split the keystroke into multiple keystrokes.");
 79  374
         }
 80  
 
 81  
 }