From 2df7bcafbdc23bf383fae31b4c85e4964f62bf1e Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sun, 10 Nov 2019 21:39:10 +0200 Subject: [PATCH] Added tests for Travis to fail if the jsons don't parse or something --- .gitignore | 1 + build.gradle | 40 ++++++- settings.gradle | 2 +- tests/build.gradle | 22 ++++ .../de/tomgrill/gdxtesting/GdxTestRunner.java | 104 ++++++++++++++++++ .../examples/AssetExistsExampleTest.java | 30 +++++ .../gdxtesting/examples/UnitTestExample.java | 30 +++++ 7 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 tests/build.gradle create mode 100644 tests/src/de/tomgrill/gdxtesting/GdxTestRunner.java create mode 100644 tests/src/de/tomgrill/gdxtesting/examples/AssetExistsExampleTest.java create mode 100644 tests/src/de/tomgrill/gdxtesting/examples/UnitTestExample.java diff --git a/.gitignore b/.gitignore index 6844baa4ec..f83e397c18 100644 --- a/.gitignore +++ b/.gitignore @@ -135,3 +135,4 @@ android/release/android.aab android/assets/maps/ android/release/android.aab.sig android/release/android-release.aab +tests/build/ diff --git a/build.gradle b/build.gradle index 34b8d99833..9386aeb4c7 100644 --- a/build.gradle +++ b/build.gradle @@ -128,7 +128,45 @@ project(":core") { implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion" } -} + + + // Taken from https://github.com/TomGrill/gdx-testing + project(":tests") { + apply plugin: "java" + + test{ + workingDir= file("../android/assets") + } + + sourceSets.test.java.srcDirs = ["src/"] + + dependencies { + + /** + * If you do have some classes to test in os specific code you may want to uncomment + * some of these lines. + * + * BUT: I recommend to create seperate test sub projects for each platform. Trust me :) + * + */ + + + compile project(":core") + + compile "junit:junit:4.12" + compile "org.mockito:mockito-all:1.9.5" + + compile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion" + compile "com.badlogicgames.gdx:gdx:$gdxVersion" + + testCompile 'junit:junit:4.12' + testCompile "org.mockito:mockito-all:1.9.5" + + testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion" + testCompile "com.badlogicgames.gdx:gdx:$gdxVersion" + testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" + } + }} tasks.eclipse.doLast { delete ".project" diff --git a/settings.gradle b/settings.gradle index 965b6a3a01..7e20081646 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include 'desktop', 'android', 'ios', 'html', 'core' \ No newline at end of file +include 'desktop', 'android', 'ios', 'html', 'core', 'tests' \ No newline at end of file diff --git a/tests/build.gradle b/tests/build.gradle new file mode 100644 index 0000000000..6340b1242f --- /dev/null +++ b/tests/build.gradle @@ -0,0 +1,22 @@ + + +apply plugin: "java" + +sourceCompatibility = 1.6 +[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' + + + +sourceSets { + test { + java { + srcDir 'src' + } + } + + +} + +eclipse.project { + name = appName + "-tests" +} diff --git a/tests/src/de/tomgrill/gdxtesting/GdxTestRunner.java b/tests/src/de/tomgrill/gdxtesting/GdxTestRunner.java new file mode 100644 index 0000000000..22669c85c1 --- /dev/null +++ b/tests/src/de/tomgrill/gdxtesting/GdxTestRunner.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright 2015 See AUTHORS file. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +package de.tomgrill.gdxtesting; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.GL20; +import static org.mockito.Mockito.mock; + +import com.badlogic.gdx.ApplicationListener; +import com.badlogic.gdx.backends.headless.HeadlessApplication; +import com.badlogic.gdx.backends.headless.HeadlessApplicationConfiguration; + +public class GdxTestRunner extends BlockJUnit4ClassRunner implements ApplicationListener { + + private Map invokeInRender = new HashMap(); + + public GdxTestRunner(Class klass) throws InitializationError { + super(klass); + HeadlessApplicationConfiguration conf = new HeadlessApplicationConfiguration(); + + new HeadlessApplication(this, conf); + Gdx.gl = mock(GL20.class); + } + + @Override + public void create() { + } + + @Override + public void resume() { + } + + @Override + public void render() { + synchronized (invokeInRender) { + for (Map.Entry each : invokeInRender.entrySet()) { + super.runChild(each.getKey(), each.getValue()); + } + invokeInRender.clear(); + } + } + + @Override + public void resize(int width, int height) { + } + + @Override + public void pause() { + } + + @Override + public void dispose() { + } + + @Override + protected void runChild(FrameworkMethod method, RunNotifier notifier) { + synchronized (invokeInRender) { + // add for invoking in render phase, where gl context is available + invokeInRender.put(method, notifier); + } + // wait until that test was invoked + waitUntilInvokedInRenderMethod(); + } + + /** + * + */ + private void waitUntilInvokedInRenderMethod() { + try { + while (true) { + Thread.sleep(10); + synchronized (invokeInRender) { + if (invokeInRender.isEmpty()) + break; + } + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + +} diff --git a/tests/src/de/tomgrill/gdxtesting/examples/AssetExistsExampleTest.java b/tests/src/de/tomgrill/gdxtesting/examples/AssetExistsExampleTest.java new file mode 100644 index 0000000000..2413606c43 --- /dev/null +++ b/tests/src/de/tomgrill/gdxtesting/examples/AssetExistsExampleTest.java @@ -0,0 +1,30 @@ +// Taken from https://github.com/TomGrill/gdx-testing + +package de.tomgrill.gdxtesting.examples; + +import com.badlogic.gdx.Gdx; +import com.unciv.models.gamebasics.GameBasics; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import de.tomgrill.gdxtesting.GdxTestRunner; + +import static org.junit.Assert.assertTrue; + +@RunWith(GdxTestRunner.class) +public class AssetExistsExampleTest { + + @Test + public void gamePngExists() { + assertTrue("This test will only pass when the game.png exists", + Gdx.files.local("game.png").exists()); + } + + @Test + public void gameBasicsLoad() { + assertTrue("This test will only pass when the game.png exists", + GameBasics.INSTANCE.getBuildings().size() > 0); + } + +} diff --git a/tests/src/de/tomgrill/gdxtesting/examples/UnitTestExample.java b/tests/src/de/tomgrill/gdxtesting/examples/UnitTestExample.java new file mode 100644 index 0000000000..6e54436e57 --- /dev/null +++ b/tests/src/de/tomgrill/gdxtesting/examples/UnitTestExample.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright 2015 See AUTHORS file. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +package de.tomgrill.gdxtesting.examples; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class UnitTestExample { + + @Test + public void oneEqualsOne() { + assertEquals(1, 1); + } + +}