8340625: Open source additional Component tests (part 3)

Reviewed-by: psadhukhan
This commit is contained in:
Alexander Zuev 2024-10-03 16:19:09 +00:00
parent 3ee94e040a
commit ebb4759c3d
3 changed files with 803 additions and 0 deletions

View File

@ -0,0 +1,225 @@
/*
* Copyright (c) 1997, 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 4045781
* @summary Exposed/damaged canvases don't always update correctly
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual PaintGlitchTest
*/
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.Scrollbar;
import java.awt.TextArea;
import java.awt.TextField;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class PaintGlitchTest extends Frame {
static final String INSTRUCTIONS = """
1. Click on the 'Painting Glitch Test' window and select from
its menu a content type (text, gradient, fill,
AWT components, Swing components etc.).
2. Select 'Modal Dialog...' to create a dialog.
3. Drag the dialog over the content very fast
for 10 seconds or so - make sure you
keep dragging while the content is painting.
4. Verify that the area exposed by the drag (the damaged regions)
always update properly no white areas or bits of the dialog
should be left after the drag operation is
completed (i.e. after you let go of the mouse).
5. Repeat for all other content types.
6. If for any content type the damaged dialog is not properly
repainted press Fail. Otherwise press Pass.
""";
public PaintGlitchTest() {
super("Painting Glitch Test");
TextPanel textPanel = new TextPanel();
GradientPanel gradientPanel = new GradientPanel();
ComponentPanel componentPanel = new ComponentPanel();
SwingPanel swingPanel = new SwingPanel();
add(textPanel);
MenuBar menubar = new MenuBar();
Menu testMenu = new Menu("Test");
testMenu.add(makeContentItem("Text Lines", textPanel) );
testMenu.add(makeContentItem("Gradient Fill", gradientPanel) );
testMenu.add(makeContentItem("AWT Components", componentPanel) );
testMenu.add(makeContentItem("Swing Components", swingPanel) );
testMenu.addSeparator();
MenuItem dialogItem = new MenuItem("Modal Dialog...");
dialogItem.addActionListener(ev -> new ObscuringDialog(PaintGlitchTest.this).show());
testMenu.add(dialogItem);
testMenu.addSeparator();
menubar.add(testMenu);
setMenuBar(menubar);
setSize(400,300);
}
public static void main(String args[]) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Repaint Glitch")
.testUI(PaintGlitchTest::new)
.instructions(INSTRUCTIONS)
.columns(40)
.logArea()
.build()
.awaitAndCheck();
}
public MenuItem makeContentItem(String title, final Component content) {
MenuItem menuItem = new MenuItem(title);
menuItem.addActionListener(
ev -> {
remove(0);
add(content);
validate();
}
);
return menuItem;
}
}
class GradientPanel extends Canvas {
public void paint(Graphics g) {
long ms = System.currentTimeMillis();
// just paint something that'll take a while
int x, y;
int width = getSize().width;
int height = getSize().height;
int step = 8;
for (x = 0; x < width; x += step) {
for (y = 0; y < height; y += step) {
int red = (255 * y) / height;
int green = (255 * x * y) / (width * height);
int blue = (255 * x) / width;
Color color = new Color(red, green, blue);
g.setColor(color);
g.fillRect(x, y, step, step);
}
}
long time = System.currentTimeMillis() - ms;
PassFailJFrame.log("GradientPanel paint took " + time + " ms");
}
public Dimension getPreferredSize() {
return new Dimension(200,1000);
}
}
class TextPanel extends Canvas {
public void paint(Graphics g) {
long ms = System.currentTimeMillis();
Font font = new Font("SanSerif", Font.ITALIC, 12);
g.setFont(font);
// just paint something that'll take a while
int x, y;
int height = getHeight();
int step = 16;
for (x = y = 0; y < height; y += step) {
g.drawString(y + " : The quick brown fox jumps over the lazy dog. " +
"The rain in Spain falls mainly on the plain.", x, y);
}
long time = System.currentTimeMillis() - ms;
PassFailJFrame.log("TextPanel paint took " + time + " ms");
}
public Dimension getPreferredSize() {
return new Dimension(640,1000);
}
}
class ComponentPanel extends Panel {
ComponentPanel() {
add(new Label("Label"));
add(new Button("Button"));
add(new Checkbox("Checkbox"));
Choice c = new Choice();
c.add("choice");
java.awt.List l = new java.awt.List();
l.add("list");
add(new Scrollbar());
add(new TextField("TextField"));
add(new TextArea("TextArea"));
add(new Panel());
add(new Canvas());
}
}
class SwingPanel extends JPanel {
SwingPanel() {
add(new JLabel("JLabel"));
add(new JButton("JButton"));
add(new JCheckBox("JCheckBox"));
JComboBox c = new JComboBox();
JList l = new JList();
add(new JScrollBar());
add(new JTextField("This is a JTextField with some text in it to make it longer."));
add(new JTextArea("This is a JTextArea with some text in it to make it longer."));
}
}
class ObscuringDialog extends Dialog {
ObscuringDialog(Frame f) {
super(f, "Obscuring Dialog");
Button ok = new Button("OK, go away");
ok.addActionListener(ev -> dispose());
add(ok);
pack();
}
}

