1 /*
   2  * Copyright (c) 2007, 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 4452373 6567564
  27   @summary Tests that all the child and toplevel components have
  28 their GraphicsConfiguration updated when the window moves from
  29 one screen to another, on Windows or Linux/Solaris + Xinerama
  30   @author artem.ananiev: area=awt.multiscreen
  31   @library ../../regtesthelpers
  32   @build Util
  33   @run main UpdateGCTest
  34 */
  35 
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 
  39 import test.java.awt.regtesthelpers.Util;
  40 
  41 public class UpdateGCTest
  42 {
  43     public static void main(String[] args)
  44     {
  45         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  46         GraphicsDevice[] gds = ge.getScreenDevices();
  47         if (gds.length < 2)
  48         {
  49             System.out.println("The test should be run in multi-screen configuration. Test PASSED/skipped");
  50             return;
  51         }
  52         boolean virtualConfig = false;
  53         for (GraphicsDevice gd : gds)
  54         {
  55             GraphicsConfiguration gc = gd.getDefaultConfiguration();
  56             if ((gc.getBounds().x != 0) || (gc.getBounds().y != 0))
  57             {
  58                 virtualConfig = true;
  59                 break;
  60             }
  61         }
  62         if (!virtualConfig)
  63         {
  64             System.out.println("The test should be run in virtual multi-screen mode. Test PASSED/skipped");
  65             return;
  66         }
  67 
  68         try
  69         {
  70             Robot robot = new Robot();
  71             Util.waitForIdle(robot);
  72 
  73             for (GraphicsDevice gdOrig : gds)
  74             {
  75                 GraphicsConfiguration gcOrig = gdOrig.getDefaultConfiguration();
  76 
  77                 // test Frame
  78                 Frame f = new Frame("F", gcOrig);
  79                 f.setSize(200, 200);
  80                 f.setLayout(new BorderLayout());
  81                 // test Canvas
  82                 f.add(new Canvas(gcOrig), BorderLayout.NORTH);
  83                 // test lightweight
  84                 Container c = new Container() {};
  85                 c.setLayout(new BorderLayout());
  86                 // test hw inside lw
  87                 c.add(new Panel());
  88                 c.add(new Canvas(gcOrig));
  89                 f.add(c, BorderLayout.SOUTH);
  90                 // test Panel
  91                 Panel p = new Panel();
  92                 p.setLayout(new BorderLayout());
  93                 // test nested Canvas
  94                 p.add(new Canvas(gcOrig), BorderLayout.NORTH);
  95                 // test nested lightweight
  96                 p.add(new Component() {}, BorderLayout.SOUTH);
  97                 // test nested panel
  98                 p.add(new Panel(), BorderLayout.CENTER);
  99                 f.add(p, BorderLayout.CENTER);
 100 
 101                 f.setVisible(true);
 102                 Util.waitForIdle(robot);
 103 
 104                 for (GraphicsDevice gd : gds)
 105                 {
 106                     GraphicsConfiguration gc = gd.getDefaultConfiguration();
 107 
 108                     f.setLocation(gc.getBounds().x + 100, gc.getBounds().y + 100);
 109                     Util.waitForIdle(robot);
 110 
 111                     checkGC(f, gc);
 112                 }
 113             }
 114         }
 115         catch (Exception z)
 116         {
 117             System.err.println("Unknown exception caught");
 118             z.printStackTrace(System.err);
 119             throw new RuntimeException("Test FAILED: " + z.getMessage());
 120         }
 121 
 122         System.out.println("Test PASSED");
 123     }
 124 
 125     private static void checkGC(Component c, GraphicsConfiguration gc)
 126     {
 127         if (c.getGraphicsConfiguration() != gc)
 128         {
 129             System.err.println("GC for component (" + c + ") is not updated");
 130             System.err.println("Right GC: " + gc);
 131             System.err.println("Component GC: " + c.getGraphicsConfiguration());
 132             throw new RuntimeException("Test FAILED: component GC is not updated");
 133         }
 134         System.out.println("Checked GC for component (" + c + "): OK");
 135 
 136         if (c instanceof Container)
 137         {
 138             Container cc = (Container)c;
 139             Component[] children = cc.getComponents();
 140             for (Component child : children)
 141             {
 142                 checkGC(child, gc);
 143             }
 144         }
 145     }
 146 }