[Other Tools & General Discussion] EatHeat:
Disclaimer

I am not the developer of this tool. I am just sharing how to test your apps' functionality via Robotium.

This tutorial assumes that you have the SDK and Eclipse all set up perfectly. Setting these up will not be covered in this tutorial.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Setting up a sample project:

Although source codes are available, it is recommended to do this manually.

Create a new android project by going to File> New> Android Application Project.

Fill in the following information until you have the project set up.

So your project is now set up. Its a simple calculator just for testing.

Type this in your main.xml;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSpace"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtFirstNumber"
/>

<EditText
android:inputType="numberDecimal"
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSpace"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSecondNumber"
/>

<EditText
android:inputType="numberDecimal"
android:id="@+id/EditText02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSpace"
/>

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSpace"
/>

<Button
android:text="Multiply"
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>

</LinearLayout>

This in your strings.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Enter two values and click on Calculate to multiply them.</string>
<string name="app_name">AndroidCalculator</string>
<string name="txtFirstNumber">Enter First Number</string>
<string name="txtSecondNumber">Enter Second Number</string>
<string name="txtSpace"></string>
</resources>

This in your Main.java :

package com.calculator;

import com.calculator.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.text.Editable;

public class Main extends Activity {
EditText FirstValue;
EditText SecondValue;
TextView Result;
Button Calculate;
float num1 , num2;

[MENTION=439709]override[/MENTION]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

FirstValue = (EditText) findViewById(R.id.EditText01);

SecondValue = (EditText) findViewById(R.id.EditText02);

Result = (TextView) findViewById(R.id.TextView01);
Result.setText("0.00");

Calculate = (Button) findViewById(R.id.Button01);

//Adding listener to button
Calculate.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
//Getting first & second values and passing to show result
showResult(FirstValue.getText(), SecondValue.getText());
}
});
}

//Showing multiply results
protected void showResult(Editable first, Editable second)
{
float num1 = Float.parseFloat(first.toString());
float num2 = Float.parseFloat(second.toString());
float result = num1 * num2;
Result.setText(String.valueOf(result));
}

}

Your basic calculator app is now ready. This is just for testing purposes so we will not be focusing on the layout too much.

Launch your app in emulator. It will look like this :

Now we will create a test android project.

Go to File> New> Project and follow like its given below :

This is also done.

Now right click on the Test project and go to New> JUnit Test Case and setup the following:

A new java file will now bee created.

Now right click on the Test Project again and go to Build Path> Configure Build Path and setup the following again:

Here Click Add External Jars and browse to the Robotium jar file. You can download it

here

. Choose the latest one to download.

Note: Not javadoc.jar Only Jar file.

Go to Order and Export and check the jar file and click OK.

Now type this in the TestMain.java :

package com.calculator.test;

import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;
import android.widget.TextView;
import com.calculator.Main;
import com.calculator.R;
import com.jayway.android.robotium.solo.Solo;

public class TestMain extends ActivityInstrumentationTestCase2<Main> {
private Solo solo;

public TestMain() {
super(Main.class);
}

[MENTION=439709]override[/MENTION]
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}

public void testDisplayBlackBox() {

//Enter 10 in first edit-field
solo.enterText(0, "10");

//Enter 20 in first edit-field
solo.enterText(1, "20");

//Click on Multiply button
solo.clickOnButton("Multiply");

//Verify that resultant of 10 x 20
assertTrue(solo.searchText("200"));

}

public void testDisplayWhiteBox() {

//Defining our own values to multiply
float firstNumber = 10;
float secondNumber = 20;
float resutl = firstNumber * secondNumber ;

//Access First value (edit-filed) and putting firstNumber value in it
EditText FirsteditText = (EditText) solo.getView(R.id.EditText01);
solo.enterText(FirsteditText, String.valueOf(firstNumber));

//Access Second value (edit-filed) and putting SecondNumber value in it
EditText SecondeditText = (EditText) solo.getView(R.id.EditText02);
solo.enterText(SecondeditText, String.valueOf(secondNumber));

//Click on Multiply button
solo.clickOnButton("Multiply");

assertTrue(solo.searchText(String.valueOf(resutl)));
TextView outputField = (TextView) solo.getView(R.id.TextView01);
//Assert to verify result with visible value
assertEquals(String.valueOf(resutl), outputField.getText().toString());
}

[MENTION=439709]override[/MENTION]
protected void tearDown() throws Exception{

solo.finishOpenedActivities();
}
}

That's it. Your test project is also done.

Now right click on the TestMain.java in Eclipse and run it as an Android JUnit Test Case:

Project will now load up in the emulator and auto testing will begin as provided in TestMain.java:

Finally your results will be shown in the JUnit panel. Which shouldn't have any errors of course.

That's it! You have just tested your app without even really testing it!! :good:​