Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #460 from permissions-dispatcher/kotlin-lint
Add kotlin test cases for lint module
- Loading branch information
Showing
7 changed files
with
289 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
88 changes: 88 additions & 0 deletions
88
lint/src/test/java/permissions/dispatcher/CallNeedsPermissionDetectorKtTest.kt
This file contains 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,88 @@ | ||
package permissions.dispatcher | ||
|
||
import org.intellij.lang.annotations.Language | ||
import org.junit.Test | ||
|
||
import com.android.tools.lint.checks.infrastructure.TestFiles.java | ||
import com.android.tools.lint.checks.infrastructure.TestFiles.kt | ||
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint | ||
import permissions.dispatcher.Utils.onNeedsPermission | ||
|
||
class CallNeedsPermissionDetectorKtTest { | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun callNeedsPermissionMethod() { | ||
@Language("kotlin") val foo = """ | ||
package com.example | ||
import permissions.dispatcher.NeedsPermission | ||
class Foo { | ||
@NeedsPermission("Test") | ||
fun fooBar() { | ||
} | ||
fun hoge() { | ||
fooBar() | ||
} | ||
} | ||
""".trimMargin() | ||
|
||
val expectedText = """ | ||
|src/com/example/Foo.kt:11: Error: Trying to access permission-protected method directly [CallNeedsPermission] | ||
| fooBar() | ||
| ~~~~~~~~ | ||
|1 errors, 0 warnings | ||
""".trimMargin() | ||
|
||
lint() | ||
.files( | ||
java(onNeedsPermission), | ||
kt(foo)) | ||
.issues(CallNeedsPermissionDetector.ISSUE) | ||
.run() | ||
.expect(expectedText) | ||
.expectErrorCount(1) | ||
.expectWarningCount(0) | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun callNeedsPermissionMethodNoError() { | ||
@Language("kotlin") val foo = """ | ||
package com.example | ||
class Foo { | ||
fun someMethod() { | ||
val baz = Baz() | ||
baz.noFooBar() | ||
} | ||
} | ||
""".trimMargin() | ||
|
||
@Language("kotlin") val baz = """ | ||
package com.example | ||
import permissions.dispatcher.NeedsPermission | ||
class Baz { | ||
@NeedsPermission("Test") | ||
fun fooBar() { | ||
} | ||
fun noFooBar() { | ||
} | ||
} | ||
""".trimMargin() | ||
|
||
lint() | ||
.files( | ||
java(onNeedsPermission), | ||
kt(foo), | ||
kt(baz)) | ||
.issues(CallNeedsPermissionDetector.ISSUE) | ||
.run() | ||
.expectClean() | ||
} | ||
} |
This file contains 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
98 changes: 98 additions & 0 deletions
98
lint/src/test/java/permissions/dispatcher/CallOnRequestPermissionsResultDetectorKtTest.kt
This file contains 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,98 @@ | ||
package permissions.dispatcher | ||
|
||
import org.intellij.lang.annotations.Language | ||
import org.junit.Test | ||
|
||
import com.android.tools.lint.checks.infrastructure.TestFiles.java | ||
import com.android.tools.lint.checks.infrastructure.TestFiles.kt | ||
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint | ||
import permissions.dispatcher.Utils.onNeedsPermission | ||
import permissions.dispatcher.Utils.onRationaleAnnotation | ||
import permissions.dispatcher.Utils.runtimePermission | ||
|
||
class CallOnRequestPermissionsResultDetectorKtTest { | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun callOnRequestPermissionsResultDetectorNoErrorForKotlin() { | ||
@Language("kotlin") val foo = """ | ||
package permissions.dispatcher | ||
@RuntimePermissions | ||
class Foo : android.app.Activity { | ||
fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | ||
onRequestPermissionsResult(requestCode, grantResults) | ||
} | ||
@NeedsPermission("Camera") | ||
fun showCamera() { | ||
} | ||
@OnShowRationale("Camera") | ||
fun someMethod() { | ||
} | ||
} | ||
""".trimMargin() | ||
|
||
@Language("kotlin") val generatedClass = """ | ||
package permissions.dispatcher | ||
fun Foo.onRequestPermissionsResult(requestCode: Int, grantResults: IntArray) { | ||
} | ||
""".trimMargin() | ||
|
||
lint() | ||
.files( | ||
java(runtimePermission), | ||
java(onNeedsPermission), | ||
java(onRationaleAnnotation), | ||
kt(generatedClass), | ||
kt(foo)) | ||
.issues(CallOnRequestPermissionsResultDetector.ISSUE) | ||
.run() | ||
.expectClean() | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun callOnRequestPermissionsResultDetector() { | ||
@Language("kotlin") val foo = """ | ||
package permissions.dispatcher | ||
@RuntimePermissions | ||
class Foo: android.app.Activity { | ||
fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | ||
} | ||
@NeedsPermission("Camera") | ||
fun showCamera() { | ||
} | ||
@OnShowRationale("Camera") | ||
fun someMethod() { | ||
} | ||
} | ||
""".trimMargin() | ||
|
||
val expectedText = """ | ||
|src/permissions/dispatcher/Foo.kt:5: Error: Generated onRequestPermissionsResult method not called [NeedOnRequestPermissionsResult] | ||
| fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { | ||
| ^ | ||
|1 errors, 0 warnings | ||
""".trimMargin() | ||
|
||
lint() | ||
.files( | ||
java(runtimePermission), | ||
java(onNeedsPermission), | ||
java(onRationaleAnnotation), | ||
kt(foo)) | ||
.issues(CallOnRequestPermissionsResultDetector.ISSUE) | ||
.run() | ||
.expect(expectedText) | ||
.expectErrorCount(1) | ||
.expectWarningCount(0) | ||
} | ||
} |
This file contains 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.