1 /* 2 * Copyright (c) 2008, 2017, 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 6594131 8186617 28 @summary Tests the Window.get/setOpacity() methods 29 */ 30 31 import java.awt.AWTException; 32 import java.awt.Frame; 33 import java.awt.GraphicsDevice; 34 import java.awt.GraphicsEnvironment; 35 import java.awt.Robot; 36 37 public class WindowOpacity { 38 39 public static void main(String[] args) throws Exception { 40 GraphicsDevice gd = 41 GraphicsEnvironment.getLocalGraphicsEnvironment() 42 .getDefaultScreenDevice(); 43 if (!gd.isWindowTranslucencySupported( 44 GraphicsDevice.WindowTranslucency.TRANSLUCENT)) { 45 System.out.println( 46 "Either the Toolkit or the native system does not support" 47 + " controlling the window opacity level."); 48 return; 49 } 50 Frame f = new Frame("Opacity test"); 51 try { 52 test(f); 53 } finally { 54 f.dispose(); 55 } 56 } 57 58 private static void test(final Frame f) throws AWTException { 59 boolean passed; 60 61 f.setUndecorated(true); 62 float curOpacity = f.getOpacity(); 63 if (curOpacity < 1.0f || curOpacity > 1.0f) { 64 throw new RuntimeException( 65 "getOpacity() reports the initial opacity level " 66 + "other than 1.0: " + curOpacity); 67 } 68 69 70 passed = false; 71 try { 72 f.setOpacity(-0.5f); 73 } catch (IllegalArgumentException e) { 74 passed = true; 75 } 76 if (!passed) { 77 throw new RuntimeException( 78 "setOpacity() allows passing negative opacity level."); 79 } 80 81 82 passed = false; 83 try { 84 f.setOpacity(1.5f); 85 } catch (IllegalArgumentException e) { 86 passed = true; 87 } 88 if (!passed) { 89 throw new RuntimeException( 90 "setOpacity() allows passing opacity level greater than 1.0."); 91 } 92 93 94 f.setOpacity(0.5f); 95 curOpacity = f.getOpacity(); 96 if (curOpacity < 0.5f || curOpacity > 0.5f) { 97 throw new RuntimeException( 98 "setOpacity() reports the opacity level that " 99 + "differs from the value set with " 100 + "setWindowOpacity: " + curOpacity); 101 } 102 103 104 f.setOpacity(0.75f); 105 curOpacity = f.getOpacity(); 106 if (curOpacity < 0.75f || curOpacity > 0.75f) { 107 throw new RuntimeException( 108 "getOpacity() reports the opacity level that " 109 + "differs from the value set with " 110 + "setWindowOpacity the second time: " 111 + curOpacity); 112 } 113 114 115 f.setBounds(100, 100, 300, 200); 116 f.setVisible(true); 117 Robot robot = new Robot(); 118 robot.waitForIdle(); 119 120 curOpacity = f.getOpacity(); 121 if (curOpacity < 0.75f || curOpacity > 0.75f) { 122 throw new RuntimeException( 123 "getOpacity() reports the opacity level that " 124 + "differs from the value set with " 125 + "setWindowOpacity before showing the frame: " 126 + curOpacity); 127 } 128 f.setOpacity(0.5f); 129 robot.waitForIdle(); 130 curOpacity = f.getOpacity(); 131 if (curOpacity < 0.5f || curOpacity > 0.5f) { 132 throw new RuntimeException( 133 "getOpacity() reports the opacity level that " 134 + "differs from the value set with " 135 + "setWindowOpacity after showing the frame: " 136 + curOpacity); 137 } 138 } 139 }