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
  43  * @library ../../../../lib/testlibrary
  44  * @build ExtendedRobot
  45  * @run main/othervm/timeout=360 -Xcheck:jni NativeErrorsInTableDnD
  46  */
  47 public final class NativeErrorsInTableDnD {
  48 
  49     private static JFrame frame;
  50 
  51     private static volatile Rectangle bounds;
  52 
  53     public static void main(final String[] args) throws Exception {
  54         if (args.length == 0) {
  55             createChildProcess();
  56             return;
  57         }
  58         for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {
  59             SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
  60 
  61             SwingUtilities.invokeAndWait(() -> {
  62                 final JTable table = new JTable(10, 10);
  63                 frame = new JFrame();
  64                 frame.setUndecorated(true);
  65                 table.setDragEnabled(true);
  66                 table.selectAll();
  67                 frame.add(table);
  68                 frame.pack();
  69                 frame.setLocationRelativeTo(null);
  70                 frame.setVisible(true);
  71             });
  72             final ExtendedRobot r = new ExtendedRobot();
  73             r.waitForIdle();
  74             SwingUtilities.invokeAndWait(() -> {
  75                 bounds = frame.getBounds();
  76             });
  77             for (int i = 0; i < 5; ++i) {
  78                 int x1 = bounds.x + bounds.width / 4;
  79                 int y1 = bounds.y + bounds.height / 4;
  80                 r.setAutoDelay(50);
  81                 // Special sequence of clicks which reproduce the problem
  82                 r.mouseMove(bounds.x + bounds.width / 7, y1);
  83                 r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  84                 r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  85                 r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  86                 r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  87                 r.mouseMove(x1, y1);
  88                 r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  89                 r.setAutoDelay(0);
  90                 r.glide(x1, y1, x1 + bounds.width / 4, y1 + bounds.height / 4);
  91                 r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  92             }
  93             SwingUtilities.invokeAndWait(() -> {
  94                 frame.dispose();
  95             });
  96             r.waitForIdle();
  97         }
  98     }
  99 
 100     static void createChildProcess() throws Exception {
 101         final String javaPath = System.getProperty("java.home");
 102         final String classPathDir = System.getProperty("java.class.path");
 103         doExec(javaPath + File.separator + "bin" + File.separator + "java",
 104                "-cp", classPathDir, "NativeErrorsInTableDnD", "start");
 105     }
 106 
 107     static void doExec(final String... cmds) throws Exception {
 108         Process p;
 109         final ProcessBuilder pb = new ProcessBuilder(cmds);
 110         for (final String cmd : cmds) {
 111             System.out.print(cmd + " ");
 112         }
 113         System.out.println();
 114         BufferedReader rdr;
 115         final List<String> errorList = new ArrayList<>();
 116         final List<String> outputList = new ArrayList<>();
 117         p = pb.start();
 118         rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
 119         String in = rdr.readLine();
 120         while (in != null) {
 121             outputList.add(in);
 122             in = rdr.readLine();
 123             System.out.println(in);
 124         }
 125         rdr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
 126         in = rdr.readLine();
 127         while (in != null) {
 128             errorList.add(in);
 129             in = rdr.readLine();
 130             System.err.println(in);
 131         }
 132         p.waitFor();
 133         p.destroy();
 134 
 135         if (!errorList.isEmpty()) {
 136             throw new RuntimeException("Error log is not empty");
 137         }
 138         final int exit = p.exitValue();
 139         if (exit != 0) {
 140             throw new RuntimeException("Exit status = " + exit);
 141         }
 142     }
 143 
 144     private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
 145         try {
 146             UIManager.setLookAndFeel(laf.getClassName());
 147             System.out.println("LookAndFeel: " + laf.getClassName());
 148         } catch (ClassNotFoundException | InstantiationException |
 149                 UnsupportedLookAndFeelException | IllegalAccessException e) {
 150             throw new RuntimeException(e);
 151         }
 152     }
 153 }