Skip to content

Commit 7f2a95c

Browse files
committed
add instrumented tests
1 parent d8bf64a commit 7f2a95c

File tree

10 files changed

+287
-81
lines changed

10 files changed

+287
-81
lines changed

app/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ dependencies {
3131
implementation 'com.android.support:appcompat-v7:27.1.1'
3232
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
3333
testImplementation 'junit:junit:4.12'
34+
androidTestImplementation 'com.android.support:support-annotations:27.1.1'
3435
androidTestImplementation 'com.android.support.test:runner:1.0.2'
36+
androidTestImplementation 'com.android.support.test:rules:1.0.2'
3537
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
3638
// Optional -- Mockito framework
37-
testImplementation 'org.mockito:mockito-core:2.19.0'
39+
testImplementation 'org.mockito:mockito-core:2.19.1'
40+
41+
3842

3943
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.jdqm.androidunittest;
2+
3+
import android.os.Parcel;
4+
import android.support.test.filters.SmallTest;
5+
import android.support.test.runner.AndroidJUnit4;
6+
import android.util.Pair;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
12+
import java.util.List;
13+
14+
import static org.hamcrest.Matchers.is;
15+
import static org.junit.Assert.assertThat;
16+
17+
/**
18+
* Created by Jdqm on 2018/7/14.
19+
*/
20+
// @RunWith 只在混合使用 JUnit3 和 JUnit4 需要,若只使用JUnit4,可省略
21+
@RunWith(AndroidJUnit4.class)
22+
@SmallTest
23+
public class LogHistoryAndroidUnitTest {
24+
25+
public static final String TEST_STRING = "This is a string";
26+
public static final long TEST_LONG = 12345678L;
27+
private LogHistory mLogHistory;
28+
29+
@Before
30+
public void setUp() {
31+
mLogHistory = new LogHistory();
32+
}
33+
34+
@Test
35+
public void logHistory_ParcelableWriteRead() {
36+
mLogHistory.addEntry(TEST_STRING, TEST_LONG);
37+
38+
// 写数据
39+
Parcel parcel = Parcel.obtain();
40+
mLogHistory.writeToParcel(parcel, mLogHistory.describeContents());
41+
42+
// 为接下来的读操作,写完数据后需要重置parcel
43+
parcel.setDataPosition(0);
44+
45+
// 读数据
46+
LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel);
47+
List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData();
48+
49+
// 验证接收到的数据是否正确
50+
assertThat(createdFromParcelData.size(), is(1));
51+
assertThat(createdFromParcelData.get(0).first, is(TEST_STRING));
52+
assertThat(createdFromParcelData.get(0).second, is(TEST_LONG));
53+
}
54+
}

app/src/main/java/com/jdqm/androidunittest/Calculate.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
package com.jdqm.androidunittest;
22

3-
import java.util.regex.Matcher;
3+
import android.text.Editable;
4+
import android.text.TextWatcher;
5+
46
import java.util.regex.Pattern;
57

