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