1 /*
   2  * Copyright (c) 2019, 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.nio.charset.StandardCharsets;
  25 import java.nio.file.Files;
  26 import java.util.Map;
  27 import java.nio.file.Path;
  28 import java.util.List;
  29 import java.util.stream.Collectors;
  30 import jdk.jpackage.test.FileAssociations;
  31 import jdk.jpackage.test.PackageType;
  32 import jdk.jpackage.test.PackageTest;
  33 import jdk.jpackage.test.TKit;
  34 import jdk.jpackage.test.Annotations.Test;
  35 import jdk.jpackage.test.*;
  36 
  37 /**
  38  * Test --linux-shortcut parameter. Output of the test should be
  39  * shortcuthinttest_1.0-1_amd64.deb or shortcuthinttest-1.0-1.amd64.rpm package
  40  * bundle. The output package should provide the same functionality as the
  41  * default package and also create a desktop shortcut.
  42  *
  43  * Finding a shortcut of the application launcher through GUI depends on desktop
  44  * environment.
  45  *
  46  * deb:
  47  * Search online for `Ways To Open A Ubuntu Application` for instructions.
  48  *
  49  * rpm:
  50  *
  51  */
  52 
  53 /*
  54  * @test
  55  * @summary jpackage with --linux-shortcut
  56  * @library ../helpers
  57  * @key jpackagePlatformPackage
  58  * @requires jpackage.test.SQETest == null
  59  * @build jdk.jpackage.test.*
  60  * @requires (os.family == "linux")
  61  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  62  * @compile ShortcutHintTest.java
  63  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  64  *  --jpt-run=ShortcutHintTest
  65  */
  66 
  67 /*
  68  * @test
  69  * @summary jpackage with --linux-shortcut
  70  * @library ../helpers
  71  * @key jpackagePlatformPackage
  72  * @build jdk.jpackage.test.*
  73  * @requires (os.family == "linux")
  74  * @requires jpackage.test.SQETest != null
  75  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  76  * @compile ShortcutHintTest.java
  77  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  78  *  --jpt-run=ShortcutHintTest.testBasic
  79  */
  80 
  81 public class ShortcutHintTest {
  82 
  83     @Test
  84     public static void testBasic() {
  85         createTest().addInitializer(cmd -> {
  86             cmd.addArgument("--linux-shortcut");
  87         }).run();
  88     }
  89 
  90     private static PackageTest createTest() {
  91         return new PackageTest()
  92                 .forTypes(PackageType.LINUX)
  93                 .configureHelloApp()
  94                 .addBundleDesktopIntegrationVerifier(true);
  95 
  96     }
  97 
  98     /**
  99      * Adding `--icon` to jpackage command line should create desktop shortcut
 100      * even though `--linux-shortcut` is omitted.
 101      */
 102     @Test
 103     public static void testCustomIcon() {
 104         createTest().addInitializer(cmd -> {
 105             cmd.setFakeRuntime();
 106             cmd.addArguments("--icon", TKit.TEST_SRC_ROOT.resolve(
 107                     "apps/dukeplug.png"));
 108         }).run();
 109     }
 110 
 111     /**
 112      * Adding `--file-associations` to jpackage command line should create
 113      * desktop shortcut even though `--linux-shortcut` is omitted.
 114      */
 115     @Test
 116     public static void testFileAssociations() {
 117         PackageTest test = createTest().addInitializer(
 118                 JPackageCommand::setFakeRuntime);
 119         new FileAssociations("ShortcutHintTest_testFileAssociations").applyTo(
 120                 test);
 121         test.run();
 122     }
 123 
 124     /**
 125      * Additional launcher with icon should create desktop shortcut even though
 126      * `--linux-shortcut` is omitted.
 127      */
 128     @Test
 129     public static void testAdditionaltLaunchers() {
 130         createTest().addInitializer(cmd -> {
 131             cmd.setFakeRuntime();
 132 
 133             final String launcherName = "Foo";
 134             final Path propsFile = TKit.workDir().resolve(
 135                     launcherName + ".properties");
 136 
 137             cmd.addArguments("--add-launcher", String.format("%s=%s",
 138                     launcherName, propsFile));
 139 
 140             TKit.createPropertiesFile(propsFile, Map.entry("icon",
 141                     TKit.TEST_SRC_ROOT.resolve("apps/dukeplug.png").toString()));
 142         }).run();
 143     }
 144 
 145     /**
 146      * .desktop file from resource dir.
 147      */
 148     @Test
 149     public static void testDesktopFileFromResourceDir() {
 150         final String expectedVersionString = "Version=12345678";
 151         TKit.withTempDirectory("resources", tempDir -> {
 152             createTest().addInitializer(cmd -> {
 153                 cmd.setFakeRuntime();
 154 
 155                 cmd.addArgument("--linux-shortcut");
 156                 cmd.addArguments("--resource-dir", tempDir);
 157 
 158                 // Create custom .desktop file in resource directory
 159                 TKit.createTextFile(tempDir.resolve(cmd.name() + ".desktop"),
 160                         List.of(
 161                                 "[Desktop Entry]",
 162                                 "Name=APPLICATION_NAME",
 163                                 "Exec=APPLICATION_LAUNCHER",
 164                                 "Terminal=false",
 165                                 "Type=Application",
 166                                 "Categories=DEPLOY_BUNDLE_CATEGORY",
 167                                 expectedVersionString
 168                         ));
 169             })
 170             .addInstallVerifier(cmd -> {
 171                 Path desktopFile = cmd.appLayout().destktopIntegrationDirectory().resolve(
 172                         String.format("%s-%s.desktop",
 173                                 LinuxHelper.getPackageName(cmd), cmd.name()));
 174                 TKit.assertFileExists(desktopFile);
 175                 TKit.assertTextStream(expectedVersionString)
 176                         .label(String.format("[%s] file", desktopFile))
 177                         .predicate(String::equals)
 178                         .apply(Files.readAllLines(desktopFile).stream());
 179             }).run();
 180         });
 181     }
 182 }