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:
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;
Project configuration:
test
andandroidTest
source sets.Application code modifications:
MainActivity
'sonCreate()
is called instead ofonNewIntent()
. AdaptonCreate()
to handle theSEARCH
action.Test code modifications:
Adaptations required to make tests run on Robolectric:
UnconfinedTestDispatcher
.MyShadowInstrumentation
: extendShadowInstrumentation
to actually launch an activity, inexecStartActivity()
.MyShadowActivity
:startActivity[ForResult]
put this activity on pause after starting the next activity.finish()
put the previous activity (if any) in resume, after finishing this activity.MyShadowActivity
: Be sure to clean up (close) theActivityScenario
of started activities, when they finish.MyShadowSearchManager
:getSearchableInfo()
actually return aSearcheableInfo
(requires reflection). This was a no-op in Robolectric, which made the search functionality in the app not work from Robolectric.Other:
TestPoemFile
, which has an issue saving the poem text starting with api level 31.RetryTestRule
on tests which seem flaky.