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