Path: blob/master/test/jdk/javax/swing/JInternalFrame/8146321/JInternalFrameIconTest.java
41153 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 8146321 815128227* @summary verifies JInternalFrame Icon and ImageIcon28* @library ../../regtesthelpers29* @build Util30* @run main JInternalFrameIconTest31*/32import java.io.File;33import java.awt.BorderLayout;34import java.awt.Component;35import java.awt.Graphics;36import java.awt.Point;37import java.awt.Rectangle;38import java.awt.Robot;39import java.awt.image.BufferedImage;40import javax.imageio.ImageIO;41import javax.swing.Icon;42import javax.swing.ImageIcon;43import javax.swing.JDesktopPane;44import javax.swing.JFrame;45import javax.swing.JInternalFrame;46import javax.swing.SwingUtilities;47import javax.swing.UIManager;48import javax.swing.UnsupportedLookAndFeelException;4950public class JInternalFrameIconTest {5152private static JFrame frame;53private static JDesktopPane desktopPane;54private static JInternalFrame internalFrame;55private static ImageIcon titleImageIcon;56private static Icon titleIcon;57private static BufferedImage imageIconImage;58private static BufferedImage iconImage;59private static Robot robot;60private static volatile String errorString = "";616263public static void main(String[] args) throws Exception {64robot = new Robot();65UIManager.LookAndFeelInfo[] lookAndFeelArray66= UIManager.getInstalledLookAndFeels();67for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {68executeCase(lookAndFeelItem.getClassName());69}70if (!"".equals(errorString)) {71throw new RuntimeException("Error Log:\n" + errorString);72}7374}7576private static void executeCase(String lookAndFeelString) throws Exception {77if (tryLookAndFeel(lookAndFeelString)) {78createImageIconUI(lookAndFeelString);79robot.waitForIdle();80robot.delay(1000);81getImageIconBufferedImage();82robot.waitForIdle();83robot.delay(1000);84cleanUp();85robot.waitForIdle();86robot.delay(1000);8788createIconUI(lookAndFeelString);89robot.waitForIdle();90robot.delay(1000);91getIconBufferedImage();92robot.waitForIdle();93robot.delay(1000);94cleanUp();95robot.waitForIdle();96robot.delay(1000);9798testIfSame(lookAndFeelString);99robot.waitForIdle();100robot.delay(1000);101}102103}104105private static void createImageIconUI(final String lookAndFeelString)106throws Exception {107SwingUtilities.invokeAndWait(new Runnable() {108@Override109public void run() {110desktopPane = new JDesktopPane();111internalFrame = new JInternalFrame();112frame = new JFrame();113internalFrame.setTitle(lookAndFeelString);114titleImageIcon = new ImageIcon() {115@Override116public int getIconWidth() {117return 16;118}119120@Override121public int getIconHeight() {122return 16;123}124125@Override126public void paintIcon(127Component c, Graphics g, int x, int y) {128g.setColor(java.awt.Color.black);129g.fillRect(x, y, 16, 16);130}131};132internalFrame.setFrameIcon(titleImageIcon);133internalFrame.setSize(500, 200);134internalFrame.setVisible(true);135desktopPane.add(internalFrame);136137frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);138frame.getContentPane().setLayout(new BorderLayout());139frame.getContentPane().add(desktopPane, "Center");140frame.setSize(500, 500);141frame.setLocationRelativeTo(null);142frame.setVisible(true);143frame.toFront();144}145});146}147148private static void createIconUI(final String lookAndFeelString)149throws Exception {150SwingUtilities.invokeAndWait(new Runnable() {151@Override152public void run() {153desktopPane = new JDesktopPane();154internalFrame = new JInternalFrame();155frame = new JFrame();156internalFrame.setTitle(lookAndFeelString);157titleIcon = new Icon() {158@Override159public int getIconWidth() {160return 16;161}162163@Override164public int getIconHeight() {165return 16;166}167168@Override169public void paintIcon(170Component c, Graphics g, int x, int y) {171g.setColor(java.awt.Color.black);172g.fillRect(x, y, 16, 16);173}174};175internalFrame.setFrameIcon(titleIcon);176internalFrame.setSize(500, 200);177internalFrame.setVisible(true);178desktopPane.add(internalFrame);179180frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);181frame.getContentPane().setLayout(new BorderLayout());182frame.getContentPane().add(desktopPane, "Center");183frame.setSize(500, 500);184frame.setLocationRelativeTo(null);185frame.setVisible(true);186frame.toFront();187}188});189}190191private static void getImageIconBufferedImage() throws Exception {192Point point = internalFrame.getLocationOnScreen();193Rectangle rect = internalFrame.getBounds();194Rectangle captureRect = new Rectangle(195point.x + internalFrame.getInsets().left,196point.y + internalFrame.getInsets().top,197titleImageIcon.getIconWidth(),198titleImageIcon.getIconHeight());199200System.out.println("imageicon captureRect " + captureRect);201imageIconImage202= robot.createScreenCapture(captureRect);203}204205private static void getIconBufferedImage() throws Exception {206Point point = internalFrame.getLocationOnScreen();207Rectangle rect = internalFrame.getBounds();208Rectangle captureRect = new Rectangle(209point.x + internalFrame.getInsets().left,210point.y + internalFrame.getInsets().top,211titleIcon.getIconWidth(),212titleIcon.getIconHeight());213214System.out.println("icon captureRect " + captureRect);215iconImage216= robot.createScreenCapture(captureRect);217}218219private static void testIfSame(final String lookAndFeelString)220throws Exception {221if (!bufferedImagesEqual(imageIconImage, iconImage)) {222ImageIO.write(imageIconImage, "png", new File("imageicon-fail.png"));223ImageIO.write(iconImage, "png", new File("iconImage-fail.png"));224String error ="[" + lookAndFeelString225+ "] : ERROR: icon and imageIcon not same.";226errorString += error;227System.err.println(error);228} else {229System.out.println("[" + lookAndFeelString230+ "] : SUCCESS: icon and imageIcon same.");231}232}233234private static boolean bufferedImagesEqual(235BufferedImage bufferedImage1, BufferedImage bufferedImage2) {236boolean flag = true;237238if (bufferedImage1.getWidth() == bufferedImage2.getWidth()239&& bufferedImage1.getHeight() == bufferedImage2.getHeight()) {240final int colorTolerance = 25;241final int mismatchTolerance = (int) (0.1242* bufferedImage1.getWidth() * bufferedImage1.getHeight());243int mismatchCounter = 0;244for (int x = 0; x < bufferedImage1.getWidth(); x++) {245for (int y = 0; y < bufferedImage1.getHeight(); y++) {246247int color1 = bufferedImage1.getRGB(x, y);248int red1 = (color1 >> 16) & 0x000000FF;249int green1 = (color1 >> 8) & 0x000000FF;250int blue1 = (color1) & 0x000000FF;251252int color2 = bufferedImage2.getRGB(x, y);253int red2 = (color2 >> 16) & 0x000000FF;254int green2 = (color2 >> 8) & 0x000000FF;255int blue2 = (color2) & 0x000000FF;256if (red1 != red2 || green1 != green2 || blue1 != blue2) {257++mismatchCounter;258if ((Math.abs(red1 - red2) > colorTolerance)259|| (Math.abs(green1 - green2) > colorTolerance)260|| (Math.abs(blue1 - blue2) > colorTolerance)) {261262flag = false;263}264}265}266}267if (mismatchCounter > mismatchTolerance) {268flag = false;269}270} else {271System.err.println("ERROR: size is different");272flag = false;273}274return flag;275}276277private static void cleanUp() throws Exception {278SwingUtilities.invokeAndWait(new Runnable() {279@Override280public void run() {281frame.dispose();282}283});284}285286private static boolean tryLookAndFeel(String lookAndFeelString)287throws Exception {288//This test case is not applicable for Motif and gtk LAFs289if(lookAndFeelString.contains("motif")290|| lookAndFeelString.contains("gtk")) {291return false;292}293try {294UIManager.setLookAndFeel(295lookAndFeelString);296297} catch (UnsupportedLookAndFeelException298| ClassNotFoundException299| InstantiationException300| IllegalAccessException e) {301return false;302}303return true;304}305}306307308