/*
 * MIT License
 *
 * Copyright (c) 2020 Jannis Weis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package test;

import java.awt.*;
import java.awt.image.BufferedImage;

import javax.swing.*;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;

import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.theme.IntelliJTheme;
import com.github.weisj.darklaf.ui.DarkPopupFactory;
import com.github.weisj.darklaf.ui.tooltip.ToolTipConstants;
import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.SystemInfo;

class TooltipTest extends AbstractImageTest {

    public TooltipTest() {
        super("tooltip");
    }

    @BeforeAll
    static void setup() {
        LafManager.install(new IntelliJTheme());
    }

    private JToolTip createTooltip() {
        JToolTip toolTip = new JToolTip();
        toolTip.setTipText("Test ToolTip");
        toolTip.putClientProperty(ToolTipConstants.KEY_STYLE, ToolTipConstants.VARIANT_BALLOON);
        toolTip.putClientProperty(DarkPopupFactory.KEY_START_HIDDEN, false);
        toolTip.setSize(toolTip.getPreferredSize());
        toolTip.doLayout();
        return toolTip;
    }

    @Test
    @EnabledOnOs({OS.MAC, OS.WINDOWS, OS.LINUX})
    void testTooltipTransparency() throws Exception {
        JToolTip toolTip = createTooltip();

        SwingUtilities.invokeAndWait(() -> {
            Popup popup = PopupFactory.getSharedInstance().getPopup(null, toolTip, 0, 0);
            popup.show();
        });

        Window window = DarkUIUtil.getWindow(toolTip);
        SwingUtilities.invokeAndWait(() -> window.setOpacity(1));

        TestUtils.runOnSwingThreadNotThrowing(() -> {
            Component c;
            for (c = toolTip.getParent(); c != null; c = c.getParent()) {
                Color bg = c.getBackground();
                Assertions.assertNotNull(bg, "Background is null for " + c);
                Assertions.assertEquals(0, bg.getAlpha(), "Background " + bg + " is opaque " + c);
                if (c instanceof Window) break;
            }
            Assertions.assertEquals(c, window, "Did not traverse full hierarchy");

            JRootPane rootPane = SwingUtilities.getRootPane(window);
            Assertions.assertNotNull(rootPane, "RootPane is null");
            Assertions.assertFalse(rootPane.isOpaque(), "RootPane is opaque");

            Color backgroundColor = window.getBackground();
            Assertions.assertNotNull(backgroundColor, "Background is null");

            BufferedImage img = saveScreenShot(getPath("tooltip_" + SystemInfo.getOsName()), window);
            Assertions.assertNotNull(img, "Tooltip Image is null");
            int alpha = getAlpha(img.getRGB(img.getMinX(), img.getMinY() + img.getHeight() - 1));
            Assertions.assertEquals(0, alpha, "Tooltip is opaque");
        });
    }

    private int getAlpha(final int rgb) {
        return new Color(rgb, true).getAlpha();
    }
}

@@ -1196,31 +1196,110 @@
 ge;%0A
-%0Aimport javax.swing.*;%0A
+import java.util.logging.Logger;%0A%0Aimport javax.swing.*;%0A%0Aimport com.github.weisj.darklaf.util.LogUtil;
 %0Aimp
@@ -1864,16 +1864,96 @@
 Test %7B%0A%0A
+    private static final Logger LOGGER = LogUtil.getLogger(TooltipTest.class);%0A%0A
     publ
@@ -2626,16 +2626,235 @@
 ption %7B%0A
+        LOGGER.info(%22DISPLAY = %22 + System.getenv(%22DISPLAY%22));%0A        LOGGER.info(%22java.awt.headless = %22 + System.getProperty(%22java.awt.headless%22));%0A        LOGGER.info(%22Headless: %22 + GraphicsEnvironment.isHeadless());%0A
         
@@ -2886,24 +2886,65 @@
 Tooltip();%0A%0A
+        LOGGER.info(%22Requesting popup%22);%0A
         Swin
@@ -3088,24 +3088,72 @@
 pup.show();%0A
+            LOGGER.info(%22Got popup: %22 + popup);%0A
         %7D);%0A
@@ -3204,24 +3204,106 @@
 w(toolTip);%0A
+        LOGGER.info(%22Popup Window: %22 + window);%0A        if (window != null) %7B%0A    
         Swin
@@ -3355,16 +3355,26 @@
 ity(1));
+%0A        %7D
 %0A%0A      
@@ -3446,16 +3446,66 @@
 nent c;%0A
+            LOGGER.info(%22Checking backgrounds:%22);%0A
         
@@ -3608,32 +3608,86 @@
 etBackground();%0A
+                LOGGER.info(%22bg = %22 + bg + %22: %22 + c);%0A
                 
@@ -4213,146 +4213,8 @@
 );%0A%0A
-            Color backgroundColor = window.getBackground();%0A            Assertions.assertNotNull(backgroundColor, %22Background is null%22);%0A%0A
     

Improve tooltip test.