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