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.assertArrayEquals;
     20 import static org.junit.Assert.assertEquals;
     21 import static org.junit.Assert.assertNotNull;
     22 import static org.junit.Assert.assertSame;
     23 
     24 import android.support.test.InstrumentationRegistry;
     25 import android.support.test.annotation.UiThreadTest;
     26 import android.support.test.filters.SmallTest;
     27 import android.support.test.runner.AndroidJUnit4;
     28 import android.text.Layout;
     29 import android.text.SpannableString;
     30 import android.text.Spanned;
     31 import android.text.method.SingleLineTransformationMethod;
     32 import android.text.style.AlignmentSpan;
     33 import android.util.TypedValue;
     34 import android.widget.EditText;
     35 
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 
     39 /**
     40  * Test {@link SingleLineTransformationMethod}.
     41  */
     42 @SmallTest
     43 @RunWith(AndroidJUnit4.class)
     44 public class SingleLineTransformationMethodTest {
     45     @Test
     46     public void testConstructor() {
     47         new SingleLineTransformationMethod();
     48     }
     49 
     50     @Test
     51     public void testGetInstance() {
     52         SingleLineTransformationMethod method0 = SingleLineTransformationMethod.getInstance();
     53         assertNotNull(method0);
     54 
     55         SingleLineTransformationMethod method1 = SingleLineTransformationMethod.getInstance();
     56         assertSame(method0, method1);
     57     }
     58 
     59     @Test
     60     public void testGetReplacement() {
     61         MySingleLineTranformationMethod method = new MySingleLineTranformationMethod();
     62         assertArrayEquals(new char[] { ' ', '\uFEFF' }, method.getReplacement());
     63         assertArrayEquals(new char[] { '\n', '\r' }, method.getOriginal());
     64     }
     65 
     66     @UiThreadTest
     67     @Test
     68     public void testGetTransformation() {
     69         SingleLineTransformationMethod method = SingleLineTransformationMethod.getInstance();
     70         CharSequence result = method.getTransformation("hello\nworld\r", null);
     71         assertEquals("hello world\uFEFF", result.toString());
     72 
     73         EditText editText = new EditTextNoIme(InstrumentationRegistry.getTargetContext());
     74         editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
     75         editText.setText("hello\nworld\r");
     76         // TODO cannot get transformed text from the view
     77     }
     78 
     79     @Test
     80     public void testSubsequence_doesNotThrowExceptionWithParagraphSpans() {
     81         final SingleLineTransformationMethod method = SingleLineTransformationMethod.getInstance();
     82         final SpannableString original = new SpannableString("\ntest data\nb");
     83         final AlignmentSpan.Standard span = new AlignmentSpan.Standard(
     84                 Layout.Alignment.ALIGN_NORMAL);
     85         original.setSpan(span, 1, original.length() - 1, Spanned.SPAN_PARAGRAPH);
     86 
     87         final CharSequence transformed = method.getTransformation(original, null);
     88         // expectation: should not throw an exception
     89         transformed.subSequence(0, transformed.length());
     90     }
     91 
     92 
     93 
     94     private static class MySingleLineTranformationMethod extends SingleLineTransformationMethod {
     95         @Override
     96         protected char[] getOriginal() {
     97             return super.getOriginal();
     98         }
     99 
    100         @Override
    101         protected char[] getReplacement() {
    102             return super.getReplacement();
    103         }
    104     }
    105 }
    106