Issue
I just ran into this issue where I'm trying to test if a fragment is displayed, but for some reason I can't test based on the fragment's root ID. Here's the code:
MainActivity.java:
package com.gregspitz.simplefragmenttesttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.gregspitz.simplefragmenttesttest.MainActivity">
<fragment
android:id="@+id/main_fragment_holder"
android:name="com.gregspitz.simplefragmenttesttest.BasicFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
BasicFragment.java:
package com.gregspitz.simplefragmenttesttest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BasicFragment extends Fragment {
public BasicFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_basic, container, false);
}
}
fragment_basic.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/basic_fragment"
tools:context="com.gregspitz.simplefragmenttesttest.BasicFragment">
<TextView
android:id="@+id/simple_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"/>
</LinearLayout>
MainActivityTest.java:
package com.gregspitz.simplefragmenttesttest;
import android.support.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.*;
/**
* Created by gregs on 2/6/2018.
*/
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityTestRule =
new ActivityTestRule<MainActivity>(MainActivity.class);
@Test
public void mainActivity_basicFragmentIsDisplayedAtStart() {
onView(withId(R.id.basic_fragment)).check(matches(isDisplayed()));
}
@Test
public void mainActivity_basicFragmentInnerIdIsFound() {
onView(withId(R.id.simple_text)).check(matches(isDisplayed()));
}
}
The second test works where I'm looking for an ID within the layout, but the first test doesn't find the layout's root ID. I just don't understand why that doesn't work and what should I do instead? Thanks for the help.
Solution
When the fragment gets inflated on the view, the Android framework replaces the root ID of the fragment's layout to the ID of the fragment itself.
The LinearLayout will have the ID changed to the ID of the fragment.
@Test
public void mainActivity_basicFragmentIsDisplayedAtStart() {
onView(withId(R.id.main_fragment_holder)).check(matches(isDisplayed()));
}
Answered By - Diego Malone
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.