-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix an issue when a listener/handler is removed before react native i…
…s initialized Add tests
- Loading branch information
Showing
2 changed files
with
176 additions
and
12 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
.../src/androidTest/java/com/walmartlabs/electrode/reactnative/bridge/BridgeHolderTests.java
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,117 @@ | ||
package com.walmartlabs.electrode.reactnative.bridge; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import com.walmartlabs.electrode.reactnative.bridge.helpers.Logger; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.UUID; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
import static junit.framework.TestCase.assertFalse; | ||
import static junit.framework.TestCase.assertNotNull; | ||
import static junit.framework.TestCase.assertTrue; | ||
import static org.junit.Assert.assertNull; | ||
|
||
public class BridgeHolderTests extends BaseBridgeTestCase { | ||
|
||
@Before | ||
public void setUp() { | ||
Logger.overrideLogLevel(Logger.LogLevel.DEBUG); | ||
} | ||
|
||
@Test | ||
public void testRequestHandlerQueuing() { | ||
String KEY_HANDLER = "requestHandler"; | ||
|
||
ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object> requestHandler1 = new ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object>() { | ||
@Override | ||
public void onRequest(@Nullable ElectrodeBridgeRequest payload, @NonNull ElectrodeBridgeResponseListener<Object> responseListener) { | ||
|
||
} | ||
}; | ||
|
||
UUID requestHandler1Uuid = ElectrodeBridgeHolder.registerRequestHandler(KEY_HANDLER, requestHandler1); | ||
assertEquals(1, ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.size()); | ||
assertTrue(ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.containsKey(KEY_HANDLER)); | ||
assertNotNull(ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.get(KEY_HANDLER)); | ||
assertEquals(requestHandler1, ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.get(KEY_HANDLER).getRequestHandler()); | ||
|
||
|
||
ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object> requestHandler2 = new ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object>() { | ||
@Override | ||
public void onRequest(@Nullable ElectrodeBridgeRequest payload, @NonNull ElectrodeBridgeResponseListener<Object> responseListener) { | ||
|
||
} | ||
}; | ||
|
||
//Add second request handler, this should replace the first one. | ||
UUID requestHandler2Uuid = ElectrodeBridgeHolder.registerRequestHandler(KEY_HANDLER, requestHandler2); | ||
assertEquals(1, ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.size()); | ||
assertTrue(ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.containsKey(KEY_HANDLER)); | ||
assertNotNull(ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.get(KEY_HANDLER)); | ||
assertEquals(requestHandler2, ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.get(KEY_HANDLER).getRequestHandler()); | ||
|
||
|
||
//Try removing the first request handler which is already replaced by second | ||
assertNull(ElectrodeBridgeHolder.unregisterRequestHandler(requestHandler1Uuid)); | ||
|
||
//Remove the second request handler | ||
assertEquals(requestHandler2, ElectrodeBridgeHolder.unregisterRequestHandler(requestHandler2Uuid)); | ||
assertEquals(0, ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.size()); | ||
assertFalse(ElectrodeBridgeHolder.mQueuedRequestHandlersRegistration.containsKey(KEY_HANDLER)); | ||
} | ||
|
||
@Test | ||
public void testEventListenerQueuing() { | ||
String KEY_LISTENER = "eventListener"; | ||
ElectrodeBridgeEventListener<ElectrodeBridgeEvent> eventListener1 = new ElectrodeBridgeEventListener<ElectrodeBridgeEvent>() { | ||
@Override | ||
public void onEvent(@Nullable ElectrodeBridgeEvent eventPayload) { | ||
|
||
} | ||
}; | ||
|
||
UUID eventListener1Uuid = ElectrodeBridgeHolder.addEventListener(KEY_LISTENER, eventListener1); | ||
|
||
assertEquals(1, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.size()); | ||
assertTrue(ElectrodeBridgeHolder.mQueuedEventListenersRegistration.containsKey(KEY_LISTENER)); | ||
assertNotNull(ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER)); | ||
assertEquals(1, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER).size()); | ||
assertEquals(eventListener1, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER).get(0).getEventListener()); | ||
|
||
|
||
//Add the second listener | ||
ElectrodeBridgeEventListener<ElectrodeBridgeEvent> eventListener2 = new ElectrodeBridgeEventListener<ElectrodeBridgeEvent>() { | ||
@Override | ||
public void onEvent(@Nullable ElectrodeBridgeEvent eventPayload) { | ||
} | ||
}; | ||
|
||
UUID eventListener2Uuid = ElectrodeBridgeHolder.addEventListener(KEY_LISTENER, eventListener2); | ||
assertEquals(1, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.size()); | ||
assertTrue(ElectrodeBridgeHolder.mQueuedEventListenersRegistration.containsKey(KEY_LISTENER)); | ||
assertNotNull(ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER)); | ||
assertEquals(2, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER).size()); | ||
assertEquals(eventListener1, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER).get(0).getEventListener()); | ||
assertEquals(eventListener2, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER).get(1).getEventListener()); | ||
|
||
//Remove one listener | ||
assertEquals(eventListener2, ElectrodeBridgeHolder.removeEventListener(eventListener2Uuid)); | ||
assertEquals(1, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.size()); | ||
assertTrue(ElectrodeBridgeHolder.mQueuedEventListenersRegistration.containsKey(KEY_LISTENER)); | ||
assertNotNull(ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER)); | ||
assertEquals(1, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER).size()); | ||
assertEquals(eventListener1, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER).get(0).getEventListener()); | ||
|
||
//Remove all listeners | ||
assertEquals(eventListener1, ElectrodeBridgeHolder.removeEventListener(eventListener1Uuid)); | ||
assertEquals(0, ElectrodeBridgeHolder.mQueuedEventListenersRegistration.size()); | ||
assertFalse(ElectrodeBridgeHolder.mQueuedEventListenersRegistration.containsKey(KEY_LISTENER)); | ||
assertNull(ElectrodeBridgeHolder.mQueuedEventListenersRegistration.get(KEY_LISTENER)); | ||
} | ||
|
||
} |
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