package com.sun.javafx.tk.quantum;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;

import com.sun.javafx.PlatformUtil;

import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
 * @author alexandr_kirov
 */
public class RT17588Test extends Application {

    /**
     * @param args the command line arguments
     */
    @BeforeClass public static void prepare() {
        String[] args = null;
        
        /* 
         * This test hangs on Mac
         */
        Assume.assumeTrue(PlatformUtil.isWindows());

        launch(args);
    }
    
    private Stage stage, newStage;
    
    @Test public void testModalFileChooser() {
        try {
            stage.show();
            final Stage newStage = new Stage();
            newStage.initModality(Modality.NONE);
            newStage.initOwner(stage);
            newStage.show();
            
            final FileChooser fc = new FileChooser();        
            
            Thread t = new Thread(new Runnable() {
                public void run(){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(RT17588Test.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    Platform.runLater(new Runnable() {
                        public void run() {
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(RT17588Test.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            stage.hide();
                            newStage.hide();
                        }
                    });
                }
            });
            t.start();
            
            fc.showOpenDialog(newStage);
            
        } catch (Throwable th) {
            th.printStackTrace();
            fail(th.getMessage());
        }
    }
                 
    @Override
    public void start(final Stage st) throws Exception {
        stage = st;
        testModalFileChooser();
    }
    
}
