1 /*
   2  * Copyright (c) 2008, 2009, 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 %I% %E%
  26  * @bug 6655001 6670649 6687141
  27  * @summary Tests that hw acceleration doesn't affect translucent/shaped windows
  28  * @author Dmitri.Trembovetski@sun.com: area=Graphics
  29  * @compile -XDignore.symbol.file=true TranslucentShapedFrameTest.java
  30  * @compile -XDignore.symbol.file=true TSFrame.java
  31  * @run main/manual/othervm TranslucentShapedFrameTest
  32  * @run main/manual/othervm -Dsun.java2d.noddraw=true TranslucentShapedFrameTest
  33  * @run main/manual/othervm -Dsun.java2d.opengl=True TranslucentShapedFrameTest
  34  */
  35 import java.awt.Color;
  36 import java.awt.Frame;
  37 import java.awt.GraphicsConfiguration;
  38 import java.awt.GraphicsDevice;
  39 import java.awt.GraphicsDevice.WindowTranslucency;
  40 import java.awt.GraphicsEnvironment;
  41 import java.awt.Shape;
  42 import java.awt.geom.Ellipse2D;
  43 import java.util.concurrent.CountDownLatch;
  44 import javax.swing.JSlider;
  45 import javax.swing.SwingUtilities;
  46 import javax.swing.UIManager;
  47 import javax.swing.UnsupportedLookAndFeelException;
  48 
  49 public class TranslucentShapedFrameTest extends javax.swing.JFrame {
  50     Frame testFrame;
  51     static CountDownLatch done;
  52     static volatile boolean failed = false;
  53     GraphicsConfiguration gcToUse = null;
  54 
  55     /**
  56      * Creates new form TranslucentShapedFrameTest
  57      */
  58     public TranslucentShapedFrameTest() {
  59         // not necessary, but we just look nicer
  60         try {
  61             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  62         } catch (Exception ex) {}
  63 
  64         initComponents();
  65         checkEffects();
  66 
  67         SwingUtilities.updateComponentTreeUI(this);
  68     }
  69 
  70     /** This method is called from within the constructor to
  71      * initialize the form.
  72      * WARNING: Do NOT modify this code. The content of this method is
  73      * always regenerated by the Form Editor.
  74      */
  75     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  76     private void initComponents() {
  77         createDisposeGrp = new javax.swing.ButtonGroup();
  78         jLabel1 = new javax.swing.JLabel();
  79         transparencySld = new javax.swing.JSlider();
  80         shapedCb = new javax.swing.JCheckBox();
  81         nonOpaqueChb = new javax.swing.JCheckBox();
  82         jScrollPane1 = new javax.swing.JScrollPane();
  83         jTextArea1 = new javax.swing.JTextArea();
  84         jLabel2 = new javax.swing.JLabel();
  85         passedBtn = new javax.swing.JButton();
  86         failedBtn = new javax.swing.JButton();
  87         createFrameBtn = new javax.swing.JToggleButton();
  88         disposeFrameBtn = new javax.swing.JToggleButton();
  89         useSwingCb = new javax.swing.JCheckBox();
  90 
  91         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  92         setTitle("TranslucentShapedFrameTest");
  93         jLabel1.setText("Frame Opacity:");
  94 
  95         transparencySld.setMajorTickSpacing(10);
  96         transparencySld.setMinorTickSpacing(5);
  97         transparencySld.setPaintLabels(true);
  98         transparencySld.setPaintTicks(true);
  99         transparencySld.setValue(100);
 100         transparencySld.addChangeListener(new javax.swing.event.ChangeListener() {
 101             public void stateChanged(javax.swing.event.ChangeEvent evt) {
 102                 transparencySldStateChanged(evt);
 103             }
 104         });
 105 
 106         shapedCb.setText("Shaped Frame");
 107         shapedCb.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
 108         shapedCb.setMargin(new java.awt.Insets(0, 0, 0, 0));
 109         shapedCb.addActionListener(new java.awt.event.ActionListener() {
 110             public void actionPerformed(java.awt.event.ActionEvent evt) {
 111                 shapedCbActionPerformed(evt);
 112             }
 113         });
 114 
 115         nonOpaqueChb.setText("Non Opaque Frame");
 116         nonOpaqueChb.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
 117         nonOpaqueChb.setMargin(new java.awt.Insets(0, 0, 0, 0));
 118         nonOpaqueChb.addActionListener(new java.awt.event.ActionListener() {
 119             public void actionPerformed(java.awt.event.ActionEvent evt) {
 120                 nonOpaqueChbActionPerformed(evt);
 121             }
 122         });
 123 
 124         jTextArea1.setColumns(20);
 125         jTextArea1.setRows(5);
 126         jTextArea1.setText("Create translucent and/or shaped, or\nnon-opaque frame. Make sure it behaves\ncorrectly (no artifacts left on the screen\nwhen dragging - if dragging is possible).\nClick \"Passed\" if the test behaves correctly,\n\"Falied\" otherwise.");
 127         jScrollPane1.setViewportView(jTextArea1);
 128 
 129         jLabel2.setText("Instructions:");
 130 
 131         passedBtn.setBackground(new Color(129, 255, 100));
 132         passedBtn.setText("Passed");
 133         passedBtn.addActionListener(new java.awt.event.ActionListener() {
 134             public void actionPerformed(java.awt.event.ActionEvent evt) {
 135                 passedBtnActionPerformed(evt);
 136             }
 137         });
 138 
 139         failedBtn.setBackground(Color.red);
 140         failedBtn.setText("Failed");
 141         failedBtn.addActionListener(new java.awt.event.ActionListener() {
 142             public void actionPerformed(java.awt.event.ActionEvent evt) {
 143                 failedBtnActionPerformed(evt);
 144             }
 145         });
 146 
 147         createDisposeGrp.add(createFrameBtn);
 148         createFrameBtn.setText("Create Frame");
 149         createFrameBtn.addActionListener(new java.awt.event.ActionListener() {
 150             public void actionPerformed(java.awt.event.ActionEvent evt) {
 151                 createFrameBtnActionPerformed(evt);
 152             }
 153         });
 154 
 155         createDisposeGrp.add(disposeFrameBtn);
 156         disposeFrameBtn.setSelected(true);
 157         disposeFrameBtn.setText("Dispose Frame");
 158         disposeFrameBtn.addActionListener(new java.awt.event.ActionListener() {
 159             public void actionPerformed(java.awt.event.ActionEvent evt) {
 160                 disposeFrameBtnActionPerformed(evt);
 161             }
 162         });
 163 
 164         useSwingCb.setText("Use JFrame");
 165         useSwingCb.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
 166         useSwingCb.setMargin(new java.awt.Insets(0, 0, 0, 0));
 167 
 168         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 169         getContentPane().setLayout(layout);
 170         layout.setHorizontalGroup(
 171             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 172             .addGroup(layout.createSequentialGroup()
 173                 .addContainerGap()
 174                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 175                     .addGroup(layout.createSequentialGroup()
 176                         .addComponent(transparencySld, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
 177                         .addContainerGap())
 178                     .addComponent(jLabel1)
 179                     .addGroup(layout.createSequentialGroup()
 180                         .addComponent(shapedCb)
 181                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 182                         .addComponent(nonOpaqueChb)
 183                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 184                         .addComponent(useSwingCb)
 185                         .addContainerGap(102, Short.MAX_VALUE))
 186                     .addGroup(layout.createSequentialGroup()
 187                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 188                             .addGroup(layout.createSequentialGroup()
 189                                 .addComponent(jLabel2)
 190                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 314, javax.swing.GroupLayout.PREFERRED_SIZE))
 191                             .addGroup(layout.createSequentialGroup()
 192                                 .addComponent(passedBtn)
 193                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 194                                 .addComponent(failedBtn)
 195                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
 196                             .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
 197                             .addGroup(layout.createSequentialGroup()
 198                                 .addComponent(createFrameBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
 199                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 200                                 .addComponent(disposeFrameBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)))
 201                         .addContainerGap())))
 202         );
 203         layout.setVerticalGroup(
 204             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 205             .addGroup(layout.createSequentialGroup()
 206                 .addContainerGap()
 207                 .addComponent(jLabel1)
 208                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 209                 .addComponent(transparencySld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 210                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 211                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 212                     .addComponent(shapedCb)
 213                     .addComponent(nonOpaqueChb)
 214                     .addComponent(useSwingCb))
 215                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 216                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 217                     .addComponent(disposeFrameBtn)
 218                     .addComponent(createFrameBtn))
 219                 .addGap(17, 17, 17)
 220                 .addComponent(jLabel2)
 221                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 222                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
 223                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 224                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 225                     .addComponent(passedBtn)
 226                     .addComponent(failedBtn))
 227                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 228         );
 229         pack();
 230     }// </editor-fold>//GEN-END:initComponents
 231 
 232     private void nonOpaqueChbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nonOpaqueChbActionPerformed
 233         if (testFrame != null) {
 234             // REMIND: this path in the test doesn't work well (test bug)
 235             testFrame.setBackground(new Color(0, 0, 0, nonOpaqueChb.isSelected() ? 0 : 255));
 236         }
 237     }//GEN-LAST:event_nonOpaqueChbActionPerformed
 238 
 239     private void shapedCbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shapedCbActionPerformed
 240         if (testFrame != null) {
 241             Shape s = null;
 242             if (shapedCb.isSelected()) {
 243                 s = new Ellipse2D.Double(0, 0,
 244                                          testFrame.getWidth(),
 245                                          testFrame.getHeight());
 246             }
 247             testFrame.setShape(s);
 248         }
 249     }//GEN-LAST:event_shapedCbActionPerformed
 250 
 251     private void transparencySldStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_transparencySldStateChanged
 252         JSlider source = (JSlider)evt.getSource();
 253             int transl = transparencySld.getValue();
 254             if (testFrame != null) {
 255                 testFrame.setOpacity((float)transl/100f);
 256             }
 257     }//GEN-LAST:event_transparencySldStateChanged
 258 
 259     private void failedBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_failedBtnActionPerformed
 260         disposeFrameBtnActionPerformed(evt);
 261         dispose();
 262         failed = true;
 263         done.countDown();
 264     }//GEN-LAST:event_failedBtnActionPerformed
 265 
 266     private void disposeFrameBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disposeFrameBtnActionPerformed
 267         TSFrame.stopThreads();
 268         if (testFrame != null) {
 269             testFrame.dispose();
 270             testFrame = null;
 271         }
 272     }//GEN-LAST:event_disposeFrameBtnActionPerformed
 273 
 274     private void createFrameBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createFrameBtnActionPerformed
 275         disposeFrameBtnActionPerformed(evt);
 276         int transl = transparencySld.getValue();
 277         testFrame = TSFrame.createGui(
 278                 useSwingCb.isSelected(), shapedCb.isSelected(),
 279                 (transl < 100), nonOpaqueChb.isSelected(),
 280                 (float)transl/100f);
 281     }//GEN-LAST:event_createFrameBtnActionPerformed
 282 
 283     private void passedBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passedBtnActionPerformed
 284         disposeFrameBtnActionPerformed(evt);
 285         dispose();
 286         done.countDown();
 287     }//GEN-LAST:event_passedBtnActionPerformed
 288 
 289     /**
 290      * @param args the command line arguments
 291      */
 292     public static void main(String args[]) {
 293         done = new CountDownLatch(1);
 294         java.awt.EventQueue.invokeLater(new Runnable() {
 295             public void run() {
 296                 new TranslucentShapedFrameTest().setVisible(true);
 297             }
 298         });
 299         try {
 300             done.await();
 301         } catch (InterruptedException ex) {}
 302         if (failed) {
 303             throw new RuntimeException("Test FAILED");
 304         }
 305         System.out.println("Test PASSED");
 306     }
 307 
 308     private void checkEffects() {
 309         GraphicsDevice gd = getGraphicsConfiguration().getDevice();
 310         if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) {
 311             shapedCb.setEnabled(false);
 312         }
 313         if (!gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) {
 314             transparencySld.setEnabled(false);
 315         }
 316         if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
 317             nonOpaqueChb.setEnabled(false);
 318         }
 319     }
 320 
 321     // Variables declaration - do not modify//GEN-BEGIN:variables
 322     private javax.swing.ButtonGroup createDisposeGrp;
 323     private javax.swing.JToggleButton createFrameBtn;
 324     private javax.swing.JToggleButton disposeFrameBtn;
 325     private javax.swing.JButton failedBtn;
 326     private javax.swing.JLabel jLabel1;
 327     private javax.swing.JLabel jLabel2;
 328     private javax.swing.JScrollPane jScrollPane1;
 329     private javax.swing.JTextArea jTextArea1;
 330     private javax.swing.JCheckBox nonOpaqueChb;
 331     private javax.swing.JButton passedBtn;
 332     private javax.swing.JCheckBox shapedCb;
 333     private javax.swing.JSlider transparencySld;
 334     private javax.swing.JCheckBox useSwingCb;
 335     // End of variables declaration//GEN-END:variables
 336 
 337 }