Android, AutoCompleteTextView, JUnit 4, Unit testing, Espresso, Hamcrest
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<AutoCompleteTextView
android:id="@+id/countryAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a country"/>
</LinearLayout>
AutoCompleteTextView countryAutoCompleteTextView = (AutoCompleteTextView)
findViewById(R.id.countryAutoCompleteTextView);
final String[] countries = {
"Norway",
"Norfolk",
"Ananas",
"Andorra",
};
final ArrayAdapter<String> countriesAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, countries);
countryAutoCompleteTextView.setAdapter(countriesAdapter);Nøyaktig det samme som ListView
Teit med data i Java-koden(?)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries">
<item>Andorra</item>
<item>United Arab Emirates</item>
<item>Afghanistan</item>
</string-array>
</resources>
final String[] countries = getResources().getStringArray(R.array.countries);final String[] countries = {
"Norway",
"Norfolk",
"Ananas",
"Andorra",
};Default ordering: samme rekkefølge som i arrayet
ANR
"Has stopped"
LOGG & si fra til bruker!
Flere strategier
TDD (Test Driven Development)
@Test
public void integerLimits() {
long result = calculator.add(Integer.MAX_VALUE, 1);
assertEquals(
"result should be Integer.MAX_VALUE + 1",
(long) Integer.MAX_VALUE + 1,
result
);
}(UI-testing med mer)
// given
Calculator calculator = new Calculator();
// when
int result = calculator.add(2, 3);
// then
assertEquals(5, result);public class CalculatorTest {
private Calculator calculator;
@Before
public void setUp() {
calculator = new Calculator();
}
// ...
}private Calculator calculator;
@Before
public void initializeCalculator() {
calculator = new Calculator();
}
@Test
public void simpleAddition() {
final String expected = 5;
final String actual = calculator.add(2, 3);
assertEquals("2 + 3 = 5", expected, actual);
}Standard JUnit
assert?
// build.gradle
// ...
android {
// ...
defaultConfig {
// ...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
// ...
dependencies {
// ...
// Runner for JUnit 4 support
androidTestCompile 'com.android.support.test:runner:0.4.1'
// Support annotations for JUnit 4 tests on older devices
androidTestCompile 'com.android.support:support-annotations:23.0.1'
// Actual JUnit
androidTestCompile 'junit:junit:4.12'
}
// ...
}public class CountryUtilsTest {
@Test
public void textViewToStringConvertsCorrectly() {
final String country = "Norway";
final String expectedText = "You selected " + country + "! Good job.";
final String actualText = CountryUtils.selectedCountryMessage(country);
assertEquals("Converted text should be equal", expectedText, actualText);
}
@Test(expected = IllegalArgumentException.class)
public void nullCountryThrows() {
CountryUtils.selectedCountryMessage(null);
}
@Test(expected = IllegalArgumentException.class)
public void emptyCountryThrows() {
CountryUtils.selectedCountryMessage(" \n \n ");
}
}
// build.gradle
// ...
dependencies {
// ...
// Espresso for UI automation testing
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
}public class AutocompleteDemoTest {
@Rule
public ActivityTestRule<AutoCompleteDemoActivity> activityRule = new ActivityTestRule<>(
AutoCompleteDemoActivity.class
);
private AutoCompleteDemoActivity activity;
@Before
public void setUp() {
this.activity = activityRule.getActivity();
}
@Test
public void viewIsPresent() {
onView(withId(R.id.countryAutoCompleteTextView))
.check(matches(isDisplayed()));
}
}public static Matcher<View> withCompletionThreshold(final int expectedThreshold) {
return new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View item) {
if (!(item instanceof AutoCompleteTextView)) {
return false;
}
final int actualThreshold = ((AutoCompleteTextView) item).getThreshold();
return expectedThreshold == actualThreshold;
}
@Override
public void describeTo(Description description) {
}
};
}
@Test
public void completionThresholdIsOne() {
final Matcher<View> autoCompleteMatcher = withId(R.id.countryAutoCompleteTextView);
onView(autoCompleteMatcher)
.check(matches(withCompletionThreshold(1)));
}onView(withId(R.id.countryAutoCompleteTextView))
.perform(typeText(input), closeSoftKeyboard());Expected items are displayed
@Test
public void expectedItemsAreDisplayed() {
final String input = "a";
onView(withId(R.id.countryAutoCompleteTextView))
.perform(typeText(input), closeSoftKeyboard());
final String[] matchingCountries = new String[] {"Ananas", "Andorra"};
// Verify that each matching country is displayed.
for (String matchingCountry : matchingCountries) {
onView(withText(matchingCountry))
.inRoot(withDecorView(not(is(activity.getWindow().getDecorView()))))
.check(matches(isDisplayed()));
}
}Orientation!
Config
Battery life
External resources