Add OffsetIconPaintTest

This commit is contained in:
Anton Tarasov
2018-04-19 13:55:49 +03:00
parent 2d3eb9a23f
commit eb6f92bd1e
7 changed files with 151 additions and 74 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 345 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 641 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

@@ -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<Integer, Integer, Integer> 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<BufferedImage, Graphics2D> 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();
}
@@ -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<Integer, Integer, Integer> 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<BufferedImage, Graphics2D> 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"
};
}
}
@@ -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"
};
}
}
@@ -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;
}