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