Path: blob/master/test/jdk/tools/jpackage/windows/WinInstallerIconTest.java
41152 views
/*1* Copyright (c) 2021, 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*/2223import java.awt.Graphics;24import java.awt.image.BufferedImage;25import java.io.IOException;26import java.nio.file.Path;27import java.util.function.Consumer;28import javax.swing.Icon;29import javax.swing.filechooser.FileSystemView;30import jdk.jpackage.test.PackageTest;31import jdk.jpackage.test.Annotations.Test;32import jdk.jpackage.test.JPackageCommand;33import jdk.jpackage.test.PackageType;34import static jdk.jpackage.test.RunnablePackageTest.Action.CREATE;35import jdk.jpackage.test.TKit;3637/**38* Test that --icon also changes icon of exe installer.39*/4041/*42* @test43* @summary jpackage with --icon parameter for exe installer44* @library ../helpers45* @key jpackagePlatformPackage46* @build jdk.jpackage.test.*47* @build WinInstallerIconTest48* @requires (os.family == "windows")49* @requires !vm.debug50* @modules jdk.jpackage/jdk.jpackage.internal51* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main52* --jpt-run=WinInstallerIconTest53*/5455/*56* note: AWT can throw assertion from GetDiBits() extracting icon57* bits in fastdebug mode on windows headless systems. That is why58* we have @requires !vm.debug" above.59*/60public class WinInstallerIconTest {6162@Test63public void test() throws IOException {64Path customIcon = iconPath("icon");6566BufferedImage[] defaultInstallerIconImg = new BufferedImage[1];6768// Create installer with the default icon69createInstaller(null, "WithDefaultIcon", installerIconImg -> {70defaultInstallerIconImg[0] = installerIconImg;71}, null, null);7273BufferedImage[] customInstallerIconImg = new BufferedImage[1];7475// Create installer with custom icon.76// This installer icon should differ from the icon77// of the installer created with the default icon.78createInstaller(customIcon, "2", installerIconImg -> {79customInstallerIconImg[0] = installerIconImg;80}, null, defaultInstallerIconImg[0]);8182// Create installer with custom icon again.83// This installer icon should differ from the icon84// of the installer created with the default icon and should have85// the same icon as the icon of installer created with custom icon.86createInstaller(customIcon, null, null,87customInstallerIconImg[0], defaultInstallerIconImg[0]);88}8990private void createInstaller(Path icon, String nameSuffix,91Consumer<BufferedImage> installerIconImgConsumer,92BufferedImage expectedInstallerIconImg,93BufferedImage unexpectedInstallerIconImg) throws IOException {9495PackageTest test = new PackageTest()96.forTypes(PackageType.WIN_EXE)97.addInitializer(JPackageCommand::setFakeRuntime)98.configureHelloApp();99if (icon != null) {100test.addInitializer(cmd -> cmd.addArguments("--icon", icon));101}102103if (nameSuffix != null) {104test.addInitializer(cmd -> {105String name = cmd.name() + nameSuffix;106cmd.setArgumentValue("--name", name);107});108}109110Path installerExePath[] = new Path[1];111112test.addBundleVerifier(cmd -> {113installerExePath[0] = cmd.outputBundle();114115Icon actualIcon = FileSystemView.getFileSystemView().getSystemIcon(116installerExePath[0].toFile());117118BufferedImage actualInstallerIconImg = loadIcon(actualIcon);119120if (installerIconImgConsumer != null) {121installerIconImgConsumer.accept(actualInstallerIconImg);122}123124if (expectedInstallerIconImg != null) {125TKit.assertTrue(imageEquals(expectedInstallerIconImg,126actualInstallerIconImg), String.format(127"Check icon of %s installer is matching expected value",128installerExePath[0]));129}130131if (unexpectedInstallerIconImg != null) {132TKit.assertFalse(imageEquals(unexpectedInstallerIconImg,133actualInstallerIconImg), String.format(134"Check icon of %s installer is NOT matching unexpected value",135installerExePath[0]));136}137});138139test.run(CREATE);140141if (installerExePath[0] != null && nameSuffix != null) {142TKit.deleteIfExists(installerExePath[0]);143}144}145146private BufferedImage loadIcon(Icon icon) {147TKit.assertNotEquals(0, icon.getIconWidth(),148"Check icon has not empty width");149TKit.assertNotEquals(0, icon.getIconHeight(),150"Check icon has not empty height");151BufferedImage img = new BufferedImage(152icon.getIconWidth(),153icon.getIconHeight(),154BufferedImage.TYPE_INT_RGB);155Graphics g = img.createGraphics();156icon.paintIcon(null, g, 0, 0);157g.dispose();158return img;159}160161private static boolean imageEquals(BufferedImage imgA, BufferedImage imgB) {162if (imgA.getWidth() == imgB.getWidth() && imgA.getHeight()163== imgB.getHeight()) {164for (int x = 0; x < imgA.getWidth(); x++) {165for (int y = 0; y < imgA.getHeight(); y++) {166if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) {167return false;168}169}170}171} else {172return false;173}174return true;175}176177private static Path iconPath(String name) {178return TKit.TEST_SRC_ROOT.resolve(Path.of("resources", name179+ TKit.ICON_SUFFIX));180}181}182183184