facebook / screenshot-tests-for-android

Generate fast deterministic screenshots during Android instrumentation tests
http://facebook.github.io/screenshot-tests-for-android
Apache License 2.0
1.74k stars 229 forks source link

NoSuchMethodException on v0.10.0 #224

Open doodla opened 5 years ago

doodla commented 5 years ago
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.company.mobile.beta.debug, PID: 6572
    java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.content.Context, class android.view.Display]
        at com.facebook.testing.screenshot.WindowAttachment.generateAttachInfo(WindowAttachment.java:230)
        at com.facebook.testing.screenshot.WindowAttachment.dispatchAttach(WindowAttachment.java:89)
        at com.facebook.testing.screenshot.internal.ScreenshotImpl.storeBitmap(ScreenshotImpl.java:182)
        at com.facebook.testing.screenshot.internal.ScreenshotImpl.record(ScreenshotImpl.java:270)
        at com.facebook.testing.screenshot.internal.RecordBuilderImpl.record(RecordBuilderImpl.java:152)
        at com.company.mobile.CkScreenshotTest.screenshotWithName(CkScreenshotTest.kt:88)
        at com.company.mobile.CkScreenshotTest.screenshotWithName$default(CkScreenshotTest.kt:63)
        at com.company.mobile.dashboard.ui.advicecards.AdviceCardTest$captureHomeCreditScoreAdviceCardWithPersonalLoanHouseAd$1.invoke(AdviceCardTest.kt:144)
        at com.company.mobile.dashboard.ui.advicecards.AdviceCardTest$captureHomeCreditScoreAdviceCardWithPersonalLoanHouseAd$1.invoke(AdviceCardTest.kt:33)
        at com.company.mobile.CkUiTest$sam$java_lang_Runnable$0.run(Unknown Source:2)
        at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:2163)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, class android.view.Display]
        at java.lang.Class.getConstructor0(Class.java:2327)
        at java.lang.Class.getConstructor(Class.java:1725)
        at com.facebook.testing.screenshot.WindowAttachment.generateAttachInfo(WindowAttachment.java:163)
        at com.facebook.testing.screenshot.WindowAttachment.dispatchAttach(WindowAttachment.java:89) 
        at com.facebook.testing.screenshot.internal.ScreenshotImpl.storeBitmap(ScreenshotImpl.java:182) 
        at com.facebook.testing.screenshot.internal.ScreenshotImpl.record(ScreenshotImpl.java:270) 
        at com.facebook.testing.screenshot.internal.RecordBuilderImpl.record(RecordBuilderImpl.java:152) 
        at com.company.mobile.CkScreenshotTest.screenshotWithName(CkScreenshotTest.kt:88) 
        at com.company.mobile.CkScreenshotTest.screenshotWithName$default(CkScreenshotTest.kt:63) 
        at com.company.mobile.dashboard.ui.advicecards.AdviceCardTest$captureHomeCreditScoreAdviceCardWithPersonalLoanHouseAd$1.invoke(AdviceCardTest.kt:144) 
        at com.company.mobile.dashboard.ui.advicecards.AdviceCardTest$captureHomeCreditScoreAdviceCardWithPersonalLoanHouseAd$1.invoke(AdviceCardTest.kt:33) 
        at com.company.mobile.CkUiTest$sam$java_lang_Runnable$0.run(Unknown Source:2) 
        at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:2163) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.content.Context, class android.view.Display]
at com.facebook.testing.screenshot.WindowAttachment.generateAttachInfo(WindowAttachment.java:230)
at com.facebook.testing.screenshot.WindowAttachment.dispatchAttach(WindowAttachment.java:89)
at com.facebook.testing.screenshot.internal.ScreenshotImpl.storeBitmap(ScreenshotImpl.java:182)
at com.facebook.testing.screenshot.internal.ScreenshotImpl.record(ScreenshotImpl.java:270)
at com.facebook.testing.screenshot.internal.RecordBuilderImpl.record(RecordBuilderImpl.java:152)
at com.company.mobile.CkScreenshotTest.screenshotWithName(CkScreenshotTest.kt:88)
at com.company.mobile.CkScreenshotTest.screenshotWithName$default(CkScreenshotTest.kt:63)
at com.company.mobile.dashboard.ui.advicecards.AdviceCardTest$captureHomeCreditScoreAdviceCardWithPersonalLoanHouseAd$1.invoke(AdviceCardTest.kt:144)
at com.company.mobile.dashboard.ui.advicecards.AdviceCardTest$captureHomeCreditScoreAdviceCardWithPersonalLoanHouseAd$1.invoke(AdviceCardTest.kt:33)
at com.company.mobile.CkUiTest$sam$java_lang_Runnable$0.run(Unknown Source:2)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:2163)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, class android.view.Display]
at java.lang.Class.getConstructor0(Class.java:2327)
at java.lang.Class.getConstructor(Class.java:1725)
at com.facebook.testing.screenshot.WindowAttachment.generateAttachInfo(WindowAttachment.java:163)
... 17 more

It seems to fail on

        viewRootImpl =
            cViewRootImpl
                .getConstructor(Context.class, Display.class)
                .newInstance(context, display);

in WindowAttachment.java.

doodla commented 5 years ago

