Skip to content

Commit

Permalink
8315726: Open source several AWT applet tests
Browse files Browse the repository at this point in the history
Reviewed-by: lucy
Backport-of: 1741d13b1260253d1e299e8da9c42b5519a7ae48
  • Loading branch information
Andrew Lu committed Feb 28, 2024
1 parent fe840ca commit e5cc20c
Show file tree
Hide file tree
Showing 4 changed files with 848 additions and 0 deletions.
174 changes: 174 additions & 0 deletions test/jdk/java/awt/Choice/ChoiceSelectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* 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.
*/

import java.awt.Choice;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Panel;

/*
* @test
* @bug 4115139 4128213
* @summary Tests that the (rather bizarre) rules for handling selection
* in Choice components are implemented as documented in
* "The Java Class Libraries 2nd Edition"
* @key headful
*/

public class ChoiceSelectTest extends Panel {
final Choice c;

public ChoiceSelectTest() {
setLayout(new FlowLayout());
c = new Choice();
add(c);
}

private void test() {
testAddition();
testInsertion();
testRemoval();
testIndices();
}

public void testAddition() {
c.removeAll();

// check that after first item added selection is zero
c.addItem("zero");
if (c.getSelectedIndex() != 0) {
throw new SelectionException("selection wrong after first add");
}

// check that selection doesn't change for subsequent adds
c.addItem("one");
c.select(1);
c.addItem("two");
if (c.getSelectedIndex() != 1) {
throw new SelectionException("selection wrong after subsequent add");
}
}

public void testInsertion() {
c.removeAll();

// check that after first item inserted selection is zero
c.insert("zero", 0);
if (c.getSelectedIndex() != 0) {
throw new SelectionException("selection wrong after first insert");
}

// check that if selected item shifted, selection goes to zero
c.insert("three", 1);
c.select(1);
c.insert("one", 1);
if (c.getSelectedIndex() != 0) {
throw new SelectionException("selection wrong after selected item shifted");
}

// check that if selected item not shifted, selection stays the same
c.select(1);
c.insert("two", 2);
if (c.getSelectedIndex() != 1) {
throw new SelectionException("selection wrong after item inserted after selection");
}
}

public void testRemoval() {
c.removeAll();

// check that if removing selected item, selection goes to 0
c.add("zero");
c.add("one");
c.add("two");
c.select(2);
c.remove(2);
if (c.getSelectedIndex() != 0) {
throw new SelectionException("selection wrong after removing selected item");
}

// check that if removing item before the selection
// the selected index is updated
c.add("two");
c.add("three");
c.select(3);
c.remove(1);
if (c.getSelectedIndex() != 2) {
throw new SelectionException("selection wrong after removing item before it");
}
}

public void testIndices() {
c.removeAll();

c.addItem("zero");
c.addItem("one");
c.addItem("two");
c.addItem("three");
c.addItem("four");
c.addItem("five");

// Test selection of negative index
try {
c.select(-1);
throw new SelectionException("Negative Index Test FAILED");
} catch (IllegalArgumentException expected) {}

// Test selection of zero index
try {
c.select(0);
} catch (IllegalArgumentException iae) {
throw new SelectionException("Zero Index Test FAILED", iae);
}

// Test selection of maximum index
try {
c.select(5);
} catch (IllegalArgumentException iae) {
throw new SelectionException("Maximum Index Test FAILED", iae);
}

// Test selection of index that is too large
try {
c.select(6);
throw new SelectionException("Greater than Maximum Index Test FAILED");
} catch (IllegalArgumentException expected) {}
}

public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(() -> new ChoiceSelectTest().test());
}

class SelectionException extends RuntimeException {
SelectionException(String msg, Throwable cause) {
super(msg, cause);
System.out.println(
"Selection item is '" + c.getSelectedItem() +
"' at index " + c.getSelectedIndex());
}

SelectionException(String msg) {
this(msg, null);
}
}
}
156 changes: 156 additions & 0 deletions test/jdk/java/awt/Component/Displayable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* 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.
*/

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.Label;
import java.awt.Panel;

/*
* @test
* @key headful
* @summary automated test for "displayable" property on Component
*/

public class Displayable extends Panel {
Label status = new Label("Displayable Test started...");

public void init() {
setLayout(new BorderLayout());
add("South", status);

LightDisplayable light = new LightDisplayable();
shouldNotBeDisplayable(light, "before added to container ");

HeavyDisplayable heavy = new HeavyDisplayable();
shouldNotBeDisplayable(heavy, "before added to container ");

add("West", light);
add("East", heavy);

statusMessage("Displayable test completed successfully.");
}

protected void addImpl(Component child, Object constraints, int index) {
super.addImpl(child, constraints, index);
if (isDisplayable()) {
shouldBeDisplayable(child, "after added to displayable container ");
} else {
shouldNotBeDisplayable(child, "after added to undisplayable container ");
}
}

public void remove(Component child) {
super.remove(child);
shouldNotBeDisplayable(child, "after removed from displayable container ");
}

public void statusMessage(String msg) {
status.setText(msg);
status.invalidate();
validate();
}

public static void shouldNotBeDisplayable(Component c, String why) {
if (c.isDisplayable()) {
throw new RuntimeException("Component is displayable "+why+c.getName());
}
}

public static void shouldBeDisplayable(Component c, String why) {
if (!c.isDisplayable()) {
throw new RuntimeException("Component is NOT displayable "+why+c.getName());
}
}

public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(() -> {
Frame f = new Frame();
try {
Displayable test = new Displayable();
test.init();
f.add("North", test);
f.pack();
} finally {
f.dispose();
}
});
}
}

class LightDisplayable extends Component {

public Dimension getPreferredSize() {
return new Dimension(50,50);
}

public void paint(Graphics g) {
Dimension size = getSize();
g.setColor(Color.blue);
g.fillRect(0, 0, size.width, size.height);
super.paint(g);
}

public void addNotify() {
Displayable.shouldNotBeDisplayable(this, "before addNotify ");
super.addNotify();
Displayable.shouldBeDisplayable(this, "after addNotify ");
}

public void removeNotify() {
Displayable.shouldBeDisplayable(this, "before removeNotify ");
super.removeNotify();
Displayable.shouldNotBeDisplayable(this, "after removeNotify ");
}
}

class HeavyDisplayable extends Panel {

public Dimension getPreferredSize() {
return new Dimension(50, 50);
}

public void paint(Graphics g) {
Dimension size = getSize();
g.setColor(Color.black);
g.fillRect(0, 0, size.width, size.height);
super.paint(g);
}

public void addNotify() {
Displayable.shouldNotBeDisplayable(this, "before addNotify ");
super.addNotify();
Displayable.shouldBeDisplayable(this, "after addNotify ");
}

public void removeNotify() {
Displayable.shouldBeDisplayable(this, "before removeNotify ");
super.removeNotify();
Displayable.shouldNotBeDisplayable(this, "after removeNotify ");
}
}

1 comment on commit e5cc20c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.