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

[Fragment] Add lint warning for calling setOnCancelListener and setOnDismissListener in onCreateDialog() #171

Closed

Conversation

tatocaster
Copy link
Contributor

Proposed Changes

When using a DialogFragment, the setOnCancelListener and setOnDismissListener callback functions within the onCreateDialog function must not be used because the DialogFragment owns these callbacks. Instead the respective onCancel and onDismiss functions can be used to achieve the desired effect.

Testing

Test: ./gradlew fragment:fragment-lint:test

Issues Fixed

Fixes: The bug on https://issuetracker.google.com/issues/181780047 being fixed

@dlam dlam requested a review from jbw0033 April 27, 2021 21:59

private const val ENTRY_METHOD = "onCreateDialog"
private const val DIALOG_FRAGMENT_CLASS = "DialogFragment"
private val callbacks = setOf("setOnCancelListener", "setOnDismissListener")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an extra line at the end of the file.

companion object Issues {
val ISSUE = Issue.create(
id = "DialogFragmentCallbacksDetector",
briefDescription = "Use onCancel() and onDismiss() callbacks to get the instead of " +
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Use onCancel() and onDismiss() instead of calling setOnCancelListener() and setOnDismissListener() from onCreateDialog()"

Instead the respective `onCancel` and `onDismiss` functions can be used to \
achieve the desired effect.""",
category = Category.CORRECTNESS,
priority = 9,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't normally set priority. You can remove this.

.run()
.expect(
"""
src/foo/TestFragment.java:11: Warning: Use onCancel() and onDismiss() callbacks to get the instead of calling setOnCancelListener() and setOnDismissListener() from onCreateDialog() [DialogFragmentCallbacksDetector]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will have to fix these to reflect the changed description above.

context.report(
issue = ISSUE,
location = context.getLocation(node),
message = "Use onCancel() and onDismiss() callbacks to get the instead of " +
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we actually separate this and report in the message based on whether setOnCancelListener or setOnDismissListener was called? I think being specific here would make it easier for developers to know what needs to be done instead.

* because the `DialogFragment` owns these callbacks. Instead the respective `onCancel` and
* `onDismiss` functions can be used to achieve the desired effect.
*/
class DialogFragmentCallbacksDetector : Detector(), SourceCodeScanner {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be more specific in the naming here. Something like OnCreateDialogIncorrectCallbackDetector


/* ktlint-disable max-line-length */
@RunWith(JUnit4::class)
class DialogFragmentCallbacksDetectorTest : LintDetectorTest() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the reports are separated out, I think we should add individual tests for using single callbacks.

@jbw0033
Copy link

jbw0033 commented May 5, 2021

From our internal lint:

fragment/fragment-lint/src/main/java/androidx/fragment/lint/OnCreateDialogIncorrectCallbackDetector.kt
#56
Patchset 1
9:12 AM
Lint 🤖
Please remove the trailing whitespace.

fragment/fragment-lint/src/test/java/androidx/fragment/lint/OnCreateDialogIncorrectCallbackDetectorTest.kt
#71
Patchset 1
9:12 AM
Lint 🤖
Please remove the trailing whitespace.

@tatocaster
Copy link
Contributor Author

@jbw0033
I still do not understand how internal linter works, when I use ./gradlew lint it passes. ¯_(ツ)_/¯
Thanks for your time and for the review 👍

@jbw0033
Copy link

jbw0033 commented May 5, 2021

Got more errors:

$SUPPORT/fragment/fragment-lint/src/main/java/androidx/fragment/lint/OnCreateDialogIncorrectCallbackDetector.kt:27:1: Unused import
$SUPPORT/fragment/fragment-lint/src/main/java/androidx/fragment/lint/OnCreateDialogIncorrectCallbackDetector.kt:159:1: Needless blank line(s)

You can try ./gradlew :fragment:fragment-lint:ktlint

@tatocaster
Copy link
Contributor Author

LOL, I was only checking Android Lint.
Thanks for guiding. It's all ok now. Will consider these hints for future PRs.

@copybara-service copybara-service bot closed this in 95ca5e2 May 6, 2021
@jbw0033
Copy link

jbw0033 commented May 6, 2021

Thank you for the pull request!

tadfisher pushed a commit to tadfisher/androidx that referenced this pull request Feb 19, 2022
* update runtime tests configuration for js and native

Change-Id: I9de7f64c2657cbe9cde979867374d65cd508b8ca

* add comments about ignored tests

Change-Id: I7caa9ee0762c715deeef761c3851f5f52d91dbc5

Co-authored-by: Oleksandr Karpovich <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants