Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/android-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
:app:testDebugUnitTest
:app-compose:testDebugUnitTest
:camera-view:jacocoCoreCoverageVerification
:camera-view:jacocoLibraryCoverageVerification
:camera-view:assembleRelease
:app:assembleDebug
:app-compose:assembleDebug
Expand Down
6 changes: 6 additions & 0 deletions camera-view/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!--
Declared so the tests can be granted it. The library's own manifest asks for nothing - what
a camera app needs is the app's decision - but a test that checks a recording carries where
it was taken has to be able to hold the permission that supplies it.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application>
<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Size;

import androidx.test.core.app.ActivityScenario;
Expand Down Expand Up @@ -71,9 +72,7 @@ public BackendMatrixDeviceTest(CameraView.Backend backend) {

@Before
public void chooseTheBackend() {
Assume.assumeTrue("no camera on this device",
ApplicationProvider.getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY));
DeviceState.assumeATestableCamera();
CameraView.setDefaultBackend(backend);
}

Expand Down Expand Up @@ -172,10 +171,19 @@ public void aRecordingProducesAFile() throws Exception {
*/
@Test
public void framesReachAnAnalyzer() throws Exception {
// Reading YUV_420_888 images from the emulated camera under SwiftShader takes the API 28
// emulator process down with it, which fails this test and then aborts every test after
// it. Skipped there rather than left to crash: it passes on hardware, so there is nothing
// here for the library to fix, and a run that stops halfway tells nobody anything.
Assume.assumeFalse("the API 28 emulator image crashes reading YUV images",
DeviceState.isEmulator() && Build.VERSION.SDK_INT < 29);

CountDownLatch analyzed = new CountDownLatch(2);
AtomicReference<Size> size = new AtomicReference<>();

withOpenCamera(camera -> {
Assume.assumeTrue(backend + " cannot analyze frames at all",
camera.isFrameAnalysisSupported());
camera.startFrameAnalysis(frame -> {
size.set(new Size(frame.getWidth(), frame.getHeight()));
analyzed.countDown();
Expand Down Expand Up @@ -283,7 +291,12 @@ private void withOpenCamera(CameraAction action) throws Exception {
}
});

Assume.assumeTrue("the " + backend + " backend did not open on this device",
// Asserted, not assumed. Skipping here is how six CameraX tests quietly passed
// on a device where the backend never opened at all: the only thing that stops a
// camera opening and is not the library's fault is the screen, and DeviceState has
// already ruled that out.
assertTrue("the " + backend + " backend did not open on this device "
+ DeviceState.whyTheCameraMightNotOpen(),
opened.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
CameraView camera = held.get();
assertNotNull(camera);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ public final class CameraDeviceCaptureTest {

@Before
public void requireACamera() {
Assume.assumeTrue("no camera on this device",
ApplicationProvider.getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY));
DeviceState.assumeATestableCamera();
}

/** A capture produces a file with bytes in it, which is the whole point of the library. */
Expand Down Expand Up @@ -347,7 +345,8 @@ private void withOpenCamera(CameraAction action) throws Exception {
}
});

assertTrue("the camera never opened", opened.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
assertTrue("the camera never opened " + DeviceState.whyTheCameraMightNotOpen(),
opened.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
CameraView camera = held.get();
assertNotNull(camera);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
package com.xlythe.view.camera.test;

import android.app.Activity;
import android.app.KeyguardManager;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.xlythe.view.camera.CameraView;
import com.xlythe.view.camera.test.R;

/**
* Hosts a CameraView for the device tests, on a screen that is actually on.
*
* <p>That last part is not incidental. A camera needs somewhere to put its preview, and a window
* on a sleeping or locked device never becomes visible - so the activity stops short of STARTED,
* and CameraX, which binds its use cases to the host's lifecycle, correctly declines to open.
* Camera2 has no such gate and opens anyway, which is what makes the failure so confusing: the
* same suite on the same device reports one backend working and another broken, and the real
* answer is that nobody woke the phone up.</p>
*/
public final class CameraTestActivity extends Activity {
private CameraView cameraView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showOnTopOfEverything();
setContentView(R.layout.activity_camera_test);
cameraView = findViewById(R.id.camera);
}

/**
* Turns the screen on, gets past the keyguard, and keeps it that way.
*
* <p>Asked for in the activity rather than arranged by adb beforehand, so that a plain {@code
* connectedAndroidTest} is as reliable as a carefully prepared one.</p>
*/
private void showOnTopOfEverything() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (Build.VERSION.SDK_INT >= 27) {
setShowWhenLocked(true);
setTurnScreenOn(true);
KeyguardManager keyguard = getSystemService(KeyguardManager.class);
if (keyguard != null) {
// Only dismisses a swipe-up keyguard. A device with a real PIN stays locked, and
// the tests say so rather than timing out one by one.
keyguard.requestDismissKeyguard(this, null);
}
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
}

@Override
protected void onStop() {
cameraView.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.xlythe.view.camera.test;

import android.app.KeyguardManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.PowerManager;

import androidx.test.core.app.ApplicationProvider;

import org.junit.Assume;

/**
* What the device has to be for a camera test to mean anything.
*
* <p>A camera needs a visible window to put its preview in, so the state of the screen decides
* whether these tests are testing the library or the lock screen. {@link CameraTestActivity}
* turns the screen on and asks the keyguard to stand aside, which covers everything except a
* device with a real PIN - and that one is worth saying out loud, because the alternative is six
* tests timing out thirty seconds at a time and blaming the backend.</p>
*/
final class DeviceState {
private DeviceState() {}

/** Skips unless there is a camera to test and a screen to show it on. */
static void assumeATestableCamera() {
Context context = ApplicationProvider.getApplicationContext();
Assume.assumeTrue("no camera on this device",
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY));

KeyguardManager keyguard = context.getSystemService(KeyguardManager.class);
Assume.assumeFalse("this device is locked with a PIN, pattern or password, so no window"
+ " can become visible and no camera can open - unlock it and run again",
keyguard != null && keyguard.isDeviceSecure() && keyguard.isKeyguardLocked());
}

/**
* Whether this is an emulator rather than a phone.
*
* <p>Only worth asking where an emulator is known to behave differently from every device,
* and the difference is the emulator's rather than the library's.</p>
*/
static boolean isEmulator() {
return Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.FINGERPRINT.contains("emulator")
|| Build.HARDWARE.equals("goldfish")
|| Build.HARDWARE.equals("ranchu")
|| Build.PRODUCT.contains("sdk");
}

/** What to tell someone whose camera never opened, which is usually about the screen. */
static String whyTheCameraMightNotOpen() {
Context context = ApplicationProvider.getApplicationContext();
PowerManager power = context.getSystemService(PowerManager.class);
KeyguardManager keyguard = context.getSystemService(KeyguardManager.class);
return "(screen interactive: " + (power != null && power.isInteractive())
+ ", keyguard locked: " + (keyguard != null && keyguard.isKeyguardLocked()) + ")";
}
}
Loading
Loading