1 /*
   2  * Copyright (c) 2016, 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.
   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 
  24 /**
  25  * @test
  26  * @key headful
  27  * @bug 8154434
  28  * @summary Open the request focus methods of the java.awt.Component which accept
  29  *          FocusEvent.Cause
  30  * @run main RequestFocusByCauseTest
  31  */
  32 
  33 import java.awt.*;
  34 import java.awt.event.FocusEvent;
  35 import java.awt.event.FocusListener;
  36 
  37 public class RequestFocusByCauseTest {
  38     static boolean success;
  39 
  40     public static void main(String[] args) throws Exception {
  41         testRequestFocusCause();
  42         testRequestFocusTemporaryCause();
  43         testRequestFocusInWindowCause();
  44         System.out.println("ok");
  45     }
  46 
  47     private static void testRequestFocusCause() throws AWTException {
  48         Frame frame = new Frame();
  49         Component c = new Button();
  50         frame.add(new Button());
  51         frame.add(c);
  52         c.addFocusListener(new FocusListener() {
  53             @Override
  54             public void focusGained(FocusEvent e) {
  55                 success = e.getCause() == FocusEvent.Cause.UNEXPECTED;
  56             }
  57 
  58             @Override
  59             public void focusLost(FocusEvent e) {}
  60         });
  61         Robot robot = new Robot();
  62 
  63         try {
  64             frame.setVisible(true);
  65             robot.waitForIdle();
  66             robot.delay(200);
  67             success = false;
  68 
  69             c.requestFocus(FocusEvent.Cause.UNEXPECTED);
  70             robot.waitForIdle();
  71             robot.delay(200);
  72             if(!success) {
  73                 throw new RuntimeException("request failed");
  74             }
  75         } finally {
  76             frame.dispose();
  77         }
  78     }
  79 
  80     private static void testRequestFocusTemporaryCause() throws AWTException {
  81         Frame frame = new Frame();
  82         frame.add(new Button() {
  83             @Override
  84             protected boolean requestFocus(boolean temporary,
  85                                                        FocusEvent.Cause cause) {
  86                 success = cause == FocusEvent.Cause.ROLLBACK;
  87                 return super.requestFocus(temporary, cause);
  88             }
  89         });
  90         Component c = new Button() {
  91             @Override
  92             public void requestFocus() {
  93                 super.requestFocus();
  94                 setFocusable(false);
  95             }
  96         };
  97         frame.add(c);
  98         Robot robot = new Robot();
  99 
 100         try {
 101             frame.setVisible(true);
 102             robot.waitForIdle();
 103             robot.delay(200);
 104 
 105             success = false;
 106             c.requestFocus();
 107             robot.waitForIdle();
 108             robot.delay(200);
 109 
 110 
 111             if(!success) {
 112                 throw new RuntimeException("rollback request is not triggered");
 113             }
 114         } finally {
 115             frame.dispose();
 116         }
 117     }
 118 
 119     private static void testRequestFocusInWindowCause() throws AWTException {
 120         Frame frame = new Frame();
 121         Component c = new Button();
 122         frame.add(new Button());
 123         frame.add(c);
 124         c.addFocusListener(new FocusListener() {
 125             @Override
 126             public void focusGained(FocusEvent e) {
 127                 success = e.getCause() == FocusEvent.Cause.UNEXPECTED;
 128             }
 129 
 130             @Override
 131             public void focusLost(FocusEvent e) {
 132             }
 133         });
 134         Robot robot = new Robot();
 135 
 136         try {
 137             frame.setVisible(true);
 138             robot.waitForIdle();
 139             robot.delay(200);
 140             success = false;
 141 
 142             c.requestFocusInWindow(FocusEvent.Cause.UNEXPECTED);
 143             robot.waitForIdle();
 144             robot.delay(200);
 145             if (!success) {
 146                 throw new RuntimeException("request in window failed");
 147             }
 148         } finally {
 149             frame.dispose();
 150         }
 151     }
 152 }
 153