GridWidgetTest.java revision fa5f60106a84a2b475c38cc6b8baa347bbd6ae1d
1062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu/*
2062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * Copyright (C) 2015 The Android Open Source Project
3062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu *
4062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * Licensed under the Apache License, Version 2.0 (the "License");
5062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * you may not use this file except in compliance with the License.
6062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * You may obtain a copy of the License at
7062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu *
8062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu *      http://www.apache.org/licenses/LICENSE-2.0
9062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu *
10062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * Unless required by applicable law or agreed to in writing, software
11062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * distributed under the License is distributed on an "AS IS" BASIS,
12062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * See the License for the specific language governing permissions and
14062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * limitations under the License.
15062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu */
16062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gupackage android.support.v17.leanback.widget;
17062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
18247741d74797105ba4f6d2834ec56dab32c14644Dake Guimport android.support.v17.leanback.tests.R;
19062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport android.test.ActivityInstrumentationTestCase2;
20062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport android.util.Log;
21062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport android.view.KeyEvent;
22062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport android.view.View;
23062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport android.view.ViewGroup;
24062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport android.app.Instrumentation;
25062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport android.content.Intent;
26062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
27062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport java.util.ArrayList;
28062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport java.util.Arrays;
29062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport java.util.Comparator;
30062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport java.util.HashMap;
31062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Guimport java.util.Iterator;
32062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
33062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu/**
34062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu * @hide from javadoc
35062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu */
36062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gupublic class GridWidgetTest extends ActivityInstrumentationTestCase2<GridActivity> {
37062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
38062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    protected GridActivity mActivity;
39062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    protected Instrumentation mInstrumentation;
40062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    protected BaseGridView mGridView;
4185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    protected GridLayoutManager mLayoutManager;
42062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    protected int mOrientation;
43247741d74797105ba4f6d2834ec56dab32c14644Dake Gu    protected int mNumRows;
44062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
45062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    private final Comparator<View> mRowSortComparator = new Comparator<View>() {
46062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        public int compare(View lhs, View rhs) {
47062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu            if (mOrientation == BaseGridView.HORIZONTAL) {
48062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu                return lhs.getLeft() - rhs.getLeft();
49062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu            } else {
50062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu                return lhs.getTop() - rhs.getTop();
51062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu            }
52062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        };
53062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    };
54062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
55062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    /**
56062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu     * Verify margins between items on same row are same.
57062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu     */
58062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    private final Runnable mVerifyLayout = new Runnable() {
59062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        @Override
60062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        public void run() {
61e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            verifyMargin();
62062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        }
63062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    };
64062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
65062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    public GridWidgetTest() {
66062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        super("android.support.v17.leanback.tests", GridActivity.class);
67062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    }
68062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
69062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    /**
70062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu     * Wait for grid view stop scroll and optionally verify state of grid view.
71062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu     */
72062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    protected void waitForScrollIdle(Runnable verify) throws Throwable {
73062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        while (mGridView.getLayoutManager().isSmoothScrolling() ||
74062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu                mGridView.getScrollState() != BaseGridView.SCROLL_STATE_IDLE) {
75062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu            try {
76062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu                Thread.sleep(100);
77062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu            } catch (InterruptedException ex) {
78062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu                break;
79062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu            }
80062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu            if (verify != null) {
81062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu                runTestOnUiThread(verify);
82062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu            }
83062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        }
84062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    }
85062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
86062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    /**
8785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu     * Wait for grid view stop animation and optionally verify state of grid view.
8885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu     */
8985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    protected void waitForTransientStateGone(Runnable verify) throws Throwable {
9085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        do {
9185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            try {
9285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                Thread.sleep(100);
9385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            } catch (InterruptedException ex) {
9485df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                break;
9585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            }
9685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            if (verify != null) {
9785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                runTestOnUiThread(verify);
9885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            }
9985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        } while (mGridView.hasTransientState());
10085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    }
10185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
10285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    /**
103062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu     * Wait for grid view stop scroll.
104062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu     */
105062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    protected void waitForScrollIdle() throws Throwable {
106062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        waitForScrollIdle(null);
107062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    }
108062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
109e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    /**
110a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu     * Scrolls using given key.
111a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu     */
112a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu    protected void scroll(int key, Runnable verify) throws Throwable {
113a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        do {
114a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            if (verify != null) {
115a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu                runTestOnUiThread(verify);
116a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            }
117a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            sendRepeatedKeys(10, key);
118a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            try {
119a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu                Thread.sleep(100);
120a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            } catch (InterruptedException ex) {
121a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu                break;
122a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            }
123a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        } while (mGridView.getLayoutManager().isSmoothScrolling() ||
124a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu                mGridView.getScrollState() != BaseGridView.SCROLL_STATE_IDLE);
125a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu    }
126a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu
127a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu    protected void scrollToBegin(Runnable verify) throws Throwable {
128a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        int key;
129a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        if (mOrientation == BaseGridView.HORIZONTAL) {
130a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            if (mGridView.getLayoutDirection() == ViewGroup.LAYOUT_DIRECTION_RTL) {
131a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu                key = KeyEvent.KEYCODE_DPAD_RIGHT;
132a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            } else {
133a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu                key = KeyEvent.KEYCODE_DPAD_LEFT;
134a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            }
135a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        } else {
136a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            key = KeyEvent.KEYCODE_DPAD_UP;
137a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        }
138a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scroll(key, verify);
139a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu    }
140a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu
141a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu    protected void scrollToEnd(Runnable verify) throws Throwable {
142a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        int key;
143a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        if (mOrientation == BaseGridView.HORIZONTAL) {
144a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            if (mGridView.getLayoutDirection() == ViewGroup.LAYOUT_DIRECTION_RTL) {
145a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu                key = KeyEvent.KEYCODE_DPAD_LEFT;
146a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            } else {
147a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu                key = KeyEvent.KEYCODE_DPAD_RIGHT;
148a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            }
149a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        } else {
150a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu            key = KeyEvent.KEYCODE_DPAD_DOWN;
151a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        }
152a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scroll(key, verify);
153a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu    }
154a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu
155a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu    /**
156e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu     * Group and sort children by their position on each row (HORIZONTAL) or column(VERTICAL).
157e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu     */
158e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    protected View[][] sortByRows() {
159e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        final HashMap<Integer, ArrayList<View>> rows = new HashMap<Integer, ArrayList<View>>();
160c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        ArrayList<Integer> rowLocations = new ArrayList();
161e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        for (int i = 0; i < mGridView.getChildCount(); i++) {
162e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            View v = mGridView.getChildAt(i);
163e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            int rowLocation;
164e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            if (mOrientation == BaseGridView.HORIZONTAL) {
165e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                rowLocation = v.getTop();
166e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            } else {
167e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                rowLocation = mGridView.getLayoutDirection() == ViewGroup.LAYOUT_DIRECTION_RTL ?
168e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    v.getRight() : v.getLeft();
169e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            }
170e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            ArrayList<View> views = rows.get(rowLocation);
171e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            if (views == null) {
172e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                views = new ArrayList<View>();
173e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                rows.put(rowLocation, views);
174c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu                rowLocations.add(rowLocation);
175e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            }
176e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            views.add(v);
177e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        }
178c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        Object[] sortedLocations = rowLocations.toArray();
179c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        Arrays.sort(sortedLocations);
180c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        if (mNumRows != rows.size()) {
181c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            assertEquals("Dump Views by rows "+rows, mNumRows, rows.size());
182c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        }
183e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        View[][] sorted = new View[rows.size()][];
184c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        for (int i = 0; i < rowLocations.size(); i++) {
185c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            Integer rowLocation = rowLocations.get(i);
186c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            ArrayList<View> arr = rows.get(rowLocation);
187e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            View[] views = arr.toArray(new View[arr.size()]);
188e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            Arrays.sort(views, mRowSortComparator);
189c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            sorted[i] = views;
190e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        }
191e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        return sorted;
192e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    }
193e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu
194e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    protected void verifyMargin() {
195e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        View[][] sorted = sortByRows();
196e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        for (int row = 0; row < sorted.length; row++) {
197e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            View[] views = sorted[row];
198e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            int margin = -1;
199e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            for (int i = 1; i < views.length; i++) {
200e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                if (mOrientation == BaseGridView.HORIZONTAL) {
201e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    if (i == 1) {
202e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                        margin = views[i].getLeft() - views[i - 1].getRight();
203e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    } else {
204e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                        assertEquals(margin, views[i].getLeft() - views[i - 1].getRight());
205e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    }
206e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                } else {
207e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    if (i == 1) {
208e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                        margin = views[i].getTop() - views[i - 1].getBottom();
209e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    } else {
210e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                        assertEquals(margin, views[i].getTop() - views[i - 1].getBottom());
211e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    }
212e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                }
213e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            }
214e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        }
215e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    }
216e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu
217e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    protected void verifyBeginAligned() {
218e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        View[][] sorted = sortByRows();
219e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        int alignedLocation = 0;
220e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        if (mOrientation == BaseGridView.HORIZONTAL) {
221e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            if (mGridView.getLayoutDirection() == ViewGroup.LAYOUT_DIRECTION_RTL) {
222e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                for (int i = 0; i < sorted.length; i++) {
223e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    if (i == 0) {
224e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                        alignedLocation = sorted[i][sorted[i].length - 1].getRight();
225e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    } else {
226e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                        assertEquals(alignedLocation, sorted[i][sorted[i].length - 1].getRight());
227e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    }
228e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                }
229e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            } else {
230e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                for (int i = 0; i < sorted.length; i++) {
231e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    if (i == 0) {
232e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                        alignedLocation = sorted[i][0].getLeft();
233e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    } else {
234e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                        assertEquals(alignedLocation, sorted[i][0].getLeft());
235e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    }
236e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                }
237e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            }
238e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        } else {
239e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            for (int i = 0; i < sorted.length; i++) {
240e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                if (i == 0) {
241e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    alignedLocation = sorted[i][0].getTop();
242e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                } else {
243e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                    assertEquals(alignedLocation, sorted[i][0].getTop());
244e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu                }
245e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu            }
246e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        }
247e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    }
248e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu
249c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu    protected int[] getEndEdges() {
250c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        View[][] sorted = sortByRows();
251c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        int[] edges = new int[sorted.length];
252c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        if (mOrientation == BaseGridView.HORIZONTAL) {
253c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            if (mGridView.getLayoutDirection() == ViewGroup.LAYOUT_DIRECTION_RTL) {
254c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu                for (int i = 0; i < sorted.length; i++) {
255c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu                    edges[i] = sorted[i][0].getLeft();
256c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu                }
257c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            } else {
258c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu                for (int i = 0; i < sorted.length; i++) {
259c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu                    edges[i] = sorted[i][sorted[i].length - 1].getRight();
260c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu                }
261c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            }
262c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        } else {
263c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            for (int i = 0; i < sorted.length; i++) {
264c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu                edges[i] = sorted[i][sorted[i].length - 1].getBottom();
265c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            }
266c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        }
267c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        return edges;
268c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu    }
269c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu
270c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu    protected void verifyEdgesSame(int[] edges, int[] edges2) {
271c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        assertEquals(edges.length, edges2.length);
272c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        for (int i = 0; i < edges.length; i++) {
273c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu            assertEquals(edges[i], edges2[i]);
274c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        }
275c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu    }
276c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu
27785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    protected void verifyBoundCount(int count) {
27885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        if (mActivity.getBoundCount() != count) {
27985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            StringBuffer b = new StringBuffer();
28085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            b.append("ItemsLength: ");
28185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            for (int i = 0; i < mActivity.mItemLengths.length; i++) {
28285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                b.append(mActivity.mItemLengths[i]).append(",");
28385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            }
28485df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            assertEquals("Bound count does not match, ItemsLengths: "+ b,
28585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                    count, mActivity.getBoundCount());
28685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        }
28785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    }
28885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
289e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    public void testThreeRowHorizontalBasic() throws Throwable {
290062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
291062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        mInstrumentation = getInstrumentation();
292062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
293247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.horizontal_grid);
294e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 100);
295062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        setActivityIntent(intent);
296062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        mActivity = getActivity();
297062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu        mGridView = mActivity.mGridView;
298247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mOrientation = BaseGridView.HORIZONTAL;
299247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mNumRows = 3;
300062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
301a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToEnd(mVerifyLayout);
30285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        verifyBoundCount(100);
303e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu
304a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToBegin(mVerifyLayout);
305062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
306e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        verifyBeginAligned();
307062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu    }
308062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu
309e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    public void testThreeColumnVerticalBasic() throws Throwable {
310e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu
311e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        mInstrumentation = getInstrumentation();
312e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
313247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.vertical_grid);
314e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 200);
315e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        setActivityIntent(intent);
316e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        mActivity = getActivity();
317e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        mGridView = mActivity.mGridView;
318247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mOrientation = BaseGridView.VERTICAL;
319247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mNumRows = 3;
320e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu
321a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToEnd(mVerifyLayout);
32285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        verifyBoundCount(200);
323e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu
324a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToBegin(mVerifyLayout);
32585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
32685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        verifyBeginAligned();
32785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    }
32885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
32985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    public void testRedundantAppendRemove() throws Throwable {
33085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mInstrumentation = getInstrumentation();
33185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
332247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
333247741d74797105ba4f6d2834ec56dab32c14644Dake Gu                R.layout.vertical_grid_testredundantappendremove);
33485df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        intent.putExtra(GridActivity.EXTRA_ITEMS, new int[]{
33585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                149,177,128,234,227,187,163,223,146,210,228,148,227,193,182,197,177,142,225,207,
33685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                157,171,209,204,187,184,123,221,197,153,202,179,193,214,226,173,225,143,188,159,
33785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                139,193,233,143,227,203,222,124,228,223,164,131,228,126,211,160,165,152,235,184,
33885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                155,224,149,181,171,229,200,234,177,130,164,172,188,139,132,203,179,220,147,131,
33985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                226,127,230,239,183,203,206,227,123,170,239,234,200,149,237,204,160,133,202,234,
34085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                173,122,139,149,151,153,216,231,121,145,227,153,186,174,223,180,123,215,206,216,
34185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                239,222,219,207,193,218,140,133,171,153,183,132,233,138,159,174,189,171,143,128,
34285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                152,222,141,202,224,190,134,120,181,231,230,136,132,224,136,210,207,150,128,183,
34385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                221,194,179,220,126,221,137,205,223,193,172,132,226,209,133,191,227,127,159,171,
34485df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                180,149,237,177,194,207,170,202,161,144,147,199,205,186,164,140,193,203,224,129});
34585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        setActivityIntent(intent);
34685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mActivity = getActivity();
34785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mGridView = mActivity.mGridView;
348247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mOrientation = BaseGridView.VERTICAL;
349247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mNumRows = 3;
35085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
351a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToEnd(mVerifyLayout);
35285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
35385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        verifyBoundCount(200);
354e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu
355a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToBegin(mVerifyLayout);
356e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu
357e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu        verifyBeginAligned();
358e7ead88853562603f76b92189a828ba14d5c4bcbDake Gu    }
35985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
36085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    public void testRedundantAppendRemove2() throws Throwable {
36185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mInstrumentation = getInstrumentation();
36285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
363247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
364247741d74797105ba4f6d2834ec56dab32c14644Dake Gu                R.layout.horizontal_grid_testredundantappendremove2);
36585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        intent.putExtra(GridActivity.EXTRA_ITEMS, new int[]{
36685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                318,333,199,224,246,273,269,289,340,313,265,306,349,269,185,282,257,354,316,252,
36785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                237,290,283,343,196,313,290,343,191,262,342,228,343,349,251,203,226,305,265,213,
36885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                216,333,295,188,187,281,288,311,244,232,224,332,290,181,267,276,226,261,335,355,
36985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                225,217,219,183,234,285,257,304,182,250,244,223,257,219,342,185,347,205,302,315,
37085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                299,309,292,237,192,309,228,250,347,227,337,298,299,185,185,331,223,284,265,351});
37185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        setActivityIntent(intent);
37285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mActivity = getActivity();
37385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mGridView = mActivity.mGridView;
374247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mOrientation = BaseGridView.HORIZONTAL;
375247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mNumRows = 3;
37685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mLayoutManager = (GridLayoutManager) mGridView.getLayoutManager();
37785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
37885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        // test append without staggered result cache
379a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToEnd(mVerifyLayout);
38085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
38185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        verifyBoundCount(100);
382c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        int[] endEdges = getEndEdges();
38385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
384a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToBegin(mVerifyLayout);
38585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
38685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        verifyBeginAligned();
38785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
38885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        // now test append with staggered result cache
38985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        runTestOnUiThread(new Runnable() {
39085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            public void run() {
39185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                mActivity.changeArraySize(3);
39285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            }
39385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        });
39485df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        Thread.sleep(500);
39585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        assertEquals("Staggerd cache should be kept as is when no item size change",
39685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                100, ((StaggeredGrid) mLayoutManager.mGrid).mLocations.size());
39785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
39885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mActivity.resetBoundCount();
39985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        runTestOnUiThread(new Runnable() {
40085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            public void run() {
40185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu                mActivity.changeArraySize(100);
40285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu            }
40385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        });
40485df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        Thread.sleep(500);
40585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
406a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToEnd(mVerifyLayout);
40785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        verifyBoundCount(100);
408c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu
409c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        // we should get same aligned end edges
410c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        int[] endEdges2 = getEndEdges();
411c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        verifyEdgesSame(endEdges, endEdges2);
41285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    }
41385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
414c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu    public void testItemMovedHorizontal() throws Throwable {
41585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
41685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mInstrumentation = getInstrumentation();
41785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
418247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
419247741d74797105ba4f6d2834ec56dab32c14644Dake Gu                R.layout.horizontal_grid);
42085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 200);
42185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        setActivityIntent(intent);
42285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mActivity = getActivity();
42385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mGridView = mActivity.mGridView;
424247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mOrientation = BaseGridView.HORIZONTAL;
425247741d74797105ba4f6d2834ec56dab32c14644Dake Gu        mNumRows = 3;
42685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
42785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mGridView.setSelectedPositionSmooth(150);
42885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        waitForScrollIdle(mVerifyLayout);
42985df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        mActivity.swap(150, 152);
43085df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        waitForTransientStateGone(null);
43185df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
43285df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        runTestOnUiThread(mVerifyLayout);
43385df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
434a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToBegin(mVerifyLayout);
43585df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
43685df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu        verifyBeginAligned();
43785df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu    }
43885df3117f0fcd0aa10d7bd45194dab97e22112f2Dake Gu
439c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu    public void testItemMovedVertical() throws Throwable {
440c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu
441c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        mInstrumentation = getInstrumentation();
442c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
443c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
444c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu                R.layout.vertical_grid);
445c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 200);
446c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        setActivityIntent(intent);
447c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        mActivity = getActivity();
448c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        mGridView = mActivity.mGridView;
449c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        mOrientation = BaseGridView.VERTICAL;
450c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        mNumRows = 3;
451c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu
452c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        mGridView.setSelectedPositionSmooth(150);
453c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        waitForScrollIdle(mVerifyLayout);
454c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        mActivity.swap(150, 152);
455c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        waitForTransientStateGone(null);
456c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu
457c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        runTestOnUiThread(mVerifyLayout);
458c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu
4597ee22537e201c3d6b5de2908aecc4b0cc59a05c2Dake Gu        scrollToEnd(mVerifyLayout);
460a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToBegin(mVerifyLayout);
461c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu
462c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu        verifyBeginAligned();
463c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu    }
464c755c85c3596bb00f30f7941bac3860a9dc91d7aDake Gu
465ede7fd3c91008fb042c736240534d0d87234f112Dake Gu    public void testItemAddRemoveHorizontal() throws Throwable {
466ede7fd3c91008fb042c736240534d0d87234f112Dake Gu
467ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        mInstrumentation = getInstrumentation();
468ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
469ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
470ede7fd3c91008fb042c736240534d0d87234f112Dake Gu                R.layout.horizontal_grid);
471ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 200);
472ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        setActivityIntent(intent);
473ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        mActivity = getActivity();
474ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        mGridView = mActivity.mGridView;
475ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        mOrientation = BaseGridView.HORIZONTAL;
476ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        mNumRows = 3;
477ede7fd3c91008fb042c736240534d0d87234f112Dake Gu
478a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToEnd(mVerifyLayout);
479ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        int[] endEdges = getEndEdges();
480ede7fd3c91008fb042c736240534d0d87234f112Dake Gu
481ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        mGridView.setSelectedPositionSmooth(150);
482ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        waitForScrollIdle(mVerifyLayout);
483ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        int[] removedItems = mActivity.removeItems(151, 4);
484ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        waitForTransientStateGone(null);
485ede7fd3c91008fb042c736240534d0d87234f112Dake Gu
486a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToEnd(mVerifyLayout);
487ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        mGridView.setSelectedPositionSmooth(150);
488ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        waitForScrollIdle(mVerifyLayout);
489ede7fd3c91008fb042c736240534d0d87234f112Dake Gu
490ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        mActivity.addItems(151, removedItems);
491ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        waitForTransientStateGone(null);
492a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToEnd(mVerifyLayout);
493ede7fd3c91008fb042c736240534d0d87234f112Dake Gu
494ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        // we should get same aligned end edges
495ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        int[] endEdges2 = getEndEdges();
496ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        verifyEdgesSame(endEdges, endEdges2);
497ede7fd3c91008fb042c736240534d0d87234f112Dake Gu
498a3c24a53034f1effc24b2d01328d63d96ad2ac7cDake Gu        scrollToBegin(mVerifyLayout);
499ede7fd3c91008fb042c736240534d0d87234f112Dake Gu        verifyBeginAligned();
500ede7fd3c91008fb042c736240534d0d87234f112Dake Gu    }
501ede7fd3c91008fb042c736240534d0d87234f112Dake Gu
50277b17ebed6d1f8488b150b25e2b6754971480caeDake Gu    public void testNonFocusableHorizontal() throws Throwable {
50377b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int numItems = 200;
50477b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int startPos = 45;
50577b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int skips = 20;
50677b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int numColumns = 3;
50777b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int endPos = startPos + numColumns * (skips + 1);
50877b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
50977b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mInstrumentation = getInstrumentation();
51077b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
51177b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
51277b17ebed6d1f8488b150b25e2b6754971480caeDake Gu                R.layout.horizontal_grid);
51377b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, numItems);
51477b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
51577b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mOrientation = BaseGridView.HORIZONTAL;
51677b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mNumRows = numColumns;
51777b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        boolean[] focusable = new boolean[numItems];
51877b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        for (int i = 0; i < focusable.length; i++) {
51977b17ebed6d1f8488b150b25e2b6754971480caeDake Gu            focusable[i] = true;
52077b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        }
52177b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        for (int i = startPos + mNumRows, j = 0; j < skips; i += mNumRows, j++) {
52277b17ebed6d1f8488b150b25e2b6754971480caeDake Gu            focusable[i] = false;
52377b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        }
52477b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        intent.putExtra(GridActivity.EXTRA_ITEMS_FOCUSABLE, focusable);
52577b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        setActivityIntent(intent);
52677b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mActivity = getActivity();
52777b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mGridView = mActivity.mGridView;
52877b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
52977b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mGridView.setSelectedPositionSmooth(startPos);
53077b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        waitForScrollIdle(mVerifyLayout);
53177b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
53277b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        if (mGridView.getLayoutDirection() == ViewGroup.LAYOUT_DIRECTION_RTL) {
53377b17ebed6d1f8488b150b25e2b6754971480caeDake Gu            sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
53477b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        } else {
53577b17ebed6d1f8488b150b25e2b6754971480caeDake Gu            sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
53677b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        }
53777b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        waitForScrollIdle(mVerifyLayout);
53877b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        assertEquals(endPos, mGridView.getSelectedPosition());
53977b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
54077b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        if (mGridView.getLayoutDirection() == ViewGroup.LAYOUT_DIRECTION_RTL) {
54177b17ebed6d1f8488b150b25e2b6754971480caeDake Gu            sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
54277b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        } else {
54377b17ebed6d1f8488b150b25e2b6754971480caeDake Gu            sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
54477b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        }
54577b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        waitForScrollIdle(mVerifyLayout);
54677b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        assertEquals(startPos, mGridView.getSelectedPosition());
54777b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
54877b17ebed6d1f8488b150b25e2b6754971480caeDake Gu    }
54977b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
55077b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
55177b17ebed6d1f8488b150b25e2b6754971480caeDake Gu    public void testNonFocusableVertical() throws Throwable {
55277b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int numItems = 200;
55377b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int startPos = 44;
55477b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int skips = 20;
55577b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int numColumns = 3;
55677b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        final int endPos = startPos + numColumns * (skips + 1);
55777b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
55877b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mInstrumentation = getInstrumentation();
55977b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
56077b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
56177b17ebed6d1f8488b150b25e2b6754971480caeDake Gu                R.layout.vertical_grid);
56277b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, numItems);
56377b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
56477b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mOrientation = BaseGridView.VERTICAL;
56577b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mNumRows = numColumns;
56677b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        boolean[] focusable = new boolean[numItems];
56777b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        for (int i = 0; i < focusable.length; i++) {
56877b17ebed6d1f8488b150b25e2b6754971480caeDake Gu            focusable[i] = true;
56977b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        }
57077b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        for (int i = startPos + mNumRows, j = 0; j < skips; i += mNumRows, j++) {
57177b17ebed6d1f8488b150b25e2b6754971480caeDake Gu            focusable[i] = false;
57277b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        }
57377b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        intent.putExtra(GridActivity.EXTRA_ITEMS_FOCUSABLE, focusable);
57477b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        setActivityIntent(intent);
57577b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mActivity = getActivity();
57677b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mGridView = mActivity.mGridView;
57777b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
57877b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        mGridView.setSelectedPositionSmooth(startPos);
57977b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        waitForScrollIdle(mVerifyLayout);
58077b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
58177b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
58277b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        waitForScrollIdle(mVerifyLayout);
58377b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        assertEquals(endPos, mGridView.getSelectedPosition());
58477b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
58577b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
58677b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        waitForScrollIdle(mVerifyLayout);
58777b17ebed6d1f8488b150b25e2b6754971480caeDake Gu        assertEquals(startPos, mGridView.getSelectedPosition());
58877b17ebed6d1f8488b150b25e2b6754971480caeDake Gu
58977b17ebed6d1f8488b150b25e2b6754971480caeDake Gu    }
5906b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu
5916b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu    public void testTransferFocusable() throws Throwable {
5926b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        final int numItems = 200;
5936b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        final int numColumns = 3;
5946b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        final int startPos = 1;
5956b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu
5966b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mInstrumentation = getInstrumentation();
5976b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
5986b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
5996b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu                R.layout.horizontal_grid);
6006b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, numItems);
6016b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
6026b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mOrientation = BaseGridView.HORIZONTAL;
6036b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mNumRows = numColumns;
6046b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        boolean[] focusable = new boolean[numItems];
6056b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        for (int i = 0; i < focusable.length; i++) {
6066b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            focusable[i] = true;
6076b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        }
6086b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        for (int i = 0; i < startPos; i++) {
6096b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            focusable[i] = false;
6106b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        }
6116b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        intent.putExtra(GridActivity.EXTRA_ITEMS_FOCUSABLE, focusable);
6126b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        setActivityIntent(intent);
6136b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mActivity = getActivity();
6146b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mGridView = mActivity.mGridView;
6156b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu
6166b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        runTestOnUiThread(new Runnable() {
6176b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            public void run() {
6186b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu                mActivity.changeArraySize(0);
6196b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            }
6206b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        });
6216b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        Thread.sleep(500);
6226b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        assertTrue(mGridView.isFocused());
6236b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu
6246b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        runTestOnUiThread(new Runnable() {
6256b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            public void run() {
6266b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu                mActivity.changeArraySize(numItems);
6276b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            }
6286b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        });
6296b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        Thread.sleep(500);
6306b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        assertTrue(mGridView.getLayoutManager().findViewByPosition(startPos).hasFocus());
6316b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu    }
6326b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu
6336b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu    public void testTransferFocusable2() throws Throwable {
6346b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        final int numItems = 200;
6356b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        final int numColumns = 3;
6366b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        final int startPos = 10;
6376b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu
6386b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mInstrumentation = getInstrumentation();
6396b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
6406b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
6416b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu                R.layout.horizontal_grid);
6426b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, numItems);
6436b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        intent.putExtra(GridActivity.EXTRA_STAGGERED, true);
6446b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mOrientation = BaseGridView.HORIZONTAL;
6456b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mNumRows = numColumns;
6466b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        boolean[] focusable = new boolean[numItems];
6476b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        for (int i = 0; i < focusable.length; i++) {
6486b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            focusable[i] = true;
6496b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        }
6506b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        for (int i = 0; i < startPos; i++) {
6516b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            focusable[i] = false;
6526b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        }
6536b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        intent.putExtra(GridActivity.EXTRA_ITEMS_FOCUSABLE, focusable);
6546b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        setActivityIntent(intent);
6556b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mActivity = getActivity();
6566b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        mGridView = mActivity.mGridView;
6576b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu
6586b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        runTestOnUiThread(new Runnable() {
6596b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            public void run() {
6606b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu                mActivity.changeArraySize(0);
6616b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            }
6626b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        });
6636b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        Thread.sleep(500);
6646b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        assertTrue(mGridView.isFocused());
6656b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu
6666b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        runTestOnUiThread(new Runnable() {
6676b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            public void run() {
6686b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu                mActivity.changeArraySize(numItems);
6696b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu            }
6706b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        });
6716b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        Thread.sleep(500);
6726b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu        assertTrue(mGridView.getLayoutManager().findViewByPosition(startPos).hasFocus());
6736b22c4c875d7a540e705b2b6b65bf52c7a89299aDake Gu    }
674fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu
675fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu    public void testNonFocusableLose() throws Throwable {
676fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        mInstrumentation = getInstrumentation();
677fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
678fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID,
679fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu                R.layout.vertical_linear);
680fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        int[] items = new int[300];
681fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        for (int i = 0; i < items.length; i++) {
682fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu            items[i] = 480;
683fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        }
684fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        intent.putExtra(GridActivity.EXTRA_ITEMS, items);
685fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
686fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        intent.putExtra(GridActivity.EXTRA_REQUEST_LAYOUT_ONFOCUS, true);
687fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        mOrientation = BaseGridView.VERTICAL;
688fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        mNumRows = 1;
689fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        int pressDown = 15;
690fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu
691fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        setActivityIntent(intent);
692fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        mActivity = getActivity();
693fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        mGridView = mActivity.mGridView;
694fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu
695fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        mGridView.setSelectedPositionSmooth(0);
696fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        Thread.sleep(100);
697fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu
698fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        for (int i = 0; i < pressDown; i++) {
699fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
700fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        }
701fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        waitForScrollIdle(mVerifyLayout);
702fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu        assertFalse(mGridView.isFocused());
703fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu
704fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu    }
705fa5f60106a84a2b475c38cc6b8baa347bbd6ae1dDake Gu
706062f7f7667bce6e9bf4814d0bb5344fe518ed89eDake Gu}
707