| /* | |
| * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. | |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| * | |
| * This code is free software; you can redistribute it and/or modify it | |
| * under the terms of the GNU General Public License version 2 only, as | |
| * published by the Free Software Foundation. | |
| * | |
| * This code is distributed in the hope that it will be useful, but WITHOUT | |
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| * version 2 for more details (a copy is included in the LICENSE file that | |
| * accompanied this code). | |
| * | |
| * You should have received a copy of the GNU General Public License version | |
| * 2 along with this work; if not, write to the Free Software Foundation, | |
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| * | |
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| * or visit www.oracle.com if you need additional information or have any | |
| * questions. | |
| */ | |
| import static java.util.Map.entry; | |
| import java.nio.file.Path; | |
| import java.util.List; | |
| import java.util.Map; | |
| import jdk.jpackage.internal.util.PListReader; | |
| import jdk.jpackage.test.Annotations.Test; | |
| import jdk.jpackage.test.JPackageCommand; | |
| import jdk.jpackage.test.MacHelper; | |
| import jdk.jpackage.test.TKit; | |
| /** | |
| * Tests generation of app image with --file-associations and mac additional file | |
| * association arguments. Test will verify that arguments correctly propagated to | |
| * Info.plist. | |
| */ | |
| /* | |
| * @test | |
| * @summary jpackage with --file-associations and mac specific file association args | |
| * @library /test/jdk/tools/jpackage/helpers | |
| * @build jdk.jpackage.test.* | |
| * @build MacFileAssociationsTest | |
| * @requires (os.family == "mac") | |
| * @run main/othervm -Xmx512m jdk.jpackage.test.Main | |
| * --jpt-run=MacFileAssociationsTest | |
| */ | |
| public class MacFileAssociationsTest { | |
| @Test | |
| public static void test() throws Exception { | |
| final Path propFile = TKit.workDir().resolve("fa.properties"); | |
| Map<String, String> map = Map.ofEntries( | |
| entry("mime-type", "application/x-jpackage-foo"), | |
| entry("extension", "foo"), | |
| entry("description", "bar"), | |
| entry("mac.CFBundleTypeRole", "Viewer"), | |
| entry("mac.LSHandlerRank", "Default"), | |
| entry("mac.NSDocumentClass", "SomeClass"), | |
| entry("mac.LSTypeIsPackage", "true"), | |
| entry("mac.LSSupportsOpeningDocumentsInPlace", "false"), | |
| entry("mac.UISupportsDocumentBrowser", "false"), | |
| entry("mac.NSExportableTypes", "public.png, public.jpg"), | |
| entry("mac.UTTypeConformsTo", "public.image, public.data")); | |
| TKit.createPropertiesFile(propFile, map); | |
| JPackageCommand cmd = JPackageCommand.helloAppImage(); | |
| cmd.addArguments("--file-associations", propFile); | |
| cmd.executeAndAssertHelloAppImageCreated(); | |
| Path appImage = cmd.outputBundle(); | |
| verifyPList(appImage); | |
| } | |
| private static void checkStringValue(PListReader plist, String key, String value) { | |
| String result = plist.queryValue(key); | |
| TKit.assertEquals(value, result, String.format( | |
| "Check value of %s plist key", key)); | |
| } | |
| private static void checkBoolValue(PListReader plist, String key, boolean value) { | |
| boolean result = plist.queryBoolValue(key); | |
| TKit.assertEquals(value, result, String.format( | |
| "Check value of %s plist key", key)); | |
| } | |
| private static void checkArrayValue(PListReader plist, String key, | |
| List<String> values) { | |
| List<String> result = plist.queryStringArrayValue(key); | |
| TKit.assertStringListEquals(values, result, String.format( | |
| "Check value of %s plist key", key)); | |
| } | |
| private static void verifyPList(Path appImage) throws Exception { | |
| final var rootPlist = MacHelper.readPListFromAppImage(appImage); | |
| TKit.traceFileContents(appImage.resolve("Contents/Info.plist"), "Info.plist"); | |
| var plist = rootPlist.queryArrayValue("CFBundleDocumentTypes", false).findFirst().map(PListReader.class::cast).orElseThrow(); | |
| checkStringValue(plist, "CFBundleTypeRole", "Viewer"); | |
| checkStringValue(plist, "LSHandlerRank", "Default"); | |
| checkStringValue(plist, "NSDocumentClass", "SomeClass"); | |
| checkBoolValue(plist, "LSTypeIsPackage", true); | |
| checkBoolValue(plist, "LSSupportsOpeningDocumentsInPlace", false); | |
| checkBoolValue(plist, "UISupportsDocumentBrowser", false); | |
| plist = rootPlist.queryArrayValue("UTExportedTypeDeclarations", false).findFirst().map(PListReader.class::cast).orElseThrow(); | |
| checkArrayValue(plist, "UTTypeConformsTo", List.of("public.image", "public.data")); | |
| plist = plist.queryDictValue("UTTypeTagSpecification"); | |
| checkArrayValue(plist, "NSExportableTypes", List.of("public.png", "public.jpg")); | |
| } | |
| } |