File: MouseModifiersUnitTest_Extra.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 (517 lines) | stat: -rw-r--r-- 20,921 bytes parent folder | download | duplicates (2)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
 * Copyright (c) 2008, 2016, 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%
  @key headful
  @bug 6315717
  @summary verifies that modifiers are correct for extra buttons
  @author Andrei Dmitriev : area=awt.mouse
  @library ../../../../lib/testlibrary
  @build jdk.testlibrary.OSInfo
  @run main MouseModifiersUnitTest_Extra
 */

import jdk.testlibrary.OSInfo;

import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;

// will process extra buttons only
// asking parameters from CMD: manual/automatic, modifier to test

public class MouseModifiersUnitTest_Extra extends Frame {
    static final int NONE = 0;
    static final int SHIFT = 1;
    static final int CTRL = 2;
    static final int ALT = 3;
    static CheckingModifierAdapterExtra adapterTest1;
    static CheckingModifierAdapterExtra adapterTest2;
    static CheckingModifierAdapterExtra adapterTest3;
    static CheckingModifierAdapterExtra adapterTest4;

    static boolean debug = true; //dump all errors (debug) or throw first(under jtreg) exception
    static boolean autorun = false; //use robot or manual run
    static int testModifier = NONE;

    static int [] mouseButtonDownMasks;

    //an arrays representing a modifiersEx of extra mouse buttons while using ALT/CTRL/SHIFT or none of them
    static int [] modifiersExStandard;
    static int [] modifiersExStandardSHIFT;
    static int [] modifiersExStandardCTRL;
    static int [] modifiersExStandardALT;

    private final static String SHIFT_MODIFIER = OSInfo.getOSType().equals(OSInfo.OSType.MACOSX) ?
                                                "\u21e7" : "Shift";

    private final static String ALT_MODIFIER = OSInfo.getOSType().equals(OSInfo.OSType.MACOSX) ?
                                                "\u2325" : "Alt";


    private final static String CTRL_MODIFIER = OSInfo.getOSType().equals(OSInfo.OSType.MACOSX) ?
                                                "\u2303" : "Ctrl";


    // BUTTON1, 2, 3 press-release.
    final static int  modifiersStandard = 0; //InputEvent.BUTTON_DOWN_MASK;

    public static void checkPressedModifiersTest(int testModifier, MouseEvent event){
        int [] curStandardExModifiers = getStandardExArray(testModifier);
        int button = event.getButton();
        int modifiers = event.getModifiers();
        int modifiersEx = event.getModifiersEx();
        int index = (button - 4)*3;
        dumpValues(button, modifiers, modifiersStandard, modifiersEx, curStandardExModifiers[index]);
        if (modifiers != modifiersStandard){
            MessageLogger.reportError("Test failed :  Pressed. modifiers != modifiersStandard");
        }

        if (modifiersEx != curStandardExModifiers[index]){
//            System.out.println(">>>>>>>>>>>>>>> Pressed. modifiersEx "+modifiersEx +" : "+!= curStandardExModifiers");
            MessageLogger.reportError("Test failed :  Pressed. modifiersEx != curStandardExModifiers. Got: "
                    + modifiersEx + " , Expected: " + curStandardExModifiers[index]);
        }

     //check event.paramString() output
        HashMap <String, String> paramStringElements = tokenizeParamString(event.paramString());
        System.out.println(event.paramString());
        checkButton(paramStringElements, button);
        checkModifiers(testModifier, paramStringElements, button);
        checkExtModifiersOnPress(testModifier, paramStringElements, button);
    }

    public static void checkExtModifiersOnReleaseClick(int testModifier, HashMap<String, String> h, int button){
        String ethalon = "";
        switch (testModifier){
            case SHIFT:{
                ethalon = SHIFT_MODIFIER;
                break;
            }
            case ALT:{
                ethalon = ALT_MODIFIER;
                break;
            }
            case CTRL:{
                ethalon = CTRL_MODIFIER;
                break;
            }
        }

        if (h.get("extModifiers") == null){
            h.put("extModifiers", "");
        }

        if (!ethalon.equals(h.get("extModifiers"))) {
            MessageLogger.reportError("Test failed :  Released/Clicked. extModifiers = "
                    + h.get("extModifiers") + " instead of : " + ethalon);
        }
    }

    public static void checkExtModifiersOnPress(int testModifier, HashMap<String, String> h, int button){
        String ethalon = "";
        switch (testModifier){
            case SHIFT:{
                ethalon = SHIFT_MODIFIER + "+";
                break;
            }
            case ALT:{
                ethalon = ALT_MODIFIER + "+";
                break;
            }
            case CTRL:{
                ethalon = CTRL_MODIFIER + "+";
                break;
            }
        }
        ethalon = ethalon + "Button" +button;

        if (!h.get("extModifiers").equals(ethalon)) {
            MessageLogger.reportError("Test failed :  Pressed. extModifiers = " +h.get("extModifiers")+" instead of : "
                    + ethalon);
        }
    }

