2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-19 09:25:34 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/16249 - ClipboardHandler cleans up returned text

This commit is contained in:
Mugurell 2021-01-06 16:34:02 +02:00
parent 90eb9cfad5
commit 8fd1fbcb80
3 changed files with 52 additions and 2 deletions

View File

@ -7,7 +7,9 @@ package org.mozilla.fenix.utils
import android.content.ClipData import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
import androidx.annotation.VisibleForTesting
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import mozilla.components.support.utils.SafeUrl
import mozilla.components.support.utils.WebURLFinder import mozilla.components.support.utils.WebURLFinder
private const val MIME_TYPE_TEXT_PLAIN = "text/plain" private const val MIME_TYPE_TEXT_PLAIN = "text/plain"
@ -16,7 +18,7 @@ private const val MIME_TYPE_TEXT_HTML = "text/html"
/** /**
* A clipboard utility class that allows copying and pasting links/text to & from the clipboard * A clipboard utility class that allows copying and pasting links/text to & from the clipboard
*/ */
class ClipboardHandler(context: Context) { class ClipboardHandler(val context: Context) {
private val clipboard = context.getSystemService<ClipboardManager>()!! private val clipboard = context.getSystemService<ClipboardManager>()!!
var text: String? var text: String?
@ -25,7 +27,7 @@ class ClipboardHandler(context: Context) {
(clipboard.isPrimaryClipPlainText() || (clipboard.isPrimaryClipPlainText() ||
clipboard.isPrimaryClipHtmlText()) clipboard.isPrimaryClipHtmlText())
) { ) {
return clipboard.firstPrimaryClipItem?.text.toString() return firstSafePrimaryClipItemText
} }
return null return null
} }
@ -51,4 +53,8 @@ class ClipboardHandler(context: Context) {
private val ClipboardManager.firstPrimaryClipItem: ClipData.Item? private val ClipboardManager.firstPrimaryClipItem: ClipData.Item?
get() = primaryClip?.getItemAt(0) get() = primaryClip?.getItemAt(0)
@VisibleForTesting
internal val firstSafePrimaryClipItemText: String?
get() = SafeUrl.stripUnsafeUrlSchemes(context, clipboard.firstPrimaryClipItem?.text)
} }

View File

@ -30,4 +30,13 @@
<item>@string/all</item> <item>@string/all</item>
<item>@string/private_string</item> <item>@string/private_string</item>
</string-array> </string-array>
<string name="resource_scheme">"resource://"</string>
<string name="chrome_scheme">"chrome://</string>
<string name="about_scheme">"about:"</string>
<string-array name="mozac_url_schemes_blocklist" >
<item>@string/resource_scheme</item>
<item>@string/chrome_scheme</item>
<item>@string/about_scheme</item>
</string-array>
</resources> </resources>

View File

@ -7,7 +7,13 @@ package org.mozilla.fenix.utils
import android.content.ClipData import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
import io.mockk.every
import io.mockk.mockkObject
import io.mockk.spyk
import io.mockk.unmockkObject
import io.mockk.verify
import mozilla.components.support.test.robolectric.testContext import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.utils.SafeUrl
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -59,4 +65,33 @@ class ClipboardHandlerTest {
clipboard.setPrimaryClip(ClipData.newHtmlText("Html", clipboardUrl, clipboardUrl)) clipboard.setPrimaryClip(ClipData.newHtmlText("Html", clipboardUrl, clipboardUrl))
assertEquals(clipboardUrl, clipboardHandler.url) assertEquals(clipboardUrl, clipboardHandler.url)
} }
@Test
fun `text should return firstSafePrimaryClipItemText`() {
val safeResult = "safeResult"
clipboard.setPrimaryClip(ClipData.newPlainText(clipboardUrl, clipboardText))
clipboardHandler = spyk(clipboardHandler)
every { clipboardHandler getProperty "firstSafePrimaryClipItemText" } propertyType String::class returns safeResult
val result = clipboardHandler.text
verify { clipboardHandler getProperty "firstSafePrimaryClipItemText" }
assertEquals(safeResult, result)
}
@Test
fun `firstSafePrimaryClipItemText should return the result of SafeUrl#stripUnsafeUrlSchemes`() {
mockkObject(SafeUrl)
try {
every { SafeUrl.stripUnsafeUrlSchemes(any(), any()) } returns "safeResult"
clipboard.setPrimaryClip(ClipData.newHtmlText("Html", clipboardUrl, clipboardUrl))
val result = clipboardHandler.firstSafePrimaryClipItemText
verify { SafeUrl.stripUnsafeUrlSchemes(testContext, clipboardUrl) }
assertEquals("safeResult", result)
} finally {
unmockkObject(SafeUrl)
}
}
} }