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