Path: blob/master/test/jdk/java/awt/Focus/Cause/FocusCauseTest.java
41152 views
/*1* Copyright (c) 2016, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 808039527@summary consider making sun.awt.CausedFocusEvent functionality public28@run main FocusCauseTest29*/303132import java.awt.*;33import java.awt.event.FocusEvent.Cause;34import java.awt.event.FocusEvent;35import java.awt.event.FocusListener;36import java.io.ByteArrayInputStream;37import java.io.IOException;38import java.io.InputStream;39import java.io.ObjectInputStream;40import java.lang.IllegalArgumentException;41import java.lang.Override;42import java.lang.RuntimeException;43import java.util.Arrays;4445public class FocusCauseTest {4647private static Cause[] causes1 = {Cause.ACTIVATION,48Cause.UNKNOWN, Cause.UNKNOWN, Cause.TRAVERSAL_FORWARD,49Cause.TRAVERSAL_FORWARD, Cause.TRAVERSAL_BACKWARD,50Cause.TRAVERSAL_BACKWARD, Cause.TRAVERSAL_UP,51Cause.TRAVERSAL_DOWN, Cause.CLEAR_GLOBAL_FOCUS_OWNER};52private static Cause[] causes2 = new Cause[10];53private static int cnt;5455static byte[] data =56{-84, -19, 0, 5, 115, 114, 0, 24, 115, 117, 110, 46, 97, 119,57116, 46, 67, 97, 117, 115, 101, 100, 70, 111, 99, 117, 115, 69, 118,58101, 110, 116, -51, 98, 39, -75, 86, 52, 107, 30, 2, 0, 1, 76, 0, 5,5999, 97, 117, 115, 101, 116, 0, 32, 76, 115, 117, 110, 47, 97, 119,60116, 47, 67, 97, 117, 115, 101, 100, 70, 111, 99, 117, 115, 69, 118,61101, 110, 116, 36, 67, 97, 117, 115, 101, 59, 120, 114, 0, 25, 106,6297, 118, 97, 46, 97, 119, 116, 46, 101, 118, 101, 110, 116, 46, 70,63111, 99, 117, 115, 69, 118, 101, 110, 116, 7, 68, -65, 75, 55, -113,6498, -52, 2, 0, 1, 90, 0, 9, 116, 101, 109, 112, 111, 114, 97, 114,65121, 120, 114, 0, 29, 106, 97, 118, 97, 46, 97, 119, 116, 46, 101,66118, 101, 110, 116, 46, 67, 111, 109, 112, 111, 110, 101, 110, 116,6769, 118, 101, 110, 116, 112, 109, -6, -107, 79, -87, -38, 69, 2, 0,680, 120, 114, 0, 17, 106, 97, 118, 97, 46, 97, 119, 116, 46, 65, 87,6984, 69, 118, 101, 110, 116, -26, -85, 45, -31, 24, -33, -118, -61,702, 0, 3, 90, 0, 8, 99, 111, 110, 115, 117, 109, 101, 100, 73, 0, 2,71105, 100, 91, 0, 5, 98, 100, 97, 116, 97, 116, 0, 2, 91, 66, 120,72114, 0, 21, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 69, 118,73101, 110, 116, 79, 98, 106, 101, 99, 116, 76, -115, 9, 78, 24, 109,74125, -88, 2, 0, 0, 120, 112, 0, 0, 0, 3, -20, 112, 0, 126, 114, 0,7530, 115, 117, 110, 46, 97, 119, 116, 46, 67, 97, 117, 115, 101, 100,7670, 111, 99, 117, 115, 69, 118, 101, 110, 116, 36, 67, 97, 117, 115,77101, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 120, 114, 0, 14, 106, 97,78118, 97, 46, 108, 97, 110, 103, 46, 69, 110, 117, 109, 0, 0, 0, 0,790, 0, 0, 0, 18, 0, 0, 120, 112, 116, 0};8081static byte[] dataOld =82{-84, -19, 0, 5, 115, 114, 0, 25, 106, 97, 118, 97, 46, 97, 119,83116, 46, 101, 118, 101, 110, 116, 46, 70, 111, 99, 117, 115, 69,84118, 101, 110, 116, 7, 68, -65, 75, 55, -113, 98, -52, 2, 0, 1, 90,850, 9, 116, 101, 109, 112, 111, 114, 97, 114, 121, 120, 114, 0, 29,86106, 97, 118, 97, 46, 97, 119, 116, 46, 101, 118, 101, 110, 116, 46,8767, 111, 109, 112, 111, 110, 101, 110, 116, 69, 118, 101, 110, 116,88112, 109, -6, -107, 79, -87, -38, 69, 2, 0, 0, 120, 114, 0, 17, 106,8997, 118, 97, 46, 97, 119, 116, 46, 65, 87, 84, 69, 118, 101, 110,90116, -26, -85, 45, -31, 24, -33, -118, -61, 2, 0, 3, 90, 0, 8, 99,91111, 110, 115, 117, 109, 101, 100, 73, 0, 2, 105, 100, 91, 0, 5, 98,92100, 97, 116, 97, 116, 0, 2, 91, 66, 120, 114, 0, 21, 106, 97, 118,9397, 46, 117, 116, 105, 108, 46, 69, 118, 101, 110, 116, 79, 98, 106,94101, 99, 116, 76, -115, 9, 78, 24, 109, 125, -88, 2, 0, 0, 120, 112,950, 0, 0, 0, 100, 112, 0};9697static 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"};102103static FocusEvent.Cause[] causesOut = {FocusEvent.Cause.UNKNOWN,104FocusEvent.Cause.MOUSE_EVENT,105FocusEvent.Cause.TRAVERSAL, FocusEvent.Cause.TRAVERSAL_UP,106FocusEvent.Cause.TRAVERSAL_DOWN, FocusEvent.Cause.TRAVERSAL_FORWARD,107FocusEvent.Cause.TRAVERSAL_BACKWARD, FocusEvent.Cause.UNKNOWN,108FocusEvent.Cause.UNKNOWN, FocusEvent.Cause.ROLLBACK,109FocusEvent.Cause.UNEXPECTED, FocusEvent.Cause.ACTIVATION,110FocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER, FocusEvent.Cause.UNKNOWN111};112113public static void main(String[] args) throws Exception {114testCauses();115testNullCause();116testCausedFocusEventDeserialization();117testFocusEventDeserialization();118System.out.println("ok");119}120121private static void testNullCause() {122try {123new FocusEvent(new Frame(), FocusEvent.FOCUS_GAINED, true,124null, null);125throw new RuntimeException("Exception is not thrown when the " +126"cause is null");127} catch (IllegalArgumentException e) {128}129}130131private static void testCauses() throws Exception {132cnt = 0;133Frame frame = new Frame();134TextField comp1 = new TextField();135comp1.addFocusListener(new FocusListener() {136@Override137public void focusGained(FocusEvent e) {138System.out.println(e.getCause());139causes2[cnt++] = e.getCause();140}141142@Override143public void focusLost(FocusEvent e) {144System.out.println(e.getCause());145causes2[cnt++] = e.getCause();146}147});148TextField comp2 = new TextField();149comp2.addFocusListener(new FocusListener() {150@Override151public void focusGained(FocusEvent e) {152System.out.println(e.getCause());153causes2[cnt++] = e.getCause();154}155156@Override157public void focusLost(FocusEvent e) {158System.out.println(e.getCause());159causes2[cnt++] = e.getCause();160}161});162frame.add(comp1, BorderLayout.NORTH);163frame.add(comp2, BorderLayout.SOUTH);164frame.setVisible(true);165166Robot robot = new Robot();167robot.delay(200);168robot.waitForIdle();169comp2.requestFocus();170robot.waitForIdle();171comp2.transferFocus();172robot.waitForIdle();173comp1.transferFocusBackward();174robot.waitForIdle();175comp2.transferFocusUpCycle();176robot.waitForIdle();177frame.transferFocusDownCycle();178robot.waitForIdle();179frame.dispose();180robot.waitForIdle();181if (!Arrays.equals(causes1, causes2)) {182throw new RuntimeException("wrong cause " + causes2);183}184}185186private static void testCausedFocusEventDeserialization() throws187Exception {188for (int i = 0; i < causesIn.length; i++) {189final String causeIn = causesIn[i];190ObjectInputStream oi = new ObjectInputStream(new InputStream() {191int cnt = 0;192@Override193public int read() throws IOException {194if(cnt < data.length) {195return data[cnt++];196} else if(cnt == data.length){197cnt++;198return causeIn.length();199} else if(cnt - data.length - 1 < causeIn.length()) {200return causeIn.getBytes()[cnt++ - data.length - 1];201}202return -1;203}204});205FocusEvent ev = (FocusEvent) oi.readObject();206System.out.println(ev);207if(ev.getCause() != causesOut[i]) {208throw new RuntimeException("Wrong cause read :" +ev.getCause());209}210}211}212213private static void testFocusEventDeserialization() throws214Exception {215ObjectInputStream oi = new ObjectInputStream(216new ByteArrayInputStream(dataOld));217FocusEvent ev = (FocusEvent)oi.readObject();218if(ev.getCause() != FocusEvent.Cause.UNKNOWN) {219throw new RuntimeException("Wrong cause in deserialized FocusEvent "220+ ev.getCause());221}222}223224}225226227