From 803bed43d75ba3acdb19f2cf1730180f25345e4d Mon Sep 17 00:00:00 2001 From: Kshitij Bhardwaj <44129798+kbhardwaj123@users.noreply.github.com> Date: Tue, 28 Jan 2020 10:32:14 +0530 Subject: [PATCH] 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 --- app/src/test/java/android/text/TextUtils.java | 82 +++++++++++++++++++ .../commons/upload/UploadControllerTest.kt | 1 + 2 files changed, 83 insertions(+) create mode 100644 app/src/test/java/android/text/TextUtils.java diff --git a/app/src/test/java/android/text/TextUtils.java b/app/src/test/java/android/text/TextUtils.java new file mode 100644 index 0000000000..0cb57cdd50 --- /dev/null +++ b/app/src/test/java/android/text/TextUtils.java @@ -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