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
  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         try {
  43             file = File.createTempFile("TestFile","txt");
  44         } catch (IOException ex) {
  45             throw new RuntimeException("Test failed. Exception thrown: ", ex);
  46         }
  47 
  48         // In case any UI that may pop up while deleting the file would
  49         // block this thread until the user actions them. Hence do file
  50         // check in a different thread and we assume it takes about sometime
  51         // till it deletes the file(or popup) on the main thread.
  52         new Thread(null, MoveToTrashTest::checkFileExistence, "FileCheck", 0, false).start();
  53         fileStatus = Desktop.getDesktop().moveToTrash(file);
  54     }

  55 
  56     private static void checkFileExistence() {
  57         Robot robot = null;
  58         try {
  59             robot = new Robot();
  60         } catch (AWTException ex) {
  61             throw new RuntimeException("Test failed. Exception thrown: ", ex);
  62         }
  63 
  64         robot.delay(1500);
  65 
  66         if(!fileStatus) {
  67             throw new RuntimeException("Test failed due to error while deleting the file");
  68         } else {
  69             if(file.exists()) {
  70                 throw new RuntimeException("Test failed");
  71             } else {
  72                 System.out.println("Test passed");
  73             }
  74         }
  75     }
  76 }
  77 
--- EOF ---