For #1744: Adds tests for clipboard
parent
fd1fcd8b89
commit
2db2d9ceae
|
@ -40,8 +40,8 @@ class IntentReceiverActivity : Activity() {
|
|||
val intent = intent?.let { Intent(intent) } ?: Intent()
|
||||
|
||||
val intentProcessors = listOf(
|
||||
components.utils.customTabIntentProcessor,
|
||||
components.utils.intentProcessor
|
||||
components.intentProcessors.customTabIntentProcessor,
|
||||
components.intentProcessors.intentProcessor
|
||||
)
|
||||
|
||||
if (intent.getBooleanExtra(SPEECH_PROCESSING, false)) {
|
||||
|
@ -60,7 +60,7 @@ class IntentReceiverActivity : Activity() {
|
|||
|
||||
private fun setIntentActivity(intent: Intent) {
|
||||
val openToBrowser = when {
|
||||
components.utils.customTabIntentProcessor.matches(intent) -> {
|
||||
components.intentProcessors.customTabIntentProcessor.matches(intent) -> {
|
||||
// TODO this needs to change: https://github.com/mozilla-mobile/fenix/issues/5225
|
||||
val activityClass = if (intent.hasExtra(EXTRA_AUTH_CUSTOM_TAB)) {
|
||||
AuthCustomTabActivity::class
|
||||
|
|
|
@ -7,6 +7,7 @@ package org.mozilla.fenix.components
|
|||
import android.content.Context
|
||||
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
|
||||
import org.mozilla.fenix.test.Mockable
|
||||
import org.mozilla.fenix.utils.ClipboardHandler
|
||||
|
||||
/**
|
||||
* Provides access to all components.
|
||||
|
@ -22,7 +23,10 @@ class Components(private val context: Context) {
|
|||
val useCases by lazy {
|
||||
UseCases(context, core.sessionManager, core.engine.settings, search.searchEngineManager, core.client)
|
||||
}
|
||||
val utils by lazy { Utilities(context, core.sessionManager, useCases.sessionUseCases, useCases.searchUseCases) }
|
||||
val intentProcessors by lazy {
|
||||
IntentProcessors(context, core.sessionManager, useCases.sessionUseCases, useCases.searchUseCases)
|
||||
}
|
||||
val analytics by lazy { Analytics(context) }
|
||||
val publicSuffixList by lazy { PublicSuffixList(context) }
|
||||
val clipboardHandler by lazy { ClipboardHandler(context) }
|
||||
}
|
||||
|
|
|
@ -11,13 +11,12 @@ import mozilla.components.feature.intent.processing.TabIntentProcessor
|
|||
import mozilla.components.feature.search.SearchUseCases
|
||||
import mozilla.components.feature.session.SessionUseCases
|
||||
import org.mozilla.fenix.test.Mockable
|
||||
import org.mozilla.fenix.utils.ClipboardHandler
|
||||
|
||||
/**
|
||||
* Component group for miscellaneous components.
|
||||
*/
|
||||
@Mockable
|
||||
class Utilities(
|
||||
class IntentProcessors(
|
||||
private val context: Context,
|
||||
private val sessionManager: SessionManager,
|
||||
private val sessionUseCases: SessionUseCases,
|
||||
|
@ -40,8 +39,4 @@ class Utilities(
|
|||
val customTabIntentProcessor by lazy {
|
||||
CustomTabIntentProcessor(sessionManager, sessionUseCases.loadUrl, context.resources)
|
||||
}
|
||||
|
||||
val clipboardHandler by lazy {
|
||||
ClipboardHandler(context)
|
||||
}
|
||||
}
|
|
@ -54,7 +54,7 @@ class BrowserToolbarView(
|
|||
val isCustomTabSession = customTabSession != null
|
||||
|
||||
view.setOnUrlLongClickListener {
|
||||
val clipboard = view.context.components.utils.clipboardHandler
|
||||
val clipboard = view.context.components.clipboardHandler
|
||||
val customView = LayoutInflater.from(view.context).inflate(R.layout.browser_toolbar_popup_window, null)
|
||||
val popupWindow = PopupWindow(customView,
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT,
|
||||
|
|
|
@ -204,7 +204,7 @@ class SearchFragment : Fragment(), BackHandler {
|
|||
fill_link_from_clipboard.setOnClickListener {
|
||||
(activity as HomeActivity)
|
||||
.openToBrowserAndLoad(
|
||||
searchTermOrURL = requireContext().components.utils.clipboardHandler.url ?: "",
|
||||
searchTermOrURL = requireContext().components.clipboardHandler.url ?: "",
|
||||
newTab = searchStore.state.session == null,
|
||||
from = BrowserDirection.FromSearch
|
||||
)
|
||||
|
@ -216,7 +216,7 @@ class SearchFragment : Fragment(), BackHandler {
|
|||
updateSearchEngineIcon(it)
|
||||
updateSearchShortuctsIcon(it)
|
||||
updateSearchWithLabel(it)
|
||||
updateClipboardSuggestion(it, requireContext().components.utils.clipboardHandler.url)
|
||||
updateClipboardSuggestion(it, requireContext().components.clipboardHandler.url)
|
||||
}
|
||||
|
||||
startPostponedEnterTransition()
|
||||
|
@ -244,7 +244,7 @@ class SearchFragment : Fragment(), BackHandler {
|
|||
toolbarView.view.requestFocus()
|
||||
}
|
||||
|
||||
updateClipboardSuggestion(searchStore.state, requireContext().components.utils.clipboardHandler.url)
|
||||
updateClipboardSuggestion(searchStore.state, requireContext().components.clipboardHandler.url)
|
||||
|
||||
permissionDidUpdate = false
|
||||
(activity as AppCompatActivity).supportActionBar?.hide()
|
||||
|
|
|
@ -11,6 +11,9 @@ import mozilla.components.support.utils.WebURLFinder
|
|||
|
||||
private const val MIME_TYPE_TEXT_PLAIN = "text/plain"
|
||||
|
||||
/**
|
||||
* A clipboard utility class that allows copying and pasting links/text to & from the clipboard
|
||||
*/
|
||||
class ClipboardHandler(context: Context) {
|
||||
private val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
|
||||
|
@ -30,6 +33,8 @@ class ClipboardHandler(context: Context) {
|
|||
|
||||
val url: String?
|
||||
get() {
|
||||
if (text == null) { return null }
|
||||
|
||||
val finder = WebURLFinder(text)
|
||||
return finder.bestWebURL()
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ package org.mozilla.fenix.components
|
|||
import android.content.Context
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
||||
import org.mozilla.fenix.utils.ClipboardHandler
|
||||
|
||||
@ObsoleteCoroutinesApi
|
||||
class TestComponents(private val context: Context) : Components(context) {
|
||||
|
@ -25,8 +26,8 @@ class TestComponents(private val context: Context) : Components(context) {
|
|||
core.client
|
||||
)
|
||||
}
|
||||
override val utils by lazy {
|
||||
Utilities(
|
||||
override val intentProcessors by lazy {
|
||||
IntentProcessors(
|
||||
context,
|
||||
core.sessionManager,
|
||||
useCases.sessionUseCases,
|
||||
|
@ -34,4 +35,6 @@ class TestComponents(private val context: Context) : Components(context) {
|
|||
)
|
||||
}
|
||||
override val analytics by lazy { Analytics(context) }
|
||||
|
||||
override val clipboardHandler by lazy { ClipboardHandler(context) }
|
||||
}
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.utils
|
||||
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.TestApplication
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@ObsoleteCoroutinesApi
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
@Config(application = TestApplication::class)
|
||||
class ClipboardHandlerTest {
|
||||
|
||||
private val clipboardUrl = "https://www.mozilla.org"
|
||||
private val clipboardText = "Mozilla"
|
||||
private lateinit var clipboard: ClipboardManager
|
||||
private lateinit var clipboardHandler: ClipboardHandler
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
clipboard = testContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
clipboardHandler = ClipboardHandler(testContext)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getText() {
|
||||
assertEquals(null, clipboardHandler.text)
|
||||
|
||||
clipboard.primaryClip = ClipData.newPlainText("Text", clipboardText)
|
||||
assertEquals(clipboardText, clipboardHandler.text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun setText() {
|
||||
assertEquals(null, clipboardHandler.text)
|
||||
|
||||
clipboardHandler.text = clipboardText
|
||||
assertEquals(clipboardText, clipboardHandler.text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getUrl() {
|
||||
assertEquals(null, clipboardHandler.url)
|
||||
|
||||
clipboard.primaryClip = ClipData.newPlainText("Text", clipboardUrl)
|
||||
assertEquals(clipboardUrl, clipboardHandler.url)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue