Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add screenshot and view hierarchy to integration list #2698

Merged
merged 6 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## Features

- Add Screenshot and ViewHierarchy to integrations list ([#2698](https://github.com/getsentry/sentry-java/pull/2698))

### Fixes

- Use `configureScope` instead of `withScope` in `Hub.close()`. This ensures that the main scope releases the in-memory data when closing a hub instance. ([#2688](https://github.com/getsentry/sentry-java/pull/2688))
Expand Down
4 changes: 2 additions & 2 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public final class io/sentry/android/core/PhoneStateBreadcrumbsIntegration : io/
public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V
}

public final class io/sentry/android/core/ScreenshotEventProcessor : io/sentry/EventProcessor {
public final class io/sentry/android/core/ScreenshotEventProcessor : io/sentry/EventProcessor, io/sentry/IntegrationName {
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;Lio/sentry/android/core/BuildInfoProvider;)V
public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent;
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public final class io/sentry/android/core/UserInteractionIntegration : android/a
public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V
}

public final class io/sentry/android/core/ViewHierarchyEventProcessor : io/sentry/EventProcessor {
public final class io/sentry/android/core/ViewHierarchyEventProcessor : io/sentry/EventProcessor, io/sentry/IntegrationName {
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;)V
public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent;
public static fun snapshotViewHierarchy (Landroid/app/Activity;Lio/sentry/ILogger;)Lio/sentry/protocol/ViewHierarchy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.sentry.Attachment;
import io.sentry.EventProcessor;
import io.sentry.Hint;
import io.sentry.IntegrationName;
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.util.HintUtils;
Expand All @@ -20,7 +21,7 @@
* captured.
*/
@ApiStatus.Internal
public final class ScreenshotEventProcessor implements EventProcessor {
public final class ScreenshotEventProcessor implements EventProcessor, IntegrationName {

private final @NotNull SentryAndroidOptions options;
private final @NotNull BuildInfoProvider buildInfoProvider;
Expand All @@ -31,6 +32,9 @@ public ScreenshotEventProcessor(
this.options = Objects.requireNonNull(options, "SentryAndroidOptions is required");
this.buildInfoProvider =
Objects.requireNonNull(buildInfoProvider, "BuildInfoProvider is required");
if (options.isAttachScreenshot()) {
addIntegrationToSdkVersion();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.sentry.Hint;
import io.sentry.ILogger;
import io.sentry.ISerializer;
import io.sentry.IntegrationName;
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.android.core.internal.gestures.ViewUtils;
Expand All @@ -25,12 +26,15 @@

/** ViewHierarchyEventProcessor responsible for taking a snapshot of the current view hierarchy. */
@ApiStatus.Internal
public final class ViewHierarchyEventProcessor implements EventProcessor {
public final class ViewHierarchyEventProcessor implements EventProcessor, IntegrationName {

private final @NotNull SentryAndroidOptions options;

public ViewHierarchyEventProcessor(final @NotNull SentryAndroidOptions options) {
this.options = Objects.requireNonNull(options, "SentryAndroidOptions is required");
if (options.isAttachViewHierarchy()) {
addIntegrationToSdkVersion();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import io.sentry.Attachment
import io.sentry.Hint
import io.sentry.MainEventProcessor
import io.sentry.SentryEvent
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.TypeCheckHint.ANDROID_ACTIVITY
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertSame
import kotlin.test.assertTrue
Expand Down Expand Up @@ -153,5 +155,25 @@ class ScreenshotEventProcessorTest {
assertNull(hint.screenshot)
}

@Test
fun `when enabled, the feature is added to the integration list`() {
SentryIntegrationPackageStorage.getInstance().clearStorage()
val hint = Hint()
val sut = fixture.getSut(true)
val event = fixture.mainProcessor.process(getEvent(), hint)
sut.process(event, hint)
assertTrue(fixture.options.sdkVersion!!.integrationSet.contains("Screenshot"))
}

@Test
fun `when not enabled, the feature is not added to the integration list`() {
SentryIntegrationPackageStorage.getInstance().clearStorage()
val hint = Hint()
val sut = fixture.getSut(false)
val event = fixture.mainProcessor.process(getEvent(), hint)
sut.process(event, hint)
assertFalse(fixture.options.sdkVersion!!.integrationSet.contains("Screenshot"))
}

private fun getEvent(): SentryEvent = SentryEvent(Throwable("Throwable"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ class SentryAndroidOptionsTest {
assertFalse(sentryOptions.isEnableUserInteractionTracing)
}

@Test
fun `attach view hierarchy is disabled by default for Android`() {
val sentryOptions = SentryAndroidOptions()

assertFalse(sentryOptions.isAttachViewHierarchy)
}

private class CustomDebugImagesLoader : IDebugImagesLoader {
override fun loadDebugImages(): List<DebugImage>? = null
override fun clearDebugImages() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.sentry.Hint
import io.sentry.JsonSerializable
import io.sentry.JsonSerializer
import io.sentry.SentryEvent
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.TypeCheckHint
import io.sentry.protocol.SentryException
import org.junit.runner.RunWith
Expand All @@ -23,6 +24,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

@RunWith(AndroidJUnit4::class)
class ViewHierarchyEventProcessorTest {
Expand Down Expand Up @@ -263,6 +265,30 @@ class ViewHierarchyEventProcessorTest {
}
}

@Test
fun `when enabled, the feature is added to the integration list`() {
SentryIntegrationPackageStorage.getInstance().clearStorage()
val (event, hint) = fixture.process(
true,
SentryEvent().apply {
exceptions = listOf(SentryException())
}
)
assertTrue(fixture.options.sdkVersion!!.integrationSet.contains("ViewHierarchy"))
}

@Test
fun `when not enabled, the feature is not added to the integration list`() {
SentryIntegrationPackageStorage.getInstance().clearStorage()
val (event, hint) = fixture.process(
false,
SentryEvent().apply {
exceptions = listOf(SentryException())
}
)
assertFalse(fixture.options.sdkVersion!!.integrationSet.contains("ViewHierarchy"))
}

private fun mockedView(
x: Float,
y: Float,
Expand Down
3 changes: 2 additions & 1 deletion sentry/src/main/java/io/sentry/IntegrationName.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ default String getIntegrationName() {
.getSimpleName()
.replace("Sentry", "")
.replace("Integration", "")
.replace("Interceptor", "");
.replace("Interceptor", "")
.replace("EventProcessor", "");
}

default void addIntegrationToSdkVersion() {
Expand Down
Loading