PageRenderTime 4556ms CodeModel.GetById 33ms RepoModel.GetById 27ms app.codeStats 0ms

/src/test/java/com/heavylead/models/concrete/TestGameSettingsModel.java

http://heavylead.googlecode.com/
Java | 631 lines | 402 code | 100 blank | 129 comment | 0 complexity | 742858183509223eca7ee6ac054a00c7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.heavylead.models.concrete;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Dictionary;
  5. import java.util.Hashtable;
  6. import java.util.List;
  7. import junit.framework.TestCase;
  8. import org.jmock.Expectations;
  9. import org.jmock.Mockery;
  10. import com.google.inject.Guice;
  11. import com.google.inject.Injector;
  12. import com.heavylead.injection.HeavyLeadTestModule;
  13. import com.heavylead.models.interfaces.IGameSettingsModel;
  14. import com.heavylead.models.interfaces.ITranslator;
  15. import com.jme.system.GameSettings;
  16. /**
  17. * The Class TestGameSettingsModel.
  18. */
  19. public final class TestGameSettingsModel extends TestCase {
  20. /** The Constant FR. */
  21. private static final String FR = "fr";
  22. /** The Constant FRENCH. */
  23. private static final String FRENCH = "French";
  24. /** The Constant ENGLISH. */
  25. private static final String ENGLISH = "English";
  26. /** The Constant TEST_WIDTH. */
  27. private static final int TEST_WIDTH = 10;
  28. /** The Constant TEST_HEIGHT. */
  29. private static final int TEST_HEIGHT = 10;
  30. /** The Constant TEST_RESOLUTION. */
  31. private static final String TEST_RESOLUTION = "10x10";
  32. /** The Constant TEST_DEPTH. */
  33. private static final int TEST_DEPTH = 16;
  34. /** The Constant TEST_FREQUENCY. */
  35. private static final int TEST_FREQUENCY = 60;
  36. /** The Constant TEST_ISFULLSCREEN. */
  37. private static final boolean TEST_ISFULLSCREEN = false;
  38. /** The Constant LANGUAGE. */
  39. private static final String LANGUAGE = "Language";
  40. /** The Constant TEST_DISPLAYLANGUAGE. */
  41. private static final String EN = "en";
  42. /** The _test injector. */
  43. private Injector _testInjector;
  44. /** The _mockery. */
  45. private Mockery _mockery;
  46. /** The _game settings. */
  47. private GameSettings _gameSettings;
  48. /** The _translator. */
  49. private ITranslator _translator;
  50. /** The _model. */
  51. private IGameSettingsModel _model;
  52. /**
  53. * Instantiates a new test game settings model.
  54. */
  55. public TestGameSettingsModel() {
  56. }
  57. /**
  58. * Sets the up.
  59. *
  60. * @see junit.framework.TestCase#setUp()
  61. */
  62. public void setUp() {
  63. _testInjector = Guice.createInjector(new HeavyLeadTestModule());
  64. _mockery = _testInjector.getInstance(Mockery.class);
  65. _gameSettings = _testInjector.getInstance(GameSettings.class);
  66. _translator = _testInjector.getInstance(ITranslator.class);
  67. final Dictionary<String, String> languageOptions = new Hashtable<String, String>();
  68. languageOptions.put(EN, ENGLISH);
  69. languageOptions.put(FR, FRENCH);
  70. _mockery.checking(new Expectations() {
  71. {
  72. oneOf(_translator).getLanguageOptions();
  73. will(returnValue(languageOptions));
  74. setGameSettingReturnValues(TEST_WIDTH,
  75. TEST_HEIGHT,
  76. TEST_DEPTH,
  77. TEST_FREQUENCY,
  78. TEST_ISFULLSCREEN,
  79. EN);
  80. }
  81. });
  82. _model = new GameSettingsModel(_gameSettings, _translator);
  83. }
  84. /**
  85. * Gets the settings values.
  86. *
  87. * @param width the width
  88. * @param height the height
  89. * @param depth the depth
  90. * @param frequency the frequency
  91. * @param isFullScreen the is full screen
  92. * @param displayLanguage the display language
  93. */
  94. private void setGameSettingReturnValues(final int width,
  95. final int height,
  96. final int depth,
  97. final int frequency,
  98. final boolean isFullScreen,
  99. final String displayLanguage) {
  100. _mockery.checking(new Expectations() {
  101. {
  102. oneOf(_gameSettings).getWidth();
  103. will(returnValue(width));
  104. oneOf(_gameSettings).getHeight();
  105. will(returnValue(height));
  106. oneOf(_gameSettings).getDepth();
  107. will(returnValue(depth));
  108. oneOf(_gameSettings).getFrequency();
  109. will(returnValue(frequency));
  110. oneOf(_gameSettings).isFullscreen();
  111. will(returnValue(isFullScreen));
  112. oneOf(_gameSettings).get(LANGUAGE, EN);
  113. will(returnValue(displayLanguage));
  114. }
  115. });
  116. }
  117. /**
  118. * Tear down.
  119. *
  120. * @see junit.framework.TestCase#tearDown()
  121. */
  122. public void tearDown() {
  123. _mockery.assertIsSatisfied();
  124. }
  125. /**
  126. * Test creation.
  127. */
  128. public void testCreation() {
  129. }
  130. /**
  131. * Test get resolution options.
  132. */
  133. public void testGetResolutionOptions() {
  134. final List<String> expectedOptions = new ArrayList<String>();
  135. expectedOptions.add("800x600");
  136. expectedOptions.add("1024x768");
  137. expectedOptions.add("1280x1024");
  138. expectedOptions.add("1600x1200");
  139. expectedOptions.add("1440x900");
  140. assertEquals(expectedOptions, _model.getResolutionOptions());
  141. }
  142. /**
  143. * Test get depth options.
  144. */
  145. public void testGetDepthOptions() {
  146. final List<String> expectedOptions = new ArrayList<String>();
  147. expectedOptions.add("16");
  148. expectedOptions.add("24");
  149. expectedOptions.add("32");
  150. assertEquals(expectedOptions, _model.getDepthOptions());
  151. }
  152. /**
  153. * Test get frequency options.
  154. */
  155. public void testGetFrequencyOptions() {
  156. final List<String> expectedOptions = new ArrayList<String>();
  157. expectedOptions.add("60");
  158. expectedOptions.add("70");
  159. expectedOptions.add("72");
  160. expectedOptions.add("75");
  161. expectedOptions.add("85");
  162. expectedOptions.add("100");
  163. expectedOptions.add("120");
  164. expectedOptions.add("140");
  165. assertEquals(expectedOptions, _model.getFrequencyOptions());
  166. }
  167. /**
  168. * Test set display width.
  169. */
  170. public void testSetDisplayWidth() {
  171. _mockery.checking(new Expectations() {
  172. {
  173. oneOf(_gameSettings).setWidth(TEST_WIDTH);
  174. }
  175. });
  176. _model.setDisplayWidth(TEST_WIDTH);
  177. }
  178. /**
  179. * Test get display width.
  180. */
  181. public void testGetDisplayWidth() {
  182. _mockery.checking(new Expectations() {
  183. {
  184. oneOf(_gameSettings).getWidth();
  185. will(returnValue(TEST_WIDTH));
  186. }
  187. });
  188. assertEquals(TEST_WIDTH, _model.getDisplayWidth());
  189. }
  190. /**
  191. * Test set display height.
  192. */
  193. public void testSetDisplayHeight() {
  194. _mockery.checking(new Expectations() {
  195. {
  196. oneOf(_gameSettings).setHeight(TEST_HEIGHT);
  197. }
  198. });
  199. _model.setDisplayHeight(TEST_HEIGHT);
  200. }
  201. /**
  202. * Test get display height.
  203. */
  204. public void testGetDisplayHeight() {
  205. _mockery.checking(new Expectations() {
  206. {
  207. oneOf(_gameSettings).getHeight();
  208. will(returnValue(TEST_HEIGHT));
  209. }
  210. });
  211. assertEquals(TEST_HEIGHT, _model.getDisplayHeight());
  212. }
  213. /**
  214. * Test get resolution.
  215. */
  216. public void testGetResolution() {
  217. _mockery.checking(new Expectations() {
  218. {
  219. oneOf(_gameSettings).getWidth();
  220. will(returnValue(TEST_WIDTH));
  221. oneOf(_gameSettings).getHeight();
  222. will(returnValue(TEST_HEIGHT));
  223. }
  224. });
  225. assertEquals(TEST_RESOLUTION, _model.getResolution());
  226. }
  227. /**
  228. * Test set depth.
  229. */
  230. public void testSetDepth() {
  231. _mockery.checking(new Expectations() {
  232. {
  233. oneOf(_gameSettings).setDepth(TEST_DEPTH);
  234. }
  235. });
  236. _model.setDepth(TEST_DEPTH);
  237. }
  238. /**
  239. * Test get depth.
  240. */
  241. public void testGetDepth() {
  242. _mockery.checking(new Expectations() {
  243. {
  244. oneOf(_gameSettings).getDepth();
  245. will(returnValue(TEST_DEPTH));
  246. }
  247. });
  248. assertEquals(Integer.toString(TEST_DEPTH), _model.getDepth());
  249. }
  250. /**
  251. * Test set frequency.
  252. */
  253. public void testSetFrequency() {
  254. _mockery.checking(new Expectations() {
  255. {
  256. oneOf(_gameSettings).setFrequency(TEST_FREQUENCY);
  257. }
  258. });
  259. _model.setFrequency(TEST_FREQUENCY);
  260. }
  261. /**
  262. * Test get frequency.
  263. */
  264. public void testGetFrequency() {
  265. _mockery.checking(new Expectations() {
  266. {
  267. oneOf(_gameSettings).getFrequency();
  268. will(returnValue(TEST_FREQUENCY));
  269. }
  270. });
  271. assertEquals(Integer.toString(TEST_FREQUENCY), _model.getFrequency());
  272. }
  273. /**
  274. * Test Set is full screen.
  275. */
  276. public void testSetIsFullScreen() {
  277. _mockery.checking(new Expectations() {
  278. {
  279. oneOf(_gameSettings).setFullscreen(TEST_ISFULLSCREEN);
  280. }
  281. });
  282. _model.setIsFullScreen(TEST_ISFULLSCREEN);
  283. }
  284. /**
  285. * Test get is full screen.
  286. */
  287. public void testGetIsFullScreen() {
  288. _mockery.checking(new Expectations() {
  289. {
  290. oneOf(_gameSettings).isFullscreen();
  291. will(returnValue(TEST_ISFULLSCREEN));
  292. }
  293. });
  294. assertEquals(TEST_ISFULLSCREEN, _model.getIsFullScreen());
  295. }
  296. /**
  297. * Test get language options.
  298. */
  299. public void testGetLanguageOptions() {
  300. final Dictionary<String, String> expectedLanguageOptions =
  301. new Hashtable<String, String>();
  302. expectedLanguageOptions.put(EN, ENGLISH);
  303. expectedLanguageOptions.put(FR, FRENCH);
  304. final Dictionary<String, String> languageOptions = _model.getLanguageOptions();
  305. assertEquals(expectedLanguageOptions, languageOptions);
  306. }
  307. /**
  308. * Test display language.
  309. */
  310. public void testSetDisplayLanguage() {
  311. _mockery.checking(new Expectations() {
  312. {
  313. oneOf(_gameSettings).set(LANGUAGE, EN);
  314. oneOf(_translator).setLanguage(EN);
  315. }
  316. });
  317. _model.setDisplayLanguage(EN);
  318. }
  319. /**
  320. * Test get display language.
  321. */
  322. public void testGetDisplayLanguage() {
  323. _mockery.checking(new Expectations() {
  324. {
  325. oneOf(_gameSettings).get(LANGUAGE, EN);
  326. will(returnValue(EN));
  327. }
  328. });
  329. assertEquals(EN, _model.getDisplayLanguage());
  330. }
  331. /**
  332. * Test save.
  333. *
  334. * @throws IOException Signals that an I/O exception has occurred.
  335. */
  336. public void testSaveSuccessful() throws IOException {
  337. _mockery.checking(new Expectations() {
  338. {
  339. oneOf(_gameSettings).save();
  340. }
  341. });
  342. setGameSettingReturnValues(TEST_WIDTH,
  343. TEST_HEIGHT,
  344. TEST_DEPTH,
  345. TEST_FREQUENCY,
  346. TEST_ISFULLSCREEN,
  347. EN);
  348. _model.save();
  349. }
  350. /**
  351. * Test save with exception.
  352. *
  353. * @throws IOException Signals that an I/O exception has occurred.
  354. */
  355. public void testSaveWithException() throws IOException {
  356. _mockery.checking(new Expectations() {
  357. {
  358. oneOf(_gameSettings).save();
  359. will(throwException(new IOException()));
  360. }
  361. });
  362. try {
  363. _model.save();
  364. // fail because we should have thrown an exception
  365. assertTrue(false);
  366. } catch (IOException e) {
  367. // pass if we threw an exception
  368. assertTrue(true);
  369. }
  370. }
  371. /**
  372. * Test changing resolution makes model dirty.
  373. */
  374. public void testChangingResolutionMakesModelDirty() {
  375. setGameSettingReturnValues(TEST_WIDTH,
  376. TEST_HEIGHT,
  377. TEST_DEPTH,
  378. TEST_FREQUENCY,
  379. TEST_ISFULLSCREEN,
  380. EN);
  381. assertFalse(_model.isDirty());
  382. final int newHeight = TEST_HEIGHT + 1;
  383. _mockery.checking(new Expectations() {
  384. {
  385. oneOf(_gameSettings).setHeight(newHeight);
  386. }
  387. });
  388. _model.setDisplayHeight(newHeight);
  389. setGameSettingReturnValues(TEST_WIDTH,
  390. newHeight,
  391. TEST_DEPTH,
  392. TEST_FREQUENCY,
  393. TEST_ISFULLSCREEN,
  394. EN);
  395. assertTrue(_model.isDirty());
  396. }
  397. /**
  398. * Test changing depth makes model dirty.
  399. */
  400. public void testChangingDepthMakesModelDirty() {
  401. setGameSettingReturnValues(TEST_WIDTH,
  402. TEST_HEIGHT,
  403. TEST_DEPTH,
  404. TEST_FREQUENCY,
  405. TEST_ISFULLSCREEN,
  406. EN);
  407. assertFalse(_model.isDirty());
  408. final int newDepth = TEST_DEPTH + 1;
  409. _mockery.checking(new Expectations() {
  410. {
  411. oneOf(_gameSettings).setDepth(newDepth);
  412. }
  413. });
  414. _model.setDepth(newDepth);
  415. setGameSettingReturnValues(TEST_WIDTH,
  416. TEST_HEIGHT,
  417. newDepth,
  418. TEST_FREQUENCY,
  419. TEST_ISFULLSCREEN,
  420. EN);
  421. assertTrue(_model.isDirty());
  422. }
  423. /**
  424. * Test changing frequency makes model dirty.
  425. */
  426. public void testChangingFrequencyMakesModelDirty() {
  427. setGameSettingReturnValues(TEST_WIDTH,
  428. TEST_HEIGHT,
  429. TEST_DEPTH,
  430. TEST_FREQUENCY,
  431. TEST_ISFULLSCREEN,
  432. EN);
  433. assertFalse(_model.isDirty());
  434. final int newFrequency = TEST_FREQUENCY + 1;
  435. _mockery.checking(new Expectations() {
  436. {
  437. oneOf(_gameSettings).setFrequency(newFrequency);
  438. }
  439. });
  440. _model.setFrequency(newFrequency);
  441. setGameSettingReturnValues(TEST_WIDTH,
  442. TEST_HEIGHT,
  443. TEST_DEPTH,
  444. newFrequency,
  445. TEST_ISFULLSCREEN,
  446. EN);
  447. assertTrue(_model.isDirty());
  448. }
  449. /**
  450. * Test changing is full screen makes model dirty.
  451. */
  452. public void testChangingIsFullScreenMakesModelDirty() {
  453. setGameSettingReturnValues(TEST_WIDTH,
  454. TEST_HEIGHT,
  455. TEST_DEPTH,
  456. TEST_FREQUENCY,
  457. TEST_ISFULLSCREEN,
  458. EN);
  459. assertFalse(_model.isDirty());
  460. final boolean newIsFullScreen = !TEST_ISFULLSCREEN;
  461. _mockery.checking(new Expectations() {
  462. {
  463. oneOf(_gameSettings).setFullscreen(newIsFullScreen);
  464. }
  465. });
  466. _model.setIsFullScreen(newIsFullScreen);
  467. setGameSettingReturnValues(TEST_WIDTH,
  468. TEST_HEIGHT,
  469. TEST_DEPTH,
  470. TEST_FREQUENCY,
  471. newIsFullScreen,
  472. EN);
  473. assertTrue(_model.isDirty());
  474. }
  475. /**
  476. * Test changing display language makes model dirty.
  477. */
  478. public void testChangingDisplayLanguageMakesModelDirty() {
  479. setGameSettingReturnValues(TEST_WIDTH,
  480. TEST_HEIGHT,
  481. TEST_DEPTH,
  482. TEST_FREQUENCY,
  483. TEST_ISFULLSCREEN,
  484. EN);
  485. assertFalse(_model.isDirty());
  486. final String newDisplayLanguage = "ab";
  487. _mockery.checking(new Expectations() {
  488. {
  489. oneOf(_gameSettings).set(LANGUAGE, newDisplayLanguage);
  490. oneOf(_translator).setLanguage(newDisplayLanguage);
  491. }
  492. });
  493. _model.setDisplayLanguage(newDisplayLanguage);
  494. setGameSettingReturnValues(TEST_WIDTH,
  495. TEST_HEIGHT,
  496. TEST_DEPTH,
  497. TEST_FREQUENCY,
  498. TEST_ISFULLSCREEN,
  499. newDisplayLanguage);
  500. assertTrue(_model.isDirty());
  501. }
  502. /**
  503. * Test saving dirty model makes it clean.
  504. *
  505. * @throws IOException Signals that an I/O exception has occurred.
  506. */
  507. public void testSavingDirtyModelMakesItClean() throws IOException {
  508. // Use existing code to make a change to the settings
  509. testChangingResolutionMakesModelDirty();
  510. _mockery.checking(new Expectations() {
  511. {
  512. oneOf(_gameSettings).save();
  513. }
  514. });
  515. final int newHeight = TEST_HEIGHT + 1;
  516. setGameSettingReturnValues(TEST_WIDTH,
  517. newHeight,
  518. TEST_DEPTH,
  519. TEST_FREQUENCY,
  520. TEST_ISFULLSCREEN,
  521. EN);
  522. _model.save();
  523. setGameSettingReturnValues(TEST_WIDTH,
  524. newHeight,
  525. TEST_DEPTH,
  526. TEST_FREQUENCY,
  527. TEST_ISFULLSCREEN,
  528. EN);
  529. assertFalse(_model.isDirty());
  530. }
  531. }