-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ribot/unit_testing
Unit testing with Robolectric
- Loading branch information
Showing
6 changed files
with
284 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
language: java | ||
jdk: oraclejdk7 | ||
language: android | ||
android: | ||
components: | ||
- build-tools-19.1.0 | ||
licenses: | ||
- android-sdk-license-5be876d5 | ||
before_install: | ||
# Install base Android SDK | ||
- sudo apt-get update -qq | ||
- sudo apt-get install -qq libstdc++6:i386 lib32z1 expect | ||
- export COMPONENTS=build-tools-19.1.0,android-19,sys-img-armeabi-v7a-android-19,extra-android-support | ||
- curl -L https://raw.github.com/embarkmobile/android-sdk-installer/version-2/android-sdk-installer | bash /dev/stdin --install=$COMPONENTS | ||
- source ~/.android-sdk-installer/env | ||
|
||
# Create and start emulator | ||
- echo no | android create avd --force -n test -t android-19 --abi armeabi-v7a | ||
- emulator -avd test -no-skin -no-audio -no-window & | ||
|
||
- export TERM=dumb | ||
- ./gradlew -v | ||
- uname -a | ||
before_script: | ||
- wait_for_emulator | ||
|
||
script: ./gradlew demo:connectedCheck | ||
- ./wait_for_emulator | ||
script: ./gradlew clean library:test demo:connectedCheck |
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
57 changes: 57 additions & 0 deletions
57
library/src/androidTest/java/uk/co/ribot/easyadapter/ClassAnnotationParserTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package uk.co.ribot.easyadapter; | ||
|
||
import android.view.View; | ||
|
||
import junit.framework.Assert; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
import uk.co.ribot.easyadapter.annotations.ClassAnnotationParser; | ||
import uk.co.ribot.easyadapter.annotations.LayoutId; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE) | ||
public class ClassAnnotationParserTest { | ||
|
||
private static final int LAYOUT_ID = 1; | ||
|
||
@Test public void testGetLayoutId() throws Exception{ | ||
Integer layoutId = ClassAnnotationParser.getLayoutId(ItemViewHolderWithLayoutId.class); | ||
Assert.assertTrue(layoutId.equals(LAYOUT_ID)); | ||
} | ||
|
||
@Test public void testGetLayoutIdWithoutAnnotation() throws Exception{ | ||
Integer layoutId = ClassAnnotationParser.getLayoutId(ItemViewHolderWithoutLayoutId.class); | ||
Assert.assertNull(layoutId); | ||
} | ||
|
||
/*** Test ItemViewHolders ***/ | ||
|
||
@LayoutId(LAYOUT_ID) | ||
private static class ItemViewHolderWithLayoutId extends ItemViewHolder<Object> { | ||
|
||
ItemViewHolderWithLayoutId(View view) { | ||
super(view); | ||
} | ||
|
||
@Override | ||
public void onSetValues(Object item, PositionInfo positionInfo) { | ||
|
||
} | ||
} | ||
|
||
private static class ItemViewHolderWithoutLayoutId extends ItemViewHolder<Object> { | ||
|
||
ItemViewHolderWithoutLayoutId(View view) { | ||
super(view); | ||
} | ||
|
||
@Override | ||
public void onSetValues(Object item, PositionInfo positionInfo) { | ||
|
||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
library/src/androidTest/java/uk/co/ribot/easyadapter/EasyAdapterUtilTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package uk.co.ribot.easyadapter; | ||
|
||
import android.view.View; | ||
|
||
import junit.framework.Assert; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
import uk.co.ribot.easyadapter.annotations.LayoutId; | ||
|
||
|
||
@RunWith(RobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE) | ||
public class EasyAdapterUtilTest { | ||
|
||
private static final int LAYOUT_ID = 1; | ||
|
||
@Test public void testCreateViewHolder() throws Exception { | ||
View view = new View(Robolectric.application); | ||
ItemViewHolder<?> itemViewHolder = EasyAdapterUtil.createViewHolder(view, ValidItemViewHolder.class); | ||
Assert.assertNotNull(itemViewHolder); | ||
} | ||
|
||
@Test public void testCreateInvalidViewHolder() throws Exception { | ||
View view = new View(Robolectric.application); | ||
InvalidViewHolderException exception = null; | ||
try { | ||
//Should throw a InvalidViewHolderException exception | ||
EasyAdapterUtil.createViewHolder(view, InvalidItemViewHolder.class); | ||
}catch (InvalidViewHolderException e) { | ||
exception = e; | ||
} | ||
Assert.assertNotNull(exception); | ||
} | ||
|
||
@Test public void testParseItemLayoutId() throws Exception { | ||
Integer layoutId = EasyAdapterUtil.parseItemLayoutId(ItemViewHolderWithLayoutId.class); | ||
Assert.assertTrue(layoutId.equals(LAYOUT_ID)); | ||
} | ||
|
||
@Test public void testParseItemLayoutIdWithoutLayoutId() throws Exception { | ||
LayoutIdMissingException exception = null; | ||
try { | ||
EasyAdapterUtil.parseItemLayoutId(ItemViewHolderWithoutLayoutId.class); | ||
}catch (LayoutIdMissingException e) { | ||
exception = e; | ||
} | ||
Assert.assertNotNull(exception); | ||
} | ||
|
||
/*** Test ItemViewHolders ***/ | ||
|
||
private static class ValidItemViewHolder extends ItemViewHolder<Object> { | ||
|
||
public ValidItemViewHolder(View view) { | ||
super(view); | ||
} | ||
|
||
@Override | ||
public void onSetValues(Object item, PositionInfo positionInfo) { | ||
|
||
} | ||
|
||
} | ||
|
||
//This ItemViewHolder is invalid because it has another parameter in the constructor | ||
private static class InvalidItemViewHolder extends ItemViewHolder<Object> { | ||
|
||
public InvalidItemViewHolder(View view, int anotherParam) { | ||
super(view); | ||
} | ||
|
||
@Override | ||
public void onSetValues(Object item, PositionInfo positionInfo) { | ||
|
||
} | ||
|
||
} | ||
|
||
@LayoutId(LAYOUT_ID) | ||
private static class ItemViewHolderWithLayoutId extends ItemViewHolder<Object> { | ||
|
||
ItemViewHolderWithLayoutId(View view) { | ||
super(view); | ||
} | ||
|
||
@Override | ||
public void onSetValues(Object item, PositionInfo positionInfo) { | ||
|
||
} | ||
} | ||
|
||
//This view holders doesn't have a LayoutId annotation | ||
private static class ItemViewHolderWithoutLayoutId extends ItemViewHolder<Object> { | ||
|
||
ItemViewHolderWithoutLayoutId(View view) { | ||
super(view); | ||
} | ||
|
||
@Override | ||
public void onSetValues(Object item, PositionInfo positionInfo) { | ||
|
||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
library/src/androidTest/java/uk/co/ribot/easyadapter/FieldAnnotationParserTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package uk.co.ribot.easyadapter; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.Activity; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.widget.ImageView; | ||
import android.widget.LinearLayout; | ||
import android.widget.TextView; | ||
|
||
import junit.framework.Assert; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
import uk.co.ribot.easyadapter.annotations.FieldAnnotationParser; | ||
import uk.co.ribot.easyadapter.annotations.ViewId; | ||
|
||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) //Disable because this code will run inside robolectric with api level 17+ | ||
@RunWith(RobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE) | ||
public class FieldAnnotationParserTest { | ||
|
||
private static final int TEXT_VIEW_ID = 1000; | ||
private static final int IMAGE_VIEW_ID = 2000; | ||
|
||
@Test public void testSetViewFields() throws Exception { | ||
TestObjectWithAnnotations testObject = new TestObjectWithAnnotations(); | ||
FieldAnnotationParser.setViewFields(testObject, createTestLinearLayout()); | ||
Assert.assertNotNull(testObject.textView); | ||
Assert.assertNotNull(testObject.imageView); | ||
} | ||
|
||
@Test public void testSetViewFieldsActivity() throws Exception { | ||
Activity testActivity = Robolectric.buildActivity(TestActivity.class).create().get(); | ||
TestObjectWithAnnotations testObject = new TestObjectWithAnnotations();; | ||
FieldAnnotationParser.setViewFields(testObject, testActivity); | ||
Assert.assertNotNull(testObject.textView); | ||
Assert.assertNotNull(testObject.imageView); | ||
} | ||
|
||
@SuppressWarnings("ResourceType") //Because of warning when setting a hardcoded ID into the view | ||
private static LinearLayout createTestLinearLayout() { | ||
LinearLayout linearLayout = new LinearLayout(Robolectric.application); | ||
TextView textView = new TextView(Robolectric.application); | ||
textView.setId(TEXT_VIEW_ID); | ||
linearLayout.addView(textView); | ||
ImageView imageView = new ImageView(Robolectric.application); | ||
imageView.setId(IMAGE_VIEW_ID); | ||
linearLayout.addView(imageView); | ||
return linearLayout; | ||
} | ||
|
||
private class TestObjectWithAnnotations { | ||
|
||
@ViewId(TEXT_VIEW_ID) | ||
TextView textView; | ||
@ViewId(IMAGE_VIEW_ID) | ||
ImageView imageView; | ||
|
||
} | ||
|
||
static class TestActivity extends Activity { | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(createTestLinearLayout()); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/bash | ||
|
||
# Originally written by Ralf Kistner <[email protected]>, but placed in the public domain | ||
|
||
set +e | ||
|
||
bootanim="" | ||
failcounter=0 | ||
until [[ "$bootanim" =~ "stopped" ]]; do | ||
bootanim=`adb -e shell getprop init.svc.bootanim 2>&1` | ||
echo "$bootanim" | ||
if [[ "$bootanim" =~ "not found" ]]; then | ||
let "failcounter += 1" | ||
if [[ $failcounter -gt 15 ]]; then | ||
echo "Failed to start emulator" | ||
exit 1 | ||
fi | ||
fi | ||
sleep 1 | ||
done | ||
echo "Done" |