adding tests around share intent handling
This commit is contained in:
parent
89348995c2
commit
ad76cbbc40
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.app.features.attachments
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import im.vector.lib.multipicker.MultiPicker
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class MultiPickerIncomingFiles @Inject constructor(
|
||||||
|
private val context: Context,
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun image(intent: Intent) = MultiPicker.get(MultiPicker.IMAGE).getIncomingFiles(context, intent).map { it.toContentAttachmentData() }
|
||||||
|
|
||||||
|
fun video(intent: Intent) = MultiPicker.get(MultiPicker.VIDEO).getIncomingFiles(context, intent).map { it.toContentAttachmentData() }
|
||||||
|
|
||||||
|
fun media(intent: Intent) = MultiPicker.get(MultiPicker.MEDIA).getIncomingFiles(context, intent).map { it.toContentAttachmentData() }
|
||||||
|
|
||||||
|
fun file(intent: Intent) = MultiPicker.get(MultiPicker.FILE).getIncomingFiles(context, intent).map { it.toContentAttachmentData() }
|
||||||
|
|
||||||
|
fun audio(intent: Intent) = MultiPicker.get(MultiPicker.AUDIO).getIncomingFiles(context, intent).map { it.toContentAttachmentData() }
|
||||||
|
}
|
||||||
@ -18,11 +18,12 @@ package im.vector.app.features.attachments
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import im.vector.lib.multipicker.MultiPicker
|
|
||||||
import org.matrix.android.sdk.api.session.content.ContentAttachmentData
|
import org.matrix.android.sdk.api.session.content.ContentAttachmentData
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ShareIntentHandler @Inject constructor() {
|
class ShareIntentHandler @Inject constructor(
|
||||||
|
private val multiPickerIncomingFiles: MultiPickerIncomingFiles,
|
||||||
|
) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This methods aims to handle incoming share intents.
|
* This methods aims to handle incoming share intents.
|
||||||
@ -33,38 +34,11 @@ class ShareIntentHandler @Inject constructor() {
|
|||||||
val type = intent.resolveType(context) ?: return false
|
val type = intent.resolveType(context) ?: return false
|
||||||
return when {
|
return when {
|
||||||
type == "text/plain" -> handlePlainText(intent, onPlainText)
|
type == "text/plain" -> handlePlainText(intent, onPlainText)
|
||||||
type.startsWith("image") -> {
|
type.startsWith("image") -> onFile(multiPickerIncomingFiles.image(intent)).let { true }
|
||||||
onFile(
|
type.startsWith("video") -> onFile(multiPickerIncomingFiles.video(intent)).let { true }
|
||||||
MultiPicker.get(MultiPicker.IMAGE).getIncomingFiles(context, intent).map {
|
type.startsWith("audio") -> onFile(multiPickerIncomingFiles.audio(intent)).let { true }
|
||||||
it.toContentAttachmentData()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
true
|
|
||||||
}
|
|
||||||
type.startsWith("video") -> {
|
|
||||||
onFile(
|
|
||||||
MultiPicker.get(MultiPicker.VIDEO).getIncomingFiles(context, intent).map {
|
|
||||||
it.toContentAttachmentData()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
true
|
|
||||||
}
|
|
||||||
type.startsWith("audio") -> {
|
|
||||||
onFile(
|
|
||||||
MultiPicker.get(MultiPicker.AUDIO).getIncomingFiles(context, intent).map {
|
|
||||||
it.toContentAttachmentData()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
type.startsWith("application") || type.startsWith("file") || type.startsWith("text") || type.startsWith("*") -> {
|
type.startsWith("application") || type.startsWith("file") || type.startsWith("text") || type.startsWith("*") -> {
|
||||||
onFile(
|
onFile(multiPickerIncomingFiles.file(intent)).let { true }
|
||||||
MultiPicker.get(MultiPicker.FILE).getIncomingFiles(context, intent).map {
|
|
||||||
it.toContentAttachmentData()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,166 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.app.features.attachments
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import im.vector.app.test.fakes.FakeContext
|
||||||
|
import im.vector.app.test.fakes.FakeFunction1
|
||||||
|
import im.vector.app.test.fakes.FakeIntent
|
||||||
|
import im.vector.app.test.fakes.FakeMultiPickerIncomingFiles
|
||||||
|
import im.vector.app.test.fixtures.ContentAttachmentDataFixture.aContentAttachmentData
|
||||||
|
import org.amshove.kluent.shouldBeEqualTo
|
||||||
|
import org.junit.Test
|
||||||
|
import org.matrix.android.sdk.api.session.content.ContentAttachmentData
|
||||||
|
|
||||||
|
private val A_CONTEXT = FakeContext().instance
|
||||||
|
private const val A_PLAIN_TEXT_EXTRA = "plain text for sharing"
|
||||||
|
private val A_CONTENT_ATTACHMENT_LIST = listOf(aContentAttachmentData())
|
||||||
|
|
||||||
|
class ShareIntentHandlerTest {
|
||||||
|
|
||||||
|
private val fakeMultiPickerIncomingFiles = FakeMultiPickerIncomingFiles()
|
||||||
|
private val onFile = FakeFunction1<List<ContentAttachmentData>>()
|
||||||
|
private val onPlainText = FakeFunction1<String>()
|
||||||
|
|
||||||
|
private val shareIntentHandler = ShareIntentHandler(fakeMultiPickerIncomingFiles.instance)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given an unhandled sharing intent type, when handling intent, then is not handled`() {
|
||||||
|
val unknownShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "unknown/type") }
|
||||||
|
|
||||||
|
val handled = handleIncomingShareIntent(unknownShareIntent)
|
||||||
|
|
||||||
|
onFile.verifyNoInteractions()
|
||||||
|
onPlainText.verifyNoInteractions()
|
||||||
|
handled shouldBeEqualTo false
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a plain text sharing intent, when handling intent, then is handled and parses plain text content`() {
|
||||||
|
val plainTextShareIntent = FakeIntent().also {
|
||||||
|
it.givenResolvesType(A_CONTEXT, "text/plain")
|
||||||
|
it.givenCharSequenceExtra(key = Intent.EXTRA_TEXT, value = A_PLAIN_TEXT_EXTRA)
|
||||||
|
}
|
||||||
|
|
||||||
|
val handled = handleIncomingShareIntent(plainTextShareIntent)
|
||||||
|
|
||||||
|
onFile.verifyNoInteractions()
|
||||||
|
onPlainText.assertValue(A_PLAIN_TEXT_EXTRA)
|
||||||
|
handled shouldBeEqualTo true
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given an empty plain text sharing intent, when handling intent, then is not handled`() {
|
||||||
|
val plainTextShareIntent = FakeIntent().also {
|
||||||
|
it.givenResolvesType(A_CONTEXT, "text/plain")
|
||||||
|
it.givenCharSequenceExtra(key = Intent.EXTRA_TEXT, value = "")
|
||||||
|
}
|
||||||
|
|
||||||
|
val handled = handleIncomingShareIntent(plainTextShareIntent)
|
||||||
|
|
||||||
|
onFile.verifyNoInteractions()
|
||||||
|
onPlainText.verifyNoInteractions()
|
||||||
|
handled shouldBeEqualTo false
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given an image sharing intent, when handling intent, then is handled and parses image files`() {
|
||||||
|
val imageShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "image/png") }
|
||||||
|
fakeMultiPickerIncomingFiles.givenImageReturns(imageShareIntent.instance, A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
|
||||||
|
val handled = handleIncomingShareIntent(imageShareIntent)
|
||||||
|
|
||||||
|
onFile.assertValue(A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
onPlainText.verifyNoInteractions()
|
||||||
|
handled shouldBeEqualTo true
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given an audio sharing intent, when handling intent, then is handled and parses audio files`() {
|
||||||
|
val audioShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "audio/mp3") }
|
||||||
|
fakeMultiPickerIncomingFiles.givenAudioReturns(audioShareIntent.instance, A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
|
||||||
|
val handled = handleIncomingShareIntent(audioShareIntent)
|
||||||
|
|
||||||
|
onFile.assertValue(A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
onPlainText.verifyNoInteractions()
|
||||||
|
handled shouldBeEqualTo true
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given an video sharing intent, when handling intent, then is handled and parses video files`() {
|
||||||
|
val videoShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "video/mp4") }
|
||||||
|
fakeMultiPickerIncomingFiles.givenVideoReturns(videoShareIntent.instance, A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
|
||||||
|
val handled = handleIncomingShareIntent(videoShareIntent)
|
||||||
|
|
||||||
|
onFile.assertValue(A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
onPlainText.verifyNoInteractions()
|
||||||
|
handled shouldBeEqualTo true
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a file sharing intent, when handling intent, then is handled and parses files`() {
|
||||||
|
val fileShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "file/*") }
|
||||||
|
fakeMultiPickerIncomingFiles.givenFileReturns(fileShareIntent.instance, A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
|
||||||
|
val handled = handleIncomingShareIntent(fileShareIntent)
|
||||||
|
|
||||||
|
onFile.assertValue(A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
onPlainText.verifyNoInteractions()
|
||||||
|
handled shouldBeEqualTo true
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a application sharing intent, when handling intent, then is handled and parses files`() {
|
||||||
|
val fileShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "application/apk") }
|
||||||
|
fakeMultiPickerIncomingFiles.givenFileReturns(fileShareIntent.instance, A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
|
||||||
|
handleIncomingShareIntent(fileShareIntent)
|
||||||
|
|
||||||
|
onFile.assertValue(A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
onPlainText.verifyNoInteractions()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a text sharing intent, when handling intent, then is handled and parses text files`() {
|
||||||
|
val fileShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "text/ics") }
|
||||||
|
fakeMultiPickerIncomingFiles.givenFileReturns(fileShareIntent.instance, A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
|
||||||
|
val handled = handleIncomingShareIntent(fileShareIntent)
|
||||||
|
|
||||||
|
onFile.assertValue(A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
onPlainText.verifyNoInteractions()
|
||||||
|
handled shouldBeEqualTo true
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a wildcard sharing intent, when handling intent, then is handled and parses files`() {
|
||||||
|
val fileShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "*/*") }
|
||||||
|
fakeMultiPickerIncomingFiles.givenFileReturns(fileShareIntent.instance, A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
|
||||||
|
val handled = handleIncomingShareIntent(fileShareIntent)
|
||||||
|
|
||||||
|
onFile.assertValue(A_CONTENT_ATTACHMENT_LIST)
|
||||||
|
onPlainText.verifyNoInteractions()
|
||||||
|
handled shouldBeEqualTo true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleIncomingShareIntent(intent: FakeIntent): Boolean {
|
||||||
|
return shareIntentHandler.handleIncomingShareIntent(A_CONTEXT, intent.instance, onFile.capture, onPlainText.capture)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.app.test.fakes
|
||||||
|
|
||||||
|
import org.amshove.kluent.shouldBeEqualTo
|
||||||
|
|
||||||
|
class FakeFunction1<T : Any> {
|
||||||
|
|
||||||
|
private lateinit var capturedValue: T
|
||||||
|
|
||||||
|
val capture: (T) -> Unit = {
|
||||||
|
capturedValue = it
|
||||||
|
}
|
||||||
|
|
||||||
|
fun verifyNoInteractions() {
|
||||||
|
this::capturedValue.isInitialized shouldBeEqualTo false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun assertValue(value: T) {
|
||||||
|
capturedValue shouldBeEqualTo value
|
||||||
|
}
|
||||||
|
}
|
||||||
35
vector/src/test/java/im/vector/app/test/fakes/FakeIntent.kt
Normal file
35
vector/src/test/java/im/vector/app/test/fakes/FakeIntent.kt
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.app.test.fakes
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
|
||||||
|
class FakeIntent {
|
||||||
|
|
||||||
|
val instance = mockk<Intent>()
|
||||||
|
|
||||||
|
fun givenResolvesType(context: Context, type: String?) {
|
||||||
|
every { instance.resolveType(context) } returns type
|
||||||
|
}
|
||||||
|
|
||||||
|
fun givenCharSequenceExtra(key: String, value: CharSequence) {
|
||||||
|
every { instance.getCharSequenceExtra(key) } returns value
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.app.test.fakes
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import im.vector.app.features.attachments.MultiPickerIncomingFiles
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import org.matrix.android.sdk.api.session.content.ContentAttachmentData
|
||||||
|
|
||||||
|
class FakeMultiPickerIncomingFiles {
|
||||||
|
|
||||||
|
val instance = mockk<MultiPickerIncomingFiles>()
|
||||||
|
|
||||||
|
fun givenFileReturns(intent: Intent, result: List<ContentAttachmentData>) {
|
||||||
|
every { instance.file(intent) } returns result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun givenAudioReturns(intent: Intent, result: List<ContentAttachmentData>) {
|
||||||
|
every { instance.audio(intent) } returns result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun givenVideoReturns(intent: Intent, result: List<ContentAttachmentData>) {
|
||||||
|
every { instance.video(intent) } returns result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun givenImageReturns(intent: Intent, result: List<ContentAttachmentData>) {
|
||||||
|
every { instance.image(intent) } returns result
|
||||||
|
}
|
||||||
|
}
|
||||||
29
vector/src/test/java/im/vector/app/test/fixtures/ContentAttachmentDataFixture.kt
vendored
Normal file
29
vector/src/test/java/im/vector/app/test/fixtures/ContentAttachmentDataFixture.kt
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.app.test.fixtures
|
||||||
|
|
||||||
|
import im.vector.app.test.fakes.FakeUri
|
||||||
|
import org.matrix.android.sdk.api.session.content.ContentAttachmentData
|
||||||
|
|
||||||
|
object ContentAttachmentDataFixture {
|
||||||
|
|
||||||
|
fun aContentAttachmentData() = ContentAttachmentData(
|
||||||
|
type = ContentAttachmentData.Type.AUDIO,
|
||||||
|
queryUri = FakeUri().instance,
|
||||||
|
mimeType = null,
|
||||||
|
)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user