Home | History | Annotate | Download | only in cts
      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.fail;
     21 
     22 import android.support.test.annotation.UiThreadTest;
     23 import android.support.test.filters.MediumTest;
     24 import android.support.test.rule.ActivityTestRule;
     25 import android.support.test.runner.AndroidJUnit4;
     26 import android.text.method.ReplacementTransformationMethod;
     27 import android.util.TypedValue;
     28 import android.widget.EditText;
     29 
     30 import org.junit.Before;
     31 import org.junit.Rule;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 
     35 /**
     36  * Test {@link ReplacementTransformationMethod}.
     37  */
     38 @MediumTest
     39 @RunWith(AndroidJUnit4.class)
     40 public class ReplacementTransformationMethodTest {
     41     private final char[] ORIGINAL = new char[] { '0', '1' };
     42     private final char[] ORIGINAL_WITH_MORE_CHARS = new char[] { '0', '1', '2' };
     43     private final char[] ORIGINAL_WITH_SAME_CHARS = new char[] { '0', '0' };
     44     private final char[] REPLACEMENT = new char[] { '3', '4' };
     45     private final char[] REPLACEMENT_WITH_MORE_CHARS = new char[] { '3', '4', '5' };
     46     private final char[] REPLACEMENT_WITH_SAME_CHARS = new char[] { '3', '3' };
     47 
     48     private EditText mEditText;
     49 
     50     @Rule
     51     public ActivityTestRule<CtsActivity> mActivityRule = new ActivityTestRule<>(CtsActivity.class);
     52 
     53     @UiThreadTest
     54     @Before
     55     public void setup() throws Throwable {
     56         mEditText = new EditTextNoIme(mActivityRule.getActivity());
     57         mEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
     58     }
     59 
     60     @Test
     61     public void testGetTransformation() {
     62         MyReplacementTransformationMethod method =
     63             new MyReplacementTransformationMethod(ORIGINAL, REPLACEMENT);
     64         CharSequence result = method.getTransformation("010101", null);
     65         assertEquals("343434", result.toString());
     66 
     67         mEditText.setTransformationMethod(method);
     68         mEditText.setText("010101");
     69         // TODO cannot get transformed text from the view
     70     }
     71 
     72     @Test
     73     public void testGetTransformationWithAbnormalCharSequence() {
     74         ReplacementTransformationMethod method = new MyReplacementTransformationMethod(ORIGINAL,
     75                 REPLACEMENT);
     76 
     77         try {
     78             method.getTransformation(null, null);
     79             fail("The method should check whether the char sequence is null.");
     80         } catch (NullPointerException e) {
     81             // expected
     82         }
     83 
     84         assertEquals("", method.getTransformation("", null).toString());
     85     }
     86 
     87     @Test
     88     public void testGetTransformationWithAbmornalReplacement() {
     89         // replacement has same chars
     90         ReplacementTransformationMethod method =
     91             new MyReplacementTransformationMethod(ORIGINAL, REPLACEMENT_WITH_SAME_CHARS);
     92         assertEquals("333333", method.getTransformation("010101", null).toString());
     93 
     94         mEditText.setTransformationMethod(method);
     95         mEditText.setText("010101");
     96         // TODO cannot get transformed text from the view
     97 
     98         // replacement has more chars than original
     99         method = new MyReplacementTransformationMethod(ORIGINAL, REPLACEMENT_WITH_MORE_CHARS);
    100         assertEquals("343434", method.getTransformation("010101", null).toString());
    101 
    102         mEditText.setTransformationMethod(method);
    103         mEditText.setText("010101");
    104         // TODO cannot get transformed text from the view
    105     }
    106 
    107     @Test
    108     public void testGetTransformationWithAbnormalOriginal() {
    109         // original has same chars
    110         ReplacementTransformationMethod method =
    111                 new MyReplacementTransformationMethod(ORIGINAL_WITH_SAME_CHARS, REPLACEMENT);
    112         assertEquals("414141", method.getTransformation("010101", null).toString());
    113 
    114         mEditText.setTransformationMethod(method);
    115         mEditText.setText("010101");
    116         // TODO cannot get transformed text from the view
    117     }
    118 
    119     @Test(expected=ArrayIndexOutOfBoundsException.class)
    120     public void testGetTransformationMismatchCharCount() {
    121         // original has more chars than replacement
    122         ReplacementTransformationMethod method =
    123                 new MyReplacementTransformationMethod(ORIGINAL_WITH_MORE_CHARS, REPLACEMENT);
    124         method.getTransformation("012012012", null);
    125     }
    126 
    127     @Test
    128     public void testOnFocusChanged() {
    129         ReplacementTransformationMethod method = new MyReplacementTransformationMethod(ORIGINAL,
    130                 REPLACEMENT);
    131         // blank method
    132         method.onFocusChanged(null, null, true, 0, null);
    133     }
    134 
    135     private static class MyReplacementTransformationMethod extends ReplacementTransformationMethod {
    136         private char[] mOriginal;
    137 
    138         private char[] mReplacement;
    139 
    140         public MyReplacementTransformationMethod(char[] original, char[] replacement) {
    141             mOriginal = original;
    142             mReplacement = replacement;
    143         }
    144 
    145         @Override
    146         protected char[] getOriginal() {
    147             return mOriginal;
    148         }
    149 
    150         @Override
    151         protected char[] getReplacement() {
    152             return mReplacement;
    153         }
    154     }
    155 }
    156