Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test specific hardware features with Robolectric on android?
    primarykey
    data
    text
    <p>I started writing a robolectric test around hardware specific features, like sensors and camera (front and back).</p> <p>Imagine this class:</p> <pre><code>class CheckHardware { private bolean hasCamera(Context context) { PackageManager pm = context.callingActivityContext .getPackageManager(); // camera support Boolean frontCam = pm.hasSystemFeature("android.hardware.camera.front"); Boolean rearCam = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA); if (frontCam || rearCam) { return true; } return false; } } </code></pre> <p>So i want to test different scenarios where there is a front camera and a rear camera, only a front camera or no camera at all. In my app its a bit more complicated but hope this makes it easier to understand what i mean.</p> <p>For now i did it like this, which feels a bit odd.</p> <pre><code>RobolectricPackageManager pm = (RobolectricPackageManager) Robolectric.application.getPackageManager(); pm.setSystemFeature(PackageManager.FEATURE_CAMERA, true); </code></pre> <p>I thought about writing my own test runner, so for all expected hardware settings a specific runner like this</p> <pre><code>public class WithCameraTestRunner extends RobolectricTestRunner { @Override public void setupApplicationstate(RobolectricConfig robolectricConfig) { super.setupApplicationState(robolectricConfig); ShadowApplication shadowApplication = shadowOf(Robolectric.application); shadowApplication.setPackageName(robolectricConfig.getPackageName()); RobolectricPackageManager pm = new RobolectricPackageManager(Robolectric.application, robolectricConfig) pm.setSystemFeature(PackageManager.FEATURE_CAMERA, true); shadowApplication.setPackageManager(pm); } } </code></pre> <p>Not quite happy with that either as i want to test different scenarios in the same test.</p> <p>Any ideas? Whats the best approach for this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload