Skip to content

Commit

Permalink
8284316: Support accessibility ManualTestFrame.java for non SwingSet …
Browse files Browse the repository at this point in the history
…tests

Reviewed-by: kizune
  • Loading branch information
lawrence-andrew authored and prrace committed May 9, 2022
1 parent 40470d8 commit 6a7c023
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 47 deletions.
41 changes: 19 additions & 22 deletions test/jdk/javax/accessibility/manual/SwingSetTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/**
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

import lib.ManualTestFrame;
import lib.TestResult;

import javax.imageio.ImageIO;
import java.io.File;
import java.util.function.Consumer;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.util.function.Supplier;
import javax.swing.JEditorPane;

import static java.io.File.separator;

Expand All @@ -22,10 +21,21 @@ public class SwingSetTest {
public static void main(String[] args) throws IOException, InterruptedException,
ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
System.out.println("test image " + System.getenv("TEST_IMAGE_DIR"));

Consumer<JEditorPane> testInstructionProvider = e -> {
try {
e.setContentType("text/html");
e.setPage(SwingSetTest.class.getResource(args[0] + ".html"));
} catch (IOException exception) {
exception.printStackTrace();
}
};

Supplier<TestResult> resultSupplier = ManualTestFrame.showUI(args[0],
"Wait for SwingSet2 to load, follow the instructions, select pass or fail. " +
"Do not close windows manually.",
SwingSetTest.class.getResource(args[0] + ".html"));
testInstructionProvider);

String swingSetJar = System.getenv("SWINGSET2_JAR");
if (swingSetJar == null) {
swingSetJar = "file://" + System.getProperty("java.home") +
Expand All @@ -37,23 +47,10 @@ public static void main(String[] args) throws IOException, InterruptedException,
System.out.println("Loading SwingSet2 from " + swingSetJar);
ClassLoader ss = new URLClassLoader(new URL[]{new URL(swingSetJar)});
ss.loadClass("SwingSet2").getMethod("main", String[].class).invoke(null, (Object)new String[0]);

//this will block until user decision to pass or fail the test
TestResult result = resultSupplier.get();
if (result != null) {
System.err.println("Failure reason: \n" + result.getFailureDescription());
if (result.getScreenCapture() != null) {
File screenDump = new File(System.getProperty("test.classes") + separator + args[0] + ".png");
System.err.println("Saving screen image to " + screenDump.getAbsolutePath());
ImageIO.write(result.getScreenCapture(), "png", screenDump);
}
Throwable e = result.getException();
if (e != null) {
throw new RuntimeException(e);
} else {
if (!result.getStatus()) throw new RuntimeException("Test failed!");
}
} else {
throw new RuntimeException("No result returned!");
}
ManualTestFrame.handleResult(result, args[0]);
}
}
}

117 changes: 117 additions & 0 deletions test/jdk/javax/accessibility/manual/TestJProgressBarAccessibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2022 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 manual test for accessibility JProgressBar
@run main/manual TestJProgressBarAccessibility
*/

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.accessibility.AccessibleContext;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import lib.ManualTestFrame;
import lib.TestResult;

public class TestJProgressBarAccessibility {

private static JFrame frame;
private static volatile int value = 10;
private static final String instruction = """
Aim : Check whether JProgressBar value is read in case of VoiceOver or
Screen magnifier shows the magnified value in case of Screen magnifier is enabled
1) Move the mouse pointer over the JProgressBar and if you
hear the JProgressBar value in case of VoiceOver then the test pass else fail.
2) Move the mouse pointer over the JProgressBar and if you see the magnified value
when Screen magnifier is enabled then the test pass else fail.
""";

private static void createTestUI() throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(() -> {
frame = new JFrame("Test JProgressBar accessibility");
JProgressBar progressBar = new JProgressBar();
progressBar.setValue(value);
progressBar.setStringPainted(true);

progressBar.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if ( value == 100) {
value = 0;
} else {
value += 5;
}
progressBar.setValue(value);
}
});

AccessibleContext accessibleContext =
progressBar.getAccessibleContext();
accessibleContext.setAccessibleName("JProgressBar accessibility name");
accessibleContext.setAccessibleDescription("Jprogress accessibility " +
"description");

frame.getContentPane().add(progressBar, BorderLayout.CENTER);

frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}