    public static void checkModifiers(int testModifier, HashMap<String, String> h, int button){
        // none of modifiers for extra button should be null
        if (h.get("modifiers") != null) {
            MessageLogger.reportError("Test failed : modifiers != null");
        }
    }

    public static void checkButton(HashMap<String, String> h, int button){
        if (h.get("button") == null) {
            MessageLogger.reportError("Test failed :  checkButton(). button is absent in paramString()");
        }
        if (Integer.parseInt(h.get("button")) != button) {
            MessageLogger.reportError("Test failed :  checkButton. button in paramString() doesn't equal to button being pressed.");
        }
    }
    public static HashMap<String, String> tokenizeParamString(String param){
        HashMap <String, String> params = new HashMap<>();
        StringTokenizer st = new StringTokenizer(param, ",=");
        while (st.hasMoreTokens()){
            String tmp = st.nextToken();
//            System.out.println("PARSER : "+tmp);
            if (tmp.equals("button") ||
                    tmp.equals("modifiers") ||
                    tmp.equals("extModifiers")) {
                params.put(tmp, st.nextToken());
            }
        }
        return params;
    }

    public static Vector<String> tokenizeModifiers(String modifierList){
        Vector<String> modifiers = new Vector<>();
        StringTokenizer st = new StringTokenizer(modifierList, "+");
        while (st.hasMoreTokens()){
            String tmp = st.nextToken();
            modifiers.addElement(tmp);
            System.out.println("MODIFIER PARSER : "+tmp);
        }
        return modifiers;
    }

    public static void checkReleasedModifiersTest(int testModifier, MouseEvent event){
        int [] curStandardExModifiers = getStandardExArray(testModifier);
        int button = event.getButton();
        int modifiers = event.getModifiers();
        int modifiersEx = event.getModifiersEx();
        int index = (button - 4)*3 + 1;
        dumpValues(button, modifiers, modifiersStandard, modifiersEx, curStandardExModifiers[index]);
        if (modifiers != modifiersStandard){
            MessageLogger.reportError("Test failed :  Released. modifiers != modifiersStandard");
        }

        if (modifiersEx != curStandardExModifiers[index]){
            MessageLogger.reportError("Test failed :  Released. modifiersEx != curStandardExModifiers. Got: "
                    + modifiersEx + " , Expected: " + curStandardExModifiers[index]);
        }

     //check event.paramString() output
        HashMap <String, String> paramStringElements = tokenizeParamString(event.paramString());
        checkButton(paramStringElements, button);
        checkModifiers(testModifier, paramStringElements, button);
        System.out.println("paramStringElements = "+paramStringElements);
        checkExtModifiersOnReleaseClick(testModifier, paramStringElements, button);
    }

    public static void checkClickedModifiersTest(int testModifier, MouseEvent event){
        int [] curStandardExModifiers = getStandardExArray(testModifier);
        int button = event.getButton();
        int modifiers = event.getModifiers();
        int modifiersEx = event.getModifiersEx();
        int index = (button - 4)*3 + 2;
        dumpValues(button, modifiers, modifiersStandard, modifiersEx, curStandardExModifiers[index]);
        if (modifiers != modifiersStandard){
            MessageLogger.reportError("Test failed :  Clicked. modifiers != modifiersStandard");
        }

        if (modifiersEx != curStandardExModifiers[index]){
            MessageLogger.reportError("Test failed :  Clicked. modifiersEx != curStandardExModifiers. Got: "
                    + modifiersEx + " , Expected: " + curStandardExModifiers[index]);
        }

     //check event.paramString() output
        HashMap <String, String> paramStringElements = tokenizeParamString(event.paramString());
        checkButton(paramStringElements, button);
        checkModifiers(testModifier, paramStringElements, button);
        checkExtModifiersOnReleaseClick(testModifier, paramStringElements, button);
    }

    private static int[] getStandardExArray(int testModifier) {
        int [] curStandardExModifiers;
        switch (testModifier){
            case SHIFT:
                curStandardExModifiers = modifiersExStandardSHIFT;
                break;
            case CTRL:
                curStandardExModifiers = modifiersExStandardCTRL;
                break;
            case ALT:
                curStandardExModifiers = modifiersExStandardALT;
                break;
            default: //NONE by default
                curStandardExModifiers = modifiersExStandard;
        }
        return curStandardExModifiers;
    }

    static Robot robot;
    public void init() {
        this.setLayout(new BorderLayout());
        try {
            robot  = new Robot();
            robot.setAutoDelay(100);
            robot.setAutoWaitForIdle(true);
        } catch (Exception e) {
            MessageLogger.reportError("Test failed. "+e);
        }
    }//End  init()

