File: ModifierPermutation.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (343 lines) | stat: -rw-r--r-- 14,013 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
  test %I% %E%
  @bug 6315717
  @summary presses buttons in all permutations and verifies modifiers
  @author Andrei Dmitriev : area=awt.mouse
  @run main ModifierPermutation
 */
//package modifierpermutation;

/*
The test will try to press-release every button present on the mouse in different order.
Here are some abbreviations:
 BUTTON1 press = P1
 BUTTON2 press = P2 etc.
 BUTTON1 release = R1
 BUTTON2 release = R2 etc.
Only sequences alike below are possible : <P1, P2, R2, R1>.
Sequences like <P1, P2, R1, R2> will not be covered by this test due to its probable complexity.
 */

import java.awt.*;
import sun.awt.SunToolkit;
import java.awt.event.*;
import java.util.Arrays;

public class ModifierPermutation {
    static boolean failed = false;
    final static int BUTTONSNUMBER = MouseInfo.getNumberOfButtons();

/*
 * Because of some problems with BUTTONx_MASK
 * (they are not ordered. Instead, their values are: 16 8 4)
 * We have to use array [1..n] and make every permutation on its
 * containment. After each permutation, make the same thing with
 * array of buttons and array of expected modifiers.
 */
    static SunToolkit st = (SunToolkit)(Toolkit.getDefaultToolkit());
    //all button masks
    static int [] mouseButtons = new int [BUTTONSNUMBER]; //BUTTONx_MASK
    static int [] mouseButtonsDown = new int [BUTTONSNUMBER]; //BUTTONx_DOWN_MASK

    //used to store mouse buttons sequences to press/to release
    static int [] affectedButtonsToPressRelease;
//    static int [] buttonsToRelease;
//    static int [] modifiersToVerifyOnPressRelease;

    static Robot robot;
    static CheckingAdapter adapterTest1;
    static Frame f;

    static {
        for (int i = 0; i < BUTTONSNUMBER; i++){
            mouseButtons[i] = InputEvent.getMaskForButton(i+1); //then change first three elements here to BUTTONx_MASK
            mouseButtonsDown[i] = InputEvent.getMaskForButton(i+1);
        }
        //mouseButtons initially has following values : 16 8 4.
/*        mouseButtons[0] = InputEvent.BUTTON1_MASK;
        mouseButtons[1] = InputEvent.BUTTON2_MASK;
        mouseButtons[2] = InputEvent.BUTTON3_MASK;
 */
    }

