1 /*
   2  * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.jemmy.input;
  24 
  25 
  26 import java.awt.event.MouseEvent;
  27 import java.util.Arrays;
  28 import java.util.HashSet;
  29 import org.jemmy.JemmyException;
  30 import org.jemmy.interfaces.Keyboard.KeyboardButton;
  31 import org.jemmy.interfaces.Modifier;
  32 import org.jemmy.interfaces.Mouse.MouseButton;
  33 import static org.jemmy.interfaces.Keyboard.KeyboardButtons.*;
  34 import org.jemmy.interfaces.Keyboard.KeyboardModifiers;
  35 import org.jemmy.interfaces.Mouse.MouseButtons;
  36 import org.jemmy.interfaces.Mouse.MouseModifiers;
  37 import org.testng.annotations.AfterClass;
  38 import org.testng.annotations.AfterMethod;
  39 import org.testng.annotations.BeforeClass;
  40 import org.testng.annotations.BeforeMethod;
  41 import org.testng.annotations.Test;
  42 
  43 import static java.awt.event.KeyEvent.*;
  44 import static org.testng.Assert.fail;
  45 import static org.testng.AssertJUnit.assertEquals;
  46 
  47 
  48 /**
  49  *
  50  * @author Alexander Kouznetsov <mrkam@mail.ru>
  51  */
  52 public class AWTMapTest {
  53 
  54     public AWTMapTest() {
  55     }
  56 
  57     @BeforeClass
  58     public static void setUpClass() throws Exception {
  59     }
  60 
  61     @AfterClass
  62     public static void tearDownClass() throws Exception {
  63     }
  64 
  65     @BeforeMethod
  66     public void setUp() {
  67     }
  68 
  69     @AfterMethod
  70     public void tearDown() {
  71     }
  72 
  73     private final KeyboardButton [] jemmyKeyboardButtons = new KeyboardButton [] { A, ADD, D5, F5, NUMPAD5, OPEN_BRACKET };
  74     private final int [] awtKeyboardButtons = new int [] { VK_A, VK_ADD, VK_5, VK_F5, VK_NUMPAD5, VK_OPEN_BRACKET };
  75     /**
  76      * Test of convert method, of class AWTMap.
  77      */
  78     @Test
  79     public void testConvert_KeyboardKeyboardButton() {
  80         System.out.println("convert");
  81         for(int i = 0; i < jemmyKeyboardButtons.length; i++) {
  82             int result = new AWTMap().convert(jemmyKeyboardButtons[i]);
  83             assertEquals("Failed check for " + jemmyKeyboardButtons[i], awtKeyboardButtons[i], result);
  84         }
  85     }
  86 
  87     private final Modifier[][] jemmyModifierCombinations = new Modifier [][] {
  88             { KeyboardModifiers.SHIFT_DOWN_MASK },
  89             { KeyboardModifiers.CTRL_DOWN_MASK, KeyboardModifiers.SHIFT_DOWN_MASK },
  90             { KeyboardModifiers.CTRL_DOWN_MASK, KeyboardModifiers.ALT_DOWN_MASK, KeyboardModifiers.SHIFT_DOWN_MASK },
  91             { MouseModifiers.BUTTON1_DOWN_MASK },
  92             { MouseModifiers.BUTTON1_DOWN_MASK, KeyboardModifiers.SHIFT_DOWN_MASK },
  93     };
  94     private final int[] awtModifierCombinations = new int [] {
  95             SHIFT_DOWN_MASK,
  96             CTRL_DOWN_MASK | SHIFT_DOWN_MASK,
  97             CTRL_DOWN_MASK | ALT_DOWN_MASK | SHIFT_DOWN_MASK,
  98             BUTTON1_DOWN_MASK,
  99             BUTTON1_DOWN_MASK | SHIFT_DOWN_MASK
 100     };
 101 
 102     /**
 103      * Test of convert method, of class AWTMap.
 104      */
 105     @Test
 106     public void testConvert_ModifierArr() {
 107         System.out.println("convert");
 108         for(int i = 0; i < jemmyModifierCombinations.length; i++) {
 109             Modifier[] modifiers = jemmyModifierCombinations[i];
 110             int expResult = awtModifierCombinations[i];
 111             int result = new AWTMap().convert(modifiers);
 112             assertEquals("Failed check for " + Arrays.toString(modifiers), expResult, result);
 113         }
 114     }
 115 
 116     private final MouseButton [] jemmyMouseButtons = new MouseButton [] { MouseButtons.BUTTON1 };
 117     private final int [] awtMouseButtons = new int [] { BUTTON1_MASK };
 118 
 119     /**
 120      * Test of convert method, of class AWTMap.
 121      */
 122     @Test
 123     public void testConvert_MouseMouseButton() {
 124         System.out.println("convert");
 125         for(int i = 0; i < jemmyMouseButtons.length; i++) {
 126             MouseButton button = jemmyMouseButtons[i];
 127             int expResult = awtMouseButtons[i];
 128             int result = new AWTMap().convert(button);
 129             assertEquals("Failed check for " + button, expResult, result);
 130         }
 131     }
 132 
 133     /**
 134      * Test of convertKeyboardButton method, of class AWTMap.
 135      */
 136     @Test
 137     public void testConvertKeyboardButton() {
 138         System.out.println("convertKeyboardButton");
 139         for (int i = 0; i < awtKeyboardButtons.length; i++) {
 140             int key = awtKeyboardButtons[i];
 141             KeyboardButton expResult = jemmyKeyboardButtons[i];
 142             KeyboardButton result = new AWTMap().convertKeyboardButton(key);
 143             assertEquals("Failed check for " + expResult, expResult, result);
 144         }
 145     }
 146 
 147     /**
 148      * Test of convertModifiers method, of class AWTMap.
 149      */
 150     @Test
 151     public void testConvertModifiers() {
 152         System.out.println("convertModifiers");
 153         for (int i = 0; i < awtModifierCombinations.length; i ++) {
 154             int modifiers = awtModifierCombinations[i];
 155             Modifier[] expResult = jemmyModifierCombinations[i];
 156             Modifier[] result = new AWTMap().convertModifiers(modifiers);
 157             assertEquals("Failed check with " + Arrays.toString(expResult), new HashSet<Modifier>(Arrays.asList(expResult)), new HashSet<Modifier>(Arrays.asList(result)));
 158         }
 159     }
 160 
 161     /**
 162      * Test of convertMouseButton method, of class AWTMap.
 163      */
 164     @Test
 165     public void testConvertMouseButton() {
 166         System.out.println("convertMouseButton");
 167         for (int i = 0; i < awtMouseButtons.length; i++) {
 168             int button = awtMouseButtons[i];
 169             MouseButton expResult = jemmyMouseButtons[i];
 170             MouseButton result = new AWTMap().convertMouseButton(button);
 171             assertEquals("Check failed with " + expResult, expResult, result);
 172         }
 173     }
 174 
 175     @Test
 176     public void testGetException() {
 177         try {
 178             new AWTMap().convert(new KeyboardButton() {});
 179             fail("No JemmyException");
 180         } catch(JemmyException e) {
 181         } catch(Exception e) {
 182             fail("Not a JemmyException");
 183         }
 184         try {
 185             new AWTMap().convert(new MouseButton() {});
 186             fail("No JemmyException");
 187         } catch(JemmyException e) {
 188         } catch(Exception e) {
 189             fail("Not a JemmyException");
 190         }
 191         try {
 192             new AWTMap().convert(new Modifier() {}, new Modifier() {});
 193             fail("No JemmyException");
 194         } catch(JemmyException e) {
 195         } catch(Exception e) {
 196             fail("Not a JemmyException");
 197         }
 198         try {
 199             new AWTMap().convertKeyboardButton(-1);
 200             fail("No JemmyException");
 201         } catch(JemmyException e) {
 202         } catch(Exception e) {
 203             fail("Not a JemmyException");
 204         }
 205         try {
 206             new AWTMap().convertMouseButton(-1);
 207             fail("No JemmyException");
 208         } catch(JemmyException e) {
 209         } catch(Exception e) {
 210             fail("Not a JemmyException");
 211         }
 212     }
 213 }