Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/AWT_Mixing/MixingPanelsResizing.java
47626 views
/*1* Copyright (c) 2014, 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@bug 678621937@summary Issues when resizing the frame after mixing of heavy weight & light weight components38@author [email protected]: area=awt.mixing39@library ../../regtesthelpers40@build Util41@build FrameBorderCounter42@run main MixingPanelsResizing43*/44public class MixingPanelsResizing {4546static volatile boolean failed = false;4748private static JFrame frame;49private static JButton jbutton;50private static Button awtButton;51private static JButton jbutton2;52private static Button awtButton2;53private static final Color jbColor = Color.RED;54private static final Color awtColor = Color.ORANGE;55private static final Color jb2Color = Color.BLUE;56private static final Color awt2Color = Color.CYAN;57private static final int ROBOT_DELAY = 500;5859private static Point lLoc;60private static int borderShift;6162private static int frameBorderCounter() {63String JAVA_HOME = System.getProperty("java.home");64try {65Process p = Runtime.getRuntime().exec(JAVA_HOME + "/bin/java FrameBorderCounter");66try {67p.waitFor();68} catch (InterruptedException e) {69e.printStackTrace();70throw new RuntimeException(e);71}72if (p.exitValue() != 0) {73throw new RuntimeException("FrameBorderCounter exited with not null code!\n" + readInputStream(p.getErrorStream()));74}75return Integer.parseInt(readInputStream(p.getInputStream()).trim());76} catch (IOException e) {77e.printStackTrace();78throw new RuntimeException(e);79}80}8182private static String readInputStream(InputStream is) throws IOException {83byte[] buffer = new byte[4096];84int len = 0;85StringBuilder sb = new StringBuilder();86try (InputStreamReader isr = new InputStreamReader(is)) {87while ((len = is.read(buffer)) > 0) {88sb.append(new String(buffer, 0, len));89}90}91return sb.toString();92}9394private static void init() throws Exception {95//*** Create instructions for the user here ***9697borderShift = frameBorderCounter();98borderShift = Math.abs(borderShift) == 1 ? borderShift : (borderShift / 2);99String[] instructions = {100"This is an AUTOMATIC test, simply wait until it is done.",101"The result (passed or failed) will be shown in the",102"message window below."103};104SwingUtilities.invokeAndWait(new Runnable() {105public void run() {106Sysout.createDialog();107Sysout.printInstructions(instructions);108109// prepare controls110111frame = new JFrame();112113Panel awtPanel = new Panel();114awtPanel.setBackground(Color.GREEN);115awtButton = new Button("AWTButton");116awtPanel.add(awtButton);117awtButton.setForeground(awtColor);118awtButton.setBackground(awtColor);119jbutton = new JButton("SwingButton");120awtPanel.add(jbutton);121jbutton.setForeground(jbColor);122jbutton.setBackground(jbColor);123124JPanel jPanel = new JPanel();125jbutton2 = new JButton("SwingButton2");126jPanel.add(jbutton2);127jbutton2.setForeground(jb2Color);128jbutton2.setBackground(jb2Color);129awtButton2 = new Button("AWT Button2");130jPanel.add(awtButton2);131awtButton2.setForeground(awt2Color);132awtButton2.setBackground(awt2Color);133jPanel.setBackground(Color.YELLOW);134135frame.add(awtPanel, BorderLayout.SOUTH);136frame.add(jPanel, BorderLayout.NORTH);137138frame.pack();139frame.setVisible(true);140}141});142143/////////////////////////144145final Robot robot = Util.createRobot();146robot.setAutoDelay(ROBOT_DELAY);147148Util.waitForIdle(robot);149150SwingUtilities.invokeAndWait(new Runnable() {151public void run() {152lLoc = frame.getLocationOnScreen();153lLoc.translate(frame.getWidth() + borderShift, frame.getHeight() + borderShift);154}155});156157//grow158robot.mouseMove(lLoc.x, lLoc.y);159robot.mousePress(InputEvent.BUTTON1_MASK);160161Runnable test = new Runnable() {162163public void run() {164Point btnLoc = jbutton.getLocationOnScreen();165Color c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5);166if (!c.equals(jbColor)) {167fail("JButton was not redrawn properly on AWT Panel during move");168}169170btnLoc = awtButton.getLocationOnScreen();171c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5);172if (!c.equals(awtColor)) {173fail("AWT Button was not redrawn properly on AWT Panel during move");174}175176btnLoc = jbutton2.getLocationOnScreen();177c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5);178if (!c.equals(jb2Color)) {179fail("JButton was not redrawn properly on JPanel during move");180}181182btnLoc = awtButton2.getLocationOnScreen();183c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5);184if (!c.equals(awt2Color)) {185fail("ATW Button was not redrawn properly on JPanel during move");186}187}188};189190for (int i = 0; i < 30; i++) {191test.run();192robot.mouseMove(lLoc.x + 20 * i, lLoc.y + 10 * i);193}194robot.mouseRelease(InputEvent.BUTTON1_MASK);195196//back197System.out.println("fast back");198robot.mousePress(InputEvent.BUTTON1_MASK);199for (int i = 5; i >= 0; i--) {200test.run();201robot.mouseMove(lLoc.x + 120 * i, lLoc.y + 60 * i);202}203robot.mouseRelease(InputEvent.BUTTON1_MASK);204205pass();206}//End init()207/*****************************************************208* Standard Test Machinery Section209* DO NOT modify anything in this section -- it's a210* standard chunk of code which has all of the211* synchronisation necessary for the test harness.212* By keeping it the same in all tests, it is easier213* to read and understand someone else's test, as214* well as insuring that all tests behave correctly215* with the test harness.216* There is a section following this for test-217* classes218******************************************************/219private static boolean theTestPassed = false;220private static boolean testGeneratedInterrupt = false;221private static String failureMessage = "";222private static Thread mainThread = null;223private static int sleepTime = 300000;224225// Not sure about what happens if multiple of this test are226// instantiated in the same VM. Being static (and using227// static vars), it aint gonna work. Not worrying about228// it for now.229public static void main(String args[]) throws Exception {230if (!Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {231System.out.println("Dynamic layout is not active. Test passes.");232return;233}234mainThread = Thread.currentThread();235try {236init();237} catch (TestPassedException e) {238//The test passed, so just return from main and harness will239// interepret this return as a pass240return;241}242//At this point, neither test pass nor test fail has been243// called -- either would have thrown an exception and ended the244// test, so we know we have multiple threads.245246//Test involves other threads, so sleep and wait for them to247// called pass() or fail()248try {249Thread.sleep(sleepTime);250//Timed out, so fail the test251throw new RuntimeException("Timed out after " + sleepTime / 1000 + " seconds");252} catch (InterruptedException e) {253//The test harness may have interrupted the test. If so, rethrow the exception254// so that the harness gets it and deals with it.255if (!testGeneratedInterrupt) {256throw e;257}258259//reset flag in case hit this code more than once for some reason (just safety)260testGeneratedInterrupt = false;261262if (theTestPassed == false) {263throw new RuntimeException(failureMessage);264}265}266267}//main268269public static synchronized void setTimeoutTo(int seconds) {270sleepTime = seconds * 1000;271}272273public static synchronized void pass() {274Sysout.println("The test passed.");275Sysout.println("The test is over, hit Ctl-C to stop Java VM");276//first check if this is executing in main thread277if (mainThread == Thread.currentThread()) {278//Still in the main thread, so set the flag just for kicks,279// and throw a test passed exception which will be caught280// and end the test.281theTestPassed = true;282throw new TestPassedException();283}284theTestPassed = true;285testGeneratedInterrupt = true;286mainThread.interrupt();287}//pass()288289public static synchronized void fail() {290//test writer didn't specify why test failed, so give generic291fail("it just plain failed! :-)");292}293294public static synchronized void fail(String whyFailed) {295Sysout.println("The test failed: " + whyFailed);296Sysout.println("The test is over, hit Ctl-C to stop Java VM");297//check if this called from main thread298if (mainThread == Thread.currentThread()) {299//If main thread, fail now 'cause not sleeping300throw new RuntimeException(whyFailed);301}302theTestPassed = false;303testGeneratedInterrupt = true;304failureMessage = whyFailed;305mainThread.interrupt();306}//fail()307}// class JButtonInGlassPane308class TestPassedException extends RuntimeException {309}310311//*********** End Standard Test Machinery Section **********312//************ Begin classes defined for the test ****************313// if want to make listeners, here is the recommended place for them, then instantiate314// them in init()315316/* Example of a class which may be written as part of a test317class NewClass implements anInterface318{319static int newVar = 0;320321public void eventDispatched(AWTEvent e)322{323//Counting events to see if we get enough324eventCount++;325326if( eventCount == 20 )327{328//got enough events, so pass329330JButtonInGlassPane.pass();331}332else if( tries == 20 )333{334//tried too many times without getting enough events so fail335336JButtonInGlassPane.fail();337}338339}// eventDispatched()340341}// NewClass class342343*/344//************** End classes defined for the test *******************345/****************************************************346Standard Test Machinery347DO NOT modify anything below -- it's a standard348chunk of code whose purpose is to make user349interaction uniform, and thereby make it simpler350to read and understand someone else's test.351****************************************************/352/**353This is part of the standard test machinery.354It creates a dialog (with the instructions), and is the interface355for sending text messages to the user.356To print the instructions, send an array of strings to Sysout.createDialog357WithInstructions method. Put one line of instructions per array entry.358To display a message for the tester to see, simply call Sysout.println359with the string to be displayed.360This mimics System.out.println but works within the test harness as well361as standalone.362*/363class Sysout {364365private static TestDialog dialog;366367public static void createDialogWithInstructions(String[] instructions) {368dialog = new TestDialog(new Frame(), "Instructions");369dialog.printInstructions(instructions);370//dialog.setVisible(true);371println("Any messages for the tester will display here.");372}373374public static void createDialog() {375dialog = new TestDialog(new Frame(), "Instructions");376String[] defInstr = {"Instructions will appear here. ", ""};377dialog.printInstructions(defInstr);378//dialog.setVisible(true);379println("Any messages for the tester will display here.");380}381382public static void printInstructions(String[] instructions) {383dialog.printInstructions(instructions);384}385386public static void println(String messageIn) {387dialog.displayMessage(messageIn);388System.out.println(messageIn);389}390}// Sysout class391392/**393This is part of the standard test machinery. It provides a place for the394test instructions to be displayed, and a place for interactive messages395to the user to be displayed.396To have the test instructions displayed, see Sysout.397To have a message to the user be displayed, see Sysout.398Do not call anything in this dialog directly.399*/400class TestDialog extends Dialog {401402TextArea instructionsText;403TextArea messageText;404int maxStringLength = 80;405406//DO NOT call this directly, go through Sysout407public TestDialog(Frame frame, String name) {408super(frame, name);409int scrollBoth = TextArea.SCROLLBARS_BOTH;410instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);411add("North", instructionsText);412413messageText = new TextArea("", 5, maxStringLength, scrollBoth);414add("Center", messageText);415416pack();417418//setVisible(true);419}// TestDialog()420421//DO NOT call this directly, go through Sysout422public void printInstructions(String[] instructions) {423//Clear out any current instructions424instructionsText.setText("");425426//Go down array of instruction strings427428String printStr, remainingStr;429for (int i = 0; i < instructions.length; i++) {430//chop up each into pieces maxSringLength long431remainingStr = instructions[i];432while (remainingStr.length() > 0) {433//if longer than max then chop off first max chars to print434if (remainingStr.length() >= maxStringLength) {435//Try to chop on a word boundary436int posOfSpace = remainingStr.lastIndexOf(' ', maxStringLength - 1);437438if (posOfSpace <= 0) {439posOfSpace = maxStringLength - 1;440}441442printStr = remainingStr.substring(0, posOfSpace + 1);443remainingStr = remainingStr.substring(posOfSpace + 1);444} //else just print445else {446printStr = remainingStr;447remainingStr = "";448}449450instructionsText.append(printStr + "\n");451452}// while453454}// for455456}//printInstructions()457458//DO NOT call this directly, go through Sysout459public void displayMessage(String messageIn) {460messageText.append(messageIn + "\n");461System.out.println(messageIn);462}463}// TestDialog class464465466467