Skip to content

Commit

Permalink
Test to verify that built-in field adapters can be overridden by cust…
Browse files Browse the repository at this point in the history
…om adapters. Reaction to #34
  • Loading branch information
tomas-vondracek committed Jul 20, 2015
1 parent ecca8c7 commit fe8ed9d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
// Robolectric tests
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'com.squareup:fest-android:1.0.8'
testCompile 'com.squareup:fest-android:1.0.8@aar'
testCompile('org.robolectric:robolectric:2.4') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package eu.inmite.android.lib.validations;

import android.widget.TextView;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.lang.annotation.Annotation;

import eu.inmite.android.lib.validations.form.FormValidator;
import eu.inmite.android.lib.validations.form.annotations.MinLength;
import eu.inmite.android.lib.validations.form.annotations.NotEmpty;
import eu.inmite.android.lib.validations.form.iface.IFieldAdapter;

import static org.junit.Assert.assertTrue;

/**
* @author Tomáš Vondráček ([email protected]) on 20/07/15.
*/
@RunWith(RobolectricTestRunner.class)
@Config(manifest=Config.NONE)
public class CustomAdapterForSupportedViewTest {

public static class CustomTextViewAdapter implements IFieldAdapter<TextView, CharSequence> {

@Override
public String getFieldValue(Annotation annotation, TextView fieldView) {
return fieldView.getText() + "some added text";
}
}

private static class ModelWithValidation {

@NotEmpty
@MinLength(3)
private final TextView view = new TextView(Robolectric.application);
}

@Before
public void setUp() throws Exception {
FormValidator.registerViewAdapter(TextView.class, CustomTextViewAdapter.class);
}

@After
public void tearDown() throws Exception {
FormValidator.clearViewAdapters();
}

@Test
public void customAdapterShouldBeUsed() throws Exception {
final ModelWithValidation model = new ModelWithValidation();
model.view.setText(""); //adapter will add text and therefore validation should pass

final boolean valid = FormValidator.validate(Robolectric.application, model, null);
assertTrue(valid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import android.content.Context;
import android.view.View;
import eu.inmite.android.lib.validations.form.FormValidator;
import eu.inmite.android.lib.validations.form.annotations.MinLength;
import eu.inmite.android.lib.validations.form.annotations.NotEmpty;
import eu.inmite.android.lib.validations.form.iface.IFieldAdapter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -16,6 +13,11 @@

import java.lang.annotation.Annotation;

import eu.inmite.android.lib.validations.form.FormValidator;
import eu.inmite.android.lib.validations.form.annotations.MinLength;
import eu.inmite.android.lib.validations.form.annotations.NotEmpty;
import eu.inmite.android.lib.validations.form.iface.IFieldAdapter;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down

0 comments on commit fe8ed9d

Please sign in to comment.