/src/test/java/com/heavylead/models/concrete/TestGameSettingsModel.java
Java | 631 lines | 402 code | 100 blank | 129 comment | 0 complexity | 742858183509223eca7ee6ac054a00c7 MD5 | raw file
Possible License(s): BSD-3-Clause
- package com.heavylead.models.concrete;
-
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Dictionary;
- import java.util.Hashtable;
- import java.util.List;
-
- import junit.framework.TestCase;
-
- import org.jmock.Expectations;
- import org.jmock.Mockery;
-
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- import com.heavylead.injection.HeavyLeadTestModule;
- import com.heavylead.models.interfaces.IGameSettingsModel;
- import com.heavylead.models.interfaces.ITranslator;
- import com.jme.system.GameSettings;
-
- /**
- * The Class TestGameSettingsModel.
- */
- public final class TestGameSettingsModel extends TestCase {
-
- /** The Constant FR. */
- private static final String FR = "fr";
-
- /** The Constant FRENCH. */
- private static final String FRENCH = "French";
-
- /** The Constant ENGLISH. */
- private static final String ENGLISH = "English";
-
- /** The Constant TEST_WIDTH. */
- private static final int TEST_WIDTH = 10;
-
- /** The Constant TEST_HEIGHT. */
- private static final int TEST_HEIGHT = 10;
-
- /** The Constant TEST_RESOLUTION. */
- private static final String TEST_RESOLUTION = "10x10";
-
- /** The Constant TEST_DEPTH. */
- private static final int TEST_DEPTH = 16;
-
- /** The Constant TEST_FREQUENCY. */
- private static final int TEST_FREQUENCY = 60;
-
- /** The Constant TEST_ISFULLSCREEN. */
- private static final boolean TEST_ISFULLSCREEN = false;
-
- /** The Constant LANGUAGE. */
- private static final String LANGUAGE = "Language";
-
- /** The Constant TEST_DISPLAYLANGUAGE. */
- private static final String EN = "en";
-
- /** The _test injector. */
- private Injector _testInjector;
-
- /** The _mockery. */
- private Mockery _mockery;
-
- /** The _game settings. */
- private GameSettings _gameSettings;
-
- /** The _translator. */
- private ITranslator _translator;
-
- /** The _model. */
- private IGameSettingsModel _model;
-
- /**
- * Instantiates a new test game settings model.
- */
- public TestGameSettingsModel() {
- }
-
- /**
- * Sets the up.
- *
- * @see junit.framework.TestCase#setUp()
- */
- public void setUp() {
- _testInjector = Guice.createInjector(new HeavyLeadTestModule());
-
- _mockery = _testInjector.getInstance(Mockery.class);
-
- _gameSettings = _testInjector.getInstance(GameSettings.class);
- _translator = _testInjector.getInstance(ITranslator.class);
-
- final Dictionary<String, String> languageOptions = new Hashtable<String, String>();
- languageOptions.put(EN, ENGLISH);
- languageOptions.put(FR, FRENCH);
-
- _mockery.checking(new Expectations() {
- {
- oneOf(_translator).getLanguageOptions();
- will(returnValue(languageOptions));
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
- }
-
- });
-
- _model = new GameSettingsModel(_gameSettings, _translator);
- }
-
- /**
- * Gets the settings values.
- *
- * @param width the width
- * @param height the height
- * @param depth the depth
- * @param frequency the frequency
- * @param isFullScreen the is full screen
- * @param displayLanguage the display language
- */
- private void setGameSettingReturnValues(final int width,
- final int height,
- final int depth,
- final int frequency,
- final boolean isFullScreen,
- final String displayLanguage) {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).getWidth();
- will(returnValue(width));
- oneOf(_gameSettings).getHeight();
- will(returnValue(height));
- oneOf(_gameSettings).getDepth();
- will(returnValue(depth));
- oneOf(_gameSettings).getFrequency();
- will(returnValue(frequency));
- oneOf(_gameSettings).isFullscreen();
- will(returnValue(isFullScreen));
- oneOf(_gameSettings).get(LANGUAGE, EN);
- will(returnValue(displayLanguage));
- }
- });
- }
-
- /**
- * Tear down.
- *
- * @see junit.framework.TestCase#tearDown()
- */
- public void tearDown() {
- _mockery.assertIsSatisfied();
- }
-
- /**
- * Test creation.
- */
- public void testCreation() {
- }
-
- /**
- * Test get resolution options.
- */
- public void testGetResolutionOptions() {
- final List<String> expectedOptions = new ArrayList<String>();
- expectedOptions.add("800x600");
- expectedOptions.add("1024x768");
- expectedOptions.add("1280x1024");
- expectedOptions.add("1600x1200");
- expectedOptions.add("1440x900");
-
- assertEquals(expectedOptions, _model.getResolutionOptions());
- }
-
- /**
- * Test get depth options.
- */
- public void testGetDepthOptions() {
- final List<String> expectedOptions = new ArrayList<String>();
- expectedOptions.add("16");
- expectedOptions.add("24");
- expectedOptions.add("32");
-
- assertEquals(expectedOptions, _model.getDepthOptions());
- }
-
- /**
- * Test get frequency options.
- */
- public void testGetFrequencyOptions() {
- final List<String> expectedOptions = new ArrayList<String>();
- expectedOptions.add("60");
- expectedOptions.add("70");
- expectedOptions.add("72");
- expectedOptions.add("75");
- expectedOptions.add("85");
- expectedOptions.add("100");
- expectedOptions.add("120");
- expectedOptions.add("140");
-
- assertEquals(expectedOptions, _model.getFrequencyOptions());
- }
-
- /**
- * Test set display width.
- */
- public void testSetDisplayWidth() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).setWidth(TEST_WIDTH);
- }
- });
-
- _model.setDisplayWidth(TEST_WIDTH);
- }
-
- /**
- * Test get display width.
- */
- public void testGetDisplayWidth() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).getWidth();
- will(returnValue(TEST_WIDTH));
- }
- });
-
- assertEquals(TEST_WIDTH, _model.getDisplayWidth());
- }
-
- /**
- * Test set display height.
- */
- public void testSetDisplayHeight() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).setHeight(TEST_HEIGHT);
- }
- });
-
- _model.setDisplayHeight(TEST_HEIGHT);
- }
-
- /**
- * Test get display height.
- */
- public void testGetDisplayHeight() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).getHeight();
- will(returnValue(TEST_HEIGHT));
- }
- });
-
- assertEquals(TEST_HEIGHT, _model.getDisplayHeight());
- }
-
- /**
- * Test get resolution.
- */
- public void testGetResolution() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).getWidth();
- will(returnValue(TEST_WIDTH));
- oneOf(_gameSettings).getHeight();
- will(returnValue(TEST_HEIGHT));
- }
- });
-
- assertEquals(TEST_RESOLUTION, _model.getResolution());
- }
-
- /**
- * Test set depth.
- */
- public void testSetDepth() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).setDepth(TEST_DEPTH);
- }
- });
-
- _model.setDepth(TEST_DEPTH);
- }
-
- /**
- * Test get depth.
- */
- public void testGetDepth() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).getDepth();
- will(returnValue(TEST_DEPTH));
- }
- });
-
- assertEquals(Integer.toString(TEST_DEPTH), _model.getDepth());
- }
-
- /**
- * Test set frequency.
- */
- public void testSetFrequency() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).setFrequency(TEST_FREQUENCY);
- }
- });
-
- _model.setFrequency(TEST_FREQUENCY);
- }
-
- /**
- * Test get frequency.
- */
- public void testGetFrequency() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).getFrequency();
- will(returnValue(TEST_FREQUENCY));
- }
- });
-
- assertEquals(Integer.toString(TEST_FREQUENCY), _model.getFrequency());
- }
-
- /**
- * Test Set is full screen.
- */
- public void testSetIsFullScreen() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).setFullscreen(TEST_ISFULLSCREEN);
- }
- });
-
- _model.setIsFullScreen(TEST_ISFULLSCREEN);
- }
-
- /**
- * Test get is full screen.
- */
- public void testGetIsFullScreen() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).isFullscreen();
- will(returnValue(TEST_ISFULLSCREEN));
- }
- });
-
- assertEquals(TEST_ISFULLSCREEN, _model.getIsFullScreen());
- }
-
- /**
- * Test get language options.
- */
- public void testGetLanguageOptions() {
- final Dictionary<String, String> expectedLanguageOptions =
- new Hashtable<String, String>();
- expectedLanguageOptions.put(EN, ENGLISH);
- expectedLanguageOptions.put(FR, FRENCH);
-
- final Dictionary<String, String> languageOptions = _model.getLanguageOptions();
-
- assertEquals(expectedLanguageOptions, languageOptions);
- }
-
- /**
- * Test display language.
- */
- public void testSetDisplayLanguage() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).set(LANGUAGE, EN);
- oneOf(_translator).setLanguage(EN);
- }
- });
-
- _model.setDisplayLanguage(EN);
- }
-
- /**
- * Test get display language.
- */
- public void testGetDisplayLanguage() {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).get(LANGUAGE, EN);
- will(returnValue(EN));
- }
- });
-
- assertEquals(EN, _model.getDisplayLanguage());
- }
-
- /**
- * Test save.
- *
- * @throws IOException Signals that an I/O exception has occurred.
- */
- public void testSaveSuccessful() throws IOException {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).save();
- }
- });
-
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
-
- _model.save();
- }
-
- /**
- * Test save with exception.
- *
- * @throws IOException Signals that an I/O exception has occurred.
- */
- public void testSaveWithException() throws IOException {
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).save();
- will(throwException(new IOException()));
- }
- });
-
- try {
- _model.save();
-
- // fail because we should have thrown an exception
- assertTrue(false);
- } catch (IOException e) {
- // pass if we threw an exception
- assertTrue(true);
- }
- }
-
- /**
- * Test changing resolution makes model dirty.
- */
- public void testChangingResolutionMakesModelDirty() {
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
- assertFalse(_model.isDirty());
-
- final int newHeight = TEST_HEIGHT + 1;
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).setHeight(newHeight);
- }
- });
-
- _model.setDisplayHeight(newHeight);
-
- setGameSettingReturnValues(TEST_WIDTH,
- newHeight,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
- assertTrue(_model.isDirty());
- }
-
- /**
- * Test changing depth makes model dirty.
- */
- public void testChangingDepthMakesModelDirty() {
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
- assertFalse(_model.isDirty());
-
- final int newDepth = TEST_DEPTH + 1;
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).setDepth(newDepth);
- }
- });
-
- _model.setDepth(newDepth);
-
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- newDepth,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
- assertTrue(_model.isDirty());
- }
-
- /**
- * Test changing frequency makes model dirty.
- */
- public void testChangingFrequencyMakesModelDirty() {
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
- assertFalse(_model.isDirty());
-
- final int newFrequency = TEST_FREQUENCY + 1;
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).setFrequency(newFrequency);
- }
- });
-
- _model.setFrequency(newFrequency);
-
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- newFrequency,
- TEST_ISFULLSCREEN,
- EN);
- assertTrue(_model.isDirty());
-
- }
-
- /**
- * Test changing is full screen makes model dirty.
- */
- public void testChangingIsFullScreenMakesModelDirty() {
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
- assertFalse(_model.isDirty());
-
- final boolean newIsFullScreen = !TEST_ISFULLSCREEN;
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).setFullscreen(newIsFullScreen);
- }
- });
-
- _model.setIsFullScreen(newIsFullScreen);
-
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- TEST_FREQUENCY,
- newIsFullScreen,
- EN);
- assertTrue(_model.isDirty());
- }
-
- /**
- * Test changing display language makes model dirty.
- */
- public void testChangingDisplayLanguageMakesModelDirty() {
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
- assertFalse(_model.isDirty());
-
- final String newDisplayLanguage = "ab";
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).set(LANGUAGE, newDisplayLanguage);
- oneOf(_translator).setLanguage(newDisplayLanguage);
- }
- });
-
- _model.setDisplayLanguage(newDisplayLanguage);
-
- setGameSettingReturnValues(TEST_WIDTH,
- TEST_HEIGHT,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- newDisplayLanguage);
- assertTrue(_model.isDirty());
- }
-
- /**
- * Test saving dirty model makes it clean.
- *
- * @throws IOException Signals that an I/O exception has occurred.
- */
- public void testSavingDirtyModelMakesItClean() throws IOException {
- // Use existing code to make a change to the settings
- testChangingResolutionMakesModelDirty();
-
- _mockery.checking(new Expectations() {
- {
- oneOf(_gameSettings).save();
- }
- });
-
- final int newHeight = TEST_HEIGHT + 1;
- setGameSettingReturnValues(TEST_WIDTH,
- newHeight,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
-
- _model.save();
-
- setGameSettingReturnValues(TEST_WIDTH,
- newHeight,
- TEST_DEPTH,
- TEST_FREQUENCY,
- TEST_ISFULLSCREEN,
- EN);
-
- assertFalse(_model.isDirty());
- }
- }