Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some tests run on roblectric #188

Merged
merged 18 commits into from
Apr 17, 2024
Merged

Make some tests run on roblectric #188

merged 18 commits into from
Apr 17, 2024

Conversation

caarmen
Copy link
Owner

@caarmen caarmen commented Apr 13, 2024

Project configuration:

  • Modifications to jacoco dependencies:
    • Don't force enabling test code coverage if testDebugUnitTest or connectedDebugAndroidTest are gradle tasks on the command line.
    • Don't make the jacoco task depend on these other two tasks, instead make it run after the other tasks. This allows us to run jvm tests and get code coverage, without having to run device tests too.
  • Add necessary dependencies to the test source set.
  • Put common test code (utilities and tests) into a sharedTest folder, which is included in the test and androidTest source sets.

Application code modifications:

  • When using the search feature from Robolectric, the MainActivity's onCreate() is called instead of onNewIntent(). Adapt onCreate() to handle the SEARCH action.

Test code modifications:

  • Make some test utility functions public, which were package scoped. This allows us to put the shared tests into a different package, while still accessing the utility code. Putting the tests into a shared package allows us to more easily run only the shared tests, which is useful for comparing test execution between on-device and Robolectric tests:
    • On-device:
      ./gradlew :app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.package="ca.rmen.android.poetassistant.shared.main"     
      
    • Robolectric:
      ./gradlew :app:testDebugUnitTest --tests "ca.rmen.android.poetassistant.shared.main.*"
      

Adaptations required to make tests run on Robolectric:

  • Threading:
    • Introduce InstantTaskExecutorRule which allows architecture components (like LiveData) to have their "background" tasks executed synchronously under the hood.
    • Set the main coroutine dispatcher to be the UnconfinedTestDispatcher.
  • New shadows:
    • Add multi-activity support:
      • Actually start the next activity:
        • MyShadowInstrumentation: extend ShadowInstrumentation to actually launch an activity, in execStartActivity().
      • Manage activity life cycle changes when starting another activity:
        • MyShadowActivity:
          • Make startActivity[ForResult] put this activity on pause after starting the next activity.
          • Make finish() put the previous activity (if any) in resume, after finishing this activity.
      • Cleanup:
        • MyShadowActivity: Be sure to clean up (close) the ActivityScenario of started activities, when they finish.
    • MyShadowSearchManager:
      • Make getSearchableInfo() actually return a SearcheableInfo (requires reflection). This was a no-op in Robolectric, which made the search functionality in the app not work from Robolectric.

Other:

  • Bump the api level used by default for Robolectric tests from 28 to 34. Make an exception for TestPoemFile, which has an issue saving the poem text starting with api level 31.
  • Don't use orchestrator by default
  • Only use the RetryTestRule on tests which seem flaky.
  • Add some scripts to run tests on Firebase Test Lab devices;

@caarmen caarmen marked this pull request as draft April 13, 2024 22:43
@caarmen caarmen force-pushed the omg-what-happened branch 15 times, most recently from e24d6e1 to b3bd3d6 Compare April 15, 2024 20:22
caarmen added 13 commits April 16, 2024 21:42
Make an exception for one test in `TestPoemFile`, which breaks starting with api level 31.
This allows us to get a test coverage report without having to run both robolectric and instrumentation tests.
Some functions in these test files aren't yet ready to be run on robolectric. Only move the test functions that can run on robolectric for now.
…est nullable.

When we add the equivalent class in the test source set, it will be a no-op (it will return null).
* Add multi-activity support:
  - Actually start the next activity:
    - `MyShadowInstrumentation`: extend `ShadowInstrumentation` to actually launch an activity, in `execStartActivity()`.
  - Manage activity life cycle changes when starting another activity:
    - `MyShadowActivity`:
      - Make `startActivity[ForResult]` put *this* activity on pause after starting the *next* activity.
      - Make `finish()` put the *previous* activity (if any) in resume, after finishing *this* activity.
  - Cleanup:
    - `MyShadowActivity`: Be sure to clean up (close) the `ActivityScenario` of started activities, when they finish.
* `MyShadowSearchManager`:
  - Make `getSearchableInfo()` actually return a `SearcheableInfo` (requires reflection). This was a no-op in Robolectric, which made the search functionality in the app not work from Robolectric.
* Add the sharedTest folder to the `test` source set.
* Add necessary dependencies to the `test` source set.
* Add basic implementations of some classes which are also in the androidTest source set:
  - ca/rmen/android/poetassistant/InstrumentationThreading.kt
  - ca/rmen/android/poetassistant/dagger/TestResultListModule.kt
* Allow db queries on the main thread, only for robolectric tests
@caarmen caarmen force-pushed the omg-what-happened branch from b3bd3d6 to 3660de1 Compare April 16, 2024 19:43
@caarmen caarmen force-pushed the omg-what-happened branch from 24a8650 to 42da864 Compare April 17, 2024 17:25
@caarmen caarmen changed the title [WIP 🚧 ] Make some tests run on roblectric Make some tests run on roblectric Apr 17, 2024
@caarmen caarmen marked this pull request as ready for review April 17, 2024 17:27
@caarmen caarmen force-pushed the omg-what-happened branch from d00ec33 to 38e5dd5 Compare April 17, 2024 18:46
This is just so we remember that this option exists.

By default we're not using orchestrator anyway.

If we need to re-enable orchestrator, we can see then whether to keep or remove `clearPackageData`.
@caarmen caarmen merged commit c45b91a into master Apr 17, 2024
@caarmen caarmen deleted the omg-what-happened branch April 17, 2024 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant