|
| 1 | +package org.wordpress.aztec |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.view.MenuItem |
| 5 | +import android.widget.PopupMenu |
| 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.source.SourceViewEditText |
| 14 | +import org.wordpress.aztec.toolbar.AztecToolbar |
| 15 | + |
| 16 | +@RunWith(RobolectricTestRunner::class) |
| 17 | +@Config(sdk = intArrayOf(23)) |
| 18 | +class ImageBlockTest { |
| 19 | + lateinit var editText: AztecText |
| 20 | + lateinit var menuList: PopupMenu |
| 21 | + lateinit var menuListOrdered: MenuItem |
| 22 | + lateinit var menuListUnordered: MenuItem |
| 23 | + lateinit var sourceText: SourceViewEditText |
| 24 | + lateinit var toolbar: AztecToolbar |
| 25 | + |
| 26 | + /** |
| 27 | + * Initialize variables. |
| 28 | + */ |
| 29 | + @Before |
| 30 | + fun init() { |
| 31 | + val activity = Robolectric.buildActivity(Activity::class.java).create().visible().get() |
| 32 | + editText = AztecText(activity) |
| 33 | + editText.setCalypsoMode(false) |
| 34 | + editText.addMediaAfterBlocks() |
| 35 | + sourceText = SourceViewEditText(activity) |
| 36 | + sourceText.setCalypsoMode(false) |
| 37 | + toolbar = AztecToolbar(activity) |
| 38 | + toolbar.setEditor(editText, sourceText) |
| 39 | + menuList = toolbar.getListMenu() as PopupMenu |
| 40 | + menuListOrdered = menuList.menu.getItem(1) |
| 41 | + menuListUnordered = menuList.menu.getItem(0) |
| 42 | + activity.setContentView(editText) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @Throws(Exception::class) |
| 47 | + fun addImageAfterAListAtTheEnd() { |
| 48 | + editText.fromHtml("<ul><li>item 1</li><li>item 2</li></ul>") |
| 49 | + |
| 50 | + editText.setSelection(editText.editableText.indexOf("2")) |
| 51 | + val attributes = AztecAttributes() |
| 52 | + attributes.setValue("id", "1234") |
| 53 | + editText.insertImage(null, attributes) |
| 54 | + |
| 55 | + Assert.assertEquals("<ul><li>item 1</li><li>item 2</li></ul><img id=\"1234\" />", editText.toHtml()) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @Throws(Exception::class) |
| 60 | + fun addHRAfterAListAtTheEnd() { |
| 61 | + editText.fromHtml("<ul><li>item 1</li><li>item 2</li></ul>") |
| 62 | + |
| 63 | + editText.setSelection(editText.editableText.indexOf("2")) |
| 64 | + editText.lineBlockFormatter.applyHorizontalRule(false) |
| 65 | + |
| 66 | + Assert.assertEquals("<ul><li>item 1</li><li>item 2</li></ul><hr />", editText.toHtml()) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @Throws(Exception::class) |
| 71 | + fun addImageAfterAListInTheMiddle() { |
| 72 | + editText.fromHtml("<ul><li>item 1</li><li>item 2</li></ul>\n<p>test</p>") |
| 73 | + |
| 74 | + editText.setSelection(editText.editableText.indexOf("2")) |
| 75 | + val attributes = AztecAttributes() |
| 76 | + attributes.setValue("id", "1234") |
| 77 | + editText.insertImage(null, attributes) |
| 78 | + |
| 79 | + Assert.assertEquals("<ul><li>item 1</li><li>item 2</li></ul><img id=\"1234\" /><p>test</p>", editText.toHtml()) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + @Throws(Exception::class) |
| 84 | + fun addHRAfterAListInTheMiddle() { |
| 85 | + editText.fromHtml("<ul><li>item 1</li><li>item 2</li></ul>\n<p>test</p>") |
| 86 | + |
| 87 | + editText.setSelection(editText.editableText.indexOf("2")) |
| 88 | + editText.lineBlockFormatter.applyHorizontalRule(false) |
| 89 | + |
| 90 | + Assert.assertEquals("<ul><li>item 1</li><li>item 2</li></ul><hr /><p>test</p>", editText.toHtml()) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + @Throws(Exception::class) |
| 95 | + fun addImageAfterHeadline() { |
| 96 | + editText.fromHtml("<h1>Headline 1</h1>") |
| 97 | + |
| 98 | + editText.setSelection(editText.editableText.indexOf("1")) |
| 99 | + val attributes = AztecAttributes() |
| 100 | + attributes.setValue("id", "1234") |
| 101 | + editText.insertImage(null, attributes) |
| 102 | + |
| 103 | + Assert.assertEquals("<h1>Headline 1</h1><img id=\"1234\" />", editText.toHtml()) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + @Throws(Exception::class) |
| 108 | + fun addHRAfterHeadline() { |
| 109 | + editText.fromHtml("<h1>Headline 1</h1>") |
| 110 | + |
| 111 | + editText.setSelection(editText.editableText.indexOf("1")) |
| 112 | + editText.lineBlockFormatter.applyHorizontalRule(false) |
| 113 | + |
| 114 | + Assert.assertEquals("<h1>Headline 1</h1><hr />", editText.toHtml()) |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @Throws(Exception::class) |
| 119 | + fun addImageBetweenHeadlines() { |
| 120 | + editText.fromHtml("<h1>Headline 1</h1><h2>Headline 2</h2>") |
| 121 | + |
| 122 | + editText.setSelection(editText.editableText.indexOf("1")) |
| 123 | + val attributes = AztecAttributes() |
| 124 | + attributes.setValue("id", "1234") |
| 125 | + editText.insertImage(null, attributes) |
| 126 | + |
| 127 | + Assert.assertEquals("<h1>Headline 1</h1><img id=\"1234\" /><h2>Headline 2</h2>", editText.toHtml()) |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + @Throws(Exception::class) |
| 132 | + fun addHRBetweenHeadlines() { |
| 133 | + editText.fromHtml("<h1>Headline 1</h1><h2>Headline 2</h2>") |
| 134 | + |
| 135 | + editText.setSelection(editText.editableText.indexOf("1")) |
| 136 | + editText.lineBlockFormatter.applyHorizontalRule(false) |
| 137 | + |
| 138 | + Assert.assertEquals("<h1>Headline 1</h1><hr /><h2>Headline 2</h2>", editText.toHtml()) |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + @Throws(Exception::class) |
| 143 | + fun addImageAtTheEndWhenNoBlocksPresent() { |
| 144 | + editText.fromHtml("Test 1<br>test 2<br>test 3") |
| 145 | + |
| 146 | + editText.setSelection(editText.editableText.indexOf("3")) |
| 147 | + val attributes = AztecAttributes() |
| 148 | + attributes.setValue("id", "1234") |
| 149 | + editText.insertImage(null, attributes) |
| 150 | + |
| 151 | + Assert.assertEquals("Test 1<br>test 2<br>test 3<img id=\"1234\" />", editText.toHtml()) |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + @Throws(Exception::class) |
| 156 | + fun addImageInTheMiddleWhenNoBlocksPresent() { |
| 157 | + editText.fromHtml("Test 1<br>test 2<br>test 3") |
| 158 | + |
| 159 | + editText.setSelection(editText.editableText.indexOf("2")) |
| 160 | + val attributes = AztecAttributes() |
| 161 | + attributes.setValue("id", "1234") |
| 162 | + editText.insertImage(null, attributes) |
| 163 | + |
| 164 | + Assert.assertEquals("Test 1<br>test 2<img id=\"1234\" /><br>test 3", editText.toHtml()) |
| 165 | + } |
| 166 | +} |
0 commit comments