1 /*
   2  * Copyright (c) 2018, 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. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.netbeans.jemmy.operators;
  27 
  28 import static org.testng.Assert.fail;
  29 
  30 import javax.swing.JDesktopPane;
  31 import javax.swing.JFrame;
  32 import javax.swing.JInternalFrame;
  33 import javax.swing.UIManager;
  34 import javax.swing.event.InternalFrameEvent;
  35 import javax.swing.event.InternalFrameListener;
  36 
  37 import org.netbeans.jemmy.JemmyProperties;
  38 import org.netbeans.jemmy.LookAndFeelProvider;
  39 import org.netbeans.jemmy.TimeoutExpiredException;
  40 import org.testng.annotations.AfterMethod;
  41 import org.testng.annotations.BeforeMethod;
  42 import org.testng.annotations.Test;
  43 
  44 public class JInternalFrameOperatorCloseTest {
  45 
  46     private JFrameOperator frameOper;
  47 
  48     private JInternalFrameOperator internalFrameOper;
  49 
  50     @BeforeMethod
  51     private void setUp(Object[] args) throws Exception {
  52         UIManager.setLookAndFeel((String)args[0]);
  53         JFrame frame = new JFrame();
  54         JDesktopPane desktop = new JDesktopPane();
  55         frame.setContentPane(desktop);
  56         JemmyProperties.setCurrentDispatchingModel(
  57                 JemmyProperties.getCurrentDispatchingModel());
  58         JInternalFrame internalFrame = new JInternalFrame("JInternalFrameOperatorTest", true, true, true, true);
  59         internalFrame.setName("JInternalFrameOperatorTest");
  60         internalFrame.setSize(200, 200);
  61         internalFrame.setVisible(true);
  62         desktop.add(internalFrame);
  63         frame.setSize(400,400);
  64         frame.setLocationRelativeTo(null);
  65         frame.setVisible(true);
  66         frameOper = new JFrameOperator();
  67         internalFrameOper = new JInternalFrameOperator(frameOper);
  68         internalFrameOper.setVerification(true);
  69     }
  70 
  71     @AfterMethod
  72     protected void tearDown() throws Exception {
  73         frameOper.setVisible(false);
  74         frameOper.dispose();
  75     }
  76 
  77     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
  78     public void testClose(String lookAndFeel) throws Exception {
  79         InternalFrameListener listener = new InternalFrameListener() {
  80 
  81             @Override
  82             public void internalFrameOpened(InternalFrameEvent e) {
  83             }
  84 
  85             @Override
  86             public void internalFrameIconified(InternalFrameEvent e) {
  87             }
  88 
  89             @Override
  90             public void internalFrameDeiconified(InternalFrameEvent e) {
  91             }
  92 
  93             @Override
  94             public void internalFrameDeactivated(InternalFrameEvent e) {
  95             }
  96 
  97             @Override
  98             public void internalFrameClosing(InternalFrameEvent e) {
  99                 try {
 100                     this.wait(80000);
 101                 } catch (InterruptedException e1) {
 102                     e1.printStackTrace();
 103                 }
 104             }
 105 
 106             @Override
 107             public void internalFrameClosed(InternalFrameEvent e) {
 108             }
 109 
 110             @Override
 111             public void internalFrameActivated(InternalFrameEvent e) {
 112             }
 113         };
 114 
 115         // Making not to close the fame for 1 minute and expecting TimeoutExpiredException
 116         // from waitClosed()
 117         try {
 118             internalFrameOper.addInternalFrameListener(listener);
 119             internalFrameOper.close();
 120             fail();
 121         } catch (TimeoutExpiredException e) {
 122         } finally {
 123             internalFrameOper.removeInternalFrameListener(listener);
 124         }
 125 
 126         // Really closing the frame
 127         internalFrameOper.close();
 128     }
 129 
 130 }