|
| 1 | +package com.instabug.reactlibrary; |
| 2 | + |
| 3 | +import android.os.Handler; |
| 4 | +import android.os.Looper; |
| 5 | +import android.os.SystemClock; |
| 6 | + |
| 7 | +import com.facebook.react.bridge.Callback; |
| 8 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 9 | +import com.instabug.apm.APM; |
| 10 | +import com.instabug.apm.model.ExecutionTrace; |
| 11 | +import java.util.HashMap; |
| 12 | +import javax.annotation.Nonnull; |
| 13 | + |
| 14 | +import com.facebook.react.bridge.Arguments; |
| 15 | +import com.facebook.react.bridge.Callback; |
| 16 | +import com.facebook.react.bridge.JavaOnlyArray; |
| 17 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 18 | +import com.facebook.react.bridge.WritableMap; |
| 19 | +import com.instabug.library.Feature; |
| 20 | +import com.instabug.reactlibrary.utils.InstabugUtil; |
| 21 | +import com.instabug.survey.OnDismissCallback; |
| 22 | +import com.instabug.survey.OnShowCallback; |
| 23 | +import com.instabug.survey.Surveys; |
| 24 | +import com.instabug.reactlibrary.utils.MainThreadHandler; |
| 25 | + |
| 26 | +import org.json.JSONArray; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.mockito.Matchers; |
| 31 | +import org.mockito.internal.verification.VerificationModeFactory; |
| 32 | +import org.mockito.invocation.InvocationOnMock; |
| 33 | +import org.mockito.stubbing.Answer; |
| 34 | +import org.powermock.api.mockito.PowerMockito; |
| 35 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 36 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 37 | + |
| 38 | +import java.util.concurrent.Executors; |
| 39 | +import java.util.concurrent.ScheduledExecutorService; |
| 40 | + |
| 41 | +import static org.mockito.Matchers.any; |
| 42 | +import static org.mockito.Matchers.anyLong; |
| 43 | +import static org.mockito.Matchers.eq; |
| 44 | +import static org.mockito.Mockito.verify; |
| 45 | +import static org.powermock.api.mockito.PowerMockito.doAnswer; |
| 46 | +import static org.powermock.api.mockito.PowerMockito.mock; |
| 47 | +import static org.powermock.api.mockito.PowerMockito.when; |
| 48 | + |
| 49 | +@RunWith(PowerMockRunner.class) |
| 50 | +@PrepareForTest({Looper.class, android.os.Handler.class, APM.class, ExecutionTrace.class, SystemClock.class, Runnable.class, RNInstabugAPMModule.class, Arguments.class, InstabugUtil.class, MainThreadHandler.class}) |
| 51 | + |
| 52 | +public class RNInstabugAPMModuleTest { |
| 53 | + |
| 54 | + private RNInstabugAPMModule apmModule = new RNInstabugAPMModule(null); |
| 55 | + |
| 56 | + private final static ScheduledExecutorService mainThread = Executors.newSingleThreadScheduledExecutor(); |
| 57 | + |
| 58 | + @Before |
| 59 | + public void mockMainThreadHandler() throws Exception { |
| 60 | + PowerMockito.mockStatic(Looper.class); |
| 61 | + Looper mockMainThreadLooper = mock(Looper.class); |
| 62 | + when(Looper.getMainLooper()).thenReturn(mockMainThreadLooper); |
| 63 | + Handler mockMainThreadHandler = mock(Handler.class); |
| 64 | + Answer<Boolean> handlerPostAnswer = new Answer<Boolean>() { |
| 65 | + @Override |
| 66 | + public Boolean answer(InvocationOnMock invocation) throws Throwable { |
| 67 | + invocation.getArgumentAt(0, Runnable.class).run(); |
| 68 | + return true; |
| 69 | + } |
| 70 | + }; |
| 71 | + doAnswer(handlerPostAnswer).when(mockMainThreadHandler).post(any(Runnable.class)); |
| 72 | + doAnswer(handlerPostAnswer).when(mockMainThreadHandler).postDelayed(any(Runnable.class), anyLong()); |
| 73 | + PowerMockito.whenNew(Handler.class).withArguments(mockMainThreadLooper).thenReturn(mockMainThreadHandler); |
| 74 | + } |
| 75 | + |
| 76 | + /********APM*********/ |
| 77 | + |
| 78 | + @Test |
| 79 | + public void givenFalse$setEnabled_whenQuery_thenShouldCallNativeApiWithDisabled() { |
| 80 | + // given |
| 81 | + PowerMockito.mockStatic(APM.class); |
| 82 | + // when |
| 83 | + apmModule.setEnabled(false); |
| 84 | + // then |
| 85 | + PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 86 | + APM.setEnabled(false); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void givenTrue$setEnabled_whenQuery_thenShouldCallNativeApiWithEnabled() { |
| 91 | + // given |
| 92 | + PowerMockito.mockStatic(APM.class); |
| 93 | + // when |
| 94 | + apmModule.setEnabled(true); |
| 95 | + // then |
| 96 | + PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 97 | + APM.setEnabled(true); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void givenFalse$setAppLaunchEnabled_whenQuery_thenShouldCallNativeApiWithDisabled() { |
| 102 | + // given |
| 103 | + PowerMockito.mockStatic(APM.class); |
| 104 | + // when |
| 105 | + apmModule.setAppLaunchEnabled(false); |
| 106 | + // then |
| 107 | + PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 108 | + APM.setAppLaunchEnabled(false); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void givenTrue$setAppLaunchEnabled_whenQuery_thenShouldCallNativeApiWithEnabled() { |
| 113 | + // given |
| 114 | + PowerMockito.mockStatic(APM.class); |
| 115 | + // when |
| 116 | + apmModule.setAppLaunchEnabled(true); |
| 117 | + // then |
| 118 | + PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 119 | + APM.setAppLaunchEnabled(true); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void givenString$startExecutionTrace_whenQuery_thenShouldCallNativeApi() { |
| 124 | + // given |
| 125 | + PowerMockito.mockStatic(APM.class); |
| 126 | + Callback callback = mock(Callback.class); |
| 127 | + // when |
| 128 | + apmModule.startExecutionTrace("trace", "1", callback); |
| 129 | + // then |
| 130 | + PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 131 | + APM.startExecutionTrace("trace"); |
| 132 | + verify(callback).invoke(any()); |
| 133 | + } |
| 134 | + |
| 135 | + // @Test |
| 136 | + // public void givenString$setExecutionTraceAttribute_whenQuery_thenShouldCallNativeApiWithIntArgs() { |
| 137 | + // // given |
| 138 | + // PowerMockito.mockStatic(APM.class); |
| 139 | + // ExecutionTrace trace = mock(ExecutionTrace.class); |
| 140 | + // Callback callback = mock(Callback.class); |
| 141 | + // // when |
| 142 | + // PowerMockito.whenNew(ExecutionTrace.class).withArguments("trace").thenReturn(trace); |
| 143 | + // apmModule.startExecutionTrace("trace", "1", callback); |
| 144 | + // apmModule.setExecutionTraceAttribute("trace", "key", "value"); |
| 145 | + // // then |
| 146 | + // verify(trace).setAttribute("key", "value"); |
| 147 | + // } |
| 148 | + |
| 149 | + // @Test |
| 150 | + // public void givenTrace$endExecutionTrace_whenQuery_thenShouldCallNativeApiWithIntArgs() { |
| 151 | + // // given |
| 152 | + // PowerMockito.mockStatic(APM.class); |
| 153 | + // ExecutionTrace trace = mock(ExecutionTrace.class); |
| 154 | + // Callback callback = mock(Callback.class); |
| 155 | + // // when |
| 156 | + // PowerMockito.whenNew(ExecutionTrace.class).withArguments("trace").thenReturn(trace); |
| 157 | + // apmModule.startExecutionTrace("trace", "1", callback); |
| 158 | + // apmModule.endExecutionTrace("1"); |
| 159 | + // // then |
| 160 | + // verify(trace).end(); |
| 161 | + // } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void givenString$startUITrace_whenQuery_thenShouldCallNativeApiWithEnabled() { |
| 165 | + // given |
| 166 | + PowerMockito.mockStatic(APM.class); |
| 167 | + // when |
| 168 | + apmModule.startUITrace("uiTrace"); |
| 169 | + // then |
| 170 | + PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 171 | + APM.startUITrace("uiTrace"); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void given$endUITrace_whenQuery_thenShouldCallNativeApiWithEnabled() { |
| 176 | + // given |
| 177 | + PowerMockito.mockStatic(APM.class); |
| 178 | + // when |
| 179 | + apmModule.startUITrace("uiTrace"); |
| 180 | + apmModule.endUITrace(); |
| 181 | + // then |
| 182 | + PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 183 | + APM.endUITrace(); |
| 184 | + } |
| 185 | +} |
0 commit comments