// Copyright 2000-2019 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;

import com.intellij.idea.HardwareAgentRequired;
import com.intellij.internal.IconsLoadTime;
import com.intellij.internal.IconsLoadTime.StatData;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.ui.icons.ImageType;
import com.intellij.util.ui.TestScaleHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static com.intellij.testFramework.PlatformTestUtil.assertTiming;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assume.assumeTrue;

/**
 * Estimates SVG icon average load time.
 *
 * @author tav
 */
@HardwareAgentRequired
public class IconsLoadTimePerformanceTest {
  private static final Logger LOG = Logger.getInstance(IconsLoadTimePerformanceTest.class);
  private static final int SVG_ICON_AVERAGE_LOAD_TIME_EXPECTED = 30; // ms
  private static final int SVG_ICON_QUORUM_COUNT = 50;

  // a list of icons for which we have SVG versions
  private static final String ICONS_LIST_PATH = PlatformTestUtil.getPlatformTestDataPath() + "icons/icons_list.txt";

  @Before
  public void setState() {
    TestScaleHelper.setSystemProperty("idea.measure.icon.load.time", "true");
  }

  @After
  public void restoreState() {
    TestScaleHelper.restoreSystemProperties();
    TestScaleHelper.restoreRegistryProperties();
  }

  @Test
  public void loadIcons() throws ClassNotFoundException, IOException {
    assertNotNull(Class.forName(IconsLoadTime.class.getName())); // force static init

    try (BufferedReader br = Files.newBufferedReader(Paths.get(ICONS_LIST_PATH))) {
      String iconPath;
      while ((iconPath = br.readLine()) != null) {
        ImageLoader.loadFromUrl(new File(PlatformTestUtil.getCommunityPath() + "/" + iconPath).toURI().toURL());
      }
    }
    StatData svgData = IconsLoadTime.getStatData(false, ImageType.SVG);

    assumeTrue("no SVG load statistics gathered", svgData != null);
    LOG.debug(String.valueOf(svgData));

    assumeTrue("too few icons loaded: " + svgData.count + "; expecting > " + SVG_ICON_QUORUM_COUNT,
               svgData.count >= SVG_ICON_QUORUM_COUNT);

    assertTiming("SVG icon load time raised to " + String.format("%.02fms", svgData.averageTime),
                 SVG_ICON_AVERAGE_LOAD_TIME_EXPECTED, (int)svgData.averageTime);
  }
}

@@ -446,16 +446,59 @@
 geType;%0A
+import com.intellij.ui.scale.ScaleContext;%0A
 import c
@@ -607,16 +607,51 @@
 .Test;%0A%0A
+import java.awt.image.ImageFilter;%0A
 import j
@@ -1241,12 +1241,103 @@
 CTED
- = 3
+_NO_CACHE = 100; // ms%0A  private static final int SVG_ICON_AVERAGE_LOAD_TIME_EXPECTED_CACHE = 5
 0; /
@@ -1682,16 +1682,96 @@
 true%22);%0A
+    TestScaleHelper.setSystemProperty(%22idea.ui.icons.svg.disk.cache%22, %22false%22);%0A
   %7D%0A%0A  @
@@ -1934,18 +1934,384 @@
 oid 
-loadIcons(
+test() throws IOException, ClassNotFoundException %7B%0A    TestScaleHelper.setSystemProperty(%22idea.ui.icons.svg.disk.cache%22, %22false%22);%0A    loadIcons(SVG_ICON_AVERAGE_LOAD_TIME_EXPECTED_NO_CACHE);%0A%0A    TestScaleHelper.setSystemProperty(%22idea.ui.icons.svg.disk.cache%22, %22true%22);%0A    loadIcons(SVG_ICON_AVERAGE_LOAD_TIME_EXPECTED_CACHE);%0A  %7D%0A%0A  public void loadIcons(int expectedTime
 ) th
@@ -2630,16 +2630,27 @@
 FromUrl(
+%0A          
 new File
@@ -2719,16 +2719,140 @@
 .toURL()
+,%0A          true,%0A          false, // do not use global cache%0A          new ImageFilter%5B%5D%7B%7D,%0A          ScaleContext.create()
 );%0A     
@@ -3318,43 +3318,20 @@
     
-SVG_ICON_AVERAGE_LOAD_TIME_EXPECTED
+expectedTime
 , (i

Improve IconsLoadTimePerformanceTest