Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Paint/ComponentIsNotDrawnAfterRemoveAddTest/ComponentIsNotDrawnAfterRemoveAddTest.java
47525 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/* @test24@bug 813958125@summary AWT components are not drawn after removal and addition to a container26@author Anton Litvinov27*/2829import java.awt.Button;30import java.awt.Color;31import java.awt.Canvas;32import java.awt.Frame;33import java.awt.Graphics;34import java.awt.Panel;35import java.util.ArrayList;3637public class ComponentIsNotDrawnAfterRemoveAddTest {38private final Frame frame;39private final Panel panel;40private final ArrayList<Testable> compList = new ArrayList<Testable>();4142public ComponentIsNotDrawnAfterRemoveAddTest() {43frame = new Frame("ComponentIsNotDrawnAfterRemoveAddTest");44frame.setSize(500, 500);45frame.setLocation(200, 200);46frame.setLayout(null);47frame.setBackground(Color.RED);4849panel = new Panel();50panel.setLayout(null);51panel.setBounds(25, 100, 455, 295);52panel.setBackground(Color.GREEN);5354for (int i = 0; i < 10; i++) {55TestCanvas canv1 = new TestCanvas();56canv1.setBounds(i * 45 + 5, 15, 30 + i, 30 + i);57panel.add(canv1);58compList.add(canv1);5960TestButton btn1 = new TestButton();61btn1.setBounds(i * 45 + 5, 60, 30 + i, 30 + i);62panel.add(btn1);63compList.add(btn1);6465TestCanvas canv2 = new TestCanvas();66canv2.setBounds(i * 45 + 5, 105, 30 + i, 30 + i);67panel.add(canv2);68compList.add(canv2);6970TestButton btn2 = new TestButton();71btn2.setBounds(i * 45 + 5, 150, 30 + i, 30 + i);72panel.add(btn2);73compList.add(btn2);7475TestCanvas canv3 = new TestCanvas();76canv3.setBounds(i * 45 + 5, 195, 30 + i, 30 + i);77panel.add(canv3);78compList.add(canv3);7980TestButton btn3 = new TestButton();81btn3.setBounds(i * 45 + 5, 240, 30 + i, 30 + i);82panel.add(btn3);83compList.add(btn3);84}8586frame.add(panel);87frame.setVisible(true);88}8990private void runTest() {91try {92doSleep(1500);93checkTestableComponents();9495for (int i = 0; i < 5; i++) {96System.err.println(String.format("Test iteration #%d:", i));9798frame.remove(panel);99frame.invalidate();100frame.validate();101frame.add(panel);102103doSleep(1500);104checkTestableComponents();105}106} finally {107frame.dispose();108}109}110111private void doSleep(long millis) {112try {113Thread.sleep(millis);114} catch (InterruptedException ie) {115ie.printStackTrace();116}117}118119private void checkTestableComponents() throws RuntimeException {120int notDrawnCompsCount = 0;121for (Testable comp : compList) {122if (!comp.wasPaintCalled()) {123notDrawnCompsCount++;124} else {125comp.resetPaintCalledFlag();126}127}128if (notDrawnCompsCount > 0) {129throw new RuntimeException(String.format(130"'paint' method of %d components was not called.", notDrawnCompsCount));131}132}133134private interface Testable {135boolean wasPaintCalled();136void resetPaintCalledFlag();137}138139private static class TestCanvas extends Canvas implements Testable {140private volatile boolean paintWasCalled = false;141142@Override143public void paint(Graphics g) {144paintWasCalled = true;145super.paint(g);146g.setColor(Color.BLUE);147g.fillRect(0, 0, getWidth(), getHeight());148}149150@Override151public boolean wasPaintCalled() {152return paintWasCalled;153}154155@Override156public void resetPaintCalledFlag() {157paintWasCalled = false;158}159}160161private static class TestButton extends Button implements Testable {162private volatile boolean paintWasCalled = false;163164@Override165public void paint(Graphics g) {166paintWasCalled = true;167super.paint(g);168g.setColor(Color.YELLOW);169g.fillRect(0, 0, 15, 15);170}171172@Override173public boolean wasPaintCalled() {174return paintWasCalled;175}176177@Override178public void resetPaintCalledFlag() {179paintWasCalled = false;180}181}182183public static void main(String[] args) {184new ComponentIsNotDrawnAfterRemoveAddTest().runTest();185}186}187188189