Skip to content

Commit 6de466f

Browse files
Copilotlaeubi
authored andcommitted
Add tests for file URL query parameters in icon size hints
1 parent 4c65f01 commit 6de466f

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/images/URLHintProviderTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
import static org.junit.jupiter.api.Assertions.assertEquals;
1717
import static org.junit.jupiter.api.Assertions.assertNotNull;
1818

19+
import java.io.File;
20+
import java.io.IOException;
1921
import java.net.URL;
22+
import java.nio.file.Files;
23+
import java.nio.file.StandardCopyOption;
2024

2125
import org.eclipse.jface.resource.ImageDescriptor;
2226
import org.eclipse.swt.graphics.ImageData;
@@ -199,4 +203,71 @@ public void testQueryParameterWithMultipleParams() throws Exception {
199203
assertEquals(24, imageData.height, "Height should be 24 from query parameter");
200204
}
201205

206+
/**
207+
* Tests that file: URLs can have query parameters for size hints.
208+
* This is important because file URLs need special handling - the query
209+
* parameter should be used for size detection but stripped when accessing
210+
* the actual file.
211+
*/
212+
@Test
213+
public void testFileURLWithQueryParameter() throws IOException {
214+
// Copy test SVG to a temporary file
215+
URL resourceUrl = URLHintProviderTest.class.getResource("/icons/imagetests/test-icon.svg");
216+
assertNotNull(resourceUrl, "Test SVG not found");
217+
218+
File tempSvg = File.createTempFile("test-icon", ".svg");
219+
try {
220+
Files.copy(resourceUrl.openStream(), tempSvg.toPath(), StandardCopyOption.REPLACE_EXISTING);
221+
222+
// Create file: URL with query parameter
223+
URL fileUrl = tempSvg.toURI().toURL();
224+
String fileUrlWithQuery = fileUrl.toExternalForm() + "?size=16x16";
225+
URL fileUrlQuery = new URL(fileUrlWithQuery);
226+
227+
// Verify URL has file protocol and query parameter
228+
assertEquals("file", fileUrlQuery.getProtocol(), "Should be a file URL");
229+
assertEquals("size=16x16", fileUrlQuery.getQuery(), "Query parameter should be preserved");
230+
231+
// Test that ImageDescriptor can load the image with query parameter
232+
ImageDescriptor descriptor = ImageDescriptor.createFromURL(fileUrlQuery);
233+
ImageData imageData = descriptor.getImageData(100);
234+
235+
assertNotNull(imageData, "ImageData should not be null for file URL with query parameter");
236+
assertEquals(16, imageData.width, "Width should be 16 based on query parameter");
237+
assertEquals(16, imageData.height, "Height should be 16 based on query parameter");
238+
} finally {
239+
tempSvg.delete();
240+
}
241+
}
242+
243+
/**
244+
* Tests that file: URLs with query parameters work at different zoom levels.
245+
*/
246+
@Test
247+
public void testFileURLWithQueryParameterZoom() throws IOException {
248+
// Copy test SVG to a temporary file
249+
URL resourceUrl = URLHintProviderTest.class.getResource("/icons/imagetests/test-icon.svg");
250+
assertNotNull(resourceUrl, "Test SVG not found");
251+
252+
File tempSvg = File.createTempFile("test-icon", ".svg");
253+
try {
254+
Files.copy(resourceUrl.openStream(), tempSvg.toPath(), StandardCopyOption.REPLACE_EXISTING);
255+
256+
// Create file: URL with query parameter
257+
URL fileUrl = tempSvg.toURI().toURL();
258+
String fileUrlWithQuery = fileUrl.toExternalForm() + "?size=16x16";
259+
URL fileUrlQuery = new URL(fileUrlWithQuery);
260+
261+
// Test at 200% zoom
262+
ImageDescriptor descriptor = ImageDescriptor.createFromURL(fileUrlQuery);
263+
ImageData imageData = descriptor.getImageData(200);
264+
265+
assertNotNull(imageData, "ImageData should not be null");
266+
assertEquals(32, imageData.width, "Width should be 32 (16*2) at 200% zoom");
267+
assertEquals(32, imageData.height, "Height should be 32 (16*2) at 200% zoom");
268+
} finally {
269+
tempSvg.delete();
270+
}
271+
}
272+
202273
}

0 commit comments

Comments
 (0)