Basic setup for testing controllers in isolation, with example

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-06-29 13:29:27 +02:00 committed by Andy Scherzinger
parent d781c79930
commit 812d720611
6 changed files with 279 additions and 2 deletions

View file

@ -139,6 +139,7 @@ android {
ext { ext {
androidxCameraVersion = "1.2.3" androidxCameraVersion = "1.2.3"
androidXTestVersion = "1.5.0"
coilKtVersion = "2.4.0" coilKtVersion = "2.4.0"
daggerVersion = "2.48.1" daggerVersion = "2.48.1"
emojiVersion = "1.4.0" emojiVersion = "1.4.0"
@ -289,7 +290,11 @@ dependencies {
androidTestImplementation 'org.mockito:mockito-android:5.6.0' androidTestImplementation 'org.mockito:mockito-android:5.6.0'
testImplementation 'androidx.arch.core:core-testing:2.2.0' testImplementation 'androidx.arch.core:core-testing:2.2.0'
androidTestImplementation "androidx.test:core:1.5.0" androidTestImplementation "androidx.test:core:$androidXTestVersion"
androidTestImplementation "androidx.test:core-ktx:$androidXTestVersion"
androidTestImplementation "androidx.test:runner:$androidXTestVersion"
androidTestImplementation "androidx.test:rules:$androidXTestVersion"
androidTestImplementation "androidx.test.ext:junit:1.1.3"
// Espresso core // Espresso core
androidTestImplementation ("androidx.test.espresso:espresso-core:$espressoVersion", { androidTestImplementation ("androidx.test.espresso:espresso-core:$espressoVersion", {
@ -298,7 +303,7 @@ dependencies {
androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion" androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion" androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion" androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"
androidTestImplementation('com.android.support.test.espresso:espresso-intents:3.0.2') androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0' spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0'
spotbugsPlugins 'com.mebigfatguy.fb-contrib:fb-contrib:7.6.0' spotbugsPlugins 'com.mebigfatguy.fb-contrib:fb-contrib:7.6.0'

View file

@ -0,0 +1,62 @@
/*
* Nextcloud Talk application
*
* @author Álvaro Brey
* Copyright (C) 2022 Álvaro Brey
* Copyright (C) 2022 Nextcloud GmbH
*
* 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.controllers
import androidx.test.core.app.launchActivity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.nextcloud.talk.R
import com.nextcloud.talk.test.BaseIT
import com.nextcloud.talk.test.ControllerTestActivity
import org.hamcrest.Matchers.not
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ProfileControllerIT : BaseIT() {
@Test
@Suppress("Detekt.MagicNumber")
fun testClickEdit() {
launchActivity<ControllerTestActivity>().use { scenario ->
val controller = ProfileController()
scenario.onActivity { activity ->
activity.setController(controller)
}
Thread.sleep(2000) // TODO find a workaround for waiting until controller is set
// editing options not visible on launch
onView(withId(R.id.avatar_buttons)).check(matches(not(isDisplayed())))
onView(withId(R.id.edit)).perform(click())
// editing options now visible
onView(withId(R.id.avatar_buttons)).check(matches(isDisplayed()))
}
}
}

View file

@ -0,0 +1,80 @@
/*
* Nextcloud Talk application
*
* @author Álvaro Brey
* Copyright (C) 2022 Álvaro Brey
* Copyright (C) 2022 Nextcloud GmbH
*
* 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.test
import android.annotation.SuppressLint
import androidx.test.platform.app.InstrumentationRegistry
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.data.source.local.TalkDatabase
import com.nextcloud.talk.data.user.UsersRepositoryImpl
import com.nextcloud.talk.users.UserManager
import org.junit.Before
/**
* Base class for integration tests that need to ensure database consistency
*/
abstract class BaseIT {
private lateinit var database: TalkDatabase
private lateinit var userManager: UserManager
@SuppressLint("CheckResult")
@Before
fun setUp() {
initFields()
database.clearAllTables()
createTestUser()
}
fun initFields() {
// these should be injected but AutoDagger does not work here
database = TalkDatabase.getInstance(NextcloudTalkApplication.sharedApplication!!)
userManager = UserManager(UsersRepositoryImpl(database.usersDao()))
}
@SuppressLint("CheckResult")
private fun createTestUser() {
val arguments = InstrumentationRegistry.getArguments()
val baseUrl = arguments.getString(ARG_SERVER_URL)
val loginName = arguments.getString(ARG_SERVER_USERNAME)
val password = arguments.getString(ARG_SERVER_PASSWORD)
userManager.createOrUpdateUser(
loginName,
UserManager.UserAttributes(
id = 1, serverUrl = baseUrl,
currentUser = true,
userId = loginName, token = password, displayName = loginName, pushConfigurationState = null,
capabilities = null, certificateAlias = null, externalSignalingServer = null
)
).blockingGet()
}
companion object {
private const val ARG_SERVER_URL = "TEST_SERVER_URL"
private const val ARG_SERVER_USERNAME = "TEST_SERVER_USERNAME"
private const val ARG_SERVER_PASSWORD = "TEST_SERVER_USERNAME"
}
}

View file

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Nextcloud Talk application
~
~ @author Álvaro Brey
~ Copyright (C) 2022 Álvaro Brey
~ Copyright (C) 2022 Nextcloud GmbH
~
~ 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.
~
~ This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application tools:ignore="MissingApplicationIcon">
<activity
android:name=".test.ControllerTestActivity"
android:theme="@style/AppTheme"
android:exported="false" />
</application>
</manifest>

View file

@ -0,0 +1,48 @@
/*
* Nextcloud Talk application
*
* @author Álvaro Brey
* Copyright (C) 2022 Álvaro Brey
* Copyright (C) 2022 Nextcloud GmbH
*
* 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.test
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.bluelinelabs.conductor.Conductor
import com.bluelinelabs.conductor.Controller
import com.bluelinelabs.conductor.Router
import com.bluelinelabs.conductor.RouterTransaction
import com.nextcloud.talk.databinding.ActivityControllerTestBinding
class ControllerTestActivity : AppCompatActivity() {
lateinit var binding: ActivityControllerTestBinding
lateinit var router: Router
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityControllerTestBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
router = Conductor.attachRouter(this, binding.controllerContainer, savedInstanceState)
}
fun setController(controller: Controller) {
router.setRoot(RouterTransaction.with(controller))
}
}

View file

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Nextcloud Talk application
~
~ @author Álvaro Brey
~ Copyright (C) 2022 Álvaro Brey
~ Copyright (C) 2022 Nextcloud GmbH
~
~ 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.
~
~ This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
-->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:theme="@style/AppTheme"
tools:context=".test.ControllerTestActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<com.bluelinelabs.conductor.ChangeHandlerFrameLayout
android:id="@+id/controller_container"
android:layout_height="0dp"
android:layout_width="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.constraintlayout.widget.ConstraintLayout>