public static void main(String[] args) throws InterruptedException,
InvocationTargetException, IOException {

Consumer<JEditorPane> testInstProvider = e -> {
e.setContentType("text/plain");
e.setText(instruction);
};

Supplier<TestResult> resultSupplier = ManualTestFrame.showUI(
"JProgressBar " +
"Accessibility Test", "Wait until the Test UI is " +
"seen", testInstProvider);

// Create and show TestUI
createTestUI();

//this will block until user decision to pass or fail the test
TestResult testResult = resultSupplier.get();
ManualTestFrame.handleResult(testResult,"TestJProgressBarAccessibility");
}
}

15 changes: 8 additions & 7 deletions test/jdk/javax/accessibility/manual/lib/DescriptionPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package lib;

import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.net.URL;
import java.util.function.Consumer;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
* Displays instructions provided through a URL.
*/
class DescriptionPane extends JPanel {

DescriptionPane(URL instructions) throws IOException {
DescriptionPane(Consumer<JEditorPane> instructions) {
JEditorPane editorPane = new JEditorPane();
editorPane.setFocusable(false);
editorPane.setContentType("text/html");
editorPane.setPage(instructions);
instructions.accept(editorPane);
editorPane.setEditable(false);

JScrollPane esp = new JScrollPane(editorPane);
Expand All @@ -51,3 +51,4 @@ class DescriptionPane extends JPanel {
add(esp);
}
}

81 changes: 63 additions & 18 deletions test/jdk/javax/accessibility/manual/lib/ManualTestFrame.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 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
Expand All @@ -22,25 +22,31 @@
*/
package lib;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

import static java.awt.BorderLayout.*;
import static java.awt.BorderLayout.CENTER;
import static java.awt.BorderLayout.NORTH;
import static java.awt.BorderLayout.SOUTH;
import static java.io.File.separator;
import static javax.swing.SwingUtilities.invokeAndWait;

/**
Expand All @@ -51,7 +57,9 @@ public class ManualTestFrame extends JFrame {

private boolean alreadyFailed = false;

private ManualTestFrame(String testName, String headerText, URL instructions, Consumer<TestResult> listener) throws IOException {
private ManualTestFrame(String testName, String headerText,
Consumer<JEditorPane> instructions,
Consumer<TestResult> listener) throws IOException {

super(testName);

Expand Down Expand Up @@ -124,14 +132,17 @@ private ManualTestFrame(String testName, String headerText, URL instructions, Co

/**
* Show a test control frame which allows a user to either pass or fail the test.
* @param testName
* @param headerText
* @param instructions
*
* @param testName name of the testcase
* @param headerText information to the user to wait for the test frame.
* @param instructions test instruction for the user
* @return Returning supplier blocks till the test is passed or failed by the user.
* @throws InterruptedException
* @throws InvocationTargetException
* @throws InterruptedException exception
* @throws InvocationTargetException exception
*/
public static Supplier<TestResult> showUI(String testName, String headerText, URL instructions)
public static Supplier<TestResult> showUI(String testName,
String headerText,
Consumer<JEditorPane> instructions)
throws InterruptedException, InvocationTargetException {
AtomicReference<TestResult> resultContainer = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
Expand All @@ -148,12 +159,46 @@ public static Supplier<TestResult> showUI(String testName, String headerText, UR
});
return () -> {
try {
latch.await();
int timeout = Integer.getInteger("timeout", 10);
System.out.println("timeout value : " + timeout);
if (!latch.await(timeout, TimeUnit.MINUTES)) {
throw new RuntimeException("Timeout : User failed to " +
"take decision on the test result.");
}
} catch (InterruptedException e) {
return new TestResult(e);
}
return resultContainer.get();
};
}

/**
* Checks the TestResult after user interacted with the manual TestFrame
* and the test UI.
*
* @param result Instance of the TestResult
* @param testName name of the testcase
* @throws IOException exception
*/
public static void handleResult(TestResult result, String testName) throws IOException {
if (result != null) {
System.err.println("Failure reason: \n" + result.getFailureDescription());
if (result.getScreenCapture() != null) {
File screenDump = new File(System.getProperty("test.classes") + separator + testName + ".png");
System.err.println("Saving screen image to " + screenDump.getAbsolutePath());
ImageIO.write(result.getScreenCapture(), "png", screenDump);
}
Throwable e = result.getException();
if (e != null) {
throw new RuntimeException(e);
} else {
if (!result.getStatus())
throw new RuntimeException("Test failed!");
}
} else {
throw new RuntimeException("No result returned!");
}
}

}

1 comment on commit 6a7c023

@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.