2018-03-28 04:47:00 +11:00
|
|
|
/* Copyright 2018 charlag
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
package com.keylesspalace.tusky
|
|
|
|
|
2018-04-29 18:08:25 +10:00
|
|
|
import android.text.SpannedString
|
2018-03-10 08:02:32 +11:00
|
|
|
import android.widget.EditText
|
|
|
|
import com.keylesspalace.tusky.db.AccountEntity
|
|
|
|
import com.keylesspalace.tusky.db.AccountManager
|
2018-05-27 18:22:12 +10:00
|
|
|
import com.keylesspalace.tusky.db.AppDatabase
|
|
|
|
import com.keylesspalace.tusky.db.InstanceDao
|
2018-04-29 18:08:25 +10:00
|
|
|
import com.keylesspalace.tusky.entity.Account
|
2018-04-14 06:37:21 +10:00
|
|
|
import com.keylesspalace.tusky.entity.Emoji
|
2018-04-22 18:35:46 +10:00
|
|
|
import com.keylesspalace.tusky.entity.Instance
|
2018-04-14 06:37:21 +10:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
|
|
|
import okhttp3.Request
|
2018-04-29 18:08:25 +10:00
|
|
|
import okhttp3.ResponseBody
|
2018-05-17 03:14:26 +10:00
|
|
|
import org.junit.Assert
|
2018-04-29 18:08:25 +10:00
|
|
|
import org.junit.Assert.*
|
2018-03-10 08:02:32 +11:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.runner.RunWith
|
|
|
|
import org.mockito.Mockito
|
|
|
|
import org.mockito.Mockito.`when`
|
2018-05-27 18:22:12 +10:00
|
|
|
import org.mockito.Mockito.mock
|
2018-03-10 08:02:32 +11:00
|
|
|
import org.robolectric.Robolectric
|
|
|
|
import org.robolectric.RobolectricTestRunner
|
|
|
|
import org.robolectric.annotation.Config
|
|
|
|
import org.robolectric.fakes.RoboMenuItem
|
2018-04-14 06:37:21 +10:00
|
|
|
import retrofit2.Call
|
|
|
|
import retrofit2.Callback
|
|
|
|
import retrofit2.Response
|
2018-03-10 08:02:32 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by charlag on 3/7/18.
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Config(application = FakeTuskyApplication::class)
|
|
|
|
@RunWith(RobolectricTestRunner::class)
|
|
|
|
class ComposeActivityTest {
|
|
|
|
|
|
|
|
lateinit var activity: ComposeActivity
|
|
|
|
lateinit var accountManagerMock: AccountManager
|
2018-04-14 06:37:21 +10:00
|
|
|
lateinit var apiMock: MastodonApi
|
2018-03-10 08:02:32 +11:00
|
|
|
|
|
|
|
val account = AccountEntity(
|
|
|
|
id = 1,
|
|
|
|
domain = "example.token",
|
|
|
|
accessToken = "token",
|
|
|
|
isActive = true,
|
|
|
|
accountId = "1",
|
|
|
|
username = "username",
|
|
|
|
displayName = "Display Name",
|
|
|
|
profilePictureUrl = "",
|
|
|
|
notificationsEnabled = true,
|
|
|
|
notificationsMentioned = true,
|
|
|
|
notificationsFollowed = true,
|
|
|
|
notificationsReblogged = true,
|
|
|
|
notificationsFavorited = true,
|
|
|
|
notificationSound = true,
|
|
|
|
notificationVibration = true,
|
|
|
|
notificationLight = true
|
|
|
|
)
|
2018-04-29 18:08:25 +10:00
|
|
|
var instanceResponseCallback: ((Call<Instance>?, Callback<Instance>?)->Unit)? = null
|
2018-03-10 08:02:32 +11:00
|
|
|
|
|
|
|
@Before
|
2018-04-29 18:08:25 +10:00
|
|
|
fun setupActivity() {
|
2018-03-10 08:02:32 +11:00
|
|
|
val controller = Robolectric.buildActivity(ComposeActivity::class.java)
|
|
|
|
activity = controller.get()
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
accountManagerMock = Mockito.mock(AccountManager::class.java)
|
2018-04-14 06:37:21 +10:00
|
|
|
|
|
|
|
apiMock = Mockito.mock(MastodonApi::class.java)
|
|
|
|
`when`(apiMock.customEmojis).thenReturn(object: Call<List<Emoji>> {
|
|
|
|
override fun isExecuted(): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
override fun clone(): Call<List<Emoji>> {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
override fun isCanceled(): Boolean {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
override fun cancel() {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
override fun execute(): Response<List<Emoji>> {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
override fun request(): Request {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun enqueue(callback: Callback<List<Emoji>>?) {}
|
|
|
|
})
|
2018-04-22 18:35:46 +10:00
|
|
|
`when`(apiMock.instance).thenReturn(object: Call<Instance> {
|
|
|
|
override fun isExecuted(): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
override fun clone(): Call<Instance> {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
override fun isCanceled(): Boolean {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
override fun cancel() {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
override fun execute(): Response<Instance> {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
override fun request(): Request {
|
|
|
|
throw Error("not implemented")
|
|
|
|
}
|
|
|
|
|
2018-04-29 18:08:25 +10:00
|
|
|
override fun enqueue(callback: Callback<Instance>?) {
|
|
|
|
instanceResponseCallback?.invoke(this, callback)
|
|
|
|
}
|
2018-04-22 18:35:46 +10:00
|
|
|
})
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
val instanceDaoMock = mock(InstanceDao::class.java)
|
|
|
|
val dbMock = mock(AppDatabase::class.java)
|
|
|
|
`when`(dbMock.instanceDao()).thenReturn(instanceDaoMock)
|
|
|
|
|
2018-04-14 06:37:21 +10:00
|
|
|
activity.mastodonApi = apiMock
|
2018-03-28 04:47:00 +11:00
|
|
|
activity.accountManager = accountManagerMock
|
2018-05-27 18:22:12 +10:00
|
|
|
activity.database = dbMock
|
2018-04-14 06:37:21 +10:00
|
|
|
|
|
|
|
`when`(accountManagerMock.activeAccount).thenReturn(account)
|
|
|
|
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
controller.create().start()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun whenCloseButtonPressedAndEmpty_finish() {
|
|
|
|
clickUp()
|
|
|
|
assertTrue(activity.isFinishing)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun whenCloseButtonPressedNotEmpty_notFinish() {
|
|
|
|
insertSomeTextInContent()
|
|
|
|
clickUp()
|
|
|
|
assertFalse(activity.isFinishing)
|
|
|
|
// We would like to check for dialog but Robolectric doesn't work with AppCompat v7 yet
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun whenBackButtonPressedAndEmpty_finish() {
|
|
|
|
clickBack()
|
|
|
|
assertTrue(activity.isFinishing)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun whenBackButtonPressedNotEmpty_notFinish() {
|
|
|
|
insertSomeTextInContent()
|
|
|
|
clickBack()
|
|
|
|
assertFalse(activity.isFinishing)
|
|
|
|
// We would like to check for dialog but Robolectric doesn't work with AppCompat v7 yet
|
|
|
|
}
|
|
|
|
|
2018-04-29 18:08:25 +10:00
|
|
|
@Test
|
|
|
|
fun whenMaximumTootCharsIsNull_defaultLimitIsUsed() {
|
|
|
|
instanceResponseCallback = getSuccessResponseCallbackWithMaximumTootCharacters(null)
|
|
|
|
setupActivity()
|
|
|
|
assertEquals(ComposeActivity.STATUS_CHARACTER_LIMIT, activity.maximumTootCharacters)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun whenMaximumTootCharsIsPopulated_customLimitIsUsed() {
|
|
|
|
val customMaximum = 1000
|
|
|
|
instanceResponseCallback = getSuccessResponseCallbackWithMaximumTootCharacters(customMaximum)
|
|
|
|
setupActivity()
|
|
|
|
assertEquals(customMaximum, activity.maximumTootCharacters)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun whenInitialInstanceRequestFails_defaultValueIsUsed() {
|
|
|
|
instanceResponseCallback = {
|
|
|
|
call: Call<Instance>?, callback: Callback<Instance>? ->
|
|
|
|
if (call != null) {
|
|
|
|
callback?.onResponse(call, Response.error(400, ResponseBody.create(null, "")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setupActivity()
|
|
|
|
assertEquals(ComposeActivity.STATUS_CHARACTER_LIMIT, activity.maximumTootCharacters)
|
|
|
|
}
|
|
|
|
|
2018-05-17 03:14:26 +10:00
|
|
|
@Test
|
|
|
|
fun whenTextContainsUrl_onlyEllipsizedURLIsCountedAgainstCharacterLimit() {
|
|
|
|
val url = "https://www.google.dk/search?biw=1920&bih=990&tbm=isch&sa=1&ei=bmDrWuOoKMv6kwWOkIaoDQ&q=indiana+jones+i+hate+snakes+animated&oq=indiana+jones+i+hate+snakes+animated&gs_l=psy-ab.3...54174.55443.0.55553.9.7.0.0.0.0.255.333.1j0j1.2.0....0...1c.1.64.psy-ab..7.0.0....0.40G-kcDkC6A#imgdii=PSp15hQjN1JqvM:&imgrc=H0hyE2JW5wrpBM:"
|
|
|
|
val additionalContent = "Check out this @image #search result: "
|
|
|
|
insertSomeTextInContent(additionalContent + url)
|
|
|
|
Assert.assertEquals(activity.calculateRemainingCharacters(), activity.maximumTootCharacters - additionalContent.length - ComposeActivity.MAXIMUM_URL_LENGTH)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun whenTextContainsMultipleURLs_allURLsGetEllipsized() {
|
|
|
|
val url = "https://www.google.dk/search?biw=1920&bih=990&tbm=isch&sa=1&ei=bmDrWuOoKMv6kwWOkIaoDQ&q=indiana+jones+i+hate+snakes+animated&oq=indiana+jones+i+hate+snakes+animated&gs_l=psy-ab.3...54174.55443.0.55553.9.7.0.0.0.0.255.333.1j0j1.2.0....0...1c.1.64.psy-ab..7.0.0....0.40G-kcDkC6A#imgdii=PSp15hQjN1JqvM:&imgrc=H0hyE2JW5wrpBM:"
|
|
|
|
val additionalContent = " Check out this @image #search result: "
|
|
|
|
insertSomeTextInContent(url + additionalContent + url)
|
|
|
|
Assert.assertEquals(activity.calculateRemainingCharacters(),
|
|
|
|
activity.maximumTootCharacters - additionalContent.length - (ComposeActivity.MAXIMUM_URL_LENGTH * 2))
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
private fun clickUp() {
|
|
|
|
val menuItem = RoboMenuItem(android.R.id.home)
|
|
|
|
activity.onOptionsItemSelected(menuItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun clickBack() {
|
|
|
|
activity.onBackPressed()
|
|
|
|
}
|
|
|
|
|
2018-05-17 03:14:26 +10:00
|
|
|
private fun insertSomeTextInContent(text: String? = null) {
|
|
|
|
activity.findViewById<EditText>(R.id.composeEditField).setText(text ?: "Some text")
|
2018-03-10 08:02:32 +11:00
|
|
|
}
|
2018-04-29 18:08:25 +10:00
|
|
|
|
|
|
|
private fun getInstanceWithMaximumTootCharacters(maximumTootCharacters: Int?): Instance
|
|
|
|
{
|
|
|
|
return Instance(
|
|
|
|
"https://example.token",
|
|
|
|
"Example dot Token",
|
|
|
|
"Example instance for testing",
|
|
|
|
"admin@example.token",
|
|
|
|
"2.6.3",
|
|
|
|
HashMap<String, String>(),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
listOf("en"),
|
|
|
|
Account(
|
|
|
|
"1",
|
|
|
|
"admin",
|
|
|
|
"admin",
|
|
|
|
"admin",
|
|
|
|
SpannedString(""),
|
|
|
|
"https://example.token",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2018-06-18 21:26:18 +10:00
|
|
|
null,
|
|
|
|
false
|
2018-04-29 18:08:25 +10:00
|
|
|
),
|
|
|
|
maximumTootCharacters
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getSuccessResponseCallbackWithMaximumTootCharacters(maximumTootCharacters: Int?): (Call<Instance>?, Callback<Instance>?) -> Unit
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
call: Call<Instance>?, callback: Callback<Instance>? ->
|
|
|
|
if (call != null) {
|
|
|
|
callback?.onResponse(call, Response.success(getInstanceWithMaximumTootCharacters(maximumTootCharacters)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 08:02:32 +11:00
|
|
|
}
|