1 /*
   2  * Copyright (c) 2010, 2014, 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 javax.swing.*;
  25 import java.awt.*;
  26 
  27 /*
  28  * @test
  29  * @summary Check if a per-pixel translucent window shows only the area having
  30  *          opaque pixels
  31  * Test Description: Check if PERPIXEL_TRANSLUCENT Translucency type is supported
  32  *      on the current platform. Proceed if it is supported. Create a swing window
  33  *      with some swing components in it and a transparent background (alpha 0.0).
  34  *      Bring this window on top of a known background. Do this test for JFrame,
  35  *      JWindow and JDialog
  36  * Expected Result: Only the components present in the window must be shown. Other
  37  *      areas of the window must be transparent so that the background shows
  38  * @author mrkam
  39  * @library ../../../../lib/testlibrary
  40  * @build Common ExtendedRobot
  41  * @run main PerPixelTranslucentSwing
  42  */
  43 
  44 public class PerPixelTranslucentSwing extends Common {
  45 
  46     JButton north;
  47 
  48     public static void main(String[] ignored) throws Exception {
  49         FG_COLOR = new Color(200, 0, 0, 0);
  50         if (checkTranslucencyMode(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT))
  51             for (Class<Window> windowClass: WINDOWS_TO_TEST)
  52                 new PerPixelTranslucentSwing(windowClass).doTest();
  53     }
  54 
  55     public PerPixelTranslucentSwing(Class windowClass) throws Exception {
  56         super(windowClass);
  57     }
  58 
  59     @Override
  60     public void createSwingComponents() {
  61         Container contentPane = RootPaneContainer.class.cast(window).getContentPane();
  62         BorderLayout bl = new BorderLayout(10, 5);
  63         contentPane.setLayout(bl);
  64 
  65         north = new JButton("North");
  66         contentPane.add(north, BorderLayout.NORTH);
  67 
  68         JList center = new JList(new String[] {"Center"});
  69         contentPane.add(center, BorderLayout.CENTER);
  70 
  71         JTextField south = new JTextField("South");
  72         contentPane.add(south, BorderLayout.SOUTH);
  73 
  74         window.pack();
  75         window.setVisible(true);
  76 
  77         north.requestFocus();
  78     }
  79 
  80     @Override
  81     public void doTest() throws Exception {
  82         robot.waitForIdle(delay);
  83 
  84         // Check for background translucency
  85         Rectangle bounds = north.getBounds();
  86         Point loc = north.getLocationOnScreen();
  87 
  88         Color color = robot.getPixelColor(loc.x + bounds.width / 2, loc.y + bounds.height + 3);
  89         System.out.println(color);
  90         if (FG_COLOR.getRGB() == color.getRGB())
  91             throw new RuntimeException("Background is not translucent (" + color + ")");
  92 
  93         EventQueue.invokeAndWait(this::dispose);
  94     }
  95 }