Added
Link Here
|
1 |
package org.eclipse.ua.tests.forms.util; |
2 |
|
3 |
import org.eclipse.swt.graphics.Color; |
4 |
import org.eclipse.swt.graphics.Image; |
5 |
import org.eclipse.swt.widgets.Display; |
6 |
import org.eclipse.ui.internal.forms.widgets.FormImages; |
7 |
|
8 |
import junit.framework.Assert; |
9 |
import junit.framework.TestCase; |
10 |
|
11 |
public class FormImagesTests extends TestCase { |
12 |
public void testSingleton() { |
13 |
Display display = Display.getCurrent(); |
14 |
FormImages instance = FormImages.getInstance(); |
15 |
// ensure the singleton is returning the same instance |
16 |
Assert.assertTrue("getInstance() returned a different FormImages instance", instance.equals(FormImages.getInstance())); |
17 |
Image gradient = instance.getGradient(display, new Color(display, 1, 1, 1), new Color(display, 7, 7, 7), 21, 21, 0); |
18 |
instance.markFinished(gradient); |
19 |
// ensure the singleton is returning the same instance after creating and disposing one gradient |
20 |
Assert.assertTrue("getInstance() returned a different FormImages instance after creation and disposal of one image", instance.equals(FormImages.getInstance())); |
21 |
} |
22 |
|
23 |
public void testDisposeOne() { |
24 |
Display display = Display.getCurrent(); |
25 |
Image gradient = FormImages.getInstance().getGradient(display, new Color(display, 255, 255, 255), new Color(display, 0, 0, 0), 21, 21, 0); |
26 |
FormImages.getInstance().markFinished(gradient); |
27 |
// ensure that getting a single gradient and marking it as finished disposed it |
28 |
Assert.assertTrue("markFinished(...) did not dispose an image after a single getGradient()", gradient.isDisposed()); |
29 |
} |
30 |
|
31 |
public void testMultipleInstances() { |
32 |
Display display = Display.getCurrent(); |
33 |
Image gradient = FormImages.getInstance().getGradient(display, new Color(display, 200, 200, 200), new Color(display, 0, 0, 0), 30, 16, 3); |
34 |
int count; |
35 |
// ensure that the same image is returned for many calls with the same parameter |
36 |
for (count = 1; count < 20; count ++) |
37 |
Assert.assertEquals("getGradient(...) returned a different image for the same params on iteration "+count, |
38 |
gradient, FormImages.getInstance().getGradient(display, new Color(display, 200, 200, 200), new Color(display, 0, 0, 0), 30, 16, 3)); |
39 |
for ( ;count > 0; count--) { |
40 |
FormImages.getInstance().markFinished(gradient); |
41 |
if (count != 1) |
42 |
// ensure that the gradient is not disposed early |
43 |
Assert.assertFalse("markFinished(...) disposed a shared image early on iteration "+count,gradient.isDisposed()); |
44 |
else |
45 |
// ensure that the gradient is disposed on the last markFinished |
46 |
Assert.assertTrue("markFinished(...) did not dispose a shared image on the last call",gradient.isDisposed()); |
47 |
} |
48 |
} |
49 |
|
50 |
public void testMultipleUniqueInstances() { |
51 |
Display display = Display.getCurrent(); |
52 |
Image[] images = new Image[10]; |
53 |
images[0] = FormImages.getInstance().getGradient(display, new Color(display, 1, 0, 0), new Color(display, 100, 100, 100), 25, 23, 1); |
54 |
images[1] = FormImages.getInstance().getGradient(display, new Color(display, 0, 1, 0), new Color(display, 100, 100, 100), 25, 23, 1); |
55 |
images[2] = FormImages.getInstance().getGradient(display, new Color(display, 0, 0, 1), new Color(display, 100, 100, 100), 25, 23, 1); |
56 |
images[3] = FormImages.getInstance().getGradient(display, new Color(display, 0, 0, 0), new Color(display, 101, 100, 100), 25, 23, 1); |
57 |
images[4] = FormImages.getInstance().getGradient(display, new Color(display, 0, 0, 0), new Color(display, 100, 101, 100), 25, 23, 1); |
58 |
images[5] = FormImages.getInstance().getGradient(display, new Color(display, 0, 0, 0), new Color(display, 100, 100, 101), 25, 23, 1); |
59 |
images[6] = FormImages.getInstance().getGradient(display, new Color(display, 0, 0, 0), new Color(display, 100, 100, 100), 20, 23, 1); |
60 |
images[7] = FormImages.getInstance().getGradient(display, new Color(display, 0, 0, 0), new Color(display, 100, 100, 100), 25, 10, 1); |
61 |
images[8] = FormImages.getInstance().getGradient(display, new Color(display, 0, 0, 0), new Color(display, 100, 100, 100), 25, 23, 2); |
62 |
images[9] = FormImages.getInstance().getGradient(display, new Color(display, 1, 1, 1), new Color(display, 101, 101, 101), 20, 10, 2); |
63 |
// ensure none of the images are the same |
64 |
for (int i = 0; i < images.length - 1; i++) { |
65 |
for (int j = i+1; j < images.length; j++) { |
66 |
Assert.assertNotSame("getGradient(...) returned the same image for different parameters: i = " + i + "; j = " + j, images[i], images[j]); |
67 |
} |
68 |
} |
69 |
// ensure all of the images are disposed with one call to markFinished |
70 |
for (int i = 0; i < images.length; i++) { |
71 |
FormImages.getInstance().markFinished(images[i]); |
72 |
Assert.assertTrue("markFinished(...) did not dispose an image that was only requested once: i = " + i, images[i].isDisposed()); |
73 |
} |
74 |
} |
75 |
} |