|
21 | 21 | import static org.junit.Assert.assertNotSame; |
22 | 22 | import static org.junit.Assert.assertNull; |
23 | 23 |
|
| 24 | +import java.io.File; |
24 | 25 | import java.io.IOException; |
25 | 26 | import java.net.URL; |
26 | 27 |
|
27 | 28 | import org.eclipse.core.runtime.Adapters; |
28 | 29 | import org.eclipse.core.runtime.IPath; |
| 30 | +import org.eclipse.jface.internal.InternalPolicy; |
29 | 31 | import org.eclipse.jface.resource.ImageDescriptor; |
30 | 32 | import org.eclipse.swt.graphics.Image; |
31 | 33 | import org.eclipse.swt.graphics.ImageData; |
@@ -122,20 +124,52 @@ public void testImageFileNameProviderGetxName() { |
122 | 124 |
|
123 | 125 | @Test |
124 | 126 | public void testImageFileNameProviderGetxName_forFileURL() throws IOException { |
125 | | - URL imageFileURL = tempFolder.newFile("image.png").toURI().toURL(); |
126 | | - tempFolder.newFile("image@2x.png"); |
| 127 | + testImageFileNameProviderGetxName_forFileURL(true); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testImageFileNameProviderGetxName_forFileURL_noOSGi() throws IOException { |
| 132 | + testImageFileNameProviderGetxName_forFileURL(false); |
| 133 | + } |
| 134 | + |
| 135 | + private void testImageFileNameProviderGetxName_forFileURL(boolean osgiAvailable) throws IOException { |
| 136 | + boolean oldOsgiAvailable = InternalPolicy.OSGI_AVAILABLE; |
| 137 | + InternalPolicy.OSGI_AVAILABLE = osgiAvailable; |
| 138 | + try { |
| 139 | + URL imageFileURL = tempFolder.newFile("image.png").toURI().toURL(); |
| 140 | + tempFolder.newFile("image@2x.png"); |
| 141 | + ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageFileURL); |
| 142 | + |
| 143 | + ImageFileNameProvider fileNameProvider = Adapters.adapt(descriptor, ImageFileNameProvider.class); |
| 144 | + assertNotNull("URLImageDescriptor does not adapt to ImageFileNameProvider", fileNameProvider); |
| 145 | + String imagePath100 = fileNameProvider.getImagePath(100); |
| 146 | + assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the 100% path", imagePath100); |
| 147 | + assertEquals(IPath.fromOSString(imagePath100).lastSegment(), "image.png"); |
| 148 | + String imagePath200 = fileNameProvider.getImagePath(200); |
| 149 | + assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the @2x path", imagePath200); |
| 150 | + assertEquals(IPath.fromOSString(imagePath200).lastSegment(), "image@2x.png"); |
| 151 | + String imagePath150 = fileNameProvider.getImagePath(150); |
| 152 | + assertNull("URLImageDescriptor's ImageFileNameProvider does return a @1.5x path", imagePath150); |
| 153 | + } finally { |
| 154 | + InternalPolicy.OSGI_AVAILABLE = oldOsgiAvailable; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testImageFileNameProviderGetxName_forFileURL_WhiteSpace() throws IOException { |
| 160 | + File imageFolder = tempFolder.newFolder("folder with spaces"); |
| 161 | + File imageFile = new File(imageFolder, "image with spaces.png"); |
| 162 | + imageFile.createNewFile(); |
| 163 | + |
| 164 | + // This is an invalid URL because the whitespace characters are not properly encoded |
| 165 | + URL imageFileURL = new URL("file", null, imageFile.getPath()); |
127 | 166 | ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageFileURL); |
128 | 167 |
|
129 | 168 | ImageFileNameProvider fileNameProvider = Adapters.adapt(descriptor, ImageFileNameProvider.class); |
130 | 169 | assertNotNull("URLImageDescriptor does not adapt to ImageFileNameProvider", fileNameProvider); |
| 170 | + |
131 | 171 | String imagePath100 = fileNameProvider.getImagePath(100); |
132 | 172 | assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the 100% path", imagePath100); |
133 | | - assertEquals(IPath.fromOSString(imagePath100).lastSegment(), "image.png"); |
134 | | - String imagePath200 = fileNameProvider.getImagePath(200); |
135 | | - assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the @2x path", imagePath200); |
136 | | - assertEquals(IPath.fromOSString(imagePath200).lastSegment(), "image@2x.png"); |
137 | | - String imagePath150 = fileNameProvider.getImagePath(150); |
138 | | - assertNull("URLImageDescriptor's ImageFileNameProvider does return a @1.5x path", imagePath150); |
139 | 173 | } |
140 | 174 |
|
141 | 175 | @Test |
|
0 commit comments