Refactoring in ui scale tests

This commit is contained in:
Anton Tarasov
2018-03-23 13:12:06 +03:00
parent c171770816
commit 5cfcf6bc53
4 changed files with 33 additions and 55 deletions
@@ -5,8 +5,6 @@ package com.intellij.util;
import com.intellij.internal.IconsLoadTime;
import com.intellij.internal.IconsLoadTime.StatData;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.registry.RegistryValue;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.ui.TestScaleHelper;
import org.junit.After;
@@ -32,14 +30,16 @@ public class IconsLoadTimeTest {
// a list of icons for which we have SVG versions
private static final String ICONS_LIST_PATH = PlatformTestUtil.getPlatformTestDataPath() + "icons/icons_list.txt";
private static boolean initialSvgProp;
@Before
public void setState() {
TestScaleHelper.setProperty("idea.is.internal", "true");
RegistryValue rv = Registry.get("ide.svg.icon");
initialSvgProp = rv.asBoolean();
if (!initialSvgProp) rv.setValue(true);
TestScaleHelper.setSystemProperty("idea.is.internal", "true");
TestScaleHelper.setRegistryProperty("ide.svg.icon", "true");
}
@After
public void restoreState() {
TestScaleHelper.restoreSystemProperties();
TestScaleHelper.restoreRegistryProperties();
}
@Test
@@ -63,10 +63,4 @@ public class IconsLoadTimeTest {
assertTiming("SVG icon load time raised to " + String.format("%.02fms", svgData.averageTime),
SVG_ICON_AVERAGE_LOAD_TIME_EXPECTED, (int)svgData.averageTime);
}
@After
public void restoreState() {
TestScaleHelper.restoreProperties();
Registry.get("ide.svg.icon").setValue(initialSvgProp);
}
}
@@ -3,13 +3,10 @@ package com.intellij.util.ui;
import com.intellij.openapi.util.IconLoader.CachedImageIcon;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.registry.RegistryValue;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.IconUtil;
import com.intellij.util.ui.JBUI.ScaleContext;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -26,25 +23,11 @@ import static com.intellij.util.ui.JBUI.ScaleType.USR_SCALE;
* @author tav
*/
public class IconScaleTest extends TestScaleHelper {
private static boolean initialSvgProp;
@Before
@Override
public void setState() {
super.setState();
RegistryValue rv = Registry.get("ide.svg.icon");
initialSvgProp = rv.asBoolean();
rv.setValue(true);
}
@After
@Override
public void restoreState() {
super.restoreState();
Registry.get("ide.svg.icon").setValue(initialSvgProp);
setRegistryProperty("ide.svg.icon", "true");
}
@Test
@@ -2,14 +2,11 @@
package com.intellij.util.ui;
import com.intellij.openapi.util.IconLoader.CachedImageIcon;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.registry.RegistryValue;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.IconUtil;
import com.intellij.util.ImageLoader;
import com.intellij.util.ui.JBUI.ScaleContext;
import com.intellij.util.ui.paint.ImageComparator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -27,22 +24,11 @@ import static junit.framework.TestCase.assertTrue;
* @author tav
*/
public class SvgIconPaintTest extends TestScaleHelper {
private static boolean initialSvgProp;
@Before
@Override
public void setState() {
super.setState();
RegistryValue rv = Registry.get("ide.svg.icon");
initialSvgProp = rv.asBoolean();
if (!initialSvgProp) rv.setValue(true);
}
@After
@Override
public void restoreState() {
super.restoreState();
Registry.get("ide.svg.icon").setValue(initialSvgProp);
setRegistryProperty("ide.svg.icon", "true");
}
@Test
@@ -56,8 +42,7 @@ public class SvgIconPaintTest extends TestScaleHelper {
//save(iconImage);
BufferedImage goldImage = load();
ImageComparator comparator =
new ImageComparator(new ImageComparator.ColorAASmoother(0, 0.3f));
ImageComparator comparator = new ImageComparator(new ImageComparator.ColorAASmoother(0, 0.3f));
StringBuilder sb = new StringBuilder("images mismatch: ");
assertTrue(sb.toString(), comparator.compare(iconImage, goldImage, sb));
}
@@ -1,6 +1,8 @@
// 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.registry.Registry;
import com.intellij.openapi.util.registry.RegistryValue;
import com.intellij.util.SystemProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -20,7 +22,8 @@ import java.util.Map;
public class TestScaleHelper {
private static final String STANDALONE_PROP = "intellij.test.standalone";
private static final Map<String, String> origProps = new HashMap<>();
private static final Map<String, String> originalSysProps = new HashMap<>();
private static final Map<String, String> originalRegProps = new HashMap<>();
private float originalUserScale;
private boolean originalJreHiDPIEnabled;
@@ -35,11 +38,18 @@ public class TestScaleHelper {
public void restoreState() {
JBUI.setUserScaleFactor(originalUserScale);
overrideJreHiDPIEnabled(originalJreHiDPIEnabled);
restoreProperties();
restoreRegistryProperties();
restoreSystemProperties();
}
public static void setProperty(@NotNull String name, @Nullable String value) {
origProps.put(name, System.getProperty(name));
public static void setRegistryProperty(@NotNull String key, @NotNull String value) {
final RegistryValue prop = Registry.get(key);
originalRegProps.put(key, prop.asString());
prop.setValue(value);
}
public static void setSystemProperty(@NotNull String name, @Nullable String value) {
originalSysProps.put(name, System.getProperty(name));
_setProperty(name, value);
}
@@ -52,12 +62,18 @@ public class TestScaleHelper {
}
}
public static void restoreProperties() {
for (Map.Entry<String, String> entry : origProps.entrySet()) {
public static void restoreSystemProperties() {
for (Map.Entry<String, String> entry : originalSysProps.entrySet()) {
_setProperty(entry.getKey(), entry.getValue());
}
}
public static void restoreRegistryProperties() {
for (Map.Entry<String, String> entry : originalRegProps.entrySet()) {
Registry.get(entry.getKey()).setValue(entry.getValue());
}
}
public static void overrideJreHiDPIEnabled(boolean enabled) {
UIUtil.test_jreHiDPI().set(enabled);
}