diff options
author | Allan Wang <me@allanwang.ca> | 2021-04-18 18:30:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-18 18:30:03 -0700 |
commit | bf55dbf9b46cd8826987ee6e556307e24f3d46cc (patch) | |
tree | aaca512641e6e4503859c4cb97a29a9296669f9d /app/src/test/kotlin/com | |
parent | 88a5d17814ff9f7c44f79216510d405f057ae454 (diff) | |
parent | 997362de5aab00f8d258b6fe8a058a8b3e018694 (diff) | |
download | frost-bf55dbf9b46cd8826987ee6e556307e24f3d46cc.tar.gz frost-bf55dbf9b46cd8826987ee6e556307e24f3d46cc.tar.bz2 frost-bf55dbf9b46cd8826987ee6e556307e24f3d46cc.zip |
Merge pull request #1770 from AllanWang/hilt
Diffstat (limited to 'app/src/test/kotlin/com')
14 files changed, 56 insertions, 44 deletions
diff --git a/app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt index 4064a5ff..48587ea9 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt @@ -18,6 +18,12 @@ package com.pitchedapps.frost.debugger import com.pitchedapps.frost.facebook.FB_URL_BASE import com.pitchedapps.frost.internal.COOKIE +import kotlinx.coroutines.runBlocking +import okhttp3.mockwebserver.Dispatcher +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest +import org.junit.Assume.assumeTrue import java.io.File import java.util.zip.ZipFile import kotlin.test.AfterTest @@ -27,12 +33,6 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertTrue -import kotlinx.coroutines.runBlocking -import okhttp3.mockwebserver.Dispatcher -import okhttp3.mockwebserver.MockResponse -import okhttp3.mockwebserver.MockWebServer -import okhttp3.mockwebserver.RecordedRequest -import org.junit.Assume.assumeTrue /** * Created by Allan Wang on 05/01/18. @@ -46,8 +46,9 @@ class OfflineWebsiteTest { fun before() { val buildPath = if (File("").absoluteFile.name == "app") "build/offline_test" else "app/build/offline_test" - baseDir = File(buildPath) - assertTrue(baseDir.deleteRecursively(), "Failed to clean base dir") + val rootDir = File(buildPath) + rootDir.deleteRecursively() + baseDir = rootDir.resolve(System.currentTimeMillis().toString()) server = MockWebServer() server.start() } @@ -61,7 +62,7 @@ class OfflineWebsiteTest { url: String = server.url("/").toString(), cookie: String = "" ): ZipFile { - val name = "test${System.currentTimeMillis()}" + val name = "test" runBlocking { val success = OfflineWebsite(url, cookie, baseDir = baseDir) .loadAndZip(name) @@ -247,7 +248,8 @@ class OfflineWebsiteTest { assertEquals(5, zip.size(), "2 files expected") zip.assertContentEquals( - "index.html", content + "index.html", + content .replace(css1Url.toString(), "assets/a0_1.css") .replace(css2Url.toString(), "assets/a1_2.css") .replace(js1Url.toString(), "assets/a2_1.js.txt") diff --git a/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbDomTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbDomTest.kt index 544d904f..9472adfe 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbDomTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbDomTest.kt @@ -18,9 +18,9 @@ package com.pitchedapps.frost.facebook import com.pitchedapps.frost.internal.authDependent import com.pitchedapps.frost.internal.testJsoup -import kotlin.test.assertNotNull import org.junit.BeforeClass import org.junit.Test +import kotlin.test.assertNotNull class FbDomTest { diff --git a/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbRegexTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbRegexTest.kt index 08957ea9..d37e55c1 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbRegexTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbRegexTest.kt @@ -16,9 +16,9 @@ */ package com.pitchedapps.frost.facebook -import kotlin.test.assertEquals import org.apache.commons.text.StringEscapeUtils import org.junit.Test +import kotlin.test.assertEquals /** * Created by Allan Wang on 24/12/17. diff --git a/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbUrlTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbUrlTest.kt index 433fdb2a..558cffa1 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbUrlTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/facebook/FbUrlTest.kt @@ -18,10 +18,10 @@ package com.pitchedapps.frost.facebook import com.pitchedapps.frost.utils.isImageUrl import com.pitchedapps.frost.utils.isIndirectImageUrl +import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue -import org.junit.Test /** * Created by Allan Wang on 2017-07-07. diff --git a/app/src/test/kotlin/com/pitchedapps/frost/facebook/parsers/FbParseTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/facebook/parsers/FbParseTest.kt index e3b449c0..a5955101 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/facebook/parsers/FbParseTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/facebook/parsers/FbParseTest.kt @@ -20,12 +20,12 @@ import com.pitchedapps.frost.internal.COOKIE import com.pitchedapps.frost.internal.assertComponentsNotEmpty import com.pitchedapps.frost.internal.assertDescending import com.pitchedapps.frost.internal.authDependent +import org.junit.BeforeClass +import org.junit.Test import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertTrue import kotlin.test.fail -import org.junit.BeforeClass -import org.junit.Test /** * Created by Allan Wang on 24/12/17. diff --git a/app/src/test/kotlin/com/pitchedapps/frost/facebook/requests/FbFullImageTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/facebook/requests/FbFullImageTest.kt index 64a9fe31..cb8dd5e1 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/facebook/requests/FbFullImageTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/facebook/requests/FbFullImageTest.kt @@ -18,10 +18,10 @@ package com.pitchedapps.frost.facebook.requests import com.pitchedapps.frost.internal.COOKIE import com.pitchedapps.frost.internal.authDependent -import kotlin.test.assertNotNull import kotlinx.coroutines.runBlocking import org.junit.BeforeClass import org.junit.Test +import kotlin.test.assertNotNull /** * Created by Allan Wang on 12/04/18. diff --git a/app/src/test/kotlin/com/pitchedapps/frost/internal/Internal.kt b/app/src/test/kotlin/com/pitchedapps/frost/internal/Internal.kt index 17ce847c..7c12ba73 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/internal/Internal.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/internal/Internal.kt @@ -20,6 +20,7 @@ import com.pitchedapps.frost.facebook.FB_USER_MATCHER import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.facebook.get import com.pitchedapps.frost.utils.frostJsoup +import org.junit.Assume import java.io.File import java.io.FileInputStream import java.util.Properties @@ -27,7 +28,6 @@ import kotlin.reflect.full.starProjectedType import kotlin.test.assertEquals import kotlin.test.assertTrue import kotlin.test.fail -import org.junit.Assume /** * Created by Allan Wang on 21/12/17. diff --git a/app/src/test/kotlin/com/pitchedapps/frost/kotlin/FlyweightTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/kotlin/FlyweightTest.kt index 20cdd5ec..89289322 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/kotlin/FlyweightTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/kotlin/FlyweightTest.kt @@ -16,6 +16,11 @@ */ package com.pitchedapps.frost.kotlin +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.runBlocking +import org.junit.Rule +import org.junit.rules.Timeout import java.util.concurrent.atomic.AtomicInteger import kotlin.test.BeforeTest import kotlin.test.Test @@ -23,11 +28,6 @@ import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue import kotlin.test.fail -import kotlinx.coroutines.CancellationException -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.runBlocking -import org.junit.Rule -import org.junit.rules.Timeout class FlyweightTest { diff --git a/app/src/test/kotlin/com/pitchedapps/frost/prefs/PrefsTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/prefs/PrefsTest.kt index 28c0c899..a51dc460 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/prefs/PrefsTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/prefs/PrefsTest.kt @@ -16,9 +16,9 @@ */ package com.pitchedapps.frost.prefs -import kotlin.test.assertEquals import org.junit.Before import org.junit.Test +import kotlin.test.assertEquals /** * Created by Allan Wang on 2017-05-31. diff --git a/app/src/test/kotlin/com/pitchedapps/frost/utils/BuildUtilsTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/utils/BuildUtilsTest.kt index 596693e7..a40372af 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/utils/BuildUtilsTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/utils/BuildUtilsTest.kt @@ -17,10 +17,10 @@ package com.pitchedapps.frost.utils import com.pitchedapps.frost.BuildConfig +import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull -import org.junit.Test class BuildUtilsTest { diff --git a/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt index cb427faa..2744d0d8 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt @@ -17,13 +17,6 @@ package com.pitchedapps.frost.utils import com.pitchedapps.frost.kotlin.Flyweight -import java.util.concurrent.Executors -import kotlin.coroutines.EmptyCoroutineContext -import kotlin.test.Ignore -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFalse -import kotlin.test.assertTrue import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.GlobalScope @@ -33,12 +26,26 @@ import kotlinx.coroutines.async import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.ReceiveChannel -import kotlinx.coroutines.channels.count import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.count +import kotlinx.coroutines.flow.filterNotNull +import kotlinx.coroutines.flow.receiveAsFlow +import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext +import java.util.concurrent.Executors +import kotlin.coroutines.EmptyCoroutineContext +import kotlin.test.Ignore +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue /** * Collection of tests around coroutines @@ -67,6 +74,7 @@ class CoroutineTest { ): List<T> = withContext(Dispatchers.IO) { val data = mutableListOf<T>() + channel.receiveAsFlow() for (c in channel) { data.add(c) if (shouldEnd(c)) break @@ -118,6 +126,9 @@ class CoroutineTest { } } + private fun <T : Any> SharedFlow<T?>.takeUntilNull(): Flow<T> = + takeWhile { it != null }.filterNotNull() + /** * Sanity check to ensure that contexts are being honoured */ @@ -128,13 +139,10 @@ class CoroutineTest { Thread(r, mainTag) }.asCoroutineDispatcher() - val channel = BroadcastChannel<String>(100) - + val flow = MutableSharedFlow<String?>(100) runBlocking(Dispatchers.IO) { - val receiver1 = channel.openSubscription() - val receiver2 = channel.openSubscription() launch(mainDispatcher) { - for (thread in receiver1) { + flow.takeUntilNull().collect { thread -> assertTrue( Thread.currentThread().name.startsWith(mainTag), "Channel should be received in main thread" @@ -146,10 +154,11 @@ class CoroutineTest { } } listOf(EmptyCoroutineContext, Dispatchers.IO, Dispatchers.Default, Dispatchers.IO).map { - async(it) { channel.send(Thread.currentThread().name) } + async(it) { flow.emit(Thread.currentThread().name) } }.joinAll() - channel.close() - assertEquals(4, receiver2.count(), "Not all events received") + flow.emit(null) + val count = flow.takeUntilNull().count() + assertEquals(4, count, "Not all events received") } } @@ -159,6 +168,7 @@ class CoroutineTest { * Events should be consumed when there is no pending consumer on previous elements. */ @Test + @Ignore("Move to flow") fun throttledChannel() { val channel = Channel<Int>(Channel.CONFLATED) runBlocking { @@ -177,7 +187,7 @@ class CoroutineTest { val received = deferred.await() assertTrue( received.size < 20, - "Received data should be throttled; expected that around 1/10th of all events are consumed" + "Received data should be throttled; expected that around 1/10th of all events are consumed, but received ${received.size}" ) println(received) } diff --git a/app/src/test/kotlin/com/pitchedapps/frost/utils/JsoupCleanerTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/utils/JsoupCleanerTest.kt index 7c277dc7..7ec01e6d 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/utils/JsoupCleanerTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/utils/JsoupCleanerTest.kt @@ -16,8 +16,8 @@ */ package com.pitchedapps.frost.utils -import kotlin.test.assertEquals import org.junit.Test +import kotlin.test.assertEquals /** * Created by Allan Wang on 2017-08-10. diff --git a/app/src/test/kotlin/com/pitchedapps/frost/utils/StringEscapeUtilsTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/utils/StringEscapeUtilsTest.kt index 408dd214..f5ea56ae 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/utils/StringEscapeUtilsTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/utils/StringEscapeUtilsTest.kt @@ -16,8 +16,8 @@ */ package com.pitchedapps.frost.utils -import kotlin.test.assertEquals import org.junit.Test +import kotlin.test.assertEquals /** * Created by Allan Wang on 11/03/18. diff --git a/app/src/test/kotlin/com/pitchedapps/frost/utils/UrlTests.kt b/app/src/test/kotlin/com/pitchedapps/frost/utils/UrlTests.kt index ac6068ef..485d0a7a 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/utils/UrlTests.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/utils/UrlTests.kt @@ -17,10 +17,10 @@ package com.pitchedapps.frost.utils import com.pitchedapps.frost.facebook.FACEBOOK_COM +import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue -import org.junit.Test /** * Created by Allan Wang on 2017-11-15. |