A common situation I face is that I wish to test a custom View
visibly on screen while it is being developed as part of a large application. I realise that unit testing exists, particularly using ActivityUnitTestCase
, but as far as I understand these frameworks don't actually attach the components visibly on screen.
At present, the way I tend to do this is to just place the component wherever it will actually be used within the application. This often means needing to wait some time for the application to start or to navigate through parts of the application before it is visible, which can become cumbersome.
The better way I've found is to create an Activity
within the application that exists purely for test purposes (e.g. to simply display a custom View
I'm developing), which is launched to display the custom View
(or whatever) that I am working on. To do this, the only intent filter I've assigned to that Activity
in the manifest is android.intent.action.MAIN
, and not android.intent.category.LAUNCHER
. Then, I can simply create a Run/Debug Configuration in Android Studio to launch that Activity
directly. This, as far as I believe, effectively allows me to have Activity
classes which may only be launched by me from the IDE.
My questions are:
Does omitting
android.intent.category.LAUNCHER
guarantee that users of the application won't be able to launch thatActivity
by any means nor be aware of its existence? Is this a safe to haveActivity
classes for development only?Is there a workflow or test framework of any kind I could use to improve on how I am doing this?
Well,
ActivityInstrumentationTestCase2
does, as does the newActivityTestRule
. I don't recall playing withActivityUnitTestCase
.No. It won't be easy for them to start it, but it is exported and has an
<intent-filter>
, so somebody could find a way.I would put them in your
debug
sourceset, assuming that you are using Android Studio. Here is a sample project that has a production launcher activity inmain
and a diagnostic activity indebug
. In arelease
build, the diagnostic activity does not even ship.