1 /* 2 * Copyright (C) 2008 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 17 package android.text.method.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotEquals; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertNotSame; 23 import static org.junit.Assert.assertSame; 24 25 import android.support.test.filters.MediumTest; 26 import android.support.test.runner.AndroidJUnit4; 27 import android.text.InputType; 28 import android.text.method.DateKeyListener; 29 import android.view.KeyEvent; 30 31 import com.android.compatibility.common.util.CtsKeyEventUtil; 32 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 import java.util.Locale; 37 38 /** 39 * Test {@link android.text.method.DateKeyListener}. 40 */ 41 @MediumTest 42 @RunWith(AndroidJUnit4.class) 43 public class DateKeyListenerTest extends KeyListenerTestCase { 44 @Test 45 public void testConstructor() { 46 // deprecated empty constructor 47 new DateKeyListener(); 48 49 // newer constructor that takes locales 50 new DateKeyListener(null); // fallback to old behavior 51 new DateKeyListener(Locale.US); 52 new DateKeyListener(Locale.forLanguageTag("fa-IR")); 53 } 54 55 @Test 56 public void testGetInstance() { 57 final DateKeyListener emptyListener1 = DateKeyListener.getInstance(); 58 final DateKeyListener emptyListener2 = DateKeyListener.getInstance(); 59 final DateKeyListener nullListener = DateKeyListener.getInstance(null); 60 61 assertNotNull(emptyListener1); 62 assertNotNull(emptyListener2); 63 assertNotNull(nullListener); 64 assertSame(emptyListener1, emptyListener2); 65 assertSame(emptyListener1, nullListener); 66 67 final DateKeyListener usListener1 = DateKeyListener.getInstance(Locale.US); 68 final DateKeyListener usListener2 = DateKeyListener.getInstance(new Locale("en", "US")); 69 final DateKeyListener irListener = DateKeyListener.getInstance( 70 Locale.forLanguageTag("fa-IR")); 71 72 assertNotNull(usListener1); 73 assertNotNull(usListener2); 74 assertNotNull(irListener); 75 assertSame(usListener1, usListener2); 76 assertNotSame(usListener1, irListener); 77 assertNotSame(usListener1, nullListener); 78 } 79 80 @Test 81 public void testGetAcceptedChars() { 82 assertNotNull(DateKeyListener.CHARACTERS); 83 84 final MockDateKeyListener emptyMockDateKeyListener = new MockDateKeyListener(); 85 assertSame(DateKeyListener.CHARACTERS, emptyMockDateKeyListener.getAcceptedChars()); 86 87 final MockDateKeyListener usMockDateKeyListener = new MockDateKeyListener(Locale.US); 88 assertNotSame(DateKeyListener.CHARACTERS, usMockDateKeyListener.getAcceptedChars()); 89 90 MockDateKeyListener irMockDateKeyListener = new MockDateKeyListener( 91 Locale.forLanguageTag("fa-IR")); 92 final String acceptedChars = new String(irMockDateKeyListener.getAcceptedChars()); 93 // Make sure all these chararacters are accepted. 94 final char[] expectedChars = { 95 '\u06F0', '\u06F1', '\u06F2', '\u06F3', '\u06F4', 96 '\u06F5', '\u06F6', '\u06F7', '\u06F8', '\u06F9', 97 '/' 98 }; 99 for (int i = 0; i < expectedChars.length; i++) { 100 assertNotEquals(-1, acceptedChars.indexOf(expectedChars[i])); 101 } 102 // Make sure all these chararacters are not accepted. 103 final char[] unexpectedChars = { 104 '0', '1', '2', '3', '4', 105 '5', '6', '7', '8', '9' 106 }; 107 for (int i = 0; i < unexpectedChars.length; i++) { 108 assertEquals(-1, acceptedChars.indexOf(unexpectedChars[i])); 109 } 110 } 111 112 @Test 113 public void testGetInputType() { 114 // The "normal" input type that has been used consistently until Android O. 115 final int dateTimeType = InputType.TYPE_CLASS_DATETIME 116 | InputType.TYPE_DATETIME_VARIATION_DATE; 117 // Fallback for locales that need more characters. 118 final int textType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL; 119 120 // Deprecated constructor that needs to preserve pre-existing behavior. 121 DateKeyListener dateKeyListener = new DateKeyListener(); 122 assertEquals(dateTimeType, dateKeyListener.getInputType()); 123 124 // TYPE_CLASS_DATETIME is fine for English locales. 125 dateKeyListener = new DateKeyListener(Locale.US); 126 assertEquals(dateTimeType, dateKeyListener.getInputType()); 127 dateKeyListener = new DateKeyListener(Locale.UK); 128 assertEquals(dateTimeType, dateKeyListener.getInputType()); 129 130 // Persian needs more characters then typically provided by datetime inputs, so it falls 131 // back on normal text. 132 dateKeyListener = new DateKeyListener(Locale.forLanguageTag("fa-IR")); 133 assertEquals(textType, dateKeyListener.getInputType()); 134 } 135 136 /* 137 * Scenario description: 138 * 1. Press '1' key and check if the content of TextView becomes "1" 139 * 2. Press '2' key and check if the content of TextView becomes "12" 140 * 3. Press an unaccepted key if it exists and this key will not be accepted. 141 * 4. Press '-' key and check if the content of TextView becomes "12-" 142 * 5. Press '/' key and check if the content of TextView becomes "12-/" 143 * 6. remove DateKeyListener and Press '/' key, this key will not be accepted 144 */ 145 @Test 146 public void testDateTimeKeyListener() { 147 final DateKeyListener dateKeyListener = DateKeyListener.getInstance(); 148 149 setKeyListenerSync(dateKeyListener); 150 assertEquals("", mTextView.getText().toString()); 151 152 // press '1' key. 153 CtsKeyEventUtil.sendKeys(mInstrumentation, mTextView, KeyEvent.KEYCODE_1); 154 assertEquals("1", mTextView.getText().toString()); 155 156 // press '2' key. 157 CtsKeyEventUtil.sendKeys(mInstrumentation, mTextView, KeyEvent.KEYCODE_2); 158 assertEquals("12", mTextView.getText().toString()); 159 160 // press an unaccepted key if it exists. 161 int keyCode = TextMethodUtils.getUnacceptedKeyCode(DateKeyListener.CHARACTERS); 162 if (-1 != keyCode) { 163 CtsKeyEventUtil.sendKeys(mInstrumentation, mTextView, keyCode); 164 assertEquals("12", mTextView.getText().toString()); 165 } 166 167 // press '-' key. 168 CtsKeyEventUtil.sendKeys(mInstrumentation, mTextView, KeyEvent.KEYCODE_MINUS); 169 assertEquals("12-", mTextView.getText().toString()); 170 171 // press '/' key. 172 CtsKeyEventUtil.sendKeys(mInstrumentation, mTextView, KeyEvent.KEYCODE_SLASH); 173 assertEquals("12-/", mTextView.getText().toString()); 174 175 // remove DateKeyListener 176 setKeyListenerSync(null); 177 assertEquals("12-/", mTextView.getText().toString()); 178 179 // press '/' key, it will not be accepted. 180 CtsKeyEventUtil.sendKeys(mInstrumentation, mTextView, KeyEvent.KEYCODE_SLASH); 181 assertEquals("12-/", mTextView.getText().toString()); 182 } 183 184 /** 185 * A mocked {@link android.text.method.DateKeyListener} for testing purposes. 186 * 187 * Allows {@link DateKeyListenerTest} to call 188 * {@link android.text.method.DateKeyListener#getAcceptedChars()}. 189 */ 190 private class MockDateKeyListener extends DateKeyListener { 191 MockDateKeyListener() { 192 super(); 193 } 194 195 MockDateKeyListener(Locale locale) { 196 super(locale); 197 } 198 199 @Override 200 protected char[] getAcceptedChars() { 201 return super.getAcceptedChars(); 202 } 203 } 204 } 205