-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b83b12
commit a8e87a9
Showing
9 changed files
with
353 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
kotlin/android/app/src/androidTest/java/dev/firezone/android/core/HiltTestRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* Licensed under Apache 2.0 (C) 2024 Firezone, Inc. */ | ||
package dev.firezone.android.core | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.test.runner.AndroidJUnitRunner | ||
import dagger.hilt.android.testing.HiltTestApplication | ||
|
||
class HiltTestRunner : AndroidJUnitRunner() { | ||
override fun newApplication( | ||
cl: ClassLoader?, | ||
className: String?, | ||
context: Context?, | ||
): Application { | ||
return super.newApplication(cl, HiltTestApplication::class.java.name, context) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
kotlin/android/app/src/androidTest/java/dev/firezone/android/core/ViewActions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* Licensed under Apache 2.0 (C) 2024 Firezone, Inc. */ | ||
package dev.firezone.android.core | ||
|
||
import android.view.View | ||
import androidx.test.espresso.PerformException | ||
import androidx.test.espresso.UiController | ||
import androidx.test.espresso.ViewAction | ||
import androidx.test.espresso.matcher.ViewMatchers.isRoot | ||
import androidx.test.espresso.util.HumanReadables | ||
import androidx.test.espresso.util.TreeIterables | ||
import org.hamcrest.Matcher | ||
import org.hamcrest.StringDescription | ||
import java.util.concurrent.TimeoutException | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
fun waitForView( | ||
vararg matchers: Matcher<View>, | ||
timeout: Duration = 5.seconds, | ||
): ViewAction { | ||
return object : ViewAction { | ||
private val timeoutMillis = timeout.inWholeMilliseconds | ||
|
||
override fun getConstraints() = isRoot() | ||
|
||
override fun getDescription(): String { | ||
val subDescription = StringDescription() | ||
matchers.forEach { it.describeTo(subDescription) } | ||
return "Wait for a view matching one of: $subDescription; with a timeout of $timeout." | ||
} | ||
|
||
override fun perform( | ||
uiController: UiController, | ||
rootView: View, | ||
) { | ||
uiController.loopMainThreadUntilIdle() | ||
val startTime = System.currentTimeMillis() | ||
val endTime = startTime + timeoutMillis | ||
|
||
do { | ||
for (child in TreeIterables.breadthFirstViewTraversal(rootView)) { | ||
if (matchers.any { matcher -> matcher.matches(child) }) { | ||
return | ||
} | ||
} | ||
uiController.loopMainThreadForAtLeast(100) | ||
} while (System.currentTimeMillis() < endTime) | ||
|
||
throw PerformException.Builder() | ||
.withCause(TimeoutException()) | ||
.withActionDescription(this.description) | ||
.withViewDescription(HumanReadables.describe(rootView)) | ||
.build() | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
kotlin/android/app/src/androidTest/java/dev/firezone/android/core/di/TestRuntimeModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* Licensed under Apache 2.0 (C) 2024 Firezone, Inc. */ | ||
package dev.firezone.android.core.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.components.SingletonComponent | ||
import dagger.hilt.testing.TestInstallIn | ||
import dev.firezone.android.core.ApplicationMode | ||
|
||
@Module | ||
@TestInstallIn( | ||
components = [SingletonComponent::class], | ||
replaces = [RuntimeModule::class], | ||
) | ||
object TestRuntimeModule { | ||
@Provides | ||
internal fun provideApplicationMode() = ApplicationMode.TESTING | ||
} |
Oops, something went wrong.