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