1 /* 2 * Copyright (c) 2004, 2014, 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 import java.awt.*; 25 import java.awt.event.*; 26 import java.util.*; 27 28 /* 29 * @test 30 * @summary Set the Focus Traversal Policy and check whether the First, Initial, 31 * Default & Last Components are returning properly. Try to traverse 32 * the focus forward and backward within a focus cycle using the traversal 33 * key explicitly set by the user. 34 * @author Aruna Samji 35 * @library ../../../lib/testlibrary 36 * @build ExtendedRobot 37 * @run main TaskFocusTraversalPolicy 38 */ 39 40 public class TaskFocusTraversalPolicy extends Task<GUIMerlinFocus> { 41 42 public static void main (String[] args) throws Exception { 43 new TaskFocusTraversalPolicy(GUIMerlinFocus.class, new ExtendedRobot()).runTask(); 44 } 45 46 TaskFocusTraversalPolicy(Class guiClass, ExtendedRobot robot) throws Exception { 47 super(guiClass, robot); 48 } 49 50 public void task() throws Exception { 51 EventQueue.invokeAndWait(() -> gui.testFrame1.setVisible(true)); 52 robot.waitForIdle(1000); 53 54 if (gui.focusGained5 == null || ! gui.focusGained5.equals(gui.button7)) 55 throw new RuntimeException("Default Component is not getting focus"); 56 57 58 EventQueue.invokeAndWait(gui.testFrame1::dispose); 59 robot.waitForIdle(1000); 60 61 // 1.checking whether 62 // i)The InitialComponent is getting focus when the 63 // new FocusTraversalPolicy is set for a container. 64 // ii)checking whether the First, Initial, Default, Next and 65 // Previous Components are returned properly as per the 66 // traversal policy set for the container. 67 68 //Setting the traversal policy for the frame. 69 if ( (gui.cp.getInitialComponent(gui.testFrame)) != gui.choice5) 70 throw new RuntimeException("Initial Component is not returning correctly."); 71 72 if ( (gui.cp.getFirstComponent(gui.testFrame)) != gui.choice5) 73 throw new RuntimeException("First Component is not returning correctly."); 74 75 if ( (gui.cp.getDefaultComponent(gui.testFrame)) != gui.choice5) 76 throw new RuntimeException("Default Component is not returning correctly."); 77 78 if ( (gui.cp.getLastComponent(gui.testFrame)) != gui.checkbox5) 79 throw new RuntimeException("Last Component is not returning correctly."); 80 81 if ( (gui.cp.getComponentAfter(gui.testFrame,gui.button5)) != gui.choice5) 82 throw new RuntimeException("Next Component is not returned correctly for button."); 83 84 if ( (gui.cp.getComponentAfter(gui.testFrame,gui.list5)) != gui.textfield5) 85 throw new RuntimeException("Next Component is not returned correctly for list."); 86 87 if ( (gui.cp.getComponentAfter(gui.testFrame,gui.button6)) != gui.button5) 88 throw new RuntimeException("Next Component is not returned correctly for button2."); 89 90 if ( (gui.cp.getComponentAfter(gui.testFrame,gui.choice5)) != gui.list5) 91 throw new RuntimeException("Next Component is not returned correctly for choice."); 92 93 if ( (gui.cp.getComponentAfter(gui.testFrame,gui.textfield5)) != gui.button6) 94 throw new RuntimeException("Next Component is not returned correctly for textfield."); 95 96 if ( (gui.cp.getComponentBefore(gui.testFrame,gui.button5)) != gui.button6) 97 throw new RuntimeException("Previous Component is not returned correctly for button."); 98 99 if ( (gui.cp.getComponentBefore(gui.testFrame,gui.list5)) != gui.choice5) 100 throw new RuntimeException("Previous Component is not returned correctly for list."); 101 102 if ( (gui.cp.getComponentBefore(gui.testFrame,gui.button6)) != gui.textfield5) 103 throw new RuntimeException("Previous Component is not returned correctly for button2."); 104 105 if ( (gui.cp.getComponentBefore(gui.testFrame,gui.choice5)) != gui.button5) 106 throw new RuntimeException("Previous Component is not returned correctly for choice."); 107 108 if ( (gui.cp.getComponentBefore(gui.testFrame,gui.textfield5)) != gui.list5) 109 throw new RuntimeException("Previous Component is not returned correctly for textfield."); 110 111 EventQueue.invokeAndWait(() -> gui.testFrame.setVisible(true)); 112 robot.waitForIdle(1000); 113 114 if (gui.focusGained5 != gui.choice5) 115 throw new RuntimeException("Initial Component is not getting focus " + 116 "after setting the traversal policy"); 117 118 EventQueue.invokeAndWait(gui.testFrame::dispose); 119 120 //Forward and backward traversal 121 EventQueue.invokeAndWait(() -> { 122 gui.frame5.setSize(500,270); 123 gui.frame5.setVisible(true); 124 }); 125 robot.waitForIdle(1000); 126 127 HashSet hs = new HashSet(); 128 HashSet hs1 = new HashSet(); 129 130 //Assersion:By default, Containers must not become the 131 // Focus Cycle Roots without programmatically 132 // setting it as a Focus Cycle Root. 133 134 if (gui.firstpanel.isFocusCycleRoot()) 135 throw new RuntimeException("By default, Containers becomes the Focus " + 136 "Cycle Roots without programmatically setting it as a " + 137 "Focus Cycle Root."); 138 139 EventQueue.invokeAndWait(() -> { 140 gui.firstpanel.setFocusCycleRoot(true); 141 gui.frame5.toFront(); 142 }); 143 robot.waitForIdle(1000); 144 145 Point firstButtonCenter = gui.firstbutton.getLocationOnScreen(); 146 firstButtonCenter.translate(gui.firstbutton.getWidth()/2, gui.firstbutton.getHeight()/2); 147 robot.mouseMove(gui.firstbutton.getLocationOnScreen()); 148 robot.glide(gui.firstbutton.getLocationOnScreen(), firstButtonCenter); 149 robot.click(); 150 robot.waitForIdle(1000); 151 152 //Assersion:When the focus owner is a Component inside a particular 153 // cycle, normal forward and backward focus traversal must 154 // not move the focus owner above the focus cycle root in 155 // the Component hierarchy 156 157 //TAB key is pressed 158 robot.type(KeyEvent.VK_TAB); 159 robot.waitForIdle(1000); 160 161 if (gui.compgain != gui.firstchoice) 162 throw new RuntimeException("When the focus owner is a Component " + 163 "inside a particular cycle, normal forward and backward " + 164 "focus traversal moves the focus owner above the focus " + 165 "cycle root in the Component hierarchy"); 166 167 //TAB key is pressed 168 robot.type(KeyEvent.VK_TAB); 169 robot.waitForIdle(1000); 170 171 if (gui.compgain != gui.firsttextfield) 172 throw new RuntimeException("When the focus owner is a Component " + 173 "inside a particular cycle, normal forward and backward " + 174 "focus traversal moves the focus owner above the focus " + 175 "cycle root in the Component hierarchy"); 176 177 robot.type(KeyEvent.VK_TAB); 178 robot.waitForIdle(1000); 179 180 if (gui.compgain != gui.firstbutton) 181 throw new RuntimeException("When the focus owner is a Component " + 182 "inside a particular cycle, normal forward and backward " + 183 "focus traversal moves the focus owner above the focus " + 184 "cycle root in the Component hierarchy"); 185 186 //Assersion:Most recent Focus Owner must be either of the following 187 // 1) Focused Component 188 // 2) A Component which must gain focus when its 189 // parent Window is focused 190 191 EventQueue.invokeAndWait(() -> gui.manager.downFocusCycle(gui.firstpanel)); 192 robot.waitForIdle(1000); 193 194 if (gui.compgain != gui.firstbutton) 195 throw new RuntimeException("Component does not gain focus when its " + 196 "parent Window is focused "); 197 198 //Assersion:When a Key is set as forward traversal key 199 // (KeyboardFocusManager.FORWARD_TRAVERSAL_KEY), 200 // the key must generate a forward focus traversal 201 // for every each component. 202 203 EventQueue.invokeAndWait(() -> { 204 gui.firstbutton.requestFocusInWindow(); 205 206 //setting "B" as FORWARD_TRAVERSAL_KEY. 207 hs.add(java.awt.AWTKeyStroke.getAWTKeyStroke("B")); 208 209 KeyboardFocusManager.getCurrentKeyboardFocusManager() 210 .setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, hs); 211 }); 212 robot.waitForIdle(1000); 213 214 //key press and key release of "B". 215 robot.type(KeyEvent.VK_B); 216 robot.waitForIdle(1000); 217 218 if (gui.compgain != gui.firstchoice) 219 throw new RuntimeException("When a Key is set as forward traversal " + 220 "key,the key does not generate a forward focus traversal " + 221 "for every each component."); 222 223 //key press and key release of "B". 224 robot.type(KeyEvent.VK_B); 225 robot.waitForIdle(1000); 226 227 if (gui.compgain != gui.firsttextfield) 228 throw new RuntimeException("Assertion failed: Normal forward focus " + 229 "traversal is bringing focus out of the focus cycle"); 230 231 //Assersion:When a Key is set as backward traversal key 232 // (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEY), 233 // the key must generate a backward focus traversal 234 // for every each component. 235 236 EventQueue.invokeAndWait(() -> { 237 //setting "C" as BACKWARD_TRAVERSAL_KEY. 238 hs1.add(java.awt.AWTKeyStroke.getAWTKeyStroke("C")); 239 240 KeyboardFocusManager.getCurrentKeyboardFocusManager() 241 .setDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, hs1); 242 }); 243 robot.waitForIdle(1000); 244 245 //key press and key release of "C". 246 robot.type(KeyEvent.VK_C); 247 robot.waitForIdle(1000); 248 249 if (gui.compgain != gui.firstchoice) 250 throw new RuntimeException("Assertion failed: Normal forward focus " + 251 "traversal is bringing focus out of the focus cycle"); 252 253 //key press and key release of "C". 254 robot.type(KeyEvent.VK_C); 255 robot.waitForIdle(1000); 256 257 if (gui.compgain != gui.firstbutton) 258 throw new RuntimeException("Assertion failed:Normal forward focus " + 259 "traversal is bringing focus out of the focus cycle"); 260 261 robot.waitForIdle(1000); 262 263 EventQueue.invokeAndWait(() -> { 264 KeyboardFocusManager.setCurrentKeyboardFocusManager(null); 265 gui.frame5.dispose(); 266 }); 267 } 268 }