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.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import android.app.Activity; 24 import android.support.test.annotation.UiThreadTest; 25 import android.support.test.filters.MediumTest; 26 import android.support.test.rule.ActivityTestRule; 27 import android.support.test.runner.AndroidJUnit4; 28 import android.text.Editable; 29 import android.text.Selection; 30 import android.text.method.CharacterPickerDialog; 31 import android.view.View; 32 import android.widget.Gallery; 33 34 import org.junit.Before; 35 import org.junit.Rule; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 @MediumTest 40 @RunWith(AndroidJUnit4.class) 41 public class CharacterPickerDialogTest { 42 private Activity mActivity; 43 44 @Rule 45 public ActivityTestRule<CtsActivity> mActivityRule = new ActivityTestRule<>(CtsActivity.class); 46 47 @Before 48 public void setup() { 49 mActivity = mActivityRule.getActivity(); 50 } 51 52 @UiThreadTest 53 @Test 54 public void testConstructor() { 55 final CharSequence str = "123456"; 56 final Editable content = Editable.Factory.getInstance().newEditable(str); 57 final View view = new TextViewNoIme(mActivity); 58 new CharacterPickerDialog(view.getContext(), view, content, "\u00A1", false); 59 } 60 61 @UiThreadTest 62 @Test(expected=NullPointerException.class) 63 public void testConstructorNullContext() { 64 final CharSequence str = "123456"; 65 final Editable content = Editable.Factory.getInstance().newEditable(str); 66 final View view = new TextViewNoIme(mActivity); 67 new CharacterPickerDialog(null, view, content, "\u00A1", false); 68 } 69 70 @UiThreadTest 71 @Test 72 public void testOnItemClick() { 73 final Gallery parent = new Gallery(mActivity); 74 final CharSequence str = "123456"; 75 Editable text = Editable.Factory.getInstance().newEditable(str); 76 final View view = new TextViewNoIme(mActivity); 77 CharacterPickerDialog replacePickerDialog = 78 new CharacterPickerDialog(view.getContext(), view, text, "abc", false); 79 80 // insert 'a' to the beginning of text 81 replacePickerDialog.show(); 82 Selection.setSelection(text, 0, 0); 83 assertEquals(str, text.toString()); 84 assertTrue(replacePickerDialog.isShowing()); 85 86 replacePickerDialog.onItemClick(parent, view, 0, 0); 87 assertEquals("a123456", text.toString()); 88 assertFalse(replacePickerDialog.isShowing()); 89 90 // replace the second character '1' with 'c' 91 replacePickerDialog.show(); 92 Selection.setSelection(text, 2, 2); 93 assertTrue(replacePickerDialog.isShowing()); 94 95 replacePickerDialog.onItemClick(parent, view, 2, 0); 96 assertEquals("ac23456", text.toString()); 97 assertFalse(replacePickerDialog.isShowing()); 98 99 // insert character 'c' between '2' and '3' 100 text = Editable.Factory.getInstance().newEditable(str); 101 CharacterPickerDialog insertPickerDialog = 102 new CharacterPickerDialog(view.getContext(), view, text, "abc", true); 103 Selection.setSelection(text, 2, 2); 104 assertEquals(str, text.toString()); 105 insertPickerDialog.show(); 106 assertTrue(insertPickerDialog.isShowing()); 107 108 insertPickerDialog.onItemClick(parent, view, 2, 0); 109 assertEquals("12c3456", text.toString()); 110 assertFalse(insertPickerDialog.isShowing()); 111 } 112 113 @UiThreadTest 114 @Test 115 public void testOnClick() { 116 final CharSequence str = "123456"; 117 final Editable content = Editable.Factory.getInstance().newEditable(str); 118 final View view = new TextViewNoIme(mActivity); 119 CharacterPickerDialog characterPickerDialog = 120 new CharacterPickerDialog(view.getContext(), view, content, "\u00A1", false); 121 122 characterPickerDialog.show(); 123 assertTrue(characterPickerDialog.isShowing()); 124 125 // nothing to test here, just make sure onClick does not throw exception 126 characterPickerDialog.onClick(view); 127 } 128 } 129