29
29
30
30
import android .app .Activity ;
31
31
32
+ import androidx .annotation .NonNull ;
33
+
34
+ import com .onesignal .OneSignal ;
32
35
import com .onesignal .OneSignalPackagePrivateHelper .PushRegistratorFCM ;
33
36
import com .onesignal .PushRegistrator ;
34
- import com .onesignal .ShadowFirebaseCloudMessaging ;
37
+ import com .onesignal .ShadowFirebaseApp ;
35
38
import com .onesignal .ShadowGooglePlayServicesUtil ;
39
+ import com .onesignal .ShadowOSUtils ;
40
+ import com .onesignal .ShadowOneSignalRestClient ;
41
+ import com .onesignal .StaticResetHelper ;
36
42
import com .onesignal .example .BlankActivity ;
37
43
44
+ import org .junit .After ;
38
45
import org .junit .Before ;
39
46
import org .junit .BeforeClass ;
40
47
import org .junit .Test ;
41
48
import org .junit .runner .RunWith ;
42
49
import org .robolectric .Robolectric ;
43
50
import org .robolectric .RobolectricTestRunner ;
44
51
import org .robolectric .annotation .Config ;
52
+ import org .robolectric .annotation .LooperMode ;
45
53
import org .robolectric .shadows .ShadowLog ;
46
54
55
+ import static com .test .onesignal .TestHelpers .threadAndTaskWait ;
56
+
47
57
import static junit .framework .Assert .assertTrue ;
48
58
49
59
@ Config (packageName = "com.onesignal.example" ,
50
60
shadows = {
51
61
ShadowGooglePlayServicesUtil .class ,
52
- ShadowFirebaseCloudMessaging .class },
53
- sdk = 21
62
+ ShadowOSUtils .class ,
63
+ ShadowOneSignalRestClient .class ,
64
+ ShadowFirebaseApp .class ,
65
+ },
66
+ sdk = 28
54
67
)
55
68
@ RunWith (RobolectricTestRunner .class )
69
+ @ LooperMode (LooperMode .Mode .LEGACY )
56
70
public class PushRegistratorRunner {
57
71
58
72
private Activity blankActivity ;
59
- private static boolean callbackFired ;
60
73
61
74
@ BeforeClass // Runs only once, before any tests
62
75
public static void setUpClass () throws Exception {
63
76
ShadowLog .stream = System .out ;
64
77
TestHelpers .beforeTestSuite ();
78
+ StaticResetHelper .saveStaticValues ();
65
79
}
66
80
67
81
@ Before // Before each test
68
- public void beforeEachTest () {
82
+ public void beforeEachTest () throws Exception {
83
+ TestHelpers .beforeTestInitAndCleanup ();
69
84
blankActivity = Robolectric .buildActivity (BlankActivity .class ).create ().get ();
70
- callbackFired = false ;
71
- ShadowFirebaseCloudMessaging .exists = true ;
72
85
}
73
86
74
- @ Test
75
- public void testGooglePlayServicesAPKMissingOnDevice () {
76
- PushRegistratorFCM pushReg = new PushRegistratorFCM ();
77
- final Thread testThread = Thread .currentThread ();
78
-
79
- pushReg .registerForPush (blankActivity , "" , new PushRegistrator .RegisteredHandler () {
80
- @ Override
81
- public void complete (String id , int status ) {
82
- callbackFired = true ;
83
- testThread .interrupt ();
84
- }
85
- });
87
+ @ After
88
+ public void afterEachTest () throws Exception {
89
+ TestHelpers .afterTestCleanup ();
90
+ }
91
+
92
+ static private class RegisteredHandler implements PushRegistrator .RegisteredHandler {
93
+ private final Thread testThread ;
94
+ public boolean callbackFired ;
95
+
96
+ RegisteredHandler (@ NonNull Thread testThread ) {
97
+ this .testThread = testThread ;
98
+ }
99
+
100
+ @ Override
101
+ public void complete (String id , int status ) {
102
+ callbackFired = true ;
103
+ testThread .interrupt ();
104
+ }
105
+ }
106
+
107
+ private void initOneSignalAndWait () throws Exception {
108
+ OneSignal .initWithContext (blankActivity );
109
+ OneSignal .setAppId ("11111111-2222-3333-4444-555555555555" );
110
+ threadAndTaskWait ();
111
+ }
112
+
113
+ private boolean performRegisterForPush () throws Exception {
114
+ initOneSignalAndWait ();
115
+
116
+ RegisteredHandler registeredHandler = new RegisteredHandler (Thread .currentThread ());
117
+
118
+ PushRegistratorFCM pushReg = new PushRegistratorFCM (blankActivity , null );
119
+ pushReg .registerForPush (blankActivity , "123456789" , registeredHandler );
86
120
try {Thread .sleep (5000 );} catch (Throwable t ) {}
87
121
88
- assertTrue ( callbackFired ) ;
122
+ return registeredHandler . callbackFired ;
89
123
}
90
124
91
125
@ Test
92
- public void testFCMPartOfGooglePlayServicesMissing () {
93
- PushRegistratorFCM pushReg = new PushRegistratorFCM ();
94
- ShadowFirebaseCloudMessaging .exists = false ;
95
-
96
- final Thread testThread = Thread .currentThread ();
97
-
98
- pushReg .registerForPush (blankActivity , "" , new PushRegistrator .RegisteredHandler () {
99
- @ Override
100
- public void complete (String id , int status ) {
101
- callbackFired = true ;
102
- testThread .interrupt ();
103
- }
104
- });
105
- try {Thread .sleep (5000 );} catch (Throwable t ) {}
126
+ public void testGooglePlayServicesAPKMissingOnDevice () throws Exception {
127
+ ShadowOSUtils .isGMSInstalledAndEnabled = false ;
128
+ boolean callbackFired = performRegisterForPush ();
129
+ assertTrue (callbackFired );
130
+ }
106
131
132
+ @ Test
133
+ public void testFCMPartOfGooglePlayServicesMissing () throws Exception {
134
+ ShadowOSUtils .isGMSInstalledAndEnabled = true ;
135
+ boolean callbackFired = performRegisterForPush ();
107
136
assertTrue (callbackFired );
108
137
}
109
138
}
0 commit comments