Let's do this together as a class.
DisplayFragment.java
public class DisplayFragment extends Fragment
{
@Override
public View onCreateView( LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState )
{
View layout = inflater.inflate( R.layout.display,
container,
false );
return layout;
}
}
The XML view doesn't exist, so we'll create a file called display.xml
in the src/layout/
folder. We'll add the specifics of the view later.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
DisplayFragmentTest.java
@RunWith (RobolectricTestRunner.class)
public class DisplayFragmentTest
{
@Test
public void shouldNotBeNull() throws Exception
{
DisplayFragment fragment = new DisplayFragment();
startFragment( fragment);
assertNotNull( fragment );
}
}
At this point we should be able to run the test successfully.
Let's add our fragment to main.xml
(layout file specified in setContentView()
of the CalculatorActivity
).
<fragment
android:id="@+id/display_fragment"
android:name="com.mobiquity.calculator.DisplayFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
@Test
public void shouldHaveDisplayFragment() throws Exception
{
assertNotNull( activity.getFragmentManager()
.findFragmentById( R.id.display_fragment ) );
}
Make sure to run the tests and then commit your work to your repository.