Path: blob/master/test/jdk/java/awt/Mixing/AWT_Mixing/MixingPanelsResizing.java
41152 views
/*1* Copyright (c) 2014, 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*/222324import java.awt.*;25import java.awt.event.InputEvent;26import javax.swing.*;27import java.io.*;28import test.java.awt.regtesthelpers.Util;2930/**31* AWT/Swing overlapping test for Panel and JPanel behavior during resizing.32* <p>See <a href="https://bugs.openjdk.java.net/browse/JDK-6786219">JDK-6786219</a> for details33*/34/*35* @test36* @key headful37* @bug 6786219 822182338* @summary Issues when resizing the frame after mixing of heavy weight & light weight components39* @author [email protected]: area=awt.mixing40* @library ../../regtesthelpers41* @build Util42* @build FrameBorderCounter43* @run main MixingPanelsResizing44*/45public class MixingPanelsResizing {4647static volatile boolean failed = false;4849private static JFrame frame;50private static JButton jbutton;51private static Button awtButton;52private static JButton jbutton2;53private static Button awtButton2;54private static final Color jbColor = Color.RED;55private static final Color awtColor = Color.ORANGE;56private static final Color jb2Color = Color.BLUE;57private static final Color awt2Color = Color.CYAN;58private static final int ROBOT_DELAY = 500;5960private static Point lLoc;61private static int borderShift;6263private static int frameBorderCounter() {64String JAVA_HOME = System.getProperty("java.home");65try {66Process p = Runtime.getRuntime().exec(JAVA_HOME + "/bin/java FrameBorderCounter");67try {68p.waitFor();69} catch (InterruptedException e) {70e.printStackTrace();71throw new RuntimeException(e);72}73if (p.exitValue() != 0) {74throw new RuntimeException("FrameBorderCounter exited with not null code!\n" + readInputStream(p.getErrorStream()));75}76return Integer.parseInt(readInputStream(p.getInputStream()).trim());77} catch (IOException e) {78e.printStackTrace();79throw new RuntimeException(e);80}81}8283private static String readInputStream(InputStream is) throws IOException {84byte[] buffer = new byte[4096];85int len = 0;86StringBuilder sb = new StringBuilder();87try (InputStreamReader isr = new InputStreamReader(is)) {88while ((len = is.read(buffer)) > 0) {89sb.append(new String(buffer, 0, len));90}91}92return sb.toString();93}9495private static void init() throws Exception {96//*** Create instructions for the user here ***9798borderShift = frameBorderCounter();99borderShift = Math.abs(borderShift) == 1 ? borderShift : (borderShift / 2);100SwingUtilities.invokeAndWait(new Runnable() {101public void run() {102// prepare controls103104frame = new JFrame();105106Panel awtPanel = new Panel();107awtPanel.setBackground(Color.GREEN);108awtButton = new Button("AWTButton");109awtPanel.add(awtButton);110awtButton.setForeground(awtColor);111awtButton.setBackground(awtColor);112jbutton = new JButton("SwingButton");113awtPanel.add(jbutton);114jbutton.setForeground(jbColor);115jbutton.setBackground(jbColor);116117JPanel jPanel = new JPanel();118jbutton2 = new JButton("SwingButton2");119jPanel.add(jbutton2);120jbutton2.setForeground(jb2Color);121jbutton2.setBackground(jb2Color);122awtButton2 = new Button("AWT Button2");123jPanel.add(awtButton2);124awtButton2.setForeground(awt2Color);125awtButton2.setBackground(awt2Color);126jPanel.setBackground(Color.YELLOW);127128frame.add(awtPanel, BorderLayout.SOUTH);129frame.add(jPanel, BorderLayout.NORTH);130131frame.pack();132frame.setVisible(true);133}134});135136/////////////////////////137138final Robot robot = Util.createRobot();139robot.setAutoDelay(ROBOT_DELAY);140141Util.waitForIdle(robot);142143SwingUtilities.invokeAndWait(new Runnable() {144public void run() {145lLoc = frame.getLocationOnScreen();146lLoc.translate(frame.getWidth() + borderShift, frame.getHeight() + borderShift);147}148});149150//grow151robot.mouseMove(lLoc.x, lLoc.y);152robot.mousePress(InputEvent.BUTTON1_MASK);153154Runnable test = new Runnable() {155156public void run() {157Point btnLoc = jbutton.getLocationOnScreen();158Color c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5);159if (!c.equals(jbColor)) {160fail("JButton was not redrawn properly on AWT Panel during move");161}162163btnLoc = awtButton.getLocationOnScreen();164c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5);165if (!c.equals(awtColor)) {166fail("AWT Button was not redrawn properly on AWT Panel during move");167}168169btnLoc = jbutton2.getLocationOnScreen();170c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5);171if (!c.equals(jb2Color)) {172fail("JButton was not redrawn properly on JPanel during move");173}174175btnLoc = awtButton2.getLocationOnScreen();176c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5);177if (!c.equals(awt2Color)) {178fail("ATW Button was not redrawn properly on JPanel during move");179}180}181};182183for (int i = 0; i < 30; i++) {184test.run();185robot.mouseMove(lLoc.x + 20 * i, lLoc.y + 10 * i);186}187robot.mouseRelease(InputEvent.BUTTON1_MASK);188189//back190System.out.println("fast back");191robot.mousePress(InputEvent.BUTTON1_MASK);192for (int i = 5; i >= 0; i--) {193test.run();194robot.mouseMove(lLoc.x + 120 * i, lLoc.y + 60 * i);195}196robot.mouseRelease(InputEvent.BUTTON1_MASK);197198pass();199}//End init()200/*****************************************************201* Standard Test Machinery Section202* DO NOT modify anything in this section -- it's a203* standard chunk of code which has all of the204* synchronisation necessary for the test harness.205* By keeping it the same in all tests, it is easier206* to read and understand someone else's test, as207* well as insuring that all tests behave correctly208* with the test harness.209* There is a section following this for test-210* classes211******************************************************/212private static boolean theTestPassed = false;213private static boolean testGeneratedInterrupt = false;214private static String failureMessage = "";215private static Thread mainThread = null;216private static int sleepTime = 300000;217218// Not sure about what happens if multiple of this test are219// instantiated in the same VM. Being static (and using220// static vars), it aint gonna work. Not worrying about221// it for now.222public static void main(String args[]) throws Exception {223if (!Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {224System.out.println("Dynamic layout is not active. Test passes.");225return;226}227mainThread = Thread.currentThread();228try {229init();230} catch (TestPassedException e) {231//The test passed, so just return from main and harness will232// interepret this return as a pass233return;234}235//At this point, neither test pass nor test fail has been236// called -- either would have thrown an exception and ended the237// test, so we know we have multiple threads.238239//Test involves other threads, so sleep and wait for them to240// called pass() or fail()241try {242Thread.sleep(sleepTime);243//Timed out, so fail the test244throw new RuntimeException("Timed out after " + sleepTime / 1000 + " seconds");245} catch (InterruptedException e) {246//The test harness may have interrupted the test. If so, rethrow the exception247// so that the harness gets it and deals with it.248if (!testGeneratedInterrupt) {249throw e;250}251252//reset flag in case hit this code more than once for some reason (just safety)253testGeneratedInterrupt = false;254255if (theTestPassed == false) {256throw new RuntimeException(failureMessage);257}258}259260}//main261262public static synchronized void setTimeoutTo(int seconds) {263sleepTime = seconds * 1000;264}265266public static synchronized void pass() {267System.out.println("The test passed.");268System.out.println("The test is over, hit Ctl-C to stop Java VM");269//first check if this is executing in main thread270if (mainThread == Thread.currentThread()) {271//Still in the main thread, so set the flag just for kicks,272// and throw a test passed exception which will be caught273// and end the test.274theTestPassed = true;275throw new TestPassedException();276}277theTestPassed = true;278testGeneratedInterrupt = true;279mainThread.interrupt();280}//pass()281282public static synchronized void fail() {283//test writer didn't specify why test failed, so give generic284fail("it just plain failed! :-)");285}286287public static synchronized void fail(String whyFailed) {288System.out.println("The test failed: " + whyFailed);289System.out.println("The test is over, hit Ctl-C to stop Java VM");290//check if this called from main thread291if (mainThread == Thread.currentThread()) {292//If main thread, fail now 'cause not sleeping293throw new RuntimeException(whyFailed);294}295theTestPassed = false;296testGeneratedInterrupt = true;297failureMessage = whyFailed;298mainThread.interrupt();299}//fail()300}// class JButtonInGlassPane301class TestPassedException extends RuntimeException {302}303304305