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