undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,169 @@
/*
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@test
@bug 4092418
@summary Test for drag events been taking by Lightweight Component
@key headful
@run main LWClobberDragEvent
*/
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class LWClobberDragEvent implements MouseListener, MouseMotionListener {
boolean isDragging;
static Frame frame;
LightweightComp lc;
final static int LWWidth = 200;
final static int LWHeight = 100;
final static int MAX_COUNT = 100;
Point locationOfLW;
public static void main(String[] args) throws Exception {
LWClobberDragEvent test = new LWClobberDragEvent();
try {
test.init();
test.start();
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
public void init() throws Exception {
EventQueue.invokeAndWait(() -> {
frame = new Frame();
frame.setLayout(new BorderLayout());
isDragging = false;
frame.addMouseMotionListener(this);
frame.addMouseListener(this);
frame.setBackground(Color.white);
lc = new LightweightComp();
lc.setSize(LWWidth, LWHeight);
lc.setLocation(50, 50);
lc.addMouseListener(this);
lc.addMouseMotionListener(this);
frame.add(lc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
public void start() throws Exception {
Robot robot = new Robot();
robot.delay(1000);
robot.waitForIdle();
EventQueue.invokeAndWait(() -> {
locationOfLW = getLocation(lc);
robot.mouseMove(locationOfLW.x + lc.getWidth() / 2,
locationOfLW.y - lc.getHeight() / 2);
});
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(1000);
//move mouse till the bottom of LWComponent
for (int i = 1; i < LWHeight + lc.getHeight() / 2; i++) {
robot.mouseMove(locationOfLW.x + lc.getWidth() / 2,
locationOfLW.y - lc.getHeight() / 2 + i);
}
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(1000);
System.out.println("Test Passed.");
}
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) {
if (evt.getSource() == this) {
if (isDragging) {
isDragging = false;
}
} else {
}
}
public Point getLocation( Component co ) throws RuntimeException {
Point pt = null;
boolean bFound = false;
int count = 0;
while ( !bFound ) {
try {
pt = co.getLocationOnScreen();
bFound = true;
} catch ( Exception ex ) {
bFound = false;
count++;
}
if ( !bFound && count > MAX_COUNT ) {
throw new RuntimeException("don't see a component to get location");
}
}
return pt;
}
public void mousePressed(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseMoved(MouseEvent evt) { }
public void mouseDragged(MouseEvent evt) {
if (evt.getSource() == this) {
if (!isDragging) {
isDragging = true;
}
} else {
if (isDragging) {
throw new RuntimeException("Test failed: Lightweight got dragging event.");
}
}
}
}
class LightweightComp extends Component {
public void paint(Graphics g) {
Dimension d = getSize();
g.setColor(Color.red);
g.fillRect(0, 0, d.width, d.height);
}
}

View file

@ -0,0 +1,99 @@
/*
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4147246
* @summary Simple check for peer != null in Component.componentMoved
* @key headful
* @run main LWParentMovedTest
*/
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
public class LWParentMovedTest {
static CMTFrame f;
// test will throw an exception and fail if lwc is null
public static void main(String[] args) throws Exception {
try {
EventQueue.invokeAndWait(() -> f = new CMTFrame());
} finally {
if (f != null) {
EventQueue.invokeAndWait(() -> f.dispose());
}
}
}
}
class CMTFrame extends Frame {
Container lwc;
Button button;
public CMTFrame() {
super("Moving LWC Test");
setLayout(new FlowLayout());
lwc = new LWSquare(Color.blue, 100, 100);
button = new Button();
lwc.add(button);
add(lwc);
setSize(400, 300);
setVisible(true);
// queue up a bunch of COMPONENT_MOVED events
for (int i = 0; i < 1000; i++) {
lwc.setLocation(i, i);
}
// remove heavyweight from lightweight container
lwc.remove(button);
}
}
//
// Lightweight container
//
class LWSquare extends Container {
int width;
int height;
public LWSquare(Color color, int w, int h) {
setBackground(color);
setLayout(new FlowLayout());
width = w;
height = h;
setName("LWSquare-" + color.toString());
}
public void paint(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, 1000, 1000);
}
}

View file

@ -0,0 +1,155 @@
/*
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4095214
* @summary Test change of focus on lightweights using the tab key
* @key headful
* @run main LightWeightTabFocus
*/
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class LightWeightTabFocus {
private static Frame f;
private static LightweightButton btn1;
private static Button btn2;
private static Robot robot;
private static volatile Point point;
private static Point loc;
public static void main(String[] args) throws Exception {
robot = new Robot();
robot.setAutoDelay(100);
try {
EventQueue.invokeAndWait(() -> createUI());
robot.delay(1000);
EventQueue.invokeAndWait(() -> {
loc = f.getLocation();
point = btn2.getLocation();
});
robot.mouseMove(loc.x + point.x, loc.y + point.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
// First TAB
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
if (!btn1.hasFocus()) {
new RuntimeException("First tab failed");
}
// Second TAB
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
if (!btn2.hasFocus()) {
new RuntimeException("Second tab failed");
}
// First SHIFT+TAB
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_SHIFT);
if (!btn1.hasFocus()) {
new RuntimeException("First shift+tab failed");
}
// Second SHIFT+TAB
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_SHIFT);
if (!btn2.hasFocus()) {
new RuntimeException("Second shift+tab failed");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
EventQueue.invokeAndWait(() -> {
if (f != null) {
f.dispose();
}
});
}
}
private static Frame createUI() {
f = new Frame("TAB Focus Change on LW Test");
f.setLayout(new FlowLayout());
btn1 = new LightweightButton();
f.add(btn1);
btn2 = new Button("Click Me To start");
f.add(btn2);
f.pack();
f.setVisible(true);
return f;
}
}
class LightweightButton extends Component implements FocusListener {
boolean focus;
LightweightButton() {
focus = false;
addFocusListener(this);
}
public Dimension getPreferredSize()
{
return new Dimension(100, 100);
}
public void focusGained(FocusEvent e) {
focus = true;
repaint();
}
public void focusLost(FocusEvent e) {
focus = false;
repaint();
}
public void paint(Graphics g) {
if (focus) {
g.drawString("Has Focus", 10, 20);
} else {
g.drawString("Not Focused", 10, 20);
}
}
public boolean isFocusable() {
return true;
}
}

View file

@ -0,0 +1,115 @@
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
/*
* @test
* @bug 4116029
* @summary drawString does not honor clipping regions for lightweight components
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual LightweightCliprect
*/
public class LightweightCliprect {
private static final String INSTRUCTIONS = """
If some text is drawn outside the red rectangle, press "Fail" button.
Otherwise, press "Pass" button.
""";
public static void main(String[] args) throws Exception {
PassFailJFrame.builder()
.title("LightweightCliprect Instructions Frame")
.instructions(INSTRUCTIONS)
.testTimeOut(5)
.rows(10)
.columns(45)
.testUI(LightweightCliprect::createUI)
.build()
.awaitAndCheck();
}
private static Frame createUI() {
Frame frame = new Frame("DefaultSize");
Container panel = new MyContainer();
MyComponent c = new MyComponent();
panel.add(c);
frame.add(panel);
frame.setSize(400, 300);
return frame;
}
}
class MyComponent extends Component {
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillRect(20, 20, 400, 200);
Shape clip = g.getClip();
g.setClip(20, 20, 400, 200);
//draw the current java version in the component
g.setColor(Color.black);
String version = System.getProperty("java.version");
String vendor = System.getProperty("java.vendor");
int y = 10;
for(int i = 0; i < 30; i++) {
g.drawString("Lightweight: Java version: " + version +
", Vendor: " + vendor, 10, y += 20);
}
g.setColor(c);
g.setClip(clip);
super.paint(g);
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
class MyContainer extends Container {
public MyContainer() {
super();
setLayout(new FlowLayout());
}
public void paint(Graphics g) {
Rectangle bounds = new Rectangle(getSize());
g.setColor(Color.cyan);
g.drawRect(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1);
super.paint(g);
}
}

View file

@ -0,0 +1,151 @@
/*
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@test
@bug 4050138
@summary Lightweight components: Enter/Exit mouse events
incorrectly reported during drag
@key headful
@run main LightweightDragTest
*/
import java.awt.AWTException;
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
public class LightweightDragTest {
MyComponent c,c2;
static Frame frame;
volatile int x = 0;
volatile int y = 0;
volatile int x2 = 0;
volatile int y2 = 0;
public static void main(String[] args) throws Exception {
LightweightDragTest test = new LightweightDragTest();
try {
EventQueue.invokeAndWait(() -> {
test.init();
});
test.start();
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
public void init() {
frame = new Frame("Test LightWeight Component Drag");
c = new MyComponent();
c2 = new MyComponent();
frame.add(c, BorderLayout.WEST);
frame.add(c2, BorderLayout.EAST);
frame.setSize(250, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void start() throws Exception {
Robot rb;
try {
rb = new Robot();
} catch (AWTException e) {
throw new Error("Could not create robot");
}
rb.setAutoDelay(10);
rb.delay(1000);
rb.waitForIdle();
EventQueue.invokeAndWait(() -> {
x = c.getLocationOnScreen().x + c.getWidth() / 2;
y = c.getLocationOnScreen().y + c.getHeight() / 2;
x2 = c2.getLocationOnScreen().x + c2.getWidth() / 2;
y2 = c2.getLocationOnScreen().y + c2.getHeight() / 2;
});
int xt = x;
int yt = y;
rb.mouseMove(xt, yt);
rb.mousePress(InputEvent.BUTTON1_MASK);
EventQueue.invokeAndWait(() -> {
c.isInside = true;
c2.isInside = false;
});
// drag
while (xt != x2 || yt != y2) {
if (x2 > xt) ++xt;
if (x2 < xt) --xt;
if (y2 > yt) ++yt;
if (y2 < yt) --yt;
rb.mouseMove(xt, yt);
}
rb.mouseRelease(InputEvent.BUTTON1_MASK);
EventQueue.invokeAndWait(() -> {
if (c.isInside || !c2.isInside) {
throw new Error("Test failed: mouse events did not arrive");
}
});
}
}
class MyComponent extends Component {
public boolean isInside;
public void paint(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,getSize().width,getSize().height);
}
public MyComponent() {
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
setBackground(Color.blue);
}
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
public void processEvent(AWTEvent e) {
int eventID = e.getID();
if ((eventID == MouseEvent.MOUSE_ENTERED)) {
setBackground(Color.red);
repaint();
isInside = true;
} else if (eventID == MouseEvent.MOUSE_EXITED) {
setBackground(Color.blue);
repaint();
isInside = false;
}
super.processEvent(e);
}
}

View file

@ -0,0 +1,305 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @key headful
* @summary Test of mouse move messages to lightweight components
* @library ../../regtesthelpers
* @build Util
* @compile LightweightEventTest.java
* @run main LightweightEventTest
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.SwingUtilities;
import test.java.awt.regtesthelpers.Util;
/*
There are 3 steps to this test :
1. Two frames are created one with heavy weight component and
another with light weight component. Each frame has a centrally placed
button
2. Mouse is dragged along diagonals of each window using Robot object
3. Events are noted for mouse in and out of frames & buttons and asserted
*/
public class LightweightEventTest {
private static EventBug HeavyComponent;
private static EventBug LightComponent;
private static Robot testRobot;
public static void main(String[] args) throws Throwable {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
constructTestUI();
}
});
try {
testRobot = new Robot();
} catch (AWTException ex) {
throw new RuntimeException("Could not initiate a drag operation");
}
testRobot.waitForIdle();
// Method performing auto test operation
boolean result = test();
disposeTestUI();
if (result == false) {
throw new RuntimeException("Test FAILED!");
}
}
private static boolean test() {
// Test events for HeavyComponent
Point loc = HeavyComponent.getLocationOnScreen();
Dimension size = HeavyComponent.getSize();
Util.mouseMove(testRobot,
new Point((int) loc.x + 4, (int) loc.y + 4),
new Point((int) loc.x + size.width, (int) loc.y + size.height));
testRobot.waitForIdle();
boolean HeavyComponentAssert = HeavyComponent.assertEvents(2, 1);
// Test events for LightComponent
loc = LightComponent.getLocationOnScreen();
size = LightComponent.getSize();
Util.mouseMove(testRobot,
new Point((int) loc.x + 4, (int) loc.y + 4),
new Point((int) loc.x + size.width, (int) loc.y + size.height));
testRobot.waitForIdle();
boolean LightComponentAssert = LightComponent.assertEvents(2, 1);
return (HeavyComponentAssert && LightComponentAssert);
}
private static void constructTestUI() {
// here, create the items that will be tested for correct behavior
HeavyComponent = new EventBug();
Button b = (Button) HeavyComponent.add("Center", new Button("Heavy"));
LightComponent = new EventBug();
BorderedLabel b1 = (BorderedLabel) LightComponent.add("Center",
new BorderedLabel("Lite"));
HeavyComponent.addListeners(b);
LightComponent.addListeners(b1);
LightComponent.setLocation(200, 0);
HeavyComponent.setVisible(true);
LightComponent.setVisible(true);
}
private static void disposeTestUI() {
HeavyComponent.setVisible(false);
LightComponent.setVisible(false);
HeavyComponent.dispose();
LightComponent.dispose();
}
}
/*
* Lightweight component
*/
class BorderedLabel extends Component {
boolean superIsButton = false;
String labelString;
BorderedLabel(String labelString) {
this.labelString = labelString;
Component thisComponent = this;
superIsButton = (thisComponent instanceof Button);
if (superIsButton) {
((Button) thisComponent).setLabel(labelString);
}
}
@Override
public Dimension getMinimumSize() {
Dimension minSize = new Dimension();
if (superIsButton) {
minSize = super.getMinimumSize();
} else {
Graphics g = getGraphics();
FontMetrics metrics = g.getFontMetrics();
minSize.width = metrics.stringWidth(labelString) + 14;
minSize.height = metrics.getMaxAscent()
+ metrics.getMaxDescent() + 9;
g.dispose();
g = null;
}
return minSize;
}
@Override
public Dimension getPreferredSize() {
Dimension prefSize;
if (superIsButton) {
prefSize = super.getPreferredSize();
} else {
prefSize = getMinimumSize();
}
return prefSize;
}
@Override
public void paint(Graphics g) {
super.paint(g);
Rectangle bounds = getBounds();
if (superIsButton) {
return;
}
Dimension size = getSize();
Color oldColor = g.getColor();
// draw border
g.setColor(getBackground());
g.fill3DRect(0, 0, size.width, size.height, false);
g.fill3DRect(3, 3, size.width - 6, size.height - 6, true);
// draw text
FontMetrics metrics = g.getFontMetrics();
int centerX = size.width / 2;
int centerY = size.height / 2;
int textX = centerX - (metrics.stringWidth(labelString) / 2);
int textY = centerY
+ ((metrics.getMaxAscent() + metrics.getMaxDescent()) / 2);
g.setColor(getForeground());
g.drawString(labelString, textX, textY);
g.setColor(oldColor);
}
} // class BorderedLabel
class EventBug extends Container {
Frame testFrame;
int frameEnters = 0;
int frameExits = 0;
int buttonEnters = 0;
int buttonExits = 0;
public EventBug() {
super();
testFrame = new Frame();
testFrame.setLayout(new BorderLayout());
this.setLayout(new BorderLayout());
testFrame.add("Center", this);
testFrame.pack();
testFrame.setVisible(true);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
@Override
public Insets getInsets() {
return new Insets(20, 20, 20, 20);
}
public boolean assertEvents(int expectedFrameEnterEvents,
int expectedButtonEnterEvents) {
return (frameEnters == expectedFrameEnterEvents)
&& (buttonEnters == expectedButtonEnterEvents);
}
// Forward to the Window
@Override
public void setLocation(int x, int y) {
testFrame.setLocation(x, y);
}
@Override
public void setVisible(boolean b) {
testFrame.setVisible(b);
}
public void dispose() {
testFrame.dispose();
}
// Add listeners to Frame and button
public void addListeners(Component b) {
b.setName("Button");
b.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
buttonEnters++;
}
@Override
public void mouseExited(MouseEvent e) {
buttonExits++;
}
});
testFrame.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
frameEnters++;
}
@Override
public void mouseExited(MouseEvent e) {
frameExits++;
}
});
}
} // class EventBug

View file

@ -0,0 +1,182 @@
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4077709 4153989
* @summary Lightweight component font settable test
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual LightweightFontTest
*/
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
public class LightweightFontTest {
static Font desiredFont = null;
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
[ There are 7 steps to this test ]
1. The 2 bordered labels (Emacs vs. vi) should be in a LARGE font
(approximately 1/2 inch tall)
2. The labels should not overlap.
3. Each button should be large enough to contain the entire label.
4. The labels should have red backgrounds
5. The text in the left label should be blue and the right yellow
6. Resize the window to make it much smaller and larger
7. The buttons should never overlap, and they should be large
enough to contain the entire label.
(although the button may disappear if there is not enough
room in the window)"
""";
PassFailJFrame.builder()
.title("Test Instructions")
.instructions(INSTRUCTIONS)
.rows((int) INSTRUCTIONS.lines().count() + 2)
.columns(35)
.testUI(LightweightFontTest::createUI)
.logArea(5)
.build()
.awaitAndCheck();
}
private static Frame createUI() {
Frame f = new Frame("Lightweight Font Test");
f.setLayout(new FlowLayout());
desiredFont = new Font(Font.DIALOG, Font.PLAIN, 36);
Component component;
component = new BorderedLabel("Emacs or vi?");
component.setFont(desiredFont);
component.setBackground(Color.red);
component.setForeground(Color.blue);
f.add(component);
component = new BorderedLabel("Vi or Emacs???");
component.setFont(desiredFont);
component.setBackground(Color.red);
component.setForeground(Color.yellow);
f.add(component);
f.pack();
return f;
}
}
/**
* Lightweight component
*/
class BorderedLabel extends Component {
boolean superIsButton;
String labelString;
BorderedLabel(String labelString) {
this.labelString = labelString;
Component thisComponent = this;
superIsButton = (thisComponent instanceof Button);
if(superIsButton) {
((Button)thisComponent).setLabel(labelString);
}
}
public Dimension getMinimumSize() {
Dimension minSize = new Dimension();
if (superIsButton) {
minSize = super.getMinimumSize();
} else {
Graphics g = getGraphics();
verifyFont(g);
FontMetrics metrics = g.getFontMetrics();
minSize.width = metrics.stringWidth(labelString) + 14;
minSize.height = metrics.getMaxAscent() + metrics.getMaxDescent() + 9;
g.dispose();
}
return minSize;
}
public Dimension getPreferredSize() {
Dimension prefSize = new Dimension();
if (superIsButton) {
prefSize = super.getPreferredSize();
} else {
prefSize = getMinimumSize();
}
return prefSize;
}
public void paint(Graphics g) {
verifyFont(g);
super.paint(g);
if (superIsButton) {
return;
}
Dimension size = getSize();
Color oldColor = g.getColor();
// draw border
g.setColor(getBackground());
g.fill3DRect(0, 0, size.width, size.height, false);
g.fill3DRect(3, 3, size.width - 6, size.height - 6, true);
// draw text
FontMetrics metrics = g.getFontMetrics();
int centerX = size.width / 2;
int centerY = size.height / 2;
int textX = centerX - (metrics.stringWidth(labelString) / 2);
int textY = centerY + ((metrics.getMaxAscent()
+ metrics.getMaxDescent()) / 2);
g.setColor(getForeground());
g.drawString(labelString, textX, textY);
g.setColor(oldColor);
}
/**
* Verifies that the font is correct and prints a warning
* message and/or throws a RuntimeException if it is not.
*/
private void verifyFont(Graphics g) {
Font desiredFont = LightweightFontTest.desiredFont;
Font actualFont = g.getFont();
if (!actualFont.equals(desiredFont)) {
PassFailJFrame.log("AWT BUG: FONT INFORMATION LOST!");
PassFailJFrame.log(" Desired font: " + desiredFont);
PassFailJFrame.log(" Actual font: " + actualFont);
PassFailJFrame.forceFail();
}
}
}

View file

@ -0,0 +1,118 @@
/*
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4058400
* @summary Tests that calling addNotify on a lightweight component more than
* once does not break event dispatching for that component.
* @key headful
* @run main MultipleAddNotifyTest
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MultipleAddNotifyTest {
static volatile boolean passFlag;
static volatile int posX;
static volatile int posY;
static Frame f;
static LightComponent l;
public static void main(String[] args) throws Exception {
Robot r;
try {
r = new Robot();
r.setAutoWaitForIdle(true);
passFlag = false;
EventQueue.invokeAndWait(() -> {
f = new Frame("Multiple addNotify Test");
l = new LightComponent();
f.setLayout(new FlowLayout());
l.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked");
passFlag = true;
}
});
f.add(l);
f.addNotify();
f.addNotify();
if (!l.isVisible()) {
throw new RuntimeException("Test failed. LW Component " +
"not visible.");
}
f.setSize(200, 200);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
r.waitForIdle();
r.delay(1000);
EventQueue.invokeAndWait(() -> {
posX = f.getX() + l.getWidth() + (l.getWidth() / 2);
posY = f.getY() + l.getHeight();
});
r.mouseMove(posX, posY);
r.delay(500);
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
r.delay(500);
if (!passFlag) {
throw new RuntimeException("Test failed. MouseClicked event " +
"not working properly.");
}
} finally {
EventQueue.invokeAndWait(() -> {
if (f != null) {
f.dispose();
}
});
}
}
}
class LightComponent extends Component {
public void paint(Graphics g) {
setSize(100, 100);
Dimension d = getSize();
g.setColor(Color.red);
g.fillRect(0, 0, d.width, d.height);
}
}

View file

@ -0,0 +1,93 @@
/*
* Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4476083
* @summary Disabled components do not receive MouseEvent in Popups
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual PopupTest
*/
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class PopupTest {
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
PopupMenus should disappear when a disabled component is
clicked.
Step 1. Pop down the popup menu by clicking on it.
Step 2. Click on the disabled component to make the menu
disappear.
If the menu disappears when the disabled component is clicked,
the test passes, otherwise, the test fails.
""";
PassFailJFrame.builder()
.title("Test Instructions")
.instructions(INSTRUCTIONS)
.columns(35)
.testUI(PopupTest::createUI)
.build()
.awaitAndCheck();
}
private static Frame createUI() {
Frame f = new Frame("Disabled Component in Popup Test");
f.setLayout(new BorderLayout());
JButton b = new JButton("step 1: press me to display menu");
b.addActionListener(e -> {
JPopupMenu m = new JPopupMenu();
m.add(new JMenuItem("item 1"));
m.add(new JMenuItem("item 2"));
m.add(new JMenuItem("item 3"));
m.add(new JMenuItem("item 4"));
m.add(new JMenuItem("item 5"));
m.add(new JMenuItem("item 6"));
m.show((Component) e.getSource(), 0, 10);
});
JLabel disabled = new JLabel("step 2: click me. the menu should be " +
"dismissed");
disabled.setEnabled(false);
JLabel enabled = new JLabel("step 3: there is no step 3");
f.add(BorderLayout.NORTH, b);
f.add(BorderLayout.CENTER, disabled);
f.add(BorderLayout.SOUTH, enabled);
f.setSize(300, 200);
return f;
}
}