Skip to content

Commit

Permalink
Add tests related to authorization callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Mar 29, 2019
1 parent df71602 commit e4d26a1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit e4d26a1

Please sign in to comment.