-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(android): setup native unit tests
- Loading branch information
Showing
6 changed files
with
251 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...android/src/test/kotlin/dev/flutterquill/quill_native_bridge/FlutterPigeonResponseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dev.flutterquill.quill_native_bridge | ||
|
||
import dev.flutterquill.quill_native_bridge.generated.FlutterError | ||
import dev.flutterquill.quill_native_bridge.util.respondFlutterPigeonError | ||
import dev.flutterquill.quill_native_bridge.util.respondFlutterPigeonSuccess | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertTrue | ||
|
||
class FlutterPigeonResponseTest { | ||
@Test | ||
fun `respondFlutterPigeonSuccess invokes with success result correctly`() { | ||
var invoked = false | ||
val messageInput = "Hello, World!" | ||
|
||
val callback: (Result<String>) -> Unit = { | ||
invoked = true | ||
|
||
assertTrue(it.isSuccess) | ||
assertEquals(messageInput, it.getOrNull()) | ||
} | ||
|
||
callback.respondFlutterPigeonSuccess(messageInput) | ||
|
||
assertTrue(invoked) | ||
} | ||
|
||
@Test | ||
fun `respondFlutterPigeonError invokes with error result correctly`() { | ||
var invoked = false | ||
val flutterPigeonError = FlutterError("EXAMPLE_CODE", "Example message", "Example Details") | ||
|
||
val callback: (Result<FlutterError>) -> Unit = { | ||
invoked = true | ||
|
||
assertTrue(it.isFailure) | ||
|
||
val capturedFlutterPigeonError = it.exceptionOrNull() | ||
assertIs<FlutterError>(capturedFlutterPigeonError) | ||
|
||
assertEquals(flutterPigeonError.code, capturedFlutterPigeonError.code) | ||
assertEquals(flutterPigeonError.message, capturedFlutterPigeonError.message) | ||
assertEquals(flutterPigeonError.details, capturedFlutterPigeonError.details) | ||
} | ||
|
||
callback.respondFlutterPigeonError( | ||
code = flutterPigeonError.code, | ||
message = flutterPigeonError.message, | ||
details = flutterPigeonError.details, | ||
) | ||
|
||
assertTrue(invoked) | ||
} | ||
} |
163 changes: 163 additions & 0 deletions
163
...droid/src/test/kotlin/dev/flutterquill/quill_native_bridge/QuillNativeBridgePluginTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package dev.flutterquill.quill_native_bridge | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding | ||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding | ||
import io.flutter.plugin.common.BinaryMessenger | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.times | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.verifyNoMoreInteractions | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
class QuillNativeBridgePluginTest { | ||
|
||
private lateinit var mockPluginApi: QuillNativeBridgeImpl | ||
private lateinit var mockApplication: Application | ||
private lateinit var mockActivity: Activity | ||
|
||
private lateinit var mockFlutterPluginBinding: FlutterPluginBinding | ||
private lateinit var mockActivityBinding: ActivityPluginBinding | ||
private lateinit var mockBinaryMessenger: BinaryMessenger | ||
|
||
private lateinit var plugin: QuillNativeBridgePlugin | ||
|
||
@BeforeTest | ||
fun setup() { | ||
mockPluginApi = mock() | ||
mockApplication = mock() | ||
mockActivity = mock() | ||
mockBinaryMessenger = mock() | ||
mockFlutterPluginBinding = mock { | ||
on { applicationContext }.thenReturn(mockApplication) | ||
on { binaryMessenger }.thenReturn(mockBinaryMessenger) | ||
} | ||
mockActivityBinding = mock { on { activity }.thenReturn(mockActivity) } | ||
|
||
plugin = QuillNativeBridgePlugin() | ||
} | ||
|
||
@Test | ||
fun `The log tag is correct`() { | ||
assertEquals("QuillNativeBridgePlugin", QuillNativeBridgePlugin.TAG) | ||
} | ||
|
||
@Test | ||
fun `onAttachedToEngine sets up the plugin API`() { | ||
assertNull(plugin.pluginApi, "The plugin API is null initially") | ||
|
||
plugin.onAttachedToEngine(mockFlutterPluginBinding) | ||
|
||
verify(mockFlutterPluginBinding).binaryMessenger | ||
verify(mockFlutterPluginBinding).applicationContext | ||
verifyNoMoreInteractions(mockFlutterPluginBinding) | ||
|
||
assertNotNull(plugin.pluginApi, "The plugin API should be not null after the attach") | ||
} | ||
|
||
@Test | ||
fun `onDetachedFromEngine tears down the plugin API`() { | ||
plugin.onAttachedToEngine(mockFlutterPluginBinding) | ||
plugin.onDetachedFromEngine(mockFlutterPluginBinding) | ||
|
||
verify(mockFlutterPluginBinding, times(2)).binaryMessenger | ||
verify(mockFlutterPluginBinding).applicationContext | ||
verifyNoMoreInteractions(mockFlutterPluginBinding) | ||
|
||
assertNull(plugin.pluginApi, "The plugin API be null after the detach") | ||
} | ||
|
||
@Test | ||
fun `setActivityPluginBinding updates ActivityPluginBinding reference correctly`() { | ||
plugin.pluginApi = mockPluginApi | ||
|
||
assertNotNull(plugin.pluginApi, "The plugin API expected to be not null") | ||
assertNull(plugin.activityPluginBinding, "The activity plugin binding is null initially") | ||
|
||
val input = mock<ActivityPluginBinding>() | ||
|
||
plugin.setActivityPluginBinding(input, "Any") | ||
|
||
assertNotNull(plugin.activityPluginBinding) | ||
assertEquals(input, plugin.activityPluginBinding) | ||
|
||
verify(mockPluginApi).setActivityPluginBinding(input) | ||
verifyNoMoreInteractions(mockPluginApi) | ||
} | ||
|
||
@Test | ||
fun `disposeActivityPluginBinding updates ActivityPluginBinding reference to null`() { | ||
plugin.pluginApi = mockPluginApi | ||
|
||
assertNotNull(plugin.pluginApi, "The plugin API expected to be not null") | ||
assertNull(plugin.activityPluginBinding, "The activity plugin binding is null initially") | ||
|
||
plugin.setActivityPluginBinding(mock(), "Any") | ||
assertNotNull(plugin.activityPluginBinding) | ||
|
||
verify(mockPluginApi).setActivityPluginBinding(any()) | ||
|
||
plugin.disposeActivityPluginBinding("Any") | ||
|
||
assertNull(plugin.activityPluginBinding) | ||
|
||
verify(mockPluginApi).setActivityPluginBinding(null) | ||
verifyNoMoreInteractions(mockPluginApi) | ||
} | ||
|
||
@Test | ||
fun `onAttachedToActivity updates ActivityPluginBinding reference correctly`() { | ||
plugin.pluginApi = mockPluginApi | ||
|
||
val input = mock<ActivityPluginBinding>() | ||
|
||
plugin.onAttachedToActivity(input) | ||
|
||
assertNotNull(plugin.activityPluginBinding) | ||
assertEquals(input, plugin.activityPluginBinding) | ||
} | ||
|
||
@Test | ||
fun `onDetachedFromActivityForConfigChanges updates ActivityPluginBinding reference to null`() { | ||
plugin.activityPluginBinding = mock() | ||
|
||
assertNotNull(plugin.activityPluginBinding) | ||
|
||
plugin.pluginApi = mockPluginApi | ||
|
||
plugin.onDetachedFromActivityForConfigChanges() | ||
|
||
assertNull(plugin.activityPluginBinding) | ||
} | ||
|
||
@Test | ||
fun `onReattachedToActivityForConfigChanges updates ActivityPluginBinding reference correctly`() { | ||
plugin.pluginApi = mockPluginApi | ||
|
||
val input = mock<ActivityPluginBinding>() | ||
|
||
plugin.onReattachedToActivityForConfigChanges(input) | ||
|
||
assertNotNull(plugin.activityPluginBinding) | ||
assertEquals(input, plugin.activityPluginBinding) | ||
} | ||
|
||
@Test | ||
fun `onDetachedFromActivity updates ActivityPluginBinding reference to null`() { | ||
plugin.activityPluginBinding = mock() | ||
|
||
assertNotNull(plugin.activityPluginBinding) | ||
|
||
plugin.pluginApi = mockPluginApi | ||
|
||
plugin.onDetachedFromActivity() | ||
|
||
assertNull(plugin.activityPluginBinding) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters