// Copyright 2000-2020 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.openapi.roots.impl;

import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.openapi.vfs.VirtualFileWithId;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DirectoryIndexLightTest extends BasePlatformTestCase {

  public void testAccessPerformance() throws IOException {
    VirtualFile fsRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///");
    List<VirtualFile> files = new ArrayList<>();
    WriteAction.run(() -> {
      for (int i = 0; i < 100; i++) {
        VirtualFile directory = myFixture.getTempDirFixture().findOrCreateDir("dir" + i);
        for (int j = 0; j < 50; j++) {
          directory = directory.createChildDirectory(this, "subDir");
        }
        for (int j = 0; j < 50; j++) {
          files.add(directory.createChildData(this, "file" + j));
        }
      }
    });

    List<LightVirtualFile> filesWithoutId = Arrays.asList(new MyVirtualFile1(), new MyVirtualFile2(), new MyVirtualFile3());

    ProjectFileIndex index = ProjectFileIndex.getInstance(getProject());

    PlatformTestUtil.startPerformanceTest("Directory index query", 2500, () -> {
      for (int i = 0; i < 100; i++) {
        assertFalse(index.isInContent(fsRoot));
        for (LightVirtualFile file : filesWithoutId) {
          assertFalse(file instanceof VirtualFileWithId);
          assertFalse(index.isInContent(file));
          assertFalse(index.isInSource(file));
          assertFalse(index.isInLibrary(file));
        }
        for (VirtualFile file : files) {
          assertTrue(index.isInContent(file));
          assertTrue(index.isInSource(file));
          assertFalse(index.isInLibrary(file));
        }
      }
    }).reattemptUntilJitSettlesDown().assertTiming();
  }

  private static class MyVirtualFile1 extends LightVirtualFile {
  }

  private static class MyVirtualFile2 extends LightVirtualFile {
  }

  private static class MyVirtualFile3 extends LightVirtualFile {
  }

}

@@ -1381,68 +1381,226 @@
     
-List%3CLightVirtualFile%3E filesWithout
+VirtualFile noId1 = new LightVirtualFile();%0A    VirtualFile no
 Id
+2
  = 
-Arrays.asList(new My
+new LightVirtualFile() %7B%0A      @Override%0A      public VirtualFile getParent() %7B%0A        return noId1;%0A      %7D%0A    %7D;%0A    VirtualFile noId3 = new Light
 Virt
@@ -1610,55 +1610,177 @@
 File
-1
 ()
-, new MyVirtualFile2(), new MyVirtualFile3()
+ %7B%0A      @Override%0A      public VirtualFile getParent() %7B%0A        return noId2;%0A      %7D%0A    %7D;%0A    List%3CVirtualFile%3E filesWithoutId = Arrays.asList(noId1, noId2, noId3
 );%0A%0A
@@ -2033,21 +2033,16 @@
    for (
-Light
 VirtualF
@@ -2477,24 +2477,24 @@
   %7D%0A      %7D%0A
+
     %7D).reatt
@@ -2544,216 +2544,6 @@
  %7D%0A%0A
-  private static class MyVirtualFile1 extends LightVirtualFile %7B%0A  %7D%0A%0A  private static class MyVirtualFile2 extends LightVirtualFile %7B%0A  %7D%0A%0A  private static class MyVirtualFile3 extends LightVirtualFile %7B%0A  %7D%0A%0A
 %7D%0A

make DirectoryIndexLightTest more interesting: give files without id some parent hierarchy