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.io.IOException;
  25 import java.nio.file.Files;
  26 import java.util.Map;
  27 import java.nio.file.Path;
  28 import java.util.List;
  29 import jdk.jpackage.test.AdditionalLauncher;
  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         PackageTest test = createTest();
 131 
 132         new AdditionalLauncher("Foo").setIcon(TKit.TEST_SRC_ROOT.resolve(
 133                 "apps/dukeplug.png")).applyTo(test);
 134 
 135         test.addInitializer(JPackageCommand::setFakeRuntime).run();
 136     }
 137 
 138     /**
 139      * .desktop file from resource dir.
 140      */
 141     @Test
 142     public static void testDesktopFileFromResourceDir() throws IOException {
 143         final String expectedVersionString = "Version=12345678";
 144 
 145         final Path tempDir = TKit.createTempDirectory("resources");
 146 
 147         createTest().addInitializer(cmd -> {
 148             cmd.setFakeRuntime();
 149 
 150             cmd.addArgument("--linux-shortcut");
 151             cmd.addArguments("--resource-dir", tempDir);
 152 
 153             // Create custom .desktop file in resource directory
 154             TKit.createTextFile(tempDir.resolve(cmd.name() + ".desktop"),
 155                     List.of(
 156                             "[Desktop Entry]",
 157                             "Name=APPLICATION_NAME",
 158                             "Exec=APPLICATION_LAUNCHER",
 159                             "Terminal=false",
 160                             "Type=Application",
 161                             "Categories=DEPLOY_BUNDLE_CATEGORY",
 162                             expectedVersionString
 163                     ));
 164         })
 165         .addInstallVerifier(cmd -> {
 166             Path desktopFile = cmd.appLayout().destktopIntegrationDirectory().resolve(
 167                     String.format("%s-%s.desktop",
 168                             LinuxHelper.getPackageName(cmd), cmd.name()));
 169             TKit.assertFileExists(desktopFile);
 170             TKit.assertTextStream(expectedVersionString)
 171                     .label(String.format("[%s] file", desktopFile))
 172                     .predicate(String::equals)
 173                     .apply(Files.readAllLines(desktopFile).stream());
 174         }).run();
 175     }
 176 }