--- /dev/null 2014-07-28 16:11:16.120781410 +0400 +++ new/test/java/awt/reliability/TaskFocusTraversalPolicy.java 2014-07-28 16:22:03.020769564 +0400 @@ -0,0 +1,259 @@ +/* + * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +/* + * @test + * @summary Set the Focus Traversal Policy and check whether the First, Initial, + * Default & Last Components are returning properly. Try to traverse + * the focus forward and backward within a focus cycle using the traversal + * key explicitly set by the user. + * @author Aruna Samji + * @library ../../../lib/testlibrary + * @build ExtendedRobot + * @run main TaskFocusTraversalPolicy + */ + +public class TaskFocusTraversalPolicy extends Task { + + public static void main (String[] args) throws Exception { + new TaskFocusTraversalPolicy(new GUIMerlinFocus(), new ExtendedRobot()).runTask(); + } + + TaskFocusTraversalPolicy(GUIMerlinFocus gui, ExtendedRobot robot) { + super(gui, robot); + } + + public void task() throws Exception { + gui.testFrame1.setVisible(true); + robot.waitForIdle(1000); + + if (gui.focusGained5 == null || ! gui.focusGained5.equals(gui.button7)) + throw new RuntimeException("Default Component is not getting focus"); + + + gui.testFrame1.dispose(); + robot.waitForIdle(1000); + + // 1.checking whether + // i)The InitialComponent is getting focus when the + // new FocusTraversalPolicy is set for a container. + // ii)checking whether the First, Initial, Default, Next and + // Previous Components are returned properly as per the + // traversal policy set for the container. + + //Setting the traversal policy for the frame. + if ( (gui.cp.getInitialComponent(gui.testFrame)) != gui.choice5) + throw new RuntimeException("Initial Component is not returning correctly."); + + if ( (gui.cp.getFirstComponent(gui.testFrame)) != gui.choice5) + throw new RuntimeException("First Component is not returning correctly."); + + if ( (gui.cp.getDefaultComponent(gui.testFrame)) != gui.choice5) + throw new RuntimeException("Default Component is not returning correctly."); + + if ( (gui.cp.getLastComponent(gui.testFrame)) != gui.checkbox5) + throw new RuntimeException("Last Component is not returning correctly."); + + if ( (gui.cp.getComponentAfter(gui.testFrame,gui.button5)) != gui.choice5) + throw new RuntimeException("Next Component is not returned correctly for button."); + + if ( (gui.cp.getComponentAfter(gui.testFrame,gui.list5)) != gui.textfield5) + throw new RuntimeException("Next Component is not returned correctly for list."); + + if ( (gui.cp.getComponentAfter(gui.testFrame,gui.button6)) != gui.button5) + throw new RuntimeException("Next Component is not returned correctly for button2."); + + if ( (gui.cp.getComponentAfter(gui.testFrame,gui.choice5)) != gui.list5) + throw new RuntimeException("Next Component is not returned correctly for choice."); + + if ( (gui.cp.getComponentAfter(gui.testFrame,gui.textfield5)) != gui.button6) + throw new RuntimeException("Next Component is not returned correctly for textfield."); + + if ( (gui.cp.getComponentBefore(gui.testFrame,gui.button5)) != gui.button6) + throw new RuntimeException("Previous Component is not returned correctly for button."); + + if ( (gui.cp.getComponentBefore(gui.testFrame,gui.list5)) != gui.choice5) + throw new RuntimeException("Previous Component is not returned correctly for list."); + + if ( (gui.cp.getComponentBefore(gui.testFrame,gui.button6)) != gui.textfield5) + throw new RuntimeException("Previous Component is not returned correctly for button2."); + + if ( (gui.cp.getComponentBefore(gui.testFrame,gui.choice5)) != gui.button5) + throw new RuntimeException("Previous Component is not returned correctly for choice."); + + if ( (gui.cp.getComponentBefore(gui.testFrame,gui.textfield5)) != gui.list5) + throw new RuntimeException("Previous Component is not returned correctly for textfield."); + + gui.testFrame.setVisible(true); + robot.waitForIdle(1000); + + if (gui.focusGained5 != gui.choice5) + throw new RuntimeException("Initial Component is not getting focus " + + "after setting the traversal policy"); + + gui.testFrame.dispose(); + + //Forward and backward traversal + gui.frame5.setSize(500,270); + gui.frame5.setVisible(true); + robot.waitForIdle(1000); + + HashSet hs = new HashSet(); + HashSet hs1 = new HashSet(); + + //Assersion:By default, Containers must not become the + // Focus Cycle Roots without programmatically + // setting it as a Focus Cycle Root. + + if (gui.firstpanel.isFocusCycleRoot()) + throw new RuntimeException("By default, Containers becomes the Focus " + + "Cycle Roots without programmatically setting it as a " + + "Focus Cycle Root."); + + gui.firstpanel.setFocusCycleRoot(true); + gui.frame5.toFront(); + + Point firstButtonCenter = gui.firstbutton.getLocationOnScreen(); + firstButtonCenter.translate(gui.firstbutton.getWidth()/2, gui.firstbutton.getHeight()/2); + robot.mouseMove(gui.firstbutton.getLocationOnScreen()); + robot.glide(gui.firstbutton.getLocationOnScreen(), firstButtonCenter); + robot.click(); + robot.waitForIdle(1000); + + //Assersion:When the focus owner is a Component inside a particular + // cycle, normal forward and backward focus traversal must + // not move the focus owner above the focus cycle root in + // the Component hierarchy + + //TAB key is pressed + robot.type(KeyEvent.VK_TAB); + robot.waitForIdle(1000); + + if (gui.compgain != gui.firstchoice) + throw new RuntimeException("When the focus owner is a Component " + + "inside a particular cycle, normal forward and backward " + + "focus traversal moves the focus owner above the focus " + + "cycle root in the Component hierarchy"); + + //TAB key is pressed + robot.type(KeyEvent.VK_TAB); + robot.waitForIdle(1000); + + if (gui.compgain != gui.firsttextfield) + throw new RuntimeException("When the focus owner is a Component " + + "inside a particular cycle, normal forward and backward " + + "focus traversal moves the focus owner above the focus " + + "cycle root in the Component hierarchy"); + + robot.type(KeyEvent.VK_TAB); + robot.waitForIdle(1000); + + if (gui.compgain != gui.firstbutton) + throw new RuntimeException("When the focus owner is a Component " + + "inside a particular cycle, normal forward and backward " + + "focus traversal moves the focus owner above the focus " + + "cycle root in the Component hierarchy"); + + //Assersion:Most recent Focus Owner must be either of the following + // 1) Focused Component + // 2) A Component which must gain focus when its + // parent Window is focused + + gui.manager.downFocusCycle(gui.firstpanel); + robot.waitForIdle(1000); + + if (gui.compgain != gui.firstbutton) + throw new RuntimeException("Component does not gain focus when its " + + "parent Window is focused "); + + //Assersion:When a Key is set as forward traversal key + // (KeyboardFocusManager.FORWARD_TRAVERSAL_KEY), + // the key must generate a forward focus traversal + // for every each component. + + gui.firstbutton.requestFocusInWindow(); + + //setting "B" as FORWARD_TRAVERSAL_KEY. + hs.add(java.awt.AWTKeyStroke.getAWTKeyStroke("B")); + + KeyboardFocusManager.getCurrentKeyboardFocusManager() + .setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, hs); + + robot.waitForIdle(1000); + + //key press and key release of "B". + robot.type(KeyEvent.VK_B); + robot.waitForIdle(1000); + + if (gui.compgain != gui.firstchoice) + throw new RuntimeException("When a Key is set as forward traversal " + + "key,the key does not generate a forward focus traversal " + + "for every each component."); + + //key press and key release of "B". + robot.type(KeyEvent.VK_B); + robot.waitForIdle(1000); + + if (gui.compgain != gui.firsttextfield) + throw new RuntimeException("Assertion failed: Normal forward focus " + + "traversal is bringing focus out of the focus cycle"); + + //Assersion:When a Key is set as backward traversal key + // (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEY), + // the key must generate a backward focus traversal + // for every each component. + + //setting "C" as BACKWARD_TRAVERSAL_KEY. + + hs1.add(java.awt.AWTKeyStroke.getAWTKeyStroke("C")); + + KeyboardFocusManager.getCurrentKeyboardFocusManager() + .setDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, hs1); + robot.waitForIdle(1000); + + //key press and key release of "C". + robot.type(KeyEvent.VK_C); + robot.waitForIdle(1000); + + if (gui.compgain != gui.firstchoice) + throw new RuntimeException("Assertion failed: Normal forward focus " + + "traversal is bringing focus out of the focus cycle"); + + //key press and key release of "C". + robot.type(KeyEvent.VK_C); + robot.waitForIdle(1000); + + if (gui.compgain != gui.firstbutton) + throw new RuntimeException("Assertion failed:Normal forward focus " + + "traversal is bringing focus out of the focus cycle"); + + robot.waitForIdle(1000); + + KeyboardFocusManager.setCurrentKeyboardFocusManager(null); + gui.frame5.dispose(); + } +}