From 812d7206110da6c170edc77c6e0dd42414dd0c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Wed, 29 Jun 2022 13:29:27 +0200 Subject: [PATCH] Basic setup for testing controllers in isolation, with example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Brey --- app/build.gradle | 9 ++- .../talk/controllers/ProfileControllerIT.kt | 62 ++++++++++++++ .../java/com/nextcloud/talk/test/BaseIT.kt | 80 +++++++++++++++++++ app/src/debug/AndroidManifest.xml | 32 ++++++++ .../talk/test/ControllerTestActivity.kt | 48 +++++++++++ .../res/layout/activity_controller_test.xml | 50 ++++++++++++ 6 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 app/src/androidTest/java/com/nextcloud/talk/controllers/ProfileControllerIT.kt create mode 100644 app/src/androidTest/java/com/nextcloud/talk/test/BaseIT.kt create mode 100644 app/src/debug/AndroidManifest.xml create mode 100644 app/src/debug/java/com/nextcloud/talk/test/ControllerTestActivity.kt create mode 100644 app/src/debug/res/layout/activity_controller_test.xml diff --git a/app/build.gradle b/app/build.gradle index cdc3ee0ec..2eb56aaef 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -139,6 +139,7 @@ android { ext { androidxCameraVersion = "1.2.3" + androidXTestVersion = "1.5.0" coilKtVersion = "2.4.0" daggerVersion = "2.48.1" emojiVersion = "1.4.0" @@ -289,7 +290,11 @@ dependencies { androidTestImplementation 'org.mockito:mockito-android:5.6.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 androidTestImplementation ("androidx.test.espresso:espresso-core:$espressoVersion", { @@ -298,7 +303,7 @@ dependencies { androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion" androidTestImplementation "androidx.test.espresso:espresso-web:$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.mebigfatguy.fb-contrib:fb-contrib:7.6.0' diff --git a/app/src/androidTest/java/com/nextcloud/talk/controllers/ProfileControllerIT.kt b/app/src/androidTest/java/com/nextcloud/talk/controllers/ProfileControllerIT.kt new file mode 100644 index 000000000..ccc32921d --- /dev/null +++ b/app/src/androidTest/java/com/nextcloud/talk/controllers/ProfileControllerIT.kt @@ -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 . + */ + +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().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())) + } + } +} diff --git a/app/src/androidTest/java/com/nextcloud/talk/test/BaseIT.kt b/app/src/androidTest/java/com/nextcloud/talk/test/BaseIT.kt new file mode 100644 index 000000000..bd412d4dc --- /dev/null +++ b/app/src/androidTest/java/com/nextcloud/talk/test/BaseIT.kt @@ -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 . + */ + +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" + } +} diff --git a/app/src/debug/AndroidManifest.xml b/app/src/debug/AndroidManifest.xml new file mode 100644 index 000000000..5ab30479d --- /dev/null +++ b/app/src/debug/AndroidManifest.xml @@ -0,0 +1,32 @@ + + + + + + + + + diff --git a/app/src/debug/java/com/nextcloud/talk/test/ControllerTestActivity.kt b/app/src/debug/java/com/nextcloud/talk/test/ControllerTestActivity.kt new file mode 100644 index 000000000..7c1341813 --- /dev/null +++ b/app/src/debug/java/com/nextcloud/talk/test/ControllerTestActivity.kt @@ -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 . + */ + +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)) + } +} diff --git a/app/src/debug/res/layout/activity_controller_test.xml b/app/src/debug/res/layout/activity_controller_test.xml new file mode 100644 index 000000000..f3ea8715d --- /dev/null +++ b/app/src/debug/res/layout/activity_controller_test.xml @@ -0,0 +1,50 @@ + + + + + + + + +