1 /*
   2  * Copyright (c) 2018, 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.nio.file.Path;
  27 import java.nio.file.StandardCopyOption;
  28 import jdk.incubator.jpackage.internal.IOUtils;
  29 import jdk.jpackage.test.TKit;
  30 import jdk.jpackage.test.Functional;
  31 import jdk.jpackage.test.Annotations.*;
  32 import jdk.jpackage.test.JPackageCommand;
  33 
  34 /*
  35  * @test
  36  * @summary jpackage create image with custom icon
  37  * @library ../helpers
  38  * @build jdk.jpackage.test.*
  39  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  40  * @compile IconTest.java
  41  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  42  *  --jpt-run=IconTest
  43  */
  44 
  45 public class IconTest {
  46     @Test
  47     public static void testResourceDir() throws IOException {
  48         TKit.withTempDirectory("resources", tempDir -> {
  49             JPackageCommand cmd = JPackageCommand.helloAppImage()
  50                     .addArguments("--resource-dir", tempDir);
  51 
  52             Files.copy(GOLDEN_ICON, tempDir.resolve(appIconFileName(cmd)),
  53                     StandardCopyOption.REPLACE_EXISTING);
  54 
  55             testIt(cmd);
  56         });
  57     }
  58 
  59     @Test
  60     @Parameter("true")
  61     @Parameter("false")
  62     public static void testParameter(boolean relativePath) throws IOException {
  63         final Path iconPath;
  64         if (relativePath) {
  65             iconPath = TKit.createRelativePathCopy(GOLDEN_ICON);
  66         } else {
  67             iconPath = GOLDEN_ICON;
  68         }
  69 
  70         testIt(JPackageCommand.helloAppImage().addArguments("--icon", iconPath));
  71     }
  72 
  73     private static String appIconFileName(JPackageCommand cmd) {
  74         return IOUtils.replaceSuffix(cmd.appLauncherPath().getFileName(),
  75                 TKit.ICON_SUFFIX).toString();
  76     }
  77 
  78     private static void testIt(JPackageCommand cmd) throws IOException {
  79         cmd.executeAndAssertHelloAppImageCreated();
  80 
  81         Path iconPath = cmd.appLayout().destktopIntegrationDirectory().resolve(
  82                 appIconFileName(cmd));
  83 
  84         TKit.assertFileExists(iconPath);
  85         TKit.assertTrue(-1 == Files.mismatch(GOLDEN_ICON, iconPath),
  86                 String.format(
  87                         "Check application icon file [%s] is a copy of source icon file [%s]",
  88                         iconPath, GOLDEN_ICON));
  89     }
  90 
  91     private final static Path GOLDEN_ICON = TKit.TEST_SRC_ROOT.resolve(Path.of(
  92             "resources", "icon" + TKit.ICON_SUFFIX));
  93 }