@@ -27,7 +27,8 @@ class PlaceholderTest {
27
27
lateinit var toolbar: AztecToolbar
28
28
lateinit var placeholderManager: PlaceholderManager
29
29
30
- private val uuid: String = " uuid123"
30
+ private val uuid1: String = " uuid1"
31
+ private val uuid2: String = " uuid2"
31
32
32
33
/* *
33
34
* Initialize variables.
@@ -38,8 +39,9 @@ class PlaceholderTest {
38
39
container = FrameLayout (activity)
39
40
editText = AztecText (activity)
40
41
container.addView(editText, FrameLayout .LayoutParams (MATCH_PARENT , MATCH_PARENT ))
42
+ var counter = 0
41
43
placeholderManager = PlaceholderManager (editText, container, generateUuid = {
42
- uuid
44
+ listOf (uuid1, uuid2)[counter ++ ]
43
45
})
44
46
placeholderManager.registerAdapter(ImageWithCaptionAdapter ())
45
47
editText.setCalypsoMode(false )
@@ -65,10 +67,10 @@ class PlaceholderTest {
65
67
editText.setSelection(0 )
66
68
ImageWithCaptionAdapter .insertImageWithCaption(placeholderManager, " image.jpg" , " Caption 123" )
67
69
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())
69
71
70
72
placeholderManager.removeItem {
71
- it.getValue(" uuid" ) == uuid
73
+ it.getValue(" uuid" ) == uuid1
72
74
}
73
75
74
76
Assert .assertEquals(initialHtml, editText.toHtml())
@@ -85,10 +87,10 @@ class PlaceholderTest {
85
87
editText.setSelection(editText.editableText.indexOf(" 1" ))
86
88
ImageWithCaptionAdapter .insertImageWithCaption(placeholderManager, " image.jpg" , " Caption 123" )
87
89
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())
89
91
90
92
placeholderManager.removeItem {
91
- it.getValue(" uuid" ) == uuid
93
+ it.getValue(" uuid" ) == uuid1
92
94
}
93
95
94
96
Assert .assertEquals(initialHtml, editText.toHtml())
@@ -97,7 +99,7 @@ class PlaceholderTest {
97
99
98
100
@Test
99
101
@Throws(Exception ::class )
100
- fun insertOrUpdateAPlaceholderAtTheBeginning () {
102
+ fun updatePlaceholderAtTheBeginning () {
101
103
runBlocking {
102
104
val initialHtml = " <p>Line 1</p>"
103
105
editText.fromHtml(initialHtml)
@@ -109,7 +111,7 @@ class PlaceholderTest {
109
111
Assert .assertEquals(" ${placeholderWithCaption(" Caption 1 - Caption 2" )} <p>Line 1</p>" , editText.toHtml())
110
112
111
113
placeholderManager.removeItem {
112
- it.getValue(" uuid" ) == uuid
114
+ it.getValue(" uuid" ) == uuid1
113
115
}
114
116
115
117
Assert .assertEquals(initialHtml, editText.toHtml())
@@ -118,7 +120,28 @@ class PlaceholderTest {
118
120
119
121
@Test
120
122
@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 () {
122
145
runBlocking {
123
146
val initialHtml = " <p>Line 1</p>${placeholderWithCaption(" First" )} <p>Line 2</p>"
124
147
editText.fromHtml(initialHtml)
@@ -132,7 +155,21 @@ class PlaceholderTest {
132
155
133
156
@Test
134
157
@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 () {
136
173
runBlocking {
137
174
val initialHtml = " <p>Line 1</p>${placeholderWithCaption(" First" )} <p>Line 2</p>"
138
175
editText.fromHtml(initialHtml)
@@ -144,18 +181,32 @@ class PlaceholderTest {
144
181
}
145
182
}
146
183
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
+
147
198
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\" />"
149
200
}
150
201
151
202
@Test
152
203
@Throws(Exception ::class )
153
204
fun updatePlaceholderWhenItShouldBe () {
154
205
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>"
156
207
editText.fromHtml(initialHtml)
157
208
158
- placeholderManager.removeOrUpdate(" uuid123 " , shouldUpdateItem = {
209
+ placeholderManager.removeOrUpdate(" uuid1 " , shouldUpdateItem = {
159
210
true
160
211
}) { currentAttributes ->
161
212
val result = mutableMapOf<String , String >()
@@ -164,21 +215,21 @@ class PlaceholderTest {
164
215
result
165
216
}
166
217
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())
168
219
}
169
220
}
170
221
171
222
@Test
172
223
@Throws(Exception ::class )
173
224
fun updatePlaceholderAtTheEnd () {
174
225
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>"
176
227
editText.fromHtml(initialHtml)
177
228
editText.setSelection(editText.editableText.indexOf(" First" ) + 1 )
178
229
val initialSelectionStart = editText.selectionStart
179
230
val initialSelectionEnd = editText.selectionEnd
180
231
181
- placeholderManager.removeOrUpdate(" uuid123 " , shouldUpdateItem = {
232
+ placeholderManager.removeOrUpdate(" uuid1 " , shouldUpdateItem = {
182
233
true
183
234
}) { currentAttributes ->
184
235
val result = mutableMapOf<String , String >()
@@ -187,7 +238,7 @@ class PlaceholderTest {
187
238
result
188
239
}
189
240
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())
191
242
Assert .assertEquals(initialSelectionStart, editText.selectionStart)
192
243
Assert .assertEquals(initialSelectionEnd, editText.selectionEnd)
193
244
}
@@ -197,10 +248,10 @@ class PlaceholderTest {
197
248
@Throws(Exception ::class )
198
249
fun removePlaceholderWhenItShouldNotBeUpdated () {
199
250
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>"
201
252
editText.fromHtml(initialHtml)
202
253
203
- placeholderManager.removeOrUpdate(" uuid123 " , shouldUpdateItem = {
254
+ placeholderManager.removeOrUpdate(" uuid1 " , shouldUpdateItem = {
204
255
false
205
256
}) { currentAttributes ->
206
257
val result = mutableMapOf<String , String >()
0 commit comments