lundi 2 février 2015

How to Unit Test if a fragment has loaded correctly

I am really new to the concept of Unit Testing in android and I am trying to test some functionality of my app. Basically what I want is to test if a fragment has loaded correctly. In my case in the activity where the fragments are crated I have 3 buttons. Each button calls a different fragment. So I want to know how to unit test this. I think if I perform on click test on each button but I don't know how to test if the fragment has loaded correctly or not.


This is my test code so far:



public class TestHomeFragments extends ActivityInstrumentationTestCase2<Home>
{
private Home activity;
private Zone zone;

//Public Constructor
public TestHomeFragments() {
super(Home.class);
}

public void setUp() throws Exception
{
super.setUp();
setActivityInitialTouchMode(false);
activity = getActivity();
}

private Fragment startFragment(Fragment fragment) {
FragmentTransaction transaction = zone.getFragmentManager().beginTransaction();
transaction.add(R.id.recyclerViewZone, fragment, "tag");
transaction.commit();
getInstrumentation().waitForIdleSync();
Fragment frag = zone.getFragmentManager().findFragmentByTag("tag");
return frag;
}

public void testFragment() {
Fragment fragment = new Fragment() {
//Override methods and add assertations here.
};

startFragment(fragment);
}
}

Aucun commentaire:

Enregistrer un commentaire