1 // 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.
2 package org.intellij.images.index;
4 import com.intellij.openapi.vfs.VfsUtil;
5 import com.intellij.openapi.vfs.VirtualFile;
6 import com.intellij.testFramework.fixtures.BasePlatformTestCase;
7 import com.intellij.util.indexing.FileBasedIndex;
8 import org.intellij.images.util.ImageInfo;
9 import org.jetbrains.annotations.NotNull;
11 import java.io.IOException;
13 import static org.junit.Assert.assertNotEquals;
15 public class ImageInfoIndexTest extends BasePlatformTestCase {
16 public void testIndexModification() throws IOException {
17 VirtualFile file = myFixture.addFileToProject("image.svg", "<svg width='300' height='300' xmlns='http://www.w3.org/2000/svg'></svg>").getVirtualFile();
18 long stamp = getIndexStamp();
19 ImageInfo value = getIndexValue(file);
21 VfsUtil.saveText(file, "<svg width='500' height='300' xmlns='http://www.w3.org/2000/svg'></svg>");
22 assertNotEquals(stamp, getIndexStamp());
23 assertNotEquals(value, getIndexValue(file));
24 stamp = getIndexStamp();
25 value = getIndexValue(file);
27 VfsUtil.saveText(file, "<svg width='500' height='300' xmlns='http://www.w3.org/2000/svg'><path d=\"M10 10\"/></svg>");
28 assertEquals(stamp, getIndexStamp());
29 assertEquals(value, getIndexValue(file));
32 public void testIndexingSameImages() throws IOException {
33 String text = "<svg width='300' height='300' xmlns='http://www.w3.org/2000/svg'></svg>";
34 VirtualFile file1 = myFixture.addFileToProject("image1.svg", text).getVirtualFile();
35 VirtualFile file2 = myFixture.addFileToProject("image2.svg", text).getVirtualFile();
37 assertEquals(getIndexValue(file1), getIndexValue(file2));
38 assertEquals(300, getIndexValue(file1).width);
39 assertEquals(300, getIndexValue(file2).width);
41 VfsUtil.saveText(file1, "<svg width='500' height='300' xmlns='http://www.w3.org/2000/svg'></svg>");
42 assertEquals(500, getIndexValue(file1).width);
43 assertEquals(300, getIndexValue(file2).width);
46 private long getIndexStamp() {
47 return FileBasedIndex.getInstance().getIndexModificationStamp(ImageInfoIndex.INDEX_ID, myFixture.getProject());
50 private ImageInfo getIndexValue(@NotNull VirtualFile file) {
51 return ImageInfoIndex.getInfo(file, myFixture.getProject());
55 protected boolean isWriteActionRequired() {