Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Pass on browser-engine-*
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoryk committed Feb 15, 2022
1 parent 928d658 commit bd38d9a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package mozilla.components.browser.engine.gecko

import android.util.Log
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
Expand All @@ -21,13 +22,19 @@ import kotlin.coroutines.suspendCoroutine
* Wait for a GeckoResult to be complete in a co-routine.
*/
suspend fun <T> GeckoResult<T>.await() = suspendCoroutine<T?> { continuation ->
Log.w("gri", "2.1")
Log.w("gri", "looper: $looper")
then(
{
Log.w("gri", "2.2")
continuation.resume(it)
Log.w("gri", "2.3")
GeckoResult<Void>()
},
{
Log.w("gri", "2.4")
continuation.resumeWithException(it)
Log.w("gri", "2.5")
GeckoResult<Void>()
}
)
Expand Down Expand Up @@ -65,11 +72,17 @@ fun <T> CoroutineScope.launchGeckoResult(
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T
) = GeckoResult<T>().apply {
Log.w("gri", "1.1")
launch(context, start) {
Log.w("gri", "1.2")
try {
Log.w("gri", "1.3")
val value = block()
Log.w("gri", "1.4")
complete(value)
Log.w("gri", "1.5")
} catch (exception: Throwable) {
Log.w("gri", "1.6")
completeExceptionally(exception)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import android.os.Message
import android.view.WindowManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import mozilla.components.browser.engine.gecko.ext.geckoTrackingProtectionPermission
import mozilla.components.browser.engine.gecko.ext.isExcludedForTrackingProtection
import mozilla.components.browser.engine.gecko.permission.geckoContentPermission
Expand Down Expand Up @@ -772,7 +773,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `notifies configured history delegate of title changes`() = runBlockingTest {
fun `notifies configured history delegate of title changes`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
runtime, geckoSessionProvider = geckoSessionProvider,
context = coroutineContext
Expand All @@ -799,7 +800,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `does not notify configured history delegate of title changes for private sessions`() = runBlockingTest {
fun `does not notify configured history delegate of title changes for private sessions`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand Down Expand Up @@ -831,7 +832,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `notifies configured history delegate of preview image URL changes`() = runBlockingTest {
fun `notifies configured history delegate of preview image URL changes`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
runtime, geckoSessionProvider = geckoSessionProvider,
context = coroutineContext
Expand Down Expand Up @@ -862,7 +863,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `does not notify configured history delegate of preview image URL changes for private sessions`() = runBlockingTest {
fun `does not notify configured history delegate of preview image URL changes for private sessions`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand Down Expand Up @@ -894,7 +895,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `does not notify configured history delegate for top-level visits to error pages`() = runBlockingTest {
fun `does not notify configured history delegate for top-level visits to error pages`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand All @@ -917,7 +918,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `notifies configured history delegate of visits`() = runBlockingTest {
fun `notifies configured history delegate of visits`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand All @@ -936,7 +937,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `notifies configured history delegate of reloads`() = runBlockingTest {
fun `notifies configured history delegate of reloads`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand All @@ -955,7 +956,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `checks with the delegate before trying to record a visit`() = runBlockingTest {
fun `checks with the delegate before trying to record a visit`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand Down Expand Up @@ -983,7 +984,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `correctly processes redirect visit flags`() = runBlockingTest {
fun `correctly processes redirect visit flags`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand Down Expand Up @@ -1045,7 +1046,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `does not notify configured history delegate of visits for private sessions`() = runBlockingTest {
fun `does not notify configured history delegate of visits for private sessions`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand All @@ -1064,7 +1065,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `requests visited URLs from configured history delegate`() = runBlockingTest {
fun `requests visited URLs from configured history delegate`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand All @@ -1086,7 +1087,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `does not request visited URLs from configured history delegate in private sessions`() = runBlockingTest {
fun `does not request visited URLs from configured history delegate in private sessions`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand All @@ -1105,7 +1106,7 @@ class GeckoEngineSessionTest {
}

@Test
fun `notifies configured history delegate of state changes`() = runBlockingTest {
fun `notifies configured history delegate of state changes`() = runTest(UnconfinedTestDispatcher()) {
val engineSession = GeckoEngineSession(
mock(),
geckoSessionProvider = geckoSessionProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,77 @@

package mozilla.components.browser.engine.gecko

import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.*
import mozilla.components.support.test.mock
import mozilla.components.support.test.whenever
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.gecko.util.ThreadUtils
import org.mozilla.geckoview.GeckoResult

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class GeckoResultTest {

private val dispatcher = StandardTestDispatcher()

@Test
fun awaitWithResult() = runBlockingTest {
fun awaitWithResult() = runTest(UnconfinedTestDispatcher()) {
val result = GeckoResult.fromValue(42).await()
assertEquals(42, result)
}

@Test(expected = IllegalStateException::class)
fun awaitWithException() = runBlockingTest {
fun awaitWithException() = runTest(UnconfinedTestDispatcher()) {
GeckoResult.fromException<Unit>(IllegalStateException()).await()
}

@Test
fun fromResult() = runBlockingTest {
val result = launchGeckoResult { 42 }
fun fromResult() = runTest(dispatcher) {
Dispatchers.setMain(dispatcher)
Log.w("gri", "1")
if (ThreadUtils.isOnUiThread()) {
Log.w("gri", "isOnUiThread")
}
val result = launchGeckoResult { Log.w("gri", "2"); 42 }
Log.w("gri", "3")

result.then<Int> {
Log.w("gri", "4")
assertEquals(42, it)
GeckoResult.fromValue(null)
}.await()
Log.w("gri", "5")
Dispatchers.resetMain()
}

@Test
fun fromException() = runBlockingTest {
val result = launchGeckoResult { throw IllegalStateException() }

fun fromException() = runTest(UnconfinedTestDispatcher()) {
Log.w("gri", "1")
val result = this.launchGeckoResult { Log.w("gri", "2"); throw IllegalStateException() }
Log.w("gri", "3")
result.then<Unit>(
{
Log.w("gri", "4")
assertTrue("Invalid branch", false)
GeckoResult.fromValue(null)
},
{
Log.w("gri", "5")
assertTrue(it is IllegalStateException)
GeckoResult.fromValue(null)
}
).await()
Log.w("gri", "6")
}

@Test
fun asCancellableOperation() = runBlockingTest {
fun asCancellableOperation() = runTest(UnconfinedTestDispatcher()) {
val geckoResult: GeckoResult<Int> = mock()
val op = geckoResult.asCancellableOperation()

Expand Down
Loading

0 comments on commit bd38d9a

Please sign in to comment.