aboutsummaryrefslogtreecommitdiff
path: root/v1/src/main/java/com/xtremelabs/robolectric/shadows/ShadowAdapterView.java
blob: 7c3b13f7781d1661adf0f1d5b79e663a838f4ccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package com.xtremelabs.robolectric.shadows;

import android.database.DataSetObserver;
import android.os.Handler;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

import java.util.ArrayList;
import java.util.List;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(AdapterView.class)
public class ShadowAdapterView extends ShadowViewGroup {
    private static int ignoreRowsAtEndOfList = 0;
    private static boolean automaticallyUpdateRowViews = true;

    @RealObject
    private AdapterView realAdapterView;

    private Adapter adapter;
    private View mEmptyView;
    private AdapterView.OnItemSelectedListener onItemSelectedListener;
    private AdapterView.OnItemClickListener onItemClickListener;
    private AdapterView.OnItemLongClickListener onItemLongClickListener;
    private boolean valid = false;
    private int selectedPosition;
    private int itemCount = 0;

    private List<Object> previousItems = new ArrayList<Object>();

    @Implementation
    public void setAdapter(Adapter adapter) {
        this.adapter = adapter;

        if (null != adapter) {
            adapter.registerDataSetObserver(new AdapterViewDataSetObserver());
        }

        invalidateAndScheduleUpdate();
        setSelection(0);
    }

    @Implementation
    public void setEmptyView(View emptyView) {
        this.mEmptyView = emptyView;
        updateEmptyStatus(adapter == null || adapter.isEmpty());
    }

    @Implementation
    public int getPositionForView(android.view.View view) {
        while (view.getParent() != null && view.getParent() != realView) {
            view = (View) view.getParent();
        }

        for (int i = 0; i < getChildCount(); i++) {
            if (view == getChildAt(i)) {
                return i;
            }
        }

        return AdapterView.INVALID_POSITION;
    }

