Path: blob/master/test/jdk/java/awt/Focus/ChildWindowFocusTest/ChildWindowFocusTest.java
41152 views
/*1* Copyright (c) 2004, 2018, 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 509032527@summary Tests that Window's child can be focused on XAWT.28@run main ChildWindowFocusTest29*/3031import java.awt.*;32import java.awt.event.*;3334public class ChildWindowFocusTest {35Robot robot;36Frame frame = new Frame("Owner");37Button button0 = new Button("button-0");38TextField text0 = new TextField("text-0");39TextField text1 = new TextField("text-1");40Window win1 = new TestWindow(frame, text0, 110);41Window win2 = new TestWindow(win1, text1, 220);42Frame outerFrame = new Frame("Outer");43Button button1 = new Button("button-1");44int shift;4546public static void main(final String[] args) {47ChildWindowFocusTest app = new ChildWindowFocusTest();48app.init();49app.start();50}5152public void init() {53try {54robot = new Robot();55} catch (AWTException e) {56throw new RuntimeException("Error: unable to create robot", e);57}58shift = 100;59}6061public void start() {6263frame.setBounds(0, 50, 400, 100);64frame.setLayout(new FlowLayout());65frame.add(button0);6667outerFrame.setBounds(0, 390, 400, 100);68outerFrame.setLayout(new FlowLayout());69outerFrame.add(button1);7071adjustAndShow(new Component[] {frame, win1, win2, outerFrame});72robot.waitForIdle();7374test();75}7677void adjustAndShow(Component[] comps) {78for (Component comp: comps) {79comp.setLocation(shift, (int)comp.getLocation().getY());80comp.setVisible(true);81robot.waitForIdle();82}83}8485void test() {86clickOnCheckFocusOwner(button0);87clickOnCheckFocusOwner(text1);88clickOnCheckFocusOwner(button1);89clickOn(frame);90checkFocusOwner(text1);91clickOnCheckFocusOwner(text0);92clickOnCheckFocusOwner(button1);93clickOn(frame);94checkFocusOwner(text0);9596System.out.println("Test passed.");97}9899void clickOnCheckFocusOwner(Component c) {100clickOn(c);101if (!checkFocusOwner(c)) {102throw new RuntimeException("Test failed: couldn't focus <" + c + "> by mouse click!");103}104}105106boolean checkFocusOwner(Component comp) {107return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());108}109110void clickOn(Component c) {111Point p = c.getLocationOnScreen();112Dimension d = c.getSize();113114System.out.println("Clicking " + c);115116if (c instanceof Frame) {117robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);118} else {119robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));120}121robot.delay(50);122robot.mousePress(InputEvent.BUTTON1_MASK);123robot.delay(50);124robot.mouseRelease(InputEvent.BUTTON1_MASK);125robot.waitForIdle();126}127128}129130class TestWindow extends Window {131TestWindow(Window owner, Component comp, int x) {132super(owner);133setBackground(Color.blue);134setLayout(new FlowLayout());135add(comp);136comp.setBackground(Color.yellow);137setBounds(0, x, 100, 100);138}139}140141142