-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
) * Introduce test coroutine dispatchers support in Espresso. This piggybacks off of the solution introduced in #1276 for Robolectric. That PR allows Robolectric tests in app module to share dependencies with production components by creating a test application & telling Robolectric to use it instead of OppiaApplication via a @config annotation. This PR achieves the same thing by using a custom test runner that reads the same annotation and forces Espresso & instrumentation to use the test application instead of the default OppiaApplication. This setup will be easier once #59 is finished since we can specify the application in per-test manifests that both Robolectric & Espresso will respect. Note that while this lets the same test coroutine dispatchers work in both production & test code for Espresso, some additional work was needed to ensure the coroutines behave correctly. In particular, the coroutines use a fake system clock in Robolectric that requires explicit synchronization points in the tests to allow the clock to move forward & properly coordinate execution between background & main thread operations. However, in Espresso, since everything is using a real clock an idling resource is the preferred way to synchronize execution: it allows the background coroutines to operate in real-time much like they would in production, and then notify Espresso when completed. The test dispatchers API is now setup to support both synchronization mechanisms for both Robolectric & Espresso (the idling resource does nothing on Robolectric and the force synchronization effectively does nothing on Espresso). The first test being demonstrated as now stable is SplashActivityTest (as part of downstream work in #1397. * Revert "Fixes #941: Add radar effect in Hints and solution (#1475)" This reverts commit 41eb10b. * Stabilize StateFragmentTest such that it passes on both Robolectric and Espresso. Note that some issues were found during this: #1612 (#1611 was found a few weeks ago, but it also affects these tests). To ensure the tests can still be run, a @runon annotation was added to allow tests to target specific test platforms. The tests that currently fail on Robolectric due to #1611 and #1612 are disabled for that platform. The test suite as a whole has been verified to pass in its current state on both Robolectric and Espresso (on a Pixel XL). The aim of this PR is to actually enable critical state fragment tests in CI, so both StateFragmentTest and StateFragmentLocalTest are being enabled in GitHub actions. * Enable StateFragmentTest (Robolectric) & StateFragmentLocalTest for CI. * Add thorough documentation for new dispatchers. * Clean up comments & add additional documentation. * Fix lint errors. * Fix broken test after changes to FakeSystemClock. * Fix linter errors. * Use a custom executor service for Glide requests that coordinates with Oppia's test dispatchers. Note that this does not actually introduce the service--that will happen in a new branch. * Introduce new executor service which allows interop with Kotlin coroutines, plus a test to verify that it fundamentally follows one interpretation of ExecutorService's API. * Fix flaky timeout tests by improving cancellation cooperation for invokeAny() and provide longer timeouts for tests that are CPU-sensitive. * Add documentation & clean up unused code. * Lint fixes. * Significantly reorganize invokeAll() to try and make it more cooperative for cancellation, and increase timeout times in tests to reduce flakiness for time-sensitive tests. Some tests are remaining flaky, so ignoring those. Re-add maybeWithTimeoutOrNull since it actually was needed. * Lint fixes. * Post-merge module fixes. * Post-merge fixes with ratio input & add a TODO to improve speed of the new coroutine executor service. * Revert "Fixes part of #40 & #42: Generalisation Highfi Mobile Portrait + Landscape - Buttons (#1653)" This reverts commit 1bb1ffa. * Ensure terminated tasks do not interfere with one another (timeouts should happen individually for each task during termination). This fixes a failure observed in StateFragmentLocalTest in #1630. * Ignore failing tests until #1769 is resolved. * Fix awaitTermination & improve test. Improve stack trace for test dispatcher timeouts. * Fix slow & broken tests in Robolectric for StateFragmentLocalTest. * Add missing deps for StateFragmentLocalTest. * Address reviewer comments.
- Loading branch information
1 parent
000e91e
commit 9f6db93
Showing
9 changed files
with
704 additions
and
485 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
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
1,045 changes: 576 additions & 469 deletions
1,045
app/src/sharedTest/java/org/oppia/app/player/state/StateFragmentTest.kt
Large diffs are not rendered by default.
Oops, something went wrong.
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
28 changes: 28 additions & 0 deletions
28
testing/src/main/java/org/oppia/testing/OppiaTestAnnotations.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,28 @@ | ||
package org.oppia.testing | ||
|
||
import org.oppia.testing.TestPlatform.ESPRESSO | ||
import org.oppia.testing.TestPlatform.ROBOLECTRIC | ||
|
||
/** Specifies a test platform to target in conjunction with [RunOn]. */ | ||
enum class TestPlatform { | ||
/** Corresponds to local tests run in the Java VM via Robolectric. */ | ||
ROBOLECTRIC, | ||
|
||
/** Corresponds to instrumented tests that can run on a real device or emulator via Espresso. */ | ||
ESPRESSO | ||
} | ||
|
||
/** | ||
* Test class or method annotation for specifying all of platforms which either the tests of the | ||
* class or the specific method may run on. By default, tests are assumed to be able to run on both | ||
* Espresso & Robolectric. | ||
* | ||
* The target platforms are specified as varargs of [TestPlatform]s. | ||
* | ||
* Note that this annotation only works if the test also has an [OppiaTestRule] hooked up. | ||
* | ||
* Note that when defined on both a class and a method, the list of platforms defined on the method | ||
* is used and any defined at the class level are ignored. | ||
*/ | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) | ||
annotation class RunOn(vararg val testPlatforms: TestPlatform = [ROBOLECTRIC, ESPRESSO]) |
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 @@ | ||
package org.oppia.testing | ||
|
||
import android.os.Build | ||
import org.junit.AssumptionViolatedException | ||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
/** JUnit rule to enable [RunOn] test targeting. */ | ||
class OppiaTestRule : TestRule { | ||
override fun apply(base: Statement?, description: Description?): Statement { | ||
return object : Statement() { | ||
override fun evaluate() { | ||
val targetPlatforms = description.getTargetPlatforms() | ||
val currentPlatform = getCurrentPlatform() | ||
if (currentPlatform in targetPlatforms) { | ||
// Only run this test if it's targeting the current platform. | ||
base?.evaluate() | ||
} else { | ||
// See https://github.com/junit-team/junit4/issues/116 for context. | ||
throw AssumptionViolatedException( | ||
"Test targeting ${targetPlatforms.toPluralDescription()} ignored on $currentPlatform" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun getCurrentPlatform(): TestPlatform { | ||
return if (Build.FINGERPRINT.contains("robolectric", ignoreCase = true)) { | ||
TestPlatform.ROBOLECTRIC | ||
} else { | ||
TestPlatform.ESPRESSO | ||
} | ||
} | ||
|
||
private companion object { | ||
private fun Array<out TestPlatform>.toPluralDescription(): String { | ||
return if (size > 1) "platforms ${this.joinToString()}" else "platform ${this.first()}" | ||
} | ||
|
||
private fun Description?.getTargetPlatforms(): Array<out TestPlatform> { | ||
val methodTargetPlatforms = this?.getTargetTestPlatforms() | ||
val classTargetPlatforms = this?.testClass?.getTargetTestPlatforms() | ||
return methodTargetPlatforms ?: classTargetPlatforms ?: TestPlatform.values() | ||
} | ||
|
||
private fun Description.getTargetTestPlatforms(): Array<out TestPlatform>? { | ||
return getAnnotation(RunOn::class.java)?.testPlatforms | ||
} | ||
|
||
private fun <T> Class<T>.getTargetTestPlatforms(): Array<out TestPlatform>? { | ||
return getAnnotation(RunOn::class.java)?.testPlatforms | ||
} | ||
} | ||
} |
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