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 /*
  25   @test
  26   @bug 8190515 8193468
  27   @summary java.awt.Desktop.moveToTrash(File) prompts on Windows 7 but not on Mac.
  28   @run main MoveToTrashTest
  29 */
  30 
  31 import java.io.File;
  32 import java.awt.Desktop;
  33 import java.awt.Robot;
  34 import java.io.IOException;
  35 import java.awt.AWTException;
  36 
  37 public class MoveToTrashTest {
  38     private static File file = null;
  39     private static boolean fileStatus = false;
  40 
  41     public static void main(String[] args) {
  42         if(!Desktop.getDesktop().isSupported(Desktop.Action.MOVE_TO_TRASH)) {
  43             System.out.println("Move to trash action is not supported on the"+ 
  44                " platform under test."+" Marking the test passed");
  45         } else {
  46             try {
  47                 file = File.createTempFile("TestFile","txt");
  48             } catch (IOException ex) {
  49                 throw new RuntimeException("Test failed. Exception thrown: ", ex);
  50             }
  51 
  52             // In case any UI that may pop up while deleting the file would
  53             // block this thread until the user actions them. Hence do file
  54             // check in a different thread and we assume it takes about sometime
  55             // till it deletes the file(or popup) on the main thread.
  56             new Thread(null, MoveToTrashTest::checkFileExistence, "FileCheck", 0, false).start();
  57             fileStatus = Desktop.getDesktop().moveToTrash(file);
  58         }
  59     }
  60 
  61     private static void checkFileExistence() {
  62         Robot robot = null;
  63         try {
  64             robot = new Robot();
  65         } catch (AWTException ex) {
  66             throw new RuntimeException("Test failed. Exception thrown: ", ex);
  67         }
  68 
  69         robot.delay(1500);
  70 
  71         if(!fileStatus) {
  72             throw new RuntimeException("Test failed due to error while deleting the file");
  73         } else {
  74             if(file.exists()) {
  75                 throw new RuntimeException("Test failed");
  76             } else {
  77                 System.out.println("Test passed");
  78             }
  79         }
  80     }
  81 }
  82 
--- EOF ---