Karumi / Dexter

Android library that simplifies the process of requesting permissions at runtime.
http://karumi.com
Apache License 2.0
5.23k stars 671 forks source link

Use of Dexter when running Roboelectric unit test #192

Open joreilly opened 6 years ago

joreilly commented 6 years ago

Is there any recommended way of using Dexter when running as part of Roboelectric unit test? For example, is there a shadow version of Dexter that can be used?

pedrovgs commented 6 years ago

Hi @joreilly we don't have any shadow for Dexter and I don't think we need it. As Dexter is just a wrapper on top of the Android Permissions system, if you have a shadow for the Android SDK permissions API you won't need any shadow for this library. Is this the answer to your question?

joreilly commented 6 years ago

Hi @pedrovgs, I had thought that as well but get following crash when I unit test activity that uses Dexter

java.lang.VerifyError: Expecting a stackmap frame at branch target 23
Exception Details:
  Location:
    com/karumi/dexter/Dexter.check()V @0: aload_0
  Reason:
    Expected stackmap frame at this location.
  Bytecode:
    0x0000000: 2ab7 001b 4cb2 0014 2ab4 0015 2ab4 0016
    0x0000010: 2bb6 001e a700 114c 2ab4 0013 2bb4 0018
    0x0000020: b900 2e02 00b1                         
  Exception Handler Table:
    bci [0, 20] => handler: 23
pedrovgs commented 6 years ago

I have no idea how this is even possible... @Serchinastico any clue?

norbertschuler commented 4 years ago

Hi @pedrovgs, I had thought that as well but get following crash when I unit test activity that uses Dexter

Have you ever got a solution for this? We have a similar problem with Dexter 6.2.0 and roboletric 4.3.1 trying to run a unit test like this:

java.lang.VerifyError: Bad type on operand stack in putfield
Exception Details:
  Location:
    com/karumi/dexter/Dexter.$$robo$$com_karumi_dexter_Dexter$__constructor__(Landroid/content/Context;)V @10: putfield
  Reason:
    Type ‚android/content/Context‘ (current frame, stack[0]) is not assignable to ‚com/karumi/dexter/Dexter‘ (constant pool 44)
  Current Frame:
    bci: @10
    flags: { }
    locals: { ‚com/karumi/dexter/listener/multi/BaseMultiplePermissionsListener‘, ‚android/content/Context‘ }
    stack: { ‚android/content/Context‘, ‚com/karumi/dexter/listener/multi/BaseMultiplePermissionsListener‘ }
  Bytecode:
    0x0000000: 2bbb 0026 594b b700 2a2a b500 2cbb 002e
    0x0000010: 594b b700 2f2a b500 3103 b500 33b8 0036
    0x0000020: b1
pedrovgs commented 4 years ago

Hi @norbertschuler what you post there seems to be a crash in a forced downcasting :S My recommendation for you in this scenario is to wrap Dexter instances with a class and use a mock instead. This should be a valid workaround. Keep in mind you can use ShadowApplication in Robolectric in order to grant permissions when running your tests.

norbertschuler commented 4 years ago

@pedrovgs Thank you, but we have omitted creating a mock for Dexter now and just use it directly in Robolectric. We just granted access to our PermissionsListener object to the unit test, so the test can call onPermissionsChecked on it.