package ro.mirceanistor.gltestutils.sample;

import android.opengl.GLES20;
import android.support.test.filters.Suppress;
import android.support.test.rule.ActivityTestRule;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

import ro.mirceanistor.gltestutils.GLTestUtils;
import ro.mirceanistor.gltestutils.sample.MainActivity;

import static junit.framework.TestCase.assertTrue;

/**
 * Created by mirceanis on 2016-05-06.
 * Example of usage of the GLTestUtils class
 */
public class MainActivityTest {

    @Rule
    public ActivityTestRule mActivityRule = new ActivityTestRule<>(MainActivity.class);

    @Before
    public void setUp() throws Exception {

    }

    @Test
    public void testSomethingOnGLThread() throws Exception {
        GLTestUtils.runOnGLThreadAndWait(new Runnable() {
            @Override
            public void run() {
                assertTrue(true);
            }
        });
    }

    @Test
    public void testSomethingAfterGLThread() throws Exception {
        final boolean[] something = {false};

        GLTestUtils.runOnGLThreadAndWait(new Runnable() {
            @Override
            public void run() {
                something[0] = true;
            }
        });

        Assert.assertTrue("something should be true", something[0]);
    }

    /**
     * An example of test running GL code on a GL thread
     * @throws Exception
     */
    @Test
    public void testShaderCreationOnGLThread() throws Exception {
        GLTestUtils.runOnGLThreadAndWait(new Runnable() {
            @Override
            public void run() {
                int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
                Assert.assertTrue("shader handle is non zero", vertexShaderHandle != 0);
            }
        });
    }

    /**
     * GLTestUtils needs to be released
     * @throws Exception
     */
    @After
    public void tearDown() throws Exception {
        //There must be a release, otherwise it will leak
        GLTestUtils.release();
    }

    // this test fails and brings down the whole test suite.
    // unsuppress if you need to see how it fails
    @Suppress
    @Test
    public void testSomethingFailingOnGLThread() throws Exception {
        GLTestUtils.runOnGLThreadAndWait(new Runnable() {
            @Override
            public void run() {
                assertTrue("I failed on the GL thread", false);
            }
        });
        assertTrue(true);
    }

    // this test fails and brings down the whole test suite.
    // unsuppress if you need to see how it fails
    @Suppress
    @Test
    public void testShaderCompilationOnTestThread() throws Exception {
        int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
        assertTrue("shader handle is non zero", vertexShaderHandle != 0);
    }

    // this test fails and brings down the whole test suite.
    // unsuppress if you need to see how it fails
    @Suppress
    @Test
    public void testShaderCompilationOnUIThread() throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);

        mActivityRule.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
                Assert.assertTrue("shader handle is non zero", vertexShaderHandle != 0);
                latch.countDown();
            }
        });

        // Wait for the ui thread task to finish
        latch.await();

    }


}
@@ -164,16 +164,66 @@
 estRule;
+%0Aimport android.support.test.runner.AndroidJUnit4;
 %0A%0Aimport
@@ -335,16 +335,49 @@
 it.Test;
+%0Aimport org.junit.runner.RunWith;
 %0A%0Aimport
@@ -466,64 +466,8 @@
 ils;
-%0Aimport ro.mirceanistor.gltestutils.sample.MainActivity;
 %0A%0Aim
@@ -604,20 +604,50 @@
 s class%0A
-
  */%0A
+@RunWith(AndroidJUnit4.class)%0A
 public c
@@ -1944,32 +1944,611 @@
      %7D);%0A    %7D%0A%0A
+    /**%0A     * An example of test that uses width and height in a useless manner%0A     * @throws Exception%0A     */%0A    @Test%0A    public void testHasNonZeroDimensions() throws Exception %7B%0A        GLTestUtils.runOnGLThreadAndWait(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                Assert.assertTrue(%22width is supposed to be non zero%22, GLTestUtils.getWidth() != 0);%0A            %7D%0A        %7D);%0A%0A        //should also work outside of the GL context%0A        Assert.assertTrue(%22height is supposed to be non zero%22, GLTestUtils.getHeight() != 0);%0A    %7D%0A%0A
     /**%0A     * G

add test with gl surface view dimensions