1 /*
   2  * Copyright (c) 2018, 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 java.awt.EventQueue;
  25 import java.awt.GraphicsConfiguration;
  26 import java.util.Objects;
  27 import java.util.concurrent.CountDownLatch;
  28 import java.util.concurrent.TimeUnit;
  29 
  30 import javax.swing.JButton;
  31 import javax.swing.JFrame;
  32 
  33 /**
  34  * @test
  35  * @key headful
  36  * @bug 8201552
  37  * @summary Verifies if graphicsConfiguration property notification is sent
  38  *          when frame is shown on the screen.
  39  * @run main TestSingleScreenGConfigNotify
  40  * @run main/othervm -Dsun.java2d.uiScale=2.25 TestSingleScreenGConfigNotify
  41  */
  42 public final class TestSingleScreenGConfigNotify {
  43 
  44     private static String name = "graphicsConfiguration";
  45     private static CountDownLatch go = new CountDownLatch(1);
  46     private static JFrame frame;
  47     private static GraphicsConfiguration after;
  48     private static GraphicsConfiguration before;
  49     private static JButton button;
  50 
  51     public static void main(final String[] args) throws Exception {
  52         EventQueue.invokeAndWait(() -> {
  53             frame = new JFrame();
  54 
  55             frame.setSize(300,300);
  56             frame.setLocationRelativeTo(null);
  57             button = new JButton();
  58             button.addPropertyChangeListener(evt -> {
  59                 if (evt.getPropertyName().equals(name)) {
  60                     go.countDown();
  61                 }
  62             });
  63 
  64             before = button.getGraphicsConfiguration();
  65 
  66             frame.add(button);
  67             frame.setVisible(true);
  68         });
  69 
  70         boolean called = go.await(10, TimeUnit.SECONDS);
  71 
  72         EventQueue.invokeAndWait(() -> {
  73             after = button.getGraphicsConfiguration();
  74             frame.dispose();
  75         });
  76 
  77         if (Objects.equals(before, after) && called) {
  78             throw new RuntimeException("propertyChange() should not be called");
  79         }
  80         if (!Objects.equals(before, after) && !called) {
  81             throw new RuntimeException("propertyChange() should be called");
  82         }
  83     }
  84 }