Skip to content

Commit

Permalink
Wait for activity to be set rather than immediately return in waitUnt…
Browse files Browse the repository at this point in the history
…ilSystemConditionsAvailable (#1926)

* Wait for activity to be set rather than immediately return

* Fix linting problems

---------

Co-authored-by: jinliu9508 <[email protected]>
  • Loading branch information
brismithers and jinliu9508 committed Jan 31, 2024
1 parent fc56400 commit 30433c1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.onesignal.core.internal.application.IActivityLifecycleHandler
import com.onesignal.core.internal.application.IApplicationLifecycleHandler
import com.onesignal.core.internal.application.IApplicationService
import com.onesignal.debug.internal.logging.Logging
import kotlinx.coroutines.delay
import java.lang.ref.WeakReference

class ApplicationService() : IApplicationService, ActivityLifecycleCallbacks, OnGlobalLayoutListener {
Expand Down Expand Up @@ -208,10 +209,27 @@ class ApplicationService() : IApplicationService, ActivityLifecycleCallbacks, On
}

override suspend fun waitUntilSystemConditionsAvailable(): Boolean {
val currentActivity = current
if (currentActivity == null) {
Logging.warn("ApplicationService.waitUntilSystemConditionsAvailable: current is null")
return false
var currentActivity = current

// if this is called just after focusing, it's possible the current activity has not yet
// been set up. So we check for up to 5 seconds, then bail if it doesn't happen. We only
// do this when called on a non-main thread, if we're running on the main thread the
// activity cannot be setup, so we don't wait around.
var waitForActivityRetryCount =
if (AndroidUtils.isRunningOnMainThread()) {
50
} else {
0
}
while (currentActivity == null) {
waitForActivityRetryCount++
if (waitForActivityRetryCount > 50) {
Logging.warn("ApplicationService.waitUntilSystemConditionsAvailable: current is null")
return false
}
delay(100)

currentActivity = current
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.onesignal.core.internal.application
import android.app.Activity
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.onesignal.common.threading.WaiterWithValue
import com.onesignal.common.threading.suspendifyOnThread
import com.onesignal.core.internal.application.impl.ApplicationService
import com.onesignal.debug.LogLevel
import com.onesignal.debug.internal.logging.Logging
Expand All @@ -12,6 +14,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.runner.junit4.KotestTestRunner
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.delay
import org.junit.runner.RunWith
import org.robolectric.Robolectric

Expand Down Expand Up @@ -189,6 +192,58 @@ class ApplicationServiceTests : FunSpec({
response shouldBe false
}

test("wait until system condition returns false if activity not started within 5 seconds") {
// Given
val activity: Activity
Robolectric.buildActivity(Activity::class.java).use { controller ->
controller.setup() // Moves Activity to RESUMED state
activity = controller.get()
}
val applicationService = ApplicationService()

val waiter = WaiterWithValue<Boolean>()

// When
suspendifyOnThread {
val response = applicationService.waitUntilSystemConditionsAvailable()
waiter.wake(response)
}

delay(7000)

applicationService.onActivityStarted(activity)
val response = waiter.waitForWake()

// Then
response shouldBe false
}

test("wait until system condition returns true when an activity is started within 5 seconds") {
// Given
val activity: Activity
Robolectric.buildActivity(Activity::class.java).use { controller ->
controller.setup() // Moves Activity to RESUMED state
activity = controller.get()
}
val applicationService = ApplicationService()

val waiter = WaiterWithValue<Boolean>()

// When
suspendifyOnThread {
val response = applicationService.waitUntilSystemConditionsAvailable()
waiter.wake(response)
}

delay(3000)

applicationService.onActivityStarted(activity)
val response = waiter.waitForWake()

// Then
response shouldBe true
}

test("wait until system condition returns true when there is no system condition") {
// Given
val activity: Activity
Expand Down

0 comments on commit 30433c1

Please sign in to comment.