Although I'm not sure why.

 public ViewRootImpl(Context context, Display display) {
        mContext = context;
        mWindowSession = WindowManagerGlobal.getWindowSession();
        mDisplay = display;
        mBasePackageName = context.getBasePackageName();
        mThread = Thread.currentThread();
        mLocation = new WindowLeaked(null);
        mLocation.fillInStackTrace();
....

The constructor seems to exist.

xiphirx commented 5 years ago

What API level?

doodla commented 5 years ago

28

xiphirx commented 5 years ago

Successfully repro'd, looking. Thanks!

xiphirx commented 5 years ago

This is due to the non sdk interface restrictions that came with Android 9+

To get around this for testing, you need to run

adb shell settings put global hidden_api_policy_p_apps 1
adb shell settings put global hidden_api_policy_pre_p_apps 1
adb shell settings put global hidden_api_policy  1 // this is for Q+

And it will work. It seems like we can likely put this in the framework, but we also need to evaluate if we still need all of the reflection hackery...

karabaralex commented 5 years ago

adb settings above did not help. However it's different crash now: 04:23:33 V/InstrumentationResultParser: Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 04:23:33 V/InstrumentationResultParser: at com.facebook.testing.screenshot.WindowAttachment.generateAttachInfo(WindowAttachment.java:230) 04:23:33 V/InstrumentationResultParser: at com.facebook.testing.screenshot.WindowAttachment.dispatchAttach(WindowAttachment.java:89) 04:23:33 V/InstrumentationResultParser: at com.facebook.testing.screenshot.internal.ScreenshotImpl.storeBitmap(ScreenshotImpl.java:182) 04:23:33 V/InstrumentationResultParser: at com.facebook.testing.screenshot.internal.ScreenshotImpl.access$000(ScreenshotImpl.java:50) 04:23:33 V/InstrumentationResultParser: at com.facebook.testing.screenshot.internal.ScreenshotImpl$2.call(ScreenshotImpl.java:170) 04:23:33 V/InstrumentationResultParser: at com.facebook.testing.screenshot.internal.ScreenshotImpl$2.call(ScreenshotImpl.java:167) 04:23:33 V/InstrumentationResultParser: at com.facebook.testing.screenshot.internal.ScreenshotImpl$3.run(ScreenshotImpl.java:327) 04:23:33 V/InstrumentationResultParser: at android.os.Handler.handleCallback(Handler.java:873) 04:23:33 V/InstrumentationResultParser: at android.os.Handler.dispatchMessage(Handler.java:99) 04:23:33 V/InstrumentationResultParser: at android.os.Looper.loop(Looper.java:214) 04:23:33 V/InstrumentationResultParser: at android.app.ActivityThread.main(ActivityThread.java:7045) 04:23:33 V/InstrumentationResultParser: at java.lang.reflect.Method.invoke(Native Method) 04:23:33 V/InstrumentationResultParser: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 04:23:33 V/InstrumentationResultParser: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) 04:23:33 V/InstrumentationResultParser: Caused by: java.lang.reflect.InvocationTargetException 04:23:33 V/InstrumentationResultParser: at java.lang.reflect.Constructor.newInstance0(Native Method) 04:23:33 V/InstrumentationResultParser: at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 04:23:33 V/InstrumentationResultParser: at com.facebook.testing.screenshot.WindowAttachment.invokeConstructor(WindowAttachment.java:238) 04:23:33 V/InstrumentationResultParser: at com.facebook.testing.screenshot.WindowAttachment.generateAttachInfo(WindowAttachment.java:219) 04:23:33 V/InstrumentationResultParser: ... 13 more 04:23:33 V/InstrumentationResultParser: Caused by: java.lang.NullPointerException: Expected to unbox a 'boolean' primitive type but was returned null 04:23:33 V/InstrumentationResultParser: at $Proxy8.isCoverOpen(Unknown Source) 04:23:33 V/InstrumentationResultParser: at android.view.View$AttachInfo.(View.java:28669) 04:23:33 V/InstrumentationResultParser: ... 17 more

xiphirx commented 5 years ago

Thats a separate issue with running the tests on Samsung Devices. Samsung has extra methods that are non standard they expect on their AttachInfo class, a fix similar to https://github.com/facebook/screenshot-tests-for-android/commit/0b9e8e90679f5c4b21368d33a81a6f5bc630d265 is needed for that.

japplin commented 4 years ago

Any suggestions on how to handle this when using firebase?

sridhar-vadlamani-ck commented 4 years ago

Any suggestions on how to handle this when using firebase?

@xiphirx This is a valid concern. We don't have access to the command line there to modify these policies. I'm still on 0.9.0 because of this.

xiphirx commented 4 years ago

Can you try executing them as part of your test? Similarly to https://github.com/facebook/screenshot-tests-for-android/blob/master/core/src/main/java/com/facebook/testing/screenshot/internal/ScreenshotDirectories.java#L80

I'm also not familiar with firebase's interface. You dont have any adb connection to the device/emulator? I wonder how the plugin even works then...

doodla commented 4 years ago

I'm sure they have an adb connection. But we don't have access to the terminal to run these commands. I'll try executing them as part of the test itself and see if that works.

but we also need to evaluate if we still need all of the reflection hackery...

any progress on this?

xiphirx commented 4 years ago

What I mean is that the plugin portion of this library just calls adb directly, so if it can you should be able to too.

I haven't been able to spend much time at all on this project, so I haven't evaluated all of the reflection hackery yet.

doodla commented 4 years ago

What I mean is that the plugin portion of this library just calls adb directly, so if it can you should be able to too.

We just use the core library. :)