Skip to content

Commit 78fef0d

Browse files
author
Julian Raj
committed
add unit tests and yml file for CI
1 parent 7804473 commit 78fef0d

File tree

10 files changed

+210
-44
lines changed

10 files changed

+210
-44
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ allprojects {
2222
task clean(type: Delete) {
2323
delete rootProject.buildDir
2424
}
25+
26+
task runUnitTests(dependsOn: [':validatedtextinputlayout:test']) {
27+
description 'Run unit tests for the validatedtextinputlayout.'
28+
}

circle.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
general:
2+
artifacts:
3+
- /home/ubuntu/your-app-name/app/build/outputs/apk/
4+
5+
machine:
6+
environment:
7+
ANDROID_HOME: /usr/local/android-sdk-linux
8+
9+
dependencies:
10+
pre:
11+
- 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
12+
- echo y | android update sdk --no-ui --all --filter build-tools-23.0.2
13+
14+
test:
15+
override:
16+
- (./gradlew assemble):
17+
timeout: 360
18+
- ./gradlew runUnitTests

validatedtextinputlayout/build.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ext {
1414
siteUrl = 'https://github.com/julianraj/ValidatedTextInputLayout'
1515
gitUrl = 'https://github.com/julianraj/ValidatedTextInputLayout.git'
1616

17-
libraryVersion = '0.0.1'
17+
libraryVersion = '0.0.2'
1818

1919
developerId = 'julianraj'
2020
developerName = 'Julian Raj Manandhar'
@@ -33,7 +33,7 @@ android {
3333
minSdkVersion 9
3434
targetSdkVersion 23
3535
versionCode 1
36-
versionName "0.0.1"
36+
versionName "0.0.2"
3737
}
3838
buildTypes {
3939
release {
@@ -46,8 +46,11 @@ android {
4646
dependencies {
4747
compile fileTree(dir: 'libs', include: ['*.jar'])
4848
testCompile 'junit:junit:4.12'
49+
testCompile "org.mockito:mockito-core:1.10.19"
4950
compile 'com.android.support:design:23.2.0'
5051
}
5152

52-
apply from: 'install.gradle'
53-
apply from: 'bintray.gradle'
53+
if (project.rootProject.file('local.properties').exists()) {
54+
apply from: 'install.gradle'
55+
apply from: 'bintray.gradle'
56+
}

validatedtextinputlayout/src/androidTest/java/com/julianraj/validatedinputtextlayout/ApplicationTest.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

validatedtextinputlayout/src/main/java/com/julianraj/validatedinputtextlayout/ValidatedTextInputLayout.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
110110

111111
@Override
112112
public void onTextChanged(CharSequence s, int start, int before, int count) {
113-
if (mAutoValidate) validate();
113+
if (isAutoValidated()) validate();
114114
else setError(null);
115115
}
116116

@@ -170,7 +170,7 @@ public void autoTrimValue(boolean flag) {
170170
/**
171171
* @return if auto-trimming input field value is enabled
172172
*/
173-
public boolean isAutoTrimmed(){
173+
public boolean isAutoTrimEnabled(){
174174
return mAutoTrimValue;
175175
}
176176

@@ -202,7 +202,7 @@ public boolean validate() {
202202
* @see #autoTrimValue(boolean)
203203
*/
204204
public String getValue() {
205-
if (mAutoTrimValue) return getEditText().getText().toString().trim();
205+
if (isAutoTrimEnabled()) return getEditText().getText().toString().trim();
206206
else return getEditText().getText().toString();
207207
}
208208
}

validatedtextinputlayout/src/main/java/com/julianraj/validatedinputtextlayout/validator/LengthValidator.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* Validator to set length constraints to the associated {@link ValidatedTextInputLayout}
9+
*
910
* @see BaseValidator
1011
*/
1112
public class LengthValidator extends BaseValidator {
@@ -32,22 +33,27 @@ public class LengthValidator extends BaseValidator {
3233
private int mMaximumLength = LENGTH_INDEFINITE;
3334

3435
/**
35-
*
36-
* @param pMaximumLength maximum length that the value of field can be
3736
* @param pErrorMessage error message to display if validation fails
3837
*/
38+
public LengthValidator(@NonNull String pErrorMessage) {
39+
super(pErrorMessage);
40+
}
41+
42+
/**
43+
* @param pMaximumLength maximum length that the value of field can be
44+
* @param pErrorMessage error message to display if validation fails
45+
*/
3946
public LengthValidator(int pMaximumLength, @NonNull String pErrorMessage) {
4047
super(pErrorMessage);
4148
mMaximumLength = pMaximumLength;
4249
}
4350

4451
/**
45-
*
4652
* @param pMinimumLength minimum length that the value of field must be
4753
* @param pMaximumLength maximum length that the value of field can be
48-
* @param pErrorMessage error message to display if validation fails
54+
* @param pErrorMessage error message to display if validation fails
4955
*/
50-
public LengthValidator(int pMinimumLength, int pMaximumLength,@NonNull String pErrorMessage) {
56+
public LengthValidator(int pMinimumLength, int pMaximumLength, @NonNull String pErrorMessage) {
5157
super(pErrorMessage);
5258
mMinimumLength = pMinimumLength;
5359
mMaximumLength = pMaximumLength;
@@ -63,10 +69,32 @@ public LengthValidator(int pMinimumLength, int pMaximumLength,@NonNull String pE
6369
@Override
6470
public boolean isValid(String pText) {
6571
int length = pText.length();
66-
if(mMaximumLength == LENGTH_INDEFINITE) {
67-
return length >= mMinimumLength;
68-
}else{
69-
return (length >= mMinimumLength && length <= mMaximumLength);
72+
if (getMaximumLength() == LENGTH_INDEFINITE) {
73+
return length >= getMinimumLength();
74+
} else {
75+
return (length >= getMinimumLength() && length <= getMaximumLength());
7076
}
7177
}
78+
79+
/**
80+
* @param pMinimumLength minimum required length
81+
*/
82+
public void setMinimumLength(int pMinimumLength) {
83+
mMinimumLength = pMinimumLength;
84+
}
85+
86+
/**
87+
* @param pMaximumLength maximum valid length
88+
*/
89+
public void setMaximumLength(int pMaximumLength) {
90+
mMaximumLength = pMaximumLength;
91+
}
92+
93+
public int getMinimumLength() {
94+
return mMinimumLength;
95+
}
96+
97+
public int getMaximumLength() {
98+
return mMaximumLength;
99+
}
72100
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.julianraj.validatedinputtextlayout;
2+
3+
import com.julianraj.validatedinputtextlayout.validator.BaseValidator;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import static org.hamcrest.core.Is.is;
9+
import static org.junit.Assert.assertThat;
10+
11+
/**
12+
* To work on unit tests, switch the Test Artifact in the Build Variants view.
13+
*/
14+
public class BaseValidatorTest {
15+
16+
BaseValidator mValidator;
17+
18+
public static final String ERROR_MESSAGE = "I am error message.";
19+
20+
@Before
21+
public void setUp() throws Exception {
22+
mValidator = new BaseValidator(ERROR_MESSAGE) {
23+
@Override
24+
public boolean isValid(String pText) {
25+
return false;
26+
}
27+
};
28+
}
29+
30+
@Test
31+
public void baseValidator_Constructor() {
32+
assertThat(mValidator.getErrorMessage(), is(ERROR_MESSAGE));
33+
}
34+
}

validatedtextinputlayout/src/test/java/com/julianraj/validatedinputtextlayout/ExampleUnitTest.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.julianraj.validatedinputtextlayout;
2+
3+
import com.julianraj.validatedinputtextlayout.validator.LengthValidator;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.mockito.Mock;
9+
10+
import static org.hamcrest.core.Is.is;
11+
import static org.junit.Assert.assertThat;
12+
import static org.mockito.Mockito.CALLS_REAL_METHODS;
13+
import static org.mockito.Mockito.mock;
14+
import static org.mockito.Mockito.withSettings;
15+
16+
/**
17+
* To work on unit tests, switch the Test Artifact in the Build Variants view.
18+
*/
19+
public class LengthValidatorTest {
20+
21+
@Mock
22+
LengthValidator mValidator;
23+
24+
public static final int MIN_LENGTH = 4;
25+
public static final int MAX_LENGTH = 10;
26+
27+
public static final String CORRECT_SAMPLE = "test me";
28+
public static final String INCORRECT_SAMPLE1 = "me";
29+
public static final String INCORRECT_SAMPLE2 = "test me wrong";
30+
31+
@Before
32+
public void setUp() throws Exception {
33+
//MockitoAnnotations.initMocks(this);
34+
mValidator = mock(LengthValidator.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
35+
}
36+
37+
@Test
38+
public void lengthValidator_ReturnsTrue() {
39+
mValidator.setMaximumLength(LengthValidator.LENGTH_INDEFINITE);
40+
mValidator.setMinimumLength(MIN_LENGTH);
41+
assertThat(mValidator.isValid(CORRECT_SAMPLE), is(true));
42+
43+
mValidator.setMaximumLength(MAX_LENGTH);
44+
mValidator.setMinimumLength(LengthValidator.LENGTH_ZERO);
45+
assertThat(mValidator.isValid(CORRECT_SAMPLE), is(true));
46+
47+
mValidator.setMaximumLength(MAX_LENGTH);
48+
mValidator.setMinimumLength(MIN_LENGTH);
49+
assertThat(mValidator.isValid(CORRECT_SAMPLE), is(true));
50+
}
51+
52+
@Test
53+
public void lengthValidator_ReturnsFalse() {
54+
assertThat(mValidator.isValid(INCORRECT_SAMPLE1), is(false));
55+
assertThat(mValidator.isValid(INCORRECT_SAMPLE2), is(false));
56+
}
57+
58+
@After
59+
public void tearDown() throws Exception {
60+
}
61+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.julianraj.validatedinputtextlayout;
2+
3+
import com.julianraj.validatedinputtextlayout.validator.RequiredValidator;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.mockito.Mock;
9+
10+
import static org.hamcrest.core.Is.is;
11+
import static org.junit.Assert.assertThat;
12+
import static org.mockito.Mockito.CALLS_REAL_METHODS;
13+
import static org.mockito.Mockito.mock;
14+
import static org.mockito.Mockito.withSettings;
15+
16+
/**
17+
* To work on unit tests, switch the Test Artifact in the Build Variants view.
18+
*/
19+
public class RequiredValidatorTest {
20+
21+
@Mock
22+
RequiredValidator mValidator;
23+
24+
public static final String CORRECT_SAMPLE = "test string";
25+
public static final String INCORRECT_SAMPLE1 = "";
26+
27+
@Before
28+
public void setUp() throws Exception {
29+
//MockitoAnnotations.initMocks(this);
30+
mValidator = mock(RequiredValidator.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
31+
}
32+
33+
@Test
34+
public void requiredValidator_ReturnsTrue() {
35+
assertThat(mValidator.isValid(CORRECT_SAMPLE), is(true));
36+
}
37+
38+
@Test
39+
public void requiredValidator_ReturnsFalse() {
40+
assertThat(mValidator.isValid(INCORRECT_SAMPLE1), is(false));
41+
}
42+
43+
@After
44+
public void tearDown() throws Exception {
45+
}
46+
}

0 commit comments

Comments
 (0)