1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget.focus;
18
19import android.widget.focus.VerticalFocusSearch;
20
21import android.test.ActivityInstrumentationTestCase;
22import android.test.suitebuilder.annotation.Suppress;
23import android.view.FocusFinder;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.Button;
27import android.widget.LinearLayout;
28
29/**
30 * Tests that focus searching works on a vertical linear layout of buttons of
31 * various widths and horizontal placements.
32 */
33// Suppress until bug http://b/issue?id=1416545 is fixed
34@Suppress
35public class VerticalFocusSearchTest extends ActivityInstrumentationTestCase<VerticalFocusSearch> {
36
37    private LinearLayout mLayout;
38
39    private Button mTopWide;
40    private Button mMidSkinny1Left;
41    private Button mMidSkinny2Right;
42    private Button mBottomWide;
43
44    private FocusSearchAlg mFocusFinder;
45
46    // helps test old and new impls when figuring out why something that used
47    // to work doesn't anymore (or verifying that new works as well as old).
48    interface FocusSearchAlg {
49        View findNextFocus(ViewGroup root, View focused, int direction);
50    }
51
52    // calls new impl
53    static class NewFocusSearchAlg implements FocusSearchAlg {
54
55        public View findNextFocus(ViewGroup root, View focused, int direction) {
56            return FocusFinder.getInstance()
57                    .findNextFocus(root, focused, direction);
58        }
59    }
60
61    public VerticalFocusSearchTest() {
62        super("com.android.frameworks.coretests", VerticalFocusSearch.class);
63    }
64
65
66    @Override
67    protected void setUp() throws Exception {
68        super.setUp();
69
70        mLayout = getActivity().getLayout();
71        mTopWide = getActivity().getTopWide();
72        mMidSkinny1Left = getActivity().getMidSkinny1Left();
73        mMidSkinny2Right = getActivity().getMidSkinny2Right();
74        mBottomWide = getActivity().getBottomWide();
75
76        mFocusFinder = new NewFocusSearchAlg();
77    }
78
79    public void testPreconditions() {
80        assertNotNull(mLayout);
81        assertNotNull(mTopWide);
82        assertNotNull(mMidSkinny1Left);
83        assertNotNull(mMidSkinny2Right);
84        assertNotNull(mBottomWide);
85    }
86
87    public void testSearchFromTopButton() {
88        assertNull("going up from mTopWide.",
89                mFocusFinder.findNextFocus(mLayout, mTopWide, View.FOCUS_UP));
90
91        assertNull("going left from mTopWide.",
92                mFocusFinder.findNextFocus(mLayout, mTopWide, View.FOCUS_LEFT));
93
94        assertNull("going right from mTopWide.",
95                mFocusFinder.findNextFocus(mLayout, mTopWide, View.FOCUS_RIGHT));
96
97        assertEquals("going down from mTopWide.",
98                mMidSkinny1Left,
99                mFocusFinder
100                .findNextFocus(mLayout, mTopWide, View.FOCUS_DOWN));
101    }
102
103    public void testSearchFromMidLeft() {
104        assertNull("going left should have no next focus",
105                mFocusFinder.findNextFocus(mLayout, mMidSkinny1Left, View.FOCUS_LEFT));
106
107        assertEquals("going right from mMidSkinny1Left should go to mMidSkinny2Right",
108                mMidSkinny2Right,
109                mFocusFinder.findNextFocus(mLayout, mMidSkinny1Left, View.FOCUS_RIGHT));
110
111        assertEquals("going up from mMidSkinny1Left should go to mTopWide",
112                mTopWide,
113                mFocusFinder.findNextFocus(mLayout, mMidSkinny1Left, View.FOCUS_UP));
114
115        assertEquals("going down from mMidSkinny1Left should go to mMidSkinny2Right",
116                mMidSkinny2Right,
117                mFocusFinder.findNextFocus(mLayout, mMidSkinny1Left, View.FOCUS_DOWN));
118    }
119
120    public void testSearchFromMidRight() {
121        assertEquals("going left from mMidSkinny2Right should go to mMidSkinny1Left",
122                mMidSkinny1Left,
123                mFocusFinder.findNextFocus(mLayout, mMidSkinny2Right, View.FOCUS_LEFT));
124
125        assertNull("going right should have no next focus",
126                mFocusFinder.findNextFocus(mLayout, mMidSkinny2Right, View.FOCUS_RIGHT));
127
128        assertEquals("going up from mMidSkinny2Right should go to mMidSkinny1Left",
129                mMidSkinny1Left,
130                mFocusFinder.findNextFocus(mLayout, mMidSkinny2Right, View.FOCUS_UP));
131
132        assertEquals("going down from mMidSkinny2Right should go to mBottomWide",
133                mBottomWide,
134                mFocusFinder.findNextFocus(mLayout, mMidSkinny2Right, View.FOCUS_DOWN));
135
136    }
137
138    public void testSearchFromFromBottom() {
139        assertNull("going down from bottom button should have no next focus.",
140                mFocusFinder.findNextFocus(mLayout, mBottomWide, View.FOCUS_DOWN));
141
142        assertNull("going left from bottom button should have no next focus.",
143                mFocusFinder.findNextFocus(mLayout, mBottomWide, View.FOCUS_LEFT));
144
145        assertNull("going right from bottom button should have no next focus.",
146                mFocusFinder.findNextFocus(mLayout, mBottomWide, View.FOCUS_RIGHT));
147
148        assertEquals("going up from bottom button should go to mMidSkinny2Right.",
149                mMidSkinny2Right,
150                mFocusFinder.findNextFocus(mLayout, mBottomWide, View.FOCUS_UP));
151    }
152}
153