Programmatically setting genymotion gps during testing causes “SecurityException: invalid package name” when setting latitude

主宰稳场 提交于 2019-12-23 02:32:18

问题


When testing, I am setting the lat long via Genymotion like so:

 package com.mypackage.name;

 public class TestGps extends ActivityInstrumentationTestCase2<MyClass>{

    ... //setup just calls super.setup();

    public void testLocationButton(){
       Gps gps = GenymotionManager.getGenymotionManager(getInstrumentation().getContext()).getGps();
       gps.setStatus(Gps.Status.ENABLED);
       gps.setLatitude(40.7127); // the error happens here
       gps.setLongitude(74.0059);
   }
}

I get the following issue when run:

java.lang.SecurityException: invalid package name: com.mypackage.name
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1499)
at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:582)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:867)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:490)
at com.genymotion.api.Gps.waitForTargetLocation(Gps.java:271)
at com.genymotion.api.Gps.setLatitude(Gps.java:134)

回答1:


In a test Instrumentation.getContext() returns the context of the tests. To access the context of the app being tested, you need to call Instrumentation.getTargetContext() instead. You should also consider using InstrumentationRegistry. This is now the preferred access point for Instrumentation instances.



来源:https://stackoverflow.com/questions/35190048/programmatically-setting-genymotion-gps-during-testing-causes-securityexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!