Skip to content

Commit 6933132

Browse files
authored
Merge pull request #75 from zyfy29/fix-fill-shared-link-titles
fix: failed to generate title for url-encoded links
2 parents bb70b59 + 02ee44b commit 6933132

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ dependencies {
8484
androidTestImplementation(libs.androidx.core.testing)
8585
androidTestImplementation(libs.kotlinx.coroutines.testing)
8686
androidTestImplementation(libs.androidx.espresso.core)
87+
androidTestImplementation(libs.androidx.espresso.intents)
8788
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.amrdeveloper.linkhub
2+
3+
import android.content.Intent
4+
import androidx.test.espresso.Espresso.onView
5+
import androidx.test.espresso.assertion.ViewAssertions.matches
6+
import androidx.test.espresso.intent.rule.IntentsTestRule
7+
import androidx.test.espresso.matcher.ViewMatchers.withId
8+
import androidx.test.espresso.matcher.ViewMatchers.withText
9+
import androidx.test.ext.junit.runners.AndroidJUnit4
10+
import com.amrdeveloper.linkhub.ui.MainActivity
11+
import org.junit.Rule
12+
import org.junit.Test
13+
import org.junit.runner.RunWith
14+
15+
@RunWith(AndroidJUnit4::class)
16+
class ShareLinkTest {
17+
@get:Rule
18+
val intentsTestRule = IntentsTestRule(MainActivity::class.java, true, false)
19+
20+
private fun runCaseFor(sharedLink: String, title: String, subTitle: String) {
21+
val intent = Intent(Intent.ACTION_SEND).apply {
22+
putExtra(Intent.EXTRA_TEXT, sharedLink)
23+
}
24+
intentsTestRule.launchActivity(intent)
25+
26+
onView(withId(R.id.link_url_edit))
27+
.check(matches(withText(sharedLink)))
28+
onView(withId(R.id.link_title_edit))
29+
.check(matches(withText(title)))
30+
onView(withId(R.id.link_subtitle_edit))
31+
.check(matches(withText(subTitle)))
32+
}
33+
34+
@Test
35+
fun generalLink() {
36+
runCaseFor("https://amr.com", "amr Link", "Link from amr website")
37+
}
38+
39+
@Test
40+
fun stackoverflowQuestion() {
41+
runCaseFor("https://stackoverflow.com/questions/android", "Programming question", "Question from stackoverflow.com")
42+
}
43+
44+
@Test
45+
fun linkContainsSpace() {
46+
// not a valid URI, skip generation
47+
runCaseFor("https://www.example.com/hello world", "", "")
48+
}
49+
}

app/src/main/java/com/amrdeveloper/linkhub/ui/link/LinkFragment.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ class LinkFragment : Fragment() {
9292

9393
binding.linkUrlEdit.setText(sharedLink)
9494

95-
val encodedUrl = URLEncoder.encode(sharedLink, "utf-8")
96-
linkViewModel.generateLinkTitleAndSubTitle(encodedUrl)
95+
linkViewModel.generateLinkTitleAndSubTitle(sharedLink)
9796
}
9897

9998
private fun handleLinkArgument() {

app/src/main/java/com/amrdeveloper/linkhub/ui/link/LinkViewModel.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
1515
import kotlinx.coroutines.Dispatchers
1616
import kotlinx.coroutines.launch
1717
import kotlinx.coroutines.withContext
18+
import java.net.URI
1819
import javax.inject.Inject
1920

2021
@HiltViewModel
@@ -72,10 +73,12 @@ class LinkViewModel @Inject constructor(
7273
}
7374
}
7475

76+
private fun isValidURI(url: String) =
77+
URLUtil.isValidUrl(url) && runCatching { URI(url) }.isSuccess
78+
7579
fun generateLinkTitleAndSubTitle(url: String) {
7680
viewModelScope.launch(Dispatchers.IO) {
77-
val isValidLink = URLUtil.isValidUrl(url)
78-
if (isValidLink.not()) return@launch
81+
if (isValidURI(url).not()) return@launch
7982
val linkInfo = generateLinkInfo(url)
8083
withContext(Dispatchers.Main) {
8184
linkInfoLiveData.value = linkInfo

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ksp = "2.0.21-1.0.27"
3333
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
3434
androidx-core-testing = { module = "androidx.arch.core:core-testing", version.ref = "core-testing" }
3535
androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso-core" }
36+
androidx-espresso-intents = { module = "androidx.test.espresso:espresso-intents", version.ref = "espresso-core" }
3637
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "fragment-ktx" }
3738
androidx-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-junit" }
3839
androidx-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "ktx" }

0 commit comments

Comments
 (0)