1 /* 2 * Copyright (C) 2015 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 android.databinding.testapp; 17 18 import android.databinding.testapp.databinding.TextViewAdapterTestBinding; 19 import android.databinding.testapp.vo.TextViewBindingObject; 20 21 import android.annotation.TargetApi; 22 import android.databinding.adapters.TextViewBindingAdapter; 23 import android.graphics.drawable.ColorDrawable; 24 import android.os.Build; 25 import android.test.UiThreadTest; 26 import android.text.Editable; 27 import android.text.InputFilter; 28 import android.text.InputType; 29 import android.text.Spannable; 30 import android.text.method.DialerKeyListener; 31 import android.text.method.DigitsKeyListener; 32 import android.text.method.KeyListener; 33 import android.text.method.TextKeyListener; 34 import android.widget.TextView; 35 36 public class TextViewBindingAdapterTest 37 extends BindingAdapterTestBase<TextViewAdapterTestBinding, TextViewBindingObject> { 38 39 public TextViewBindingAdapterTest() { 40 super(TextViewAdapterTestBinding.class, TextViewBindingObject.class, 41 R.layout.text_view_adapter_test); 42 } 43 44 public void testNumeric() throws Throwable { 45 TextView view = mBinder.numericText; 46 assertTrue(view.getKeyListener() instanceof DigitsKeyListener); 47 DigitsKeyListener listener = (DigitsKeyListener) view.getKeyListener(); 48 assertEquals(getExpectedNumericType(), listener.getInputType()); 49 50 changeValues(); 51 52 assertTrue(view.getKeyListener() instanceof DigitsKeyListener); 53 listener = (DigitsKeyListener) view.getKeyListener(); 54 assertEquals(getExpectedNumericType(), listener.getInputType()); 55 } 56 57 private int getExpectedNumericType() { 58 int expectedType = InputType.TYPE_CLASS_NUMBER; 59 if ((mBindingObject.getNumeric() & TextViewBindingAdapter.SIGNED) != 0) { 60 expectedType |= InputType.TYPE_NUMBER_FLAG_SIGNED; 61 } 62 if ((mBindingObject.getNumeric() & TextViewBindingAdapter.DECIMAL) != 0) { 63 expectedType |= InputType.TYPE_NUMBER_FLAG_DECIMAL; 64 } 65 return expectedType; 66 } 67 68 public void testDrawables() throws Throwable { 69 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 70 TextView view = mBinder.textDrawableNormal; 71 assertEquals(mBindingObject.getDrawableLeft(), 72 ((ColorDrawable) view.getCompoundDrawables()[0]).getColor()); 73 assertEquals(mBindingObject.getDrawableTop(), 74 ((ColorDrawable) view.getCompoundDrawables()[1]).getColor()); 75 assertEquals(mBindingObject.getDrawableRight(), 76 ((ColorDrawable) view.getCompoundDrawables()[2]).getColor()); 77 assertEquals(mBindingObject.getDrawableBottom(), 78 ((ColorDrawable) view.getCompoundDrawables()[3]).getColor()); 79 80 changeValues(); 81 82 assertEquals(mBindingObject.getDrawableLeft(), 83 ((ColorDrawable) view.getCompoundDrawables()[0]).getColor()); 84 assertEquals(mBindingObject.getDrawableTop(), 85 ((ColorDrawable) view.getCompoundDrawables()[1]).getColor()); 86 assertEquals(mBindingObject.getDrawableRight(), 87 ((ColorDrawable) view.getCompoundDrawables()[2]).getColor()); 88 assertEquals(mBindingObject.getDrawableBottom(), 89 ((ColorDrawable) view.getCompoundDrawables()[3]).getColor()); 90 } 91 } 92 93 public void testDrawableStartEnd() throws Throwable { 94 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 95 TextView view = mBinder.textDrawableStartEnd; 96 assertEquals(mBindingObject.getDrawableStart(), 97 ((ColorDrawable) view.getCompoundDrawablesRelative()[0]).getColor()); 98 assertEquals(mBindingObject.getDrawableEnd(), 99 ((ColorDrawable) view.getCompoundDrawablesRelative()[2]).getColor()); 100 101 changeValues(); 102 103 assertEquals(mBindingObject.getDrawableStart(), 104 ((ColorDrawable) view.getCompoundDrawablesRelative()[0]).getColor()); 105 assertEquals(mBindingObject.getDrawableEnd(), 106 ((ColorDrawable) view.getCompoundDrawablesRelative()[2]).getColor()); 107 } 108 } 109 110 public void testSimpleProperties() throws Throwable { 111 TextView view = mBinder.textView; 112 113 assertEquals(mBindingObject.getAutoLink(), view.getAutoLinkMask()); 114 assertEquals(mBindingObject.getDrawablePadding(), view.getCompoundDrawablePadding()); 115 assertEquals(mBindingObject.getTextSize(), view.getTextSize()); 116 assertEquals(mBindingObject.getTextColorHint(), view.getHintTextColors().getDefaultColor()); 117 assertEquals(mBindingObject.getTextColorLink(), view.getLinkTextColors().getDefaultColor()); 118 assertEquals(mBindingObject.isAutoText(), isAutoTextEnabled(view)); 119 assertEquals(mBindingObject.getCapitalize(), getCapitalization(view)); 120 assertEquals(mBindingObject.getImeActionLabel(), view.getImeActionLabel()); 121 assertEquals(mBindingObject.getImeActionId(), view.getImeActionId()); 122 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 123 assertEquals(mBindingObject.getTextColorHighlight(), view.getHighlightColor()); 124 assertEquals(mBindingObject.getLineSpacingExtra(), view.getLineSpacingExtra()); 125 assertEquals(mBindingObject.getLineSpacingMultiplier(), 126 view.getLineSpacingMultiplier()); 127 assertEquals(mBindingObject.getShadowColor(), view.getShadowColor()); 128 assertEquals(mBindingObject.getShadowDx(), view.getShadowDx()); 129 assertEquals(mBindingObject.getShadowDy(), view.getShadowDy()); 130 assertEquals(mBindingObject.getShadowRadius(), view.getShadowRadius()); 131 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 132 assertEquals(mBindingObject.getMaxLength(), getMaxLength(view)); 133 } 134 } 135 136 changeValues(); 137 138 assertEquals(mBindingObject.getAutoLink(), view.getAutoLinkMask()); 139 assertEquals(mBindingObject.getDrawablePadding(), view.getCompoundDrawablePadding()); 140 assertEquals(mBindingObject.getTextSize(), view.getTextSize()); 141 assertEquals(mBindingObject.getTextColorHint(), view.getHintTextColors().getDefaultColor()); 142 assertEquals(mBindingObject.getTextColorLink(), view.getLinkTextColors().getDefaultColor()); 143 assertEquals(mBindingObject.isAutoText(), isAutoTextEnabled(view)); 144 assertEquals(mBindingObject.getCapitalize(), getCapitalization(view)); 145 assertEquals(mBindingObject.getImeActionLabel(), view.getImeActionLabel()); 146 assertEquals(mBindingObject.getImeActionId(), view.getImeActionId()); 147 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 148 assertEquals(mBindingObject.getTextColorHighlight(), view.getHighlightColor()); 149 assertEquals(mBindingObject.getLineSpacingExtra(), view.getLineSpacingExtra()); 150 assertEquals(mBindingObject.getLineSpacingMultiplier(), 151 view.getLineSpacingMultiplier()); 152 assertEquals(mBindingObject.getShadowColor(), view.getShadowColor()); 153 assertEquals(mBindingObject.getShadowDx(), view.getShadowDx()); 154 assertEquals(mBindingObject.getShadowDy(), view.getShadowDy()); 155 assertEquals(mBindingObject.getShadowRadius(), view.getShadowRadius()); 156 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 157 assertEquals(mBindingObject.getMaxLength(), getMaxLength(view)); 158 } 159 } 160 161 runTestOnUiThread(new Runnable() { 162 @Override 163 public void run() { 164 mBindingObject.setCapitalize(TextKeyListener.Capitalize.CHARACTERS); 165 mBinder.executePendingBindings(); 166 } 167 }); 168 169 assertEquals(mBindingObject.getCapitalize(), getCapitalization(view)); 170 171 runTestOnUiThread(new Runnable() { 172 @Override 173 public void run() { 174 mBindingObject.setCapitalize(TextKeyListener.Capitalize.WORDS); 175 mBinder.executePendingBindings(); 176 } 177 }); 178 179 assertEquals(mBindingObject.getCapitalize(), getCapitalization(view)); 180 } 181 182 private static boolean isAutoTextEnabled(TextView view) { 183 KeyListener keyListener = view.getKeyListener(); 184 if (keyListener == null) { 185 return false; 186 } 187 if (!(keyListener instanceof TextKeyListener)) { 188 return false; 189 } 190 TextKeyListener textKeyListener = (TextKeyListener) keyListener; 191 return ((textKeyListener.getInputType() & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) != 0); 192 } 193 194 private static TextKeyListener.Capitalize getCapitalization(TextView view) { 195 KeyListener keyListener = view.getKeyListener(); 196 if (keyListener == null) { 197 return TextKeyListener.Capitalize.NONE; 198 } 199 int inputType = keyListener.getInputType(); 200 if ((inputType & InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS) != 0) { 201 return TextKeyListener.Capitalize.CHARACTERS; 202 } else if ((inputType & InputType.TYPE_TEXT_FLAG_CAP_WORDS) != 0) { 203 return TextKeyListener.Capitalize.WORDS; 204 } else if ((inputType & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) != 0) { 205 return TextKeyListener.Capitalize.SENTENCES; 206 } else { 207 return TextKeyListener.Capitalize.NONE; 208 } 209 } 210 211 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 212 private static int getMaxLength(TextView view) { 213 InputFilter[] filters = view.getFilters(); 214 for (InputFilter filter : filters) { 215 if (filter instanceof InputFilter.LengthFilter) { 216 InputFilter.LengthFilter lengthFilter = (InputFilter.LengthFilter) filter; 217 return lengthFilter.getMax(); 218 } 219 } 220 return -1; 221 } 222 223 public void testAllCaps() throws Throwable { 224 TextView view = mBinder.textAllCaps; 225 226 assertEquals(mBindingObject.isTextAllCaps(), view.getTransformationMethod() != null); 227 if (view.getTransformationMethod() != null) { 228 assertEquals("ALL CAPS", 229 view.getTransformationMethod().getTransformation("all caps", view)); 230 } 231 232 changeValues(); 233 234 assertEquals(mBindingObject.isTextAllCaps(), view.getTransformationMethod() != null); 235 if (view.getTransformationMethod() != null) { 236 assertEquals("ALL CAPS", 237 view.getTransformationMethod().getTransformation("all caps", view)); 238 } 239 } 240 241 public void testBufferType() throws Throwable { 242 TextView view = mBinder.textBufferType; 243 244 assertEquals(mBindingObject.getBufferType(), getBufferType(view)); 245 changeValues(); 246 assertEquals(mBindingObject.getBufferType(), getBufferType(view)); 247 } 248 249 private static TextView.BufferType getBufferType(TextView view) { 250 CharSequence text = view.getText(); 251 if (text instanceof Editable) { 252 return TextView.BufferType.EDITABLE; 253 } 254 if (text instanceof Spannable) { 255 return TextView.BufferType.SPANNABLE; 256 } 257 return TextView.BufferType.NORMAL; 258 } 259 260 public void testInputType() throws Throwable { 261 TextView view = mBinder.textInputType; 262 assertEquals(mBindingObject.getInputType(), view.getInputType()); 263 changeValues(); 264 assertEquals(mBindingObject.getInputType(), view.getInputType()); 265 } 266 267 public void testDigits() throws Throwable { 268 TextView view = mBinder.textDigits; 269 assertEquals(mBindingObject.getDigits(), getDigits(view)); 270 changeValues(); 271 assertEquals(mBindingObject.getDigits(), getDigits(view)); 272 } 273 274 private static String getDigits(TextView textView) { 275 KeyListener keyListener = textView.getKeyListener(); 276 if (!(keyListener instanceof DigitsKeyListener)) { 277 return null; 278 } 279 DigitsKeyListener digitsKeyListener = (DigitsKeyListener) keyListener; 280 String input = "abcdefghijklmnopqrstuvwxyz"; 281 Spannable spannable = Spannable.Factory.getInstance().newSpannable(input); 282 return digitsKeyListener.filter(input, 0, input.length(), spannable, 0, input.length()) 283 .toString(); 284 } 285 286 public void testPhoneNumber() throws Throwable { 287 TextView textView = mBinder.textPhoneNumber; 288 assertEquals(mBindingObject.isPhoneNumber(), isPhoneNumber(textView)); 289 changeValues(); 290 assertEquals(mBindingObject.isPhoneNumber(), isPhoneNumber(textView)); 291 } 292 293 private static boolean isPhoneNumber(TextView view) { 294 KeyListener keyListener = view.getKeyListener(); 295 return (keyListener instanceof DialerKeyListener); 296 } 297 298 public void testInputMethod() throws Throwable { 299 TextView textView = mBinder.textInputMethod; 300 assertTrue(TextViewBindingObject.KeyListener1.class.isInstance(textView.getKeyListener())); 301 changeValues(); 302 assertTrue(TextViewBindingObject.KeyListener2.class.isInstance(textView.getKeyListener())); 303 } 304 305 @UiThreadTest 306 public void testTextWithTheme() throws Throwable { 307 TextView textView = mBinder.textWithTheme; 308 assertNotNull(textView.getTextColors()); 309 } 310 311 @UiThreadTest 312 public void testTextWithColor() throws Throwable { 313 TextView textView = mBinder.textWithColor; 314 int expectedColor = mBinder.getRoot().getResources().getColor( 315 android.R.color.holo_blue_bright); 316 assertEquals(expectedColor, textView.getCurrentTextColor()); 317 } 318 } 319