1 /*
   2  * Copyright (c) 2016, 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  /*
  25  @test
  26  @bug 8156099
  27  @summary Drag and drop of link from web browser, DataFlavor types
  28     application/x-java-url and text/uri-list, getTransferData returns null
  29  @run main/manual DragLinkFromBrowser
  30  */
  31 
  32 import java.awt.Frame;
  33 import java.awt.Button;
  34 import java.awt.Panel;
  35 import java.awt.TextArea;
  36 import java.awt.Color;
  37 import java.awt.GridBagConstraints;
  38 import java.awt.GridBagLayout;
  39 import java.awt.event.ActionEvent;
  40 import java.awt.event.ActionListener;
  41 import java.awt.datatransfer.DataFlavor;
  42 import java.awt.datatransfer.Transferable;
  43 import java.awt.datatransfer.UnsupportedFlavorException;
  44 import java.io.IOException;
  45 import javax.swing.JFrame;
  46 import javax.swing.JPanel;
  47 import javax.swing.TransferHandler;
  48 import javax.swing.SwingUtilities;
  49 import javax.swing.JOptionPane;
  50 
  51 public class DragLinkFromBrowser implements ActionListener {
  52 
  53     private static GridBagLayout layout;
  54     private static Panel mainControlPanel;
  55     private static Panel resultButtonPanel;
  56     private static TextArea instructionTextArea;
  57     private static Button passButton;
  58     private static Button failButton;
  59     private static Frame mainFrame;
  60     private static Thread mainThread = null;
  61     private static volatile boolean testPassed = false;
  62     private static volatile boolean isInterrupted = false;
  63     private static volatile String failMessage;
  64     private static final int testTimeOut = 300000;
  65     private static JFrame urlFrame;
  66     private static JPanel urlPanel;
  67     private static boolean testFailStatus = false;
  68 
  69     public static void dragLinkFromWebBrowser() {
  70 
  71         urlFrame = new JFrame();
  72         urlPanel = new JPanel();
  73         urlFrame.getContentPane().add(urlPanel);
  74         urlPanel.setTransferHandler(new TransferHandler() {
  75             @Override
  76             public boolean canImport(final TransferSupport support) {
  77                 return true;
  78             }
  79 
  80             @Override
  81             public boolean importData(final TransferSupport support) {
  82                 final Transferable transferable = support.getTransferable();
  83                 final DataFlavor[] flavors
  84                         = transferable.getTransferDataFlavors();
  85                 
  86                 for (final DataFlavor flavor : flavors) {
  87                     try {
  88                         final Object transferData
  89                                 = transferable.getTransferData(flavor);
  90 
  91                         if (transferData == null) {
  92                             testFailStatus = true;
  93                         } else {
  94                             String flavorMessage = flavor.toString();
  95                             String transferDataMessage = transferData.toString();
  96                             if (flavorMessage.contains("error")
  97                                 || transferDataMessage.contains("null")) {
  98                                 testFailStatus = true;
  99                             }
 100                         }
 101                     } catch (UnsupportedFlavorException e) {
 102                         testFailed("UnsupportedFlavorException - test Failed");
 103                     } catch (IOException e) {
 104                         testFailed("IOException - test Failed");
 105                     }
 106                 }
 107 
 108                 if (testFailStatus) {
 109                     JOptionPane.showMessageDialog(urlPanel, "Dragging link from "
 110                         + "web browser Failed. getTransferData returns null");
 111                 } else {
 112                     JOptionPane.showMessageDialog(urlPanel, "Test Passed");
 113                 }
 114                 
 115                 return true;
 116             }
 117         });
 118         SwingUtilities.invokeLater(new Runnable() {
 119             @Override
 120             public void run() {
 121                 urlFrame.setBounds(500, 10, 200, 200);
 122                 urlFrame.setVisible(true);
 123             }
 124         });
 125     }
 126 
 127     private void createInstructionUI() {
 128         mainFrame = new Frame("Updating TrayIcon Popup Menu Item Test");
 129         layout = new GridBagLayout();
 130         mainControlPanel = new Panel(layout);
 131         resultButtonPanel = new Panel(layout);
 132 
 133         GridBagConstraints gbc = new GridBagConstraints();
 134         String instructions
 135                 = "INSTRUCTIONS:"
 136                 + "\n   1. Open any browser"
 137                 + "\n   2. Select and drag URL from the browser page and "
 138                 + "drop it on the panel"
 139                 + "\n   3. If test fails, then a popup message will be displayed."
 140                 + "\n   5. Otherwise test passed.";
 141 
 142         instructionTextArea = new TextArea();
 143         instructionTextArea.setText(instructions);
 144         instructionTextArea.setEnabled(false);
 145         instructionTextArea.setBackground(Color.white);
 146 
 147         gbc.gridx = 0;
 148         gbc.gridy = 0;
 149         gbc.fill = GridBagConstraints.HORIZONTAL;
 150         mainControlPanel.add(instructionTextArea, gbc);
 151 
 152         passButton = new Button("Pass");
 153         passButton.setName("Pass");
 154         passButton.addActionListener(this);
 155 
 156         failButton = new Button("Fail");
 157         failButton.setName("Fail");
 158         failButton.addActionListener(this);
 159 
 160         gbc.gridx = 0;
 161         gbc.gridy = 0;
 162         resultButtonPanel.add(passButton, gbc);
 163         gbc.gridx = 1;
 164         gbc.gridy = 0;
 165         resultButtonPanel.add(failButton, gbc);
 166         gbc.gridx = 0;
 167         gbc.gridy = 1;
 168         mainControlPanel.add(resultButtonPanel, gbc);
 169 
 170         mainFrame.add(mainControlPanel);
 171         mainFrame.pack();
 172         mainFrame.setVisible(true);
 173     }
 174 
 175     @Override
 176     public void actionPerformed(ActionEvent ae) {
 177         if (ae.getSource() instanceof Button) {
 178             Button btn = (Button) ae.getSource();
 179             switch (btn.getName()) {
 180                 case "Pass":
 181                     testPassed = true;
 182                     isInterrupted = true;
 183                     mainThread.interrupt();
 184                     break;
 185 
 186                 case "Fail":
 187                     testFailed("Dragging link from web browser Failed");
 188                     break;
 189             }
 190         }
 191     }
 192 
 193     public static void cleanUp() {
 194         urlFrame.dispose();
 195         mainFrame.dispose();
 196     }
 197 
 198     public static void testFailed(String message) {
 199         testPassed = false;
 200         isInterrupted = true;
 201         failMessage = message;
 202         mainThread.interrupt();
 203     }
 204 
 205     public static void main(final String[] args) throws Exception {
 206 
 207         DragLinkFromBrowser linkFromBrowser = new DragLinkFromBrowser();
 208         linkFromBrowser.createInstructionUI();
 209         linkFromBrowser.dragLinkFromWebBrowser();
 210 
 211         mainThread = Thread.currentThread();
 212         try {
 213             mainThread.sleep(testTimeOut);
 214         } catch (InterruptedException ex) {
 215             if (!testPassed) {
 216                 throw new RuntimeException(failMessage);
 217             }
 218         } finally {
 219             cleanUp();
 220         }
 221 
 222         if (!isInterrupted) {
 223             throw new RuntimeException("Test Timed out after "
 224                     + testTimeOut / 1000 + " seconds");
 225         }
 226     }
 227 }
 228