1 /*
   2  * Copyright (c) 2007, 2015, 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 /*
  25  * @test
  26  * @bug 6356322
  27  * @key headful
  28  * @summary Tests that embedded frame's graphics configuration is updated
  29  *          correctly when it is moved to another screen in multiscreen system,
  30  *          XToolkit
  31  * @author artem.ananiev@sun.com: area=awt.multiscreen
  32  * @requires (os.family == "linux") | (os.family == "solaris")
  33  * @modules java.desktop/sun.awt
  34  *          java.desktop/sun.awt.X11
  35  *          java.desktop/java.awt.peer
  36  * @run main GraphicsConfigTest
  37  */
  38 
  39 import java.awt.*;
  40 import java.awt.peer.*;
  41 import java.lang.reflect.*;
  42 import java.util.*;
  43 import sun.awt.*;
  44 
  45 public class GraphicsConfigTest {
  46 
  47     private static void init()
  48         throws InterruptedException, AWTException {
  49         if (!isXToolkit()) {
  50             System.err.println("The test should be run only on XToolkit");
  51             return;
  52         }
  53 
  54         GraphicsEnvironment ge =
  55                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  56         GraphicsDevice[] gds = ge.getScreenDevices();
  57         if (gds.length < 2) {
  58             System.err.println("The test should be run only in"
  59                 + " multiscreen configuration");
  60             return;
  61         }
  62 
  63         boolean xinerama = Arrays.stream(gds)
  64             .map((gd) -> gd.getDefaultConfiguration().getBounds())
  65             .filter((r) -> r.x != 0 || r.y != 0).findFirst().isPresent();
  66 
  67         if (!xinerama) {
  68             System.err.println("The test should be run only with Xinerama ON");
  69             return;
  70         }
  71 
  72         Rectangle r0 = gds[0].getDefaultConfiguration().getBounds();
  73         Rectangle r1 = gds[1].getDefaultConfiguration().getBounds();
  74 
  75         System.setProperty("sun.awt.xembedserver", "true");
  76         Frame f = new Frame("F");
  77         try {
  78             final Robot robot = new Robot();
  79 
  80             f.setBounds(r0.x + 100, r0.y + 100, 200, 200);
  81             f.setVisible(true);
  82             robot.waitForIdle();
  83             Thread.sleep(1000);
  84 
  85             Canvas c = new Canvas();
  86             f.add(c);
  87             AWTAccessor.ComponentAccessor acc =
  88                         AWTAccessor.getComponentAccessor();
  89             WindowIDProvider wip = acc.getPeer(c);
  90             long h = wip.getWindow();
  91 
  92             EmbeddedFrame e = createEmbeddedFrame(h);
  93             acc.<FramePeer>getPeer(e).setBoundsPrivate(0, 0, 100,
  94                 100); // triggers XConfigureEvent
  95             e.registerListeners();
  96             e.setVisible(true);
  97             robot.waitForIdle();
  98             Thread.sleep(1000);
  99 
 100             if (!checkGC(f, e)) {
 101                 throw new RuntimeException("Failed at checkpoint 1");
 102             }
 103 
 104             f.setLocation(r1.x + 100, r1.y + 100);
 105             Thread.sleep(100);
 106             acc.<FramePeer>getPeer(e).setBoundsPrivate(0, 0, 101,
 107                 101); // triggers XConfigureEvent
 108             robot.waitForIdle();
 109             Thread.sleep(1000);
 110 
 111             if (!checkGC(f, e)) {
 112                 throw new RuntimeException("Failed at checkpoint 2");
 113             }
 114 
 115             f.setLocation(r0.x + 100, r0.y + 100);
 116             Thread.sleep(100);
 117             acc.<FramePeer>getPeer(e).setBoundsPrivate(0, 0, 102,
 118                 102); // triggers XConfigureEvent
 119             robot.waitForIdle();
 120             Thread.sleep(1000);
 121 
 122             if (!checkGC(f, e)) {
 123                 throw new RuntimeException("Failed at checkpoint 3");
 124             }
 125 
 126         } finally {
 127             f.dispose();
 128         }
 129     }
 130 
 131     private static boolean isXToolkit() {
 132         return Toolkit.getDefaultToolkit().getClass()
 133                         .getName().equals("sun.awt.X11.XToolkit");
 134     }
 135 
 136     private static EmbeddedFrame createEmbeddedFrame(long window) {
 137         try {
 138             Class cl = Class.forName("sun.awt.X11.XEmbeddedFrame");
 139             Constructor cons = cl.getConstructor(
 140                 new Class[]{Long.TYPE, Boolean.TYPE});
 141             return (EmbeddedFrame) cons.newInstance(new Object[]{window, true});
 142         } catch (Exception e) {
 143             e.printStackTrace();
 144             throw new RuntimeException("Can't create embedded frame");
 145         }
 146     }
 147 
 148     private static boolean checkGC(Component c, Component d) {
 149         GraphicsConfiguration g1 = c.getGraphicsConfiguration();
 150         System.err.println(g1);
 151         GraphicsConfiguration g2 = d.getGraphicsConfiguration();
 152         System.err.println(g2);
 153 
 154         return g1.equals(g2);
 155     }
 156 
 157     public static void main(String args[]) throws InterruptedException, AWTException {
 158         init();
 159     }
 160 }