I am attempting to assert that the correct Preferences are being initialized when my Settings app's onCreate()
method is called. I am trying to do this using the robolectric
API.
Say I can reference an ActivityController
- called activityCont
and I also have a reference to the activity itself - called myActivity
, obtained using activityCont.get()
). I also have a reference to a FragmentManager
called fragMgr
. So my setup is:
@Before
public void setup() {
activityCont = Robolectric.buildActivity(mySettings.class);
myActivity = activityCont.get();
activityCont.create();
FragMgr = myActivity.getFragmentManager();
}
First I want to assert that certain fragments are being built. So for that I use:
@Test
public void canBuildParticularFragment() {
activityCont.attach().start().resume();
Assert.assertNotNull(FragMgr.findFragmentByTag((new myFragmentClass()).getTag()))
}
However, this test always fails, even though I can attest to the fact that myFragmentClass IS being built. Am I simply setting the activity to be in the wrong lifecycle stage? At which stage are fragments created so they can be accessible?
My other suspicion is that getTag()
simply gets a tag specific to that instance of the class, which would mean the tag I "get()
" in the above code would not return any fragments since it's different. If this is true, how would I get the correct fragment, since I don't know its tag, and I'd prefer not giving it an id in the xml file that contains it?
Lastly, I want to be able to, once I have a reference to the fragment, to get()
a list of the preferences
being displayed so I can assert
they are correct based on some other configurations. So, how can I get the correct instance of the myFragmentClass
preferenceFragment
, and how can I get a list of its preferences
?