Skip to content

Commit

Permalink
Merge pull request #14 from ribot/unit_testing
Browse files Browse the repository at this point in the history
Unit testing with Robolectric
  • Loading branch information
hitherejoe committed Jul 8, 2014
2 parents 35efa91 + 099d873 commit 59bb42a
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 13 deletions.
24 changes: 11 additions & 13 deletions .travis.yml
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
13 changes: 13 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import com.android.builder.core.BuilderConstants

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
}
}

apply plugin: 'com.android.library'
apply plugin: 'maven'
apply plugin: 'signing'
apply plugin: 'android-test'

version = "1.1.0"
group = "uk.co.ribot"

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

androidTestCompile 'junit:junit:4.11'
androidTestCompile 'org.robolectric:robolectric:2.3'
}

android {
Expand Down
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) {

}
}
}
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) {

}
}
}
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());
}
}
}
21 changes: 21 additions & 0 deletions wait_for_emulator
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"

0 comments on commit 59bb42a

Please sign in to comment.