diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index b2346146b..a011a2331 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -772,7 +772,7 @@ public final class ComposeActivity setStatusVisibility(visibility); } - int calculateRemainingCharacters() { + int calculateTextLength() { int offset = 0; URLSpan[] urlSpans = textEditor.getUrls(); if (urlSpans != null) { @@ -780,15 +780,15 @@ public final class ComposeActivity offset += Math.max(0, span.getURL().length() - MAXIMUM_URL_LENGTH); } } - int remaining = maximumTootCharacters - textEditor.length() + offset; + int length = textEditor.length() - offset; if (statusHideText) { - remaining -= contentWarningEditor.length(); + length += contentWarningEditor.length(); } - return remaining; + return length; } private void updateVisibleCharactersLeft() { - this.charactersLeft.setText(String.format(Locale.getDefault(), "%d", calculateRemainingCharacters())); + this.charactersLeft.setText(String.format(Locale.getDefault(), "%d", maximumTootCharacters - calculateTextLength())); } private void onContentWarningChanged() { @@ -925,15 +925,13 @@ public final class ComposeActivity } private void onReadySuccess(Status.Visibility visibility, boolean sensitive) { - /* Validate the status meets the character limit. This has to be delayed until after all - * uploads finish because their links are added when the upload succeeds and that affects - * whether the limit is met or not. */ + /* Validate the status meets the character limit. */ String contentText = textEditor.getText().toString(); String spoilerText = ""; if (statusHideText) { spoilerText = contentWarningEditor.getText().toString(); } - int characterCount = contentText.length() + spoilerText.length(); + int characterCount = calculateTextLength(); if (characterCount <= 0 && mediaQueued.size()==0) { textEditor.setError(getString(R.string.error_empty)); enableButtons(); diff --git a/app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt b/app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt index 4c0609720..76411d8d8 100644 --- a/app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt +++ b/app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt @@ -201,11 +201,27 @@ class ComposeActivityTest { } @Test - fun whenTextContainsUrl_onlyEllipsizedURLIsCountedAgainstCharacterLimit() { + fun whenTextContainsNoUrl_everyCharacterIsCounted() { + val content = "This is test content please ignore thx " + insertSomeTextInContent(content) + Assert.assertEquals(activity.calculateTextLength(), content.length) + } + + @Test + fun whenTextContainsUrl_onlyEllipsizedURLIsCounted() { val url = "https://www.google.dk/search?biw=1920&bih=990&tbm=isch&sa=1&ei=bmDrWuOoKMv6kwWOkIaoDQ&q=indiana+jones+i+hate+snakes+animated&oq=indiana+jones+i+hate+snakes+animated&gs_l=psy-ab.3...54174.55443.0.55553.9.7.0.0.0.0.255.333.1j0j1.2.0....0...1c.1.64.psy-ab..7.0.0....0.40G-kcDkC6A#imgdii=PSp15hQjN1JqvM:&imgrc=H0hyE2JW5wrpBM:" val additionalContent = "Check out this @image #search result: " insertSomeTextInContent(additionalContent + url) - Assert.assertEquals(activity.calculateRemainingCharacters(), activity.maximumTootCharacters - additionalContent.length - ComposeActivity.MAXIMUM_URL_LENGTH) + Assert.assertEquals(activity.calculateTextLength(), additionalContent.length + ComposeActivity.MAXIMUM_URL_LENGTH) + } + + @Test + fun whenTextContainsMultipleUrls_onlyEllipsizedURLIsCounted() { + val shortUrl = "https://tusky.app" + val url = "https://www.google.dk/search?biw=1920&bih=990&tbm=isch&sa=1&ei=bmDrWuOoKMv6kwWOkIaoDQ&q=indiana+jones+i+hate+snakes+animated&oq=indiana+jones+i+hate+snakes+animated&gs_l=psy-ab.3...54174.55443.0.55553.9.7.0.0.0.0.255.333.1j0j1.2.0....0...1c.1.64.psy-ab..7.0.0....0.40G-kcDkC6A#imgdii=PSp15hQjN1JqvM:&imgrc=H0hyE2JW5wrpBM:" + val additionalContent = " Check out this @image #search result: " + insertSomeTextInContent(shortUrl + additionalContent + url) + Assert.assertEquals(activity.calculateTextLength(), additionalContent.length + shortUrl.length + ComposeActivity.MAXIMUM_URL_LENGTH) } @Test @@ -213,8 +229,7 @@ class ComposeActivityTest { val url = "https://www.google.dk/search?biw=1920&bih=990&tbm=isch&sa=1&ei=bmDrWuOoKMv6kwWOkIaoDQ&q=indiana+jones+i+hate+snakes+animated&oq=indiana+jones+i+hate+snakes+animated&gs_l=psy-ab.3...54174.55443.0.55553.9.7.0.0.0.0.255.333.1j0j1.2.0....0...1c.1.64.psy-ab..7.0.0....0.40G-kcDkC6A#imgdii=PSp15hQjN1JqvM:&imgrc=H0hyE2JW5wrpBM:" val additionalContent = " Check out this @image #search result: " insertSomeTextInContent(url + additionalContent + url) - Assert.assertEquals(activity.calculateRemainingCharacters(), - activity.maximumTootCharacters - additionalContent.length - (ComposeActivity.MAXIMUM_URL_LENGTH * 2)) + Assert.assertEquals(activity.calculateTextLength(), additionalContent.length + (ComposeActivity.MAXIMUM_URL_LENGTH * 2)) } private fun clickUp() {