Normally the first thing we do in an Activity is to set the content view to a layout xml.
For example consider the below TestActivity, onCreate it sets its contentView to test_layout.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestActivity extends Activity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.test_layout); | |
} | |
} |
And the test_layout.xml is (Note : id of the root element android:id="@+id/test_layout_root")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/test_layout_root" | |
android:layout_height="match_parent" | |
android:layout_width="wrap_content" | |
... | |
... | |
</TableLayout> |
In the robolectric test, we need to get the activity's content view id and assert it with the layout id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(RobolectricTestRunner.class) | |
public class TestActivityTest { | |
private TestActivity testActivity; | |
@Before | |
public void setup() { | |
testActivity = new TestActivity(); | |
testActivity.onCreate(null); | |
} | |
@Test | |
public void shouldSetTheTestLayoutOnCreate() { | |
assertEquals(R.id.test_layout_root, shadowOf(testActivity).getContentView().getId()); | |
} | |
} |
No comments:
Post a Comment