|
| 1 | +package xyz.block.buzz.mobile |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.content.Intent |
| 5 | +import android.net.Uri |
| 6 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 7 | +import androidx.test.platform.app.InstrumentationRegistry |
| 8 | +import io.flutter.plugin.common.BinaryMessenger |
| 9 | +import io.flutter.plugin.common.FlutterException |
| 10 | +import io.flutter.plugin.common.MethodCall |
| 11 | +import io.flutter.plugin.common.StandardMethodCodec |
| 12 | +import java.nio.ByteBuffer |
| 13 | +import org.junit.Test |
| 14 | +import org.junit.runner.RunWith |
| 15 | +import kotlin.test.assertEquals |
| 16 | +import kotlin.test.assertNull |
| 17 | + |
| 18 | +/** Real Android Uri/Intent and registered channel; only browser launch is replaced. */ |
| 19 | +@RunWith(AndroidJUnit4::class) |
| 20 | +class BrowserAuthPluginTest { |
| 21 | + @Test fun closeRetryCallbackAndLateResume() { |
| 22 | + InstrumentationRegistry.getInstrumentation().runOnMainSync { runJourney() } |
| 23 | + } |
| 24 | + |
| 25 | + private fun runJourney() { |
| 26 | + val messenger = Messenger() |
| 27 | + var launches = 0 |
| 28 | + val plugin = BrowserAuthPlugin(Activity(), messenger) { launches++ } |
| 29 | + val first = messenger.start("first") |
| 30 | + plugin.onResume() // Initial resume is not a close. |
| 31 | + assertEquals(0, first.replies) |
| 32 | + plugin.onPause() |
| 33 | + plugin.onResume() |
| 34 | + assertEquals("cancelled", first.error) |
| 35 | + assertEquals(1, first.replies) |
| 36 | + |
| 37 | + val second = messenger.start("second") |
| 38 | + plugin.onPause() |
| 39 | + plugin.onNewIntent(Intent(Intent.ACTION_VIEW, Uri.parse( |
| 40 | + "xyz.block.buzz.mobile:/auth/callback?state=first&code=old", |
| 41 | + ))) |
| 42 | + assertEquals(0, second.replies) |
| 43 | + val callback = "xyz.block.buzz.mobile:/auth/callback?state=second&code=code" |
| 44 | + plugin.onNewIntent(Intent(Intent.ACTION_VIEW, Uri.parse(callback))) |
| 45 | + plugin.onResume() |
| 46 | + assertEquals(callback, second.value) |
| 47 | + assertNull(second.error) |
| 48 | + assertEquals(1, second.replies) |
| 49 | + assertEquals(2, launches) |
| 50 | + |
| 51 | + val third = messenger.start("third") |
| 52 | + messenger.call("cancel", null) |
| 53 | + plugin.onPause() |
| 54 | + plugin.onResume() |
| 55 | + assertEquals("cancelled", third.error) |
| 56 | + assertEquals(1, third.replies) |
| 57 | + plugin.dispose() |
| 58 | + assertNull(messenger.handler) |
| 59 | + } |
| 60 | + |
| 61 | + private class Reply { |
| 62 | + var replies = 0 |
| 63 | + var value: Any? = null |
| 64 | + var error: String? = null |
| 65 | + } |
| 66 | + |
| 67 | + private class Messenger : BinaryMessenger { |
| 68 | + var handler: BinaryMessenger.BinaryMessageHandler? = null |
| 69 | + fun start(state: String) = call("start", mapOf( |
| 70 | + "url" to "https://app.builderlab.xyz/api/goose/v1/auth/login?state=$state", |
| 71 | + )) |
| 72 | + fun call(method: String, arguments: Any?): Reply { |
| 73 | + val reply = Reply() |
| 74 | + val message = StandardMethodCodec.INSTANCE.encodeMethodCall(MethodCall(method, arguments)) |
| 75 | + message.flip() |
| 76 | + handler!!.onMessage(message) { response -> |
| 77 | + reply.replies++ |
| 78 | + response!!.flip() |
| 79 | + try { |
| 80 | + reply.value = StandardMethodCodec.INSTANCE.decodeEnvelope(response) |
| 81 | + } catch (error: FlutterException) { |
| 82 | + reply.error = error.code |
| 83 | + } |
| 84 | + } |
| 85 | + return reply |
| 86 | + } |
| 87 | + override fun send(channel: String, message: ByteBuffer?) = Unit |
| 88 | + override fun send(channel: String, message: ByteBuffer?, callback: BinaryMessenger.BinaryReply?) = Unit |
| 89 | + override fun setMessageHandler(channel: String, handler: BinaryMessenger.BinaryMessageHandler?) { |
| 90 | + assertEquals("buzz/browser_auth", channel) |
| 91 | + this.handler = handler |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments