Skip to content

Commit e280bb5

Browse files
Fix fa icon verification on Linux
1 parent 277db7a commit e280bb5

File tree

4 files changed

+67
-40
lines changed

4 files changed

+67
-40
lines changed

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/FileAssociations.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package jdk.jpackage.test;
2424

25+
2526
import java.awt.Desktop;
2627
import java.io.IOException;
2728
import java.nio.file.Path;
@@ -32,7 +33,6 @@
3233
import java.util.Objects;
3334
import java.util.Optional;
3435
import java.util.TreeMap;
35-
import jdk.jpackage.internal.util.PathUtils;
3636

3737

3838
public final class FileAssociations {
@@ -75,13 +75,6 @@ public FileAssociations setIcon(Path v) {
7575
return this;
7676
}
7777

78-
Path getLinuxIconFileName() {
79-
if (icon == null) {
80-
return null;
81-
}
82-
return Path.of(getMime().replace('/', '-') + PathUtils.getSuffix(icon));
83-
}
84-
8578
Path getPropertiesFile() {
8679
return file;
8780
}
@@ -94,6 +87,10 @@ String getMime() {
9487
return "application/x-jpackage-" + suffixName;
9588
}
9689

90+
boolean hasIcon() {
91+
return icon != null;
92+
}
93+
9794
public void applyTo(PackageTest test) {
9895
test.notForTypes(PackageType.MAC_DMG, () -> {
9996
test.addInitializer(cmd -> {

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/LauncherIconVerifier.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,11 @@ public Optional<Path> expectIcon() {
6666
}
6767

6868
public void applyTo(JPackageCommand cmd) throws IOException {
69-
final String curLauncherName;
70-
final String label;
71-
if (launcherName == null) {
72-
curLauncherName = cmd.name();
73-
label = "main";
74-
} else {
75-
curLauncherName = launcherName;
76-
label = String.format("[%s]", launcherName);
77-
}
69+
final String label = Optional.ofNullable(launcherName).map(v -> {
70+
return String.format("[%s]", v);
71+
}).orElse("main");
7872

79-
Path iconPath = cmd.appLayout().desktopIntegrationDirectory().resolve(
80-
curLauncherName + TKit.ICON_SUFFIX);
73+
Path iconPath = cmd.appLayout().desktopIntegrationDirectory().resolve(iconFileName(cmd));
8174

8275
if (TKit.isWindows()) {
8376
TKit.assertPathExists(iconPath, false);
@@ -99,6 +92,14 @@ public void applyTo(JPackageCommand cmd) throws IOException {
9992
}
10093
}
10194

95+
private Path iconFileName(JPackageCommand cmd) {
96+
if (TKit.isLinux()) {
97+
return LinuxHelper.getLauncherIconFileName(cmd, launcherName);
98+
} else {
99+
return Path.of(Optional.ofNullable(launcherName).orElseGet(cmd::name) + TKit.ICON_SUFFIX);
100+
}
101+
}
102+
102103
private String launcherName;
103104
private Path expectedIcon;
104105
private boolean expectedDefault;

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/LinuxHelper.java

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.Set;
4646
import java.util.TreeMap;
4747
import java.util.TreeSet;
48+
import java.util.function.Consumer;
4849
import java.util.function.Function;
4950
import java.util.function.Predicate;
5051
import java.util.regex.Matcher;
@@ -90,9 +91,7 @@ public static Path getDesktopFile(JPackageCommand cmd) {
9091

9192
public static Path getDesktopFile(JPackageCommand cmd, String launcherName) {
9293
cmd.verifyIsOfType(PackageType.LINUX);
93-
String desktopFileName = String.format("%s-%s.desktop", getPackageName(
94-
cmd), Optional.ofNullable(launcherName).orElseGet(
95-
() -> cmd.name()).replaceAll("\\s+", "_"));
94+
var desktopFileName = getLauncherDesktopFileName(cmd, launcherName);
9695
return cmd.appLayout().desktopIntegrationDirectory().resolve(
9796
desktopFileName);
9897
}
@@ -204,6 +203,20 @@ public static String getBundleProperty(JPackageCommand cmd,
204203
}
205204
}
206205

206+
private static Path getFaIconFileName(JPackageCommand cmd, String mimeType) {
207+
return Path.of(mimeType.replace('/', '-') + ".png");
208+
}
209+
210+
static Path getLauncherDesktopFileName(JPackageCommand cmd, String launcherName) {
211+
return Path.of(String.format("%s-%s.desktop", getPackageName(cmd),
212+
Optional.ofNullable(launcherName).orElseGet(cmd::name).replaceAll("\\s+", "_")));
213+
}
214+
215+
static Path getLauncherIconFileName(JPackageCommand cmd, String launcherName) {
216+
return Path.of(String.format("%s.png",
217+
Optional.ofNullable(launcherName).orElseGet(cmd::name).replaceAll("\\s+", "_")));
218+
}
219+
207220
static PackageHandlers createDebPackageHandlers() {
208221
return new PackageHandlers(LinuxHelper::installDeb, LinuxHelper::uninstallDeb, LinuxHelper::unpackDeb);
209222
}
@@ -423,7 +436,12 @@ static void addBundleDesktopIntegrationVerifier(PackageTest test, boolean integr
423436
});
424437
}
425438

426-
static void verifyDesktopFiles(JPackageCommand cmd, boolean installed) {
439+
static void verifyDesktopIntegrationFiles(JPackageCommand cmd, boolean installed) {
440+
verifyDesktopFiles(cmd, installed);
441+
verifyAllIconsReferenced(cmd);
442+
}
443+
444+
private static void verifyDesktopFiles(JPackageCommand cmd, boolean installed) {
427445
final var desktopFiles = getDesktopFiles(cmd);
428446
try {
429447
if (installed) {
@@ -451,8 +469,6 @@ static void verifyDesktopFiles(JPackageCommand cmd, boolean installed) {
451469
} catch (IOException ex) {
452470
throw new UncheckedIOException(ex);
453471
}
454-
455-
verifyIcons(cmd);
456472
}
457473

458474
private static Collection<Path> getDesktopFiles(JPackageCommand cmd) {
@@ -481,7 +497,7 @@ private static Stream<Path> relativePackageFilesInSubdirectory(
481497
}).map(packageDir::relativize);
482498
}
483499

484-
private static void verifyIcons(JPackageCommand cmd) {
500+
private static void verifyAllIconsReferenced(JPackageCommand cmd) {
485501

486502
var installCmd = Optional.ofNullable(cmd.unpackedPackageDirectory()).map(_ -> {
487503
return cmd.createMutableCopy().setUnpackedPackageLocation(null);
@@ -495,8 +511,18 @@ private static void verifyIcons(JPackageCommand cmd) {
495511
}).map(installCmd.appLayout().desktopIntegrationDirectory()::resolve).collect(toSet());
496512

497513
var referencedIcons = getDesktopFiles(cmd).stream().map(path -> {
498-
return new DesktopFile(path, false).findQuotedValue("Icon");
499-
}).filter(Optional::isPresent).map(Optional::get).map(Path::of).collect(toSet());
514+
return new DesktopFile(path, false);
515+
}).<Path>mapMulti((desktopFile, sink) -> {
516+
desktopFile.findQuotedValue("Icon").map(Path::of).ifPresent(sink);
517+
desktopFile.find("MimeType").ifPresent(str -> {
518+
Stream.of(str.split(";"))
519+
.map(mimeType -> {
520+
return getFaIconFileName(cmd, mimeType);
521+
})
522+
.map(installCmd.appLayout().desktopIntegrationDirectory()::resolve)
523+
.forEach(sink);
524+
});
525+
}).collect(toSet());
500526

501527
var unreferencedIconFiles = Comm.compare(installedIconFiles, referencedIcons).unique1().stream().sorted().toList();
502528

@@ -686,16 +712,19 @@ static void addFileAssociationsVerifier(PackageTest test, FileAssociations fa) {
686712
});
687713

688714
test.addBundleVerifier(cmd -> {
689-
final Path mimeTypeIconFileName = fa.getLinuxIconFileName();
690-
if (mimeTypeIconFileName != null) {
691-
// Verify there are xdg registration commands for mime icon file.
692-
Path mimeTypeIcon = cmd.appLayout().desktopIntegrationDirectory().resolve(
693-
mimeTypeIconFileName);
694-
695-
Map<Scriptlet, List<String>> scriptlets = getScriptlets(cmd);
696-
scriptlets.entrySet().stream().forEach(e -> verifyIconInScriptlet(
697-
e.getKey(), e.getValue(), mimeTypeIcon));
698-
}
715+
Optional.of(fa).filter(FileAssociations::hasIcon)
716+
.map(FileAssociations::getMime)
717+
.map(mimeType -> {
718+
return getFaIconFileName(cmd, mimeType);
719+
}).ifPresent(mimeTypeIconFileName -> {
720+
// Verify there are xdg registration commands for mime icon file.
721+
Path mimeTypeIcon = cmd.appLayout().desktopIntegrationDirectory().resolve(
722+
mimeTypeIconFileName);
723+
724+
Map<Scriptlet, List<String>> scriptlets = getScriptlets(cmd);
725+
scriptlets.entrySet().stream().forEach(e -> verifyIconInScriptlet(
726+
e.getKey(), e.getValue(), mimeTypeIcon));
727+
});
699728
});
700729
}
701730

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/PackageTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ private void verifyPackageInstalled(JPackageCommand cmd) {
784784
}
785785

786786
if (isOfType(cmd, LINUX)) {
787-
LinuxHelper.verifyDesktopFiles(cmd, true);
787+
LinuxHelper.verifyDesktopIntegrationFiles(cmd, true);
788788
}
789789
}
790790

@@ -865,7 +865,7 @@ private void verifyPackageUninstalled(JPackageCommand cmd) {
865865
}
866866

867867
if (isOfType(cmd, LINUX)) {
868-
LinuxHelper.verifyDesktopFiles(cmd, false);
868+
LinuxHelper.verifyDesktopIntegrationFiles(cmd, false);
869869
}
870870
}
871871

0 commit comments

Comments
 (0)