Bug P4
Status Update
Comments
mt...@gmail.com <mt...@gmail.com> #2
Do you have a repro project that you could share with us?
There should be no need to excluding any dependencies: we automatically remove them from the test APK if they are in the main APK.
There should be no need to excluding any dependencies: we automatically remove them from the test APK if they are in the main APK.
ow...@google.com <ow...@google.com> #3
I'm having some trouble parsing exactly what you're saying.
It looks like you are talking about androidx.test
, not androidx.arch.core:core-testing
I believe you should be able to get by without a revokePermissionRule, by using checkSelfPermission
throughout your app, and writing tests without permissions granted.
I believe you should be able to provide your Context in any on-device test.
mt...@gmail.com <mt...@gmail.com> #4
Yes, sorry androidx.test component.
I have some JUNit test like:
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.GrantPermissionRule
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
/**
* @author mtrakal on 20.10.2020.
*/
@RunWith(AndroidJUnit4::class)
class PermissionHelperTest {
/**
* grant all permissions for current Instrumentation (all test cases!) and not possible to revoke it! :/
*/
@get:Rule
var grantPermissions: GrantPermissionRule = GrantPermissionRule.grant()
var helper: PermissionHelper = PermissionHelper()
var activityScenario: ActivityScenario<AppCompatActivity> = ActivityScenario.launch(AppCompatActivity::class.java)
var fragmentScenario: FragmentScenario<Fragment> = launchFragmentInContainer(factory = FragmentFactory())
@Test
fun `default provider`() {
Assert.assertTrue(helper.provider is PermissionProviderDexterImpl)
}
@Test
fun `camera permission granted`() {
activityScenario.onActivity {
helper.requestCamera(
it,
permissionGranted = { Assert.assertTrue(true) },
permissionDenied = { Assert.assertTrue(false) }
)
}
}
// TODO: Not possible test this case
@Test
fun `camera permission denied`() {
activityScenario.onActivity {
helper.requestCamera(
it,
permissionGranted = { Assert.assertTrue(false) },
permissionDenied = { Assert.assertTrue(true) }
)
}
}
}
In this scenario, I need to test the opposite scenario too. Not just permissionGranted, but permissionDenied too. But it's not possible, because at the beginning I grant permissions through:
@get:Rule
var grantPermissions: GrantPermissionRule = GrantPermissionRule.grant()
this allow permissions, but after I'm not able test permissionDenied.
Or is there some other way, than split code to 2 files and in one have GrantPermissionRule call and in second file don't have it? It's crazy workaround have more files just for permissions...
I have some JUNit test like:
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.GrantPermissionRule
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
/**
* @author mtrakal on 20.10.2020.
*/
@RunWith(AndroidJUnit4::class)
class PermissionHelperTest {
/**
* grant all permissions for current Instrumentation (all test cases!) and not possible to revoke it! :/
*/
@get:Rule
var grantPermissions: GrantPermissionRule = GrantPermissionRule.grant()
var helper: PermissionHelper = PermissionHelper()
var activityScenario: ActivityScenario<AppCompatActivity> = ActivityScenario.launch(AppCompatActivity::class.java)
var fragmentScenario: FragmentScenario<Fragment> = launchFragmentInContainer(factory = FragmentFactory())
@Test
fun `default provider`() {
Assert.assertTrue(helper.provider is PermissionProviderDexterImpl)
}
@Test
fun `camera permission granted`() {
activityScenario.onActivity {
helper.requestCamera(
it,
permissionGranted = { Assert.assertTrue(true) },
permissionDenied = { Assert.assertTrue(false) }
)
}
}
// TODO: Not possible test this case
@Test
fun `camera permission denied`() {
activityScenario.onActivity {
helper.requestCamera(
it,
permissionGranted = { Assert.assertTrue(false) },
permissionDenied = { Assert.assertTrue(true) }
)
}
}
}
In this scenario, I need to test the opposite scenario too. Not just permissionGranted, but permissionDenied too. But it's not possible, because at the beginning I grant permissions through:
@get:Rule
var grantPermissions: GrantPermissionRule = GrantPermissionRule.grant()
this allow permissions, but after I'm not able test permissionDenied.
Or is there some other way, than split code to 2 files and in one have GrantPermissionRule call and in second file don't have it? It's crazy workaround have more files just for permissions...
ae...@google.com <ae...@google.com> #5
This bug has not been updated in the last year. Please reopen if we still need to look into this.
Description
JUnit test:
@RunWith(AndroidJUnit4::class)
class PermissionsTest {
var context: Context = ApplicationProvider.getApplicationContext()
/**
* This didn't work, due to missing context parameter / context NPE
*/
private fun grantPermission(vararg permissions: String) {
PermissionRequester().apply {
addPermissions(*permissions)
requestPermissions()
}
}
}
java.lang.NullPointerException
at androidx.test.internal.util.Checks.checkNotNull(Checks.java:34)
at androidx.test.runner.permission.UiAutomationShellCommand.<init>(UiAutomationShellCommand.java:65)
at androidx.test.runner.permission.PermissionRequester.addPermissions(PermissionRequester.java:92)
at cz.bsc.mobileproduct.permissions.impl.PermissionHelperTest.grantPermission(PermissionHelperTest.kt:135)
at cz.bsc.mobileproduct.permissions.impl.PermissionHelperTest.request camera activity(PermissionHelperTest.kt:53)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
Not possible to provide my test context from var context: Context = ApplicationProvider.getApplicationContext()
but it should be same as from non-param constructor, but it fail on this.