-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: #3343 TextUtils.isEmpty creates problems when unit testing wit…
…h 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
- Loading branch information
1 parent
d235e5a
commit 803bed4
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package android.text; | ||
|
||
|
||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* This Class Mocks TextUtils for the purpose of testing. | ||
* NOTE: This class would not change the function of the TextUtils used in app | ||
* it onlt mocks it for the unit tests | ||
* | ||
*/ | ||
public class TextUtils { | ||
/** | ||
* mocks TextUtils.isEmpty | ||
*/ | ||
public static boolean isEmpty(@Nullable CharSequence str) { | ||
return str == null || str.length() == 0; | ||
} | ||
|
||
/** | ||
* mocks TextUtils.equals | ||
*/ | ||
public static boolean equals(CharSequence a, CharSequence b) { | ||
if (a == b) return true; | ||
int length; | ||
if (a != null && b != null && (length = a.length()) == b.length()) { | ||
if (a instanceof String && b instanceof String) { | ||
return a.equals(b); | ||
} else { | ||
for (int i = 0; i < length; i++) { | ||
if (a.charAt(i) != b.charAt(i)) return false; | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* mocks TextUtils.isDigitsOnly | ||
*/ | ||
public static boolean isDigitsOnly(CharSequence str) { | ||
final int len = str.length(); | ||
for (int cp, i = 0; i < len; i += Character.charCount(cp)) { | ||
cp = Character.codePointAt(str, i); | ||
if (!Character.isDigit(cp)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* mocks TextUtils.isNewline | ||
*/ | ||
private static boolean isNewline(int codePoint) { | ||
int type = Character.getType(codePoint); | ||
return type == Character.PARAGRAPH_SEPARATOR || type == Character.LINE_SEPARATOR | ||
|| codePoint == 10; | ||
} | ||
|
||
/** | ||
* Returns whether the given CharSequence contains any printable characters. | ||
*/ | ||
public static boolean isGraphic(CharSequence str) { | ||
final int len = str.length(); | ||
for (int cp, i=0; i<len; i+=Character.charCount(cp)) { | ||
cp = Character.codePointAt(str, i); | ||
int gc = Character.getType(cp); | ||
if (gc != Character.CONTROL | ||
&& gc != Character.FORMAT | ||
&& gc != Character.SURROGATE | ||
&& gc != Character.UNASSIGNED | ||
&& gc != Character.LINE_SEPARATOR | ||
&& gc != Character.PARAGRAPH_SEPARATOR | ||
&& gc != Character.SPACE_SEPARATOR) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters