summaryrefslogtreecommitdiff
path: root/core/src/test/java
diff options
context:
space:
mode:
authororionlee <orionlee@yahoo.com>2018-05-18 13:21:36 -0700
committerorionlee <orionlee@yahoo.com>2018-05-18 13:21:36 -0700
commit46ae3e0b00a3b3ec8ef7ba01efad8bcfb1a79398 (patch)
tree9467bb3db44be725d2fc56092f98f602d2face42 /core/src/test/java
parentf05c7e23efce568b830d000df773209640028d7c (diff)
downloadAntennaPod-46ae3e0b00a3b3ec8ef7ba01efad8bcfb1a79398.zip
core tests: Convert tests that are effectively unit tests to be one
(standard android junit tests). Provides implementations for android platform utils needed (in unit test environment): 1. android.util.Log, 2. android.text.TextUtils.isEmpty()
Diffstat (limited to 'core/src/test/java')
-rw-r--r--core/src/test/java/android/text/TextUtils.java32
-rw-r--r--core/src/test/java/android/util/Log.java245
-rw-r--r--core/src/test/java/de/danoeh/antennapod/core/feed/FeedImageMother.java9
-rw-r--r--core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemMother.java16
-rw-r--r--core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java71
-rw-r--r--core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java14
-rw-r--r--core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java102
-rw-r--r--core/src/test/java/de/danoeh/antennapod/core/util/LongLongMapTest.java65
8 files changed, 554 insertions, 0 deletions
diff --git a/core/src/test/java/android/text/TextUtils.java b/core/src/test/java/android/text/TextUtils.java
new file mode 100644
index 000000000..c31234171
--- /dev/null
+++ b/core/src/test/java/android/text/TextUtils.java
@@ -0,0 +1,32 @@
+package android.text;
+
+/**
+ * A slim-down version of standard {@link android.text.TextUtils} to be used in unit tests.
+ */
+public class TextUtils {
+
+ /**
+ * Returns true if a and b are equal, including if they are both null.
+ * <p><i>Note: In platform versions 1.1 and earlier, this method only worked well if
+ * both the arguments were instances of String.</i></p>
+ * @param a first CharSequence to check
+ * @param b second CharSequence to check
+ * @return true if a and b are equal
+ */
+ 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;
+ }
+
+}
diff --git a/core/src/test/java/android/util/Log.java b/core/src/test/java/android/util/Log.java
new file mode 100644
index 000000000..881d10209
--- /dev/null
+++ b/core/src/test/java/android/util/Log.java
@@ -0,0 +1,245 @@
+package android.util;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+/**
+ * A stub for {@link android.util.Log} to be used in unit tests.
+ *
+ * It outputs the log statements to standard error.
+ */
+public final class Log {
+
+ /**
+ * Priority constant for the println method; use Log.v.
+ */
+ public static final int VERBOSE = 2;
+
+ /**
+ * Priority constant for the println method; use Log.d.
+ */
+ public static final int DEBUG = 3;
+
+ /**
+ * Priority constant for the println method; use Log.i.
+ */
+ public static final int INFO = 4;
+
+ /**
+ * Priority constant for the println method; use Log.w.
+ */
+ public static final int WARN = 5;
+
+ /**
+ * Priority constant for the println method; use Log.e.
+ */
+ public static final int ERROR = 6;
+
+ /**
+ * Priority constant for the println method.
+ */
+ public static final int ASSERT = 7;
+
+ private Log() {
+ }
+
+ /**
+ * Send a {@link #VERBOSE} log message.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ */
+ public static int v(String tag, String msg) {
+ return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
+ }
+
+ /**
+ * Send a {@link #VERBOSE} log message and log the exception.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ * @param tr An exception to log
+ */
+ public static int v(String tag, String msg, Throwable tr) {
+ return printlns(LOG_ID_MAIN, VERBOSE, tag, msg, tr);
+ }
+
+ /**
+ * Send a {@link #DEBUG} log message.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ */
+ public static int d(String tag, String msg) {
+ return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
+ }
+
+ /**
+ * Send a {@link #DEBUG} log message and log the exception.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ * @param tr An exception to log
+ */
+ public static int d(String tag, String msg, Throwable tr) {
+ return printlns(LOG_ID_MAIN, DEBUG, tag, msg, tr);
+ }
+
+ /**
+ * Send an {@link #INFO} log message.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ */
+ public static int i(String tag, String msg) {
+ return println_native(LOG_ID_MAIN, INFO, tag, msg);
+ }
+
+ /**
+ * Send a {@link #INFO} log message and log the exception.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ * @param tr An exception to log
+ */
+ public static int i(String tag, String msg, Throwable tr) {
+ return printlns(LOG_ID_MAIN, INFO, tag, msg, tr);
+ }
+
+ /**
+ * Send a {@link #WARN} log message.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ */
+ public static int w(String tag, String msg) {
+ return println_native(LOG_ID_MAIN, WARN, tag, msg);
+ }
+
+ /**
+ * Send a {@link #WARN} log message and log the exception.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ * @param tr An exception to log
+ */
+ public static int w(String tag, String msg, Throwable tr) {
+ return printlns(LOG_ID_MAIN, WARN, tag, msg, tr);
+ }
+
+ /**
+ * Checks to see whether or not a log for the specified tag is loggable at the specified level.
+ *
+ * @return true in all cases (for unit test environment)
+ */
+ public static boolean isLoggable(String tag, int level) {
+ return true;
+ }
+
+ /*
+ * Send a {@link #WARN} log message and log the exception.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param tr An exception to log
+ */
+ public static int w(String tag, Throwable tr) {
+ return printlns(LOG_ID_MAIN, WARN, tag, "", tr);
+ }
+
+ /**
+ * Send an {@link #ERROR} log message.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ */
+ public static int e(String tag, String msg) {
+ return println_native(LOG_ID_MAIN, ERROR, tag, msg);
+ }
+
+ /**
+ * Send a {@link #ERROR} log message and log the exception.
+ * @param tag Used to identify the source of a log message. It usually identifies
+ * the class or activity where the log call occurs.
+ * @param msg The message you would like logged.
+ * @param tr An exception to log
+ */
+ public static int e(String tag, String msg, Throwable tr) {
+ return printlns(LOG_ID_MAIN, ERROR, tag, msg, tr);
+ }
+
+ /**
+ * What a Terrible Failure: Report a condition that should never happen.
+ * The error will always be logged at level ASSERT with the call stack.
+ * Depending on system configuration, a report may be added to the
+ * {@link android.os.DropBoxManager} and/or the process may be terminated
+ * immediately with an error dialog.
+ * @param tag Used to identify the source of a log message.
+ * @param msg The message you would like logged.
+ */
+ public static int wtf(String tag, String msg) {
+ return wtf(LOG_ID_MAIN, tag, msg, null, false, false);
+ }
+
+ /**
+ * Like {@link #wtf(String, String)}, but also writes to the log the full
+ * call stack.
+ * @hide
+ */
+ public static int wtfStack(String tag, String msg) {
+ return wtf(LOG_ID_MAIN, tag, msg, null, true, false);
+ }
+
+ /**
+ * What a Terrible Failure: Report an exception that should never happen.
+ * Similar to {@link #wtf(String, String)}, with an exception to log.
+ * @param tag Used to identify the source of a log message.
+ * @param tr An exception to log.
+ */
+ public static int wtf(String tag, Throwable tr) {
+ return wtf(LOG_ID_MAIN, tag, tr.getMessage(), tr, false, false);
+ }
+
+ /**
+ * What a Terrible Failure: Report an exception that should never happen.
+ * Similar to {@link #wtf(String, Throwable)}, with a message as well.
+ * @param tag Used to identify the source of a log message.
+ * @param msg The message you would like logged.
+ * @param tr An exception to log. May be null.
+ */
+ public static int wtf(String tag, String msg, Throwable tr) {
+ return wtf(LOG_ID_MAIN, tag, msg, tr, false, false);
+ }
+
+ /**
+ * Priority Constant for wtf.
+ * Added for this custom Log implementation, not in android sources.
+ */
+ private static final int WTF = 8;
+ static int wtf(int logId, String tag, String msg, Throwable tr, boolean localStack,
+ boolean system) {
+ return printlns(LOG_ID_MAIN, WTF, tag, msg, tr);
+ }
+
+ private static final int LOG_ID_MAIN = 0;
+
+ private static final String[] PRIORITY_ABBREV = { "0", "1", "V", "D", "I", "W", "E", "A", "WTF" };
+
+ private static int println_native(int bufID, int priority, String tag, String msg) {
+ String res = PRIORITY_ABBREV[priority] + "/" + tag + " " + msg + System.lineSeparator();
+ System.err.print(res);
+ return res.length();
+ }
+
+ private static int printlns(int bufID, int priority, String tag, String msg,
+ Throwable tr) {
+ StringWriter trSW = new StringWriter();
+ if (tr != null) {
+ trSW.append(" , Exception: ");
+ PrintWriter trPW = new PrintWriter(trSW);
+ tr.printStackTrace(trPW);
+ trPW.flush();
+ }
+ return println_native(bufID, priority, tag, msg + trSW.toString());
+ }
+
+}
diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedImageMother.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedImageMother.java
new file mode 100644
index 000000000..0fb4992ba
--- /dev/null
+++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedImageMother.java
@@ -0,0 +1,9 @@
+package de.danoeh.antennapod.core.feed;
+
+class FeedImageMother {
+
+ public static FeedImage anyFeedImage() {
+ return new FeedImage(0, "image", null, "http://example.com/picture", false);
+ }
+
+}
diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemMother.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemMother.java
new file mode 100644
index 000000000..3d7c4fe5f
--- /dev/null
+++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemMother.java
@@ -0,0 +1,16 @@
+package de.danoeh.antennapod.core.feed;
+
+import java.util.Date;
+
+import static de.danoeh.antennapod.core.feed.FeedImageMother.anyFeedImage;
+import static de.danoeh.antennapod.core.feed.FeedMother.anyFeed;
+
+class FeedItemMother {
+
+ static FeedItem anyFeedItemWithImage() {
+ FeedItem item = new FeedItem(0, "Item", "Item", "url", new Date(), FeedItem.PLAYED, anyFeed());
+ item.setImage(anyFeedImage());
+ return item;
+ }
+
+}
diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java
new file mode 100644
index 000000000..92aacd9d7
--- /dev/null
+++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java
@@ -0,0 +1,71 @@
+package de.danoeh.antennapod.core.feed;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static de.danoeh.antennapod.core.feed.FeedItemMother.anyFeedItemWithImage;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class FeedItemTest {
+
+ private FeedItem original;
+ private FeedImage originalImage;
+ private FeedItem changedFeedItem;
+
+ @Before
+ public void setUp() {
+ original = anyFeedItemWithImage();
+ originalImage = original.getImage();
+ changedFeedItem = anyFeedItemWithImage();
+ }
+
+ @Test
+ public void testUpdateFromOther_feedItemImageDownloadUrlChanged() throws Exception {
+ setNewFeedItemImageDownloadUrl();
+
+ original.updateFromOther(changedFeedItem);
+
+ feedItemImageWasUpdated();
+ }
+
+ @Test
+ public void testUpdateFromOther_feedItemImageRemoved() throws Exception {
+ feedItemImageRemoved();
+
+ original.updateFromOther(changedFeedItem);
+
+ feedItemImageWasNotUpdated();
+ }
+
+ @Test
+ public void testUpdateFromOther_feedItemImageAdded() throws Exception {
+ feedItemHadNoImage();
+ setNewFeedItemImageDownloadUrl();
+
+ original.updateFromOther(changedFeedItem);
+
+ feedItemImageWasUpdated();
+ }
+
+ private void feedItemHadNoImage() {
+ original.setImage(null);
+ }
+
+ private void setNewFeedItemImageDownloadUrl() {
+ changedFeedItem.getImage().setDownload_url("http://example.com/new_picture");
+ }
+
+ private void feedItemImageRemoved() {
+ changedFeedItem.setImage(null);
+ }
+
+ private void feedItemImageWasUpdated() {
+ assertEquals(original.getImage().getDownload_url(), changedFeedItem.getImage().getDownload_url());
+ }
+
+ private void feedItemImageWasNotUpdated() {
+ assertTrue(originalImage == original.getImage());
+ }
+
+} \ No newline at end of file
diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java
new file mode 100644
index 000000000..fecc8e377
--- /dev/null
+++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java
@@ -0,0 +1,14 @@
+package de.danoeh.antennapod.core.feed;
+
+import static de.danoeh.antennapod.core.feed.FeedImageMother.anyFeedImage;
+
+class FeedMother {
+
+ public static Feed anyFeed() {
+ FeedImage image = anyFeedImage();
+ return new Feed(0, null, "title", "http://example.com", "This is the description",
+ "http://example.com/payment", "Daniel", "en", null, "http://example.com/feed", image,
+ null, "http://example.com/feed", true);
+ }
+
+}
diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java
new file mode 100644
index 000000000..55f3bdafe
--- /dev/null
+++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java
@@ -0,0 +1,102 @@
+package de.danoeh.antennapod.core.feed;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static de.danoeh.antennapod.core.feed.FeedImageMother.anyFeedImage;
+import static de.danoeh.antennapod.core.feed.FeedMother.anyFeed;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class FeedTest {
+
+ private Feed original;
+ private FeedImage originalImage;
+ private Feed changedFeed;
+
+ @Before
+ public void setUp() {
+ original = anyFeed();
+ originalImage = original.getImage();
+ changedFeed = anyFeed();
+ }
+
+ @Test
+ public void testCompareWithOther_feedImageDownloadUrlChanged() throws Exception {
+ setNewFeedImageDownloadUrl();
+
+ feedHasChanged();
+ }
+
+ @Test
+ public void testCompareWithOther_sameFeedImage() throws Exception {
+ changedFeed.setImage(anyFeedImage());
+
+ feedHasNotChanged();
+ }
+
+ @Test
+ public void testCompareWithOther_feedImageRemoved() throws Exception {
+ feedImageRemoved();
+
+ feedHasNotChanged();
+ }
+
+ @Test
+ public void testUpdateFromOther_feedImageDownloadUrlChanged() throws Exception {
+ setNewFeedImageDownloadUrl();
+
+ original.updateFromOther(changedFeed);
+
+ feedImageWasUpdated();
+ }
+
+ @Test
+ public void testUpdateFromOther_feedImageRemoved() throws Exception {
+ feedImageRemoved();
+
+ original.updateFromOther(changedFeed);
+
+ feedImageWasNotUpdated();
+ }
+
+ @Test
+ public void testUpdateFromOther_feedImageAdded() throws Exception {
+ feedHadNoImage();
+ setNewFeedImageDownloadUrl();
+
+ original.updateFromOther(changedFeed);
+
+ feedImageWasUpdated();
+ }
+
+ private void feedHasNotChanged() {
+ assertFalse(original.compareWithOther(changedFeed));
+ }
+
+ private void feedHadNoImage() {
+ original.setImage(null);
+ }
+
+ private void setNewFeedImageDownloadUrl() {
+ changedFeed.getImage().setDownload_url("http://example.com/new_picture");
+ }
+
+ private void feedHasChanged() {
+ assertTrue(original.compareWithOther(changedFeed));
+ }
+
+ private void feedImageRemoved() {
+ changedFeed.setImage(null);
+ }
+
+ private void feedImageWasUpdated() {
+ assertEquals(original.getImage().getDownload_url(), changedFeed.getImage().getDownload_url());
+ }
+
+ private void feedImageWasNotUpdated() {
+ assertTrue(originalImage == original.getImage());
+ }
+
+} \ No newline at end of file
diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/LongLongMapTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/LongLongMapTest.java
new file mode 100644
index 000000000..0ed77eb9f
--- /dev/null
+++ b/core/src/test/java/de/danoeh/antennapod/core/util/LongLongMapTest.java
@@ -0,0 +1,65 @@
+package de.danoeh.antennapod.core.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class LongLongMapTest {
+
+ @Test
+ public void testEmptyMap() {
+ LongIntMap map = new LongIntMap();
+ assertEquals(0, map.size());
+ assertEquals("LongLongMap{}", map.toString());
+ assertEquals(0, map.get(42));
+ assertEquals(-1, map.get(42, -1));
+ assertEquals(false, map.delete(42));
+ assertEquals(-1, map.indexOfKey(42));
+ assertEquals(-1, map.indexOfValue(42));
+ assertEquals(1, map.hashCode());
+ }
+
+ @Test
+ public void testSingleElement() {
+ LongIntMap map = new LongIntMap();
+ map.put(17, 42);
+ assertEquals(1, map.size());
+ assertEquals("LongLongMap{17=42}", map.toString());
+ assertEquals(42, map.get(17));
+ assertEquals(42, map.get(17, -1));
+ assertEquals(0, map.indexOfKey(17));
+ assertEquals(0, map.indexOfValue(42));
+ assertEquals(true, map.delete(17));
+ }
+
+ @Test
+ public void testAddAndDelete() {
+ LongIntMap map = new LongIntMap();
+ for(int i=0; i < 100; i++) {
+ map.put(i * 17, i * 42);
+ }
+ assertEquals(100, map.size());
+ assertEquals(0, map.get(0));
+ assertEquals(42, map.get(17));
+ assertEquals(42, map.get(17, -1));
+ assertEquals(1, map.indexOfKey(17));
+ assertEquals(1, map.indexOfValue(42));
+ for(int i=0; i < 100; i++) {
+ assertEquals(true, map.delete(i * 17));
+ }
+ }
+
+ @Test
+ public void testOverwrite() {
+ LongIntMap map = new LongIntMap();
+ map.put(17, 42);
+ assertEquals(1, map.size());
+ assertEquals("LongLongMap{17=42}", map.toString());
+ assertEquals(42, map.get(17));
+ map.put(17, 23);
+ assertEquals(1, map.size());
+ assertEquals("LongLongMap{17=23}", map.toString());
+ assertEquals(23, map.get(17));
+ }
+
+}