Path: blob/master/test/jdk/java/awt/Mouse/MouseModifiersUnitTest/ModifierPermutation.java
41152 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24test %I% %E%25@bug 631571726@summary presses buttons in all permutations and verifies modifiers27@author Andrei Dmitriev : area=awt.mouse28@run main ModifierPermutation29*/30//package modifierpermutation;3132/*33The test will try to press-release every button present on the mouse in different order.34Here are some abbreviations:35BUTTON1 press = P136BUTTON2 press = P2 etc.37BUTTON1 release = R138BUTTON2 release = R2 etc.39Only sequences alike below are possible : <P1, P2, R2, R1>.40Sequences like <P1, P2, R1, R2> will not be covered by this test due to its probable complexity.41*/4243import java.awt.*;44import sun.awt.SunToolkit;45import java.awt.event.*;46import java.util.Arrays;4748public class ModifierPermutation {49static boolean failed = false;50final static int BUTTONSNUMBER = MouseInfo.getNumberOfButtons();5152/*53* Because of some problems with BUTTONx_MASK54* (they are not ordered. Instead, their values are: 16 8 4)55* We have to use array [1..n] and make every permutation on its56* containment. After each permutation, make the same thing with57* array of buttons and array of expected modifiers.58*/59static SunToolkit st = (SunToolkit)(Toolkit.getDefaultToolkit());60//all button masks61static int [] mouseButtons = new int [BUTTONSNUMBER]; //BUTTONx_MASK62static int [] mouseButtonsDown = new int [BUTTONSNUMBER]; //BUTTONx_DOWN_MASK6364//used to store mouse buttons sequences to press/to release65static int [] affectedButtonsToPressRelease;66// static int [] buttonsToRelease;67// static int [] modifiersToVerifyOnPressRelease;6869static Robot robot;70static CheckingAdapter adapterTest1;71static Frame f;7273static {74for (int i = 0; i < BUTTONSNUMBER; i++){75mouseButtons[i] = InputEvent.getMaskForButton(i+1); //then change first three elements here to BUTTONx_MASK76mouseButtonsDown[i] = InputEvent.getMaskForButton(i+1);77}78//mouseButtons initially has following values : 16 8 4.79/* mouseButtons[0] = InputEvent.BUTTON1_MASK;80mouseButtons[1] = InputEvent.BUTTON2_MASK;81mouseButtons[2] = InputEvent.BUTTON3_MASK;82*/83}8485public static void main(String s[]){86init();8788try {89robot = new Robot();90} catch (Exception e){91e.printStackTrace();92throw new RuntimeException("Test failed.", e);93}94robot.delay(500);95robot.mouseMove(f.getLocationOnScreen().x + f.getWidth()/2, f.getLocationOnScreen().y + f.getHeight()/2);96robot.delay(500);97//Top limit is the factorial of the number of existing buttons98for (int k = 0; k < factorial(mouseButtons.length)-1; k++){99//now we will press 2 up to maximum buttons and release them in different order and listen for100// PRESSED events and check it's ExModifiers101for (int buttonsToPressNumber = 2; buttonsToPressNumber <= BUTTONSNUMBER; buttonsToPressNumber++ ){102System.out.println(">>>");103104//Now get the slice of affected buttons105affectedButtonsToPressRelease = Arrays.copyOf(mouseButtons, buttonsToPressNumber);106// modifiersToVerifyOnPressRelease = Arrays.copyOf(mouseButtons, buttonsToPressNumber);107108//Now press all these buttons in the order as they are in array affectedButtonsToPressRelease109//And release all these buttons in back order.110111dumpArray("Affected Buttons ", affectedButtonsToPressRelease);112pressAllButtons(affectedButtonsToPressRelease);113releaseAllButtonsForwardOrder(affectedButtonsToPressRelease);114// nextPermutation(i, buttonsToRelease);115//TODO: press buttons and release them backward116//All I have to add is :117// pressAllButtons(affectedButtonsToPressRelease);118// releaseAllButtonsBackwardOrder(affectedButtonsToPressRelease);119120System.out.println("<<<");121}122nextPermutation(k, mouseButtons);123// PermutationGenerator.nextPermutation(k, mouseButtonsDown);124dumpArray("mouseButtons (step="+k+")", mouseButtons);125// dumpArray("mouseButtonsDown (step="+k+")", mouseButtonsDown);126}127}128129private static void init(){130adapterTest1 = new CheckingAdapter();131f = new Frame("Robot presses mouse here");132f.setSize(300, 300);133f.setVisible(true);134f.addMouseListener(adapterTest1);135}136public static int factorial(int t){137if (t <=1 ) {138return 1;139} else {140return t*factorial(t-1);141}142}143144// use this variable to get current button on EDT in checkModifiers()145static volatile int currentButtonIndexUnderAction;146147public static void pressAllButtons(int []array){148for (int i = 0; i <array.length; i ++){149if (failed) {150throw new RuntimeException("PRESSED_EVENT is not filled with correct values. Review messaage above.");151}152System.out.println("Pressing button = " + array[i]);153currentButtonIndexUnderAction = i;154robot.mousePress(array[i]);155System.out.println("currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);156st.realSync();157// robot.delay(100);158}159}160161public static void releaseAllButtonsForwardOrder(int []array){162for (int i = 0; i <array.length; i ++){163System.out.println("Releasing button = " + array[i]);164currentButtonIndexUnderAction = i;165robot.mouseRelease(array[i]);166System.out.println("currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);167st.realSync();168// robot.delay(100);169}170}171172public static void checkModifiersOnPress(MouseEvent e){173System.out.println("checkModifiers. currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);174for (int i = 0; i<= currentButtonIndexUnderAction; i++){175if ((e.getModifiersEx() & affectedButtonsToPressRelease[i]) == 0){176System.out.println("ERROR["+i+"]: PRESSED_EVENT is not filled with correct values. affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i] +" Event = "+e);177ModifierPermutation.failed = true;178} else {179System.out.println("CORRECT["+i+"]: affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i]+ " Event = "+e);180}181}182}183184/*======================================================================*/185public static void dumpValues(int button, int modifiers, int modifiersStandard, int modifiersEx, int modifiersExStandard){186System.out.println("Button = "+button + "Modifiers = "+ modifiers + " standard = "+ modifiersStandard);187System.out.println(" ModifiersEx = "+ modifiersEx + " standardEx = "+ modifiersExStandard);188}189190public static void dumpArray(String id, int [] array){191System.out.print(id);192for (int i = 0; i < array.length; i++){193System.out.print(array[i]+" ");194}195System.out.println();196}197public static void nextPermutation(int step, int []array){198int i;199int leftEl = 0;200int rightEl = 0;201202i = array.length - 2;203while (i>=0) {204if (array[i] < array[i+1]){205leftEl = i;206// System.out.println("leftEl = "+leftEl);207break;208}209i--;210}211212i = array.length - 1;213while (i>=0) {214if (array[i] > array[leftEl]) {215rightEl = i;216// System.out.println("rightEl = "+rightEl);217break;218}219i--;220}221swapElements(array, leftEl, rightEl);222if (leftEl + 2 < array.length){223// System.out.println("sort");224Arrays.sort(array, leftEl + 1 , array.length);225}226}227228public static void swapElements(int [] array, int leftEl, int rightEl){229int tmp = array[leftEl];230array[leftEl] = array[rightEl];231array[rightEl] = tmp;232}233234public static void checkModifiersOnRelease(MouseEvent e){235System.out.println("CheckModifiersOnRelease. currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);236for (int i = currentButtonIndexUnderAction+1; i<affectedButtonsToPressRelease.length; i++){237if ((e.getModifiersEx() & affectedButtonsToPressRelease[i]) == 0){238System.out.println("ERROR["+i+"]: RELEASED_EVENT is not filled with correct values. affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i] +" Event = "+e);239ModifierPermutation.failed = true;240} else {241System.out.println("CORRECT["+i+"]: affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i]+ " Event = "+e);242}243}244}245246public static void checkModifiersOnClick(MouseEvent e){247System.out.println("CheckModifiersOnClick. currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);248//Actually the same as in checkModifiersOnRelease()249for (int i = currentButtonIndexUnderAction+1; i<affectedButtonsToPressRelease.length; i++){250if ((e.getModifiersEx() & affectedButtonsToPressRelease[i]) == 0){251System.out.println("ERROR["+i+"]: CLICK_EVENT is not filled with correct values. affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i] +" Event = "+e);252ModifierPermutation.failed = true;253} else {254System.out.println("CORRECT["+i+"]: affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i]+ " Event = "+e);255}256}257}258}259///~ ModifierPermutation clas260261/* A class that invoke appropriate verification262* routine with current modifier.263*/264class CheckingAdapter extends MouseAdapter{265public CheckingAdapter(){}266267public void mousePressed(MouseEvent e) {268System.out.println("PRESSED "+e);269ModifierPermutation.checkModifiersOnPress(e);270}271public void mouseReleased(MouseEvent e) {272System.out.println("RELEASED "+e);273ModifierPermutation.checkModifiersOnRelease(e);274275}276public void mouseClicked(MouseEvent e) {277System.out.println("CLICKED "+e);278ModifierPermutation.checkModifiersOnClick(e);279}280}281282// A class that could make a standard permutation with no regard to the283// values of array passed in.284// It uses a buttonIndicesToPermutate array with [1..N] values to perform285// these permutations.286//Note that nextPermutation is a static method and you can't keep track287// of more the single permutation sequence.288/*289class PermutationGenerator{290final static int BUTTONSNUMBER = MouseInfo.getNumberOfButtons();291static int [] buttonIndicesToPermutate = new int [BUTTONSNUMBER];;292public PermutationGenerator(){293for (int i = 0; i < BUTTONSNUMBER; i++){294buttonIndicesToPermutate[i] = i+1; //fill it with [1..N] values295}296}297298public static void nextPermutation(int step, int []array){299if (array.length != buttonIndicesToPermutate.length) {300throw new IllegalArgumentException("Array should have length equals to mouse buttons number.");301}302int i;303int leftEl = 0;304int rightEl = 0;305306i = array.length - 2;307while (i>=0) {308if (buttonIndicesToPermutate[i] < buttonIndicesToPermutate[i+1]){309leftEl = i;310// System.out.println("leftEl = "+leftEl);311break;312}313i--;314}315316i = array.length - 1;317while (i>=0) {318if (buttonIndicesToPermutate[i] >buttonIndicesToPermutate[leftEl]) {319rightEl = i;320// System.out.println("rightEl = "+rightEl);321break;322}323i--;324}325swapElements(array, leftEl, rightEl);326swapElements(buttonIndicesToPermutate, leftEl, rightEl);327328if (leftEl + 2 < array.length){329// System.out.println("sort");330//need to make our own sorting because arraysort makes this on actual values in array...331Arrays.sort(array, leftEl + 1 , array.length);332Arrays.sort(buttonIndicesToPermutate, leftEl + 1 , buttonIndicesToPermutate.length);333// sortArray(array, leftEl + 1 , array.length);334}335}336public static void swapElements(int [] array, int leftEl, int rightEl){337int tmp = array[leftEl];338array[leftEl] = array[rightEl];339array[rightEl] = tmp;340}341}342*/343344345