• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static org.robolectric.shadow.api.Shadow.directlyOn;
4 
5 import android.view.View;
6 import android.widget.Adapter;
7 import android.widget.AdapterView;
8 import android.widget.FrameLayout;
9 import org.robolectric.annotation.Implementation;
10 import org.robolectric.annotation.Implements;
11 import org.robolectric.annotation.RealObject;
12 import org.robolectric.shadow.api.Shadow;
13 import org.robolectric.util.ReflectionHelpers.ClassParameter;
14 
15 @SuppressWarnings({"UnusedDeclaration"})
16 @Implements(AdapterView.class)
17 public class ShadowAdapterView<T extends Adapter> extends ShadowViewGroup {
18   private static int ignoreRowsAtEndOfList = 0;
19 
20   @RealObject
21   private AdapterView<T> realAdapterView;
22 
23   private AdapterView.OnItemSelectedListener itemSelectedListener;
24 
25   @Implementation
setOnItemSelectedListener( AdapterView.OnItemSelectedListener itemSelectedListener)26   protected void setOnItemSelectedListener(
27       AdapterView.OnItemSelectedListener itemSelectedListener) {
28     this.itemSelectedListener = itemSelectedListener;
29     directlyOn(realAdapterView, AdapterView.class, "setOnItemSelectedListener", ClassParameter.from(AdapterView.OnItemSelectedListener.class, itemSelectedListener));
30   }
31 
getItemSelectedListener()32   public AdapterView.OnItemSelectedListener getItemSelectedListener() {
33     return itemSelectedListener;
34   }
35 
performItemClick(int position)36   public boolean performItemClick(int position) {
37     return realAdapterView.performItemClick(realAdapterView.getChildAt(position),
38         position, realAdapterView.getItemIdAtPosition(position));
39   }
40 
findIndexOfItemContainingText(String targetText)41   public int findIndexOfItemContainingText(String targetText) {
42     for (int i = 0; i < realAdapterView.getCount(); i++) {
43       View childView = realAdapterView.getAdapter().getView(i, null, new FrameLayout(realAdapterView.getContext()));
44       ShadowView shadowView = Shadow.extract(childView);
45       String innerText = shadowView.innerText();
46       if (innerText.contains(targetText)) {
47         return i;
48       }
49     }
50     return -1;
51   }
52 
findItemContainingText(String targetText)53   public View findItemContainingText(String targetText) {
54     int itemIndex = findIndexOfItemContainingText(targetText);
55     if (itemIndex == -1) {
56       return null;
57     }
58     return realAdapterView.getChildAt(itemIndex);
59   }
60 
clickFirstItemContainingText(String targetText)61   public void clickFirstItemContainingText(String targetText) {
62     int itemIndex = findIndexOfItemContainingText(targetText);
63     if (itemIndex == -1) {
64       throw new IllegalArgumentException("No item found containing text \"" + targetText + "\"");
65     }
66     performItemClick(itemIndex);
67   }
68 
populateItems()69   public void populateItems() {
70     realView.measure(0, 0);
71     realView.layout(0, 0, 100, 10000);
72   }
73 
selectItemWithText(String s)74   public void selectItemWithText(String s) {
75     int itemIndex = findIndexOfItemContainingText(s);
76     realAdapterView.setSelection(itemIndex);
77   }
78 }
79