From eb6f92bd1e350bb968d9ed8f9ab6a6a70c7fe0be Mon Sep 17 00:00:00 2001 From: Anton Tarasov Date: Thu, 19 Apr 2018 13:53:25 +0300 Subject: [PATCH] Add OffsetIconPaintTest --- .../testData/ui/gold_OffsetIcon@1x.png | Bin 0 -> 345 bytes .../testData/ui/gold_OffsetIcon@2x.png | Bin 0 -> 641 bytes .../testData/ui/gold_OffsetIcon@4x.png | Bin 0 -> 2798 bytes .../util/ui/CompositeIconPaintTestHelper.java | 88 ++++++++++++++++++ .../util/ui/LayeredIconPaintTest.java | 80 ++++------------ .../intellij/util/ui/OffsetIconPaintTest.java | 43 +++++++++ .../intellij/util/ui/RowIconPaintTest.java | 14 +-- 7 files changed, 151 insertions(+), 74 deletions(-) create mode 100644 platform/platform-tests/testData/ui/gold_OffsetIcon@1x.png create mode 100644 platform/platform-tests/testData/ui/gold_OffsetIcon@2x.png create mode 100644 platform/platform-tests/testData/ui/gold_OffsetIcon@4x.png create mode 100644 platform/platform-tests/testSrc/com/intellij/util/ui/CompositeIconPaintTestHelper.java create mode 100644 platform/platform-tests/testSrc/com/intellij/util/ui/OffsetIconPaintTest.java 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 0000000000000000000000000000000000000000..e821f9402fc854bbf18774207e5972d657f8a37c GIT binary patch literal 345 zcmV-f0jBmP#Dl9d;(#|5f^EV+{z{r_Qk1~7AFfiZ{;5(CMB)KHY0L7C`! rOhE7T7{9wW;sbtwFo+EjL(X6TG8i>YG=`SN@Ny@35bRbYBVUu7^9Y^QtG|>3h_y_WS7Hc2?4p1n`jx@ z{&$y>+*mnG&F)lvH*@&u>Fu7GCY6e@xtXamZpN@(DZV&T>@fSxur9?{M;hB0`!+ju zK9#fv!`aYJ%pZmv%cN%EI<@agYL#wMWBzK;7mP`9fVo(c2;=KnanX_HPZ$SdcULR) zV{45DA2tqwK8#^bw2`4JO*3B4OJe~Zb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 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; }