Add ComposeActivity tests. Add ServiceLocator (#542)
This commit is contained in:
parent
4e617dccc7
commit
28e46c9cc0
18 changed files with 337 additions and 154 deletions
103
app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt
Normal file
103
app/src/test/java/com/keylesspalace/tusky/ComposeActivityTest.kt
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
package com.keylesspalace.tusky
|
||||
|
||||
import android.widget.EditText
|
||||
import com.keylesspalace.tusky.db.AccountEntity
|
||||
import com.keylesspalace.tusky.db.AccountManager
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.fakes.RoboMenuItem
|
||||
|
||||
/**
|
||||
* Created by charlag on 3/7/18.
|
||||
*/
|
||||
|
||||
@Config(application = FakeTuskyApplication::class)
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class ComposeActivityTest {
|
||||
|
||||
lateinit var activity: ComposeActivity
|
||||
lateinit var application: FakeTuskyApplication
|
||||
lateinit var serviceLocator: TuskyApplication.ServiceLocator
|
||||
lateinit var accountManagerMock: AccountManager
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
@Before
|
||||
fun before() {
|
||||
val controller = Robolectric.buildActivity(ComposeActivity::class.java)
|
||||
activity = controller.get()
|
||||
application = activity.application as FakeTuskyApplication
|
||||
serviceLocator = Mockito.mock(TuskyApplication.ServiceLocator::class.java)
|
||||
application.locator = serviceLocator
|
||||
accountManagerMock = Mockito.mock(AccountManager::class.java)
|
||||
`when`(serviceLocator.get(AccountManager::class.java)).thenReturn(accountManagerMock)
|
||||
`when`(accountManagerMock.activeAccount).thenReturn(account)
|
||||
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
|
||||
}
|
||||
|
||||
private fun clickUp() {
|
||||
val menuItem = RoboMenuItem(android.R.id.home)
|
||||
activity.onOptionsItemSelected(menuItem)
|
||||
}
|
||||
|
||||
private fun clickBack() {
|
||||
activity.onBackPressed()
|
||||
}
|
||||
|
||||
private fun insertSomeTextInContent() {
|
||||
activity.findViewById<EditText>(R.id.compose_edit_field).setText("Some text")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package com.keylesspalace.tusky;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.keylesspalace.tusky
|
||||
|
||||
/**
|
||||
* Created by charlag on 3/7/18.
|
||||
*/
|
||||
|
||||
class FakeTuskyApplication : TuskyApplication() {
|
||||
|
||||
lateinit var locator: ServiceLocator
|
||||
|
||||
override fun initPicasso() {
|
||||
// No-op
|
||||
}
|
||||
|
||||
override fun getServiceLocator(): ServiceLocator {
|
||||
return locator
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue