1 /* 2 * Copyright (c) 2013, 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 import sun.awt.SunToolkit; 25 26 import javax.swing.*; 27 import java.awt.*; 28 import java.io.ByteArrayInputStream; 29 import java.io.ByteArrayOutputStream; 30 import java.io.ObjectInputStream; 31 import java.io.ObjectOutputStream; 32 33 /* 34 * @test 35 * @key headful 36 * @bug 8027152 37 * @summary Checks that ownedWindowList is serialized and deserialized properly and alwaysOnTop works after deserialization 38 * @author Petr Pchelko 39 * @run main OwnedWindowsSerialization 40 */ 41 public class OwnedWindowsSerialization { 42 43 private static final String TOP_FRAME_LABEL = "Top Frame"; 44 private static final String DIALOG_LABEL = "Dialog"; 45 private static final String SUBDIALOG_LABEL = "Subdialog"; 46 47 private static volatile Frame topFrame; 48 private static volatile Dialog dialog; 49 private static volatile Dialog subDialog; 50 51 public static void main(String[] args) throws Exception { 52 SwingUtilities.invokeAndWait(() -> { 53 topFrame = new Frame(TOP_FRAME_LABEL); 54 topFrame.setAlwaysOnTop(true); 55 dialog = new Dialog(topFrame, DIALOG_LABEL); 56 subDialog = new Dialog(dialog, SUBDIALOG_LABEL); 57 }); 58 59 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 60 61 if (!topFrame.isAlwaysOnTop() || !dialog.isAlwaysOnTop() || !subDialog.isAlwaysOnTop()) { 62 throw new RuntimeException("TEST FAILED: AlwaysOnTop was not set properly"); 63 } 64 65 //Serialize 66 byte[] serializedObject; 67 try (ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 68 ObjectOutputStream outputStream = new ObjectOutputStream(bytes)) { 69 outputStream.writeObject(topFrame); 70 outputStream.flush(); 71 serializedObject = bytes.toByteArray(); 72 } 73 74 if (serializedObject == null) { 75 throw new RuntimeException("FAILED: Serialized byte array in null"); 76 } 77 78 //Deserialize 79 Frame deserializedFrame; 80 try (ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(serializedObject))) { 81 deserializedFrame = (Frame) inputStream.readObject(); 82 } 83 84 //Check the state of the deserialized objects 85 if (!TOP_FRAME_LABEL.equals(deserializedFrame.getTitle())) { 86 throw new RuntimeException("FAILED: Top frame deserialized incorrectly"); 87 } 88 89 if (!deserializedFrame.isAlwaysOnTop()) { 90 throw new RuntimeException("FAILED: Top frame alwaysOnTop not set after deserialization"); 91 } 92 93 Window[] topOwnedWindows = topFrame.getOwnedWindows(); 94 if (topOwnedWindows.length != 1 || !DIALOG_LABEL.equals(((Dialog) topOwnedWindows[0]).getTitle())) { 95 throw new RuntimeException("FAILED: Dialog deserialized incorrectly"); 96 } 97 98 if (!topOwnedWindows[0].isAlwaysOnTop()) { 99 throw new RuntimeException("FAILED: Dialog alwaysOnTop not set after deserialization"); 100 } 101 102 Window dialog = topOwnedWindows[0]; 103 Window[] dialogOwnedWindows = dialog.getOwnedWindows(); 104 if (dialogOwnedWindows.length != 1 || !SUBDIALOG_LABEL.equals(((Dialog) dialogOwnedWindows[0]).getTitle())) { 105 throw new RuntimeException("FAILED: Subdialog deserialized incorrectly"); 106 } 107 108 if (!dialogOwnedWindows[0].isAlwaysOnTop()) { 109 throw new RuntimeException("FAILED: Subdialog alwaysOnTop not set after deserialization"); 110 } 111 } 112 }