    public static void main(String s[]){
        init();

        try {
            robot = new Robot();
        } catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("Test failed.", e);
        }
        robot.delay(500);
        robot.mouseMove(f.getLocationOnScreen().x + f.getWidth()/2, f.getLocationOnScreen().y + f.getHeight()/2);
        robot.delay(500);
        //Top limit is the factorial of the number of existing buttons
        for (int k = 0; k < factorial(mouseButtons.length)-1; k++){
            //now we will press 2 up to maximum buttons and release them in different order and listen for
            // PRESSED events and check it's ExModifiers
            for (int buttonsToPressNumber = 2; buttonsToPressNumber <= BUTTONSNUMBER; buttonsToPressNumber++ ){
                System.out.println(">>>");

                //Now get the slice of affected buttons
                affectedButtonsToPressRelease = Arrays.copyOf(mouseButtons, buttonsToPressNumber);
//                modifiersToVerifyOnPressRelease = Arrays.copyOf(mouseButtons, buttonsToPressNumber);

                //Now press all these buttons in the order as they are in array affectedButtonsToPressRelease
                //And release all these buttons in back order.

                dumpArray("Affected Buttons ", affectedButtonsToPressRelease);
                pressAllButtons(affectedButtonsToPressRelease);
                releaseAllButtonsForwardOrder(affectedButtonsToPressRelease);
//                    nextPermutation(i, buttonsToRelease);
                //TODO: press buttons and release them backward
                //All I have to add is :
//                pressAllButtons(affectedButtonsToPressRelease);
//                releaseAllButtonsBackwardOrder(affectedButtonsToPressRelease);

                System.out.println("<<<");
            }
            nextPermutation(k, mouseButtons);
//            PermutationGenerator.nextPermutation(k, mouseButtonsDown);
            dumpArray("mouseButtons (step="+k+")", mouseButtons);
//            dumpArray("mouseButtonsDown (step="+k+")", mouseButtonsDown);
        }
    }

    private static void init(){
        adapterTest1 = new CheckingAdapter();
        f = new Frame("Robot presses mouse here");
        f.setSize(300, 300);
        f.setVisible(true);
        f.addMouseListener(adapterTest1);
    }
    public static int factorial(int t){
        if (t <=1 ) {
            return 1;
        } else {
            return t*factorial(t-1);
        }
    }

    // use this variable to get current button on EDT in checkModifiers()
    static volatile int currentButtonIndexUnderAction;

    public static void pressAllButtons(int []array){
        for (int i = 0; i <array.length; i ++){
            if (failed) {
                throw new RuntimeException("PRESSED_EVENT is not filled with correct values. Review messaage above.");
            }
            System.out.println("Pressing button = " + array[i]);
            currentButtonIndexUnderAction = i;
            robot.mousePress(array[i]);
            System.out.println("currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);
            st.realSync();
            //            robot.delay(100);
        }
    }

    public static void releaseAllButtonsForwardOrder(int []array){
        for (int i = 0; i <array.length; i ++){
            System.out.println("Releasing button = " + array[i]);
            currentButtonIndexUnderAction = i;
            robot.mouseRelease(array[i]);
            System.out.println("currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);
            st.realSync();
            //            robot.delay(100);
        }
    }

    public static void checkModifiersOnPress(MouseEvent e){
        System.out.println("checkModifiers. currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);
        for (int i = 0; i<= currentButtonIndexUnderAction; i++){
            if ((e.getModifiersEx() & affectedButtonsToPressRelease[i]) == 0){
                System.out.println("ERROR["+i+"]: PRESSED_EVENT is not filled with correct values. affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i] +" Event = "+e);
                ModifierPermutation.failed = true;
            } else {
                System.out.println("CORRECT["+i+"]: affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i]+ " Event = "+e);
            }
        }
    }

    /*======================================================================*/
    public static void dumpValues(int button, int modifiers, int modifiersStandard, int modifiersEx, int modifiersExStandard){
        System.out.println("Button = "+button + "Modifiers = "+ modifiers + " standard = "+ modifiersStandard);
        System.out.println("                   ModifiersEx = "+ modifiersEx + " standardEx = "+ modifiersExStandard);
    }

    public static void dumpArray(String id, int [] array){
        System.out.print(id);
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
    public static void nextPermutation(int step, int []array){
        int i;
        int leftEl = 0;
        int rightEl = 0;

        i = array.length - 2;
        while (i>=0) {
            if (array[i] < array[i+1]){
                leftEl = i;
                //                        System.out.println("leftEl = "+leftEl);
                break;
            }
            i--;
        }

        i = array.length - 1;
        while (i>=0) {
            if (array[i] > array[leftEl]) {
                rightEl = i;
                //                        System.out.println("rightEl = "+rightEl);
                break;
            }
            i--;
        }
        swapElements(array, leftEl, rightEl);
        if (leftEl + 2 <  array.length){
            //                    System.out.println("sort");
            Arrays.sort(array, leftEl + 1 , array.length);
        }
    }

    public static void swapElements(int [] array, int leftEl, int rightEl){
        int tmp = array[leftEl];
        array[leftEl] = array[rightEl];
        array[rightEl] = tmp;
    }

    public static void checkModifiersOnRelease(MouseEvent e){
        System.out.println("CheckModifiersOnRelease. currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);
        for (int i = currentButtonIndexUnderAction+1; i<affectedButtonsToPressRelease.length; i++){
            if ((e.getModifiersEx() & affectedButtonsToPressRelease[i]) == 0){
                System.out.println("ERROR["+i+"]: RELEASED_EVENT is not filled with correct values. affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i] +" Event = "+e);
                ModifierPermutation.failed = true;
            } else {
                System.out.println("CORRECT["+i+"]: affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i]+ " Event = "+e);
            }
        }
    }

    public static void checkModifiersOnClick(MouseEvent e){
        System.out.println("CheckModifiersOnClick. currentButtonIndexUnderAction ="+currentButtonIndexUnderAction);
//Actually the same as in checkModifiersOnRelease()
        for (int i = currentButtonIndexUnderAction+1; i<affectedButtonsToPressRelease.length; i++){
            if ((e.getModifiersEx() & affectedButtonsToPressRelease[i]) == 0){
                System.out.println("ERROR["+i+"]: CLICK_EVENT is not filled with correct values. affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i] +" Event = "+e);
                ModifierPermutation.failed = true;
            } else {
                System.out.println("CORRECT["+i+"]: affectedButtonsToPressRelease[i]= "+affectedButtonsToPressRelease[i]+ " Event = "+e);
            }
        }
    }
}
///~ ModifierPermutation clas

/* A class that invoke appropriate verification
 * routine with current modifier.
 */
class CheckingAdapter extends MouseAdapter{
    public CheckingAdapter(){}

    public void mousePressed(MouseEvent e) {
        System.out.println("PRESSED "+e);
        ModifierPermutation.checkModifiersOnPress(e);
    }
    public void mouseReleased(MouseEvent e) {
        System.out.println("RELEASED "+e);
        ModifierPermutation.checkModifiersOnRelease(e);

    }
    public void mouseClicked(MouseEvent e) {
        System.out.println("CLICKED "+e);
        ModifierPermutation.checkModifiersOnClick(e);
    }
}

// A class that could make a standard permutation with no regard to the
// values of array passed in.
// It uses a buttonIndicesToPermutate array with [1..N] values to perform
// these permutations.
//Note that nextPermutation is a static method and you can't keep track
// of more the single permutation sequence.
/*
class PermutationGenerator{
    final static int BUTTONSNUMBER = MouseInfo.getNumberOfButtons();
    static int [] buttonIndicesToPermutate = new int [BUTTONSNUMBER];;
    public PermutationGenerator(){
        for (int i = 0; i < BUTTONSNUMBER; i++){
            buttonIndicesToPermutate[i] = i+1; //fill it with [1..N] values
        }
    }

    public static void nextPermutation(int step, int []array){
        if (array.length != buttonIndicesToPermutate.length) {
            throw new IllegalArgumentException("Array should have length equals to mouse buttons number.");
        }
        int i;
        int leftEl = 0;
        int rightEl = 0;

        i = array.length - 2;
        while (i>=0) {
            if (buttonIndicesToPermutate[i] < buttonIndicesToPermutate[i+1]){
                leftEl = i;
                //                        System.out.println("leftEl = "+leftEl);
                break;
            }
            i--;
        }

        i = array.length - 1;
        while (i>=0) {
            if (buttonIndicesToPermutate[i] >buttonIndicesToPermutate[leftEl]) {
                rightEl = i;
                //                        System.out.println("rightEl = "+rightEl);
                break;
            }
            i--;
        }
        swapElements(array, leftEl, rightEl);
        swapElements(buttonIndicesToPermutate, leftEl, rightEl);

        if (leftEl + 2 <  array.length){
            //                    System.out.println("sort");
//need to make our own sorting because arraysort makes this on actual values in array...
            Arrays.sort(array, leftEl + 1 , array.length);
            Arrays.sort(buttonIndicesToPermutate, leftEl + 1 , buttonIndicesToPermutate.length);
//            sortArray(array, leftEl + 1 , array.length);
        }
    }
    public static void swapElements(int [] array, int leftEl, int rightEl){
        int tmp = array[leftEl];
        array[leftEl] = array[rightEl];
        array[rightEl] = tmp;
    }
}
*/