1 /*
   2  * Copyright (c) 2016, 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 consider making sun.awt.CausedFocusEvent functionality public
  28   @run main FocusCauseTest
  29 */
  30 
  31 
  32 import java.awt.*;
  33 import java.awt.event.FocusEvent.Cause;
  34 import java.awt.event.FocusEvent;
  35 import java.awt.event.FocusListener;
  36 import java.io.ByteArrayInputStream;
  37 import java.io.IOException;
  38 import java.io.InputStream;
  39 import java.io.ObjectInputStream;
  40 import java.lang.IllegalArgumentException;
  41 import java.lang.Override;
  42 import java.lang.RuntimeException;
  43 import java.util.Arrays;
  44 
  45 public class FocusCauseTest {
  46 
  47     private static Cause[] causes1 = {Cause.ACTIVATION,
  48             Cause.UNKNOWN, Cause.UNKNOWN, Cause.TRAVERSAL_FORWARD,
  49             Cause.TRAVERSAL_FORWARD, Cause.TRAVERSAL_BACKWARD,
  50             Cause.TRAVERSAL_BACKWARD, Cause.TRAVERSAL_UP,
  51             Cause.TRAVERSAL_DOWN, Cause.CLEAR_GLOBAL_FOCUS_OWNER};
  52     private static Cause[] causes2 = new Cause[10];
  53     private static int cnt;
  54 
  55     static byte[] data =
  56             {-84, -19, 0, 5, 115, 114, 0, 24, 115, 117, 110, 46, 97, 119,
  57             116, 46, 67, 97, 117, 115, 101, 100, 70, 111, 99, 117, 115, 69, 118,
  58             101, 110, 116, -51, 98, 39, -75, 86, 52, 107, 30, 2, 0, 1, 76, 0, 5,
  59             99, 97, 117, 115, 101, 116, 0, 32, 76, 115, 117, 110, 47, 97, 119,
  60             116, 47, 67, 97, 117, 115, 101, 100, 70, 111, 99, 117, 115, 69, 118,
  61             101, 110, 116, 36, 67, 97, 117, 115, 101, 59, 120, 114, 0, 25, 106,
  62             97, 118, 97, 46, 97, 119, 116, 46, 101, 118, 101, 110, 116, 46, 70,
  63             111, 99, 117, 115, 69, 118, 101, 110, 116, 7, 68, -65, 75, 55, -113,
  64             98, -52, 2, 0, 1, 90, 0, 9, 116, 101, 109, 112, 111, 114, 97, 114,
  65             121, 120, 114, 0, 29, 106, 97, 118, 97, 46, 97, 119, 116, 46, 101,
  66             118, 101, 110, 116, 46, 67, 111, 109, 112, 111, 110, 101, 110, 116,
  67             69, 118, 101, 110, 116, 112, 109, -6, -107, 79, -87, -38, 69, 2, 0,
  68             0, 120, 114, 0, 17, 106, 97, 118, 97, 46, 97, 119, 116, 46, 65, 87,
  69             84, 69, 118, 101, 110, 116, -26, -85, 45, -31, 24, -33, -118, -61,
  70             2, 0, 3, 90, 0, 8, 99, 111, 110, 115, 117, 109, 101, 100, 73, 0, 2,
  71             105, 100, 91, 0, 5, 98, 100, 97, 116, 97, 116, 0, 2, 91, 66, 120,
  72             114, 0, 21, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 69, 118,
  73             101, 110, 116, 79, 98, 106, 101, 99, 116, 76, -115, 9, 78, 24, 109,
  74             125, -88, 2, 0, 0, 120, 112, 0, 0, 0, 3, -20, 112, 0, 126, 114, 0,
  75             30, 115, 117, 110, 46, 97, 119, 116, 46, 67, 97, 117, 115, 101, 100,
  76             70, 111, 99, 117, 115, 69, 118, 101, 110, 116, 36, 67, 97, 117, 115,
  77             101, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 120, 114, 0, 14, 106, 97,
  78             118, 97, 46, 108, 97, 110, 103, 46, 69, 110, 117, 109, 0, 0, 0, 0,
  79             0, 0, 0, 0, 18, 0, 0, 120, 112, 116, 0};
  80 
  81     static byte[] dataOld =
  82             {-84, -19, 0, 5, 115, 114, 0, 25, 106, 97, 118, 97, 46, 97, 119,
  83             116, 46, 101, 118, 101, 110, 116, 46, 70, 111, 99, 117, 115, 69,
  84             118, 101, 110, 116, 7, 68, -65, 75, 55, -113, 98, -52, 2, 0, 1, 90,
  85             0, 9, 116, 101, 109, 112, 111, 114, 97, 114, 121, 120, 114, 0, 29,
  86             106, 97, 118, 97, 46, 97, 119, 116, 46, 101, 118, 101, 110, 116, 46,
  87             67, 111, 109, 112, 111, 110, 101, 110, 116, 69, 118, 101, 110, 116,
  88             112, 109, -6, -107, 79, -87, -38, 69, 2, 0, 0, 120, 114, 0, 17, 106,
  89             97, 118, 97, 46, 97, 119, 116, 46, 65, 87, 84, 69, 118, 101, 110,
  90             116, -26, -85, 45, -31, 24, -33, -118, -61, 2, 0, 3, 90, 0, 8, 99,
  91             111, 110, 115, 117, 109, 101, 100, 73, 0, 2, 105, 100, 91, 0, 5, 98,
  92             100, 97, 116, 97, 116, 0, 2, 91, 66, 120, 114, 0, 21, 106, 97, 118,
  93             97, 46, 117, 116, 105, 108, 46, 69, 118, 101, 110, 116, 79, 98, 106,
  94             101, 99, 116, 76, -115, 9, 78, 24, 109, 125, -88, 2, 0, 0, 120, 112,
  95             0, 0, 0, 0, 100, 112, 0};
  96 
  97     static String[] causesIn = {"UNKNOWN", "MOUSE_EVENT", "TRAVERSAL",
  98             "TRAVERSAL_UP", "TRAVERSAL_DOWN", "TRAVERSAL_FORWARD",
  99             "TRAVERSAL_BACKWARD", "MANUAL_REQUEST", "AUTOMATIC_TRAVERSE"
 100             ,"ROLLBACK", "NATIVE_SYSTEM", "ACTIVATION",
 101             "CLEAR_GLOBAL_FOCUS_OWNER", "RETARGETED"};
 102 
 103     static FocusEvent.Cause[] causesOut = {FocusEvent.Cause.UNKNOWN,
 104             FocusEvent.Cause.MOUSE_EVENT,
 105             FocusEvent.Cause.TRAVERSAL, FocusEvent.Cause.TRAVERSAL_UP,
 106             FocusEvent.Cause.TRAVERSAL_DOWN, FocusEvent.Cause.TRAVERSAL_FORWARD,
 107             FocusEvent.Cause.TRAVERSAL_BACKWARD, FocusEvent.Cause.UNKNOWN,
 108             FocusEvent.Cause.UNKNOWN, FocusEvent.Cause.ROLLBACK,
 109             FocusEvent.Cause.UNEXPECTED, FocusEvent.Cause.ACTIVATION,
 110             FocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER, FocusEvent.Cause.UNKNOWN
 111     };
 112 
 113     public static void main(String[] args) throws Exception {
 114         testCauses();
 115         testNullCause();
 116         testCausedFocusEventDeserialization();
 117         testFocusEventDeserialization();
 118         System.out.println("ok");
 119     }
 120 
 121     private static void testNullCause() {
 122         try {
 123             new FocusEvent(new Frame(), FocusEvent.FOCUS_GAINED, true,
 124                     null, null);
 125             throw new RuntimeException("Exception is not thrown when the " +
 126                     "cause is null");
 127         } catch (IllegalArgumentException e) {
 128         }
 129     }
 130 
 131     private static void testCauses() throws Exception {
 132         cnt = 0;
 133         Frame frame = new Frame();
 134         TextField comp1 = new TextField();
 135         comp1.addFocusListener(new FocusListener() {
 136             @Override
 137             public void focusGained(FocusEvent e) {
 138                 System.out.println(e.getCause());
 139                 causes2[cnt++] = e.getCause();
 140             }
 141 
 142             @Override
 143             public void focusLost(FocusEvent e) {
 144                 System.out.println(e.getCause());
 145                 causes2[cnt++] = e.getCause();
 146             }
 147         });
 148         TextField comp2 = new TextField();
 149         comp2.addFocusListener(new FocusListener() {
 150             @Override
 151             public void focusGained(FocusEvent e) {
 152                 System.out.println(e.getCause());
 153                 causes2[cnt++] = e.getCause();
 154             }
 155 
 156             @Override
 157             public void focusLost(FocusEvent e) {
 158                 System.out.println(e.getCause());
 159                 causes2[cnt++] = e.getCause();
 160             }
 161         });
 162         frame.add(comp1, BorderLayout.NORTH);
 163         frame.add(comp2, BorderLayout.SOUTH);
 164         frame.setVisible(true);
 165 
 166         Robot robot = new Robot();
 167         robot.delay(200);
 168         robot.waitForIdle();
 169         comp2.requestFocus();
 170         robot.waitForIdle();
 171         comp2.transferFocus();
 172         robot.waitForIdle();
 173         comp1.transferFocusBackward();
 174         robot.waitForIdle();
 175         comp2.transferFocusUpCycle();
 176         robot.waitForIdle();
 177         frame.transferFocusDownCycle();
 178         robot.waitForIdle();
 179         frame.dispose();
 180         robot.waitForIdle();
 181         if (!Arrays.equals(causes1, causes2)) {
 182             throw new RuntimeException("wrong cause " + causes2);
 183         }
 184     }
 185 
 186     private static void  testCausedFocusEventDeserialization() throws
 187             Exception {
 188         for (int i = 0; i < causesIn.length; i++) {
 189             final String causeIn = causesIn[i];
 190             ObjectInputStream oi = new ObjectInputStream(new InputStream() {
 191                 int cnt = 0;
 192                 @Override
 193                 public int read() throws IOException {
 194                     if(cnt < data.length) {
 195                         return data[cnt++];
 196                     } else if(cnt == data.length){
 197                         cnt++;
 198                         return causeIn.length();
 199                     } else if(cnt - data.length - 1 < causeIn.length()) {
 200                         return causeIn.getBytes()[cnt++ - data.length - 1];
 201                     }
 202                     return -1;
 203                 }
 204             });
 205             FocusEvent ev = (FocusEvent) oi.readObject();
 206             System.out.println(ev);
 207             if(ev.getCause() != causesOut[i]) {
 208                 throw new RuntimeException("Wrong cause read :" +ev.getCause());
 209             }
 210         }
 211     }
 212 
 213     private static void testFocusEventDeserialization() throws
 214             Exception {
 215         ObjectInputStream oi = new ObjectInputStream(
 216                 new ByteArrayInputStream(dataOld));
 217         FocusEvent ev = (FocusEvent)oi.readObject();
 218         if(ev.getCause() != FocusEvent.Cause.UNKNOWN) {
 219             throw new RuntimeException("Wrong cause in deserialized FocusEvent "
 220                     + ev.getCause());
 221         }
 222     }
 223 
 224 }