    public void start() {
        //Get things going.  Request focus, set size, et cetera
        setSize(200,200);
        setVisible(true);
        validate();
        if (autorun) {
            testNONE();
            testSHIFT();
            testCTRL();
            testALT();
        } else {
            switch (testModifier){
                case SHIFT:
                    this.addMouseListener(adapterTest2);
                    break;
                case CTRL:
                    this.addMouseListener(adapterTest3);
                    break;
                case ALT:
                    this.addMouseListener(adapterTest4);
                    break;
                default:  //NONE by default
                    this.addMouseListener(adapterTest1);
            }
        }
    }// start()

    //000000000000000000000000000000000000000000000000000000000000000
    public void testNONE(){
        this.addMouseListener(adapterTest1);
        robot.delay(1000);
        robot.mouseMove(getLocationOnScreen().x + getWidth()/2, getLocationOnScreen().y + getHeight()/2);
        for (int i = 3; i< mouseButtonDownMasks.length; i++){
            System.out.println("testNONE() => " + mouseButtonDownMasks[i]);
            robot.mousePress(mouseButtonDownMasks[i]);
            robot.mouseRelease(mouseButtonDownMasks[i]);
        }
        robot.delay(1000);
        this.removeMouseListener(adapterTest1);
    }

    public void testSHIFT(){
        this.addMouseListener(adapterTest2);
        robot.delay(1000);
        robot.mouseMove(getLocationOnScreen().x + getWidth()/2, getLocationOnScreen().y + getHeight()/2);
        for (int i = 3; i< mouseButtonDownMasks.length; i++){
            robot.keyPress(KeyEvent.VK_SHIFT);
            System.out.println("testSHIFT() => " + mouseButtonDownMasks[i]);
            robot.mousePress(mouseButtonDownMasks[i]);
            robot.mouseRelease(mouseButtonDownMasks[i]);
            robot.keyRelease(KeyEvent.VK_SHIFT);
        }
        robot.delay(1000);
        this.removeMouseListener(adapterTest2);
    }

    public void testCTRL(){
        this.addMouseListener(adapterTest3);
        robot.delay(1000);
        robot.mouseMove(getLocationOnScreen().x + getWidth()/2, getLocationOnScreen().y + getHeight()/2);
        for (int i = 3; i< mouseButtonDownMasks.length; i++){
            robot.keyPress(KeyEvent.VK_CONTROL);
            System.out.println("testCTRL() => " + mouseButtonDownMasks[i]);
            robot.mousePress(mouseButtonDownMasks[i]);
            robot.mouseRelease(mouseButtonDownMasks[i]);
            robot.keyRelease(KeyEvent.VK_CONTROL);
        }
        robot.delay(1000);
        this.removeMouseListener(adapterTest3);
    }

    public void testALT(){
        this.addMouseListener(adapterTest4);
        robot.delay(1000);
        robot.mouseMove(getLocationOnScreen().x + getWidth()/2, getLocationOnScreen().y + getHeight()/2);
        for (int i = 3; i< mouseButtonDownMasks.length; i++){
            robot.keyPress(KeyEvent.VK_ALT);
            System.out.println("testALT() => " + mouseButtonDownMasks[i]);
            robot.mousePress(mouseButtonDownMasks[i]);
            robot.mouseRelease(mouseButtonDownMasks[i]);
            robot.keyRelease(KeyEvent.VK_ALT);
        }
        robot.delay(1000);
        this.removeMouseListener(adapterTest4);
    }

