/src/test/java/com/heavylead/presenters/TestNavigator.java

http://heavylead.googlecode.com/ · Java · 428 lines · 293 code · 72 blank · 63 comment · 0 complexity · 42a035e25493b1e7d18e85188af74e60 MD5 · raw file

  1. package com.heavylead.presenters;
  2. import java.util.ArrayList;
  3. import java.util.Dictionary;
  4. import java.util.Hashtable;
  5. import java.util.List;
  6. import junit.framework.TestCase;
  7. import org.jmock.Expectations;
  8. import org.jmock.Mockery;
  9. import com.google.inject.Guice;
  10. import com.google.inject.Injector;
  11. import com.heavylead.IHeavyLeadGame;
  12. import com.heavylead.injection.HeavyLeadTestModule;
  13. import com.heavylead.models.interfaces.IGameSettingsModel;
  14. import com.heavylead.models.interfaces.ISelectCampaignModel;
  15. import com.heavylead.models.interfaces.ITranslator;
  16. import com.heavylead.views.interfaces.IEditSettingsView;
  17. import com.heavylead.views.interfaces.IEditSettingsViewListener;
  18. import com.heavylead.views.interfaces.IMainMenuView;
  19. import com.heavylead.views.interfaces.IMainMenuViewListener;
  20. import com.heavylead.views.interfaces.ISelectCampaignView;
  21. import com.heavylead.views.interfaces.ISelectCampaignViewListener;
  22. import com.heavylead.wrappers.interfaces.IGameStateManager;
  23. /**
  24. * The Class TestGbuiViewLoader.
  25. */
  26. public final class TestNavigator extends TestCase {
  27. private Injector _testInjector;
  28. /** The _mockery. */
  29. private Mockery _mockery;
  30. /** The _mock translator. */
  31. private ITranslator _mockTranslator;
  32. private IGameStateManager _gameStateManager;
  33. /** The _gbui view loader. */
  34. private Navigator _navigator;
  35. private IMainMenuView _mainMenuView;
  36. private ISelectCampaignView _selectCampaignView;
  37. private IGameSettingsModel _gameSettingsModel;
  38. private IEditSettingsView _editSettingsView;
  39. /**
  40. * Instantiates a new test gbui view loader.
  41. */
  42. public TestNavigator() {
  43. }
  44. /**
  45. * @see junit.framework.TestCase#setUp()
  46. */
  47. public void setUp() {
  48. _testInjector = Guice.createInjector(new HeavyLeadTestModule());
  49. _mockery = _testInjector.getInstance(Mockery.class);
  50. _mockTranslator = _testInjector.getInstance(ITranslator.class);
  51. _gameStateManager = _testInjector.getInstance(IGameStateManager.class);
  52. _mainMenuView = _testInjector.getInstance(IMainMenuView.class);
  53. _selectCampaignView = _testInjector.getInstance(ISelectCampaignView.class);
  54. _gameSettingsModel = _testInjector.getInstance(IGameSettingsModel.class);
  55. _editSettingsView = _testInjector.getInstance(IEditSettingsView.class);
  56. _navigator = new Navigator();
  57. _navigator.setInjector(_testInjector);
  58. _testInjector.injectMembers(_navigator);
  59. }
  60. /**
  61. * @see junit.framework.TestCase#tearDown()
  62. */
  63. public void tearDown() {
  64. _mockery.assertIsSatisfied();
  65. }
  66. /**
  67. * Test display main menu.
  68. */
  69. public void testDisplayMainMenu() {
  70. displayMainMenu();
  71. }
  72. /**
  73. * Display main menu.
  74. */
  75. private void displayMainMenu() {
  76. final int displayWidth = 10;
  77. final int displayHeight = 10;
  78. _mockery.checking(new Expectations() {
  79. {
  80. oneOf(_mainMenuView).setViewListener(
  81. with(any(IMainMenuViewListener.class)));
  82. oneOf(_mainMenuView).bindKeys();
  83. oneOf(_mainMenuView).getTranslationTags();
  84. oneOf(_mockTranslator).getTranslatedPhrases(
  85. new ArrayList<String>());
  86. will(returnValue(new Hashtable<String, String>()));
  87. oneOf(_mainMenuView).setTranslationPhrases(
  88. new Hashtable<String, String>());
  89. oneOf(_mainMenuView).activate();
  90. oneOf(_gameStateManager).attachChild(_mainMenuView);
  91. oneOf(_gameSettingsModel).getDisplayWidth();
  92. will(returnValue(displayWidth));
  93. oneOf(_gameSettingsModel).getDisplayHeight();
  94. will(returnValue(displayHeight));
  95. oneOf(_mainMenuView).layout(displayWidth, displayHeight);
  96. }
  97. });
  98. _navigator.displayMainMenu();
  99. }
  100. /**
  101. * Test hide main menu.
  102. */
  103. public void testHideMainMenu() {
  104. displayAndHideMainMenu();
  105. }
  106. /**
  107. * Display and hide main menu.
  108. */
  109. private void displayAndHideMainMenu() {
  110. displayMainMenu();
  111. hideMainMenu();
  112. }
  113. /**
  114. * What happens if we call hideMainMenu twice?
  115. */
  116. public void testDoubleHideMainMenu() {
  117. displayAndHideMainMenu();
  118. hideMainMenu();
  119. }
  120. /**
  121. * Hide main menu.
  122. */
  123. private void hideMainMenu() {
  124. _mockery.checking(new Expectations() {
  125. {
  126. oneOf(_gameStateManager).hasChild(_mainMenuView);
  127. will(returnValue(true));
  128. oneOf(_mainMenuView).deactivate();
  129. oneOf(_mainMenuView).unbindKeys();
  130. oneOf(_gameStateManager).detachChild(_mainMenuView);
  131. }
  132. });
  133. _navigator.hideMainMenu();
  134. }
  135. /**
  136. * Test display select campaign.
  137. */
  138. public void testDisplaySelectCampaign() {
  139. displaySelectCampaign();
  140. }
  141. /**
  142. * Display select campaign.
  143. */
  144. private void displaySelectCampaign() {
  145. final List<String> campaignNames = new ArrayList<String>();
  146. final ISelectCampaignModel selectCampaignModel =
  147. _testInjector.getInstance(ISelectCampaignModel.class);
  148. final int displayWidth = 10;
  149. final int displayHeight = 10;
  150. _mockery.checking(new Expectations() {
  151. {
  152. oneOf(_selectCampaignView).setViewListener(
  153. with(any(ISelectCampaignViewListener.class)));
  154. oneOf(_selectCampaignView).bindKeys();
  155. oneOf(_selectCampaignView).getTranslationTags();
  156. oneOf(_mockTranslator).getTranslatedPhrases(new ArrayList<String>());
  157. will(returnValue(new Hashtable<String, String>()));
  158. oneOf(_selectCampaignView).setTranslationPhrases(
  159. new Hashtable<String, String>());
  160. oneOf(_selectCampaignView).activate();
  161. oneOf(_gameStateManager).attachChild(_selectCampaignView);
  162. oneOf(_selectCampaignView).enableContinueCampaign(false);
  163. oneOf(_selectCampaignView).enableStartNewCampaign(false);
  164. oneOf(selectCampaignModel).getCampaignNames();
  165. will(returnValue(campaignNames));
  166. oneOf(_selectCampaignView).setCampaignNames(campaignNames);
  167. oneOf(_gameSettingsModel).getDisplayWidth();
  168. will(returnValue(displayWidth));
  169. oneOf(_gameSettingsModel).getDisplayHeight();
  170. will(returnValue(displayHeight));
  171. oneOf(_selectCampaignView).layout(displayWidth, displayHeight);
  172. }
  173. });
  174. _navigator.displaySelectCampaign();
  175. }
  176. /**
  177. * Test hide select campaign.
  178. */
  179. public void testHideSelectCampaign() {
  180. displaySelectCampaign();
  181. _mockery.checking(new Expectations() {
  182. {
  183. oneOf(_gameStateManager).hasChild(_selectCampaignView);
  184. will(returnValue(true));
  185. oneOf(_selectCampaignView).deactivate();
  186. oneOf(_selectCampaignView).unbindKeys();
  187. oneOf(_gameStateManager).detachChild(_selectCampaignView);
  188. }
  189. });
  190. _navigator.hideSelectCampaign();
  191. }
  192. /**
  193. * Test show edit settings.
  194. */
  195. public void testDisplayEditSettings() {
  196. final int displayWidth = 10;
  197. final int displayHeight = 10;
  198. final List<String> translationTags = new ArrayList<String>();
  199. mockDefaultValues();
  200. _mockery.checking(new Expectations() {
  201. {
  202. oneOf(_editSettingsView).setViewListener(
  203. with(any(IEditSettingsViewListener.class)));
  204. oneOf(_editSettingsView).bindKeys();
  205. oneOf(_gameSettingsModel).getDisplayWidth();
  206. will(returnValue(displayWidth));
  207. oneOf(_gameSettingsModel).getDisplayHeight();
  208. will(returnValue(displayHeight));
  209. oneOf(_editSettingsView).layout(displayWidth, displayHeight);
  210. oneOf(_editSettingsView).getTranslationTags();
  211. will(returnValue(translationTags));
  212. oneOf(_mockTranslator).getTranslatedPhrases(translationTags);
  213. oneOf(_editSettingsView).setTranslationPhrases(null);
  214. oneOf(_editSettingsView).activate();
  215. oneOf(_gameStateManager).attachChild(_editSettingsView);
  216. }
  217. });
  218. _navigator.displayEditSettings();
  219. }
  220. /**
  221. * Mock default values.
  222. */
  223. private void mockDefaultValues() {
  224. final List<String> resolutionOptions = new ArrayList<String>();
  225. final List<String> depthOptions = new ArrayList<String>();
  226. final List<String> frequencyOptions = new ArrayList<String>();
  227. final Dictionary<String, String> languageOptions = new Hashtable<String, String>();
  228. final String resolution = "";
  229. final String colorBits = "";
  230. final String frequency = "";
  231. final boolean isFullScreen = false;
  232. final String displayLanguage = "";
  233. _mockery.checking(new Expectations() {
  234. {
  235. oneOf(_gameSettingsModel).getResolutionOptions();
  236. will(returnValue(resolutionOptions));
  237. oneOf(_editSettingsView).setResolutionOptions(resolutionOptions);
  238. oneOf(_gameSettingsModel).getDepthOptions();
  239. will(returnValue(depthOptions));
  240. oneOf(_editSettingsView).setDepthOptions(depthOptions);
  241. oneOf(_gameSettingsModel).getFrequencyOptions();
  242. will(returnValue(frequencyOptions));
  243. oneOf(_editSettingsView).setFrequencyOptions(frequencyOptions);
  244. oneOf(_gameSettingsModel).getLanguageOptions();
  245. will(returnValue(languageOptions));
  246. oneOf(_editSettingsView).setLanguageOptions(languageOptions);
  247. oneOf(_gameSettingsModel).getResolution();
  248. will(returnValue(resolution));
  249. oneOf(_editSettingsView).setResolution(resolution);
  250. oneOf(_gameSettingsModel).getDepth();
  251. will(returnValue(colorBits));
  252. oneOf(_editSettingsView).setDepth(colorBits);
  253. oneOf(_gameSettingsModel).getFrequency();
  254. will(returnValue(frequency));
  255. oneOf(_editSettingsView).setFrequency(frequency);
  256. oneOf(_gameSettingsModel).getIsFullScreen();
  257. will(returnValue(isFullScreen));
  258. oneOf(_editSettingsView).setIsFullScreen(isFullScreen);
  259. oneOf(_gameSettingsModel).getDisplayLanguage();
  260. will(returnValue(displayLanguage));
  261. oneOf(_editSettingsView).setDisplayLanguage(displayLanguage);
  262. oneOf(_editSettingsView).enableApply(false);
  263. oneOf(_editSettingsView).enableOk(false);
  264. }
  265. });
  266. }
  267. /**
  268. * Test hide edit settings.
  269. */
  270. public void testHideEditSettings() {
  271. mockDefaultValues();
  272. _mockery.checking(new Expectations() {
  273. {
  274. oneOf(_editSettingsView).setViewListener(
  275. with(any(IEditSettingsViewListener.class)));
  276. oneOf(_gameStateManager).hasChild(_editSettingsView);
  277. will(returnValue(true));
  278. oneOf(_editSettingsView).deactivate();
  279. oneOf(_editSettingsView).unbindKeys();
  280. oneOf(_gameStateManager).detachChild(_editSettingsView);
  281. }
  282. });
  283. _navigator.hideEditSettings();
  284. }
  285. /**
  286. * Test exit.
  287. */
  288. public void testExit() {
  289. final IHeavyLeadGame heavyLeadGame = _testInjector.getInstance(IHeavyLeadGame.class);
  290. _mockery.checking(new Expectations() {
  291. {
  292. oneOf(heavyLeadGame).stop();
  293. }
  294. });
  295. _navigator.exit();
  296. }
  297. /**
  298. * Test display select campaign scenario.
  299. */
  300. public void testDisplaySelectCampaignScenario() {
  301. _navigator.displaySelectCampaignScenario();
  302. }
  303. /**
  304. * Test display connect to server.
  305. */
  306. public void testDisplayConnectToServer() {
  307. _navigator.displayConnectToServer();
  308. }
  309. /**
  310. * Test reinit display.
  311. */
  312. public void testReinitDisplay() {
  313. final int displayWidth = 10;
  314. final int displayHeight = 10;
  315. final IMainMenuView mainMenuView = _testInjector.getInstance(IMainMenuView.class);
  316. final IGameSettingsModel gameSettings =
  317. _testInjector.getInstance(IGameSettingsModel.class);
  318. final ITranslator translator = _testInjector.getInstance(ITranslator.class);
  319. final List<String> translationTags = new ArrayList<String>();
  320. _mockery.checking(new Expectations() {
  321. {
  322. oneOf(mainMenuView).setViewListener(
  323. with(any(IMainMenuViewListener.class)));
  324. oneOf(mainMenuView).bindKeys();
  325. oneOf(gameSettings).getDisplayWidth();
  326. will(returnValue(displayWidth));
  327. oneOf(gameSettings).getDisplayHeight();
  328. will(returnValue(displayHeight));
  329. oneOf(mainMenuView).layout(displayWidth, displayHeight);
  330. oneOf(mainMenuView).getTranslationTags();
  331. will(returnValue(translationTags));
  332. oneOf(translator).getTranslatedPhrases(translationTags);
  333. oneOf(mainMenuView).setTranslationPhrases(null);
  334. oneOf(mainMenuView).activate();
  335. oneOf(_gameStateManager).attachChild(mainMenuView);
  336. }
  337. });
  338. _navigator.displayMainMenu();
  339. final IHeavyLeadGame heavyLeadGame = _testInjector.getInstance(IHeavyLeadGame.class);
  340. _mockery.checking(new Expectations() {
  341. {
  342. oneOf(heavyLeadGame).reinitVideo();
  343. oneOf(gameSettings).getDisplayWidth();
  344. will(returnValue(displayWidth));
  345. oneOf(gameSettings).getDisplayHeight();
  346. will(returnValue(displayHeight));
  347. oneOf(mainMenuView).layout(displayWidth, displayHeight);
  348. oneOf(mainMenuView).getTranslationTags();
  349. will(returnValue(translationTags));
  350. oneOf(translator).getTranslatedPhrases(translationTags);
  351. oneOf(mainMenuView).setTranslationPhrases(null);
  352. }
  353. });
  354. _navigator.reinitDisplay();
  355. }
  356. }