Rename VTT voice span speakerName to name

This commit is contained in:
Michał Sikora 2024-08-27 19:36:29 +02:00 committed by Ian Baker
parent ce52fc77ec
commit 24bbe6d921
5 changed files with 38 additions and 45 deletions

View File

@ -21,8 +21,6 @@ import static androidx.media3.common.util.Assertions.checkNotNull;
import android.os.Bundle; import android.os.Bundle;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
/** /**
* A span representing a speaker. * A span representing a speaker.
@ -33,22 +31,22 @@ import java.util.Set;
@UnstableApi @UnstableApi
public final class VoiceSpan { public final class VoiceSpan {
/** The speaker name. */ /** The voice name. */
public final String speakerName; public final String name;
private static final String FIELD_NAME = Util.intToStringMaxRadix(0); private static final String FIELD_NAME = Util.intToStringMaxRadix(0);
public VoiceSpan(String speakerName) { public VoiceSpan(String name) {
this.speakerName = speakerName; this.name = name;
} }
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(FIELD_NAME, speakerName); bundle.putString(FIELD_NAME, name);
return bundle; return bundle;
} }
public static VoiceSpan fromBundle(Bundle bundle) { public static VoiceSpan fromBundle(Bundle bundle) {
return new VoiceSpan(/* speakerName = */ checkNotNull(bundle.getString(FIELD_NAME))); return new VoiceSpan(/* name = */ checkNotNull(bundle.getString(FIELD_NAME)));
} }
} }

View File

@ -98,7 +98,7 @@ public class CustomCueBundlerTest {
.hasVoiceSpanBetween( .hasVoiceSpanBetween(
ALL_SPANS_TO_START_END_INDEX.get(VOICE_SPAN).first, ALL_SPANS_TO_START_END_INDEX.get(VOICE_SPAN).first,
ALL_SPANS_TO_START_END_INDEX.get(VOICE_SPAN).second) ALL_SPANS_TO_START_END_INDEX.get(VOICE_SPAN).second)
.withSpeakerName(VOICE_SPAN.speakerName); .withName(VOICE_SPAN.name);
} }
@Test @Test

View File

