Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jpackage/windows/WinInstallerIconTest.java
41152 views
1
/*
2
* Copyright (c) 2021, 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
import java.awt.Graphics;
25
import java.awt.image.BufferedImage;
26
import java.io.IOException;
27
import java.nio.file.Path;
28
import java.util.function.Consumer;
29
import javax.swing.Icon;
30
import javax.swing.filechooser.FileSystemView;
31
import jdk.jpackage.test.PackageTest;
32
import jdk.jpackage.test.Annotations.Test;
33
import jdk.jpackage.test.JPackageCommand;
34
import jdk.jpackage.test.PackageType;
35
import static jdk.jpackage.test.RunnablePackageTest.Action.CREATE;
36
import jdk.jpackage.test.TKit;
37
38
/**
39
* Test that --icon also changes icon of exe installer.
40
*/
41
42
/*
43
* @test
44
* @summary jpackage with --icon parameter for exe installer
45
* @library ../helpers
46
* @key jpackagePlatformPackage
47
* @build jdk.jpackage.test.*
48
* @build WinInstallerIconTest
49
* @requires (os.family == "windows")
50
* @requires !vm.debug
51
* @modules jdk.jpackage/jdk.jpackage.internal
52
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
53
* --jpt-run=WinInstallerIconTest
54
*/
55
56
/*
57
* note: AWT can throw assertion from GetDiBits() extracting icon
58
* bits in fastdebug mode on windows headless systems. That is why
59
* we have @requires !vm.debug" above.
60
*/
61
public class WinInstallerIconTest {
62
63
@Test
64
public void test() throws IOException {
65
Path customIcon = iconPath("icon");
66
67
BufferedImage[] defaultInstallerIconImg = new BufferedImage[1];
68
69
// Create installer with the default icon
70
createInstaller(null, "WithDefaultIcon", installerIconImg -> {
71
defaultInstallerIconImg[0] = installerIconImg;
72
}, null, null);
73
74
BufferedImage[] customInstallerIconImg = new BufferedImage[1];
75
76
// Create installer with custom icon.
77
// This installer icon should differ from the icon
78
// of the installer created with the default icon.
79
createInstaller(customIcon, "2", installerIconImg -> {
80
customInstallerIconImg[0] = installerIconImg;
81
}, null, defaultInstallerIconImg[0]);
82
83
// Create installer with custom icon again.
84
// This installer icon should differ from the icon
85
// of the installer created with the default icon and should have
86
// the same icon as the icon of installer created with custom icon.
87
createInstaller(customIcon, null, null,
88
customInstallerIconImg[0], defaultInstallerIconImg[0]);
89
}
90
91
private void createInstaller(Path icon, String nameSuffix,
92
Consumer<BufferedImage> installerIconImgConsumer,
93
BufferedImage expectedInstallerIconImg,
94
BufferedImage unexpectedInstallerIconImg) throws IOException {
95
96
PackageTest test = new PackageTest()
97
.forTypes(PackageType.WIN_EXE)
98
.addInitializer(JPackageCommand::setFakeRuntime)
99
.configureHelloApp();
100
if (icon != null) {
101
test.addInitializer(cmd -> cmd.addArguments("--icon", icon));
102
}
103
104
if (nameSuffix != null) {
105
test.addInitializer(cmd -> {
106
String name = cmd.name() + nameSuffix;
107
cmd.setArgumentValue("--name", name);
108
});
109
}
110
111
Path installerExePath[] = new Path[1];
112
113
test.addBundleVerifier(cmd -> {
114
installerExePath[0] = cmd.outputBundle();
115
116
Icon actualIcon = FileSystemView.getFileSystemView().getSystemIcon(
117
installerExePath[0].toFile());
118
119
BufferedImage actualInstallerIconImg = loadIcon(actualIcon);
120
121
if (installerIconImgConsumer != null) {
122
installerIconImgConsumer.accept(actualInstallerIconImg);
123
}
124
125
if (expectedInstallerIconImg != null) {
126
TKit.assertTrue(imageEquals(expectedInstallerIconImg,
127
actualInstallerIconImg), String.format(
128
"Check icon of %s installer is matching expected value",
129
installerExePath[0]));
130
}
131
132
if (unexpectedInstallerIconImg != null) {
133
TKit.assertFalse(imageEquals(unexpectedInstallerIconImg,
134
actualInstallerIconImg), String.format(
135
"Check icon of %s installer is NOT matching unexpected value",
136
installerExePath[0]));
137
}
138
});
139
140
test.run(CREATE);
141
142
if (installerExePath[0] != null && nameSuffix != null) {
143
TKit.deleteIfExists(installerExePath[0]);
144
}
145
}
146
147
private BufferedImage loadIcon(Icon icon) {
148
TKit.assertNotEquals(0, icon.getIconWidth(),
149
"Check icon has not empty width");
150
TKit.assertNotEquals(0, icon.getIconHeight(),
151
"Check icon has not empty height");
152
BufferedImage img = new BufferedImage(
153
icon.getIconWidth(),
154
icon.getIconHeight(),
155
BufferedImage.TYPE_INT_RGB);
156
Graphics g = img.createGraphics();
157
icon.paintIcon(null, g, 0, 0);
158
g.dispose();
159
return img;
160
}
161
162
private static boolean imageEquals(BufferedImage imgA, BufferedImage imgB) {
163
if (imgA.getWidth() == imgB.getWidth() && imgA.getHeight()
164
== imgB.getHeight()) {
165
for (int x = 0; x < imgA.getWidth(); x++) {
166
for (int y = 0; y < imgA.getHeight(); y++) {
167
if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) {
168
return false;
169
}
170
}
171
}
172
} else {
173
return false;
174
}
175
return true;
176
}
177
178
private static Path iconPath(String name) {
179
return TKit.TEST_SRC_ROOT.resolve(Path.of("resources", name
180
+ TKit.ICON_SUFFIX));
181
}
182
}
183
184