    private void invalidateAndScheduleUpdate() {
        valid = false;
        itemCount = adapter == null ? 0 : adapter.getCount();
        if (mEmptyView != null) {
            updateEmptyStatus(itemCount == 0);
        }

        if (hasOnItemSelectedListener() && itemCount == 0) {
            onItemSelectedListener.onNothingSelected(realAdapterView);
        }

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                if (!valid) {
                    update();
                    valid = true;
                }
            }
        });
    }

    private boolean hasOnItemSelectedListener() {
        return onItemSelectedListener != null;
    }

    private void updateEmptyStatus(boolean empty) {
        // code taken from the real AdapterView and commented out where not (yet?) applicable

        // we don't deal with filterMode yet...
//        if (isInFilterMode()) {
//            empty = false;
//        }

        if (empty) {
            if (mEmptyView != null) {
                mEmptyView.setVisibility(View.VISIBLE);
                setVisibility(View.GONE);
            } else {
                // If the caller just removed our empty view, make sure the list view is visible
                setVisibility(View.VISIBLE);
            }

            // leave layout for the moment...
//            // We are now GONE, so pending layouts will not be dispatched.
//            // Force one here to make sure that the state of the list matches
//            // the state of the adapter.
//            if (mDataChanged) {
//                this.onLayout(false, mLeft, mTop, mRight, mBottom);
//            }
        } else {
            if (mEmptyView != null) {
                mEmptyView.setVisibility(View.GONE);
            }
            setVisibility(View.VISIBLE);
        }
    }

    /**
     * Check if our adapter's items have changed without {@code onChanged()} or {@code onInvalidated()} having been called.
     *
     * @return true if the object is valid, false if not
     * @throws RuntimeException if the items have been changed without notification
     */
    public boolean checkValidity() {
        update();
        return valid;
    }

    /**
     * Set to avoid calling getView() on the last row(s) during validation. Useful if you are using a special
     * last row, e.g. one that goes and fetches more list data as soon as it comes into view. This sets a static
     * on the class, so be sure to call it again and set it back to 0 at the end of your test.
     *
     * @param countOfRows The number of rows to ignore at the end of the list.
     * @see com.xtremelabs.robolectric.shadows.ShadowAdapterView#checkValidity()
     */
    public static void ignoreRowsAtEndOfListDuringValidation(int countOfRows) {
        ignoreRowsAtEndOfList = countOfRows;
    }

    /**
     * Use this static method to turn off the feature of this class which calls getView() on all of the
     * adapter's rows in setAdapter() and after notifyDataSetChanged() or notifyDataSetInvalidated() is
     * called on the adapter. This feature is turned on by default. This sets a static on the class, so
     * set it back to true at the end of your test to avoid test pollution.
     *
     * @param shouldUpdate false to turn off the feature, true to turn it back on
     */
    public static void automaticallyUpdateRowViews(boolean shouldUpdate) {
        automaticallyUpdateRowViews = shouldUpdate;
    }

    @Implementation
    public int getSelectedItemPosition() {
        return selectedPosition;
    }

    @Implementation
    public Object getSelectedItem() {
        int pos = getSelectedItemPosition();
        return getItemAtPosition(pos);
    }

    @Implementation
    public Adapter getAdapter() {
        return adapter;
    }

    @Implementation
    public int getCount() {
        return itemCount;
    }

    @Implementation
    public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) {
        this.onItemSelectedListener = listener;
    }

    @Implementation
    public final AdapterView.OnItemSelectedListener getOnItemSelectedListener() {
        return onItemSelectedListener;
    }

    @Implementation
    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @Implementation
    public final AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    @Implementation
    public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) {
        this.onItemLongClickListener = listener;
    }

    @Implementation
    public AdapterView.OnItemLongClickListener getOnItemLongClickListener() {
        return onItemLongClickListener;
    }

    @Implementation
    public Object getItemAtPosition(int position) {
        Adapter adapter = getAdapter();
        return (adapter == null || position < 0) ? null : adapter.getItem(position);
    }

    @Implementation
    public long getItemIdAtPosition(int position) {
        Adapter adapter = getAdapter();
        return (adapter == null || position < 0) ? AdapterView.INVALID_ROW_ID : adapter.getItemId(position);
    }

    @Implementation
    public void setSelection(final int position) {
        selectedPosition = position;

        if (selectedPosition >= 0) {
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    if (hasOnItemSelectedListener()) {
                        onItemSelectedListener.onItemSelected(realAdapterView, getChildAt(position), position, getAdapter().getItemId(position));
                    }
                }
            });
        }
    }

    @Implementation
    public boolean performItemClick(View view, int position, long id) {
        if (onItemClickListener != null) {
            onItemClickListener.onItemClick(realAdapterView, view, position, id);
            return true;
        }
        return false;
    }

    public boolean performItemLongClick(View view, int position, long id) {
        if (onItemLongClickListener != null) {
            onItemLongClickListener.onItemLongClick(realAdapterView, view, position, id);
            return true;
        }
        return false;
    }

    public boolean performItemClick(int position) {
        return realAdapterView.performItemClick(realAdapterView.getChildAt(position),
                position, realAdapterView.getItemIdAtPosition(position));
    }

    public int findIndexOfItemContainingText(String targetText) {
        for (int i = 0; i < realAdapterView.getChildCount(); i++) {
            View childView = realAdapterView.getChildAt(i);
            String innerText = shadowOf(childView).innerText();
            if (innerText.contains(targetText)) {
                return i;
            }
        }
        return -1;
    }

    public View findItemContainingText(String targetText) {
        int itemIndex = findIndexOfItemContainingText(targetText);
        if (itemIndex == -1) {
            return null;
        }
        return realAdapterView.getChildAt(itemIndex);
    }

    public void clickFirstItemContainingText(String targetText) {
        int itemIndex = findIndexOfItemContainingText(targetText);
        if (itemIndex == -1) {
            throw new IllegalArgumentException("No item found containing text \"" + targetText + "\"");
        }
        performItemClick(itemIndex);
    }

    @Implementation
    public View getEmptyView() {
        return mEmptyView;
    }

    private void update() {
        if (!automaticallyUpdateRowViews) {
            return;
        }

        super.removeAllViews();
        addViews();
    }

    protected void addViews() {
        Adapter adapter = getAdapter();
        if (adapter != null) {
            if (valid && (previousItems.size() - ignoreRowsAtEndOfList != adapter.getCount() - ignoreRowsAtEndOfList)) {
                throw new ArrayIndexOutOfBoundsException("view is valid but adapter.getCount() has changed from " + previousItems.size() + " to " + adapter.getCount());
            }

            List<Object> newItems = new ArrayList<Object>();
            for (int i = 0; i < adapter.getCount() - ignoreRowsAtEndOfList; i++) {
                View view = adapter.getView(i, null, realAdapterView);
                // don't add null views
                if (view != null) {
                    addView(view);
                }
                newItems.add(adapter.getItem(i));
            }

            if (valid && !newItems.equals(previousItems)) {
                throw new RuntimeException("view is valid but current items <" + newItems + "> don't match previous items <" + previousItems + ">");
            }
            previousItems = newItems;
        }
    }

    /**
     * Simple default implementation of {@code android.database.DataSetObserver}
     */
    protected class AdapterViewDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            invalidateAndScheduleUpdate();
        }

        @Override
        public void onInvalidated() {
            invalidateAndScheduleUpdate();
        }
    }
}