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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attach-screenshot set on Manual init. didn't work #2186

Merged
merged 6 commits into from
Aug 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- `attach-screenshot` set on Manual init. didn't work ([#2186](https://github.com/getsentry/sentry-java/pull/2186))
- Remove extra space from `spring.factories` causing issues in old versions of Spring Boot ([#2181](https://github.com/getsentry/sentry-java/pull/2181))

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public final class ScreenshotEventProcessor
private final @NotNull Application application;
private final @NotNull SentryAndroidOptions options;
private @Nullable WeakReference<Activity> currentActivity;
final @NotNull BuildInfoProvider buildInfoProvider;
private final @NotNull BuildInfoProvider buildInfoProvider;
private boolean lifecycleCallbackInstalled = true;

public ScreenshotEventProcessor(
final @NotNull Application application,
Expand All @@ -47,27 +48,29 @@ public ScreenshotEventProcessor(
this.buildInfoProvider =
Objects.requireNonNull(buildInfoProvider, "BuildInfoProvider is required");

if (this.options.isAttachScreenshot()) {
application.registerActivityLifecycleCallbacks(this);
application.registerActivityLifecycleCallbacks(this);
}

@SuppressWarnings("NullAway")
@Override
public @NotNull SentryEvent process(final @NotNull SentryEvent event, @NotNull Hint hint) {
if (!lifecycleCallbackInstalled) {
return event;
}
if (!options.isAttachScreenshot()) {
application.unregisterActivityLifecycleCallbacks(this);
lifecycleCallbackInstalled = false;

this.options
.getLogger()
.log(
SentryLevel.DEBUG,
"attachScreenshot is enabled, ScreenshotEventProcessor is installed.");
} else {
this.options
.getLogger()
.log(
SentryLevel.DEBUG,
"attachScreenshot is disabled, ScreenshotEventProcessor isn't installed.");

return event;
}
}

@SuppressWarnings("NullAway")
@Override
public @NotNull SentryEvent process(final @NotNull SentryEvent event, @NotNull Hint hint) {
if (options.isAttachScreenshot() && event.isErrored() && currentActivity != null) {
if (event.isErrored() && currentActivity != null) {
final Activity activity = currentActivity.get();
if (isActivityValid(activity)
&& activity.getWindow() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,12 @@ class ScreenshotEventProcessorTest {
}

@Test
fun `when attach screenshot is enabled, registerActivityLifecycleCallbacks`() {
fixture.getSut(true)
fun `when adding screenshot event processor, registerActivityLifecycleCallbacks`() {
fixture.getSut()

verify(fixture.application).registerActivityLifecycleCallbacks(any())
}

@Test
fun `when attach screenshot is disabled, does not registerActivityLifecycleCallbacks`() {
fixture.getSut(false)

verify(fixture.application, never()).registerActivityLifecycleCallbacks(any())
}

@Test
fun `when close is called and attach screenshot is enabled, unregisterActivityLifecycleCallbacks`() {
val sut = fixture.getSut(true)
Expand All @@ -85,16 +78,27 @@ class ScreenshotEventProcessorTest {

@Test
fun `when close is called and attach screenshot is disabled, does not unregisterActivityLifecycleCallbacks`() {
val sut = fixture.getSut(false)
val sut = fixture.getSut()

sut.close()

verify(fixture.application, never()).unregisterActivityLifecycleCallbacks(any())
}

@Test
fun `when process is called and attachScreenshot is disabled, unregisterActivityLifecycleCallbacks`() {
val sut = fixture.getSut()
val hint = Hint()

val event = fixture.mainProcessor.process(getEvent(), hint)
sut.process(event, hint)

verify(fixture.application).unregisterActivityLifecycleCallbacks(any())
}

@Test
fun `when process is called and attachScreenshot is disabled, does nothing`() {
val sut = fixture.getSut(false)
val sut = fixture.getSut()
val hint = Hint()

sut.onActivityCreated(fixture.activity, null)
Expand Down