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 javax.swing.*;
  25 import java.awt.*;
  26 import java.io.ByteArrayInputStream;
  27 import java.io.ByteArrayOutputStream;
  28 import java.io.ObjectInputStream;
  29 import java.io.ObjectOutputStream;
  30 
  31 /*
  32  * @test
  33  * @key headful
  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         Robot robot = new Robot();
  58         robot.waitForIdle();
  59 
  60         if (!topFrame.isAlwaysOnTop() || !dialog.isAlwaysOnTop() || !subDialog.isAlwaysOnTop()) {
  61             throw new RuntimeException("TEST FAILED: AlwaysOnTop was not set properly");
  62         }
  63 
  64         //Serialize
  65         byte[] serializedObject;
  66         try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  67              ObjectOutputStream outputStream = new ObjectOutputStream(bytes)) {
  68             outputStream.writeObject(topFrame);
  69             outputStream.flush();
  70             serializedObject = bytes.toByteArray();
  71         }
  72 
  73         if (serializedObject == null) {
  74             throw new RuntimeException("FAILED: Serialized byte array in null");
  75         }
  76 
  77         //Deserialize
  78         Frame deserializedFrame;
  79         try (ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(serializedObject))) {
  80             deserializedFrame = (Frame) inputStream.readObject();
  81         }
  82 
  83         //Check the state of the deserialized objects
  84         if (!TOP_FRAME_LABEL.equals(deserializedFrame.getTitle())) {
  85             throw new RuntimeException("FAILED: Top frame deserialized incorrectly");
  86         }
  87 
  88         if (!deserializedFrame.isAlwaysOnTop()) {
  89             throw new RuntimeException("FAILED: Top frame alwaysOnTop not set after deserialization");
  90         }
  91 
  92         Window[] topOwnedWindows = topFrame.getOwnedWindows();
  93         if (topOwnedWindows.length != 1 || !DIALOG_LABEL.equals(((Dialog) topOwnedWindows[0]).getTitle())) {
  94             throw new RuntimeException("FAILED: Dialog deserialized incorrectly");
  95         }
  96 
  97         if (!topOwnedWindows[0].isAlwaysOnTop()) {
  98             throw new RuntimeException("FAILED: Dialog alwaysOnTop not set after deserialization");
  99         }
 100 
 101         Window dialog = topOwnedWindows[0];
 102         Window[] dialogOwnedWindows = dialog.getOwnedWindows();
 103         if (dialogOwnedWindows.length != 1 || !SUBDIALOG_LABEL.equals(((Dialog) dialogOwnedWindows[0]).getTitle())) {
 104             throw new RuntimeException("FAILED: Subdialog deserialized incorrectly");
 105         }
 106 
 107         if (!dialogOwnedWindows[0].isAlwaysOnTop()) {
 108             throw new RuntimeException("FAILED: Subdialog alwaysOnTop not set after deserialization");
 109         }
 110     }
 111 }