|
| 1 | +package org.wordpress.aztec.placeholders |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.text.BoringLayout |
| 5 | +import android.text.Layout |
| 6 | +import android.text.TextPaint |
| 7 | +import android.view.MenuItem |
| 8 | +import android.view.View |
| 9 | +import android.view.ViewGroup.LayoutParams.MATCH_PARENT |
| 10 | +import android.widget.FrameLayout |
| 11 | +import android.widget.PopupMenu |
| 12 | +import kotlinx.coroutines.runBlocking |
| 13 | +import org.junit.Assert |
| 14 | +import org.junit.Before |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import org.robolectric.Robolectric |
| 18 | +import org.robolectric.RobolectricTestRunner |
| 19 | +import org.wordpress.aztec.AztecAttributes |
| 20 | +import org.wordpress.aztec.AztecText |
| 21 | +import org.wordpress.aztec.source.SourceViewEditText |
| 22 | +import org.wordpress.aztec.toolbar.AztecToolbar |
| 23 | + |
| 24 | +@RunWith(RobolectricTestRunner::class) |
| 25 | +class PlaceholderTest { |
| 26 | + lateinit var container: FrameLayout |
| 27 | + lateinit var editText: AztecText |
| 28 | + lateinit var menuList: PopupMenu |
| 29 | + lateinit var menuListOrdered: MenuItem |
| 30 | + lateinit var menuListUnordered: MenuItem |
| 31 | + lateinit var sourceText: SourceViewEditText |
| 32 | + lateinit var toolbar: AztecToolbar |
| 33 | + lateinit var placeholderManager: PlaceholderManager |
| 34 | + |
| 35 | + private val uuid: String = "uuid123" |
| 36 | + |
| 37 | + /** |
| 38 | + * Initialize variables. |
| 39 | + */ |
| 40 | + @Before |
| 41 | + fun init() { |
| 42 | + val activity = Robolectric.buildActivity(Activity::class.java).create().visible().get() |
| 43 | + container = FrameLayout(activity) |
| 44 | + editText = AztecText(activity) |
| 45 | + container.addView(editText, FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)) |
| 46 | + placeholderManager = PlaceholderManager(editText, container, generateUuid = { |
| 47 | + uuid |
| 48 | + }) |
| 49 | + placeholderManager.registerAdapter(ImageWithCaptionAdapter()) |
| 50 | + editText.setCalypsoMode(false) |
| 51 | + editText.addMediaAfterBlocks() |
| 52 | + editText.plugins.add(placeholderManager) |
| 53 | + sourceText = SourceViewEditText(activity) |
| 54 | + sourceText.setCalypsoMode(false) |
| 55 | + toolbar = AztecToolbar(activity) |
| 56 | + toolbar.setEditor(editText, sourceText) |
| 57 | + menuList = toolbar.getListMenu() as PopupMenu |
| 58 | + menuListOrdered = menuList.menu.getItem(1) |
| 59 | + menuListUnordered = menuList.menu.getItem(0) |
| 60 | + activity.setContentView(container) |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @Throws(Exception::class) |
| 65 | + fun addAndRemovePlaceholderAtTheBeginning() { |
| 66 | + runBlocking { |
| 67 | + val initialHtml = "<p>Line 1</p>" |
| 68 | + editText.fromHtml(initialHtml) |
| 69 | + |
| 70 | + editText.setSelection(0) |
| 71 | + val attributes = AztecAttributes() |
| 72 | + attributes.setValue("id", "1234") |
| 73 | + ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 123") |
| 74 | + |
| 75 | + Assert.assertEquals("<placeholder uuid=\"$uuid\" type=\"image_with_caption\" src=\"image.jpg\" caption=\"Caption 123\" /><p>Line 1</p>", editText.toHtml()) |
| 76 | + |
| 77 | + placeholderManager.removeItem { |
| 78 | + it.getValue("uuid") == uuid |
| 79 | + } |
| 80 | + |
| 81 | + Assert.assertEquals(initialHtml, editText.toHtml()) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @Throws(Exception::class) |
| 87 | + fun addAndRemovePlaceholderAtTheEndOfLine() { |
| 88 | + runBlocking { |
| 89 | + val initialHtml = "<p>Line 123</p><p>Line 2</p>" |
| 90 | + editText.fromHtml(initialHtml) |
| 91 | + |
| 92 | + editText.setSelection(editText.editableText.indexOf("1")) |
| 93 | + val attributes = AztecAttributes() |
| 94 | + attributes.setValue("id", "1234") |
| 95 | + ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 123") |
| 96 | + |
| 97 | + Assert.assertEquals("<p>Line 123<placeholder uuid=\"uuid123\" type=\"image_with_caption\" src=\"image.jpg\" caption=\"Caption 123\" /></p><p>Line 2</p>", editText.toHtml()) |
| 98 | + |
| 99 | + placeholderManager.removeItem { |
| 100 | + it.getValue("uuid") == uuid |
| 101 | + } |
| 102 | + |
| 103 | + Assert.assertEquals(initialHtml, editText.toHtml()) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @Throws(Exception::class) |
| 109 | + fun insertOrUpdateAPlaceholderAtTheBeginning() { |
| 110 | + runBlocking { |
| 111 | + val initialHtml = "<p>Line 1</p>" |
| 112 | + editText.fromHtml(initialHtml) |
| 113 | + |
| 114 | + editText.setSelection(0) |
| 115 | + val attributes = AztecAttributes() |
| 116 | + attributes.setValue("id", "1234") |
| 117 | + ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 1") |
| 118 | + ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 2") |
| 119 | + |
| 120 | + Assert.assertEquals("<placeholder src=\"image.jpg\" caption=\"Caption 2 - Caption 1\" uuid=\"uuid123\" type=\"image_with_caption\" /><p>Line 1</p>", editText.toHtml()) |
| 121 | + |
| 122 | + placeholderManager.removeItem { |
| 123 | + it.getValue("uuid") == uuid |
| 124 | + } |
| 125 | + |
| 126 | + Assert.assertEquals(initialHtml, editText.toHtml()) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments