1 /*
   2  * Copyright (c) 20015, 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   @bug 8080395
  27   @summary
  28   @author    Semyon Sadetsky
  29   @run       main AutoRequestFocusSetVisibleTest
  30 */
  31 
  32 
  33 import java.awt.*;
  34 import java.awt.event.FocusEvent.Cause;
  35 import java.awt.event.FocusEvent;
  36 import java.awt.event.FocusListener;
  37 import java.util.Arrays;
  38 
  39 public class FocusCauseTest {
  40 
  41     private static Cause[] causes1 = {Cause.ACTIVATION,
  42             Cause.UNKNOWN, Cause.UNKNOWN, Cause.TRAVERSAL_FORWARD,
  43             Cause.TRAVERSAL_FORWARD, Cause.TRAVERSAL_BACKWARD,
  44             Cause.TRAVERSAL_BACKWARD, Cause.TRAVERSAL_UP,
  45             Cause.TRAVERSAL_DOWN};
  46     private static Cause[] causes2 = new Cause[9];
  47     private static int cnt;
  48 
  49     public static void main(String[] args) throws Exception {
  50         Frame frame = new Frame();
  51         TextField comp1 = new TextField();
  52         comp1.addFocusListener(new FocusListener() {
  53             @Override
  54             public void focusGained(FocusEvent e) {
  55                 causes2[cnt++] = e.getCause();
  56             }
  57 
  58             @Override
  59             public void focusLost(FocusEvent e) {
  60                 causes2[cnt++] = e.getCause();
  61             }
  62         });
  63         TextField comp2 = new TextField();
  64         comp2.addFocusListener(new FocusListener() {
  65             @Override
  66             public void focusGained(FocusEvent e) {
  67                 causes2[cnt++] = e.getCause();
  68             }
  69 
  70             @Override
  71             public void focusLost(FocusEvent e) {
  72                 causes2[cnt++] = e.getCause();
  73             }
  74         });
  75         frame.add(comp1, BorderLayout.NORTH);
  76         frame.add(comp2, BorderLayout.SOUTH);
  77         frame.setVisible(true);
  78 
  79         Robot robot = new Robot();
  80         robot.delay(200);
  81         robot.waitForIdle();
  82         comp2.requestFocus();
  83         robot.waitForIdle();
  84         comp2.transferFocus();
  85         robot.waitForIdle();
  86         comp1.transferFocusBackward();
  87         robot.waitForIdle();
  88         comp2.transferFocusUpCycle();
  89         robot.waitForIdle();
  90         frame.transferFocusDownCycle();
  91         robot.waitForIdle();
  92         if (!Arrays.equals(causes1, causes2)) {
  93             throw new RuntimeException("wrong cause " + causes2);
  94         }
  95         cnt = 0;
  96         frame.dispose();
  97         System.out.println("ok");
  98     }
  99 }