forked from robolectric/robolectric
-
Notifications
You must be signed in to change notification settings - Fork 0
Ex3. Test common form controls.
tyanhly edited this page Apr 23, 2013
·
2 revisions
This tip is example to test android form controls. One test case for test default values, the orther for changed values. OS: linux
java: 1.7
IDE: eclipse
robolectric 2.0 alpha 2
Application Name: a3 Project name: a3 Package: com.example.a3Edit layout activity_main.xml as following:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollbars="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/TextViewTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/feedbacktitle" android:textSize="10pt" > </textview> <EditText android:id="@+id/EditTextName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/feedbackname" android:inputType="textPersonName" > </edittext> <EditText android:id="@+id/EditTextEmail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/feedbackemail" android:inputType="textEmailAddress" > </edittext> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton" /> <ToggleButton android:id="@+id/toggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="Vibrate on" android:textOff="Vibrate off"/> <Spinner android:id="@+id/planetsSpinner" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Touch for submit" android:text="Submit" /> </linearlayout> </scrollview>
The layout also includes TextView, EditText, Checkbox, Button, RadioButton, ToggleButton, Spinner. These controls are used to demo some simple ideals to test. The button1 is used to perform a changed action by a trigger from user.
Edit MainActivity.javapackage com.example.a3; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Spinner; import android.widget.ToggleButton; public class MainActivity extends Activity { private ArrayAdapter<CharSequence> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1); checkBox.setChecked(true); ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); toggleButton.setChecked(true); Spinner spinner = (Spinner) this.findViewById(R.id.planetsSpinner); CharSequence[] itemArray = getResources().getTextArray( R.array.planets_array); List<CharSequence> itemList = new ArrayList<CharSequence>( Arrays.asList(itemArray)); adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, itemList); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); findViewById(R.id.button1).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { EditText editTextName = (EditText) findViewById(R.id.EditTextName); editTextName.setText("new Value"); CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1); checkBox.setChecked(false); RadioButton radioButton = (RadioButton) findViewById(R.id.radioButton1); radioButton.setChecked(true); ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); toggleButton.setChecked(false); adapter.add("another Item"); adapter.notifyDataSetChanged(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
After this step you can run to test app before write unit test.
Create test folder in root project path.Create package com.example.a3 in test folder.
Create MainActivityTest class in this package.
package com.example.a3; import java.util.ArrayList; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Spinner; import android.widget.ToggleButton; import com.xtremelabs.robolectric.RobolectricTestRunner; @RunWith(RobolectricTestRunner.class) public class MainActivityTest { private MainActivity mainActivity; private Button button1; private EditText editTextName; private EditText editTextEmail; private CheckBox checkBox; private RadioButton radioButton; private ToggleButton toggleButton; private Spinner spinner; @Before public void setup() { mainActivity = new MainActivity(); mainActivity.onCreate(null); button1 = (Button) mainActivity.findViewById(R.id.button1); editTextName = (EditText) mainActivity.findViewById(R.id.EditTextName); editTextEmail = (EditText) mainActivity .findViewById(R.id.EditTextEmail); checkBox = (CheckBox) mainActivity.findViewById(R.id.checkBox1); radioButton = (RadioButton) mainActivity .findViewById(R.id.radioButton1); toggleButton = (ToggleButton) mainActivity .findViewById(R.id.toggleButton); spinner = (Spinner) mainActivity.findViewById(R.id.planetsSpinner); } @Test public void shouldHaveValidPropertiesForFormElements() { Assert.assertEquals(button1.getText().toString(), "Submit"); Assert.assertEquals(button1.getHint().toString(), "Touch for submit"); Assert.assertEquals(editTextName.getText().toString(), ""); Assert.assertEquals(editTextName.getHint().toString(), "Full Name"); Assert.assertEquals(editTextEmail.getText().toString(), ""); Assert.assertEquals(editTextEmail.getHint().toString(), "example@email.com"); Assert.assertEquals(checkBox.isChecked(), true); Assert.assertEquals(checkBox.getText().toString(), "CheckBox"); Assert.assertEquals(radioButton.isChecked(), false); Assert.assertEquals(radioButton.getText().toString(), "RadioButton"); Assert.assertEquals(toggleButton.isChecked(), true); Assert.assertEquals(spinner.getAdapter().getCount(), 8); } @Test public void pressingTheButtonShouldChangeValueOfSomeElements() throws Exception { checkBox.callOnClick(); button1.performClick(); Assert.assertEquals(editTextName.getText().toString(), "new Value"); Assert.assertEquals(checkBox.isChecked(), false); Assert.assertEquals(radioButton.isChecked(), true); Assert.assertEquals(toggleButton.isChecked(), false); Assert.assertEquals(spinner.getAdapter().getCount(), 9); } }
- Configurate build path - Right click a3 project - Select "Refesh" - Right click a3 - Select "Build Path" => "Configure Build Path..." - Add JUnit library on library tab - Add robolectric-X.X.X-jar-with-dependencies.jar (remember! - using dependencies for this tutor) - Add android.jar from <your android install directory>/platforms/android-17/android.jar - Add maps.jar from <your android install directory>/add-ons/addon_google_apis_google_inc_17/libs/maps.jar - Configurate and run test unit - Go to "Run" => "Run Configuration" - Double click "JUnit" (not "Android JUnit Test") - Name: a3-test - Select the "Run all tests in the selected project, package or source folder:" radio button - Click the "Search" button - Select "a3" - TestRunner: JUnit 4 - Click on the link "Multiple launchers available Select one…" at the bottom of the dialog - Check the “Use configuration specific settings” box - Select "Eclipse JUnit Launcher" - Click "OK" - Click "Run"https://github.com/tyanhly/a3.git