Skip to content

Commit

Permalink
add unit tests and yml file for CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Raj committed Feb 29, 2016
1 parent 7804473 commit 78fef0d
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 44 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ allprojects {
task clean(type: Delete) {
delete rootProject.buildDir
}

task runUnitTests(dependsOn: [':validatedtextinputlayout:test']) {
description 'Run unit tests for the validatedtextinputlayout.'
}
18 changes: 18 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
general:
artifacts:
- /home/ubuntu/your-app-name/app/build/outputs/apk/

machine:
environment:
ANDROID_HOME: /usr/local/android-sdk-linux

dependencies:
pre:
- echo y | android update sdk --no-ui --all --filter tool,extra-android-m2repository,extra-android-support,extra-google-google_play_services,extra-google-m2repository,android-23
- echo y | android update sdk --no-ui --all --filter build-tools-23.0.2

test:
override:
- (./gradlew assemble):
timeout: 360
- ./gradlew runUnitTests
11 changes: 7 additions & 4 deletions validatedtextinputlayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ext {
siteUrl = 'https://github.com/julianraj/ValidatedTextInputLayout'
gitUrl = 'https://github.com/julianraj/ValidatedTextInputLayout.git'

libraryVersion = '0.0.1'
libraryVersion = '0.0.2'

developerId = 'julianraj'
developerName = 'Julian Raj Manandhar'
Expand All @@ -33,7 +33,7 @@ android {
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "0.0.1"
versionName "0.0.2"
}
buildTypes {
release {
Expand All @@ -46,8 +46,11 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.10.19"
compile 'com.android.support:design:23.2.0'
}

apply from: 'install.gradle'
apply from: 'bintray.gradle'
if (project.rootProject.file('local.properties').exists()) {
apply from: 'install.gradle'
apply from: 'bintray.gradle'
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mAutoValidate) validate();
if (isAutoValidated()) validate();
else setError(null);
}

Expand Down Expand Up @@ -170,7 +170,7 @@ public void autoTrimValue(boolean flag) {
/**
* @return if auto-trimming input field value is enabled
*/
public boolean isAutoTrimmed(){
public boolean isAutoTrimEnabled(){
return mAutoTrimValue;
}

Expand Down Expand Up @@ -202,7 +202,7 @@ public boolean validate() {
* @see #autoTrimValue(boolean)
*/
public String getValue() {
if (mAutoTrimValue) return getEditText().getText().toString().trim();
if (isAutoTrimEnabled()) return getEditText().getText().toString().trim();
else return getEditText().getText().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* Validator to set length constraints to the associated {@link ValidatedTextInputLayout}
*
* @see BaseValidator
*/
public class LengthValidator extends BaseValidator {
Expand All @@ -32,22 +33,27 @@ public class LengthValidator extends BaseValidator {
private int mMaximumLength = LENGTH_INDEFINITE;

/**
*
* @param pMaximumLength maximum length that the value of field can be
* @param pErrorMessage error message to display if validation fails
*/
public LengthValidator(@NonNull String pErrorMessage) {
super(pErrorMessage);
}

/**
* @param pMaximumLength maximum length that the value of field can be
* @param pErrorMessage error message to display if validation fails
*/
public LengthValidator(int pMaximumLength, @NonNull String pErrorMessage) {
super(pErrorMessage);
mMaximumLength = pMaximumLength;
}

/**
*
* @param pMinimumLength minimum length that the value of field must be
* @param pMaximumLength maximum length that the value of field can be
* @param pErrorMessage error message to display if validation fails
* @param pErrorMessage error message to display if validation fails
*/
public LengthValidator(int pMinimumLength, int pMaximumLength,@NonNull String pErrorMessage) {
public LengthValidator(int pMinimumLength, int pMaximumLength, @NonNull String pErrorMessage) {
super(pErrorMessage);
mMinimumLength = pMinimumLength;
mMaximumLength = pMaximumLength;
Expand All @@ -63,10 +69,32 @@ public LengthValidator(int pMinimumLength, int pMaximumLength,@NonNull String pE
@Override
public boolean isValid(String pText) {
int length = pText.length();
if(mMaximumLength == LENGTH_INDEFINITE) {
return length >= mMinimumLength;
}else{
return (length >= mMinimumLength && length <= mMaximumLength);
if (getMaximumLength() == LENGTH_INDEFINITE) {
return length >= getMinimumLength();
} else {
return (length >= getMinimumLength() && length <= getMaximumLength());
}
}

/**
* @param pMinimumLength minimum required length
*/
public void setMinimumLength(int pMinimumLength) {
mMinimumLength = pMinimumLength;
}

/**
* @param pMaximumLength maximum valid length
*/
public void setMaximumLength(int pMaximumLength) {
mMaximumLength = pMaximumLength;
}

public int getMinimumLength() {
return mMinimumLength;
}

public int getMaximumLength() {
return mMaximumLength;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.julianraj.validatedinputtextlayout;

import com.julianraj.validatedinputtextlayout.validator.BaseValidator;

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class BaseValidatorTest {

BaseValidator mValidator;

public static final String ERROR_MESSAGE = "I am error message.";

@Before
public void setUp() throws Exception {
mValidator = new BaseValidator(ERROR_MESSAGE) {
@Override
public boolean isValid(String pText) {
return false;
}
};
}

@Test
public void baseValidator_Constructor() {
assertThat(mValidator.getErrorMessage(), is(ERROR_MESSAGE));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.julianraj.validatedinputtextlayout;

import com.julianraj.validatedinputtextlayout.validator.LengthValidator;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.withSettings;

/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class LengthValidatorTest {

@Mock
LengthValidator mValidator;

public static final int MIN_LENGTH = 4;
public static final int MAX_LENGTH = 10;

public static final String CORRECT_SAMPLE = "test me";
public static final String INCORRECT_SAMPLE1 = "me";
public static final String INCORRECT_SAMPLE2 = "test me wrong";

@Before
public void setUp() throws Exception {
//MockitoAnnotations.initMocks(this);
mValidator = mock(LengthValidator.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
}

@Test
public void lengthValidator_ReturnsTrue() {
mValidator.setMaximumLength(LengthValidator.LENGTH_INDEFINITE);
mValidator.setMinimumLength(MIN_LENGTH);
assertThat(mValidator.isValid(CORRECT_SAMPLE), is(true));

mValidator.setMaximumLength(MAX_LENGTH);
mValidator.setMinimumLength(LengthValidator.LENGTH_ZERO);
assertThat(mValidator.isValid(CORRECT_SAMPLE), is(true));

mValidator.setMaximumLength(MAX_LENGTH);
mValidator.setMinimumLength(MIN_LENGTH);
assertThat(mValidator.isValid(CORRECT_SAMPLE), is(true));
}

@Test
public void lengthValidator_ReturnsFalse() {
assertThat(mValidator.isValid(INCORRECT_SAMPLE1), is(false));
assertThat(mValidator.isValid(INCORRECT_SAMPLE2), is(false));
}

@After
public void tearDown() throws Exception {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.julianraj.validatedinputtextlayout;

import com.julianraj.validatedinputtextlayout.validator.RequiredValidator;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.withSettings;

/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class RequiredValidatorTest {

@Mock
RequiredValidator mValidator;

public static final String CORRECT_SAMPLE = "test string";
public static final String INCORRECT_SAMPLE1 = "";

@Before
public void setUp() throws Exception {
//MockitoAnnotations.initMocks(this);
mValidator = mock(RequiredValidator.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
}

@Test
public void requiredValidator_ReturnsTrue() {
assertThat(mValidator.isValid(CORRECT_SAMPLE), is(true));
}

@Test
public void requiredValidator_ReturnsFalse() {
assertThat(mValidator.isValid(INCORRECT_SAMPLE1), is(false));
}

@After
public void tearDown() throws Exception {
}
}

0 comments on commit 78fef0d

Please sign in to comment.