Path: blob/master/test/jdk/javax/swing/JTable/7124218/SelectEditTableCell.java
41153 views
/*1* Copyright (c) 2015, 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/*24* @test25* @key headful26* @bug 712421827* @summary verifies different behaviour of SPACE and ENTER in JTable28* @library ../../regtesthelpers29* @build Util30* @run main SelectEditTableCell31*/32import java.awt.Point;33import java.awt.Robot;34import java.awt.event.InputEvent;35import java.awt.event.KeyEvent;36import javax.swing.DefaultListSelectionModel;37import javax.swing.JFrame;38import javax.swing.JTable;39import javax.swing.LookAndFeel;40import javax.swing.SwingUtilities;41import javax.swing.UIManager;42import javax.swing.UnsupportedLookAndFeelException;4344public class SelectEditTableCell {4546private static JFrame frame;47private static JTable table;48private static Robot robot;4950public static void main(String[] args) throws Exception {51robot = new Robot();52robot.setAutoDelay(100);53UIManager.LookAndFeelInfo[] lookAndFeelArray54= UIManager.getInstalledLookAndFeels();55for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {56executeCase(lookAndFeelItem.getClassName());57}58}5960private static void executeCase(String lookAndFeelString) throws Exception {61try {62if (tryLookAndFeel(lookAndFeelString)) {63createUI(lookAndFeelString);64robot.delay(2000);65runTestCase();66robot.delay(2000);67cleanUp();68robot.delay(2000);69}70} finally {71if (frame != null) {72SwingUtilities.invokeAndWait(frame::dispose);73}74}7576}7778private static void createUI(final String lookAndFeelString)79throws Exception {80SwingUtilities.invokeAndWait(new Runnable() {81@Override82public void run() {83String[][] data = {{"Foo"}};84String[] cols = {"One"};85table = new JTable(data, cols);86table.setSelectionMode(87DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);88frame = new JFrame(lookAndFeelString);89frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);90frame.getContentPane().add(table);91frame.pack();92frame.setSize(500, frame.getSize().height);93frame.setLocationRelativeTo(null);94frame.setVisible(true);95frame.toFront();96}97});98}99100private static void runTestCase() throws Exception {101Point centerPoint;102centerPoint = Util.getCenterPoint(table);103LookAndFeel lookAndFeel = UIManager.getLookAndFeel();104robot.mouseMove(centerPoint.x, centerPoint.y);105robot.mousePress(InputEvent.BUTTON1_MASK);106robot.mouseRelease(InputEvent.BUTTON1_MASK);107robot.waitForIdle();108SwingUtilities.invokeAndWait(new Runnable() {109@Override110public void run() {111table.clearSelection();112if (table.isEditing() || table.isCellSelected(0, 0)) {113// assumption is bad, bail114frame.dispose();115throw new AssertionError("Failed assumption: assumed no"116+ "editing and no selection.");117}118}119});120121int fetchKeyCode;122keyTap(fetchKeyCode = isMac(lookAndFeel)123? KeyEvent.VK_ENTER : KeyEvent.VK_SPACE);124final int keyCode = fetchKeyCode;125robot.waitForIdle();126SwingUtilities.invokeAndWait(new Runnable() {127@Override128public void run() {129if (!table.isCellSelected(0, 0)) {130frame.dispose();131throw new RuntimeException(((keyCode == KeyEvent.VK_ENTER)132? "Enter" : "Space")133+ " should select cell");134}135}136});137138keyTap(KeyEvent.VK_SPACE);139robot.waitForIdle();140SwingUtilities.invokeAndWait(new Runnable() {141@Override142public void run() {143if (!table.isEditing()) {144frame.dispose();145throw new RuntimeException("Space should start editing");146}147table.getCellEditor().cancelCellEditing();148table.clearSelection();149if (table.isEditing() || table.isCellSelected(0, 0)) {150// assumption is bad, bail151frame.dispose();152throw new AssertionError("Failed assumption: assumed no "153+ "editing and no selection.");154}155}156});157158// hitting a letter key will start editing159keyTap(KeyEvent.VK_A);160robot.waitForIdle();161keyTap(KeyEvent.VK_SPACE);162robot.waitForIdle();163keyTap(KeyEvent.VK_A);164robot.waitForIdle();165SwingUtilities.invokeAndWait(new Runnable() {166@Override167public void run() {168if (table.isCellSelected(0, 0)) {169frame.dispose();170throw new RuntimeException("Space should not select when "171+ "already editing.");172}173}174});175}176177private static void cleanUp() throws Exception {178SwingUtilities.invokeAndWait(new Runnable() {179@Override180public void run() {181frame.dispose();182}183});184}185186private static boolean isMac(LookAndFeel lookAndFeel) {187188return lookAndFeel.toString().toLowerCase().contains("mac");189}190191private static void keyTap(int keyCode) {192robot.keyPress(keyCode);193robot.keyRelease(keyCode);194}195196private static boolean tryLookAndFeel(String lookAndFeelString)197throws Exception {198try {199UIManager.setLookAndFeel(200lookAndFeelString);201202} catch (UnsupportedLookAndFeelException203| ClassNotFoundException204| InstantiationException205| IllegalAccessException e) {206return false;207}208return true;209}210}211212213