1 package com.xtremelabs.robolectric.shadows; 2 3 import android.graphics.drawable.Drawable; 4 import com.google.android.maps.ItemizedOverlay; 5 import com.google.android.maps.OverlayItem; 6 import com.google.android.maps.ShadowItemizedOverlayBridge; 7 import com.xtremelabs.robolectric.internal.Implementation; 8 import com.xtremelabs.robolectric.internal.Implements; 9 import com.xtremelabs.robolectric.internal.RealObject; 10 11 import java.util.ArrayList; 12 13 /** 14 * Shadow for {@code ItemizedOverlay} that keeps track of what has been called and enables the return value for 15 * {@link #hitTest(com.google.android.maps.OverlayItem, android.graphics.drawable.Drawable, int, int)} to be set up by 16 * tests. 17 */ 18 @Implements(ItemizedOverlay.class) 19 public class ShadowItemizedOverlay<Item extends OverlayItem> { 20 private boolean isPopulated; 21 private boolean shouldHit; 22 private boolean lastFocusedIndexWasReset; 23 private ArrayList<Item> items = new ArrayList<Item>(); 24 25 @RealObject 26 private ItemizedOverlay<Item> realObject; 27 28 @Implementation populate()29 public final void populate() { 30 isPopulated = true; 31 32 items.clear(); 33 for (int i = 0; i < realObject.size(); i++) { 34 items.add(getBridge().createItem(i)); 35 } 36 } 37 38 @Implementation getItem(int position)39 public final Item getItem(int position) { 40 return items.get(position); 41 } 42 43 @Implementation hitTest(OverlayItem item, android.graphics.drawable.Drawable drawable, int i, int i1)44 public boolean hitTest(OverlayItem item, android.graphics.drawable.Drawable drawable, int i, int i1) { 45 return shouldHit; 46 } 47 48 @Implementation setLastFocusedIndex(int i)49 public void setLastFocusedIndex(int i) { 50 lastFocusedIndexWasReset = (i == -1); 51 } 52 53 @Implementation boundCenterBottom(Drawable drawable)54 public static Drawable boundCenterBottom(Drawable drawable) { 55 return drawable; 56 } 57 58 /** 59 * Non-Android accessor that indicates whether {@link #setLastFocusedIndex(int)} has been called with a value other 60 * than -1. 61 * 62 * @return whether {@link #setLastFocusedIndex(int)} has been called with a value other 63 * than -1 64 */ lastFocusedIndexWasReset()65 public boolean lastFocusedIndexWasReset() { 66 return lastFocusedIndexWasReset; 67 } 68 setIsPopulated(boolean isPopulated)69 public void setIsPopulated(boolean isPopulated) { 70 this.isPopulated = isPopulated; 71 } 72 isPopulated()73 public boolean isPopulated() { 74 return isPopulated; 75 } 76 77 /** 78 * Sets up the return value for 79 * {@link #hitTest(com.google.android.maps.OverlayItem, android.graphics.drawable.Drawable, int, int)} 80 * 81 * @param shouldHit the value that 82 * {@link #hitTest(com.google.android.maps.OverlayItem, android.graphics.drawable.Drawable, int, int)} 83 * should return 84 */ setShouldHit(boolean shouldHit)85 public void setShouldHit(boolean shouldHit) { 86 this.shouldHit = shouldHit; 87 } 88 getBridge()89 private ShadowItemizedOverlayBridge<Item> getBridge() { 90 return new ShadowItemizedOverlayBridge<Item>(realObject); 91 } 92 } 93