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