Path: blob/master/test/jdk/java/awt/LightweightComponent/LightweightEventTest/LightweightEventTest.java
41153 views
/*1* Copyright (c) 2016, 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* @summary Test of mouse move messages to lightweight components27* @library ../../regtesthelpers28* @build Util29* @compile LightweightEventTest.java30* @run main LightweightEventTest31*/32import java.awt.BorderLayout;33import java.awt.Button;34import java.awt.Color;35import java.awt.Component;36import java.awt.Container;37import java.awt.Dimension;38import java.awt.FontMetrics;39import java.awt.Frame;40import java.awt.Graphics;41import java.awt.Insets;42import java.awt.Point;43import java.awt.Rectangle;44import java.awt.Robot;45import java.awt.AWTException;46import java.awt.event.MouseAdapter;47import java.awt.event.MouseEvent;48import javax.swing.SwingUtilities;49import test.java.awt.regtesthelpers.Util;505152/*53There are 3 steps to this test :541. Two frames are created one with heavy weight component and55another with light weight component. Each frame has a centrally placed56button572. Mouse is dragged along diagonals of each window using Robot object583. Events are noted for mouse in and out of frames & buttons and asserted59*/6061public class LightweightEventTest {6263private static EventBug HeavyComponent;64private static EventBug LightComponent;65private static Robot testRobot;6667public static void main(String[] args) throws Throwable {6869SwingUtilities.invokeAndWait(new Runnable() {70@Override71public void run() {72constructTestUI();73}74});7576try {77testRobot = new Robot();78} catch (AWTException ex) {79throw new RuntimeException("Could not initiate a drag operation");80}8182testRobot.waitForIdle();8384// Method performing auto test operation85boolean result = test();8687disposeTestUI();8889if (result == false) {90throw new RuntimeException("Test FAILED!");91}92}9394private static boolean test() {95// Test events for HeavyComponent96Point loc = HeavyComponent.getLocationOnScreen();97Dimension size = HeavyComponent.getSize();9899Util.mouseMove(testRobot,100new Point((int) loc.x + 4, (int) loc.y + 4),101new Point((int) loc.x + size.width, (int) loc.y + size.height));102103testRobot.waitForIdle();104105boolean HeavyComponentAssert = HeavyComponent.assertEvents(2, 1);106107// Test events for LightComponent108loc = LightComponent.getLocationOnScreen();109size = LightComponent.getSize();110111Util.mouseMove(testRobot,112new Point((int) loc.x + 4, (int) loc.y + 4),113new Point((int) loc.x + size.width, (int) loc.y + size.height));114115testRobot.waitForIdle();116117boolean LightComponentAssert = LightComponent.assertEvents(2, 1);118119return (HeavyComponentAssert && LightComponentAssert);120}121122private static void constructTestUI() {123// here, create the items that will be tested for correct behavior124HeavyComponent = new EventBug();125Button b = (Button) HeavyComponent.add("Center", new Button("Heavy"));126127LightComponent = new EventBug();128BorderedLabel b1 = (BorderedLabel) LightComponent.add("Center",129new BorderedLabel("Lite"));130131HeavyComponent.addListeners(b);132LightComponent.addListeners(b1);133134LightComponent.setLocation(200, 0);135HeavyComponent.setVisible(true);136LightComponent.setVisible(true);137}138139private static void disposeTestUI() {140HeavyComponent.setVisible(false);141LightComponent.setVisible(false);142143HeavyComponent.dispose();144LightComponent.dispose();145}146}147148/*149* Lightweight component150*/151class BorderedLabel extends Component {152153boolean superIsButton = false;154String labelString;155156BorderedLabel(String labelString) {157this.labelString = labelString;158159Component thisComponent = this;160superIsButton = (thisComponent instanceof Button);161if (superIsButton) {162((Button) thisComponent).setLabel(labelString);163}164}165166@Override167public Dimension getMinimumSize() {168Dimension minSize = new Dimension();169170if (superIsButton) {171minSize = super.getMinimumSize();172} else {173174Graphics g = getGraphics();175FontMetrics metrics = g.getFontMetrics();176177minSize.width = metrics.stringWidth(labelString) + 14;178minSize.height = metrics.getMaxAscent()179+ metrics.getMaxDescent() + 9;180181g.dispose();182g = null;183}184return minSize;185}186187@Override188public Dimension getPreferredSize() {189Dimension prefSize;190if (superIsButton) {191prefSize = super.getPreferredSize();192} else {193prefSize = getMinimumSize();194}195return prefSize;196}197198@Override199public void paint(Graphics g) {200201super.paint(g);202Rectangle bounds = getBounds();203if (superIsButton) {204return;205}206Dimension size = getSize();207Color oldColor = g.getColor();208209// draw border210g.setColor(getBackground());211g.fill3DRect(0, 0, size.width, size.height, false);212g.fill3DRect(3, 3, size.width - 6, size.height - 6, true);213214// draw text215FontMetrics metrics = g.getFontMetrics();216int centerX = size.width / 2;217int centerY = size.height / 2;218int textX = centerX - (metrics.stringWidth(labelString) / 2);219int textY = centerY220+ ((metrics.getMaxAscent() + metrics.getMaxDescent()) / 2);221g.setColor(getForeground());222g.drawString(labelString, textX, textY);223224g.setColor(oldColor);225}226} // class BorderedLabel227228class EventBug extends Container {229230Frame testFrame;231int frameEnters = 0;232int frameExits = 0;233int buttonEnters = 0;234int buttonExits = 0;235236public EventBug() {237super();238testFrame = new Frame();239testFrame.setLayout(new BorderLayout());240this.setLayout(new BorderLayout());241testFrame.add("Center", this);242testFrame.pack();243testFrame.setVisible(true);244}245246@Override247public Dimension getPreferredSize() {248return new Dimension(100, 100);249}250251@Override252public Insets getInsets() {253return new Insets(20, 20, 20, 20);254}255256public boolean assertEvents(int expectedFrameEnterEvents,257int expectedButtonEnterEvents) {258return (frameEnters == expectedFrameEnterEvents)259&& (buttonEnters == expectedButtonEnterEvents);260}261262// Forward to the Window263@Override264public void setLocation(int x, int y) {265testFrame.setLocation(x, y);266}267268@Override269public void setVisible(boolean b) {270testFrame.setVisible(b);271}272273public void dispose() {274testFrame.dispose();275}276277// Add listeners to Frame and button278public void addListeners(Component b) {279b.setName("Button");280b.addMouseListener(new MouseAdapter() {281@Override282public void mouseEntered(MouseEvent e) {283buttonEnters++;284}285286@Override287public void mouseExited(MouseEvent e) {288buttonExits++;289}290291});292testFrame.addMouseListener(new MouseAdapter() {293@Override294public void mouseEntered(MouseEvent e) {295frameEnters++;296}297298@Override299public void mouseExited(MouseEvent e) {300frameExits++;301}302});303}304} // class EventBug305306307