    //**************************************************************************************************
    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("Button = "+button + "ModifiersEx = "+ modifiersEx + "standardEx = "+ modifiersExStandard);
    }

    public static void initParams(String []s){
        if (s.length != 3){
            autorun = true;
            debug = false;
            testModifier = NONE;
        } else {
            autorun = Boolean.valueOf(s[0]);
            debug = Boolean.valueOf(s[1]);

            if (s[2].equals("NONE")){
                testModifier = NONE;
            }
            if (s[2].equals("SHIFT")){
                testModifier = SHIFT;
            }
            if (s[2].equals("CTRL")){
                testModifier = CTRL;
            }
            if (s[2].equals("ALT")){
                testModifier = ALT;
            }
        }
        MessageLogger.setDebug(debug);
        System.out.println("Autorun : " +autorun);
        System.out.println("Debug mode : " +debug);
        System.out.println("Modifier to verify : " + testModifier);
    }

    public static void initAdapters(){
        adapterTest1 = new CheckingModifierAdapterExtra(NONE);
        adapterTest2 = new CheckingModifierAdapterExtra(SHIFT);
        adapterTest3 = new CheckingModifierAdapterExtra(CTRL);
        adapterTest4 = new CheckingModifierAdapterExtra(ALT);
    }

    public static void initVars(){
        //Init the array of the mouse button masks. It will be used for generating mouse events.
        mouseButtonDownMasks = new int [MouseInfo.getNumberOfButtons()];
        for (int i = 0; i < mouseButtonDownMasks.length; i++){
            mouseButtonDownMasks[i] = InputEvent.getMaskForButton(i+1);
            System.out.println("MouseArray [i] == "+mouseButtonDownMasks[i]);
        }

        // So we need to get the number of extra buttons on the mouse:  "MouseInfo.getNumberOfButtons() - 3"
        // and multyply on 3 because each button will generate three events : PRESS, RELEASE and CLICK.
        int [] tmp = new int [(MouseInfo.getNumberOfButtons()-3)*3];

        //Fill array of expected results for the case when mouse buttons are only used (no-modifier keys)
        Arrays.fill(tmp, 0);
        for (int i = 0, j = 3; i < tmp.length; i = i + 3, j++){
            tmp[i] = mouseButtonDownMasks[j];
        }
        modifiersExStandard = Arrays.copyOf(tmp, tmp.length);

        //Fill array of expected results for the case when mouse buttons are only used with SHIFT modifier key
        Arrays.fill(tmp, InputEvent.SHIFT_DOWN_MASK);
        for (int i = 0, j = 3; i < tmp.length; i = i + 3, j++){
            System.out.println("modifiersExStandardSHIFT FILLING : " + tmp[i] + " + " + mouseButtonDownMasks[j]);
            tmp[i] = tmp[i] | mouseButtonDownMasks[j];
        }
        modifiersExStandardSHIFT = Arrays.copyOf(tmp, tmp.length);

        //Fill array of expected results for the case when mouse buttons are only used with CTRL modifier key
        Arrays.fill(tmp, InputEvent.CTRL_DOWN_MASK);
        for (int i = 0, j = 3; i < tmp.length; i = i + 3, j++){
            System.out.println("modifiersExStandardCTRL FILLING : " + tmp[i] + " + " + mouseButtonDownMasks[j]);
            tmp[i] = tmp[i] | mouseButtonDownMasks[j];
        }
        modifiersExStandardCTRL = Arrays.copyOf(tmp, tmp.length);

        //Fill array of expected results for the case when mouse buttons are only used with ALT modifier key
        Arrays.fill(tmp, InputEvent.ALT_DOWN_MASK);
        for (int i = 0, j = 3; i < tmp.length; i = i + 3, j++){
            System.out.println("modifiersExStandardALT FILLING : " + tmp[i] + " + " + mouseButtonDownMasks[j]);
            tmp[i] = tmp[i] | mouseButtonDownMasks[j];
        }
        modifiersExStandardALT = Arrays.copyOf(tmp, tmp.length);
    }

    public static void main(String []s){
        if (MouseInfo.getNumberOfButtons() < 4){
            System.out.println("There are less than 4 buttons on the mouse. The test may not be accomplished. Skipping.");
            return;
        }
        initVars();
        MouseModifiersUnitTest_Extra frame = new MouseModifiersUnitTest_Extra();
        frame.initParams(s);
        frame.init();
        initAdapters();
        frame.start();
    }

}// class

/* A class that invoke appropriate verification
 * routine with current modifier.
 */
class CheckingModifierAdapterExtra extends MouseAdapter{
    int modifier;
    public CheckingModifierAdapterExtra(int modifier){
        this.modifier = modifier;
    }

    public void mousePressed(MouseEvent e) {
        System.out.println("PRESSED "+e);
        if (e.getButton() <= MouseEvent.BUTTON3) {
            System.out.println("Standard button affected. Skip.");
        } else {
            MouseModifiersUnitTest_Extra.checkPressedModifiersTest(modifier, e);
        }
    }
    public void mouseReleased(MouseEvent e) {
        System.out.println("RELEASED "+e);
        if (e.getButton() <= MouseEvent.BUTTON3) {
            System.out.println("Standard button affected. Skip.");
        } else {
            MouseModifiersUnitTest_Extra.checkReleasedModifiersTest(modifier, e);
        }
    }
    public void mouseClicked(MouseEvent e) {
        System.out.println("CLICKED "+e);
        if (e.getButton() <= MouseEvent.BUTTON3) {
            System.out.println("Standard button affected. Skip.");
        } else {
            MouseModifiersUnitTest_Extra.checkClickedModifiersTest(modifier, e);
        }
    }
}
//Utility class that could report a message depending on current purpose of the test run
class MessageLogger{
    private static boolean debug;

    public static void setDebug(boolean d){
        debug = d;
        log("Switch to "+ ((debug)?"debug":"trial") +" mode");
    }

    public static void log(String message){
        System.out.println(message);
    }

    public static void reportError(String message){
        if (debug){
            System.out.println(message);
        } else {
            throw new RuntimeException(message);
        }
    }
}