Closed sarimmehdi closed 1 year ago
Ok, so I solved the issue by replacing my HiltTestActivity with this one (courtesy of this):
@SuppressLint("CustomSplashScreen")
@AndroidEntryPoint
class SplashScreenActivityTest: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTheme(
intent.getIntExtra(
"androidx.fragment.app.testing.FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY",
androidx.fragment.testing.R.style.FragmentScenarioEmptyFragmentActivityTheme
)
)
}
}
Then, inside launchFragmentInHiltContainer, I provided my app's custom theme which inherits from Theme.AppCompat:
@ExperimentalCoroutinesApi
inline fun <reified T : Fragment> launchFragmentInHiltContainer(
fragmentArgs: Bundle? = null,
themeResId: Int = R.style.myCustomeTheme,
fragmentFactory: FragmentFactory? = null,
crossinline action: T.() -> Unit = {}
) {
val mainActivityIntent = Intent.makeMainActivity(
ComponentName(
ApplicationProvider.getApplicationContext(),
SplashScreenActivityTest::class.java
)
).putExtra(
"androidx.fragment.app.testing.FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY",
themeResId
)
ActivityScenario.launch<SplashScreenActivityTest>(mainActivityIntent).onActivity { activity ->
fragmentFactory?.let {
activity.supportFragmentManager.fragmentFactory = it
}
val fragment = activity.supportFragmentManager.fragmentFactory.instantiate(
Preconditions.checkNotNull(T::class.java.classLoader),
T::class.java.name
)
fragment.arguments = fragmentArgs
activity.supportFragmentManager.beginTransaction()
.add(android.R.id.content, fragment, "")
.commitNow()
(fragment as T).action()
}
}
I am following the code from here:
Then the AndroidManifest.xml for the debug folder:
This is what my instrumentation test looks like:
When I run my Instrumentation Test, I get the following error:
I am able to get rid of the error by applying a theme inside the AndroidManifest.xml file of debug:
But then the prod version of my app (which also uses a custom theme derived from Theme.AppCompat inside the manifest for the app module), fails to run and complains with the following error (:feature_splash:splash_presentation is the module where I created the debug folder with the test activity similar to what is shown here):
Is there a way to make the instrumentation test run without adding that extra theme in the manifest for debug? This is what my launchFragmentInHiltContainer looks like btw: