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