-
Notifications
You must be signed in to change notification settings - Fork 376
chore: Cleaned up and Added new tests #2541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abdulraqeeb33
wants to merge
5
commits into
ar-otel-crash-reporting
Choose a base branch
from
otel-unit-tests
base: ar-otel-crash-reporting
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
336 changes: 0 additions & 336 deletions
336
.../onesignal/core/src/test/java/com/onesignal/debug/internal/crash/CrashReportUploadTest.kt
This file was deleted.
Oops, something went wrong.
70 changes: 44 additions & 26 deletions
70
...core/src/test/java/com/onesignal/debug/internal/crash/OneSignalCrashHandlerFactoryTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,57 +1,75 @@ | ||
| package com.onesignal.debug.internal.crash | ||
|
|
||
| import android.content.Context | ||
| import android.os.Build | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import br.com.colman.kotest.android.extensions.robolectric.RobolectricTest | ||
| import com.onesignal.debug.internal.logging.otel.android.AndroidOtelLogger | ||
| import com.onesignal.otel.IOtelCrashHandler | ||
| import com.onesignal.otel.IOtelLogger | ||
| import io.kotest.core.spec.style.FunSpec | ||
| import io.kotest.matchers.shouldNotBe | ||
| import io.kotest.matchers.types.shouldBeInstanceOf | ||
| import io.mockk.mockk | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @RobolectricTest | ||
| @Config(sdk = [Build.VERSION_CODES.O]) | ||
| class OneSignalCrashHandlerFactoryTest : FunSpec({ | ||
| var appContext: Context? = null | ||
| var logger: AndroidOtelLogger? = null | ||
| lateinit var appContext: Context | ||
| lateinit var logger: AndroidOtelLogger | ||
| // Save original handler to restore after tests | ||
| val originalHandler: Thread.UncaughtExceptionHandler? = Thread.getDefaultUncaughtExceptionHandler() | ||
|
|
||
| beforeAny { | ||
| if (appContext == null) { | ||
| appContext = ApplicationProvider.getApplicationContext() | ||
| logger = AndroidOtelLogger() | ||
| } | ||
| appContext = ApplicationProvider.getApplicationContext() | ||
| logger = AndroidOtelLogger() | ||
| } | ||
|
|
||
| afterEach { | ||
| // Restore original uncaught exception handler after each test | ||
| Thread.setDefaultUncaughtExceptionHandler(originalHandler) | ||
| } | ||
|
|
||
| test("createCrashHandler should return IOtelCrashHandler") { | ||
| val handler = OneSignalCrashHandlerFactory.createCrashHandler( | ||
| appContext!!, | ||
| logger!! | ||
| ) | ||
| val handler = OneSignalCrashHandlerFactory.createCrashHandler(appContext, logger) | ||
|
|
||
| handler.shouldBeInstanceOf<IOtelCrashHandler>() | ||
| } | ||
|
|
||
| test("createCrashHandler should create Otel handler for SDK 26+") { | ||
| // Note: SDK version check is handled at runtime by the factory | ||
| // This test verifies the handler can be created and initialized | ||
| val handler = OneSignalCrashHandlerFactory.createCrashHandler( | ||
| appContext!!, | ||
| logger!! | ||
| ) | ||
| test("createCrashHandler should create handler that can be initialized") { | ||
| val handler = OneSignalCrashHandlerFactory.createCrashHandler(appContext, logger) | ||
|
|
||
| handler shouldNotBe null | ||
| // Should be able to initialize | ||
| handler.initialize() | ||
| } | ||
|
|
||
| test("createCrashHandler should return no-op handler for SDK < 26") { | ||
| // Note: SDK version check is handled at runtime by the factory | ||
| // This test verifies the handler can be created and initialized | ||
| val handler = OneSignalCrashHandlerFactory.createCrashHandler( | ||
| appContext!!, | ||
| logger!! | ||
| ) | ||
| test("createCrashHandler should accept mock logger") { | ||
| val mockLogger = mockk<IOtelLogger>(relaxed = true) | ||
|
|
||
| val handler = OneSignalCrashHandlerFactory.createCrashHandler(appContext, mockLogger) | ||
|
|
||
| handler shouldNotBe null | ||
| handler.shouldBeInstanceOf<IOtelCrashHandler>() | ||
| } | ||
|
|
||
| test("handler should be idempotent when initialized multiple times") { | ||
| val handler = OneSignalCrashHandlerFactory.createCrashHandler(appContext, logger) | ||
|
|
||
| handler.initialize() | ||
| handler.initialize() // Should not throw | ||
|
|
||
| handler shouldNotBe null | ||
| handler.initialize() // Should not crash | ||
| } | ||
|
|
||
| test("createCrashHandler should work with different contexts") { | ||
| val context1: Context = ApplicationProvider.getApplicationContext() | ||
| val context2: Context = ApplicationProvider.getApplicationContext() | ||
|
|
||
| val handler1 = OneSignalCrashHandlerFactory.createCrashHandler(context1, logger) | ||
| val handler2 = OneSignalCrashHandlerFactory.createCrashHandler(context2, logger) | ||
|
|
||
| handler1 shouldNotBe null | ||
| handler2 shouldNotBe null | ||
| } | ||
| }) |
92 changes: 48 additions & 44 deletions
92
...ore/src/test/java/com/onesignal/debug/internal/crash/OneSignalCrashUploaderWrapperTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,100 +1,104 @@ | ||
| package com.onesignal.debug.internal.crash | ||
|
|
||
| import android.content.Context | ||
| import android.content.SharedPreferences | ||
| import android.os.Build | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import br.com.colman.kotest.android.extensions.robolectric.RobolectricTest | ||
| import com.onesignal.core.internal.application.IApplicationService | ||
| import com.onesignal.core.internal.config.ConfigModel | ||
| import com.onesignal.core.internal.preferences.PreferenceOneSignalKeys | ||
| import com.onesignal.core.internal.preferences.PreferenceStores | ||
| import com.onesignal.core.internal.startup.IStartableService | ||
| import io.kotest.core.spec.style.FunSpec | ||
| import io.kotest.matchers.shouldNotBe | ||
| import io.kotest.matchers.types.shouldBeInstanceOf | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.json.JSONArray | ||
| import org.json.JSONObject | ||
| import org.robolectric.annotation.Config | ||
| import com.onesignal.core.internal.config.CONFIG_NAME_SPACE as configNameSpace | ||
|
|
||
| @RobolectricTest | ||
| @Config(sdk = [Build.VERSION_CODES.O]) | ||
| class OneSignalCrashUploaderWrapperTest : FunSpec({ | ||
|
|
||
| var appContext: Context? = null | ||
| lateinit var appContext: Context | ||
| lateinit var sharedPreferences: SharedPreferences | ||
|
|
||
| beforeAny { | ||
| if (appContext == null) { | ||
| appContext = ApplicationProvider.getApplicationContext() | ||
| } | ||
| appContext = ApplicationProvider.getApplicationContext() | ||
| sharedPreferences = appContext.getSharedPreferences(PreferenceStores.ONESIGNAL, Context.MODE_PRIVATE) | ||
| } | ||
|
|
||
| afterEach { | ||
| sharedPreferences.edit().clear().commit() | ||
| } | ||
|
|
||
| test("should implement IStartableService") { | ||
| // Given | ||
| test("should implement IStartableService interface") { | ||
| val mockApplicationService = mockk<IApplicationService>(relaxed = true) | ||
| every { mockApplicationService.appContext } returns appContext!! | ||
| every { mockApplicationService.appContext } returns appContext | ||
|
|
||
| // When | ||
| val wrapper = OneSignalCrashUploaderWrapper(mockApplicationService) | ||
|
|
||
| // Then | ||
| wrapper.shouldBeInstanceOf<IStartableService>() | ||
| } | ||
|
|
||
| test("should create uploader lazily when start is called") { | ||
| // Given | ||
| test("start should complete without error when remote logging is disabled") { | ||
| // Configure remote logging as disabled (NONE) | ||
| val remoteLoggingParams = JSONObject().put("logLevel", "NONE") | ||
| val configModel = JSONObject().put(ConfigModel::remoteLoggingParams.name, remoteLoggingParams) | ||
| sharedPreferences.edit() | ||
| .putString(PreferenceOneSignalKeys.MODEL_STORE_PREFIX + configNameSpace, JSONArray().put(configModel).toString()) | ||
| .commit() | ||
|
|
||
| val mockApplicationService = mockk<IApplicationService>(relaxed = true) | ||
| every { mockApplicationService.appContext } returns appContext!! | ||
| every { mockApplicationService.appContext } returns appContext | ||
|
|
||
| val wrapper = OneSignalCrashUploaderWrapper(mockApplicationService) | ||
|
|
||
| // When | ||
| runBlocking { | ||
| wrapper.start() | ||
| } | ||
|
|
||
| // Then - should not throw, uploader should be created | ||
| // We can't directly verify the uploader was created, but if start() completes without error, | ||
| // it means the uploader was created and started successfully | ||
| // Should return early without error when remote logging is disabled | ||
| runBlocking { wrapper.start() } | ||
| } | ||
|
|
||
| test("should call uploader.start() when start() is called") { | ||
| // Given | ||
| test("start should complete without error when no crash reports exist") { | ||
| // Configure remote logging as enabled | ||
| val remoteLoggingParams = JSONObject().put("logLevel", "ERROR") | ||
| val configModel = JSONObject().put(ConfigModel::remoteLoggingParams.name, remoteLoggingParams) | ||
| sharedPreferences.edit() | ||
| .putString(PreferenceOneSignalKeys.MODEL_STORE_PREFIX + configNameSpace, JSONArray().put(configModel).toString()) | ||
| .commit() | ||
|
|
||
| val mockApplicationService = mockk<IApplicationService>(relaxed = true) | ||
| every { mockApplicationService.appContext } returns appContext!! | ||
| every { mockApplicationService.appContext } returns appContext | ||
|
|
||
| val wrapper = OneSignalCrashUploaderWrapper(mockApplicationService) | ||
|
|
||
| // When | ||
| runBlocking { | ||
| wrapper.start() | ||
| } | ||
|
|
||
| // Then - start() should complete without throwing | ||
| // The actual uploader.start() is called internally via runBlocking | ||
| // If it throws, this test would fail | ||
| // Should complete without error even when no crash reports exist | ||
| runBlocking { wrapper.start() } | ||
| } | ||
|
|
||
| test("should handle errors gracefully when uploader fails") { | ||
| // Given | ||
| test("start can be called multiple times safely") { | ||
| val mockApplicationService = mockk<IApplicationService>(relaxed = true) | ||
| every { mockApplicationService.appContext } returns appContext!! | ||
| every { mockApplicationService.appContext } returns appContext | ||
|
|
||
| val wrapper = OneSignalCrashUploaderWrapper(mockApplicationService) | ||
|
|
||
| // When/Then - start() should handle errors gracefully | ||
| // If remote logging is disabled, it should return early without error | ||
| // If there are no crash reports, it should complete without error | ||
| // Multiple calls should not throw | ||
| runBlocking { | ||
| // This should not throw even if there are no crash reports or if remote logging is disabled | ||
| wrapper.start() | ||
| wrapper.start() | ||
| } | ||
| } | ||
|
|
||
| test("should create wrapper with applicationService dependency") { | ||
| // Given | ||
| test("wrapper should be non-null after creation") { | ||
| val mockApplicationService = mockk<IApplicationService>(relaxed = true) | ||
| every { mockApplicationService.appContext } returns appContext!! | ||
| every { mockApplicationService.appContext } returns appContext | ||
|
|
||
| // When | ||
| val wrapper = OneSignalCrashUploaderWrapper(mockApplicationService) | ||
|
|
||
| // Then | ||
| wrapper shouldNotBe null | ||
| wrapper.shouldBeInstanceOf<OneSignalCrashUploaderWrapper>() | ||
| } | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.