1 /*
2  * Copyright (c) 2005, 2014, 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 /*
27   test
28   @bug 6260648
29   @summary Tests that WINDOW_DESTROY event can be handled by overriding handleEvent(). Also,
30 tests that handleEvent() is not called by AWT if any listener is added to the component
31 (i. e. when post-1.1 events schema is used)
32   @author artem.ananiev: area=awt.event
33   @run applet HandleWindowDestroyTest.html
34 */
35 
36 import java.applet.*;
37 
38 import java.awt.*;
39 import java.awt.event.*;
40 
41 public class HandleWindowDestroyTest extends Applet
42 {
43     private volatile boolean handleEventCalled;
44 
start()45     public void start ()
46     {
47         setSize (200,200);
48         setVisible(true);
49         validate();
50 
51         Robot robot;
52         try {
53             robot = new Robot();
54         }catch(Exception ex) {
55             ex.printStackTrace();
56             throw new RuntimeException("Unexpected failure");
57         }
58 
59         Frame f = new Frame("Frame")
60         {
61             public boolean handleEvent(Event e)
62             {
63                 if (e.id == Event.WINDOW_DESTROY)
64                 {
65                     handleEventCalled = true;
66                 }
67                 return super.handleEvent(e);
68             }
69         };
70         f.setBounds(100, 100, 100, 100);
71         f.setVisible(true);
72         robot.waitForIdle();
73 
74         handleEventCalled = false;
75         Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new WindowEvent(f, Event.WINDOW_DESTROY));
76         robot.waitForIdle();
77 
78         if (!handleEventCalled)
79         {
80             throw new RuntimeException("Test FAILED: handleEvent() is not called");
81         }
82 
83         f.addWindowListener(new WindowAdapter()
84         {
85         });
86 
87         handleEventCalled = false;
88         Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new WindowEvent(f, Event.WINDOW_DESTROY));
89         robot.waitForIdle();
90 
91         if (handleEventCalled)
92         {
93             throw new RuntimeException("Test FAILED: handleEvent() is called event with a listener added");
94         }
95     }
96 }
97