1package com.xtremelabs.robolectric.shadows;
2
3import com.google.android.maps.GeoPoint;
4import com.google.android.maps.ItemizedOverlay;
5import com.google.android.maps.OverlayItem;
6import com.xtremelabs.robolectric.WithTestDefaultsRunner;
7import org.junit.Test;
8import org.junit.runner.RunWith;
9
10import static org.junit.Assert.assertEquals;
11import static org.junit.Assert.assertNull;
12import static org.junit.Assert.assertTrue;
13
14@RunWith(WithTestDefaultsRunner.class)
15public class ItemizedOverlayTest {
16
17    class TestItemizedOverlay extends ItemizedOverlay<OverlayItem> {
18        public OverlayItem firstOverlayItem = new OverlayItem(new GeoPoint(0, 0), "title1", "snippet1");
19        public OverlayItem secondOverlayItem = new OverlayItem(new GeoPoint(5, 5), "title2", "snippet2");
20
21        public TestItemizedOverlay() {
22            super(null);
23        }
24
25        @Override
26        protected OverlayItem createItem(int index) {
27            if (index == 0) {
28                return firstOverlayItem;
29            } else if (index == 1) {
30                return secondOverlayItem;
31            }
32            return null;
33        }
34
35        @Override
36        public int size() {
37            return 2;
38        }
39
40        public void callPopulate() {
41            populate();
42        }
43    }
44
45    @Test
46    public void populateShouldCreateItems() {
47        TestItemizedOverlay itemizedOverlay = new TestItemizedOverlay();
48        itemizedOverlay.callPopulate();
49
50        assertEquals(itemizedOverlay.firstOverlayItem, itemizedOverlay.getItem(0));
51        assertEquals(itemizedOverlay.secondOverlayItem, itemizedOverlay.getItem(1));
52    }
53
54    @Test
55    public void callingPopulateTwoTimesShouldNotAddAdditionalItems() {
56        TestItemizedOverlay itemizedOverlay = new TestItemizedOverlay();
57        itemizedOverlay.callPopulate();
58        itemizedOverlay.callPopulate();
59
60        assertEquals(itemizedOverlay.firstOverlayItem, itemizedOverlay.getItem(0));
61        assertEquals(itemizedOverlay.secondOverlayItem, itemizedOverlay.getItem(1));
62
63        boolean indexOutOfBoundsExceptionCatched = false;
64        try {
65            itemizedOverlay.getItem(2);
66        } catch (IndexOutOfBoundsException e) {
67            indexOutOfBoundsExceptionCatched = true;
68        }
69        assertTrue(indexOutOfBoundsExceptionCatched);
70    }
71
72    @Test(expected = IndexOutOfBoundsException.class)
73    public void getItemWithoutPopulateShouldThrowIndexOutOfBoundException() {
74        TestItemizedOverlay itemizedOverlay = new TestItemizedOverlay();
75
76        assertNull(itemizedOverlay.getItem(0));
77    }
78}
79