View File

@ -0,0 +1,455 @@
/*
* 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 4292099
* @summary AWT Event delivery to processEvent
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual ProcessEvent
*/
import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.Adjustable;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.ItemSelectable;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.lang.reflect.InvocationTargetException;
public class ProcessEvent extends Frame {
static final String INSTRUCTIONS = """
Press each of the four buttons for ActionEvent, AdjustmentEvent,
ItemEvent and TextEvent. If a message for each corresponding event
appears in the log area and says the event listener was
called, then press Pass otherwise press Fail.
""";
ActionBtn af;
AdjustmentBtn adjf;
ItemBtn itf;
TextBtn txtf;
public ProcessEvent() {
setLayout(new FlowLayout());
add(af = new ActionBtn());
af.setBackground(Color.green);
add(adjf = new AdjustmentBtn());
adjf.setBackground(Color.green);
add(itf = new ItemBtn());
itf.setBackground(Color.green);
add(txtf = new TextBtn());
txtf.setBackground(Color.green);
// These action listeners simply provide feedback of when
// the event is delivered properly.
af.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
PassFailJFrame.log(af.getText()
+ ": action listener called: "
+ ae.toString());
}
});
adjf.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent ae) {
PassFailJFrame.log(adjf.getText()
+ ": adjustment listener called: "
+ ae.toString());
}
});
itf.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
PassFailJFrame.log(itf.getText()
+ ": item listener called: "
+ e.toString());
}
});
txtf.addTextListener(new TextListener() {
public void textValueChanged(TextEvent e) {
PassFailJFrame.log(txtf.getText()
+ ": text listener called: "
+ e.toString());
}
});
pack();
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Process Events Test")
.testUI(ProcessEvent::new)
.instructions(INSTRUCTIONS)
.columns(40)
.logArea()
.build()
.awaitAndCheck();
}
}
class ButtonComponent extends Component implements ItemSelectable, Adjustable {
transient protected TextListener textListener;
transient ActionListener actionListener;
transient AdjustmentListener adjustmentListener;
transient ItemListener itemListener;
String actionCommand = null;
String text = null;
public ButtonComponent(String label) {
super();
text = label;
}
public String getText() {
return text;
}
public Dimension getPreferredSize() {
return new Dimension(200, 30);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public String getActionCommand() {
if (actionCommand == null)
return getText();
else
return actionCommand;
}
public void setActionCommand(String ac) {
actionCommand = ac;
}
// ActionEvent listener support
public synchronized void addActionListener(ActionListener l) {
if (l == null) {
return;
}
enableEvents(AWTEvent.ACTION_EVENT_MASK);
actionListener = AWTEventMulticaster.add(actionListener, l);
}
public synchronized void removeActionListener(ActionListener l) {
if (l == null) {
return;
}
actionListener = AWTEventMulticaster.remove(actionListener, l);
}
// AdjustmentEvent listener support
public synchronized void addAdjustmentListener(AdjustmentListener l) {
if (l == null) {
return;
}
enableEvents(AWTEvent.ADJUSTMENT_EVENT_MASK);
adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l);
}
public synchronized void removeAdjustmentListener(AdjustmentListener l) {
if (l == null) {
return;
}
adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l);
}
// ItemEvent listener support
public synchronized void addItemListener(ItemListener l) {
if (l == null) {
return;
}
enableEvents(AWTEvent.ITEM_EVENT_MASK);
itemListener = AWTEventMulticaster.add(itemListener, l);
}
public synchronized void removeItemListener(ItemListener l) {
if (l == null) {
return;
}
itemListener = AWTEventMulticaster.remove(itemListener, l);
}
// TextEvent listener support
public synchronized void addTextListener(TextListener l) {
if (l == null) {
return;
}
enableEvents(AWTEvent.TEXT_EVENT_MASK);
textListener = AWTEventMulticaster.add(textListener, l);
}
public synchronized void removeTextListener(TextListener l) {
if (l == null) {
return;
}
textListener = AWTEventMulticaster.remove(textListener, l);
}
// Implement the processEvent and processXXXEvent methods to
// handle reception and processing of the event types.
protected void processEvent(AWTEvent e) {
if (e instanceof ActionEvent) {
processActionEvent((ActionEvent) e);
return;
}
if (e instanceof AdjustmentEvent) {
processAdjustmentEvent((AdjustmentEvent) e);
return;
}
if (e instanceof ItemEvent) {
processItemEvent((ItemEvent) e);
return;
}
if (e instanceof TextEvent) {
processTextEvent((TextEvent) e);
return;
}
super.processEvent(e);
}
protected void processActionEvent(ActionEvent e) {
if (actionListener != null) {
actionListener.actionPerformed(e);
}
}
protected void processAdjustmentEvent(AdjustmentEvent e) {
if (adjustmentListener != null) {
adjustmentListener.adjustmentValueChanged(e);
}
}
protected void processItemEvent(ItemEvent e) {
if (itemListener != null) {
itemListener.itemStateChanged(e);
}
}
protected void processTextEvent(TextEvent e) {
if (textListener != null) {
textListener.textValueChanged(e);
}
}
public void paint(Graphics g) {
Dimension dim = getSize();
g.clearRect(0, 0, dim.width, dim.height);
g.setColor(getForeground());
g.drawString(text, 2, dim.height - 2);
}
/**
* Returns the selected items or null if no items are selected.
*/
public Object[] getSelectedObjects() {
return null;
}
/**
* Gets the orientation of the adjustable object.
*/
public int getOrientation() {
return 0;
}
/**
* Gets the minimum value of the adjustable object.
*/
public int getMinimum() {
return 0;
}
/**
* Sets the minimum value of the adjustable object.
*
* @param min the minimum value
*/
public void setMinimum(int min) {
}
/**
* Gets the maximum value of the adjustable object.
*/
public int getMaximum() {
return 0;
}
/**
* Sets the maximum value of the adjustable object.
*
* @param max the maximum value
*/
public void setMaximum(int max) {
}
/**
* Gets the unit value increment for the adjustable object.
*/
public int getUnitIncrement() {
return 0;
}
/**
* Sets the unit value increment for the adjustable object.
*
* @param u the unit increment
*/
public void setUnitIncrement(int u) {
}
/**
* Gets the block value increment for the adjustable object.
*/
public int getBlockIncrement() {
return 0;
}
/**
* Sets the block value increment for the adjustable object.
*
* @param b the block increment
*/
public void setBlockIncrement(int b) {
}
/**
* Gets the length of the propertional indicator.
*/
public int getVisibleAmount() {
return 0;
}
/**
* Sets the length of the proportionl indicator of the
* adjustable object.
*
* @param v the length of the indicator
*/
public void setVisibleAmount(int v) {
}
/**
* Gets the current value of the adjustable object.
*/
public int getValue() {
return 0;
}
/**
* Sets the current value of the adjustable object. This
* value must be within the range defined by the minimum and
* maximum values for this object.
*
* @param v the current value
*/
public void setValue(int v) {
}
}
class ActionBtn extends ButtonComponent {
public ActionBtn() {
super("ActionEvent");
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
ActionEvent ae = new ActionEvent(e.getSource(),
ActionEvent.ACTION_PERFORMED,
getActionCommand());
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ae);
}
});
}
}
class AdjustmentBtn extends ButtonComponent {
public AdjustmentBtn() {
super("AdjustmentEvent");
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
AdjustmentEvent ae = new AdjustmentEvent((Adjustable) e.getSource(),
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
1, 1);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ae);
}
});
}
}
class ItemBtn extends ButtonComponent {
public ItemBtn() {
super("ItemEvent");
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
ItemEvent ae = new ItemEvent((ItemSelectable) e.getSource(),
ItemEvent.ITEM_STATE_CHANGED,
e.getSource(), 1);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ae);
}
});
}
}
class TextBtn extends ButtonComponent {
public TextBtn() {
super("TextEvent");
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
TextEvent ae = new TextEvent(e.getSource(),
TextEvent.TEXT_VALUE_CHANGED);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ae);
}
});
}
}

