aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowTypefaceTest.java
blob: 9a42b551841f644a6e9600ef1ceb3843253d82b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.O_MR1;
import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.Q;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.robolectric.Shadows.shadowOf;

import android.graphics.Typeface;
import android.graphics.fonts.Font;
import android.graphics.fonts.FontFamily;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog.LogItem;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.TestUtil;

@RunWith(AndroidJUnit4.class)
public class ShadowTypefaceTest {

  private File fontFile;

  @Before
  public void setup() {
    fontFile = TestUtil.resourcesBaseDir().resolve("assets/myFont.ttf").toFile();
  }

  @Test
  public void create_withFamilyName_shouldCreateTypeface() {
    Typeface typeface = Typeface.create("roboto", Typeface.BOLD);
    assertThat(typeface.getStyle()).isEqualTo(Typeface.BOLD);
    assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("roboto");
    assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(Typeface.BOLD);
  }

  @Test
  public void create_withFamily_shouldCreateTypeface() {
    Typeface typeface = Typeface.create(Typeface.create("roboto", Typeface.BOLD), Typeface.ITALIC);

    assertThat(typeface.getStyle()).isEqualTo(Typeface.ITALIC);
    assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("roboto");
    assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(Typeface.ITALIC);
  }

  @Test
  public void create_withoutFamily_shouldCreateTypeface() {
    Typeface typeface = Typeface.create((Typeface) null, Typeface.ITALIC);
    assertThat(typeface.getStyle()).isEqualTo(Typeface.ITALIC);
    assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo(null);
    assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(Typeface.ITALIC);
  }

  @Test
  @Config(minSdk = P)
  public void create_withFamily_customWeight_shouldCreateTypeface() {
    Typeface typeface =
        Typeface.create(
            Typeface.create("roboto", Typeface.NORMAL), /* weight= */ 400, /* italic= */ false);
    assertThat(typeface.getStyle()).isEqualTo(400);
    assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("roboto");
    assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(400);
  }

  @Test
  @Config(minSdk = P)
  public void create_withoutFamily_customWeight_shouldCreateTypeface() {
    Typeface typeface = Typeface.create(null, /* weight= */ 500, /* italic= */ false);
    assertThat(typeface.getStyle()).isEqualTo(500);
    assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo(null);
    assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(500);
  }

  @Test
  public void createFromFile_withFile_shouldCreateTypeface() {
    Typeface typeface = Typeface.createFromFile(fontFile);

    assertThat(typeface.getStyle()).isEqualTo(Typeface.NORMAL);
    assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("myFont.ttf");
  }

  @Test
  public void createFromFile_withPath_shouldCreateTypeface() {
    Typeface typeface = Typeface.createFromFile(fontFile.getPath());

    assertThat(typeface.getStyle()).isEqualTo(Typeface.NORMAL);
    assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("myFont.ttf");
    assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(Typeface.NORMAL);
  }

  @Test
  public void createFromAsset_shouldCreateTypeface() {
    Typeface typeface =
        Typeface.createFromAsset(
            ApplicationProvider.getApplicationContext().getAssets(), "myFont.ttf");

    assertThat(typeface.getStyle()).isEqualTo(Typeface.NORMAL);
    assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("myFont.ttf");
    assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(Typeface.NORMAL);
  }

  @Test
  public void createFromAsset_throwsExceptionWhenFontNotFound() {
    try {
      Typeface.createFromAsset(
          ApplicationProvider.getApplicationContext().getAssets(), "nonexistent.ttf");
      fail("Expected exception");
    } catch (RuntimeException expected) {
      // Expected
    }
  }

  @Test
  public void equals_bothRoboto_shouldBeTrue() {
    Typeface roboto = Typeface.create("roboto", Typeface.BOLD);
    assertThat(roboto).isEqualTo(Typeface.create("roboto", Typeface.BOLD));
  }

  @Test
  public void equals_robotoAndDroid_shouldBeFalse() {
    Typeface roboto = Typeface.create("roboto", Typeface.BOLD);
    Typeface droid = Typeface.create("droid", Typeface.BOLD);
    assertThat(roboto).isNotEqualTo(droid);
  }

  @Test
  public void hashCode_bothRoboto_shouldBeEqual() {
    Typeface roboto = Typeface.create("roboto", Typeface.BOLD);
    assertThat(roboto.hashCode()).isEqualTo(Typeface.create("roboto", Typeface.BOLD).hashCode());
  }

  @Test
  public void hashCode_robotoAndDroid_shouldNotBeEqual() {
    Typeface roboto = Typeface.create("roboto", Typeface.BOLD);
    Typeface droid = Typeface.create("droid", Typeface.BOLD);
    assertThat(roboto.hashCode()).isNotEqualTo(droid.hashCode());
  }

  /** Check that there is no spurious error message about /system/etc/fonts.xml */
  @Test
  @Config(minSdk = LOLLIPOP, maxSdk = O_MR1)
  public void init_shouldNotComplainAboutSystemFonts() {
    ShadowLog.clear();
    ReflectionHelpers.callStaticMethod(Typeface.class, "init");
    List<LogItem> logs = ShadowLog.getLogsForTag("Typeface");
    assertThat(logs).isEmpty();
  }

  @Test
  @Config(minSdk = Q)
  public void typeface_customFallbackBuilder_afterReset() throws IOException {
    Font font = new Font.Builder(fontFile).build();
    FontFamily family = new FontFamily.Builder(font).build();
    // This invokes the Typeface static initializer, which creates some default typefaces.
    Typeface.create("roboto", Typeface.BOLD);
    // Call the resetter to clear the FONTS map in Typeface
    ShadowLegacyTypeface.reset();
    Typeface typeface =
        new Typeface.CustomFallbackBuilder(family).setStyle(font.getStyle()).build();
    assertThat(typeface).isNotNull();
  }

  @Test
  @Config(minSdk = Q)
  public void createTypeface_withCustomFallbackBuilder() throws IOException {
    Font font = new Font.Builder(fontFile).build();
    FontFamily family = new FontFamily.Builder(font).build();
    Typeface typeface =
        new Typeface.CustomFallbackBuilder(family).setStyle(font.getStyle()).build();
    Typeface typeface2 = Typeface.create(typeface, Typeface.BOLD);
    assertThat(typeface2).isNotNull();
    assertThat(typeface2.toString()).isNotEmpty();
  }
}