Skip to content

Commit

Permalink
Using runBlockingWithTestTimeout EVERYWHERE
Browse files Browse the repository at this point in the history
  • Loading branch information
FelberMartin committed Nov 23, 2024
1 parent 741b8c4 commit 924e48c
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 343 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
package de.tum.informatics.www1.artemis.native_app.feature.courseregistration

import android.content.Context
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.hasParent
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToKey
import androidx.test.platform.app.InstrumentationRegistry
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.common.test.EndToEndTest
import de.tum.informatics.www1.artemis.native_app.core.model.Course
import de.tum.informatics.www1.artemis.native_app.core.test.BaseComposeTest
import de.tum.informatics.www1.artemis.native_app.core.test.coreTestModules
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createCourse
import de.tum.informatics.www1.artemis.native_app.feature.login.loginModule
import de.tum.informatics.www1.artemis.native_app.feature.login.test.getAdminAccessToken
import de.tum.informatics.www1.artemis.native_app.feature.login.test.performTestLogin
import de.tum.informatics.www1.artemis.native_app.feature.login.test.testLoginModule
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.withTimeout
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.experimental.categories.Category
import org.junit.runner.RunWith
import org.koin.android.ext.koin.androidContext
import org.koin.test.KoinTest
import org.koin.test.KoinTestRule
import org.koin.test.get
import org.robolectric.RobolectricTestRunner
import kotlin.test.assertEquals