@ -249,7 +249,7 @@ public final class WebvttCueParserTest {
assertThat(text.toString()).isEqualTo("Text with a single voice span"); assertThat(text.toString()).isEqualTo("Text with a single voice span");
assertThat(text) assertThat(text)
.hasVoiceSpanBetween(0, "Text with a single voice span".length()) .hasVoiceSpanBetween(0, "Text with a single voice span".length())
.withSpeakerName(""); .withName("");
} }
@Test @Test
@ -259,7 +259,7 @@ public final class WebvttCueParserTest {
assertThat(text.toString()).isEqualTo("Text with a single voice span"); assertThat(text.toString()).isEqualTo("Text with a single voice span");
assertThat(text) assertThat(text)
.hasVoiceSpanBetween(0, "Text with a single voice span".length()) .hasVoiceSpanBetween(0, "Text with a single voice span".length())
.withSpeakerName("Esme"); .withName("Esme");
} }
@Test @Test
@ -269,7 +269,7 @@ public final class WebvttCueParserTest {
assertThat(text.toString()).isEqualTo("Text with a single voice span"); assertThat(text.toString()).isEqualTo("Text with a single voice span");
assertThat(text) assertThat(text)
.hasVoiceSpanBetween(0, "Text with a single voice span".length()) .hasVoiceSpanBetween(0, "Text with a single voice span".length())
.withSpeakerName(""); .withName("");
} }
@Test @Test
@ -279,7 +279,7 @@ public final class WebvttCueParserTest {
assertThat(text.toString()).isEqualTo("Text with a single voice span"); assertThat(text.toString()).isEqualTo("Text with a single voice span");
assertThat(text) assertThat(text)
.hasVoiceSpanBetween(0, "Text with a single voice span".length()) .hasVoiceSpanBetween(0, "Text with a single voice span".length())
.withSpeakerName("Esme"); .withName("Esme");
} }
@Test @Test
@ -287,10 +287,10 @@ public final class WebvttCueParserTest {
Spanned text = parseCueText("<v.loud Esme>Text with </v><v.quiet Mary>multiple voice spans"); Spanned text = parseCueText("<v.loud Esme>Text with </v><v.quiet Mary>multiple voice spans");
assertThat(text.toString()).isEqualTo("Text with multiple voice spans"); assertThat(text.toString()).isEqualTo("Text with multiple voice spans");
assertThat(text).hasVoiceSpanBetween(0, "Text with ".length()).withSpeakerName("Esme"); assertThat(text).hasVoiceSpanBetween(0, "Text with ".length()).withName("Esme");
assertThat(text) assertThat(text)
.hasVoiceSpanBetween("Text with ".length(), "Text with multiple voice spans".length()) .hasVoiceSpanBetween("Text with ".length(), "Text with multiple voice spans".length())
.withSpeakerName("Mary"); .withName("Mary");
} }
private static Spanned parseCueText(String string) { private static Spanned parseCueText(String string) {

View File

@ -54,7 +54,6 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.RequiresNonNull; import org.checkerframework.checker.nullness.qual.RequiresNonNull;
/** A Truth {@link Subject} for assertions on {@link Spanned} instances containing text styling. */ /** A Truth {@link Subject} for assertions on {@link Spanned} instances containing text styling. */
@ -661,8 +660,8 @@ public final class SpannedSubject extends Subject {
} }
/** /**
* Checks that the subject has no {@link VoiceSpan}s on any of the text * Checks that the subject has no {@link VoiceSpan}s on any of the text between {@code start} and
* between {@code start} and {@code end}. * {@code end}.
* *
* <p>This fails even if the start and end indexes don't exactly match. * <p>This fails even if the start and end indexes don't exactly match.
* *
@ -1320,10 +1319,11 @@ public final class SpannedSubject extends Subject {
* @param name The expected name of the voice. * @param name The expected name of the voice.
* @return A {@link AndSpanFlags} object for optional additional assertions on the flags. * @return A {@link AndSpanFlags} object for optional additional assertions on the flags.
*/ */
AndSpanFlags withSpeakerName(String name); AndSpanFlags withName(String name);
} }
private static final VoiceText ALREADY_FAILED_WITH_NAME_AND_CLASSES = (name) -> ALREADY_FAILED_AND_FLAGS; private static final VoiceText ALREADY_FAILED_WITH_NAME_AND_CLASSES =
(name) -> ALREADY_FAILED_AND_FLAGS;
private static Factory<VoiceSpanSubject, List<VoiceSpan>> voiceSpanSubjects( private static Factory<VoiceSpanSubject, List<VoiceSpan>> voiceSpanSubjects(
Spanned actualSpanned) { Spanned actualSpanned) {
@ -1337,36 +1337,32 @@ public final class SpannedSubject extends Subject {
private final Spanned actualSpanned; private final Spanned actualSpanned;
private VoiceSpanSubject( private VoiceSpanSubject(
FailureMetadata metadata, FailureMetadata metadata, @Nullable List<VoiceSpan> actualSpans, Spanned actualSpanned) {
@Nullable List<VoiceSpan> actualSpans,
Spanned actualSpanned) {
super(metadata, actualSpans); super(metadata, actualSpans);
this.actualSpans = actualSpans; this.actualSpans = actualSpans;
this.actualSpanned = actualSpanned; this.actualSpanned = actualSpanned;
} }
@Override @Override
public AndSpanFlags withSpeakerName(String name) { public AndSpanFlags withName(String name) {
List<Integer> matchingSpanFlags = new ArrayList<>(); List<Integer> matchingSpanFlags = new ArrayList<>();
List<SpeakerName> voiceSpeakerName = new ArrayList<>(); List<Name> voiceName = new ArrayList<>();
for (VoiceSpan span : checkNotNull(actualSpans)) { for (VoiceSpan span : checkNotNull(actualSpans)) {
voiceSpeakerNameAndClasses.add(new SpeakerName(span.speakerName)); voiceName.add(new Name(span.name));
if (span.speakerName.equals(name)) { if (span.name.equals(name)) {
matchingSpanFlags.add(actualSpanned.getSpanFlags(span)); matchingSpanFlags.add(actualSpanned.getSpanFlags(span));
} }
} }
check("voiceSpeakerName") check("voiceName").that(voiceName).containsExactly(new Name(name));
.that(voiceSpeakerNameAndClasses)
.containsExactly(new SpeakerName(name));
return check("flags").about(spanFlags()).that(matchingSpanFlags); return check("flags").about(spanFlags()).that(matchingSpanFlags);
} }
private static final class SpeakerName { private static final class Name {
private final String speakerName; private final String name;
private SpeakerName(String name) { private Name(String name) {
this.speakerName = name; this.name = name;
} }
@Override @Override
@ -1378,18 +1374,18 @@ public final class SpannedSubject extends Subject {
return false; return false;
} }
SpeakerName that = (SpeakerName) o; Name that = (Name) o;
return name.equals(that.name); return name.equals(that.name);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(speakerName); return Objects.hash(name);
} }
@Override @Override
public String toString() { public String toString() {
return String.format("{speakerName=%s}", speakerName); return String.format("{name=%s}", name);
} }
} }
} }

