1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.Rectangle;
  25 import java.awt.event.InputEvent;
  26 import java.io.BufferedReader;
  27 import java.io.File;
  28 import java.io.InputStreamReader;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 
  32 import javax.swing.JFrame;
  33 import javax.swing.JTable;
  34 import javax.swing.SwingUtilities;
  35 import javax.swing.UIManager;
  36 import javax.swing.UnsupportedLookAndFeelException;
  37 
  38 import static javax.swing.UIManager.getInstalledLookAndFeels;
  39 
  40 /**
  41  * @test
  42  * @bug 8139050 8153871
  43  * @key headful
  44  * @library ../../../../lib/client
  45  * @build ExtendedRobot
  46  * @run main/othervm/timeout=360 -Xcheck:jni NativeErrorsInTableDnD
  47  */
  48 
  49 public final class NativeErrorsInTableDnD {
  50 
  51     private static JFrame frame;
  52 
  53     private static volatile Rectangle bounds;
  54 
  55     public static void main(final String[] args) throws Exception {
  56         if (args.length == 0) {
  57             createChildProcess();
  58             return;
  59         }
  60         for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {
  61             SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
  62 
  63             SwingUtilities.invokeAndWait(() -> {
  64                 final JTable table = new JTable(10, 10);
  65                 frame = new JFrame();
  66                 frame.setUndecorated(true);
  67                 table.setDragEnabled(true);
  68                 table.selectAll();
  69                 frame.add(table);
  70                 frame.pack();
  71                 frame.setLocationRelativeTo(null);
  72                 frame.setVisible(true);
  73             });
  74             final ExtendedRobot r = new ExtendedRobot();
  75             r.waitForIdle();
  76             SwingUtilities.invokeAndWait(() -> {
  77                 bounds = frame.getBounds();
  78             });
  79             for (int i = 0; i < 5; ++i) {
  80                 int x1 = bounds.x + bounds.width / 4;
  81                 int y1 = bounds.y + bounds.height / 4;
  82                 r.setAutoDelay(50);
  83                 // Special sequence of clicks which reproduce the problem
  84                 r.mouseMove(bounds.x + bounds.width / 7, y1);
  85                 r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  86                 r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  87                 r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  88                 r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  89                 r.mouseMove(x1, y1);
  90                 r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  91                 r.setAutoDelay(0);
  92                 r.glide(x1, y1, x1 + bounds.width / 4, y1 + bounds.height / 4);
  93                 r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  94             }
  95             SwingUtilities.invokeAndWait(() -> {
  96                 frame.dispose();
  97             });
  98             r.waitForIdle();
  99         }
 100     }
 101 
 102     static void createChildProcess() throws Exception {
 103         final String javaPath = System.getProperty("java.home");
 104         final String classPathDir = System.getProperty("java.class.path");
 105         doExec(javaPath + File.separator + "bin" + File.separator + "java",
 106                "-cp", classPathDir, "NativeErrorsInTableDnD", "start");
 107     }
 108 
 109     static void doExec(final String... cmds) throws Exception {
 110         Process p;
 111         final ProcessBuilder pb = new ProcessBuilder(cmds);
 112         for (final String cmd : cmds) {
 113             System.out.print(cmd + " ");
 114         }
 115         System.out.println();
 116         BufferedReader rdr;
 117         final List<String> errorList = new ArrayList<>();
 118         final List<String> outputList = new ArrayList<>();
 119         p = pb.start();
 120         rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
 121         String in = rdr.readLine();
 122         while (in != null) {
 123             outputList.add(in);
 124             in = rdr.readLine();
 125             System.out.println(in);
 126         }
 127         rdr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
 128         in = rdr.readLine();
 129         while (in != null) {
 130             errorList.add(in);
 131             in = rdr.readLine();
 132             System.err.println(in);
 133         }
 134         p.waitFor();
 135         p.destroy();
 136 
 137         if (!errorList.isEmpty()) {
 138             throw new RuntimeException("Error log is not empty");
 139         }
 140         final int exit = p.exitValue();
 141         if (exit != 0) {
 142             throw new RuntimeException("Exit status = " + exit);
 143         }
 144     }
 145 
 146     private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
 147         try {
 148             UIManager.setLookAndFeel(laf.getClassName());
 149             System.out.println("LookAndFeel: " + laf.getClassName());
 150         } catch (ClassNotFoundException | InstantiationException |
 151                 UnsupportedLookAndFeelException | IllegalAccessException e) {
 152             throw new RuntimeException(e);
 153         }
 154     }
 155 }