This repository was archived by the owner on Aug 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
added Android friendly init. #69
Merged
Merged
Changes from all commits
Commits
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
added Android friedly init.
- Loading branch information
commit 652da24f9f8b1b42cf29ff63d0e9f95431d9e76f
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package io.sentry.android.core; | ||
|
|
||
| import android.content.Context; | ||
| import io.sentry.core.Sentry; | ||
| import io.sentry.core.util.NonNull; | ||
|
|
||
| /** Sentry initialization class */ | ||
| public final class SentryAndroid { | ||
|
|
||
| private SentryAndroid() {} | ||
|
|
||
| /** | ||
| * Sentry initialization method if auto-init is disabled | ||
| * | ||
| * @param context Application. context | ||
| */ | ||
| public static void init(@NonNull final Context context) { | ||
| Sentry.init(options -> AndroidOptionsInitializer.init(options, context)); | ||
| } | ||
| } | ||
63 changes: 63 additions & 0 deletions
63
sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package io.sentry.android.core | ||
|
|
||
| import android.content.Context | ||
| import android.content.pm.ApplicationInfo | ||
| import android.content.pm.PackageManager | ||
| import android.os.Bundle | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import com.nhaarman.mockitokotlin2.doReturn | ||
| import com.nhaarman.mockitokotlin2.mock | ||
| import com.nhaarman.mockitokotlin2.whenever | ||
| import io.sentry.core.Sentry | ||
| import java.io.File | ||
| import kotlin.test.BeforeTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
| import org.junit.runner.RunWith | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class SentryAndroidTest { | ||
| private lateinit var context: Context | ||
|
|
||
| @BeforeTest | ||
| fun `set up`() { | ||
| context = ApplicationProvider.getApplicationContext() | ||
| Sentry.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `when applicationId is defined, dsn in meta-data, SDK initializes`() { | ||
| assertFalse(Sentry.isEnabled()) | ||
|
|
||
| val mockContext = createMockContext() | ||
| val metaData = Bundle() | ||
| mockMetaData(mockContext, metaData) | ||
|
|
||
| metaData.putString(ManifestMetadataReader.DSN_KEY, "https://[email protected]/123") | ||
|
|
||
| SentryAndroid.init(mockContext) | ||
|
|
||
| assertTrue(Sentry.isEnabled()) | ||
| } | ||
|
|
||
| private fun createMockContext(): Context { | ||
| val mockContext = mock<Context> { | ||
| on { applicationContext } doReturn context | ||
| } | ||
| whenever(mockContext.cacheDir).thenReturn(File("${File.separator}cache")) | ||
| return mockContext | ||
| } | ||
|
|
||
| private fun mockMetaData(mockContext: Context, metaData: Bundle) { | ||
| val mockPackageManager: PackageManager = mock() | ||
| val mockApplicationInfo: ApplicationInfo = mock() | ||
|
|
||
| whenever(mockContext.packageName).thenReturn("io.sentry.sample.test") | ||
| whenever(mockContext.packageManager).thenReturn(mockPackageManager) | ||
| whenever(mockPackageManager.getApplicationInfo(mockContext.packageName, PackageManager.GET_META_DATA)).thenReturn(mockApplicationInfo) | ||
|
|
||
| mockApplicationInfo.metaData = metaData | ||
| } | ||
| } |
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
17 changes: 17 additions & 0 deletions
17
sentry-sample/src/main/java/io/sentry/sample/MyApplication.java
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package io.sentry.sample; | ||
|
|
||
| import android.app.Application; | ||
|
|
||
| // import io.sentry.android.core.SentryAndroid; | ||
|
|
||
| /** Apps. main Application. */ | ||
| public class MyApplication extends Application { | ||
|
|
||
| @Override | ||
| public void onCreate() { | ||
| super.onCreate(); | ||
|
|
||
| // how to init. Sentry manually | ||
| // SentryAndroid.init(this); | ||
bruno-garcia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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.