From e4d26a16f2a8f27272a6084544411252f1734c23 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Mon, 25 Mar 2019 18:52:02 +0300 Subject: [PATCH] Add tests related to authorization callbacks --- .../presenter/P2PReceiverPresenterTest.java | 33 +++++++++++++++++++ .../p2p/presenter/P2PSenderPresenterTest.java | 31 +++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PReceiverPresenterTest.java b/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PReceiverPresenterTest.java index b6e62aa..d5fb1ee 100644 --- a/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PReceiverPresenterTest.java +++ b/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PReceiverPresenterTest.java @@ -6,6 +6,7 @@ import com.google.android.gms.nearby.connection.ConnectionInfo; import com.google.android.gms.nearby.connection.ConnectionResolution; +import com.google.android.gms.nearby.connection.DiscoveredEndpointInfo; import com.google.android.gms.nearby.connection.Payload; import com.google.android.gms.nearby.connection.PayloadCallback; @@ -28,12 +29,14 @@ import org.smartregister.p2p.contract.P2pModeSelectContract; import org.smartregister.p2p.dialog.QRCodeGeneratorDialog; import org.smartregister.p2p.handler.OnActivityRequestPermissionHandler; +import org.smartregister.p2p.sync.ConnectionState; import org.smartregister.p2p.sync.DiscoveredDevice; import org.smartregister.p2p.sync.IReceiverSyncLifecycleCallback; import java.util.ArrayList; import java.util.List; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -514,4 +517,34 @@ public void onAuthenticationCancelledShouldRejectConnectionWhenNegotiatingWithAS Mockito.verify(interactor, Mockito.times(1)) .rejectConnection(ArgumentMatchers.eq(endpointId)); } + + @Test + public void onConnectionAuthorizedShouldChangeConnectionStateToAuthorized() { + assertNull(ReflectionHelpers.getField(p2PReceiverPresenter, "connectionState")); + ReflectionHelpers.setField(p2PReceiverPresenter, "currentSender", new DiscoveredDevice("endpointid" + , new DiscoveredEndpointInfo("endpointid", "endpoint-name"))); + + p2PReceiverPresenter.onConnectionAuthorized(); + + assertEquals(ConnectionState.AUTHORIZED + , ReflectionHelpers.getField(p2PReceiverPresenter, "connectionState")); + } + + @Test + public void onConnectionAuthorizationRejectedShouldResetState() { + ReflectionHelpers.setField(p2PReceiverPresenter, "currentSender", Mockito.mock(DiscoveredDevice.class)); + ReflectionHelpers.setField(p2PReceiverPresenter, "connectionState", ConnectionState.AUTHENTICATED); + + p2PReceiverPresenter.onConnectionAuthorizationRejected("Incompatible app version"); + + Mockito.verify(interactor, Mockito.times(1)) + .closeAllEndpoints(); + Mockito.verify(interactor, Mockito.times(1)) + .connectedTo(ArgumentMatchers.eq((String) null)); + Mockito.verify(p2PReceiverPresenter, Mockito.times(1)) + .startAdvertisingMode(); + + assertNull(ReflectionHelpers.getField(p2PReceiverPresenter, "currentSender")); + assertNull(ReflectionHelpers.getField(p2PReceiverPresenter, "connectionState")); + } } \ No newline at end of file diff --git a/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PSenderPresenterTest.java b/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PSenderPresenterTest.java index 043a5d0..ebdff9e 100644 --- a/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PSenderPresenterTest.java +++ b/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PSenderPresenterTest.java @@ -29,6 +29,7 @@ import org.smartregister.p2p.contract.P2pModeSelectContract; import org.smartregister.p2p.dialog.QRCodeScanningDialog; import org.smartregister.p2p.handler.OnActivityRequestPermissionHandler; +import org.smartregister.p2p.sync.ConnectionState; import org.smartregister.p2p.sync.DiscoveredDevice; import java.util.ArrayList; @@ -511,4 +512,34 @@ public void onDisconnectedShouldResetStateAndStartDiscoveringMode() { .startDiscoveringMode(); assertNull(ReflectionHelpers.getField(p2PSenderPresenter, "currentReceiver")); } + + @Test + public void onConnectionAuthorizedShouldChangeConnectionStateToAuthorized() { + assertNull(ReflectionHelpers.getField(p2PSenderPresenter, "connectionState")); + ReflectionHelpers.setField(p2PSenderPresenter, "currentReceiver", new DiscoveredDevice("endpointid" + , new DiscoveredEndpointInfo("endpointid", "endpoint-name"))); + + p2PSenderPresenter.onConnectionAuthorized(); + + assertEquals(ConnectionState.AUTHORIZED + , ReflectionHelpers.getField(p2PSenderPresenter, "connectionState")); + } + + @Test + public void onConnectionAuthorizationRejectedShouldResetState() { + ReflectionHelpers.setField(p2PSenderPresenter, "currentReceiver", Mockito.mock(DiscoveredDevice.class)); + ReflectionHelpers.setField(p2PSenderPresenter, "connectionState", ConnectionState.AUTHENTICATED); + + p2PSenderPresenter.onConnectionAuthorizationRejected("Incompatible app version"); + + Mockito.verify(interactor, Mockito.times(1)) + .closeAllEndpoints(); + Mockito.verify(interactor, Mockito.times(1)) + .connectedTo(ArgumentMatchers.eq((String) null)); + Mockito.verify(p2PSenderPresenter, Mockito.times(1)) + .startDiscoveringMode(); + + assertNull(ReflectionHelpers.getField(p2PSenderPresenter, "currentReceiver")); + assertNull(ReflectionHelpers.getField(p2PSenderPresenter, "connectionState")); + } } \ No newline at end of file