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.style.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.os.Parcel;
     24 import android.text.SpannableString;
     25 import android.text.Spanned;
     26 import android.text.StaticLayout;
     27 import android.text.TextPaint;
     28 import android.text.style.StrikethroughSpan;
     29 
     30 import androidx.test.filters.SmallTest;
     31 import androidx.test.runner.AndroidJUnit4;
     32 
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 
     36 @SmallTest
     37 @RunWith(AndroidJUnit4.class)
     38 public class StrikethroughSpanTest {
     39     @Test
     40     public void testConstructor() {
     41         StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
     42 
     43         Parcel p = Parcel.obtain();
     44         try {
     45             strikethroughSpan.writeToParcel(p, 0);
     46             p.setDataPosition(0);
     47             new StrikethroughSpan(p);
     48         } finally {
     49             p.recycle();
     50         }
     51     }
     52 
     53     @Test
     54     public void testUpdateDrawState() {
     55         StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
     56 
     57         TextPaint tp = new TextPaint();
     58         tp.setStrikeThruText(false);
     59         assertFalse(tp.isStrikeThruText());
     60 
     61         strikethroughSpan.updateDrawState(tp);
     62         assertTrue(tp.isStrikeThruText());
     63     }
     64 
     65     @Test(expected=NullPointerException.class)
     66     public void testUpdateDrawStateNull() {
     67         StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
     68 
     69         strikethroughSpan.updateDrawState(null);
     70     }
     71 
     72     @Test
     73     public void testDescribeContents() {
     74         StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
     75         strikethroughSpan.describeContents();
     76     }
     77 
     78     @Test
     79     public void testGetSpanTypeId() {
     80         StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
     81         strikethroughSpan.getSpanTypeId();
     82     }
     83 
     84     @Test
     85     public void testWriteToParcel() {
     86         Parcel p = Parcel.obtain();
     87         try {
     88             StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
     89             strikethroughSpan.writeToParcel(p, 0);
     90             p.setDataPosition(0);
     91             new StrikethroughSpan(p);
     92         } finally {
     93             p.recycle();
     94         }
     95     }
     96 
     97     // Measures the width of some potentially-spanned text, assuming it's not too wide.
     98     private float textWidth(CharSequence text) {
     99         final TextPaint tp = new TextPaint();
    100         tp.setTextSize(100.0f); // Large enough so that the difference in kerning is visible.
    101         final int largeWidth = 10000; // Enough width so the whole text fits in one line.
    102         final StaticLayout layout = StaticLayout.Builder.obtain(
    103                 text, 0, text.length(), tp, largeWidth).build();
    104         return layout.getLineWidth(0);
    105     }
    106 
    107     @Test
    108     public void testDoesntAffectWidth() {
    109         // Roboto kerns between "P" and "."
    110         final SpannableString text = new SpannableString("P.");
    111         final float origLineWidth = textWidth(text);
    112         // Strike through just the "P".
    113         text.setSpan(new StrikethroughSpan(), 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    114         final float strokethroughLineWidth = textWidth(text);
    115         assertEquals(origLineWidth, strokethroughLineWidth, 0.0f);
    116     }
    117 }
    118