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 package jdk.jpackage.test;
  25 
  26 import java.io.IOException;
  27 import java.nio.file.Files;
  28 import java.nio.file.Path;
  29 
  30 public final class LauncherIconVerifier {
  31     public LauncherIconVerifier() {
  32     }
  33 
  34     public LauncherIconVerifier setLauncherName(String v) {
  35         launcherName = v;
  36         return this;
  37     }
  38 
  39     public LauncherIconVerifier setExpectedIcon(Path v) {
  40         expectedIcon = v;
  41         return this;
  42     }
  43 
  44     public LauncherIconVerifier setExpectedDefaultIcon() {
  45         expectedIcon = getDefaultIcon();
  46         return this;
  47     }
  48 
  49     public void applyTo(JPackageCommand cmd) throws IOException {
  50         final String curLauncherName;
  51         final String label;
  52         if (launcherName == null) {
  53             curLauncherName = cmd.name();
  54             label = "main";
  55         } else {
  56             curLauncherName = launcherName;
  57             label = String.format("[%s]", launcherName);
  58         }
  59 
  60         Path iconPath = cmd.appLayout().destktopIntegrationDirectory().resolve(
  61                 curLauncherName + TKit.ICON_SUFFIX);
  62 
  63         if (expectedIcon == null) {
  64             TKit.assertPathExists(iconPath, false);
  65             return;
  66         }
  67 
  68         TKit.assertFileExists(iconPath);
  69         TKit.assertTrue(-1 == Files.mismatch(expectedIcon, iconPath),
  70                 String.format(
  71                         "Check icon file [%s] of %s launcher is a copy of source icon file [%s]",
  72                         iconPath, label, expectedIcon));
  73     }
  74 
  75     public static Path getDefaultIcon() {
  76         final String[] components;
  77         if (TKit.isOSX()) {
  78             components = new String[] { "macosx", "java.icns" };
  79         } else if (TKit.isLinux()) {
  80             components = new String[] { "linux", "java32.png" };
  81         } else if (TKit.isWindows()) {
  82             components = new String[] { "windows", "java48.ico" };
  83         } else {
  84             throw TKit.throwUnknownPlatformError();
  85         }
  86 
  87         return TKit.SRC_ROOT.resolve(Path.of(components[0],
  88                 "classes/jdk/incubator/jpackage/internal/resources", components[1]));
  89     }
  90 
  91     private String launcherName;
  92     private Path expectedIcon;
  93 }