parent
0c04d6405c
commit
445b742455
@ -0,0 +1,56 @@ |
||||
package com.otaliastudios.cameraview; |
||||
|
||||
|
||||
import android.graphics.Canvas; |
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4; |
||||
import androidx.test.filters.SmallTest; |
||||
|
||||
import com.otaliastudios.cameraview.internal.egl.EglBaseSurface; |
||||
import com.otaliastudios.cameraview.internal.egl.EglCore; |
||||
import com.otaliastudios.cameraview.internal.egl.EglViewport; |
||||
import com.otaliastudios.cameraview.overlay.Overlay; |
||||
import com.otaliastudios.cameraview.overlay.OverlayDrawer; |
||||
import com.otaliastudios.cameraview.size.Size; |
||||
|
||||
import org.hamcrest.BaseMatcher; |
||||
import org.hamcrest.Description; |
||||
import org.junit.After; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import static org.junit.Assert.assertThat; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.ArgumentMatchers.eq; |
||||
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.spy; |
||||
import static org.mockito.Mockito.times; |
||||
import static org.mockito.Mockito.verify; |
||||
|
||||
|
||||
@SuppressWarnings("WeakerAccess") |
||||
public abstract class BaseEglTest extends BaseTest { |
||||
|
||||
protected final static int WIDTH = 100; |
||||
protected final static int HEIGHT = 100; |
||||
|
||||
protected EglCore eglCore; |
||||
protected EglBaseSurface eglSurface; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
eglCore = new EglCore(null, EglCore.FLAG_RECORDABLE); |
||||
eglSurface = new EglBaseSurface(eglCore); |
||||
eglSurface.createOffscreenSurface(WIDTH, HEIGHT); |
||||
eglSurface.makeCurrent(); |
||||
} |
||||
|
||||
@After |
||||
public void tearDown() { |
||||
eglSurface.releaseEglSurface(); |
||||
eglSurface = null; |
||||
eglCore.release(); |
||||
eglCore = null; |
||||
} |
||||
} |
@ -0,0 +1,177 @@ |
||||
package com.otaliastudios.cameraview.filter; |
||||
|
||||
|
||||
import android.opengl.GLES20; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.test.ext.junit.runners.AndroidJUnit4; |
||||
import androidx.test.filters.SmallTest; |
||||
|
||||
import com.otaliastudios.cameraview.BaseEglTest; |
||||
import com.otaliastudios.cameraview.BaseTest; |
||||
import com.otaliastudios.cameraview.internal.GlUtils; |
||||
import com.otaliastudios.cameraview.internal.egl.EglViewport; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import java.util.HashSet; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertNotNull; |
||||
import static org.junit.Assert.assertTrue; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.ArgumentMatchers.anyInt; |
||||
import static org.mockito.Mockito.never; |
||||
import static org.mockito.Mockito.spy; |
||||
import static org.mockito.Mockito.times; |
||||
import static org.mockito.Mockito.verify; |
||||
|
||||
|
||||
@RunWith(AndroidJUnit4.class) |
||||
@SmallTest |
||||
public class BaseFilterTest extends BaseEglTest { |
||||
|
||||
public static class TestFilter extends BaseFilter implements TwoParameterFilter { |
||||
|
||||
private float param1; |
||||
private float param2; |
||||
|
||||
@NonNull |
||||
@Override |
||||
public String getFragmentShader() { |
||||
return createDefaultFragmentShader(); |
||||
} |
||||
|
||||
@Override |
||||
public void setParameter1(float value) { |
||||
param1 = value; |
||||
} |
||||
|
||||
@Override |
||||
public void setParameter2(float value) { |
||||
param2 = value; |
||||
} |
||||
|
||||
@Override |
||||
public float getParameter1() { |
||||
return param1; |
||||
} |
||||
|
||||
@Override |
||||
public float getParameter2() { |
||||
return param2; |
||||
} |
||||
} |
||||
|
||||
private TestFilter filter; |
||||
|
||||
@Test |
||||
public void testCreateDefaultFragmentShader() { |
||||
filter = new TestFilter(); |
||||
filter.fragmentTextureCoordinateName = "XXX"; |
||||
String defaultFragmentShader = filter.createDefaultFragmentShader(); |
||||
assertNotNull(defaultFragmentShader); |
||||
assertTrue(defaultFragmentShader.contains(filter.fragmentTextureCoordinateName)); |
||||
} |
||||
|
||||
@Test |
||||
public void testCreateDefaultVertexShader() { |
||||
filter = new TestFilter(); |
||||
filter.vertexModelViewProjectionMatrixName = "AAA"; |
||||
filter.vertexPositionName = "BBB"; |
||||
filter.vertexTextureCoordinateName = "CCC"; |
||||
filter.vertexTransformMatrixName = "DDD"; |
||||
filter.fragmentTextureCoordinateName = "EEE"; |
||||
String defaultVertexShader = filter.createDefaultVertexShader(); |
||||
assertNotNull(defaultVertexShader); |
||||
assertTrue(defaultVertexShader.contains(filter.vertexModelViewProjectionMatrixName)); |
||||
assertTrue(defaultVertexShader.contains(filter.vertexPositionName)); |
||||
assertTrue(defaultVertexShader.contains(filter.vertexTextureCoordinateName)); |
||||
assertTrue(defaultVertexShader.contains(filter.vertexTransformMatrixName)); |
||||
assertTrue(defaultVertexShader.contains(filter.fragmentTextureCoordinateName)); |
||||
} |
||||
|
||||
@Test |
||||
public void testOnProgramCreate() { |
||||
filter = new TestFilter(); |
||||
int handle = GlUtils.createProgram(filter.getVertexShader(), filter.getFragmentShader()); |
||||
filter.onCreate(handle); |
||||
assertTrue(filter.programHandle >= 0); |
||||
filter.onDestroy(); |
||||
assertTrue(filter.programHandle < 0); |
||||
GLES20.glDeleteProgram(handle); |
||||
} |
||||
|
||||
@Test |
||||
public void testDraw_whenInvalid() { |
||||
filter = spy(new TestFilter()); |
||||
float[] matrix = new float[16]; |
||||
filter.draw(matrix); |
||||
verify(filter, never()).onPreDraw(matrix); |
||||
verify(filter, never()).onDraw(); |
||||
verify(filter, never()).onPostDraw(); |
||||
} |
||||
|
||||
@Test |
||||
public void testDraw() { |
||||
// Use an EglViewport which cares about GL setup.
|
||||
filter = spy(new TestFilter()); |
||||
EglViewport viewport = new EglViewport(filter); |
||||
int texture = viewport.createTexture(); |
||||
|
||||
float[] matrix = new float[16]; |
||||
viewport.drawFrame(texture, matrix); |
||||
verify(filter, times(1)).onPreDraw(matrix); |
||||
verify(filter, times(1)).onDraw(); |
||||
verify(filter, times(1)).onPostDraw(); |
||||
|
||||
viewport.release(); |
||||
} |
||||
|
||||
@Test(expected = RuntimeException.class) |
||||
public void testOnCopy_invalid() { |
||||
// Anonymous inner classes do not have a public constructor.
|
||||
Filter filter = new BaseFilter() { |
||||
@NonNull |
||||
@Override |
||||
public String getFragmentShader() { |
||||
return "whatever"; |
||||
} |
||||
}; |
||||
filter.copy(); |
||||
} |
||||
|
||||
@Test |
||||
public void testOnCopy() { |
||||
filter = new TestFilter(); |
||||
BaseFilter other = filter.copy(); |
||||
assertTrue(other instanceof TestFilter); |
||||
} |
||||
|
||||
@Test |
||||
public void testCopy_withSize() { |
||||
filter = new TestFilter(); |
||||
filter.setSize(WIDTH, HEIGHT); |
||||
BaseFilter other = filter.copy(); |
||||
assertEquals(WIDTH, other.size.getWidth()); |
||||
assertEquals(HEIGHT, other.size.getHeight()); |
||||
} |
||||
|
||||
@Test |
||||
public void testCopy_withParameter1() { |
||||
filter = new TestFilter(); |
||||
filter.setParameter1(0.5F); |
||||
TestFilter other = (TestFilter) filter.copy(); |
||||
assertEquals(filter.getParameter1(), other.getParameter1(), 0.001F); |
||||
} |
||||
|
||||
@Test |
||||
public void testCopy_withParameter2() { |
||||
filter = new TestFilter(); |
||||
filter.setParameter2(0.5F); |
||||
TestFilter other = (TestFilter) filter.copy(); |
||||
assertEquals(filter.getParameter2(), other.getParameter2(), 0.001F); |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
package com.otaliastudios.cameraview.filter; |
||||
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4; |
||||
import androidx.test.filters.SmallTest; |
||||
|
||||
import com.otaliastudios.cameraview.BaseTest; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertNotNull; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
|
||||
@RunWith(AndroidJUnit4.class) |
||||
@SmallTest |
||||
public class FiltersTest extends BaseTest { |
||||
|
||||
@Test |
||||
public void testNewInstance() { |
||||
// At least tests that all our default filters have a no-args constructor.
|
||||
Filters[] filtersArray = Filters.values(); |
||||
for (Filters filters : filtersArray) { |
||||
Filter filter = filters.newInstance(); |
||||
assertNotNull(filter); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,35 @@ |
||||
package com.otaliastudios.cameraview.filter; |
||||
|
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.test.ext.junit.runners.AndroidJUnit4; |
||||
import androidx.test.filters.SmallTest; |
||||
|
||||
import com.otaliastudios.cameraview.BaseTest; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
|
||||
@RunWith(AndroidJUnit4.class) |
||||
@SmallTest |
||||
public class NoFilterTest extends BaseTest { |
||||
|
||||
public static class DummyFilter extends BaseFilter { |
||||
@NonNull |
||||
@Override |
||||
public String getFragmentShader() { |
||||
return "whatever"; |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void testGetFragmentShader() { |
||||
NoFilter filter = new NoFilter(); |
||||
String defaultFragmentShader = new DummyFilter().createDefaultFragmentShader(); |
||||
assertEquals(defaultFragmentShader, filter.getFragmentShader()); |
||||
} |
||||
} |
@ -0,0 +1,43 @@ |
||||
package com.otaliastudios.cameraview.filter; |
||||
|
||||
|
||||
import android.content.res.TypedArray; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.test.ext.junit.runners.AndroidJUnit4; |
||||
import androidx.test.filters.SmallTest; |
||||
|
||||
import com.otaliastudios.cameraview.BaseTest; |
||||
import com.otaliastudios.cameraview.R; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import static junit.framework.TestCase.assertNotNull; |
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertTrue; |
||||
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
|
||||
@RunWith(AndroidJUnit4.class) |
||||
@SmallTest |
||||
public class SimpleFilterTest extends BaseTest { |
||||
|
||||
@Test |
||||
public void testGetFragmentShader() { |
||||
String shader = "shader"; |
||||
SimpleFilter filter = new SimpleFilter(shader); |
||||
assertEquals(shader, filter.getFragmentShader()); |
||||
} |
||||
|
||||
@Test |
||||
public void testCopy() { |
||||
String shader = "shader"; |
||||
SimpleFilter filter = new SimpleFilter(shader); |
||||
BaseFilter copy = filter.copy(); |
||||
assertTrue(copy instanceof SimpleFilter); |
||||
SimpleFilter simpleCopy = (SimpleFilter) copy; |
||||
assertEquals(shader, simpleCopy.getFragmentShader()); |
||||
} |
||||
} |
@ -0,0 +1,40 @@ |
||||
package com.otaliastudios.cameraview.filter; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
|
||||
/** |
||||
* The simplest possible filter that accepts a fragment shader in its constructor. |
||||
* This can be used when your fragment shader is static and has no 'runtime' parameters |
||||
* that influence its behavior. |
||||
* |
||||
* The given fragment shader should respect the default variable names, as listed |
||||
* in the {@link BaseFilter} class. |
||||
* |
||||
* NOTE: SimpleFilter is not meant to be subclassed! |
||||
* Subclassing it would require you to override {@link #onCopy()}, which would make |
||||
* this class useless. Instead, you can extend {@link BaseFilter} directly. |
||||
*/ |
||||
public final class SimpleFilter extends BaseFilter { |
||||
|
||||
private final String fragmentShader; |
||||
|
||||
/** |
||||
* Creates a new filter with the given fragment shader. |
||||
* @param fragmentShader a fragment shader |
||||
*/ |
||||
@SuppressWarnings("WeakerAccess") |
||||
public SimpleFilter(@NonNull String fragmentShader) { |
||||
this.fragmentShader = fragmentShader; |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public String getFragmentShader() { |
||||
return fragmentShader; |
||||
} |
||||
|
||||
@Override |
||||
protected BaseFilter onCopy() { |
||||
return new SimpleFilter(fragmentShader); |
||||
} |
||||
} |
Loading…
Reference in new issue