@@ -27,7 +27,8 @@ class PlaceholderTest {
2727 lateinit var toolbar: AztecToolbar
2828 lateinit var placeholderManager: PlaceholderManager
2929
30- private val uuid: String = " uuid123"
30+ private val uuid1: String = " uuid1"
31+ private val uuid2: String = " uuid2"
3132
3233 /* *
3334 * Initialize variables.
@@ -38,8 +39,9 @@ class PlaceholderTest {
3839 container = FrameLayout (activity)
3940 editText = AztecText (activity)
4041 container.addView(editText, FrameLayout .LayoutParams (MATCH_PARENT , MATCH_PARENT ))
42+ var counter = 0
4143 placeholderManager = PlaceholderManager (editText, container, generateUuid = {
42- uuid
44+ listOf (uuid1, uuid2)[counter ++ ]
4345 })
4446 placeholderManager.registerAdapter(ImageWithCaptionAdapter ())
4547 editText.setCalypsoMode(false )
@@ -65,10 +67,10 @@ class PlaceholderTest {
6567 editText.setSelection(0 )
6668 ImageWithCaptionAdapter .insertImageWithCaption(placeholderManager, " image.jpg" , " Caption 123" )
6769
68- Assert .assertEquals(" <placeholder uuid=\" $uuid \" type=\" image_with_caption\" src=\" image.jpg\" caption=\" Caption 123\" /><p>Line 1</p>" , editText.toHtml())
70+ Assert .assertEquals(" <placeholder uuid=\" $uuid1 \" type=\" image_with_caption\" src=\" image.jpg\" caption=\" Caption 123\" /><p>Line 1</p>" , editText.toHtml())
6971
7072 placeholderManager.removeItem {
71- it.getValue(" uuid" ) == uuid
73+ it.getValue(" uuid" ) == uuid1
7274 }
7375
7476 Assert .assertEquals(initialHtml, editText.toHtml())
@@ -85,10 +87,10 @@ class PlaceholderTest {
8587 editText.setSelection(editText.editableText.indexOf(" 1" ))
8688 ImageWithCaptionAdapter .insertImageWithCaption(placeholderManager, " image.jpg" , " Caption 123" )
8789
88- 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())
90+ Assert .assertEquals(" <p>Line 123<placeholder uuid=\" uuid1 \" type=\" image_with_caption\" src=\" image.jpg\" caption=\" Caption 123\" /></p><p>Line 2</p>" , editText.toHtml())
8991
9092 placeholderManager.removeItem {
91- it.getValue(" uuid" ) == uuid
93+ it.getValue(" uuid" ) == uuid1
9294 }
9395
9496 Assert .assertEquals(initialHtml, editText.toHtml())
@@ -97,7 +99,7 @@ class PlaceholderTest {
9799
98100 @Test
99101 @Throws(Exception ::class )
100- fun insertOrUpdateAPlaceholderAtTheBeginning () {
102+ fun updatePlaceholderAtTheBeginning () {
101103 runBlocking {
102104 val initialHtml = " <p>Line 1</p>"
103105 editText.fromHtml(initialHtml)
@@ -109,7 +111,7 @@ class PlaceholderTest {
109111 Assert .assertEquals(" ${placeholderWithCaption(" Caption 1 - Caption 2" )} <p>Line 1</p>" , editText.toHtml())
110112
111113 placeholderManager.removeItem {
112- it.getValue(" uuid" ) == uuid
114+ it.getValue(" uuid" ) == uuid1
113115 }
114116
115117 Assert .assertEquals(initialHtml, editText.toHtml())
@@ -118,7 +120,28 @@ class PlaceholderTest {
118120
119121 @Test
120122 @Throws(Exception ::class )
121- fun insertOrUpdateAPlaceholderWhenInsertingBeforeNewLine () {
123+ fun doNotUpdatePlaceholderAtTheBeginningWhenMergeDisabled () {
124+ runBlocking {
125+ val initialHtml = " <p>Line 1</p>"
126+ editText.fromHtml(initialHtml)
127+
128+ editText.setSelection(0 )
129+ ImageWithCaptionAdapter .insertImageWithCaption(placeholderManager, " image.jpg" , " Caption 1" )
130+ ImageWithCaptionAdapter .insertImageWithCaption(placeholderManager, " image.jpg" , " Caption 2" , shouldMergePlaceholders = false )
131+
132+ Assert .assertEquals(" <placeholder uuid=\" uuid1\" type=\" image_with_caption\" src=\" image.jpg\" caption=\" Caption 1\" /><br><placeholder uuid=\" uuid2\" type=\" image_with_caption\" src=\" image.jpg\" caption=\" Caption 2\" /><p>Line 1</p>" , editText.toHtml())
133+
134+ placeholderManager.removeItem {
135+ it.getValue(" uuid" ) == uuid1
136+ }
137+
138+ Assert .assertEquals(" <br><placeholder uuid=\" uuid2\" type=\" image_with_caption\" src=\" image.jpg\" caption=\" Caption 2\" /><p>Line 1</p>" , editText.toHtml())
139+ }
140+ }
141+
142+ @Test
143+ @Throws(Exception ::class )
144+ fun updatePlaceholderWhenInsertingBeforeNewLine () {
122145 runBlocking {
123146 val initialHtml = " <p>Line 1</p>${placeholderWithCaption(" First" )} <p>Line 2</p>"
124147 editText.fromHtml(initialHtml)
@@ -132,7 +155,21 @@ class PlaceholderTest {
132155
133156 @Test
134157 @Throws(Exception ::class )
135- fun insertOrUpdateAPlaceholderWhenInsertingRightBefore () {
158+ fun doNotUpdatePlaceholderWhenInsertingBeforeNewLineAndMergeDisabled () {
159+ runBlocking {
160+ val initialHtml = " <p>Line 1</p>${placeholderWithCaption(" First" )} <p>Line 2</p>"
161+ editText.fromHtml(initialHtml)
162+
163+ editText.setSelection(editText.editableText.indexOf(" 1" ))
164+ ImageWithCaptionAdapter .insertImageWithCaption(placeholderManager, " image.jpg" , " Second" , shouldMergePlaceholders = false )
165+
166+ Assert .assertEquals(" <p>Line 1</p><placeholder uuid=\" uuid2\" type=\" image_with_caption\" src=\" image.jpg\" caption=\" Second\" /><br><placeholder src=\" image.jpg\" caption=\" First\" uuid=\" uuid1\" type=\" image_with_caption\" /><p>Line 2</p>" , editText.toHtml())
167+ }
168+ }
169+
170+ @Test
171+ @Throws(Exception ::class )
172+ fun updatePlaceholderWhenInsertingRightBefore () {
136173 runBlocking {
137174 val initialHtml = " <p>Line 1</p>${placeholderWithCaption(" First" )} <p>Line 2</p>"
138175 editText.fromHtml(initialHtml)
@@ -144,18 +181,32 @@ class PlaceholderTest {
144181 }
145182 }
146183
184+ @Test
185+ @Throws(Exception ::class )
186+ fun doNotUpdatePlaceholderWhenInsertingRightBeforeAndMergeDisabled () {
187+ runBlocking {
188+ val initialHtml = " <p>Line 1</p>${placeholderWithCaption(" First" )} <p>Line 2</p>"
189+ editText.fromHtml(initialHtml)
190+
191+ editText.setSelection(editText.editableText.indexOf(" 1" ) + 1 )
192+ ImageWithCaptionAdapter .insertImageWithCaption(placeholderManager, " image.jpg" , " Second" , shouldMergePlaceholders = false )
193+
194+ Assert .assertEquals(" <p>Line 1</p><placeholder uuid=\" uuid2\" type=\" image_with_caption\" src=\" image.jpg\" caption=\" Second\" /><br>${placeholderWithCaption(" First" )} <p>Line 2</p>" , editText.toHtml())
195+ }
196+ }
197+
147198 private fun placeholderWithCaption (caption : String ): String {
148- return " <placeholder src=\" image.jpg\" caption=\" $caption \" uuid=\" uuid123 \" type=\" image_with_caption\" />"
199+ return " <placeholder src=\" image.jpg\" caption=\" $caption \" uuid=\" uuid1 \" type=\" image_with_caption\" />"
149200 }
150201
151202 @Test
152203 @Throws(Exception ::class )
153204 fun updatePlaceholderWhenItShouldBe () {
154205 runBlocking {
155- val initialHtml = " <placeholder uuid=\" uuid123 \" type=\" image_with_caption\" src=\" image.jpg;image2.jpg\" caption=\" Caption - 1, 2\" /><p>Line</p>"
206+ val initialHtml = " <placeholder uuid=\" uuid1 \" type=\" image_with_caption\" src=\" image.jpg;image2.jpg\" caption=\" Caption - 1, 2\" /><p>Line</p>"
156207 editText.fromHtml(initialHtml)
157208
158- placeholderManager.removeOrUpdate(" uuid123 " , shouldUpdateItem = {
209+ placeholderManager.removeOrUpdate(" uuid1 " , shouldUpdateItem = {
159210 true
160211 }) { currentAttributes ->
161212 val result = mutableMapOf<String , String >()
@@ -164,21 +215,21 @@ class PlaceholderTest {
164215 result
165216 }
166217
167- Assert .assertEquals(" <placeholder src=\" image.jpg\" caption=\" Updated caption\" uuid=\" uuid123 \" type=\" image_with_caption\" /><p>Line</p>" , editText.toHtml())
218+ Assert .assertEquals(" <placeholder src=\" image.jpg\" caption=\" Updated caption\" uuid=\" uuid1 \" type=\" image_with_caption\" /><p>Line</p>" , editText.toHtml())
168219 }
169220 }
170221
171222 @Test
172223 @Throws(Exception ::class )
173224 fun updatePlaceholderAtTheEnd () {
174225 runBlocking {
175- val initialHtml = " <p>First Line</p><placeholder uuid=\" uuid123 \" type=\" image_with_caption\" src=\" image.jpg;image2.jpg\" caption=\" Caption - 1, 2\" /><p>Second Line</p>"
226+ val initialHtml = " <p>First Line</p><placeholder uuid=\" uuid1 \" type=\" image_with_caption\" src=\" image.jpg;image2.jpg\" caption=\" Caption - 1, 2\" /><p>Second Line</p>"
176227 editText.fromHtml(initialHtml)
177228 editText.setSelection(editText.editableText.indexOf(" First" ) + 1 )
178229 val initialSelectionStart = editText.selectionStart
179230 val initialSelectionEnd = editText.selectionEnd
180231
181- placeholderManager.removeOrUpdate(" uuid123 " , shouldUpdateItem = {
232+ placeholderManager.removeOrUpdate(" uuid1 " , shouldUpdateItem = {
182233 true
183234 }) { currentAttributes ->
184235 val result = mutableMapOf<String , String >()
@@ -187,7 +238,7 @@ class PlaceholderTest {
187238 result
188239 }
189240
190- Assert .assertEquals(" <p>First Line</p><placeholder src=\" image.jpg\" caption=\" Updated caption\" uuid=\" uuid123 \" type=\" image_with_caption\" /><p>Second Line</p>" , editText.toHtml())
241+ Assert .assertEquals(" <p>First Line</p><placeholder src=\" image.jpg\" caption=\" Updated caption\" uuid=\" uuid1 \" type=\" image_with_caption\" /><p>Second Line</p>" , editText.toHtml())
191242 Assert .assertEquals(initialSelectionStart, editText.selectionStart)
192243 Assert .assertEquals(initialSelectionEnd, editText.selectionEnd)
193244 }
@@ -197,10 +248,10 @@ class PlaceholderTest {
197248 @Throws(Exception ::class )
198249 fun removePlaceholderWhenItShouldNotBeUpdated () {
199250 runBlocking {
200- val initialHtml = " <placeholder uuid=\" uuid123 \" type=\" image_with_caption\" src=\" image.jpg;image2.jpg\" caption=\" Caption - 1, 2\" /><p>Line</p>"
251+ val initialHtml = " <placeholder uuid=\" uuid1 \" type=\" image_with_caption\" src=\" image.jpg;image2.jpg\" caption=\" Caption - 1, 2\" /><p>Line</p>"
201252 editText.fromHtml(initialHtml)
202253
203- placeholderManager.removeOrUpdate(" uuid123 " , shouldUpdateItem = {
254+ placeholderManager.removeOrUpdate(" uuid1 " , shouldUpdateItem = {
204255 false
205256 }) { currentAttributes ->
206257 val result = mutableMapOf<String , String >()
0 commit comments