2
2
3
3
import androidx .test .core .app .ApplicationProvider ;
4
4
5
+ import com .onesignal .MockOSTimeImpl ;
5
6
import com .onesignal .OneSignal ;
6
7
import com .onesignal .ShadowCustomTabsClient ;
7
8
import com .onesignal .ShadowCustomTabsSession ;
21
22
import org .robolectric .annotation .Config ;
22
23
import org .robolectric .shadows .ShadowLog ;
23
24
25
+ import static com .onesignal .OneSignalPackagePrivateHelper .MIN_ON_SESSION_TIME_MILLIS ;
26
+ import static com .onesignal .OneSignalPackagePrivateHelper .OneSignal_setTime ;
24
27
import static com .onesignal .ShadowOneSignalRestClient .setRemoteParamsGetHtmlResponse ;
28
+ import static com .test .onesignal .RestClientAsserts .assertNumberOfOnSessions ;
25
29
import static com .test .onesignal .TestHelpers .threadAndTaskWait ;
26
30
27
31
@ Config (
39
43
@ RunWith (RobolectricTestRunner .class )
40
44
public class OneSignalInitializationIntegrationTestsRunner {
41
45
private ActivityController <BlankActivity > blankActivityController ;
46
+ private MockOSTimeImpl time ;
42
47
43
48
@ BeforeClass // Runs only once, before any tests
44
49
public static void setUpClass () throws Exception {
@@ -52,12 +57,23 @@ public void beforeEachTest() throws Exception {
52
57
TestHelpers .beforeTestInitAndCleanup ();
53
58
setRemoteParamsGetHtmlResponse ();
54
59
blankActivityController = Robolectric .buildActivity (BlankActivity .class ).create ();
60
+
61
+ time = new MockOSTimeImpl ();
62
+ OneSignal_setTime (time );
55
63
}
56
64
65
+ private static final long MIN_ON_SESSION_TIME_SEC = MIN_ON_SESSION_TIME_MILLIS / 1_000L ;
57
66
private static final String APP_ID = "11111111-2222-3333-4444-55555555555" ;
58
67
private static void helper_OneSignal_initWithAppContext () {
59
68
OneSignal .initWithContext (ApplicationProvider .getApplicationContext ());
60
69
}
70
+ private void helper_OneSignal_initWithActivity () {
71
+ OneSignal .initWithContext (blankActivityController .get ());
72
+ }
73
+
74
+ private void helper_advanceSystemTimeToNextOnSession () {
75
+ time .advanceSystemAndElapsedTimeBy (MIN_ON_SESSION_TIME_SEC + 1 );
76
+ }
61
77
62
78
@ Test
63
79
public void setRequiresUserPrivacyConsent_withTrue_CalledFirst_DoesNOTCreatePlayer () throws Exception {
@@ -82,4 +98,27 @@ public void setRequiresUserPrivacyConsent_withFalseAndRemoteTrue_DoesNOTCreatePl
82
98
RestClientAsserts .assertRemoteParamsWasTheOnlyNetworkCall ();
83
99
}
84
100
101
+ /**
102
+ * on_session calls should only be made if the user left the app for MIN_ON_SESSION_TIME_SEC or longer
103
+ * This test ensures that we meet the out of focus requirement, at lest through initWithContext
104
+ * being called a 2nd time code path.
105
+ */
106
+ @ Test
107
+ public void initWithContext_calledA2ndTimeAfter30OrMoreSeconds_doesNotStartNewSession () throws Exception {
108
+ // 1. Basic OneSignal init with Activity
109
+ OneSignal .setAppId (APP_ID );
110
+ helper_OneSignal_initWithActivity ();
111
+ threadAndTaskWait ();
112
+
113
+ // 2. Keep the app in focus for 30+ seconds, which is the time required to
114
+ helper_advanceSystemTimeToNextOnSession ();
115
+
116
+ // 3. Developer or OneSignal internally calls OneSignal.initWithContext
117
+ helper_OneSignal_initWithActivity ();
118
+ threadAndTaskWait ();
119
+
120
+ // 4. Ensure we do NOT make an /players/{player_id}/on_session network call.
121
+ assertNumberOfOnSessions (0 );
122
+ }
123
+
85
124
}
0 commit comments