PageRenderTime 5413ms CodeModel.GetById 36ms RepoModel.GetById 6ms app.codeStats 0ms

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

http://heavylead.googlecode.com/
Java | 262 lines | 178 code | 36 blank | 48 comment | 0 complexity | 2c77c833c9e135586881eef298678714 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /**
  2. *
  3. */
  4. package com.heavylead.presenters;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Dictionary;
  8. import java.util.Hashtable;
  9. import java.util.List;
  10. import junit.framework.TestCase;
  11. import org.jmock.Expectations;
  12. import org.jmock.Mockery;
  13. import com.google.inject.Guice;
  14. import com.google.inject.Injector;
  15. import com.heavylead.injection.HeavyLeadTestModule;
  16. import com.heavylead.models.interfaces.IGameSettingsModel;
  17. import com.heavylead.views.interfaces.IEditSettingsView;
  18. import com.heavylead.views.interfaces.IEditSettingsViewListener;
  19. import com.heavylead.views.interfaces.INavigator;
  20. /**
  21. * @author msimpson
  22. *
  23. */
  24. public final class TestEditSettingsPresenter extends TestCase {
  25. private static final int TEST_WIDTH = 10;
  26. private static final int TEST_HEIGHT = 10;
  27. private static final int TEST_DEPTH = 16;
  28. private static final int TEST_FREQUENCY = 60;
  29. private static final boolean TEST_IS_FULLSCREEN = false;
  30. private static final String TEST_DISPLAYLANGUAGE = "en";
  31. /** The _test injector. */
  32. private Injector _testInjector;
  33. /** The _mockery. */
  34. private Mockery _mockery;
  35. /** The _mock view. */
  36. private IEditSettingsView _mockView;
  37. private IGameSettingsModel _gameSettingsModel;
  38. /** The _mock view loader. */
  39. private INavigator _mockNavigator;
  40. /** The _main menu presenter. */
  41. private EditSettingsPresenter _presenter;
  42. /**
  43. * Instantiates a new test edit settings presenter.
  44. */
  45. public TestEditSettingsPresenter() {
  46. }
  47. /**
  48. * @see junit.framework.TestCase#setUp()
  49. */
  50. @Override
  51. public void setUp() {
  52. _testInjector = Guice.createInjector(new HeavyLeadTestModule());
  53. _mockery = _testInjector.getInstance(Mockery.class);
  54. _mockView = _testInjector.getInstance(IEditSettingsView.class);
  55. _gameSettingsModel = _testInjector.getInstance(IGameSettingsModel.class);
  56. _mockNavigator = _testInjector.getInstance(INavigator.class);
  57. final List<String> resolutions = new ArrayList<String>();
  58. final List<String> depthOptions = new ArrayList<String>();
  59. final List<String> frequencyOptions = new ArrayList<String>();
  60. final Dictionary<String, String> languageOptions = new Hashtable<String, String>();
  61. final String resolution = "";
  62. final String depth = "";
  63. final String frequency = "";
  64. final String displayLanguage = "";
  65. _mockery.checking(new Expectations() {
  66. {
  67. oneOf(_mockView).setViewListener(
  68. with(any(IEditSettingsViewListener.class)));
  69. oneOf(_gameSettingsModel).getResolutionOptions();
  70. will(returnValue(resolutions));
  71. oneOf(_mockView).setResolutionOptions(resolutions);
  72. oneOf(_gameSettingsModel).getDepthOptions();
  73. will(returnValue(depthOptions));
  74. oneOf(_mockView).setDepthOptions(depthOptions);
  75. oneOf(_gameSettingsModel).getFrequencyOptions();
  76. will(returnValue(frequencyOptions));
  77. oneOf(_mockView).setFrequencyOptions(frequencyOptions);
  78. oneOf(_gameSettingsModel).getLanguageOptions();
  79. will(returnValue(languageOptions));
  80. oneOf(_mockView).setLanguageOptions(languageOptions);
  81. oneOf(_gameSettingsModel).getResolution();
  82. will(returnValue(resolution));
  83. oneOf(_mockView).setResolution(resolution);
  84. oneOf(_gameSettingsModel).getDepth();
  85. will(returnValue(depth));
  86. oneOf(_mockView).setDepth(depth);
  87. oneOf(_gameSettingsModel).getFrequency();
  88. will(returnValue(frequency));
  89. oneOf(_mockView).setFrequency(frequency);
  90. oneOf(_gameSettingsModel).getIsFullScreen();
  91. will(returnValue(false));
  92. oneOf(_mockView).setIsFullScreen(false);
  93. oneOf(_gameSettingsModel).getDisplayLanguage();
  94. will(returnValue(displayLanguage));
  95. oneOf(_mockView).setDisplayLanguage(displayLanguage);
  96. oneOf(_mockView).enableApply(false);
  97. oneOf(_mockView).enableOk(false);
  98. }
  99. });
  100. _presenter = new EditSettingsPresenter(_mockView, _gameSettingsModel, _mockNavigator);
  101. }
  102. /**
  103. * @see junit.framework.TestCase#tearDown()
  104. */
  105. @Override
  106. public void tearDown() {
  107. _mockery.assertIsSatisfied();
  108. }
  109. /**
  110. * Test change resolution.
  111. */
  112. public void testSelectionChangedModelIsDirty() {
  113. readSettingsFromViewAndApplyToModel();
  114. _mockery.checking(new Expectations() {
  115. {
  116. oneOf(_gameSettingsModel).isDirty();
  117. will(returnValue(true));
  118. oneOf(_mockView).enableApply(true);
  119. oneOf(_mockView).enableOk(true);
  120. }
  121. });
  122. _presenter.selectionChanged();
  123. }
  124. /**
  125. * Test selection changed model not dirty.
  126. */
  127. public void testSelectionChangedModelNotDirty() {
  128. readSettingsFromViewAndApplyToModel();
  129. _mockery.checking(new Expectations() {
  130. {
  131. oneOf(_gameSettingsModel).isDirty();
  132. will(returnValue(false));
  133. oneOf(_mockView).enableApply(false);
  134. oneOf(_mockView).enableOk(false);
  135. }
  136. });
  137. _presenter.selectionChanged();
  138. }
  139. /**
  140. * Expect new settings.
  141. */
  142. private void readSettingsFromViewAndApplyToModel() {
  143. _mockery.checking(new Expectations() {
  144. {
  145. oneOf(_mockView).getResolutionWidth();
  146. will(returnValue(TEST_WIDTH));
  147. oneOf(_mockView).getResolutionHeight();
  148. will(returnValue(TEST_HEIGHT));
  149. oneOf(_mockView).getDepth();
  150. will(returnValue(TEST_DEPTH));
  151. oneOf(_mockView).getFrequency();
  152. will(returnValue(TEST_FREQUENCY));
  153. oneOf(_gameSettingsModel).setDisplayWidth(TEST_WIDTH);
  154. oneOf(_gameSettingsModel).setDisplayHeight(TEST_HEIGHT);
  155. oneOf(_gameSettingsModel).setDepth(TEST_DEPTH);
  156. oneOf(_gameSettingsModel).setFrequency(TEST_FREQUENCY);
  157. oneOf(_mockView).isFullScreen();
  158. will(returnValue(TEST_IS_FULLSCREEN));
  159. oneOf(_gameSettingsModel).setIsFullScreen(TEST_IS_FULLSCREEN);
  160. oneOf(_mockView).getDisplayLanguage();
  161. will(returnValue(TEST_DISPLAYLANGUAGE));
  162. oneOf(_gameSettingsModel).setDisplayLanguage(TEST_DISPLAYLANGUAGE);
  163. }
  164. });
  165. }
  166. /**
  167. * Test save settings.
  168. *
  169. * @throws IOException Signals that an I/O exception has occurred.
  170. */
  171. public void testSaveSettings() throws IOException {
  172. _mockery.checking(new Expectations() {
  173. {
  174. oneOf(_gameSettingsModel).save();
  175. oneOf(_mockNavigator).reinitDisplay();
  176. oneOf(_mockNavigator).hideEditSettings();
  177. oneOf(_mockNavigator).displayMainMenu();
  178. }
  179. });
  180. _presenter.saveSettingsPressed();
  181. }
  182. /**
  183. * Test save settings with exception.
  184. *
  185. * @throws IOException Signals that an I/O exception has occurred.
  186. */
  187. public void testSaveSettingsWithException() throws IOException {
  188. _mockery.checking(new Expectations() {
  189. {
  190. oneOf(_gameSettingsModel).save();
  191. will(throwException(new IOException()));
  192. oneOf(_mockView).showErrorDialog("Error", "error saving settings");
  193. oneOf(_mockNavigator).reinitDisplay();
  194. oneOf(_mockNavigator).hideEditSettings();
  195. oneOf(_mockNavigator).displayMainMenu();
  196. }
  197. });
  198. _presenter.saveSettingsPressed();
  199. }
  200. /**
  201. * Test apply.
  202. *
  203. * @throws IOException Signals that an I/O exception has occurred.
  204. */
  205. public void testApply() throws IOException {
  206. _mockery.checking(new Expectations() {
  207. {
  208. oneOf(_gameSettingsModel).save();
  209. oneOf(_mockNavigator).reinitDisplay();
  210. }
  211. });
  212. _presenter.applyPressed();
  213. }
  214. /**
  215. * Test cancel.
  216. */
  217. public void testCancel() {
  218. _mockery.checking(new Expectations() {
  219. {
  220. oneOf(_mockNavigator).hideEditSettings();
  221. oneOf(_mockNavigator).displayMainMenu();
  222. }
  223. });
  224. _presenter.cancelPressed();
  225. }
  226. }