-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Jakub Żerko <[email protected]>
- Loading branch information
1 parent
65fc8c7
commit 36ff8db
Showing
3 changed files
with
120 additions
and
8 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
app/src/androidTest/java/com/wire/android/LinkSpannableStringTest.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,103 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android | ||
|
||
import android.text.style.URLSpan | ||
import android.text.util.Linkify | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.wire.android.ui.common.LinkSpannableString | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class LinkSpannableStringTest { | ||
|
||
lateinit var linkSpannableString: LinkSpannableString | ||
|
||
@Before | ||
fun setUp() { | ||
linkSpannableString = LinkSpannableString("Hello, world!") | ||
} | ||
|
||
@Test | ||
fun givenValidIndices_whenSetSpanIsCalled_thenSpanIsSet() { | ||
// Given | ||
val start = 0 | ||
val end = 5 | ||
|
||
// When | ||
linkSpannableString.setSpan(URLSpan("http://example.com"), start, end, 0) | ||
|
||
// Then | ||
assert(linkSpannableString.getSpans(start, end, URLSpan::class.java).isNotEmpty()) | ||
} | ||
|
||
@Test | ||
fun givenInvalidStartIndex_whenSetSpanIsCalled_thenSpanIsNotSet() { | ||
// Given | ||
val start = -1 | ||
val end = 5 | ||
|
||
// When | ||
linkSpannableString.setSpan(URLSpan("http://example.com"), start, end, 0) | ||
|
||
// Then | ||
assert(linkSpannableString.getSpans(start, end, URLSpan::class.java).isEmpty()) | ||
} | ||
|
||
@Test | ||
fun givenInvalidEndIndex_whenSetSpanIsCalled_thenSpanIsNotSet() { | ||
// Given | ||
val start = 0 | ||
val end = 20 | ||
|
||
// When | ||
linkSpannableString.setSpan(URLSpan("http://example.com"), start, end, 0) | ||
|
||
// Then | ||
assert(linkSpannableString.getSpans(start, end, URLSpan::class.java).isEmpty()) | ||
} | ||
|
||
@Test | ||
fun givenASetSpan_whenRemoveSpanIsCalled_thenSpanIsRemoved() { | ||
// Given | ||
val urlSpan = URLSpan("http://example.com") | ||
linkSpannableString.setSpan(urlSpan, 0, 5, 0) | ||
|
||
// When | ||
linkSpannableString.removeSpan(urlSpan) | ||
|
||
// Then | ||
assert(linkSpannableString.getSpans(0, 5, URLSpan::class.java).isEmpty()) | ||
} | ||
|
||
@Test | ||
fun givenATextWithLink_whenGetLinkInfosIsCalled_thenLinkInfoIsReturned() { | ||
// Given | ||
val text = "Visit http://example.com for more info." | ||
val mask = Linkify.WEB_URLS | ||
|
||
// When | ||
val linkInfos = LinkSpannableString.getLinkInfos(text, mask) | ||
|
||
// Then | ||
assert(linkInfos.size == 1) | ||
assert(linkInfos[0].url == "http://example.com") | ||
} | ||
} |
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