1+ package com .instabug .reactlibrary ;
2+
3+ import android .os .Handler ;
4+ import android .os .Looper ;
5+
6+ import com .instabug .bug .BugReporting ;
7+
8+ import org .junit .Before ;
9+ import org .junit .Test ;
10+ import org .junit .runner .RunWith ;
11+ import org .mockito .Matchers ;
12+ import org .mockito .Spy ;
13+ import org .mockito .internal .verification .VerificationModeFactory ;
14+ import org .mockito .invocation .InvocationOnMock ;
15+ import org .mockito .stubbing .Answer ;
16+ import org .powermock .api .mockito .PowerMockito ;
17+ import org .powermock .core .classloader .annotations .PrepareForTest ;
18+ import org .powermock .modules .junit4 .PowerMockRunner ;
19+
20+ import java .util .concurrent .Executors ;
21+ import java .util .concurrent .ScheduledExecutorService ;
22+ import java .util .concurrent .TimeUnit ;
23+
24+ import static org .mockito .Matchers .any ;
25+ import static org .mockito .Matchers .anyLong ;
26+ import static org .powermock .api .mockito .PowerMockito .doAnswer ;
27+ import static org .powermock .api .mockito .PowerMockito .mock ;
28+ import static org .powermock .api .mockito .PowerMockito .when ;
29+
30+ @ RunWith (PowerMockRunner .class )
31+ @ PrepareForTest ({Looper .class , android .os .Handler .class , BugReporting .class , Runnable .class , RNInstabugReactnativeModule .class })
32+
33+ public class RNInstabugReactnativeModuleTest {
34+
35+ private RNInstabugReactnativeModule rnModule = new RNInstabugReactnativeModule (null ,null ,null );
36+
37+ private final static ScheduledExecutorService mainThread = Executors .newSingleThreadScheduledExecutor ();
38+
39+ @ Test
40+ public void testBugReportingInvoke () {
41+ PowerMockito .mockStatic (BugReporting .class );
42+ rnModule .invoke ();
43+ PowerMockito .verifyStatic (VerificationModeFactory .times (1 ));
44+ BugReporting .invoke ();
45+ }
46+
47+ @ Before
48+ public void mockMainThreadHandler () throws Exception {
49+ PowerMockito .mockStatic (Looper .class );
50+ Looper mockMainThreadLooper = mock (Looper .class );
51+ when (Looper .getMainLooper ()).thenReturn (mockMainThreadLooper );
52+ Handler mockMainThreadHandler = mock (Handler .class );
53+ Answer <Boolean > handlerPostAnswer = new Answer <Boolean >() {
54+ @ Override
55+ public Boolean answer (InvocationOnMock invocation ) throws Throwable {
56+ Runnable runnable = invocation .getArgumentAt (0 , Runnable .class );
57+ Long delay = 0L ;
58+ if (invocation .getArguments ().length > 1 ) {
59+ delay = invocation .getArgumentAt (1 , Long .class );
60+ }
61+ if (runnable != null ) {
62+ mainThread .schedule (runnable , delay , TimeUnit .MILLISECONDS );
63+ }
64+ return true ;
65+ }
66+ };
67+ doAnswer (handlerPostAnswer ).when (mockMainThreadHandler ).post (any (Runnable .class ));
68+ doAnswer (handlerPostAnswer ).when (mockMainThreadHandler ).postDelayed (any (Runnable .class ), anyLong ());
69+ PowerMockito .whenNew (Handler .class ).withArguments (mockMainThreadLooper ).thenReturn (mockMainThreadHandler );
70+ }
71+
72+
73+ }
0 commit comments