|
| 1 | +package com.instabug.reactlibrary; |
| 2 | + |
| 3 | +import static com.instabug.apm.configuration.cp.APMFeature.APM_NETWORK_PLUGIN_INSTALLED; |
| 4 | +import static com.instabug.apm.configuration.cp.APMFeature.CP_NATIVE_INTERCEPTION_ENABLED; |
| 5 | +import static org.mockito.Mockito.*; |
| 6 | +import static org.junit.Assert.*; |
| 7 | + |
| 8 | +import android.os.Looper; |
| 9 | + |
| 10 | +import com.facebook.react.bridge.Promise; |
| 11 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 12 | + |
| 13 | +import org.junit.After; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.mockito.ArgumentCaptor; |
| 17 | +import org.mockito.MockedStatic; |
| 18 | +import org.mockito.Mockito; |
| 19 | +import org.mockito.invocation.InvocationOnMock; |
| 20 | +import org.mockito.stubbing.Answer; |
| 21 | + |
| 22 | +import com.instabug.apm.InternalAPM; |
| 23 | +import com.instabug.reactlibrary.utils.MainThreadHandler; |
| 24 | + |
| 25 | +import java.util.concurrent.Executors; |
| 26 | +import java.util.concurrent.ScheduledExecutorService; |
| 27 | + |
| 28 | +public class RNInstabugNetworkLoggerModuleTest { |
| 29 | + |
| 30 | + // Mock MainThread |
| 31 | + private final static ScheduledExecutorService mainThread = Executors.newSingleThreadScheduledExecutor(); |
| 32 | + |
| 33 | + // Mock Objects |
| 34 | + private MockedStatic<Looper> mockLooper; |
| 35 | + private MockedStatic<MainThreadHandler> mockMainThreadHandler; |
| 36 | + private RNInstabugNetworkLoggerModule networkLoggerModule; |
| 37 | + private Promise mockPromise; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void mockMainThreadHandler() throws Exception { |
| 41 | + // Mock Object |
| 42 | + ReactApplicationContext mockReactApplicationContext = mock(ReactApplicationContext.class); |
| 43 | + mockPromise = mock(Promise.class); |
| 44 | + networkLoggerModule = new RNInstabugNetworkLoggerModule(mockReactApplicationContext); |
| 45 | + |
| 46 | + // Mock static functions |
| 47 | + mockLooper = mockStatic(Looper.class); |
| 48 | + mockMainThreadHandler = mockStatic(MainThreadHandler.class); |
| 49 | + // Mock Looper class |
| 50 | + Looper mockMainThreadLooper = mock(Looper.class); |
| 51 | + when(Looper.getMainLooper()).thenReturn(mockMainThreadLooper); |
| 52 | + |
| 53 | + // Override runOnMainThread |
| 54 | + Answer<Boolean> handlerPostAnswer = new Answer<Boolean>() { |
| 55 | + @Override |
| 56 | + public Boolean answer(InvocationOnMock invocation) throws Throwable { |
| 57 | + invocation.getArgument(0, Runnable.class).run(); |
| 58 | + return true; |
| 59 | + } |
| 60 | + }; |
| 61 | + Mockito.doAnswer(handlerPostAnswer).when(MainThreadHandler.class); |
| 62 | + MainThreadHandler.runOnMainThread(any(Runnable.class)); |
| 63 | + } |
| 64 | + |
| 65 | + @After |
| 66 | + public void tearDown() { |
| 67 | + // Remove static mocks |
| 68 | + mockLooper.close(); |
| 69 | + mockMainThreadHandler.close(); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testGetName() { |
| 75 | + // Test the getName method |
| 76 | + String name = networkLoggerModule.getName(); |
| 77 | + assertEquals("IBGNetworkLogger", name); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testAddListener() { |
| 82 | + // Test addListener method |
| 83 | + networkLoggerModule.addListener("event_name"); |
| 84 | + // Nothing to assert, but check no exceptions are thrown |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testRemoveListeners() { |
| 89 | + // Test removeListeners method |
| 90 | + networkLoggerModule.removeListeners(1); |
| 91 | + // Nothing to assert, but check no exceptions are thrown |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testIsNativeInterceptionEnabled_True() { |
| 96 | + |
| 97 | + |
| 98 | + // Mock InternalAPM behavior within the scope of this test |
| 99 | + try (MockedStatic<InternalAPM> internalAPMMock = mockStatic(InternalAPM.class)) { |
| 100 | + internalAPMMock.when(() -> InternalAPM._isFeatureEnabledCP(CP_NATIVE_INTERCEPTION_ENABLED, "")) |
| 101 | + .thenReturn(true); |
| 102 | + |
| 103 | + // Execute the method |
| 104 | + networkLoggerModule.isNativeInterceptionEnabled(mockPromise); |
| 105 | + |
| 106 | + // Capture the Promise.resolve() call |
| 107 | + ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 108 | + verify(mockPromise).resolve(captor.capture()); |
| 109 | + |
| 110 | + // Assert that true was passed to resolve |
| 111 | + assertTrue(captor.getValue()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testIsNativeInterceptionEnabled_False() { |
| 117 | + |
| 118 | + |
| 119 | + try (MockedStatic<InternalAPM> internalAPMMock = mockStatic(InternalAPM.class)) { |
| 120 | + internalAPMMock.when(() -> InternalAPM._isFeatureEnabledCP(CP_NATIVE_INTERCEPTION_ENABLED, "")) |
| 121 | + .thenReturn(false); |
| 122 | + |
| 123 | + // Execute the method |
| 124 | + networkLoggerModule.isNativeInterceptionEnabled(mockPromise); |
| 125 | + |
| 126 | + // Capture the Promise.resolve() call |
| 127 | + ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 128 | + verify(mockPromise).resolve(captor.capture()); |
| 129 | + |
| 130 | + // Assert that false was passed to resolve |
| 131 | + assertFalse(captor.getValue()); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testIsNativeInterceptionEnabled_Exception() { |
| 137 | + |
| 138 | + |
| 139 | + // Simulate an exception in InternalAPM |
| 140 | + try (MockedStatic<InternalAPM> internalAPMMock = mockStatic(InternalAPM.class)) { |
| 141 | + internalAPMMock.when(() -> InternalAPM._isFeatureEnabledCP(anyString(), anyString())) |
| 142 | + .thenThrow(new RuntimeException("Error")); |
| 143 | + |
| 144 | + // Execute the method |
| 145 | + networkLoggerModule.isNativeInterceptionEnabled(mockPromise); |
| 146 | + |
| 147 | + // Capture the Promise.resolve() call in case of an exception |
| 148 | + ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 149 | + verify(mockPromise).resolve(captor.capture()); |
| 150 | + |
| 151 | + // Assert that false was passed to resolve when exception occurs |
| 152 | + assertFalse(captor.getValue()); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testHasAPMNetworkPlugin_True() { |
| 158 | + |
| 159 | + |
| 160 | + try (MockedStatic<InternalAPM> internalAPMMock = mockStatic(InternalAPM.class)) { |
| 161 | + internalAPMMock.when(() -> InternalAPM._isFeatureEnabledCP(APM_NETWORK_PLUGIN_INSTALLED, "")) |
| 162 | + .thenReturn(true); |
| 163 | + |
| 164 | + // Execute the method |
| 165 | + networkLoggerModule.hasAPMNetworkPlugin(mockPromise); |
| 166 | + |
| 167 | + // Capture the Promise.resolve() call |
| 168 | + ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 169 | + verify(mockPromise).resolve(captor.capture()); |
| 170 | + |
| 171 | + // Assert that true was passed to resolve |
| 172 | + assertTrue(captor.getValue()); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void testHasAPMNetworkPlugin_False() { |
| 178 | + |
| 179 | + |
| 180 | + try (MockedStatic<InternalAPM> internalAPMMock = mockStatic(InternalAPM.class)) { |
| 181 | + internalAPMMock.when(() -> InternalAPM._isFeatureEnabledCP(APM_NETWORK_PLUGIN_INSTALLED, "")) |
| 182 | + .thenReturn(false); |
| 183 | + |
| 184 | + // Execute the method |
| 185 | + networkLoggerModule.hasAPMNetworkPlugin(mockPromise); |
| 186 | + |
| 187 | + // Capture the Promise.resolve() call |
| 188 | + ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 189 | + verify(mockPromise).resolve(captor.capture()); |
| 190 | + |
| 191 | + // Assert that false was passed to resolve |
| 192 | + assertFalse(captor.getValue()); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void testHasAPMNetworkPlugin_Exception() { |
| 198 | + |
| 199 | + |
| 200 | + // Simulate an exception in InternalAPM |
| 201 | + try (MockedStatic<InternalAPM> internalAPMMock = mockStatic(InternalAPM.class)) { |
| 202 | + internalAPMMock.when(() -> InternalAPM._isFeatureEnabledCP(anyString(), anyString())) |
| 203 | + .thenThrow(new RuntimeException("Error")); |
| 204 | + |
| 205 | + // Execute the method |
| 206 | + networkLoggerModule.hasAPMNetworkPlugin(mockPromise); |
| 207 | + |
| 208 | + // Capture the Promise.resolve() call in case of an exception |
| 209 | + ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 210 | + verify(mockPromise).resolve(captor.capture()); |
| 211 | + |
| 212 | + // Assert that false was passed to resolve when exception occurs |
| 213 | + assertFalse(captor.getValue()); |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments