procra
procra

Reputation: 555

Android - how to test if the correct fragment is loaded?

I recently started coding my first android project using Android Studio 3.1.2 and SDK 19.

Currently I'm writing the tests for my no-UI objects and want to test, which fragment an activity loads in the oncreate() method. The activity itself inspects the Intent that invoked itself and, depending on a flag inside the Intent a different Fragment shall be loaded. The Activity layout only contains a FrameLayout named fragment_container.

SplashActivity:

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if (!(getIntent().getBooleanExtra("isLaunch", true))) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new LoginFragment()).commit();
        } else {
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SplashFragment()).commit();
            }
        }
    }
}

That's in fact all the code of SplashActivity at the moment. Now my question is, if there's any way to inspect, which Fragment has been loaded? Possibly getSupportFragmentManager().getFragmentByTag()? Thanks in forward.

EDIT:

According to the suggested solution by @rxabin I added an instanceof check in my test methods.

SplashActivityTest:

@RunWith(AndroidJUnit4.class)
public class SplashActivityTest {

    private final SplashActivity testActivity = new SplashActivity();
    private final Intent testIntent = new Intent();

    @Test
    public void canLoadSplashFragment() {
        testActivity.recreate();
        Fragment fragment = testActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        assertTrue(fragment instanceof SplashFragment);
    }

    @Test
    public void canLoadLoginFragment() {
        testIntent.putExtra("isLaunch", false);
        testActivity.recreate();
        Fragment fragment = testActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        assertTrue(fragment instanceof LoginFragment);
    }

}

When I try to run this test, I get a RuntimeException: Can't create handler inside thread that has not called Looper.prepare() referring to the line where I define testActivity. Any idea how I have to instantiate an activity, so I can call testActivity.recreate() on it?

Upvotes: 0

Views: 2594

Answers (1)

rxabin
rxabin

Reputation: 106

You should be using FragmentManager's findFragmentById() method, then you can check which fragment it is by using instanceof.

Your code should look something like this:

mFragmentManager = getSupportFragmentManager();    
Fragment frag = mFragmentManager.findFragmentById(R.id.fragment_container);
if (frag instanceof LoginFragment) {
   // It's a LoginFragment
} else {
    // It's something else!
}

Upvotes: 2

Related Questions