Class SoundPlayed


  • public class SoundPlayed
    extends java.lang.Object
    It remembers the file names of sounds that are played for doing Test Driven Development (TDD) with JUnit. If we wants to verify the behaviour of any actor or world for any sound, the test can verify the played sounds.
    Author:
    Francisco Guerra (francisco.guerra@ulpgc.es)
    • Constructor Summary

      Constructors 
      Constructor Description
      SoundPlayed()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String get()
      It returns the file name of the sound that has played.
      static boolean get​(java.lang.String soundPlayed)
      It tells if it has played a sound associated with a file name.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SoundPlayed

        public SoundPlayed()
    • Method Detail

      • get

        public static java.lang.String get()
                                    throws GreenfootRunnerExpected
        It returns the file name of the sound that has played. When there are many sounds, the file names are returned in the played order.

        For the next actor behaviour:
          
             public class ExampleActorSound extends Actor {
                 @Override
                 public void act(){
                     Greenfoot.playSound("anySound.wav");
                 }
             } 
         
         
        The next test verifies the file name of sound that has been played:
          
             import ...
             import greenfoot.TestRunnerState.SoundPlayed;
             import greenfoot.TestRunnerState.WorldCreator;
             
             @RunWith(GreenfootRunner.class)
             public class ExampleActorSoundTest {
                 @Test
                 public void testAnySound() throws Exception {
                     // GIVEN 
                     World world = WorldCreator.getWorld(400, 300, 1);
                     ExampleActorSound actor = new ExampleActorSound();
                     world.addObject(actor, 1, 1);
                     
                     // WHEN 
                     WorldCreator.runOnce(world);
                     
                     // THEN 
                     assertEquals("anySound.wav", SoundPlayed.get());
                 }
             }
         
         
        Returns:
        next sound file name that was played
        Throws:
        GreenfootRunnerExpected - is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
      • get

        public static boolean get​(java.lang.String soundPlayed)
                           throws GreenfootRunnerExpected
        It tells if it has played a sound associated with a file name.

        For the next actor behaviour:
          
             public class ExampleActorSound extends Actor {
                 @Override
                 public void act(){
                     Greenfoot.playSound("anySound.wav");
                 }
             } 
         
         
        The next test verifies the file name of sound that has been played:
          
             import ...
             import greenfoot.TestRunnerState.SoundPlayed;
             import greenfoot.TestRunnerState.WorldCreator;
             
             @RunWith(GreenfootRunner.class)
             public class ExampleActorSoundTest {
                 @Test
                 public void testAnySound() throws Exception {
                     // GIVEN 
                     World world = WorldCreator.getWorld(400, 300, 1);
                     ExampleActorSound actor = new ExampleActorSound();
                     world.addObject(actor, 1, 1);
                     
                     // WHEN 
                     WorldCreator.runOnce(world);
                     
                     // THEN 
                     assertTrue(SoundPlayed.get("anySound.wav"));
                 }
             }
         
         
        Parameters:
        soundPlayed - is the file name of sound that is wanted to verify as played.
        Returns:
        true when file name is of the sound that has been played.
        Throws:
        GreenfootRunnerExpected - is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.