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