68
/**
79
* Created by Jdqm on 2018-7-13.
810
*/
9-
public class EmailValidator {
11+
public class EmailValidator implements TextWatcher {
12+
13+
/**
14+
* Email validation pattern.
15+
*/
16+
public static final Pattern EMAIL_PATTERN = Pattern.compile(
17+
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
18+
"\\@" +
19+
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
20+
"(" +
21+
"\\." +
22+
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
23+
")+"
24+
);
25+
26+
private boolean mIsValid = false;
27+
28+
public boolean isValid() {
29+
return mIsValid;
30+
}
1031

11-
public boolean isValidEmail(String email) {
12-
String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
13-
Pattern pattern =Pattern.compile(regex);
14-
Matcher matcher = pattern.matcher(email);
15-
return matcher.matches();
32+
/**
33+
* Validates if the given input is a valid email address.
34+
*
35+
* @param email The email to validate.
36+
* @return {@code true} if the input is a valid email. {@code false} otherwise.
37+
*/
38+
public static boolean isValidEmail(CharSequence email) {
39+
return email != null && EMAIL_PATTERN.matcher(email).matches();
1640
}
41+
42+
@Override
43+
final public void afterTextChanged(Editable editableText) {
44+
mIsValid = isValidEmail(editableText);
45+
}
46+
47+
@Override
48+
final public void beforeTextChanged(CharSequence s, int start, int count, int after) {/*No-op*/}
49+
50+
@Override
51+
final public void onTextChanged(CharSequence s, int start, int before, int count) {/*No-op*/}
1752
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.jdqm.androidunittest;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
import android.util.Pair;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* Created by Jdqm on 2018/7/14.
12+
*/
13+
14+
public class LogHistory implements Parcelable {
15+
16+
// Used to store the data to be used by the activity.
17+
private final List<Pair<String, Long>> mData = new ArrayList<>();
18+
19+
// Creates an empty log.
20+
public LogHistory() { }
21+
22+
@Override
23+
public int describeContents() {
24+
return 0;
25+
}
26+
27+
@Override
28+
public void writeToParcel(Parcel out, int flags) {
29+
// Prepare an array of strings and an array of timestamps.
30+
String[] texts = new String[mData.size()];
31+
long[] timestamps = new long[mData.size()];
32+
33+
// Store the data in the arrays.
34+
for (int i = 0; i < mData.size(); i++) {
35+
texts[i] = mData.get(i).first;
36+
timestamps[i] = mData.get(i).second;
37+
}
38+
// Write the size of the arrays first.
39+
out.writeInt(texts.length);
40+
41+
// Write the two arrays in a specific order.
42+
out.writeStringArray(texts);
43+
out.writeLongArray(timestamps);
44+
}
45+
46+
public static final Parcelable.Creator<LogHistory> CREATOR
47+
= new Parcelable.Creator<LogHistory>() {
48+
49+
@Override
50+
public LogHistory createFromParcel(Parcel in) {
51+
return new LogHistory(in);
52+
}
53+
54+
@Override
55+
public LogHistory[] newArray(int size) {
56+
return new LogHistory[size];
57+
}
58+
};
59+
60+
/**
61+
* Returns a copy of the current data used by the activity.
62+
*/
63+
public List<Pair<String, Long>> getData() {
64+
return new ArrayList<>(mData);
65+
}
66+
67+
/**
68+
* Adds a new entry to the log.
69+
* @param text the text to be stored in the log
70+
* @param timestamp the current time in milliseconds since January 1, 1970 00:00:00.0 UTC.
71+
*/
72+
public void addEntry(String text, long timestamp) {
73+
mData.add(new Pair<String, Long>(text, timestamp));
74+
}
75+
76+
// Constructor used from the CREATOR field that unpacks a Parcel.
77+
private LogHistory(Parcel in) {
78+
// First, read the size of the arrays that contain the data.
79+
int length = in.readInt();
80+
81+
// Create the arrays to store the data.
82+
String[] texts = new String[length];
83+
long[] timestamps = new long[length];
84+
85+
// Read the arrays in a specific order.
86+
in.readStringArray(texts);
87+
in.readLongArray(timestamps);
88+
89+
// The lengths of both arrays should match or the data is corrupted.
90+
if (texts.length != timestamps.length) {
91+
throw new IllegalStateException("Error reading from saved state.");
92+
}
93+
94+
// Reset the data container and update the data.
95+
mData.clear();
96+
for (int i = 0; i < texts.length; i++) {
97+
Pair<String, Long> pair = new Pair<>(texts[i], timestamps[i]);
98+
mData.add(pair);
99+
}
100+
}
101+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.jdqm.androidunittest;
2+
3+
/**
4+
* Created by Jdqm on 2018/7/14.
5+
*/
6+
7+
public class MyClass {
8+
public int getUniqueId(){
9+
return 0;
10+
}
11+
12+
public int compareTo(int a) {
13+
return 0;
14+
}
15+
16+
public void close() {
17+
18+
}
19+
}

app/src/test/java/com/jdqm/androidunittest/CalculateTest.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
package com.jdqm.androidunittest;
22

3-
import org.junit.Before;
43
import org.junit.Test;
54

65
import static org.hamcrest.core.Is.is;
7-
import static org.junit.Assert.assertEquals;
86
import static org.junit.Assert.assertThat;
97

108
/**
11-
* Created by yangsheng on 2018-7-13.
9+
* Created by Jdqm on 2018-7-13.
1210
*/
1311
public class EmailValidatorTest {
14-
EmailValidator emailValidator;
15-
16-
@Before
17-
public void setUp() throws Exception {
18-
emailValidator = new EmailValidator();
19-
}
2012

2113
@Test
2214
public void isValidEmail() {
23-
// assertThat(emailValidator.isValidEmail("name@email.com"), is(true));
24-
assertEquals(true, emailValidator.isValidEmail("name@email.com"));
25-
}
26-
27-
@Test
28-
public void tessss() {
29-
assertThat(emailValidator.isValidEmail("name@email.com"), is(true));
15+
assertThat(EmailValidator.isValidEmail("name@email.com"), is(true));
3016
}
3117
}

app/src/test/java/com/jdqm/androidunittest/MockUnitTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import org.junit.Test;
66
import org.junit.runner.RunWith;
77
import org.mockito.Mock;
8-
import org.mockito.runners.MockitoJUnitRunner;
8+
import org.mockito.junit.MockitoJUnitRunner;
9+
910

1011
import static org.hamcrest.core.Is.is;
1112
import static org.junit.Assert.assertThat;
1213
import static org.mockito.Mockito.when;
1314

1415
/**
15-
* Created by yangsheng on 2018-7-13.
16+
* Created by Jdqm on 2018-7-13.
1617
*/
1718

1819
@RunWith(MockitoJUnitRunner.class)
@@ -24,15 +25,7 @@ public class MockUnitTest {
2425

2526
@Test
2627
public void readStringFromContext_LocalizedString() {
27-
// Given a mocked Context injected into the object under test...
28-
when(mMockContext.getString(R.string.app_name))
29-
.thenReturn(FAKE_STRING);
30-
// ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);
31-
32-
// ...when the string is returned from the object under test...
33-
//String result = myObjectUnderTest.getHelloWorldString();
34-
35-
// ...then the result should be the expected one.
28+
when(mMockContext.getString(R.string.app_name)).thenReturn(FAKE_STRING);
3629
assertThat(mMockContext.getString(R.string.app_name), is(FAKE_STRING));
3730
}
3831
}

0 commit comments

Comments
 (0)