Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,158 @@ public void testIsFittingForDistinguishesSize() {
}
}

@Test
public void testAddReturnsConsecutiveIndicesForConsecutiveImages() {
ImageList list = new ImageList(SWT.NONE, 16, 16, 100);
Image[] images = createImages(3);
try {
assertEquals(0, list.add(images[0]));
assertEquals(1, list.add(images[1]));
assertEquals(2, list.add(images[2]));
} finally {
disposeAll(list, images);
}
}

@Test
public void testAddReusesSlotOfRemovedImage() {
ImageList list = new ImageList(SWT.NONE, 16, 16, 100);
Image[] images = createImages(3);
try {
list.add(images[0]);
list.add(images[1]);
list.put(0, null);

assertEquals(0, list.add(images[2]));
assertSame(images[2], list.get(0));
} finally {
disposeAll(list, images);
}
}

@Test
public void testPutAppendsImageAtEndOfList() {
ImageList list = new ImageList(SWT.NONE, 16, 16, 100);
Image[] images = createImages(2);
try {
list.add(images[0]);

list.put(1, images[1]);

assertSame(images[1], list.get(1));
assertEquals(2, list.size());
} finally {
disposeAll(list, images);
}
}

@Test
public void testPutReplacesImageInsideList() {
ImageList list = new ImageList(SWT.NONE, 16, 16, 100);
Image[] images = createImages(3);
try {
list.add(images[0]);
list.add(images[1]);

list.put(0, images[2]);

assertSame(images[2], list.get(0));
assertEquals(2, list.size());
} finally {
disposeAll(list, images);
}
}

@Test
public void testPutWithoutImageClearsSlotInsideList() {
ImageList list = new ImageList(SWT.NONE, 16, 16, 100);
Image[] images = createImages(2);
try {
list.add(images[0]);
list.add(images[1]);

list.put(0, null);

assertNull(list.get(0));
assertSame(images[1], list.get(1));
assertEquals(1, list.size());
} finally {
disposeAll(list, images);
}
}

@Test
public void testPutBeyondEndOfListIsIgnored() {
ImageList list = new ImageList(SWT.NONE, 16, 16, 100);
Image[] images = createImages(2);
try {
list.add(images[0]);

list.put(2, images[1]);

assertEquals(1, list.size());
} finally {
disposeAll(list, images);
}
}

@Test
public void testPutNegativeIndexIsIgnored() {
ImageList list = new ImageList(SWT.NONE, 16, 16, 100);
Image[] images = createImages(2);
try {
list.add(images[0]);

list.put(-1, images[1]);

assertSame(images[0], list.get(0));
assertEquals(1, list.size());
} finally {
disposeAll(list, images);
}
}

/**
* Tool bars address their normal, hot and disabled image list with a single
* index per item, so an image must be storable at a given index instead of at
* whatever slot the individual list happens to have free.
*/
@Test
public void testPutKeepsListsAlignedWhenTheirFreeSlotsDiffer() {
ImageList list = new ImageList(SWT.NONE, 16, 16, 100);
ImageList hotList = new ImageList(SWT.NONE, 16, 16, 100);
Image[] images = createImages(4);
try {
list.add(images[0]);
hotList.add(images[1]);
// only the first list has a free slot from here on
list.put(0, null);

int index = list.add(images[2]);
hotList.put(index, images[3]);

assertEquals(0, index);
assertSame(images[2], list.get(index));
assertSame(images[3], hotList.get(index));
} finally {
hotList.dispose();
disposeAll(list, images);
}
}

private static Image[] createImages(int count) {
Image[] images = new Image[count];
for (int i = 0; i < count; i++) {
images[i] = new Image(Display.getDefault(), 16, 16);
}
return images;
}

private static void disposeAll(ImageList list, Image[] images) {
list.dispose();
for (Image image : images) {
image.dispose();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public int add (Image image) {
if (imageAtIndex == null) break;
index++;
}
put (index, image);
return index;
}

private void append (int index, Image image, int count) {
if (count == 0) {
Rectangle bounds = image.getBounds();
width = bounds.width;
Expand All @@ -68,7 +73,6 @@ public int add (Image image) {
images = newImages;
}
images [index] = image;
return index;
}

private Image getOrClearIfDisposed(int index) {
Expand Down Expand Up @@ -386,9 +390,19 @@ public int indexOf (Image image) {
return -1;
}

/**
* Stores the given image at the given index, replacing whatever is stored at that index. Passing
* no image clears the index. The index may also address the slot right after the last one, in
* which case a new slot is appended for the given image. Nothing happens for any other index
* outside the list's current size.
*/
public void put (int index, Image image) {
if ((0 <= index && index < images.length) && (images [index] == image)) return;
int count = OS.ImageList_GetImageCount (handle);
if (index == count && image != null) {
append (index, image, count);
return;
}
if (!(0 <= index && index < count)) return;
if (image != null) setForAllHandles(index, image, count);
images [index] = image;
Expand Down
Loading
Loading