-
Notifications
You must be signed in to change notification settings - Fork 1k
[PM-40488] fix: clear clipboard setting does not clear the clipboard on Android 13+ #7169
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
mvanhorn
wants to merge
3
commits into
bitwarden:main
Choose a base branch
from
mvanhorn:fix/7069-clear-clipboard-not-clearing
base: main
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 1 commit
Commits
Show all changes
3 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
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
99 changes: 99 additions & 0 deletions
99
...st/kotlin/com/x8bit/bitwarden/data/platform/manager/clipboard/ClearClipboardWorkerTest.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,99 @@ | ||
| package com.x8bit.bitwarden.data.platform.manager.clipboard | ||
|
|
||
| import android.content.ClipData | ||
| import android.content.ClipDescription | ||
| import android.content.ClipboardManager | ||
| import android.content.Context | ||
| import android.os.Build | ||
| import androidx.work.ListenableWorker | ||
| import com.bitwarden.ui.platform.base.BaseRobolectricTest | ||
| import io.mockk.mockk | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.robolectric.RuntimeEnvironment | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| class ClearClipboardWorkerTest : BaseRobolectricTest() { | ||
|
|
||
| private lateinit var clipboardManager: ClipboardManager | ||
| private lateinit var worker: ClearClipboardWorker | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| val context = RuntimeEnvironment.getApplication() | ||
| clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager | ||
| worker = ClearClipboardWorker( | ||
| appContext = context, | ||
| workerParams = mockk(relaxed = true), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be done inline above? private val context = RuntimeEnvironment.getApplication()
private val clipboardManager = context.getSystemService<ClipboardManager>()
private val worker: ClearClipboardWorker = ClearClipboardWorker(
appContext = context,
workerParams = mockk(relaxed = true),
)or do these tests require a new instance each time? |
||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `doWork should clear a populated clipboard and return success`() { | ||
| clipboardManager.setPrimaryClip(ClipData.newPlainText("", "password")) | ||
|
|
||
| val result = worker.doWork() | ||
|
|
||
| assertTrue(clipboardManager.primaryClip?.getItemAt(0)?.text.isNullOrEmpty()) | ||
| assertEquals(ListenableWorker.Result.success(), result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `doWork should return success when the clipboard is already empty`() { | ||
| clipboardManager.clearPrimaryClip() | ||
|
|
||
| val result = worker.doWork() | ||
|
|
||
| assertFalse(clipboardManager.hasPrimaryClip()) | ||
| assertEquals(ListenableWorker.Result.success(), result) | ||
| } | ||
|
|
||
| @Config(sdk = [Build.VERSION_CODES.TIRAMISU]) | ||
| @Test | ||
| fun `doWork should overwrite the clipboard with sensitive empty content before clearing`() { | ||
| val overwrittenClip = captureEmptyClipboardOverwrite() | ||
|
|
||
| assertTrue( | ||
| overwrittenClip | ||
| ?.description | ||
| ?.extras | ||
| ?.getBoolean(ClipDescription.EXTRA_IS_SENSITIVE) == true, | ||
| ) | ||
| } | ||
|
|
||
| @Config(sdk = [Build.VERSION_CODES.S_V2]) | ||
| @Test | ||
| fun `doWork should use the legacy sensitive extra before Android 13`() { | ||
| val overwrittenClip = captureEmptyClipboardOverwrite() | ||
|
|
||
| assertTrue( | ||
| overwrittenClip | ||
| ?.description | ||
| ?.extras | ||
| ?.getBoolean("android.content.extra.IS_SENSITIVE") == true, | ||
| ) | ||
| } | ||
|
|
||
| private fun captureEmptyClipboardOverwrite(): ClipData? { | ||
| var overwrittenClip: ClipData? = null | ||
| val listener = ClipboardManager.OnPrimaryClipChangedListener { | ||
| val currentClip = clipboardManager.primaryClip | ||
| if ( | ||
| overwrittenClip == null && | ||
| currentClip?.getItemAt(0)?.text.isNullOrEmpty() | ||
| ) { | ||
| overwrittenClip = currentClip | ||
| } | ||
| } | ||
| clipboardManager.setPrimaryClip(ClipData.newPlainText("", "password")) | ||
| clipboardManager.addPrimaryClipChangedListener(listener) | ||
|
|
||
| worker.doWork() | ||
|
|
||
| clipboardManager.removePrimaryClipChangedListener(listener) | ||
| return overwrittenClip | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this version check here?