View File

@ -0,0 +1,123 @@
/*
* Copyright (c) 2003, 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 4906548 4921849
* @summary Checks that setFont and setBackground have immediate effect
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual SetBgrFnt
*/
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.lang.reflect.InvocationTargetException;
public class SetBgrFnt extends Frame {
static final String INSTRUCTIONS = """
1. Press a button marked 'Switch fonts'
fonts in three components below (a Button, a Checkbox
and a Label) must change immediately.
2. Press a button marked 'Switch background'
background of three components and canvas must change.
MacOS is an exception - AWT buttons on macOS so not
change color so on macOS only canvas, checkbox
and a label should change background.
If this is the behavior that you observe press Pass,
otherwise press Fail.
""";
Label la;
Button bu, bu1, bu2;
Checkbox cb;
Font font1, font2;
Canvas ca;
boolean bToggleFont = true;
boolean bToggleBg = true;
public SetBgrFnt() {
bu = new Button("Switch fonts");
bu1 = new Button("Switch background");
bu2 = new Button("I'm a button");
cb = new Checkbox("Checkbox I am");
la = new Label("I am a label");
ca = new Canvas();
font1 = new Font("Serif", Font.ITALIC, 22);
font2 = new Font("SansSerif", Font.PLAIN, 10);
la.setFont(font1);
cb.setFont(font1);
bu2.setFont(font1);
bu.addActionListener(ae -> {
if (bToggleFont) {
la.setFont(font2);
cb.setFont(font2);
bu2.setFont(font2);
} else {
la.setFont(font1);
cb.setFont(font1);
bu2.setFont(font1);
}
bToggleFont = !bToggleFont;
});
bu1.addActionListener(ae -> {
if (bToggleBg) {
ca.setBackground(Color.YELLOW);
setBackground(Color.YELLOW);
} else {
ca.setBackground(Color.GREEN);
setBackground(Color.GREEN);
}
bToggleBg = !bToggleBg;
});
setLayout(new GridLayout(8, 1));
add(bu);
add(bu1);
add(new Label());
add("South", la);
add("South", bu2);
add("South", cb);
add("South", ca);
pack();
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Set Font and Background Test")
.testUI(SetBgrFnt::new)
.instructions(INSTRUCTIONS)
.columns(40)
.build()
.awaitAndCheck();
}
}