Add tests for the custom toot character limit functionality (#599)

* Query instance for toot character limit
Fixes #393

* Move maximumTootCharacters to instance field

* Add caching for maximum toot characters, expanding on the emoji list storage

* Update formatting per review feedback

* Fix compose activity tests

* Rename mastodon api point for nicer interaction with kotlin

* Default emoji list to empty list instead of null, to appease json converters in failure cases

* Add test coverage for custom toot character limit setting

* Fix compiler warnings
This commit is contained in:
Levi Bard 2018-04-29 10:08:25 +02:00 committed by Konrad Pozniak
parent d4425c619a
commit ff7c54e739
2 changed files with 84 additions and 5 deletions

View File

@ -147,7 +147,7 @@ public final class ComposeActivity
Injectable, InputConnectionCompat.OnCommitContentListener { Injectable, InputConnectionCompat.OnCommitContentListener {
private static final String TAG = "ComposeActivity"; // logging tag private static final String TAG = "ComposeActivity"; // logging tag
private static final int STATUS_CHARACTER_LIMIT = 500; static final int STATUS_CHARACTER_LIMIT = 500;
private static final int STATUS_MEDIA_SIZE_LIMIT = 8388608; // 8MiB private static final int STATUS_MEDIA_SIZE_LIMIT = 8388608; // 8MiB
private static final int MEDIA_PICK_RESULT = 1; private static final int MEDIA_PICK_RESULT = 1;
private static final int MEDIA_TAKE_PHOTO_RESULT = 2; private static final int MEDIA_TAKE_PHOTO_RESULT = 2;
@ -1464,6 +1464,12 @@ public final class ComposeActivity
TuskyApplication.getDB().instanceDao().insertOrReplace(instanceEntity); TuskyApplication.getDB().instanceDao().insertOrReplace(instanceEntity);
} }
// Accessors for testing, hence package scope
int getMaximumTootCharacters()
{
return maximumTootCharacters;
}
public static final class QueuedMedia { public static final class QueuedMedia {
Type type; Type type;
ProgressImageView preview; ProgressImageView preview;

View File

@ -16,15 +16,17 @@
package com.keylesspalace.tusky package com.keylesspalace.tusky
import android.text.SpannedString
import android.widget.EditText import android.widget.EditText
import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.db.AccountManager import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.entity.Account
import com.keylesspalace.tusky.entity.Emoji import com.keylesspalace.tusky.entity.Emoji
import com.keylesspalace.tusky.entity.Instance import com.keylesspalace.tusky.entity.Instance
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
import okhttp3.Request import okhttp3.Request
import org.junit.Assert.assertFalse import okhttp3.ResponseBody
import org.junit.Assert.assertTrue import org.junit.Assert.*
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
@ -70,9 +72,10 @@ class ComposeActivityTest {
notificationVibration = true, notificationVibration = true,
notificationLight = true notificationLight = true
) )
var instanceResponseCallback: ((Call<Instance>?, Callback<Instance>?)->Unit)? = null
@Before @Before
fun before() { fun setupActivity() {
val controller = Robolectric.buildActivity(ComposeActivity::class.java) val controller = Robolectric.buildActivity(ComposeActivity::class.java)
activity = controller.get() activity = controller.get()
@ -124,7 +127,9 @@ class ComposeActivityTest {
throw Error("not implemented") throw Error("not implemented")
} }
override fun enqueue(callback: Callback<Instance>?) {} override fun enqueue(callback: Callback<Instance>?) {
instanceResponseCallback?.invoke(this, callback)
}
}) })
activity.mastodonApi = apiMock activity.mastodonApi = apiMock
@ -166,6 +171,33 @@ class ComposeActivityTest {
// We would like to check for dialog but Robolectric doesn't work with AppCompat v7 yet // We would like to check for dialog but Robolectric doesn't work with AppCompat v7 yet
} }
@Test
fun whenMaximumTootCharsIsNull_defaultLimitIsUsed() {
instanceResponseCallback = getSuccessResponseCallbackWithMaximumTootCharacters(null)
setupActivity()
assertEquals(ComposeActivity.STATUS_CHARACTER_LIMIT, activity.maximumTootCharacters)
}
@Test
fun whenMaximumTootCharsIsPopulated_customLimitIsUsed() {
val customMaximum = 1000
instanceResponseCallback = getSuccessResponseCallbackWithMaximumTootCharacters(customMaximum)
setupActivity()
assertEquals(customMaximum, activity.maximumTootCharacters)
}
@Test
fun whenInitialInstanceRequestFails_defaultValueIsUsed() {
instanceResponseCallback = {
call: Call<Instance>?, callback: Callback<Instance>? ->
if (call != null) {
callback?.onResponse(call, Response.error(400, ResponseBody.create(null, "")))
}
}
setupActivity()
assertEquals(ComposeActivity.STATUS_CHARACTER_LIMIT, activity.maximumTootCharacters)
}
private fun clickUp() { private fun clickUp() {
val menuItem = RoboMenuItem(android.R.id.home) val menuItem = RoboMenuItem(android.R.id.home)
activity.onOptionsItemSelected(menuItem) activity.onOptionsItemSelected(menuItem)
@ -178,4 +210,45 @@ class ComposeActivityTest {
private fun insertSomeTextInContent() { private fun insertSomeTextInContent() {
activity.findViewById<EditText>(R.id.composeEditField).setText("Some text") activity.findViewById<EditText>(R.id.composeEditField).setText("Some text")
} }
private fun getInstanceWithMaximumTootCharacters(maximumTootCharacters: Int?): Instance
{
return Instance(
"https://example.token",
"Example dot Token",
"Example instance for testing",
"admin@example.token",
"2.6.3",
HashMap<String, String>(),
null,
null,
listOf("en"),
Account(
"1",
"admin",
"admin",
"admin",
SpannedString(""),
"https://example.token",
"",
"",
false,
0,
0,
0,
null
),
maximumTootCharacters
)
}
private fun getSuccessResponseCallbackWithMaximumTootCharacters(maximumTootCharacters: Int?): (Call<Instance>?, Callback<Instance>?) -> Unit
{
return {
call: Call<Instance>?, callback: Callback<Instance>? ->
if (call != null) {
callback?.onResponse(call, Response.success(getInstanceWithMaximumTootCharacters(maximumTootCharacters)))
}
}
}
} }