001 package viewguitest; 002 003 004 005 import java.util.Timer; 006 007 import junit.framework.TestCase; 008 import antichess.GameTimer; 009 010 011 /** 012 * Some tests for the GameTimer, hard to do because it is a timer. A lot of tests are done 013 * in TimerLabelTest. 014 * @author nlharr 015 * 016 * Methods that need testing: 017 * start() thoroughly 018 * stop() thoroughly 019 * getTime() thoroughly 020 * addTimerListener() 021 * notifyTimeUpdate() 022 * run() 023 * setTime 024 */ 025 026 public class GameTimerTest extends TestCase 027 { 028 GameTimer timer[]; 029 030 031 public GameTimerTest(String name) { 032 super(name); 033 timer = new GameTimer[6]; 034 } 035 036 //initializes the GameTimers 037 public void initializeTimers(){ 038 for (int i=0; i<timer.length; i++){ 039 timer[i] = new GameTimer(1000*i, 100*(i+1)); 040 } 041 } 042 043 /** 044 * Tests to see if the constructor is able to create valid GameTimers 045 * without throwing errors. 046 */ 047 public void testConstructor(){ 048 try{ 049 initializeTimers(); 050 }catch (Exception ex){ 051 fail(ex.getMessage()); 052 } 053 } 054 055 /** 056 * Tests that retrieving the initial time works 057 * Tests getTime() without running the GameTimers 058 */ 059 public void testInitialGetTime(){ 060 initializeTimers(); 061 for (int i=0; i<timer.length; i++){ 062 assertEquals("Timer "+Integer.toString(i)+" did not return the proper value", 063 1000*i, timer[i].getTime()); 064 } 065 } 066 067 068 /** 069 * Naive start test to see if the start() 070 * Works by calling start() and waiting after the timers should be run down to check 071 * if the timers have 0 remaining on them. 072 */ 073 public void testStart(){ 074 initializeTimers(); 075 for (int i=0; i<timer.length; i++){ 076 timer[i].start(); 077 } 078 try{ 079 Thread.sleep(5050); 080 }catch (Exception ex){} 081 082 for (int i=0; i<timer.length; i++){ 083 assertEquals("Timer "+Integer.toString(i)+" did not return the proper value", 084 0, timer[i].getTime()); 085 } 086 } 087 088 /** 089 * Naive start test to see if the stop() 090 * Works by calling start() and waiting a very short time, stop it and then waiting 091 * until the timers would run down to zero. 092 */ 093 public void testStop(){ 094 initializeTimers(); 095 for (int i=0; i<timer.length; i++){ 096 timer[i].start(); 097 } 098 try{ 099 Thread.sleep(50); 100 }catch (Exception ex){} 101 for (int i=0; i<timer.length; i++){ 102 timer[i].stop(); 103 } 104 105 for (int i=1; i<timer.length; i++){ 106 assertFalse("Timer "+Integer.toString(i)+" did not return the proper value", 107 timer[i].getTime()==0); 108 } 109 } 110 111 112 /** 113 * Tests isRunning() 114 */ 115 public void testIsRunning(){ 116 initializeTimers(); 117 for (int i=0; i<timer.length; i++){ 118 timer[i].start(); 119 } 120 for (int i=0; i<timer.length; i++){ 121 assertTrue("Timer "+Integer.toString(i)+" did not return the proper value", 122 timer[i].isRunning()); 123 } 124 } 125 126 /** 127 * Tests setTime() and getTime() while not running 128 */ 129 public void testSetGetTime(){ 130 initializeTimers(); 131 for (int i=0; i<timer.length; i++){ 132 timer[i].setTime(i*52); 133 } 134 135 for (int i=0; i<timer.length; i++){ 136 assertEquals("Timer "+Integer.toString(i)+" did not return the proper value", 137 52*i, timer[i].getTime()); 138 } 139 140 } 141 142 }