Skip to content

Commit 803bed4

Browse files
kbhardwaj123maskaravivek
authored andcommitted
Fixes: #3343 TextUtils.isEmpty creates problems when unit testing with Mockito (#3344)
* TextUtils: add mock textUtils for tests * TextUtils: add more methods to mock for testing * UploadControllerTest: fix the test resulting travis ci to fail
1 parent d235e5a commit 803bed4

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package android.text;
2+
3+
4+
import androidx.annotation.Nullable;
5+
6+
/**
7+
* This Class Mocks TextUtils for the purpose of testing.
8+
* NOTE: This class would not change the function of the TextUtils used in app
9+
* it onlt mocks it for the unit tests
10+
*
11+
*/
12+
public class TextUtils {
13+
/**
14+
* mocks TextUtils.isEmpty
15+
*/
16+
public static boolean isEmpty(@Nullable CharSequence str) {
17+
return str == null || str.length() == 0;
18+
}
19+
20+
/**
21+
* mocks TextUtils.equals
22+
*/
23+
public static boolean equals(CharSequence a, CharSequence b) {
24+
if (a == b) return true;
25+
int length;
26+
if (a != null && b != null && (length = a.length()) == b.length()) {
27+
if (a instanceof String && b instanceof String) {
28+
return a.equals(b);
29+
} else {
30+
for (int i = 0; i < length; i++) {
31+
if (a.charAt(i) != b.charAt(i)) return false;
32+
}
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
39+
/**
40+
* mocks TextUtils.isDigitsOnly
41+
*/
42+
public static boolean isDigitsOnly(CharSequence str) {
43+
final int len = str.length();
44+
for (int cp, i = 0; i < len; i += Character.charCount(cp)) {
45+
cp = Character.codePointAt(str, i);
46+
if (!Character.isDigit(cp)) {
47+
return false;
48+
}
49+
}
50+
return true;
51+
}
52+
53+
/**
54+
* mocks TextUtils.isNewline
55+
*/
56+
private static boolean isNewline(int codePoint) {
57+
int type = Character.getType(codePoint);
58+
return type == Character.PARAGRAPH_SEPARATOR || type == Character.LINE_SEPARATOR
59+
|| codePoint == 10;
60+
}
61+
62+
/**
63+
* Returns whether the given CharSequence contains any printable characters.
64+
*/
65+
public static boolean isGraphic(CharSequence str) {
66+
final int len = str.length();
67+
for (int cp, i=0; i<len; i+=Character.charCount(cp)) {
68+
cp = Character.codePointAt(str, i);
69+
int gc = Character.getType(cp);
70+
if (gc != Character.CONTROL
71+
&& gc != Character.FORMAT
72+
&& gc != Character.SURROGATE
73+
&& gc != Character.UNASSIGNED
74+
&& gc != Character.LINE_SEPARATOR
75+
&& gc != Character.PARAGRAPH_SEPARATOR
76+
&& gc != Character.SPACE_SEPARATOR) {
77+
return true;
78+
}
79+
}
80+
return false;
81+
}
82+
}

app/src/test/kotlin/fr/free/nrw/commons/upload/UploadControllerTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class UploadControllerTest {
4949
@Test
5050
fun startUpload() {
5151
val contribution = mock(Contribution::class.java)
52+
`when`(contribution.getCreator()).thenReturn("Creator")
5253
uploadController!!.startUpload(contribution)
5354
}
5455
}

0 commit comments

Comments
 (0)