-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for test-case scenarios
- Loading branch information
Showing
8 changed files
with
484 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"spans": [ | ||
{ | ||
"insert": "RichEditor", | ||
"attributes": { | ||
"bold": true, | ||
"header": 1 | ||
} | ||
}, | ||
{ | ||
"insert": "\nf", | ||
"attributes": { | ||
"bold": true | ||
} | ||
}, | ||
{ | ||
"insert": "o", | ||
"attributes": {} | ||
}, | ||
{ | ||
"insert": "r ", | ||
"attributes": { | ||
"bold": true | ||
} | ||
}, | ||
{ | ||
"insert": "\nAndroid ", | ||
"attributes": {} | ||
}, | ||
{ | ||
"insert": "WYSIWYG ", | ||
"attributes": { | ||
"bold": true, | ||
"italic": true | ||
} | ||
} | ||
] | ||
} |
26 changes: 26 additions & 0 deletions
26
editor/src/test/java/com/canopas/editor/MainCoroutineRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.canopas.editor | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.TestDispatcher | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.setMain | ||
import org.junit.rules.TestWatcher | ||
import org.junit.runner.Description | ||
|
||
class MainCoroutineRule @OptIn(ExperimentalCoroutinesApi::class) constructor( | ||
private val dispatcher: TestDispatcher = UnconfinedTestDispatcher() | ||
) : TestWatcher() { | ||
@OptIn(ExperimentalCoroutinesApi::class) | ||
override fun starting(description: Description) { | ||
super.starting(description) | ||
Dispatchers.setMain(dispatcher) | ||
} | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
override fun finished(description: Description) { | ||
super.finished(description) | ||
Dispatchers.resetMain() | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
editor/src/test/java/com/canopas/editor/jsonparser/QuillJsonEditorParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.canopas.editor.jsonparser | ||
|
||
import com.canopas.editor.ui.model.QuillSpan | ||
import com.canopas.editor.ui.model.Span | ||
import com.canopas.editor.ui.parser.QuillEditorAdapter | ||
import com.google.common.reflect.TypeToken | ||
import com.google.gson.Gson | ||
import com.google.gson.GsonBuilder | ||
|
||
class QuillJsonEditorParser : QuillEditorAdapter { | ||
|
||
private val gson: Gson = GsonBuilder() | ||
.registerTypeAdapter(Span::class.java, QuillRichTextStateAdapter()) | ||
.create() | ||
|
||
override fun encode(input: String): QuillSpan { | ||
return gson.fromJson(input, object : TypeToken<QuillSpan>() {}.type) | ||
} | ||
|
||
override fun decode(editorValue: QuillSpan): String { | ||
return gson.toJson(editorValue) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
editor/src/test/java/com/canopas/editor/jsonparser/QuillRichTextStateAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.canopas.editor.jsonparser | ||
|
||
import com.canopas.editor.ui.model.Attributes | ||
import com.canopas.editor.ui.model.Span | ||
import com.google.gson.JsonDeserializationContext | ||
import com.google.gson.JsonDeserializer | ||
import com.google.gson.JsonElement | ||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonParseException | ||
import com.google.gson.JsonSerializationContext | ||
import com.google.gson.JsonSerializer | ||
import java.lang.reflect.Type | ||
|
||
class QuillRichTextStateAdapter : JsonSerializer<Span>, JsonDeserializer<Span> { | ||
override fun serialize( | ||
src: Span?, | ||
typeOfSrc: Type?, | ||
context: JsonSerializationContext? | ||
): JsonElement { | ||
val jsonObject = JsonObject() | ||
jsonObject.add("insert", context?.serialize(src?.insert)) | ||
jsonObject.add("attributes", context?.serialize(src?.attributes)) | ||
return jsonObject | ||
} | ||
|
||
override fun deserialize( | ||
json: JsonElement?, | ||
typeOfT: Type?, | ||
context: JsonDeserializationContext? | ||
): Span { | ||
try { | ||
val jsonObject = json?.asJsonObject ?: throw JsonParseException("Invalid JSON") | ||
val insert = jsonObject.get("insert") | ||
val attributes = jsonObject.get("attributes") | ||
return Span( | ||
insert = context?.deserialize<String>(insert, String::class.java), | ||
attributes = context?.deserialize<Attributes>(attributes, Attributes::class.java) | ||
) | ||
} catch (e: Exception) { | ||
throw JsonParseException("Invalid JSON") | ||
} | ||
} | ||
} |
Oops, something went wrong.