Convert ShareUtils to Kotlin so no TextUtils static mock is needed in tests, and re-enable relevant tests

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-05-27 14:40:45 +02:00
parent fd4b7080cc
commit 820db1e167
No known key found for this signature in database
GPG key ID: 2585783189A62105
2 changed files with 27 additions and 43 deletions

View file

@ -17,35 +17,31 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.nextcloud.talk.utils
package com.nextcloud.talk.utils; import android.content.Context
import com.nextcloud.talk.R
import com.nextcloud.talk.models.json.conversations.Conversation
import com.nextcloud.talk.utils.database.user.UserUtils
import android.content.Context; object ShareUtils {
import android.text.TextUtils; fun getStringForIntent(
context: Context?,
import com.nextcloud.talk.R; password: String?,
import com.nextcloud.talk.models.database.UserEntity; userUtils: UserUtils?,
import com.nextcloud.talk.models.json.conversations.Conversation; conversation: Conversation?
import com.nextcloud.talk.utils.database.user.UserUtils; ): String {
val userEntity = userUtils?.currentUser
import androidx.annotation.Nullable; var shareString = ""
public class ShareUtils {
public static String getStringForIntent(Context context, @Nullable String password, UserUtils userUtils, Conversation
conversation) {
UserEntity userEntity = userUtils.getCurrentUser();
String shareString = "";
if (userEntity != null && context != null) { if (userEntity != null && context != null) {
shareString = String.format(context.getResources().getString(R.string.nc_share_text), shareString = String.format(
userEntity.getBaseUrl(), conversation.getToken()); context.resources.getString(R.string.nc_share_text),
userEntity.baseUrl, conversation?.token
if (!TextUtils.isEmpty(password)) { )
shareString += String.format(context.getResources().getString(R.string.nc_share_text_pass), password); if (!password.isNullOrEmpty()) {
shareString += String.format(context.resources.getString(R.string.nc_share_text_pass), password)
} }
} }
return shareString
return shareString;
} }
} }

View file

@ -21,7 +21,6 @@ package com.nextcloud.talk.utils
import android.content.Context import android.content.Context
import android.content.res.Resources import android.content.res.Resources
import android.text.TextUtils
import at.bitfire.dav4jvm.HttpUtils.parseDate import at.bitfire.dav4jvm.HttpUtils.parseDate
import com.nextcloud.talk.R import com.nextcloud.talk.R
import com.nextcloud.talk.models.database.UserEntity import com.nextcloud.talk.models.database.UserEntity
@ -32,19 +31,10 @@ import org.junit.Assert.assertEquals
import org.junit.Before import org.junit.Before
import org.junit.Ignore import org.junit.Ignore
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.MockitoAnnotations import org.mockito.MockitoAnnotations
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import java.text.ParseException
@RunWith(PowerMockRunner::class)
@PrepareForTest(TextUtils::class)
@Ignore("Test fails on CI server. See issue https://github.com/nextcloud/talk-android/issues/1737")
class ShareUtilsTest { class ShareUtilsTest {
@Mock @Mock
private val context: Context? = null private val context: Context? = null
@ -55,30 +45,29 @@ class ShareUtilsTest {
@Mock @Mock
private val userUtils: UserUtils? = null private val userUtils: UserUtils? = null
@Mock
private val conversation: Conversation? = null
@Mock @Mock
private val userEntity: UserEntity? = null private val userEntity: UserEntity? = null
private val baseUrl = "https://my.nextcloud.com" private val baseUrl = "https://my.nextcloud.com"
private val token = "2aotbrjr" private val token = "2aotbrjr"
private lateinit var conversation: Conversation
@Before @Before
fun setUp() { fun setUp() {
MockitoAnnotations.openMocks(this) MockitoAnnotations.openMocks(this)
PowerMockito.mockStatic(TextUtils::class.java)
Mockito.`when`(userUtils!!.currentUser).thenReturn(userEntity) Mockito.`when`(userUtils!!.currentUser).thenReturn(userEntity)
Mockito.`when`(userEntity!!.baseUrl).thenReturn(baseUrl) Mockito.`when`(userEntity!!.baseUrl).thenReturn(baseUrl)
Mockito.`when`(conversation!!.token).thenReturn(token)
Mockito.`when`(context!!.resources).thenReturn(resources) Mockito.`when`(context!!.resources).thenReturn(resources)
Mockito.`when`(resources!!.getString(R.string.nc_share_text)) Mockito.`when`(resources!!.getString(R.string.nc_share_text))
.thenReturn("Join the conversation at %1\$s/index.php/call/%2\$s") .thenReturn("Join the conversation at %1\$s/index.php/call/%2\$s")
Mockito.`when`(resources.getString(R.string.nc_share_text_pass)).thenReturn("\nPassword: %1\$s") Mockito.`when`(resources.getString(R.string.nc_share_text_pass)).thenReturn("\nPassword: %1\$s")
conversation = Conversation(token = token)
} }
@Test @Test
fun stringForIntent_noPasswordGiven_correctStringWithoutPasswordReturned() { fun stringForIntent_noPasswordGiven_correctStringWithoutPasswordReturned() {
PowerMockito.`when`(TextUtils.isEmpty(ArgumentMatchers.anyString())).thenReturn(true)
val expectedResult = String.format( val expectedResult = String.format(
"Join the conversation at %s/index.php/call/%s", "Join the conversation at %s/index.php/call/%s",
baseUrl, token baseUrl, token
@ -91,7 +80,6 @@ class ShareUtilsTest {
@Test @Test
fun stringForIntent_passwordGiven_correctStringWithPasswordReturned() { fun stringForIntent_passwordGiven_correctStringWithPasswordReturned() {
PowerMockito.`when`(TextUtils.isEmpty(ArgumentMatchers.anyString())).thenReturn(false)
val password = "superSecret" val password = "superSecret"
val expectedResult = String.format( val expectedResult = String.format(
"Join the conversation at %s/index.php/call/%s\nPassword: %s", "Join the conversation at %s/index.php/call/%s\nPassword: %s",
@ -104,7 +92,7 @@ class ShareUtilsTest {
} }
@Test @Test
@Throws(ParseException::class) @Ignore("Test fails on CI server. See issue https://github.com/nextcloud/talk-android/issues/1737")
fun date() { fun date() {
assertEquals(1207778138000, parseDate("Mon, 09 Apr 2008 23:55:38 GMT")?.time) assertEquals(1207778138000, parseDate("Mon, 09 Apr 2008 23:55:38 GMT")?.time)
} }