Skip to content

Instantly share code, notes, and snippets.

@davidmigloz
Created January 28, 2016 12:53
Show Gist options
  • Select an option

  • Save davidmigloz/f4c49b52f62535e20826 to your computer and use it in GitHub Desktop.

Select an option

Save davidmigloz/f4c49b52f62535e20826 to your computer and use it in GitHub Desktop.
Solutions Robotium tests.
package com.davidmiguel.taxsystem;
import android.support.v7.widget.RecyclerView;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;
import android.widget.TextView;
import com.davidmiguel.taxsystem.activities.AddEmployee;
import com.davidmiguel.taxsystem.activities.EmployeesList;
import com.robotium.solo.Solo;
public class AddEmployeeTest extends ActivityInstrumentationTestCase2<EmployeesList> {
private Solo solo;
public AddEmployeeTest() {
super(EmployeesList.class);
}
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
public void testAddEmployeeWhiteBox() throws Exception {
// Unlock the screen if it is locked
solo.unlockScreen();
// Click on the floating action button
solo.clickOnView(solo.getView(R.id.fab));
// Check it is in AddEmployee activity
solo.assertCurrentActivity("Expected AddEmployee activity", AddEmployee.class);
// Enter data
EditText nameInput = (EditText) solo.getView(R.id.name);
EditText jobInput = (EditText) solo.getView(R.id.job);
EditText salaryInput = (EditText) solo.getView(R.id.salary);
solo.enterText(nameInput, "David");
solo.enterText(jobInput, "CEO");
solo.enterText(salaryInput, "80000");
// Click on done icon
solo.clickOnView(solo.getView(R.id.action_menu_done));
// Check Toast with "New employee created!" message appears
assertTrue("No employee created", solo.waitForText("New employee created!", 1, 2000));
// Check it is again in EmployeesList activity
solo.assertCurrentActivity("Expected EmployeesList activity", EmployeesList.class);
// Check that a new card have been created with David name
RecyclerView employeesList = (RecyclerView) solo.getView(R.id.employee_list);
int lastEmployeeIndex = employeesList.getAdapter().getItemCount() - 1;
TextView name = solo.clickInRecyclerView(lastEmployeeIndex).get(1);
assertEquals("Employee David expected", "David", name.getText());
}
public void testAddEmployeeBlackBox() throws Exception {
// Unlock the screen if it is locked
solo.unlockScreen();
// Click on the floating action button
solo.clickOnImageButton(0);
// Check it is in AddEmployee activity
solo.assertCurrentActivity("Expected AddEmployee activity", AddEmployee.class);
// Enter data
solo.enterText(0, "David");
solo.enterText(1, "CEO");
solo.enterText(2, "80000");
// Click on done icon
solo.clickOnScreen(440, 80);
// Check Toast with "New employee created!" message appears
assertTrue("No employee created", solo.searchText("New employee created!"));
// Check it is again in EmployeesList activity
solo.assertCurrentActivity("Expected EmployeesList activity", EmployeesList.class);
// Check that a new card have been created with David name
boolean found = false;
try {
for (int i = 0; ; i++) {
TextView name = solo.clickInRecyclerView(i).get(1);
if (name.getText().equals("David")) {
found = true;
break;
}
}
} catch (Exception e) {
assertEquals("Employee David expected", "David", null);
}
}
}
package com.davidmiguel.taxsystem;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite(AllTests.class.getName());
suite.addTest(TestSuite.createTest(AddEmployeeTest.class, "testAddEmployeeWhiteBox"));
suite.addTest(TestSuite.createTest(AddEmployeeTest.class, "testAddEmployeeBlackBox"));
suite.addTest(TestSuite.createTest(RemoveEmployeeTest.class, "testRemoveEmployee"));
suite.addTest(TestSuite.createTest(HelpTest.class, "testHelp"));
suite.addTest(TestSuite.createTest(CalculationTaxesTest.class, "testCalculationTaxes"));
return suite;
}
}
package com.davidmiguel.taxsystem;
import android.test.ActivityInstrumentationTestCase2;
import com.davidmiguel.taxsystem.activities.Help;
import com.robotium.solo.By;
import com.robotium.solo.Solo;
public class HelpTest extends ActivityInstrumentationTestCase2<Help> {
private Solo solo;
public HelpTest() {
super(Help.class);
}
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
public void testHelp() throws Exception {
// Unlock the screen if it is locked
solo.unlockScreen();
// Click on "How can I add a new employee?" -> id=add-employee
final By addEmployeeButton = By.id("add-employee");
// Wait for a WebElement without scrolling.
this.solo.waitForWebElement(addEmployeeButton);
// Clicks on the element
this.solo.clickOnWebElement(addEmployeeButton);
// Waits for the results page for Robotium
solo.waitForText("To add a new employee follow the next steps:", 1, 2000);
// Check if the text is visible
assertTrue("No help text found",
solo.searchText("To add a new employee follow the next steps:"));
}
}
package com.davidmiguel.taxsystem;
import android.graphics.PointF;
import android.support.v7.widget.RecyclerView;
import android.test.ActivityInstrumentationTestCase2;
import com.davidmiguel.taxsystem.activities.EmployeesList;
import com.robotium.solo.Condition;
import com.robotium.solo.Solo;
public class RemoveEmployeeTest extends ActivityInstrumentationTestCase2<EmployeesList> {
private Solo solo;
public RemoveEmployeeTest() {
super(EmployeesList.class);
}
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
public void testRemoveEmployee() throws Exception {
// Unlock the screen if it is locked
solo.unlockScreen();
// Count numbers of employees before removing
final RecyclerView employeesList = (RecyclerView) solo.getView(R.id.employee_list);
final int numEmployees = employeesList.getAdapter().getItemCount();
// Swipe from left to right in the first item
solo.swipe(new PointF(10, 200), new PointF(10, 200),
new PointF(400, 200), new PointF(400, 200));
solo.clickOnText("TaxSystem");
solo.waitForCondition(new Condition() {
@Override
public boolean isSatisfied() {
return numEmployees != employeesList.getAdapter().getItemCount();
}
}, 3000);
// Check we have one item less
int numEmployeesAfter = employeesList.getAdapter().getItemCount();
assertEquals("Expected " + (numEmployees - 1) + " employees",
numEmployees - 1, numEmployeesAfter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment