Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2018 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.widget;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import android.app.Activity;
     24 import android.support.test.filters.MediumTest;
     25 import android.support.test.rule.ActivityTestRule;
     26 import android.text.DynamicLayout;
     27 import android.text.FontFallbackSetup;
     28 import android.text.Layout;
     29 import android.text.StaticLayout;
     30 import android.util.TypedValue;
     31 import android.view.View;
     32 import android.widget.TextView.BufferType;
     33 
     34 import org.junit.Rule;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.junit.runners.Parameterized;
     38 
     39 import java.util.Arrays;
     40 import java.util.Collection;
     41 
     42 /**
     43  * Parametrized test for TextView#setFallbackLineSpacing.
     44  */
     45 @MediumTest
     46 @RunWith(Parameterized.class)
     47 public class TextViewFallbackLineSpacingTest {
     48 
     49     @Parameterized.Parameters(name = "{0}")
     50     public static Collection layouts() {
     51         return Arrays.asList(new Object[][] {
     52                 // name, enabled, BufferType
     53                 { "Enabled - StaticLayout", true, BufferType.NORMAL},
     54                 { "Disabled - StaticLayout", false, BufferType.NORMAL},
     55                 { "Enabled - DynamicLayout", true, BufferType.EDITABLE},
     56                 { "Disabled - DynamicLayout", false, BufferType.EDITABLE},
     57         });
     58     }
     59 
     60     @Rule
     61     public ActivityTestRule<TextViewActivity> mActivityRule = new ActivityTestRule<>(
     62             TextViewActivity.class);
     63 
     64     private final boolean mEnabled;
     65     private final BufferType mBufferType;
     66 
     67     public TextViewFallbackLineSpacingTest(String testName, boolean enabled,
     68             BufferType bufferType) {
     69         mEnabled = enabled;
     70         mBufferType = bufferType;
     71     }
     72 
     73     @Test
     74     public void testFallbackLineSpacing() {
     75         // All glyphs in the fonts are 1em wide.
     76         final String[] testFontFiles = {
     77                 // ascent == 1em, descent == 2em, only supports 'a' and space
     78                 "ascent1em-descent2em.ttf",
     79                 // ascent == 3em, descent == 4em, only supports 'b'
     80                 "ascent3em-descent4em.ttf"
     81         };
     82         final String xml = "<?xml version='1.0' encoding='UTF-8'?>"
     83                 + "<familyset>"
     84                 + "  <family name='sans-serif'>"
     85                 + "    <font weight='400' style='normal'>ascent1em-descent2em.ttf</font>"
     86                 + "  </family>"
     87                 + "  <family>"
     88                 + "    <font weight='400' style='normal'>ascent3em-descent4em.ttf</font>"
     89                 + "  </family>"
     90                 + "</familyset>";
     91 
     92         try (FontFallbackSetup setup =
     93                      new FontFallbackSetup("DynamicLayout", testFontFiles, xml)) {
     94             final Activity activity = mActivityRule.getActivity();
     95             final TextView textView = new TextView(activity);
     96             textView.setTypeface(setup.getTypefaceFor("sans-serif"));
     97             textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 100);
     98             // This should result in three lines.
     99             textView.setText("aaaaa aabaa aaaaa", mBufferType);
    100             textView.setPadding(0, 0, 0, 0);
    101             textView.setIncludeFontPadding(false);
    102             textView.setFallbackLineSpacing(mEnabled);
    103 
    104             final int em = (int) Math.ceil(textView.getPaint().measureText("a"));
    105             final int width = 5 * em;
    106             final int height = 30 * em; // tall enough to not affect our other measurements
    107             textView.measure(
    108                     View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
    109                     View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
    110             textView.layout(0, 0, width, height);
    111 
    112             final Layout layout = textView.getLayout();
    113             assertNotNull(layout);
    114             if (mBufferType == BufferType.NORMAL) {
    115                 assertTrue(layout instanceof StaticLayout);
    116             } else {
    117                 assertTrue(layout instanceof DynamicLayout);
    118             }
    119             assertEquals(3, layout.getLineCount());
    120 
    121             assertEquals(-em, layout.getLineAscent(0));
    122             assertEquals(2 * em, layout.getLineDescent(0));
    123 
    124             if (mEnabled) {
    125                 // The second line has a 'b', so it needs more ascent and descent.
    126                 assertEquals(-3 * em, layout.getLineAscent(1));
    127                 assertEquals(4 * em, layout.getLineDescent(1));
    128             } else {
    129                 // old behavior
    130                 assertEquals(-em, layout.getLineAscent(1));
    131                 assertEquals(2 * em, layout.getLineDescent(1));
    132             }
    133 
    134             assertEquals(-em, layout.getLineAscent(2));
    135             assertEquals(2 * em, layout.getLineDescent(2));
    136         }
    137     }
    138 }
    139