|
| 1 | +package org.wordpress.aztec |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.view.MenuItem |
| 5 | +import android.widget.PopupMenu |
| 6 | +import android.widget.ToggleButton |
| 7 | +import kotlinx.coroutines.Job |
| 8 | +import kotlinx.coroutines.launch |
| 9 | +import kotlinx.coroutines.test.runTest |
| 10 | +import org.junit.Assert |
| 11 | +import org.junit.Before |
| 12 | +import org.junit.Test |
| 13 | +import org.junit.runner.RunWith |
| 14 | +import org.robolectric.Robolectric |
| 15 | +import org.robolectric.RobolectricTestRunner |
| 16 | +import org.wordpress.aztec.source.SourceViewEditText |
| 17 | +import org.wordpress.aztec.toolbar.AztecToolbar |
| 18 | +import org.wordpress.aztec.toolbar.ToolbarAction |
| 19 | +import org.wordpress.aztec.toolbar.ToolbarItems |
| 20 | + |
| 21 | +@RunWith(RobolectricTestRunner::class) |
| 22 | +class AsyncAztecTest { |
| 23 | + lateinit var editText: AztecText |
| 24 | + lateinit var sourceText: SourceViewEditText |
| 25 | + lateinit var toolbar: AztecToolbar |
| 26 | + lateinit var buttonQuote: ToggleButton |
| 27 | + lateinit var menuHeading: PopupMenu |
| 28 | + lateinit var menuHeading1: MenuItem |
| 29 | + lateinit var menuHeading2: MenuItem |
| 30 | + lateinit var menuParagraph: MenuItem |
| 31 | + lateinit var buttonPreformat: ToggleButton |
| 32 | + lateinit var buttonBold: ToggleButton |
| 33 | + |
| 34 | + /** |
| 35 | + * Initialize variables. |
| 36 | + */ |
| 37 | + @Before |
| 38 | + fun init() { |
| 39 | + val activity = Robolectric.buildActivity(Activity::class.java).create().visible().get() |
| 40 | + editText = AztecText(activity) |
| 41 | + editText.setCalypsoMode(false) |
| 42 | + sourceText = SourceViewEditText(activity) |
| 43 | + sourceText.setCalypsoMode(false) |
| 44 | + toolbar = AztecToolbar(activity) |
| 45 | + toolbar.setToolbarItems( |
| 46 | + ToolbarItems.BasicLayout( |
| 47 | + ToolbarAction.HEADING, |
| 48 | + ToolbarAction.PREFORMAT, |
| 49 | + ToolbarAction.LIST, |
| 50 | + ToolbarAction.QUOTE, |
| 51 | + ToolbarAction.BOLD, |
| 52 | + ToolbarAction.ITALIC, |
| 53 | + ToolbarAction.LINK, |
| 54 | + ToolbarAction.UNDERLINE, |
| 55 | + ToolbarAction.STRIKETHROUGH, |
| 56 | + ToolbarAction.ALIGN_LEFT, |
| 57 | + ToolbarAction.ALIGN_CENTER, |
| 58 | + ToolbarAction.ALIGN_RIGHT, |
| 59 | + ToolbarAction.HORIZONTAL_RULE, |
| 60 | + ToolbarAction.HTML |
| 61 | + )) |
| 62 | + toolbar.setEditor(editText, sourceText) |
| 63 | + buttonQuote = toolbar.findViewById<ToggleButton>(R.id.format_bar_button_quote) |
| 64 | + menuHeading = toolbar.getHeadingMenu() as PopupMenu |
| 65 | + menuHeading1 = menuHeading.menu.getItem(1) |
| 66 | + menuHeading2 = menuHeading.menu.getItem(2) |
| 67 | + menuParagraph = menuHeading.menu.getItem(0) |
| 68 | + buttonPreformat = toolbar.findViewById<ToggleButton>(R.id.format_bar_button_pre) |
| 69 | + buttonBold = toolbar.findViewById<ToggleButton>(R.id.format_bar_button_bold) |
| 70 | + activity.setContentView(editText) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @Throws(Exception::class) |
| 75 | + fun asyncToHtmlWorksOnCurrentVersion() = runTest { |
| 76 | + editText.append("One two three") |
| 77 | + val textCopy = editText.getTextCopy() |
| 78 | + Assert.assertEquals("One two three", editText.toHtmlAsync(textCopy)) |
| 79 | + val jobs = mutableListOf<Job>() |
| 80 | + jobs.add(launch { |
| 81 | + Assert.assertEquals("One two three", editText.toHtmlAsync(textCopy)) |
| 82 | + }) |
| 83 | + toolbar.onMenuItemClick(menuHeading1) |
| 84 | + val textCopy2 = editText.getTextCopy() |
| 85 | + jobs.add(launch { |
| 86 | + Assert.assertEquals("<h1>One two three</h1>", editText.toHtmlAsync(textCopy2)) |
| 87 | + }) |
| 88 | + toolbar.onMenuItemClick(menuParagraph) |
| 89 | + val textCopy3 = editText.getTextCopy() |
| 90 | + jobs.add(launch { |
| 91 | + Assert.assertEquals("One two three", editText.toHtmlAsync(textCopy3)) |
| 92 | + }) |
| 93 | + val from = editText.editableText.indexOf("two") |
| 94 | + editText.setSelection(from, from + 3) |
| 95 | + editText.toggleFormatting(AztecTextFormat.FORMAT_BOLD) |
| 96 | + val textCopy4 = editText.getTextCopy() |
| 97 | + jobs.add(launch { |
| 98 | + Assert.assertEquals("One <strong>two</strong> three", editText.toHtmlAsync(textCopy4)) |
| 99 | + }) |
| 100 | + jobs.forEach { |
| 101 | + it.join() |
| 102 | + Assert.assertTrue(it.isCompleted) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments