1 /*
   2  * Copyright (c) 2015, 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  * @test
  24  * @bug 8067470
  25  * @summary Examine if the replacement for sun.awt.SunToolkit.setLWRequestStatus
  26  *          should be provided
  27  * @run main/othervm -Dawt.enableSyncLWFocus LightWeightSyncFocusSetTest
  28  */
  29 
  30 import javax.swing.*;
  31 import java.awt.*;
  32 import java.awt.event.FocusEvent;
  33 import java.awt.event.FocusListener;
  34 import java.awt.event.InputEvent;
  35 
  36 public class LightWeightSyncFocusSetTest {
  37 
  38     private static int gained1;
  39     private static int gained2;
  40     private static int lost1;
  41     private static int lost2;
  42     private static Window window;
  43     private static JComponent comp1;
  44     private static JComponent comp2;
  45     private static Robot robot;
  46 
  47     public static void main(String[] args) throws Exception {
  48         if( System.getProperty("awt.enableSyncLWFocus") == null ) {
  49             throw new RuntimeException("-Dawt.enableSyncLWFocus is missing");
  50         }
  51 
  52         robot = new Robot();
  53         robot.setAutoDelay(10);
  54 
  55         setup();
  56 
  57         transferFocus();
  58 
  59         int asyncGained1 = gained1;
  60         int asyncGained2 = gained2;
  61         int asyncLost1 = lost1;
  62         int asyncLost2 = lost2;
  63 
  64         window.dispose();
  65         setup();
  66 
  67         window.setSyncLWFocus(true);
  68 
  69         transferFocus();
  70 
  71         if (gained1 != asyncGained1 ||
  72             gained2 != asyncGained2 ||
  73             lost1 != asyncLost1 ||
  74             lost2 != asyncLost2 ) {
  75             window.dispose();
  76             throw new RuntimeException("Sync focus send different events");
  77         }
  78 
  79         window.dispose();
  80         System.out.println("ok");
  81 
  82     }
  83 
  84     private static void transferFocus() {
  85         gained1 = gained2 = lost1 = lost2 = 0;
  86         Point point = comp2.getLocationOnScreen();
  87         robot.mouseMove(point.x + 1, point.y + 1);
  88         robot.mousePress(InputEvent.BUTTON1_MASK);
  89         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  90         point = comp1.getLocationOnScreen();
  91         robot.mouseMove(point.x + 1, point.y + 1);
  92         robot.mousePress(InputEvent.BUTTON1_MASK);
  93         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  94         robot.delay(200);
  95         robot.waitForIdle();
  96     }
  97 
  98     private static void setup() {
  99         window = new Frame();
 100         comp1 = new JTextField("1");
 101         comp2 = new JTextField("2");
 102 
 103         window.setLayout(new FlowLayout());
 104         window.add(comp1);
 105         window.add(comp2);
 106         window.pack();
 107         window.setVisible(true);
 108         comp1.addFocusListener(new FocusListener() {
 109             @Override
 110             public void focusGained(FocusEvent e) {
 111                 gained1++;
 112             }
 113 
 114             @Override
 115             public void focusLost(FocusEvent e) {
 116                 lost1++;
 117             }
 118         });
 119         comp2.addFocusListener(new FocusListener() {
 120             @Override
 121             public void focusGained(FocusEvent e) {
 122                 gained2++;
 123             }
 124 
 125             @Override
 126             public void focusLost(FocusEvent e) {
 127                 lost2++;
 128             }
 129         });
 130         window.setVisible(true);
 131         robot.delay(200);
 132         robot.waitForIdle();
 133     }
 134 }