1 /* 2 * Copyright (C) 2017 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 package androidx.leanback.widget; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertSame; 21 import static org.junit.Assert.assertTrue; 22 23 import android.content.Context; 24 import android.support.test.InstrumentationRegistry; 25 import android.support.test.filters.SmallTest; 26 import android.support.test.runner.AndroidJUnit4; 27 import android.view.View; 28 import android.widget.Button; 29 import android.widget.LinearLayout; 30 import android.widget.TextView; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 import java.util.ArrayList; 36 37 @SmallTest 38 @RunWith(AndroidJUnit4.class) 39 40 public class ControlBarTest { 41 42 @Test defaultFocus()43 public void defaultFocus() { 44 Context context = InstrumentationRegistry.getTargetContext(); 45 final ControlBar bar = new ControlBar(context, null); 46 final TextView v1 = new Button(context); 47 bar.addView(v1, 100, 100); 48 final TextView v2 = new Button(context); 49 bar.addView(v2, 100, 100); 50 final TextView v3 = new Button(context); 51 bar.addView(v3, 100, 100); 52 InstrumentationRegistry.getInstrumentation().runOnMainSync( 53 new Runnable() { 54 @Override 55 public void run() { 56 bar.requestFocus(View.FOCUS_DOWN); 57 } 58 } 59 ); 60 assertTrue(v2.hasFocus()); 61 } 62 63 @Test persistFocus()64 public void persistFocus() { 65 Context context = InstrumentationRegistry.getTargetContext(); 66 final LinearLayout rootView = new LinearLayout(context); 67 final ControlBar bar = new ControlBar(context, null); 68 rootView.addView(bar, 800, 100); 69 final Button barSibling = new Button(context); 70 rootView.addView(barSibling, 100, 100); 71 final TextView v1 = new Button(context); 72 bar.addView(v1, 100, 100); 73 final TextView v2 = new Button(context); 74 bar.addView(v2, 100, 100); 75 final TextView v3 = new Button(context); 76 bar.addView(v3, 100, 100); 77 InstrumentationRegistry.getInstrumentation().runOnMainSync( 78 new Runnable() { 79 @Override 80 public void run() { 81 v3.requestFocus(View.FOCUS_DOWN); 82 } 83 } 84 ); 85 InstrumentationRegistry.getInstrumentation().runOnMainSync( 86 new Runnable() { 87 @Override 88 public void run() { 89 barSibling.requestFocus(); 90 } 91 } 92 ); 93 assertFalse(bar.hasFocus()); 94 InstrumentationRegistry.getInstrumentation().runOnMainSync( 95 new Runnable() { 96 @Override 97 public void run() { 98 bar.requestFocus(View.FOCUS_RIGHT); 99 } 100 } 101 ); 102 assertTrue(v3.hasFocus()); 103 } 104 105 @Test getFocusables()106 public void getFocusables() { 107 Context context = InstrumentationRegistry.getTargetContext(); 108 final LinearLayout rootView = new LinearLayout(context); 109 final ControlBar bar = new ControlBar(context, null); 110 rootView.addView(bar, 800, 100); 111 final Button barSibling = new Button(context); 112 rootView.addView(barSibling, 100, 100); 113 final TextView v1 = new Button(context); 114 bar.addView(v1, 100, 100); 115 final TextView v2 = new Button(context); 116 bar.addView(v2, 100, 100); 117 final TextView v3 = new Button(context); 118 bar.addView(v3, 100, 100); 119 120 ArrayList<View> focusables = new ArrayList(); 121 bar.addFocusables(focusables, View.FOCUS_DOWN); 122 assertEquals(1, focusables.size()); 123 assertSame(focusables.get(0), v2); 124 focusables.clear(); 125 bar.addFocusables(focusables, View.FOCUS_UP); 126 assertEquals(1, focusables.size()); 127 assertSame(focusables.get(0), v2); 128 focusables.clear(); 129 bar.addFocusables(focusables, View.FOCUS_LEFT); 130 assertEquals(3, focusables.size()); 131 focusables.clear(); 132 bar.addFocusables(focusables, View.FOCUS_RIGHT); 133 assertEquals(3, focusables.size()); 134 135 InstrumentationRegistry.getInstrumentation().runOnMainSync( 136 new Runnable() { 137 @Override 138 public void run() { 139 v3.requestFocus(View.FOCUS_DOWN); 140 } 141 } 142 ); 143 InstrumentationRegistry.getInstrumentation().runOnMainSync( 144 new Runnable() { 145 @Override 146 public void run() { 147 barSibling.requestFocus(); 148 } 149 } 150 ); 151 assertFalse(bar.hasFocus()); 152 focusables.clear(); 153 bar.addFocusables(focusables, View.FOCUS_DOWN); 154 assertEquals(1, focusables.size()); 155 assertSame(focusables.get(0), v3); 156 focusables.clear(); 157 bar.addFocusables(focusables, View.FOCUS_UP); 158 assertEquals(1, focusables.size()); 159 assertSame(focusables.get(0), v3); 160 focusables.clear(); 161 bar.addFocusables(focusables, View.FOCUS_LEFT); 162 assertEquals(3, focusables.size()); 163 focusables.clear(); 164 bar.addFocusables(focusables, View.FOCUS_RIGHT); 165 assertEquals(3, focusables.size()); 166 167 } 168 } 169