From d748061e09a72efe141c50ef8c2f27a78920536f Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Sun, 29 Apr 2018 10:08:25 +0200 Subject: [PATCH] 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 --- .../keylesspalace/tusky/ComposeActivity.java | 8 +- .../tusky/ComposeActivityTest.kt | 81 ++++++++++++++++++- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index b5f8342f..1bfe8ea5 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -147,7 +147,7 @@ public final class ComposeActivity Injectable, InputConnectionCompat.OnCommitContentListener { 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 MEDIA_PICK_RESULT = 1; private static final int MEDIA_TAKE_PHOTO_RESULT = 2; @@ -1464,6 +1464,12 @@ public final class ComposeActivity TuskyApplication.getDB().instanceDao().insertOrReplace(instanceEntity); } + // Accessors for testing, hence package scope + int getMaximumTootCharacters() + { + return maximumTootCharacters; + } + public static final class QueuedMedia { Type type; ProgressImageView preview; diff --git a/app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt b/app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt index 4a963fa9..7896ae17 100644 --- a/app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt +++ b/app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt @@ -16,15 +16,17 @@ package com.keylesspalace.tusky +import android.text.SpannedString import android.widget.EditText import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.db.AccountManager +import com.keylesspalace.tusky.entity.Account import com.keylesspalace.tusky.entity.Emoji import com.keylesspalace.tusky.entity.Instance import com.keylesspalace.tusky.network.MastodonApi import okhttp3.Request -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue +import okhttp3.ResponseBody +import org.junit.Assert.* import org.junit.Before import org.junit.Test import org.junit.runner.RunWith @@ -70,9 +72,10 @@ class ComposeActivityTest { notificationVibration = true, notificationLight = true ) + var instanceResponseCallback: ((Call?, Callback?)->Unit)? = null @Before - fun before() { + fun setupActivity() { val controller = Robolectric.buildActivity(ComposeActivity::class.java) activity = controller.get() @@ -124,7 +127,9 @@ class ComposeActivityTest { throw Error("not implemented") } - override fun enqueue(callback: Callback?) {} + override fun enqueue(callback: Callback?) { + instanceResponseCallback?.invoke(this, callback) + } }) 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 } + @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?, callback: Callback? -> + if (call != null) { + callback?.onResponse(call, Response.error(400, ResponseBody.create(null, ""))) + } + } + setupActivity() + assertEquals(ComposeActivity.STATUS_CHARACTER_LIMIT, activity.maximumTootCharacters) + } + private fun clickUp() { val menuItem = RoboMenuItem(android.R.id.home) activity.onOptionsItemSelected(menuItem) @@ -178,4 +210,45 @@ class ComposeActivityTest { private fun insertSomeTextInContent() { activity.findViewById(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(), + 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?, Callback?) -> Unit + { + return { + call: Call?, callback: Callback? -> + if (call != null) { + callback?.onResponse(call, Response.success(getInstanceWithMaximumTootCharacters(maximumTootCharacters))) + } + } + } } \ No newline at end of file