diff --git a/platform/platform-tests/testData/ui/gold_OffsetIcon@1x.png b/platform/platform-tests/testData/ui/gold_OffsetIcon@1x.png new file mode 100644 index 000000000000..e821f9402fc8 Binary files /dev/null and b/platform/platform-tests/testData/ui/gold_OffsetIcon@1x.png differ diff --git a/platform/platform-tests/testData/ui/gold_OffsetIcon@2x.png b/platform/platform-tests/testData/ui/gold_OffsetIcon@2x.png new file mode 100644 index 000000000000..d3be1f6d9c2d Binary files /dev/null and b/platform/platform-tests/testData/ui/gold_OffsetIcon@2x.png differ diff --git a/platform/platform-tests/testData/ui/gold_OffsetIcon@4x.png b/platform/platform-tests/testData/ui/gold_OffsetIcon@4x.png new file mode 100644 index 000000000000..6fed71f7eaa3 Binary files /dev/null and b/platform/platform-tests/testData/ui/gold_OffsetIcon@4x.png differ diff --git a/platform/platform-tests/testSrc/com/intellij/util/ui/CompositeIconPaintTestHelper.java b/platform/platform-tests/testSrc/com/intellij/util/ui/CompositeIconPaintTestHelper.java new file mode 100644 index 000000000000..a50e3586f0f6 --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/util/ui/CompositeIconPaintTestHelper.java @@ -0,0 +1,88 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.util.ui; + +import com.intellij.openapi.util.IconLoader; +import com.intellij.openapi.util.Pair; +import com.intellij.openapi.util.ScalableIcon; +import com.intellij.ui.RestoreScaleRule; +import com.intellij.util.ui.paint.ImageComparator; +import org.junit.ClassRule; +import org.junit.rules.ExternalResource; + +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.net.MalformedURLException; +import java.util.function.BiFunction; + +import static com.intellij.util.ui.JBUI.ScaleType.*; +import static com.intellij.util.ui.TestScaleHelper.*; + +/** + * @author tav + */ +public abstract class CompositeIconPaintTestHelper { + @ClassRule + public static final ExternalResource manageState = new RestoreScaleRule(); + + protected void test() { + overrideJreHiDPIEnabled(true); + + BiFunction bit2scale = (mask, bit) -> ((mask >> bit) & 0x1) + 1; + + for (int mask=0; mask<7; mask++) { + int iconScale = bit2scale.apply(mask, 2); + int usrScale = bit2scale.apply(mask, 1); + int sysScale = bit2scale.apply(mask, 0); + assert iconScale * usrScale * sysScale <= 4; + test(iconScale, usrScale, sysScale); + } + } + + private void test(int iconScale, int usrScale, int sysScale) { + JBUI.setUserScaleFactor(usrScale); + JBUI.ScaleContext ctx = JBUI.ScaleContext.create(SYS_SCALE.of(sysScale)/*, USR_SCALE.of(usrScale)*/); // USR_SCALE is set automatically + + String[] cellIconsPaths = getCellIconsPaths(); + int count = cellIconsPaths.length; + + IconLoader.CachedImageIcon[] cellIcons = new IconLoader.CachedImageIcon[count]; + for (int i = 0; i < count; i++) { + try { + cellIcons[i] = new IconLoader.CachedImageIcon(new File(cellIconsPaths[i]).toURI().toURL()); + } + catch (MalformedURLException e) { + throw new RuntimeException(e); + } + cellIcons[i].updateScaleContext(ctx.copy()); + } + + Icon scaledIcon = createCompositeIcon(cellIcons).scale(iconScale); + ctx.update(OBJ_SCALE.of(iconScale)); + test(scaledIcon, ctx); + } + + private void test(Icon icon, JBUI.ScaleContext ctx) { + Pair pair = createImageAndGraphics(ctx.getScale(SYS_SCALE), icon.getIconWidth(), icon.getIconHeight()); + BufferedImage iconImage = pair.first; + Graphics2D g2d = pair.second; + + icon.paintIcon(null, g2d, 0, 0); + + if (shouldSaveGoldImage()) saveImage(iconImage, getGoldImagePath((int)ctx.getScale(PIX_SCALE))); + + BufferedImage goldImage = loadImage(getGoldImagePath((int)ctx.getScale(PIX_SCALE))); + + ImageComparator.compareAndAssert( + new ImageComparator.AASmootherComparator(0.1, 0.1, new Color(0, 0, 0, 0)), goldImage, iconImage, null); + } + + protected abstract ScalableIcon createCompositeIcon(Icon... cellIcons); + + protected abstract String[] getCellIconsPaths(); + + protected abstract String getGoldImagePath(int scale); + + protected abstract boolean shouldSaveGoldImage(); +} diff --git a/platform/platform-tests/testSrc/com/intellij/util/ui/LayeredIconPaintTest.java b/platform/platform-tests/testSrc/com/intellij/util/ui/LayeredIconPaintTest.java index b4a71a541834..6409a01c3dbc 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/ui/LayeredIconPaintTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/ui/LayeredIconPaintTest.java @@ -1,99 +1,53 @@ // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.util.ui; -import com.intellij.openapi.util.IconLoader.CachedImageIcon; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.ScalableIcon; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.ui.LayeredIcon; import com.intellij.ui.RestoreScaleRule; -import com.intellij.util.ui.JBUI.ScaleContext; -import com.intellij.util.ui.paint.ImageComparator; -import com.intellij.util.ui.paint.ImageComparator.AASmootherComparator; import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.ExternalResource; import javax.swing.*; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.File; -import java.net.MalformedURLException; -import java.util.function.BiFunction; - -import static com.intellij.util.ui.JBUI.ScaleType.*; -import static com.intellij.util.ui.TestScaleHelper.*; /** * Tests {@link com.intellij.ui.LayeredIcon} painting. * * @author tav */ -public class LayeredIconPaintTest { +public class LayeredIconPaintTest extends CompositeIconPaintTestHelper { @ClassRule public static final ExternalResource manageState = new RestoreScaleRule(); @Test - public void test() throws MalformedURLException { - overrideJreHiDPIEnabled(true); - - BiFunction bit2scale = (mask, bit) -> ((mask >> bit) & 0x1) + 1; - - for (int mask=0; mask<7; mask++) { - int iconScale = bit2scale.apply(mask, 2); - int usrScale = bit2scale.apply(mask, 1); - int sysScale = bit2scale.apply(mask, 0); - assert iconScale * usrScale * sysScale <= 4; - test(iconScale, usrScale, sysScale); - } + public void test() { + super.test(); } - private void test(int iconScale, int usrScale, int sysScale) throws MalformedURLException { - JBUI.setUserScaleFactor(usrScale); - - CachedImageIcon icon1 = new CachedImageIcon(new File(getIcon1Path()).toURI().toURL()); - CachedImageIcon icon2 = new CachedImageIcon(new File(getIcon2Path()).toURI().toURL()); - - ScaleContext ctx = ScaleContext.create(SYS_SCALE.of(sysScale)/*, USR_SCALE.of(usrScale)*/); // USR_SCALE is set automatically - icon1.updateScaleContext(ctx.copy()); - icon2.updateScaleContext(ctx.copy()); - - Icon icon = createAndSetIcons(icon1, icon2).scale(iconScale); - ctx.update(OBJ_SCALE.of(iconScale)); - test(icon, ctx); - } - - protected ScalableIcon createAndSetIcons(Icon icon1, Icon icon2) { + @Override + protected ScalableIcon createCompositeIcon(Icon... cellIcons) { LayeredIcon icon = new LayeredIcon(2); - icon.setIcon(icon1, 0); - icon.setIcon(icon2, 1, JBUI.scale(10), JBUI.scale(6)); + icon.setIcon(cellIcons[0], 0); + icon.setIcon(cellIcons[1], 1, JBUI.scale(10), JBUI.scale(6)); return icon; } - private void test(Icon scaledIcon, ScaleContext ctx) { - Pair pair = createImageAndGraphics(ctx.getScale(SYS_SCALE), scaledIcon.getIconWidth(), scaledIcon.getIconHeight()); - BufferedImage iconImage = pair.first; - Graphics2D g2d = pair.second; - - scaledIcon.paintIcon(null, g2d, 0, 0); - - //saveImage(iconImage, getGoldImagePath((int)ctx.getScale(PIX_SCALE))); // uncomment to save gold image - - BufferedImage goldImage = loadImage(getGoldImagePath((int)ctx.getScale(PIX_SCALE))); - - ImageComparator.compareAndAssert( - new AASmootherComparator(0.1, 0.1, new Color(0, 0, 0, 0)), goldImage, iconImage, null); - } - + @Override protected String getGoldImagePath(int scale) { return PlatformTestUtil.getPlatformTestDataPath() + "ui/gold_LayeredIcon@" + scale + "x.png"; } - private static String getIcon1Path() { - return PlatformTestUtil.getPlatformTestDataPath() + "ui/db_set_breakpoint.png"; + @Override + protected boolean shouldSaveGoldImage() { + return false; } - private static String getIcon2Path() { - return PlatformTestUtil.getPlatformTestDataPath() + "ui/question_badge.png"; + @Override + protected String[] getCellIconsPaths() { + return new String[] { + PlatformTestUtil.getPlatformTestDataPath() + "ui/db_set_breakpoint.png", + PlatformTestUtil.getPlatformTestDataPath() + "ui/question_badge.png" + }; } } diff --git a/platform/platform-tests/testSrc/com/intellij/util/ui/OffsetIconPaintTest.java b/platform/platform-tests/testSrc/com/intellij/util/ui/OffsetIconPaintTest.java new file mode 100644 index 000000000000..01e4eb0af2d2 --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/util/ui/OffsetIconPaintTest.java @@ -0,0 +1,43 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.util.ui; + +import com.intellij.openapi.util.ScalableIcon; +import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.ui.OffsetIcon; +import org.junit.Test; + +import javax.swing.*; + +/** + * Tests {@link com.intellij.ui.OffsetIcon} painting. + * + * @author tav + */ +public class OffsetIconPaintTest extends CompositeIconPaintTestHelper { + @Test + public void test() { + super.test(); + } + + @Override + protected ScalableIcon createCompositeIcon(Icon... cellIcons) { + return new OffsetIcon(cellIcons[0].getIconWidth(), cellIcons[0]); + } + + @Override + protected String getGoldImagePath(int scale) { + return PlatformTestUtil.getPlatformTestDataPath() + "ui/gold_OffsetIcon@" + scale + "x.png"; + } + + @Override + protected boolean shouldSaveGoldImage() { + return false; + } + + @Override + protected String[] getCellIconsPaths() { + return new String[] { + PlatformTestUtil.getPlatformTestDataPath() + "ui/db_set_breakpoint.png" + }; + } +} diff --git a/platform/platform-tests/testSrc/com/intellij/util/ui/RowIconPaintTest.java b/platform/platform-tests/testSrc/com/intellij/util/ui/RowIconPaintTest.java index 32b3487abd61..ea599ae2f2a6 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/ui/RowIconPaintTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/ui/RowIconPaintTest.java @@ -4,10 +4,8 @@ package com.intellij.util.ui; import com.intellij.openapi.util.ScalableIcon; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.ui.RowIcon; -import org.junit.Test; import javax.swing.*; -import java.net.MalformedURLException; /** * Tests {@link com.intellij.ui.RowIcon} painting. @@ -15,17 +13,11 @@ import java.net.MalformedURLException; * @author tav */ public class RowIconPaintTest extends LayeredIconPaintTest { - @Test @Override - public void test() throws MalformedURLException { - super.test(); - } - - @Override - protected ScalableIcon createAndSetIcons(Icon icon1, Icon icon2) { + protected ScalableIcon createCompositeIcon(Icon... cellIcons) { RowIcon icon = new RowIcon(2); - icon.setIcon(icon1, 0); - icon.setIcon(icon2, 1); + icon.setIcon(cellIcons[0], 0); + icon.setIcon(cellIcons[1], 1); return icon; }