| 1 |
package ezvcard.io.scribe; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertArrayEquals; |
| 4 |
import static org.junit.Assert.assertEquals; |
| 5 |
import static org.junit.Assert.assertNull; |
| 6 |
|
| 7 |
import org.junit.Test; |
| 8 |
|
| 9 |
import ezvcard.io.scribe.Sensei.Check; |
| 10 |
import ezvcard.parameter.SoundType; |
| 11 |
import ezvcard.property.Sound; |
| 12 |
import ezvcard.util.DataUri; |
| 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 |
public class SoundScribeTest { |
| 47 |
private final SoundScribe scribe = new SoundScribe(); |
| 48 |
private final Sensei<Sound> sensei = new Sensei<>(scribe); |
| 49 |
|
| 50 |
private final String url = "http://example.com/song.mp3"; |
| 51 |
private final String urlUnknownExtension = "http://example.com/song.abc"; |
| 52 |
private final String urlWithoutExtension = "http://example.com/song"; |
| 53 |
private final byte[] data = "data".getBytes(); |
| 54 |
private final String dataUri = new DataUri("audio/mp3", data).toString(); |
| 55 |
|
| 56 |
@Test |
| 57 |
public void parseHtml() { |
| 58 |
sensei.assertParseHtml("<audio />").cannotParse(16); |
| 59 |
sensei.assertParseHtml("<audio><source /></audio>").cannotParse(17); |
| 60 |
sensei.assertParseHtml("<audio><source src=\"" + url + "\" /></audio>").run(hasUrl(url, SoundType.MP3)); |
| 61 |
sensei.assertParseHtml("<audio><source src=\"" + urlUnknownExtension + "\" /></audio>").run(hasUrl(urlUnknownExtension, null)); |
| 62 |
sensei.assertParseHtml("<audio><source src=\"" + urlWithoutExtension + "\" /></audio>").run(hasUrl(urlWithoutExtension, null)); |
| 63 |
sensei.assertParseHtml("<audio><source src=\"" + dataUri + "\" /></audio>").run(hasData(data, SoundType.MP3)); |
| 64 |
|
| 65 |
|
| 66 |
sensei.assertParseHtml("<source src=\"" + url + "\" />").run(hasUrl(url, SoundType.MP3)); |
| 67 |
|
| 68 |
|
| 69 |
sensei.assertParseHtml("<source type=\"audio/wav\" src=\"" + url + "\" />").run(hasUrl(url, SoundType.WAV)); |
| 70 |
|
| 71 |
|
| 72 |
sensei.assertParseHtml("<object type=\"audio/wav\" data=\"" + url + "\" />").run(hasUrl(url, SoundType.WAV)); |
| 73 |
} |
| 74 |
|
| 75 |
private Check<Sound> hasUrl(final String url, final SoundType contentType) { |
| 76 |
return actual -> { |
| 77 |
assertEquals(url, actual.getUrl()); |
| 78 |
assertNull(actual.getData()); |
| 79 |
assertEquals(contentType, actual.getContentType()); |
| 80 |
}; |
| 81 |
} |
| 82 |
|
| 83 |
private Check<Sound> hasData(final byte[] data, final SoundType contentType) { |
| 84 |
return actual -> { |
| 85 |
assertNull(actual.getUrl()); |
| 86 |
assertArrayEquals(data, actual.getData()); |
| 87 |
assertEquals(contentType, actual.getContentType()); |
| 88 |
}; |
| 89 |
} |
| 90 |
} |