Skip to content

Commit 2381a82

Browse files
committed
Corrected test to run expected code
* Tests were not running the code it was designed to test due to a number of env things such as context not being initlized which is now addressed. * Cleaned up static callbackFired var and made a class to hold on to this. * Cleaned up duplicated code by making helper method performRegisterForPush.
1 parent 0b7c7a2 commit 2381a82

File tree

1 file changed

+63
-34
lines changed

1 file changed

+63
-34
lines changed

OneSignalSDK/unittest/src/test/java/com/test/onesignal/PushRegistratorRunner.java

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,81 +29,110 @@
2929

3030
import android.app.Activity;
3131

32+
import androidx.annotation.NonNull;
33+
34+
import com.onesignal.OneSignal;
3235
import com.onesignal.OneSignalPackagePrivateHelper.PushRegistratorFCM;
3336
import com.onesignal.PushRegistrator;
34-
import com.onesignal.ShadowFirebaseCloudMessaging;
37+
import com.onesignal.ShadowFirebaseApp;
3538
import com.onesignal.ShadowGooglePlayServicesUtil;
39+
import com.onesignal.ShadowOSUtils;
40+
import com.onesignal.ShadowOneSignalRestClient;
41+
import com.onesignal.StaticResetHelper;
3642
import com.onesignal.example.BlankActivity;
3743

44+
import org.junit.After;
3845
import org.junit.Before;
3946
import org.junit.BeforeClass;
4047
import org.junit.Test;
4148
import org.junit.runner.RunWith;
4249
import org.robolectric.Robolectric;
4350
import org.robolectric.RobolectricTestRunner;
4451
import org.robolectric.annotation.Config;
52+
import org.robolectric.annotation.LooperMode;
4553
import org.robolectric.shadows.ShadowLog;
4654

55+
import static com.test.onesignal.TestHelpers.threadAndTaskWait;
56+
4757
import static junit.framework.Assert.assertTrue;
4858

4959
@Config(packageName = "com.onesignal.example",
5060
shadows = {
5161
ShadowGooglePlayServicesUtil.class,
52-
ShadowFirebaseCloudMessaging.class },
53-
sdk = 21
62+
ShadowOSUtils.class,
63+
ShadowOneSignalRestClient.class,
64+
ShadowFirebaseApp.class,
65+
},
66+
sdk = 28
5467
)
5568
@RunWith(RobolectricTestRunner.class)
69+
@LooperMode(LooperMode.Mode.LEGACY)
5670
public class PushRegistratorRunner {
5771

5872
private Activity blankActivity;
59-
private static boolean callbackFired;
6073

6174
@BeforeClass // Runs only once, before any tests
6275
public static void setUpClass() throws Exception {
6376
ShadowLog.stream = System.out;
6477
TestHelpers.beforeTestSuite();
78+
StaticResetHelper.saveStaticValues();
6579
}
6680

6781
@Before // Before each test
68-
public void beforeEachTest() {
82+
public void beforeEachTest() throws Exception {
83+
TestHelpers.beforeTestInitAndCleanup();
6984
blankActivity = Robolectric.buildActivity(BlankActivity.class).create().get();
70-
callbackFired = false;
71-
ShadowFirebaseCloudMessaging.exists = true;
7285
}
7386

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);
86120
try {Thread.sleep(5000);} catch (Throwable t) {}
87121

88-
assertTrue(callbackFired);
122+
return registeredHandler.callbackFired;
89123
}
90124

91125
@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+
}
106131

132+
@Test
133+
public void testFCMPartOfGooglePlayServicesMissing() throws Exception {
134+
ShadowOSUtils.isGMSInstalledAndEnabled = true;
135+
boolean callbackFired = performRegisterForPush();
107136
assertTrue(callbackFired);
108137
}
109138
}

0 commit comments

Comments
 (0)