|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 New Vector Ltd |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package im.vector.app.features.attachments |
| 18 | + |
| 19 | +import android.content.Intent |
| 20 | +import im.vector.app.test.fakes.FakeContext |
| 21 | +import im.vector.app.test.fakes.FakeFunction1 |
| 22 | +import im.vector.app.test.fakes.FakeIntent |
| 23 | +import im.vector.app.test.fakes.FakeMultiPickerIncomingFiles |
| 24 | +import im.vector.app.test.fixtures.ContentAttachmentDataFixture.aContentAttachmentData |
| 25 | +import org.amshove.kluent.shouldBeEqualTo |
| 26 | +import org.junit.Test |
| 27 | +import org.matrix.android.sdk.api.session.content.ContentAttachmentData |
| 28 | + |
| 29 | +private val A_CONTEXT = FakeContext().instance |
| 30 | +private const val A_PLAIN_TEXT_EXTRA = "plain text for sharing" |
| 31 | +private val A_CONTENT_ATTACHMENT_LIST = listOf(aContentAttachmentData()) |
| 32 | + |
| 33 | +class ShareIntentHandlerTest { |
| 34 | + |
| 35 | + private val fakeMultiPickerIncomingFiles = FakeMultiPickerIncomingFiles() |
| 36 | + private val onFile = FakeFunction1<List<ContentAttachmentData>>() |
| 37 | + private val onPlainText = FakeFunction1<String>() |
| 38 | + |
| 39 | + private val shareIntentHandler = ShareIntentHandler(fakeMultiPickerIncomingFiles.instance, A_CONTEXT) |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `given an unhandled sharing intent type, when handling intent, then is not handled`() { |
| 43 | + val unknownShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "unknown/type") } |
| 44 | + |
| 45 | + val handled = handleIncomingShareIntent(unknownShareIntent) |
| 46 | + |
| 47 | + onFile.verifyNoInteractions() |
| 48 | + onPlainText.verifyNoInteractions() |
| 49 | + handled shouldBeEqualTo false |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `given a plain text sharing intent, when handling intent, then is handled and parses plain text content`() { |
| 54 | + val plainTextShareIntent = FakeIntent().also { |
| 55 | + it.givenResolvesType(A_CONTEXT, "text/plain") |
| 56 | + it.givenCharSequenceExtra(key = Intent.EXTRA_TEXT, value = A_PLAIN_TEXT_EXTRA) |
| 57 | + } |
| 58 | + |
| 59 | + val handled = handleIncomingShareIntent(plainTextShareIntent) |
| 60 | + |
| 61 | + onFile.verifyNoInteractions() |
| 62 | + onPlainText.assertValue(A_PLAIN_TEXT_EXTRA) |
| 63 | + handled shouldBeEqualTo true |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `given an empty plain text sharing intent, when handling intent, then is not handled`() { |
| 68 | + val plainTextShareIntent = FakeIntent().also { |
| 69 | + it.givenResolvesType(A_CONTEXT, "text/plain") |
| 70 | + it.givenCharSequenceExtra(key = Intent.EXTRA_TEXT, value = "") |
| 71 | + } |
| 72 | + |
| 73 | + val handled = handleIncomingShareIntent(plainTextShareIntent) |
| 74 | + |
| 75 | + onFile.verifyNoInteractions() |
| 76 | + onPlainText.verifyNoInteractions() |
| 77 | + handled shouldBeEqualTo false |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `given an image sharing intent, when handling intent, then is handled and parses image files`() { |
| 82 | + val imageShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "image/png") } |
| 83 | + fakeMultiPickerIncomingFiles.givenImageReturns(imageShareIntent.instance, A_CONTENT_ATTACHMENT_LIST) |
| 84 | + |
| 85 | + val handled = handleIncomingShareIntent(imageShareIntent) |
| 86 | + |
| 87 | + onFile.assertValue(A_CONTENT_ATTACHMENT_LIST) |
| 88 | + onPlainText.verifyNoInteractions() |
| 89 | + handled shouldBeEqualTo true |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun `given an audio sharing intent, when handling intent, then is handled and parses audio files`() { |
| 94 | + val audioShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "audio/mp3") } |
| 95 | + fakeMultiPickerIncomingFiles.givenAudioReturns(audioShareIntent.instance, A_CONTENT_ATTACHMENT_LIST) |
| 96 | + |
| 97 | + val handled = handleIncomingShareIntent(audioShareIntent) |
| 98 | + |
| 99 | + onFile.assertValue(A_CONTENT_ATTACHMENT_LIST) |
| 100 | + onPlainText.verifyNoInteractions() |
| 101 | + handled shouldBeEqualTo true |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `given an video sharing intent, when handling intent, then is handled and parses video files`() { |
| 106 | + val videoShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "video/mp4") } |
| 107 | + fakeMultiPickerIncomingFiles.givenVideoReturns(videoShareIntent.instance, A_CONTENT_ATTACHMENT_LIST) |
| 108 | + |
| 109 | + val handled = handleIncomingShareIntent(videoShareIntent) |
| 110 | + |
| 111 | + onFile.assertValue(A_CONTENT_ATTACHMENT_LIST) |
| 112 | + onPlainText.verifyNoInteractions() |
| 113 | + handled shouldBeEqualTo true |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun `given a file sharing intent, when handling intent, then is handled and parses files`() { |
| 118 | + val fileShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "file/*") } |
| 119 | + fakeMultiPickerIncomingFiles.givenFileReturns(fileShareIntent.instance, A_CONTENT_ATTACHMENT_LIST) |
| 120 | + |
| 121 | + val handled = handleIncomingShareIntent(fileShareIntent) |
| 122 | + |
| 123 | + onFile.assertValue(A_CONTENT_ATTACHMENT_LIST) |
| 124 | + onPlainText.verifyNoInteractions() |
| 125 | + handled shouldBeEqualTo true |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + fun `given a application sharing intent, when handling intent, then is handled and parses files`() { |
| 130 | + val fileShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "application/apk") } |
| 131 | + fakeMultiPickerIncomingFiles.givenFileReturns(fileShareIntent.instance, A_CONTENT_ATTACHMENT_LIST) |
| 132 | + |
| 133 | + handleIncomingShareIntent(fileShareIntent) |
| 134 | + |
| 135 | + onFile.assertValue(A_CONTENT_ATTACHMENT_LIST) |
| 136 | + onPlainText.verifyNoInteractions() |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + fun `given a text sharing intent, when handling intent, then is handled and parses text files`() { |
| 141 | + val fileShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "text/ics") } |
| 142 | + fakeMultiPickerIncomingFiles.givenFileReturns(fileShareIntent.instance, A_CONTENT_ATTACHMENT_LIST) |
| 143 | + |
| 144 | + val handled = handleIncomingShareIntent(fileShareIntent) |
| 145 | + |
| 146 | + onFile.assertValue(A_CONTENT_ATTACHMENT_LIST) |
| 147 | + onPlainText.verifyNoInteractions() |
| 148 | + handled shouldBeEqualTo true |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + fun `given a wildcard sharing intent, when handling intent, then is handled and parses files`() { |
| 153 | + val fileShareIntent = FakeIntent().also { it.givenResolvesType(A_CONTEXT, "*/*") } |
| 154 | + fakeMultiPickerIncomingFiles.givenFileReturns(fileShareIntent.instance, A_CONTENT_ATTACHMENT_LIST) |
| 155 | + |
| 156 | + val handled = handleIncomingShareIntent(fileShareIntent) |
| 157 | + |
| 158 | + onFile.assertValue(A_CONTENT_ATTACHMENT_LIST) |
| 159 | + onPlainText.verifyNoInteractions() |
| 160 | + handled shouldBeEqualTo true |
| 161 | + } |
| 162 | + |
| 163 | + private fun handleIncomingShareIntent(intent: FakeIntent): Boolean { |
| 164 | + return shareIntentHandler.handleIncomingShareIntent(intent.instance, onFile.capture, onPlainText.capture) |
| 165 | + } |
| 166 | +} |
0 commit comments