-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bluetooth/Usb automatic reconnection (#516)
* Add ReaderReconnectionListener in sdk * Support iOS Bluetooth automatic reconnection. * Fix type * Add missing import. * Add auto-reconnect toggle for QA testers. * Fix syntax errors on iOS. * Remove unnecessary parameter in reconnecting callbacks. Update reader status while reconnecting fail. * Fix e2e failing in auto-reconnect testing. * Rename autoReconnect to autoReconnectOnUnexpectedDisconnect. * Code optimization , expose cancelReaderReconnection method ; Add test for RNBluetoothReaderListener; * Simplified BluetoothConnectionConfiguration/UsbConnectionConfiguration * Add emit event on reconnection callbacks.
- Loading branch information
1 parent
1870117
commit 22fe81a
Showing
13 changed files
with
309 additions
and
16 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
39 changes: 39 additions & 0 deletions
39
android/src/main/java/com/stripeterminalreactnative/listener/RNReaderReconnectionListener.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,39 @@ | ||
package com.stripeterminalreactnative.listener | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.stripe.stripeterminal.external.callable.Cancelable | ||
import com.stripe.stripeterminal.external.callable.ReaderReconnectionListener | ||
import com.stripe.stripeterminal.external.models.Reader | ||
import com.stripe.stripeterminal.external.models.TerminalException.TerminalErrorCode | ||
import com.stripeterminalreactnative.ReactExtensions.sendEvent | ||
import com.stripeterminalreactnative.ReactNativeConstants.* | ||
import com.stripeterminalreactnative.mapFromReader | ||
import com.stripeterminalreactnative.nativeMapOf | ||
|
||
class RNReaderReconnectionListener( | ||
private val context: ReactApplicationContext, | ||
private val onReaderReconnectStarted: (cancelable: Cancelable?) -> Unit, | ||
) : ReaderReconnectionListener { | ||
|
||
override fun onReaderReconnectFailed(reader: Reader) { | ||
context.sendEvent(READER_RECONNECT_FAIL.listenerName) { | ||
putMap("error", nativeMapOf { | ||
putString("code", TerminalErrorCode.UNEXPECTED_SDK_ERROR.toString()) | ||
putString("message", "Reader reconnect fail") | ||
}) | ||
} | ||
} | ||
|
||
override fun onReaderReconnectStarted(reader: Reader, cancelReconnect: Cancelable) { | ||
onReaderReconnectStarted(cancelReconnect) | ||
context.sendEvent(START_READER_RECONNECT.listenerName) { | ||
putMap("reader", mapFromReader(reader)) | ||
} | ||
} | ||
|
||
override fun onReaderReconnectSucceeded(reader: Reader) { | ||
context.sendEvent(READER_RECONNECT_SUCCEED.listenerName) { | ||
putMap("reader", mapFromReader(reader)) | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
.../src/test/java/com/stripeterminalreactnative/listener/RNReaderReconnectionListenerTest.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,69 @@ | ||
package com.stripeterminalreactnative.listener | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.stripe.stripeterminal.external.callable.Cancelable | ||
import com.stripe.stripeterminal.external.models.Reader | ||
import com.stripeterminalreactnative.* | ||
import com.stripeterminalreactnative.ReactExtensions.sendEvent | ||
import com.stripeterminalreactnative.ReactNativeConstants.READER_RECONNECT_FAIL | ||
import com.stripeterminalreactnative.ReactNativeConstants.START_READER_RECONNECT | ||
import com.stripeterminalreactnative.ReactNativeConstants.READER_RECONNECT_SUCCEED | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import org.junit.ClassRule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
import kotlin.test.assertTrue | ||
|
||
@RunWith(JUnit4::class) | ||
class RNReaderReconnectionListenerTest { | ||
|
||
companion object { | ||
@ClassRule | ||
@JvmField | ||
val typeReplacer = ReactNativeTypeReplacementRule() | ||
} | ||
|
||
private val context = mockk<ReactApplicationContext>() | ||
|
||
@Test | ||
fun `should send onReaderReconnectFailed event`() { | ||
val mockOnReaderReconnectStarted = mockk<(Cancelable?) -> Unit>(relaxed = true) | ||
val reader = mockk<Reader>(relaxed = true) | ||
val listener = RNReaderReconnectionListener(context,mockOnReaderReconnectStarted) | ||
listener.onReaderReconnectFailed(reader) | ||
|
||
verify(exactly = 1) { | ||
context.sendEvent(READER_RECONNECT_FAIL.listenerName, any()) | ||
} | ||
|
||
assertTrue(typeReplacer.sendEventSlot.captured.hasValue("error")) | ||
} | ||
|
||
@Test | ||
fun `should send onReaderReconnectStarted event`() { | ||
val mockOnReaderReconnectStarted = mockk<(Cancelable?) -> Unit>(relaxed = true) | ||
val mockCancelable = mockk<Cancelable>() | ||
val reader = mockk<Reader>(relaxed = true) | ||
val listener = RNReaderReconnectionListener(context,mockOnReaderReconnectStarted) | ||
listener.onReaderReconnectStarted(reader,mockCancelable) | ||
|
||
verify(exactly = 1) { mockOnReaderReconnectStarted.invoke(mockCancelable) } | ||
verify(exactly = 1) { context.sendEvent(START_READER_RECONNECT.listenerName, any()) } | ||
|
||
assertTrue(typeReplacer.sendEventSlot.captured.hasValue("reader")) | ||
} | ||
|
||
@Test | ||
fun `should send onReaderReconnectSucceeded event`() { | ||
val mockOnReaderReconnectStarted = mockk<(Cancelable?) -> Unit>(relaxed = true) | ||
val reader = mockk<Reader>(relaxed = true) | ||
val listener = RNReaderReconnectionListener(context,mockOnReaderReconnectStarted) | ||
listener.onReaderReconnectSucceeded(reader) | ||
|
||
verify(exactly = 1) { context.sendEvent(READER_RECONNECT_SUCCEED.listenerName, any()) } | ||
|
||
assertTrue(typeReplacer.sendEventSlot.captured.hasValue("reader")) | ||
} | ||
} |
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
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
Oops, something went wrong.