-
-
Notifications
You must be signed in to change notification settings - Fork 462
feat(replay): Add Mask/Unmask Containers for custom masking in hybrid SDKs #3881
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
Merged
+283
−0
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2dc6e5a
feat(replay): Add Mask/Unmask Containers for custom masking in hybrid…
krystofwoldrich 8398ec2
Format code
getsentry-bot 698ef11
Merge branch 'main' into kw/replay/add-mask-containers-for-hybrids
krystofwoldrich 8d18edf
Address PR feedback
romtsn 7432076
Merge branch 'main' into kw/replay/add-mask-containers-for-hybrids
romtsn 84596dc
Fix changelog
romtsn 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
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
231 changes: 231 additions & 0 deletions
231
...eplay/src/test/java/io/sentry/android/replay/viewhierarchy/ContainerMaskingOptionsTest.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,231 @@ | ||
| package io.sentry.android.replay.viewhierarchy | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Context | ||
| import android.graphics.Canvas | ||
| import android.graphics.Color | ||
| import android.graphics.drawable.Drawable | ||
| import android.os.Bundle | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.ImageView | ||
| import android.widget.LinearLayout | ||
| import android.widget.LinearLayout.LayoutParams | ||
| import android.widget.TextView | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import io.sentry.SentryOptions | ||
| import io.sentry.android.replay.maskAllImages | ||
| import io.sentry.android.replay.maskAllText | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.Robolectric.buildActivity | ||
| import org.robolectric.annotation.Config | ||
| import kotlin.test.BeforeTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| @Config(sdk = [30]) | ||
| class ContainerMaskingOptionsTest { | ||
|
|
||
| @BeforeTest | ||
| fun setup() { | ||
| System.setProperty("robolectric.areWindowsMarkedVisible", "true") | ||
| } | ||
|
|
||
| @Test | ||
| fun `when maskAllText is set TextView in Unmask container is unmasked`() { | ||
| buildActivity(MaskingOptionsActivity::class.java).setup() | ||
|
|
||
| val options = SentryOptions().apply { | ||
| experimental.sessionReplay.maskAllText = true | ||
| experimental.sessionReplay.setUnmaskViewContainerClass(CustomUnmask::class.java.name) | ||
| } | ||
|
|
||
| val textNode = ViewHierarchyNode.fromView(MaskingOptionsActivity.textViewInUnmask!!, null, 0, options) | ||
| assertFalse(textNode.shouldMask) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when maskAllImages is set ImageView in Unmask container is unmasked`() { | ||
| buildActivity(MaskingOptionsActivity::class.java).setup() | ||
|
|
||
| val options = SentryOptions().apply { | ||
| experimental.sessionReplay.maskAllImages = true | ||
| experimental.sessionReplay.setUnmaskViewContainerClass(CustomUnmask::class.java.name) | ||
| } | ||
|
|
||
| val imageNode = ViewHierarchyNode.fromView(MaskingOptionsActivity.imageViewInUnmask!!, null, 0, options) | ||
| assertFalse(imageNode.shouldMask) | ||
| } | ||
|
|
||
| @Test | ||
| fun `MaskContainer is always masked`() { | ||
| buildActivity(MaskingOptionsActivity::class.java).setup() | ||
|
|
||
| val options = SentryOptions().apply { | ||
| experimental.sessionReplay.setMaskViewContainerClass(CustomMask::class.java.name) | ||
| } | ||
|
|
||
| val maskContainer = ViewHierarchyNode.fromView(MaskingOptionsActivity.maskWithChildren!!, null, 0, options) | ||
|
|
||
| assertTrue(maskContainer.shouldMask) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when Views are in UnmaskContainer only direct children are unmasked`() { | ||
| buildActivity(MaskingOptionsActivity::class.java).setup() | ||
|
|
||
| val options = SentryOptions().apply { | ||
| experimental.sessionReplay.addMaskViewClass(CustomView::class.java.name) | ||
| experimental.sessionReplay.setUnmaskViewContainerClass(CustomUnmask::class.java.name) | ||
| } | ||
|
|
||
| val maskContainer = ViewHierarchyNode.fromView(MaskingOptionsActivity.unmaskWithChildren!!, null, 0, options) | ||
| val firstChild = ViewHierarchyNode.fromView(MaskingOptionsActivity.customViewInUnmask!!, maskContainer, 0, options) | ||
| val secondLevelChild = ViewHierarchyNode.fromView(MaskingOptionsActivity.secondLayerChildInUnmask!!, firstChild, 0, options) | ||
|
|
||
| assertFalse(maskContainer.shouldMask) | ||
| assertFalse(firstChild.shouldMask) | ||
| assertTrue(secondLevelChild.shouldMask) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when MaskContainer is direct child of UnmaskContainer all children od Mask are masked`() { | ||
| buildActivity(MaskingOptionsActivity::class.java).setup() | ||
|
|
||
| val options = SentryOptions().apply { | ||
| experimental.sessionReplay.setMaskViewContainerClass(CustomMask::class.java.name) | ||
| experimental.sessionReplay.setUnmaskViewContainerClass(CustomUnmask::class.java.name) | ||
| } | ||
|
|
||
| val unmaskNode = ViewHierarchyNode.fromView(MaskingOptionsActivity.unmaskWithMaskChild!!, null, 0, options) | ||
| val maskNode = ViewHierarchyNode.fromView(MaskingOptionsActivity.maskAsDirectChildOfUnmask!!, unmaskNode, 0, options) | ||
|
|
||
| assertFalse(unmaskNode.shouldMask) | ||
| assertTrue(maskNode.shouldMask) | ||
| } | ||
|
|
||
| private class CustomView(context: Context) : View(context) { | ||
| override fun onDraw(canvas: Canvas) { | ||
| super.onDraw(canvas) | ||
| canvas.drawColor(Color.BLACK) | ||
| } | ||
| } | ||
|
|
||
| private open class CustomGroup(context: Context) : LinearLayout(context) { | ||
| init { | ||
| setBackgroundColor(android.R.color.white) | ||
| orientation = VERTICAL | ||
| layoutParams = LayoutParams(100, 100) | ||
| } | ||
|
|
||
| override fun onDraw(canvas: Canvas) { | ||
| super.onDraw(canvas) | ||
| canvas.drawColor(Color.BLACK) | ||
| } | ||
| } | ||
|
|
||
| private class CustomMask(context: Context) : CustomGroup(context) | ||
| private class CustomUnmask(context: Context) : CustomGroup(context) | ||
|
|
||
| private class MaskingOptionsActivity : Activity() { | ||
|
|
||
| companion object { | ||
| var unmaskWithTextView: ViewGroup? = null | ||
| var textViewInUnmask: TextView? = null | ||
|
|
||
| var unmaskWithImageView: ViewGroup? = null | ||
| var imageViewInUnmask: ImageView? = null | ||
|
|
||
| var unmaskWithChildren: ViewGroup? = null | ||
| var customViewInUnmask: ViewGroup? = null | ||
| var secondLayerChildInUnmask: View? = null | ||
|
|
||
| var maskWithChildren: ViewGroup? = null | ||
|
|
||
| var unmaskWithMaskChild: ViewGroup? = null | ||
| var maskAsDirectChildOfUnmask: ViewGroup? = null | ||
| } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| val linearLayout = LinearLayout(this).apply { | ||
| setBackgroundColor(android.R.color.white) | ||
| orientation = LinearLayout.VERTICAL | ||
| layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) | ||
| } | ||
|
|
||
| val context = this | ||
|
|
||
| linearLayout.addView( | ||
| CustomUnmask(context).apply { | ||
| unmaskWithTextView = this | ||
| this.addView( | ||
| TextView(context).apply { | ||
| textViewInUnmask = this | ||
| text = "Hello, World!" | ||
| layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
|
|
||
| linearLayout.addView( | ||
| CustomUnmask(context).apply { | ||
| unmaskWithImageView = this | ||
| this.addView( | ||
| ImageView(context).apply { | ||
| imageViewInUnmask = this | ||
| val image = this::class.java.classLoader.getResource("Tongariro.jpg")!! | ||
| setImageDrawable(Drawable.createFromPath(image.path)) | ||
| layoutParams = LayoutParams(50, 50).apply { | ||
| setMargins(0, 16, 0, 0) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
|
|
||
| linearLayout.addView( | ||
| CustomUnmask(context).apply { | ||
| unmaskWithChildren = this | ||
| this.addView( | ||
| CustomGroup(context).apply { | ||
| customViewInUnmask = this | ||
| this.addView( | ||
| CustomView(context).apply { | ||
| secondLayerChildInUnmask = this | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
|
|
||
| linearLayout.addView( | ||
| CustomMask(context).apply { | ||
| maskWithChildren = this | ||
| this.addView( | ||
| CustomGroup(context).apply { | ||
| this.addView(CustomView(context)) | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
|
|
||
| linearLayout.addView( | ||
| CustomUnmask(context).apply { | ||
| unmaskWithMaskChild = this | ||
| this.addView( | ||
| CustomMask(context).apply { | ||
| maskAsDirectChildOfUnmask = this | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
|
|
||
| setContentView(linearLayout) | ||
| } | ||
| } | ||
| } |
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
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
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.