Commit ad115a5a by ibaker Committed by Oliver Woodman

Fix some boolean logic in TtmlStyle#inherit

I got confused copying the hasBackgroundColor logic in
https://github.com/google/ExoPlayer/commit/3aa52c231720eaed88cdf27eff0f97d4bcf7625f

Add tests to confirm I got it right this time

PiperOrigin-RevId: 292898421
parent 49fa6d63
......@@ -24,7 +24,6 @@ import com.google.android.exoplayer2.text.Cue.VerticalType;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/**
* Style object of a <code>TtmlNode</code>
......@@ -62,7 +61,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
private static final int OFF = 0;
private static final int ON = 1;
private @MonotonicNonNull String fontFamily;
@Nullable private String fontFamily;
private int fontColor;
private boolean hasFontColor;
private int backgroundColor;
......@@ -73,8 +72,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@OptionalBoolean private int italic;
@FontSizeUnit private int fontSizeUnit;
private float fontSize;
private @MonotonicNonNull String id;
private Layout.@MonotonicNonNull Alignment textAlign;
@Nullable private String id;
@Nullable private Layout.Alignment textAlign;
@OptionalBoolean private int textCombine;
@Cue.VerticalType private int verticalType;
......@@ -84,6 +83,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
bold = UNSPECIFIED;
italic = UNSPECIFIED;
fontSizeUnit = UNSPECIFIED;
textCombine = UNSPECIFIED;
verticalType = Cue.TYPE_UNSET;
}
......@@ -134,7 +134,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
return fontFamily;
}
public TtmlStyle setFontFamily(String fontFamily) {
public TtmlStyle setFontFamily(@Nullable String fontFamily) {
this.fontFamily = fontFamily;
return this;
}
......@@ -228,14 +228,14 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
if (chaining && !hasBackgroundColor && ancestor.hasBackgroundColor) {
setBackgroundColor(ancestor.backgroundColor);
}
if (chaining && verticalType != Cue.TYPE_UNSET && ancestor.verticalType == Cue.TYPE_UNSET) {
setVerticalType(ancestor.verticalType);
if (chaining && verticalType == Cue.TYPE_UNSET) {
verticalType = ancestor.verticalType;
}
}
return this;
}
public TtmlStyle setId(String id) {
public TtmlStyle setId(@Nullable String id) {
this.id = id;
return this;
}
......@@ -250,7 +250,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
return textAlign;
}
public TtmlStyle setTextAlign(Layout.Alignment textAlign) {
public TtmlStyle setTextAlign(@Nullable Layout.Alignment textAlign) {
this.textAlign = textAlign;
return this;
}
......
......@@ -16,7 +16,6 @@
package com.google.android.exoplayer2.text.ttml;
import static android.graphics.Color.BLACK;
import static android.graphics.Color.WHITE;
import static com.google.android.exoplayer2.text.ttml.TtmlStyle.STYLE_BOLD;
import static com.google.android.exoplayer2.text.ttml.TtmlStyle.STYLE_BOLD_ITALIC;
import static com.google.android.exoplayer2.text.ttml.TtmlStyle.STYLE_ITALIC;
......@@ -26,8 +25,10 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import android.graphics.Color;
import android.text.Layout;
import androidx.annotation.ColorInt;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import com.google.android.exoplayer2.text.Cue;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -35,58 +36,83 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public final class TtmlStyleTest {
private static final String FONT_FAMILY = "serif";
private static final String ID = "id";
public static final int FOREGROUND_COLOR = Color.WHITE;
public static final int BACKGROUND_COLOR = Color.BLACK;
private TtmlStyle style;
@Before
public void setUp() throws Exception {
style = new TtmlStyle();
}
private static final String FONT_FAMILY = "serif";
@ColorInt private static final int FONT_COLOR = Color.WHITE;
private static final float FONT_SIZE = 12.5f;
@TtmlStyle.FontSizeUnit private static final int FONT_SIZE_UNIT = TtmlStyle.FONT_SIZE_UNIT_EM;
@ColorInt private static final int BACKGROUND_COLOR = Color.BLACK;
private static final Layout.Alignment TEXT_ALIGN = Layout.Alignment.ALIGN_CENTER;
private static final boolean TEXT_COMBINE = true;
@Cue.VerticalType private static final int VERTICAL_TYPE = Cue.VERTICAL_TYPE_RL;
private final TtmlStyle populatedStyle =
new TtmlStyle()
.setId(ID)
.setItalic(true)
.setBold(true)
.setBackgroundColor(BACKGROUND_COLOR)
.setFontColor(FONT_COLOR)
.setLinethrough(true)
.setUnderline(true)
.setFontFamily(FONT_FAMILY)
.setFontSize(FONT_SIZE)
.setFontSizeUnit(FONT_SIZE_UNIT)
.setTextAlign(TEXT_ALIGN)
.setTextCombine(TEXT_COMBINE)
.setVerticalType(VERTICAL_TYPE);
@Test
public void testInheritStyle() {
style.inherit(createAncestorStyle());
TtmlStyle style = new TtmlStyle();
style.inherit(populatedStyle);
assertWithMessage("id must not be inherited").that(style.getId()).isNull();
assertThat(style.isUnderline()).isTrue();
assertThat(style.isLinethrough()).isTrue();
assertThat(style.getStyle()).isEqualTo(STYLE_BOLD_ITALIC);
assertThat(style.getFontFamily()).isEqualTo(FONT_FAMILY);
assertThat(style.getFontColor()).isEqualTo(WHITE);
assertWithMessage("do not inherit backgroundColor").that(style.hasBackgroundColor()).isFalse();
assertThat(style.getFontColor()).isEqualTo(FONT_COLOR);
assertThat(style.getFontSize()).isEqualTo(FONT_SIZE);
assertThat(style.getFontSizeUnit()).isEqualTo(FONT_SIZE_UNIT);
assertThat(style.getTextAlign()).isEqualTo(TEXT_ALIGN);
assertThat(style.getTextCombine()).isEqualTo(TEXT_COMBINE);
assertWithMessage("backgroundColor should not be inherited")
.that(style.hasBackgroundColor())
.isFalse();
assertWithMessage("verticalType should not be inherited")
.that(style.getVerticalType())
.isEqualTo(Cue.TYPE_UNSET);
}
@Test
public void testChainStyle() {
style.chain(createAncestorStyle());
TtmlStyle style = new TtmlStyle();
style.chain(populatedStyle);
assertWithMessage("id must not be inherited").that(style.getId()).isNull();
assertThat(style.isUnderline()).isTrue();
assertThat(style.isLinethrough()).isTrue();
assertThat(style.getStyle()).isEqualTo(STYLE_BOLD_ITALIC);
assertThat(style.getFontFamily()).isEqualTo(FONT_FAMILY);
assertThat(style.getFontColor()).isEqualTo(FOREGROUND_COLOR);
// do inherit backgroundColor when chaining
assertWithMessage("do not inherit backgroundColor when chaining")
.that(style.getBackgroundColor()).isEqualTo(BACKGROUND_COLOR);
}
private static TtmlStyle createAncestorStyle() {
TtmlStyle ancestor = new TtmlStyle();
ancestor.setId(ID);
ancestor.setItalic(true);
ancestor.setBold(true);
ancestor.setBackgroundColor(BACKGROUND_COLOR);
ancestor.setFontColor(FOREGROUND_COLOR);
ancestor.setLinethrough(true);
ancestor.setUnderline(true);
ancestor.setFontFamily(FONT_FAMILY);
return ancestor;
assertThat(style.getFontColor()).isEqualTo(FONT_COLOR);
assertThat(style.getFontSize()).isEqualTo(FONT_SIZE);
assertThat(style.getFontSizeUnit()).isEqualTo(FONT_SIZE_UNIT);
assertThat(style.getTextAlign()).isEqualTo(TEXT_ALIGN);
assertThat(style.getTextCombine()).isEqualTo(TEXT_COMBINE);
assertWithMessage("backgroundColor should be chained")
.that(style.getBackgroundColor())
.isEqualTo(BACKGROUND_COLOR);
assertWithMessage("verticalType should be chained")
.that(style.getVerticalType())
.isEqualTo(VERTICAL_TYPE);
}
@Test
public void testStyle() {
TtmlStyle style = new TtmlStyle();
assertThat(style.getStyle()).isEqualTo(UNSPECIFIED);
style.setItalic(true);
assertThat(style.getStyle()).isEqualTo(STYLE_ITALIC);
......@@ -100,6 +126,8 @@ public final class TtmlStyleTest {
@Test
public void testLinethrough() {
TtmlStyle style = new TtmlStyle();
assertThat(style.isLinethrough()).isFalse();
style.setLinethrough(true);
assertThat(style.isLinethrough()).isTrue();
......@@ -109,6 +137,8 @@ public final class TtmlStyleTest {
@Test
public void testUnderline() {
TtmlStyle style = new TtmlStyle();
assertThat(style.isUnderline()).isFalse();
style.setUnderline(true);
assertThat(style.isUnderline()).isTrue();
......@@ -118,6 +148,8 @@ public final class TtmlStyleTest {
@Test
public void testFontFamily() {
TtmlStyle style = new TtmlStyle();
assertThat(style.getFontFamily()).isNull();
style.setFontFamily(FONT_FAMILY);
assertThat(style.getFontFamily()).isEqualTo(FONT_FAMILY);
......@@ -126,23 +158,47 @@ public final class TtmlStyleTest {
}
@Test
public void testColor() {
public void testFontColor() {
TtmlStyle style = new TtmlStyle();
assertThat(style.hasFontColor()).isFalse();
style.setFontColor(Color.BLACK);
assertThat(style.getFontColor()).isEqualTo(BLACK);
assertThat(style.hasFontColor()).isTrue();
assertThat(style.getFontColor()).isEqualTo(BLACK);
}
@Test
public void testFontSize() {
TtmlStyle style = new TtmlStyle();
assertThat(style.getFontSize()).isEqualTo(0);
style.setFontSize(10.5f);
assertThat(style.getFontSize()).isEqualTo(10.5f);
}
@Test
public void testFontSizeUnit() {
TtmlStyle style = new TtmlStyle();
assertThat(style.getFontSizeUnit()).isEqualTo(UNSPECIFIED);
style.setFontSizeUnit(TtmlStyle.FONT_SIZE_UNIT_EM);
assertThat(style.getFontSizeUnit()).isEqualTo(TtmlStyle.FONT_SIZE_UNIT_EM);
}
@Test
public void testBackgroundColor() {
TtmlStyle style = new TtmlStyle();
assertThat(style.hasBackgroundColor()).isFalse();
style.setBackgroundColor(Color.BLACK);
assertThat(style.getBackgroundColor()).isEqualTo(BLACK);
assertThat(style.hasBackgroundColor()).isTrue();
assertThat(style.getBackgroundColor()).isEqualTo(BLACK);
}
@Test
public void testId() {
TtmlStyle style = new TtmlStyle();
assertThat(style.getId()).isNull();
style.setId(ID);
assertThat(style.getId()).isEqualTo(ID);
......@@ -150,4 +206,23 @@ public final class TtmlStyleTest {
assertThat(style.getId()).isNull();
}
@Test
public void testTextAlign() {
TtmlStyle style = new TtmlStyle();
assertThat(style.getTextAlign()).isNull();
style.setTextAlign(Layout.Alignment.ALIGN_OPPOSITE);
assertThat(style.getTextAlign()).isEqualTo(Layout.Alignment.ALIGN_OPPOSITE);
style.setTextAlign(null);
assertThat(style.getTextAlign()).isNull();
}
@Test
public void testTextCombine() {
TtmlStyle style = new TtmlStyle();
assertThat(style.getTextCombine()).isFalse();
style.setTextCombine(true);
assertThat(style.getTextCombine()).isTrue();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment