1 /* 2 * Copyright (c) 2011, 2013, 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 @summary Simple Dock icon test as well as calling other eawt.Application methods 27 @summary com.apple.junit.apple.eawt; 28 @library ../../../java/awt/regtesthelpers 29 @build VisibilityValidator 30 @run junit DockIconTest 31 */ 32 33 import java.awt.*; 34 import java.awt.image.BufferedImage; 35 import javax.swing.*; 36 import junit.framework.*; 37 import com.apple.eawt.Application; 38 import test.java.awt.regtesthelpers.VisibilityValidator; 39 40 public class DockIconTest extends TestCase { 41 42 private Application app; 43 private JFrame f; 44 private PopupMenu menu; 45 private Image defaultImg; 46 47 public void setUp() throws Exception { 48 49 app = Application.getApplication(); 50 51 f = new JFrame("testing"); 52 53 f.getContentPane().add(new JLabel(new ImageIcon(app.getDockIconImage()))); 54 f.setSize(520, 520); 55 f.setVisible(true); 56 57 menu = new PopupMenu("Dock Menu"); 58 menu.add(new MenuItem("Alpha")); 59 menu.add(new MenuItem("Beta")); 60 menu.addSeparator(); 61 menu.add(new MenuItem("Gamma")); 62 menu.add(new MenuItem("Delta")); 63 f.add(menu); 64 65 app.setDockMenu(menu); 66 67 // this requires a bundled help bundle 68 // something to think about for later 69 //app.openHelpViewer(); 70 } 71 72 public void testDockIcon() throws Exception { 73 Thread.sleep(3000); 74 75 defaultImg = app.getDockIconImage(); 76 Image toolkitImg = Toolkit.getDefaultToolkit().getImage("NSImage://NSComputer"); 77 78 assertNotNull("Toolkit.getDefaultToolkit().getImage(\"NSImage://NSComputer\") returned null", toolkitImg); 79 80 // assert default != toolkit 81 assertTrue("Default icon image and toolkit image are the same???", !imagesAreIdentical(defaultImg, toolkitImg)); 82 83 app.setDockIconImage(toolkitImg); 84 Image dockiconImg = app.getDockIconImage(); 85 86 // assert dockicon == toolkit 87 assertTrue("Dock icon image and toolkit image are not the same!", imagesAreIdentical(dockiconImg, toolkitImg)); 88 89 // assert dockicon != default 90 assertTrue("Dock icon image and default icon image are the same???", !imagesAreIdentical(dockiconImg, defaultImg)); 91 92 // we can set the dock icon badge, but there is no 93 // easy way to verify it graphically 94 app.setDockIconBadge("42"); 95 Thread.sleep(3000); 96 } 97 98 protected void tearDown() { 99 app.setDockIconBadge(""); 100 app.setDockIconImage(defaultImg); 101 f.dispose(); 102 } 103 104 public static Test suite() { 105 return new TestSuite(DockIconTest.class); 106 } 107 108 public static void main (String[] args) throws RuntimeException { 109 TestResult tr = junit.textui.TestRunner.run(suite()); 110 if((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 111 throw new RuntimeException("### Unexpected junit errors or failures."); 112 } 113 } 114 115 private BufferedImage getBufferedImageFromImage(Image img) { 116 // convert Image to BufferedImage 117 BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); 118 Graphics2D graphics = bimg.createGraphics(); 119 graphics.drawImage(img, 0, 0, null); 120 return bimg; 121 } 122 123 private boolean imagesAreIdentical(Image a, Image b) { 124 BufferedImage imgA = getBufferedImageFromImage(a); 125 BufferedImage imgB = getBufferedImageFromImage(b); 126 127 // first check dimensions 128 if (imgA.getHeight() != imgB.getHeight()) { 129 return false; 130 } 131 if (imgA.getWidth() != imgB.getWidth()) { 132 return false; 133 } 134 // then check contents 135 for (int y = 0; y < imgA.getHeight(); y++) { 136 for (int x = 0; x < imgA.getWidth(); x++) { 137 int rgbA = imgA.getRGB(x, y); 138 int rgbB = imgB.getRGB(x, y); 139 if (!VisibilityValidator.colorMatch(new Color(rgbA), new Color(rgbB))) { 140 return false; 141 } 142 } 143 } 144 return true; 145 } 146 147 }