From d96d1d06a7c1581b97c042f2a74e7cd5b1c2546e Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 17 Apr 2021 17:49:18 -0700 Subject: Update theme providers and readd koin modules --- .../com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt') 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..6f4cb90a 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. @@ -247,7 +247,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") -- cgit v1.2.3 From 41b6765da8fb0c05dff8b339ca23b6a3d39839d9 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 18 Apr 2021 17:58:23 -0700 Subject: Fix coroutine tests --- app/build.gradle | 4 +-- .../kotlin/com/pitchedapps/frost/FrostTestApp.kt | 41 ++-------------------- .../kotlin/com/pitchedapps/frost/TestModules.kt | 19 ++++++++++ .../frost/debugger/OfflineWebsiteTest.kt | 7 ++-- .../com/pitchedapps/frost/utils/CoroutineTest.kt | 30 ++++++++++------ 5 files changed, 47 insertions(+), 54 deletions(-) create mode 100644 app/src/androidTest/kotlin/com/pitchedapps/frost/TestModules.kt (limited to 'app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt') diff --git a/app/build.gradle b/app/build.gradle index 6172f54b..1046d52f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -276,9 +276,9 @@ dependencies { implementation kau.Dependencies.hilt kapt kau.Dependencies.hiltCompiler - testImplementation kau.Dependencies.hilt + testImplementation kau.Dependencies.hiltTest kaptTest kau.Dependencies.hiltCompiler - androidTestImplementation kau.Dependencies.hilt + androidTestImplementation kau.Dependencies.hiltTest kaptAndroidTest kau.Dependencies.hiltCompiler implementation kau.Dependencies.coroutines diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/FrostTestApp.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/FrostTestApp.kt index 9d0caae6..2a834cc8 100644 --- a/app/src/androidTest/kotlin/com/pitchedapps/frost/FrostTestApp.kt +++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/FrostTestApp.kt @@ -19,22 +19,13 @@ package com.pitchedapps.frost import android.app.Application import android.content.Context import androidx.test.runner.AndroidJUnitRunner -import ca.allanwang.kau.kpref.KPrefFactory -import ca.allanwang.kau.kpref.KPrefFactoryInMemory -import com.pitchedapps.frost.db.FrostDatabase -import com.pitchedapps.frost.facebook.FbCookie -import com.pitchedapps.frost.injectors.ThemeProvider import com.pitchedapps.frost.prefs.Prefs +import dagger.hilt.android.testing.HiltTestApplication import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement -import org.koin.android.ext.koin.androidContext -import org.koin.android.ext.koin.androidLogger import org.koin.core.component.KoinComponent import org.koin.core.component.get -import org.koin.core.context.startKoin -import org.koin.core.module.Module -import org.koin.dsl.module class FrostTestRunner : AndroidJUnitRunner() { override fun newApplication( @@ -42,7 +33,7 @@ class FrostTestRunner : AndroidJUnitRunner() { className: String?, context: Context? ): Application { - return super.newApplication(cl, FrostTestApp::class.java.name, context) + return super.newApplication(cl, HiltTestApplication::class.java.name, context) } } @@ -58,31 +49,3 @@ class FrostTestRule : TestRule { } } } - -class FrostTestApp : Application() { - - override fun onCreate() { - super.onCreate() - startKoin { - androidLogger() - androidContext(this@FrostTestApp) - modules( - listOf( - FrostDatabase.module(), - prefFactoryModule(), - Prefs.module(), - FbCookie.module(), - ThemeProvider.module() - ) - ) - } - } - - companion object { - fun prefFactoryModule(): Module = module { - single { - KPrefFactoryInMemory - } - } - } -} diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/TestModules.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/TestModules.kt new file mode 100644 index 00000000..3d43f1b1 --- /dev/null +++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/TestModules.kt @@ -0,0 +1,19 @@ +package com.pitchedapps.frost + +import ca.allanwang.kau.kpref.KPrefFactory +import ca.allanwang.kau.kpref.KPrefFactoryInMemory +import com.pitchedapps.frost.prefs.PrefFactoryModule +import dagger.Module +import dagger.Provides +import dagger.hilt.components.SingletonComponent +import dagger.hilt.testing.TestInstallIn + +@Module +@TestInstallIn( + components = [SingletonComponent::class], + replaces = [PrefFactoryModule::class] +) +object PrefFactoryTestModule { + @Provides + fun factory(): KPrefFactory = KPrefFactoryInMemory +} 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 6f4cb90a..23a365f4 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt @@ -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) 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 055d70ee..2744d0d8 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt @@ -26,8 +26,15 @@ 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 @@ -67,6 +74,7 @@ class CoroutineTest { ): List = withContext(Dispatchers.IO) { val data = mutableListOf() + channel.receiveAsFlow() for (c in channel) { data.add(c) if (shouldEnd(c)) break @@ -118,6 +126,9 @@ class CoroutineTest { } } + private fun SharedFlow.takeUntilNull(): Flow = + 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(100) - + val flow = MutableSharedFlow(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(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) } -- cgit v1.2.3 From c1d34aeaeeb2f98d30b04d32fe57c572c4d949ee Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 18 Apr 2021 18:06:58 -0700 Subject: Fix lint --- .../kotlin/com/pitchedapps/frost/TestModules.kt | 16 ++++++++++++++++ .../com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) (limited to 'app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt') diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/TestModules.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/TestModules.kt index 3d43f1b1..176db811 100644 --- a/app/src/androidTest/kotlin/com/pitchedapps/frost/TestModules.kt +++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/TestModules.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2021 Allan Wang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.pitchedapps.frost import ca.allanwang.kau.kpref.KPrefFactory 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 23a365f4..48587ea9 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/debugger/OfflineWebsiteTest.kt @@ -46,7 +46,7 @@ class OfflineWebsiteTest { fun before() { val buildPath = if (File("").absoluteFile.name == "app") "build/offline_test" else "app/build/offline_test" - val rootDir = File(buildPath) + val rootDir = File(buildPath) rootDir.deleteRecursively() baseDir = rootDir.resolve(System.currentTimeMillis().toString()) server = MockWebServer() -- cgit v1.2.3