Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jpackage/share/IconTest.java
41152 views
1
/*
2
* Copyright (c) 2018, 2020, 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.io.IOException;
25
import java.util.stream.Stream;
26
import java.util.stream.Collectors;
27
import java.util.function.Consumer;
28
import java.nio.file.Files;
29
import java.nio.file.Path;
30
import java.nio.file.StandardCopyOption;
31
import java.util.ArrayList;
32
import java.util.Collection;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Optional;
36
import java.util.Set;
37
import jdk.jpackage.test.TKit;
38
import jdk.jpackage.test.JPackageCommand;
39
import jdk.jpackage.test.LauncherIconVerifier;
40
import jdk.jpackage.test.PackageTest;
41
import jdk.jpackage.test.Executor;
42
import jdk.jpackage.test.LinuxHelper;
43
import jdk.jpackage.test.AdditionalLauncher;
44
import jdk.jpackage.test.Functional.ThrowingConsumer;
45
import jdk.jpackage.test.Functional.ThrowingBiConsumer;
46
import jdk.jpackage.test.Annotations.Parameters;
47
import jdk.jpackage.test.Annotations.Test;
48
49
/*
50
* @test
51
* @summary jpackage create image and package with custom icons for the main and additional launcher
52
* @library ../helpers
53
* @build jdk.jpackage.test.*
54
* @modules jdk.jpackage/jdk.jpackage.internal
55
* @compile IconTest.java
56
* @run main/othervm/timeout=540 -Xmx512m jdk.jpackage.test.Main
57
* --jpt-run=IconTest
58
*/
59
60
public class IconTest {
61
62
enum IconType {
63
/**
64
* Icon not specified.
65
*/
66
DefaultIcon,
67
68
/**
69
* Explicit no icon.
70
*/
71
NoIcon,
72
73
/**
74
* Custom icon on command line.
75
*/
76
CustomIcon,
77
78
/**
79
* Custom icon in resource dir.
80
*/
81
ResourceDirIcon,
82
83
/**
84
* Custom icon on command line and in resource dir.
85
*/
86
CustomWithResourceDirIcon
87
}
88
89
enum BundleType { AppImage, Package }
90
91
public IconTest(BundleType bundleType, IconType mainLauncherIconType,
92
IconType additionalLauncherIconType, String[] extraJPackageArgs) {
93
this.appImage = (bundleType == BundleType.AppImage);
94
this.extraJPackageArgs = extraJPackageArgs;
95
config = Map.of(
96
Launcher.Main, mainLauncherIconType,
97
Launcher.Additional, additionalLauncherIconType);
98
}
99
100
public IconTest(BundleType bundleType, IconType mainLauncherIconType,
101
IconType additionalLauncherIconType) {
102
this.appImage = (bundleType == BundleType.AppImage);
103
this.extraJPackageArgs = new String[0];
104
config = Map.of(
105
Launcher.Main, mainLauncherIconType,
106
Launcher.Additional, additionalLauncherIconType);
107
}
108
109
public IconTest(BundleType bundleType, IconType mainLauncherIconType) {
110
this.appImage = (bundleType == BundleType.AppImage);
111
this.extraJPackageArgs = new String[0];
112
config = Map.of(Launcher.Main, mainLauncherIconType);
113
}
114
115
@Parameters
116
public static Collection data() {
117
List<Object[]> data = new ArrayList<>();
118
119
var withLinuxShortcut = Set.of(IconType.DefaultIcon, IconType.NoIcon);
120
121
for (var bundleType : BundleType.values()) {
122
if (TKit.isWindows() && bundleType == BundleType.Package) {
123
// On Windows icons are embedded in launcher executables in
124
// application image. Nothing is changed when app image is
125
// packed in msi/exe package bundle, so skip testing of package
126
// bundle.
127
continue;
128
}
129
for (var mainLauncherIconType : IconType.values()) {
130
if (mainLauncherIconType == IconType.NoIcon) {
131
// `No icon` setting is not applicable for the main launcher.
132
continue;
133
}
134
135
if (TKit.isOSX()) {
136
// Custom icons not supported for additional launchers on Mac.
137
data.add(new Object[]{bundleType, mainLauncherIconType});
138
continue;
139
}
140
141
for (var additionalLauncherIconType : IconType.values()) {
142
data.add(new Object[]{bundleType, mainLauncherIconType,
143
additionalLauncherIconType});
144
145
if (TKit.isLinux() && bundleType == BundleType.Package
146
&& withLinuxShortcut.contains(mainLauncherIconType)
147
&& withLinuxShortcut.contains(
148
additionalLauncherIconType)) {
149
data.add(new Object[]{bundleType, mainLauncherIconType,
150
additionalLauncherIconType, new String[]{
151
"--linux-shortcut"}});
152
}
153
}
154
}
155
}
156
return data;
157
}
158
159
@Test
160
public void test() throws IOException {
161
if (appImage) {
162
JPackageCommand cmd = initAppImageTest();
163
var result = cmd.executeAndAssertImageCreated();
164
ThrowingConsumer.toConsumer(createInstallVerifier()).accept(cmd);
165
ThrowingBiConsumer.toBiConsumer(createBundleVerifier()).accept(cmd, result);
166
} else {
167
PackageTest test = initPackageTest();
168
test.addInstallVerifier(createInstallVerifier());
169
test.addBundleVerifier(createBundleVerifier());
170
171
test.addBundleDesktopIntegrationVerifier(config.values().stream()
172
.anyMatch(this::isWithDesktopIntegration));
173
174
test.run(PackageTest.Action.CREATE_AND_UNPACK);
175
}
176
}
177
178
boolean isWithDesktopIntegration(IconType iconType) {
179
if (appImage) {
180
return false;
181
}
182
boolean withDesktopFile = !Set.of(
183
IconType.NoIcon,
184
IconType.DefaultIcon).contains(iconType);
185
withDesktopFile |= List.of(extraJPackageArgs).contains("--linux-shortcut");
186
return withDesktopFile;
187
}
188
189
private ThrowingBiConsumer<JPackageCommand, Executor.Result> createBundleVerifier() {
190
return (cmd, result) -> {
191
var verifier = createConsoleOutputVerifier(cmd.name(), config.get(
192
Launcher.Main), null);
193
if (verifier != null) {
194
verifier.apply(result.getOutput().stream());
195
}
196
197
if (config.containsKey(Launcher.Additional)) {
198
verifier = createConsoleOutputVerifier(
199
Launcher.Additional.launcherName, config.get(
200
Launcher.Additional), config.get(Launcher.Main));
201
if (verifier != null) {
202
verifier.apply(result.getOutput().stream());
203
}
204
}
205
};
206
}
207
208
private TKit.TextStreamVerifier createConsoleOutputVerifier(
209
String launcherName, IconType iconType, IconType mainIconType) {
210
if (iconType == IconType.DefaultIcon && mainIconType != null) {
211
iconType = mainIconType;
212
}
213
return createConsoleOutputVerifier(launcherName, iconType);
214
}
215
216
private static TKit.TextStreamVerifier createConsoleOutputVerifier(
217
String launcherName, IconType iconType) {
218
String lookupString = null;
219
switch (iconType) {
220
case DefaultIcon:
221
lookupString = String.format(
222
"Using default package resource %s [icon] (add %s%s to the resource-dir to customize)",
223
"JavaApp" + TKit.ICON_SUFFIX,
224
launcherName, TKit.ICON_SUFFIX);
225
break;
226
227
case ResourceDirIcon:
228
lookupString = String.format(
229
"Using custom package resource [icon] (loaded from %s%s)",
230
launcherName, TKit.ICON_SUFFIX);
231
break;
232
233
case CustomIcon:
234
case CustomWithResourceDirIcon:
235
lookupString = "Using custom package resource [icon] (loaded from file";
236
break;
237
238
default:
239
return null;
240
}
241
242
return TKit.assertTextStream(lookupString);
243
}
244
245
private ThrowingConsumer<JPackageCommand> createInstallVerifier() {
246
LauncherIconVerifier verifier = new LauncherIconVerifier();
247
switch (config.get(Launcher.Main)) {
248
case NoIcon:
249
verifier.setExpectedIcon(null);
250
break;
251
252
case DefaultIcon:
253
verifier.setExpectedDefaultIcon();
254
break;
255
256
case CustomIcon:
257
verifier.setExpectedIcon(Launcher.Main.cmdlineIcon);
258
break;
259
260
case ResourceDirIcon:
261
verifier.setExpectedIcon(Launcher.Main.resourceDirIcon);
262
break;
263
264
case CustomWithResourceDirIcon:
265
verifier.setExpectedIcon(Launcher.Main2.cmdlineIcon);
266
break;
267
}
268
269
return cmd -> {
270
verifier.applyTo(cmd);
271
if (TKit.isLinux() && !cmd.isImagePackageType()) {
272
Path desktopFile = LinuxHelper.getDesktopFile(cmd);
273
if (isWithDesktopIntegration(config.get(Launcher.Main))) {
274
TKit.assertFileExists(desktopFile);
275
} else {
276
TKit.assertPathExists(desktopFile, false);
277
}
278
}
279
};
280
}
281
282
private void initTest(JPackageCommand cmd, PackageTest test) {
283
config.entrySet().forEach(ThrowingConsumer.toConsumer(entry -> {
284
initTest(entry.getKey(), entry.getValue(), cmd, test);
285
}));
286
287
ThrowingConsumer<JPackageCommand> initializer = testCmd -> {
288
testCmd.saveConsoleOutput(true);
289
testCmd.setFakeRuntime();
290
testCmd.addArguments(extraJPackageArgs);
291
};
292
293
if (test != null) {
294
test.addInitializer(initializer);
295
} else {
296
ThrowingConsumer.toConsumer(initializer).accept(cmd);
297
}
298
}
299
300
private static void initTest(Launcher cfg, IconType iconType,
301
JPackageCommand cmd, PackageTest test) throws IOException {
302
Consumer<AdditionalLauncher> addLauncher = v -> {
303
if (test != null) {
304
v.applyTo(test);
305
} else {
306
v.applyTo(cmd);
307
}
308
};
309
310
switch (iconType) {
311
case DefaultIcon:
312
if (cfg.launcherName != null) {
313
addLauncher.accept(new AdditionalLauncher(cfg.launcherName));
314
}
315
break;
316
317
case NoIcon:
318
if (cfg.launcherName != null) {
319
addLauncher.accept(
320
new AdditionalLauncher(cfg.launcherName).setNoIcon());
321
}
322
break;
323
324
case CustomIcon:
325
if (test != null) {
326
addCustomIcon(null, test, cfg.launcherName, cfg.cmdlineIcon);
327
} else {
328
addCustomIcon(cmd, null, cfg.launcherName, cfg.cmdlineIcon);
329
}
330
break;
331
332
case ResourceDirIcon:
333
if (Launcher.PRIMARY.contains(cfg) && cfg.launcherName != null) {
334
addLauncher.accept(new AdditionalLauncher(cfg.launcherName));
335
}
336
if (test != null) {
337
test.addInitializer(testCmd -> {
338
addResourceDirIcon(testCmd, cfg.launcherName,
339
cfg.resourceDirIcon);
340
});
341
} else {
342
addResourceDirIcon(cmd, cfg.launcherName, cfg.resourceDirIcon);
343
}
344
break;
345
346
case CustomWithResourceDirIcon:
347
switch (cfg) {
348
case Main:
349
initTest(Launcher.Main2, IconType.CustomIcon, cmd, test);
350
initTest(Launcher.Main2, IconType.ResourceDirIcon, cmd, test);
351
break;
352
353
case Additional:
354
initTest(Launcher.Additional2, IconType.CustomIcon, cmd, test);
355
initTest(Launcher.Additional2, IconType.ResourceDirIcon, cmd, test);
356
break;
357
358
default:
359
throw new IllegalArgumentException();
360
}
361
break;
362
}
363
}
364
365
private JPackageCommand initAppImageTest() {
366
JPackageCommand cmd = JPackageCommand.helloAppImage();
367
initTest(cmd, null);
368
return cmd;
369
}
370
371
private PackageTest initPackageTest() {
372
PackageTest test = new PackageTest().configureHelloApp();
373
initTest(null, test);
374
return test;
375
}
376
377
private static void addResourceDirIcon(JPackageCommand cmd,
378
String launcherName, Path iconPath) throws IOException {
379
Path resourceDir = cmd.getArgumentValue("--resource-dir", () -> null,
380
Path::of);
381
if (resourceDir == null) {
382
resourceDir = TKit.createTempDirectory("resources");
383
cmd.addArguments("--resource-dir", resourceDir);
384
}
385
386
String dstIconFileName = Optional.ofNullable(launcherName).orElseGet(
387
() -> cmd.name()) + TKit.ICON_SUFFIX;
388
389
TKit.trace(String.format("Resource file: [%s] <- [%s]",
390
resourceDir.resolve(dstIconFileName), iconPath));
391
Files.copy(iconPath, resourceDir.resolve(dstIconFileName),
392
StandardCopyOption.REPLACE_EXISTING);
393
}
394
395
private static void addCustomIcon(JPackageCommand cmd, PackageTest test,
396
String launcherName, Path iconPath) throws IOException {
397
398
if (launcherName != null) {
399
AdditionalLauncher al = new AdditionalLauncher(launcherName).setIcon(
400
iconPath);
401
if (test != null) {
402
al.applyTo(test);
403
} else {
404
al.applyTo(cmd);
405
}
406
} else if (test != null) {
407
test.addInitializer(testCmd -> {
408
testCmd.addArguments("--icon", iconPath);
409
});
410
} else {
411
cmd.addArguments("--icon", iconPath);
412
}
413
}
414
415
private enum Launcher {
416
Main(null, ICONS[0], ICONS[1]),
417
Main2(null, ICONS[1], ICONS[0]),
418
Additional("x", ICONS[2], ICONS[3]),
419
Additional2("x", ICONS[3], ICONS[2]);
420
421
Launcher(String name, Path cmdlineIcon, Path resourceDirIcon) {
422
this.launcherName = name;
423
this.cmdlineIcon = cmdlineIcon;
424
this.resourceDirIcon = resourceDirIcon;
425
}
426
427
private final String launcherName;
428
private final Path cmdlineIcon;
429
private final Path resourceDirIcon;
430
431
private final static Set<Launcher> PRIMARY = Set.of(Main, Additional);
432
}
433
434
private final boolean appImage;
435
private final Map<Launcher, IconType> config;
436
private final String[] extraJPackageArgs;
437
438
private static Path iconPath(String name) {
439
return TKit.TEST_SRC_ROOT.resolve(Path.of("resources", name
440
+ TKit.ICON_SUFFIX));
441
}
442
443
private final static Path[] ICONS = Stream.of("icon", "icon2", "icon3",
444
"icon4")
445
.map(IconTest::iconPath)
446
.collect(Collectors.toList()).toArray(Path[]::new);
447
}
448
449