@Category(EndToEndTest::class)
@RunWith(RobolectricTestRunner::class)
class CourseRegistrationE2eTest : KoinTest {

private val context: Context get() = InstrumentationRegistry.getInstrumentation().context

@get:Rule
val composeTestRule = createComposeRule()
class CourseRegistrationE2eTest : BaseComposeTest() {

@get:Rule
val koinTestRule = KoinTestRule.create {
Expand All @@ -58,11 +49,9 @@ class CourseRegistrationE2eTest : KoinTest {

@Before
fun setup() {
runBlocking {
withTimeout(DefaultTimeoutMillis) {
performTestLogin()
course = createCourse(getAdminAccessToken(), forceSelfRegistration = true)
}
runBlockingWithTestTimeout {
performTestLogin()
course = createCourse(getAdminAccessToken(), forceSelfRegistration = true)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package de.tum.informatics.www1.artemis.native_app.feature.courseview

import android.content.Context
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.platform.app.InstrumentationRegistry
import de.tum.informatics.www1.artemis.native_app.core.model.Course
import de.tum.informatics.www1.artemis.native_app.core.test.BaseComposeTest
import de.tum.informatics.www1.artemis.native_app.core.test.coreTestModules
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createCourse
import de.tum.informatics.www1.artemis.native_app.core.test.testWebsocketModule
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createCourse
import de.tum.informatics.www1.artemis.native_app.feature.courseview.ui.CourseViewModel
import de.tum.informatics.www1.artemis.native_app.feature.courseview.ui.course_overview.CourseUiScreen
import de.tum.informatics.www1.artemis.native_app.feature.courseview.ui.course_overview.DEFAULT_CONVERSATION_ID
Expand All @@ -19,26 +17,17 @@ import de.tum.informatics.www1.artemis.native_app.feature.login.loginModule
import de.tum.informatics.www1.artemis.native_app.feature.login.test.getAdminAccessToken
import de.tum.informatics.www1.artemis.native_app.feature.login.test.performTestLogin
import de.tum.informatics.www1.artemis.native_app.feature.login.test.testLoginModule
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.withTimeout
import org.junit.Before
import org.junit.Rule
import org.koin.android.ext.koin.androidContext
import org.koin.compose.LocalKoinApplication
import org.koin.compose.LocalKoinScope
import org.koin.core.annotation.KoinInternalApi
import org.koin.mp.KoinPlatformTools
import org.koin.test.KoinTest
import org.koin.test.KoinTestRule
import org.koin.test.get

abstract class BaseCourseTest : KoinTest {

protected val testDispatcher = UnconfinedTestDispatcher()

@get:Rule
val composeTestRule = createComposeRule()
abstract class BaseCourseTest : BaseComposeTest() {

@get:Rule
val koinTestRule = KoinTestRule.create {
Expand All @@ -50,16 +39,11 @@ abstract class BaseCourseTest : KoinTest {

lateinit var course: Course

val context: Context get() = InstrumentationRegistry.getInstrumentation().context

@Before
fun setup() {
runBlocking {
withTimeout(DefaultTimeoutMillis) {
performTestLogin()

course = createCourse(getAdminAccessToken())
}
runBlockingWithTestTimeout {
performTestLogin()
course = createCourse(getAdminAccessToken())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performScrollToKey
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.common.test.EndToEndTest
import de.tum.informatics.www1.artemis.native_app.core.model.exercise.Exercise
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createExercise
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createModelingExercise
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createProgramingExercise
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createTextExercise
import de.tum.informatics.www1.artemis.native_app.feature.courseview.ui.exercise_list.TEST_TAG_EXERCISE_LIST_LAZY_COLUMN
import de.tum.informatics.www1.artemis.native_app.feature.login.test.getAdminAccessToken
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.junit.Ignore
import org.junit.Test
import org.junit.experimental.categories.Category
Expand Down Expand Up @@ -80,10 +78,8 @@ class ExerciseListOverviewE2eTest : BaseCourseTest() {


private fun displayExerciseTypeTestImpl(createExercise: suspend () -> Exercise) {
val exercise = runBlocking {
withTimeout(DefaultTimeoutMillis) {
createExercise()
}
val exercise = runBlockingWithTestTimeout {
createExercise()
}

setupAndDisplayCourseUi()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
package de.tum.informatics.www1.artemis.native_app.feature.exercise_view.participate.text_exercise

import android.content.Context
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.platform.app.InstrumentationRegistry
import de.tum.informatics.www1.artemis.native_app.core.model.Course
import de.tum.informatics.www1.artemis.native_app.core.model.exercise.TextExercise
import de.tum.informatics.www1.artemis.native_app.core.test.BaseComposeTest
import de.tum.informatics.www1.artemis.native_app.core.test.coreTestModules
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.testWebsocketModule
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createCourse
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createExercise
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createTextExercise
import de.tum.informatics.www1.artemis.native_app.core.test.testWebsocketModule
import de.tum.informatics.www1.artemis.native_app.feature.exerciseview.exerciseModule
import de.tum.informatics.www1.artemis.native_app.feature.login.loginModule
import de.tum.informatics.www1.artemis.native_app.feature.login.test.getAdminAccessToken
import de.tum.informatics.www1.artemis.native_app.feature.login.test.performTestLogin
import de.tum.informatics.www1.artemis.native_app.feature.login.test.testLoginModule
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.withTimeout
import org.junit.Before
import org.junit.Rule
import org.koin.android.ext.koin.androidContext
import org.koin.test.KoinTest
import org.koin.test.KoinTestRule
import org.robolectric.shadows.ShadowLog

abstract class BaseExerciseTest : KoinTest {

protected val testDispatcher = UnconfinedTestDispatcher()

protected val context: Context get() = InstrumentationRegistry.getInstrumentation().context

@get:Rule
val composeTestRole = createComposeRule()
abstract class BaseExerciseTest : BaseComposeTest() {

@get:Rule
val koinTestRule = KoinTestRule.create {
Expand All @@ -52,18 +39,16 @@ abstract class BaseExerciseTest : KoinTest {
open fun setup() {
ShadowLog.stream = System.out

runBlocking {
withTimeout(DefaultTimeoutMillis) {
accessToken = performTestLogin()
runBlockingWithTestTimeout {
accessToken = performTestLogin()

course = createCourse(getAdminAccessToken())
exercise = createExercise(
getAdminAccessToken(),
course.id!!,
endpoint = "text-exercises",
creator = ::createTextExercise
) as TextExercise
}
course = createCourse(getAdminAccessToken())
exercise = createExercise(
getAdminAccessToken(),
course.id!!,
endpoint = "text-exercises",
creator = ::createTextExercise
) as TextExercise
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import androidx.compose.ui.test.onFirst
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.navigation.compose.rememberNavController
import de.tum.informatics.www1.artemis.native_app.core.common.test.EndToEndTest
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.common.test.EndToEndTest
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.feature.exerciseview.ExerciseViewModel
import de.tum.informatics.www1.artemis.native_app.feature.exerciseview.home.ExerciseScreen
Expand All @@ -34,7 +34,7 @@ class ExerciseOverviewE2eTest : BaseExerciseTest() {
fun `displays correct exercise data`() {
setupUiAndViewModel()

composeTestRole.onAllNodesWithText(exercise.title!!).onFirst().assertExists()
composeTestRule.onAllNodesWithText(exercise.title!!).onFirst().assertExists()
}

@OptIn(ExperimentalTestApi::class)
Expand All @@ -44,14 +44,14 @@ class ExerciseOverviewE2eTest : BaseExerciseTest() {

setupUiAndViewModel { participationId = it }

composeTestRole.onNodeWithText(
composeTestRule.onNodeWithText(
context.getString(CoreUiR.string.exercise_actions_start_exercise_button)
)
.performClick()

composeTestRole.waitUntil(DefaultTimeoutMillis) { participationId != null }
composeTestRule.waitUntil(DefaultTimeoutMillis) { participationId != null }

composeTestRole
composeTestRule
.waitUntilExactlyOneExists(
hasText(context.getString(CoreUiR.string.exercise_actions_open_exercise_button)),
DefaultTimeoutMillis
Expand All @@ -73,7 +73,7 @@ class ExerciseOverviewE2eTest : BaseExerciseTest() {
coroutineContext = testDispatcher
)

composeTestRole.setContent {
composeTestRule.setContent {
CompositionLocalProvider(
LocalKoinScope provides KoinPlatformTools.defaultContext()
.get().scopeRegistry.rootScope,
Expand Down
Loading

0 comments on commit 924e48c

Please sign in to comment.