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(new GUIMerlinFocus(), new ExtendedRobot()).runTask();
  44     }
  45 
  46     TaskFocusTraversalPolicy(GUIMerlinFocus gui, ExtendedRobot robot) {
  47          super(gui, robot);
  48     }
  49 
  50     public void task() throws Exception {
  51         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         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         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         gui.testFrame.dispose();
 119 
 120         //Forward and backward traversal
 121         gui.frame5.setSize(500,270);
 122         gui.frame5.setVisible(true);
 123         robot.waitForIdle(1000);
 124 
 125         HashSet hs = new HashSet();
 126         HashSet hs1 = new HashSet();
 127 
 128         //Assersion:By default, Containers must not become the
 129         //                      Focus Cycle Roots without programmatically
 130         //                      setting it as a Focus Cycle Root.
 131 
 132         if (gui.firstpanel.isFocusCycleRoot())
 133             throw new RuntimeException("By default, Containers becomes the Focus " +
 134                     "Cycle Roots without programmatically setting it as a " +
 135                     "Focus Cycle Root.");
 136 
 137         gui.firstpanel.setFocusCycleRoot(true);
 138         gui.frame5.toFront();
 139 
 140         Point firstButtonCenter = gui.firstbutton.getLocationOnScreen();
 141         firstButtonCenter.translate(gui.firstbutton.getWidth()/2, gui.firstbutton.getHeight()/2);
 142         robot.mouseMove(gui.firstbutton.getLocationOnScreen());
 143         robot.glide(gui.firstbutton.getLocationOnScreen(), firstButtonCenter);
 144         robot.click();
 145         robot.waitForIdle(1000);
 146 
 147         //Assersion:When the focus owner is a Component inside a particular
 148         //                      cycle, normal forward and backward focus traversal must
 149         //                      not move the focus owner above the focus cycle root in
 150         //                      the Component hierarchy
 151 
 152         //TAB key is pressed
 153         robot.type(KeyEvent.VK_TAB);
 154         robot.waitForIdle(1000);
 155 
 156         if (gui.compgain != gui.firstchoice)
 157             throw new RuntimeException("When the focus owner is a Component " +
 158                     "inside a particular cycle, normal forward and backward " +
 159                     "focus traversal moves the focus owner above the focus " +
 160                     "cycle root in the Component hierarchy");
 161 
 162         //TAB key is pressed
 163         robot.type(KeyEvent.VK_TAB);
 164         robot.waitForIdle(1000);
 165 
 166         if (gui.compgain != gui.firsttextfield)
 167             throw new RuntimeException("When the focus owner is a Component " +
 168                     "inside a particular cycle, normal forward and backward " +
 169                     "focus traversal moves the focus owner above the focus " +
 170                     "cycle root in the Component hierarchy");
 171 
 172         robot.type(KeyEvent.VK_TAB);
 173         robot.waitForIdle(1000);
 174 
 175         if (gui.compgain != gui.firstbutton)
 176             throw new RuntimeException("When the focus owner is a Component " +
 177                     "inside a particular cycle, normal forward and backward " +
 178                     "focus traversal moves the focus owner above the focus " +
 179                     "cycle root in the Component hierarchy");
 180 
 181         //Assersion:Most recent Focus Owner must be either of the following
 182         //                      1) Focused Component
 183         //                      2) A Component which must gain focus when its
 184         //                         parent Window is focused
 185 
 186         gui.manager.downFocusCycle(gui.firstpanel);
 187         robot.waitForIdle(1000);
 188 
 189         if (gui.compgain != gui.firstbutton)
 190             throw new RuntimeException("Component does not gain focus when its " +
 191                     "parent Window is focused ");
 192 
 193         //Assersion:When a Key is set as forward traversal key
 194         //                      (KeyboardFocusManager.FORWARD_TRAVERSAL_KEY),
 195         //                      the key must generate a forward focus traversal
 196         //                      for every each component.
 197 
 198         gui.firstbutton.requestFocusInWindow();
 199 
 200         //setting "B" as FORWARD_TRAVERSAL_KEY.
 201         hs.add(java.awt.AWTKeyStroke.getAWTKeyStroke("B"));
 202 
 203         KeyboardFocusManager.getCurrentKeyboardFocusManager()
 204         .setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, hs);
 205 
 206         robot.waitForIdle(1000);
 207 
 208         //key press and key release of "B".
 209         robot.type(KeyEvent.VK_B);
 210         robot.waitForIdle(1000);
 211 
 212         if (gui.compgain != gui.firstchoice)
 213             throw new RuntimeException("When a Key is set as forward traversal " +
 214                     "key,the key does not generate a forward focus traversal " +
 215                     "for every each component.");
 216 
 217         //key press and key release of "B".
 218         robot.type(KeyEvent.VK_B);
 219         robot.waitForIdle(1000);
 220 
 221         if (gui.compgain != gui.firsttextfield)
 222             throw new RuntimeException("Assertion failed: Normal forward focus " +
 223                     "traversal is bringing focus out of the focus cycle");
 224 
 225         //Assersion:When a Key is set as backward traversal key
 226         //                      (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEY),
 227         //                      the key must generate a backward focus traversal
 228         //                      for every each component.
 229 
 230         //setting "C" as BACKWARD_TRAVERSAL_KEY.
 231 
 232         hs1.add(java.awt.AWTKeyStroke.getAWTKeyStroke("C"));
 233 
 234         KeyboardFocusManager.getCurrentKeyboardFocusManager()
 235         .setDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, hs1);
 236         robot.waitForIdle(1000);
 237 
 238         //key press and key release of "C".
 239         robot.type(KeyEvent.VK_C);
 240         robot.waitForIdle(1000);
 241 
 242         if (gui.compgain != gui.firstchoice)
 243             throw new RuntimeException("Assertion failed: Normal forward focus " +
 244                     "traversal is bringing focus out of the focus cycle");
 245 
 246         //key press and key release of "C".
 247         robot.type(KeyEvent.VK_C);
 248         robot.waitForIdle(1000);
 249 
 250         if (gui.compgain != gui.firstbutton)
 251             throw new RuntimeException("Assertion failed:Normal forward focus " +
 252                     "traversal is bringing focus out of the focus cycle");
 253 
 254         robot.waitForIdle(1000);
 255 
 256         KeyboardFocusManager.setCurrentKeyboardFocusManager(null);
 257         gui.frame5.dispose();
 258     }
 259 }