|
| 1 | +@file:Suppress("DEPRECATION") |
| 2 | + |
| 3 | +package org.wordpress.aztec.plugins.shortcodes |
| 4 | + |
| 5 | +import android.app.Activity |
| 6 | +import org.junit.Assert |
| 7 | +import org.junit.Before |
| 8 | +import org.junit.Test |
| 9 | +import org.junit.runner.RunWith |
| 10 | +import org.robolectric.Robolectric |
| 11 | +import org.robolectric.RobolectricTestRunner |
| 12 | +import org.robolectric.annotation.Config |
| 13 | +import org.wordpress.aztec.AztecText |
| 14 | +import org.wordpress.aztec.plugins.shortcodes.TestUtils.safeEmpty |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests for the video shortcode plugin |
| 18 | + */ |
| 19 | +@RunWith(RobolectricTestRunner::class) |
| 20 | +@Config(constants = BuildConfig::class, sdk = intArrayOf(25)) |
| 21 | +class VideoShortcodeTest { |
| 22 | + lateinit var editText: AztecText |
| 23 | + |
| 24 | + private val htmlGutenbergVideoBlock = |
| 25 | + "<!-- wp:video {\"id\":25} --><figure class=\"wp-block-video\"><video controls src=\"https://somedomain.com/wp-content/uploads/2017/05/VID-20170508-WA0011-1.mp4\"></video></figure><!-- /wp:video -->" |
| 26 | + |
| 27 | + private val htmlNormalVideoTag = |
| 28 | + "<figure class=\"wp-block-video\"><video controls src=\"https://somedomain.com/wp-content/uploads/2017/05/VID-20170508-WA0011-1.mp4\"></video></figure>" |
| 29 | + |
| 30 | + /** |
| 31 | + * Initialize variables. |
| 32 | + */ |
| 33 | + @Before |
| 34 | + fun init() { |
| 35 | + val activity = Robolectric.buildActivity(Activity::class.java).create().visible().get() |
| 36 | + |
| 37 | + editText = AztecText(activity) |
| 38 | + editText.setCalypsoMode(false) |
| 39 | + |
| 40 | + activity.setContentView(editText) |
| 41 | + |
| 42 | + editText.plugins.add(VideoShortcodePlugin()) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @Throws(Exception::class) |
| 47 | + fun testGutenbergVideoBlockDoesntConvertToShortcode() { |
| 48 | + Assert.assertTrue(safeEmpty(editText)) |
| 49 | + |
| 50 | + editText.fromHtml(htmlGutenbergVideoBlock) |
| 51 | + |
| 52 | + // expected result: the <video> tag does not get converted to shortcode when it's found within a Gutenberg block |
| 53 | + // Note: the slight difference of <video controls> being converted to <video controls="controls"> should be equivalent |
| 54 | + val htmlWithoutShortcode = "<!-- wp:video {\"id\":25} --><figure class=\"wp-block-video\"><video controls=\"controls\" src=\"https://somedomain.com/wp-content/uploads/2017/05/VID-20170508-WA0011-1.mp4\"></video></figure><!-- /wp:video -->" |
| 55 | + |
| 56 | + Assert.assertEquals(htmlWithoutShortcode, editText.toPlainHtml()) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @Throws(Exception::class) |
| 61 | + fun testAudioTagConvertsToShortcode() { |
| 62 | + Assert.assertTrue(safeEmpty(editText)) |
| 63 | + |
| 64 | + editText.fromHtml(htmlNormalVideoTag) |
| 65 | + |
| 66 | + // expected result: the <video> tag gets converted to shortcode when it's found in free HTML |
| 67 | + val htmlWithShortcode = "<figure class=\"wp-block-video\">[video controls=\"controls\" src=\"https://somedomain.com/wp-content/uploads/2017/05/VID-20170508-WA0011-1.mp4\"]</figure>" |
| 68 | + |
| 69 | + Assert.assertEquals(htmlWithShortcode, editText.toPlainHtml()) |
| 70 | + } |
| 71 | +} |
0 commit comments