CallAbstractCheck with visitFunctionCall doesn't report issue

Hello Sonar Team !

context

I’m trying to implement a check to catch this issue :

window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) //Noncompliant {{You are keeping the screen turned on, this will drain the battery very fast. Only use this feature if strictly necessary}}

I have mocked the appropriate packages :

  • android.app.Activity
  • android.view.Window & android.view.WindowManager

My check is as follow

@Rule(key = "EC505")
class KeepScreenOnCheck : CallAbstractCheck() {
    override val functionsToVisit = listOf(
        FunMatcher(qualifier = "android.view.Window") {
            withNames(
                "addFlags",
            )
        },
    )

    override fun visitFunctionCall(
        callExpression: KtCallExpression,
        resolvedCall: ResolvedCall<*>,
        kotlinFileContext: KotlinFileContext
    ) {
        kotlinFileContext.reportIssue(callExpression.calleeExpression!!, ERROR_MESSAGE)
    }
}

and my sample

class KeepScreenOnCheckSample {
    fun foo(window: Window) {
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) //Noncompliant {{You are keeping the screen turned on, this will drain the battery very fast. Only use this feature if strictly necessary}}
    }
}

test output

org.junit.ComparisonFailure: ERROR: Expect some issues, but there's none. In file (KeepScreenOnCheckSample.kt:8)
[----------------------------------------------------------------------]
[ '-' means expected but not raised, '+' means raised but not expected ]
  <KeepScreenOnCheckSample.kt>
- 008: Noncompliant {{You are keeping the screen turned on, this will drain the battery very fast. Only use this feature if strictly necessary}}
- 008:         window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
[----------------------------------------------------------------------]
;  expected:<[<KeepScreenOnCheckSample.kt>
008: Noncompliant {{You are keeping the screen turned on, this will drain the battery very fast. Only use this feature if strictly necessary}}
008:         window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)]

> but was:<[ERROR: Expect some issues, but there's none.
<KeepScreenOnCheckSample.kt>]

For full code see this PR : EC505 - Keeping the screen turned on by 47tibo · Pull Request #8 · green-code-initiative/ecoCode-kotlin-android · GitHub

All advices are welcome :pray:

1 Like

Hello @Thibaut_Gilbert and welcome to the Sonar Community!

I see several problems in your PR.

  • The first issue is about the logic of your rule; if I understood correctly, you should mock the Window.addFlags(int) method: there is no Window.addFlags(WindowManager.LayoutParams)!

    • Please be consistent when adding the android.app.Activity, android.Window, and android.WindowManager by respecting the signatures of their methods: check the Android API Reference.
    • Make sure to add them as .java files!
  • Before reporting an issue in KeepScreenOnCheck you may want to check that the argument of the callExpression matching the FunMatcher is equal to the WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON!

  • Moving to the tests, you normally get the expected test when extending CheckTest, so you probably don’t need to specify any other test.

Please have a look at the AndroidBroadcastingCheck, it should be a valid example to help you get your implementation right!

Cheers,
Angelo