Avoid ConcurrentModificationExceptions in listeners (#88)

pull/92/head
Mattia Iavarone 7 years ago committed by GitHub
parent 85dc1a05d4
commit 011754203c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java
  2. 5
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

@ -382,6 +382,7 @@ public class CameraViewTest extends BaseTest {
//region testLocation //region testLocation
@SuppressWarnings("ConstantConditions")
@Test @Test
public void testSetLocation() { public void testSetLocation() {
cameraView.setLocation(50d, -50d); cameraView.setLocation(50d, -50d);
@ -554,6 +555,7 @@ public class CameraViewTest extends BaseTest {
//region Lists of listeners and processors //region Lists of listeners and processors
@SuppressWarnings("UseBulkOperation")
@Test @Test
public void testCameraListenerList() { public void testCameraListenerList() {
assertTrue(cameraView.mListeners.isEmpty()); assertTrue(cameraView.mListeners.isEmpty());
@ -571,8 +573,17 @@ public class CameraViewTest extends BaseTest {
cameraView.clearCameraListeners(); cameraView.clearCameraListeners();
assertTrue(cameraView.mListeners.isEmpty()); assertTrue(cameraView.mListeners.isEmpty());
// Ensure this does not throw a ConcurrentModificationException
cameraView.addCameraListener(new CameraListener() {});
cameraView.addCameraListener(new CameraListener() {});
cameraView.addCameraListener(new CameraListener() {});
for (CameraListener test : cameraView.mListeners) {
cameraView.mListeners.remove(test);
}
} }
@SuppressWarnings({"NullableProblems", "UseBulkOperation"})
@Test @Test
public void testFrameProcessorsList() { public void testFrameProcessorsList() {
assertTrue(cameraView.mFrameProcessors.isEmpty()); assertTrue(cameraView.mFrameProcessors.isEmpty());
@ -592,6 +603,14 @@ public class CameraViewTest extends BaseTest {
cameraView.clearFrameProcessors(); cameraView.clearFrameProcessors();
assertTrue(cameraView.mFrameProcessors.isEmpty()); assertTrue(cameraView.mFrameProcessors.isEmpty());
// Ensure this does not throw a ConcurrentModificationException
cameraView.addFrameProcessor(new FrameProcessor() { public void process(Frame f) {} });
cameraView.addFrameProcessor(new FrameProcessor() { public void process(Frame f) {} });
cameraView.addFrameProcessor(new FrameProcessor() { public void process(Frame f) {} });
for (FrameProcessor test : cameraView.mFrameProcessors) {
cameraView.mFrameProcessors.remove(test);
}
} }
//endregion //endregion

@ -29,6 +29,7 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import static android.view.View.MeasureSpec.AT_MOST; import static android.view.View.MeasureSpec.AT_MOST;
import static android.view.View.MeasureSpec.EXACTLY; import static android.view.View.MeasureSpec.EXACTLY;
@ -62,8 +63,8 @@ public class CameraView extends FrameLayout {
private OrientationHelper mOrientationHelper; private OrientationHelper mOrientationHelper;
private CameraController mCameraController; private CameraController mCameraController;
private MediaActionSound mSound; private MediaActionSound mSound;
/* for tests */ ArrayList<CameraListener> mListeners = new ArrayList<>(2); /* for tests */ List<CameraListener> mListeners = new CopyOnWriteArrayList<>();
/* for tests */ ArrayList<FrameProcessor> mFrameProcessors = new ArrayList<>(1); /* for tests */ List<FrameProcessor> mFrameProcessors = new CopyOnWriteArrayList<>();
// Views // Views
GridLinesLayout mGridLinesLayout; GridLinesLayout mGridLinesLayout;

Loading…
Cancel
Save