View File

@ -46,7 +46,6 @@ import androidx.media3.test.utils.truth.SpannedSubject.AndSpanFlags;
import androidx.media3.test.utils.truth.SpannedSubject.WithSpanFlags; import androidx.media3.test.utils.truth.SpannedSubject.WithSpanFlags;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.common.truth.ExpectFailure; import com.google.common.truth.ExpectFailure;
import java.util.Set;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -913,7 +912,7 @@ public class SpannedSubjectTest {
assertThat(spannable) assertThat(spannable)
.hasVoiceSpanBetween(SPAN_START, SPAN_END) .hasVoiceSpanBetween(SPAN_START, SPAN_END)
.withSpeakerName("speaker") .withName("speaker")
.andFlags(Spanned.SPAN_INCLUSIVE_EXCLUSIVE); .andFlags(Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
} }
@ -925,7 +924,7 @@ public class SpannedSubjectTest {
} }
@Test @Test
public void voiceSpan_wrongSpeakerName() { public void voiceSpan_wrongName() {
SpannableString spannable = createSpannable(new VoiceSpan("speaker")); SpannableString spannable = createSpannable(new VoiceSpan("speaker"));
AssertionError expected = AssertionError expected =
@ -934,11 +933,11 @@ public class SpannedSubjectTest {
whenTesting whenTesting
.that(spannable) .that(spannable)
.hasVoiceSpanBetween(SPAN_START, SPAN_END) .hasVoiceSpanBetween(SPAN_START, SPAN_END)
.withSpeakerName("different speaker")); .withName("different speaker"));
assertThat(expected).factValue("value of").contains("voiceSpeakerName"); assertThat(expected).factValue("value of").contains("voiceName");
assertThat(expected).factValue("expected").contains("speakerName=different speaker"); assertThat(expected).factValue("expected").contains("name=different speaker");
assertThat(expected).factValue("but was").contains("speakerName=speaker"); assertThat(expected).factValue("but was").contains("name=speaker");
} }
@Test @Test
@ -948,7 +947,7 @@ public class SpannedSubjectTest {
(subject, start, end) -> (subject, start, end) ->
subject subject
.hasVoiceSpanBetween(start, end) .hasVoiceSpanBetween(start, end)
.withSpeakerName("speaker")); .withName("speaker"));
} }
@Test @Test