Coverage Summary for Class: GdxTestRunner (io.team9.game.tests)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| GdxTestRunner | 100% (1/1) | 55.6% (5/9) | 78.6% (22/28) |
1 /******************************************************************************* 2 * Copyright 2015 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package io.team9.game.tests; 18 19 import java.util.HashMap; 20 import java.util.Map; 21 22 import org.junit.runner.notification.RunNotifier; 23 import org.junit.runners.BlockJUnit4ClassRunner; 24 import org.junit.runners.model.FrameworkMethod; 25 import org.junit.runners.model.InitializationError; 26 27 import com.badlogic.gdx.Gdx; 28 import com.badlogic.gdx.graphics.GL20; 29 import static org.mockito.Mockito.mock; 30 31 import com.badlogic.gdx.ApplicationListener; 32 import com.badlogic.gdx.backends.headless.HeadlessApplication; 33 import com.badlogic.gdx.backends.headless.HeadlessApplicationConfiguration; 34 35 public class GdxTestRunner extends BlockJUnit4ClassRunner implements ApplicationListener { 36 37 private Map<FrameworkMethod, RunNotifier> invokeInRender = new HashMap<FrameworkMethod, RunNotifier>(); 38 39 public GdxTestRunner(Class<?> klass) throws InitializationError { 40 super(klass); 41 HeadlessApplicationConfiguration conf = new HeadlessApplicationConfiguration(); 42 43 new HeadlessApplication(this, conf); 44 Gdx.gl = mock(GL20.class); 45 } 46 47 @Override 48 public void create() { 49 } 50 51 @Override 52 public void resume() { 53 } 54 55 @Override 56 public void render() { 57 synchronized (invokeInRender) { 58 for (Map.Entry<FrameworkMethod, RunNotifier> each : invokeInRender.entrySet()) { 59 super.runChild(each.getKey(), each.getValue()); 60 } 61 invokeInRender.clear(); 62 } 63 } 64 65 @Override 66 public void resize(int width, int height) { 67 } 68 69 @Override 70 public void pause() { 71 } 72 73 @Override 74 public void dispose() { 75 } 76 77 @Override 78 protected void runChild(FrameworkMethod method, RunNotifier notifier) { 79 synchronized (invokeInRender) { 80 // add for invoking in render phase, where gl context is available 81 invokeInRender.put(method, notifier); 82 } 83 // wait until that test was invoked 84 waitUntilInvokedInRenderMethod(); 85 } 86 87 private void waitUntilInvokedInRenderMethod() { 88 try { 89 while (true) { 90 Thread.sleep(10); 91 synchronized (invokeInRender) { 92 if (invokeInRender.isEmpty()) 93 break; 94 } 95 } 96 } catch (InterruptedException e) { 97 e.printStackTrace(); 98 } 99 } 100 101 }