/**
   * This test tackles the issue, that if the waitbrick is at last position within a script, the
   * whole thread will not be set to "destroy" to early. So this test checks that the script is set
   * to "sleeping" until wait time has exceeded.
   */
  public void testWaitWhenBrickIsLast() {
    String customName1 = "custom1";
    LookData customData1 = createCostumeData(customName1);

    LookData customData2 = createCostumeData(customNameResult_);

    String spriteName = "sprite1";
    sprite_ = spriteManager.getSprite(spriteName, true);
    sprite_.addLookData(customData1);
    sprite_.addLookData(customData2);

    // simulate SetLookBrick
    sprite_.getLook().setLookData(customData1);

    // create BroadcastScript which executes one NextLookBrick
    String startScriptName = "broadcastScript";
    final StartScript startScript = new StartScript(sprite_, startScriptName);

    final Formula time = new Formula(1);
    WaitBrick waitBrick = new WaitBrick(spriteName, time, startScript);
    NextLookBrick nextLookBrick = new NextLookBrick(spriteName);
    startScript.addBrick(nextLookBrick);
    startScript.addBrick(waitBrick);

    final CatThread thread = new CatThread(spriteName + startScriptName, startScript);
    scheduler.schedule(thread);
    // execute nextLookBrick
    scheduler.execute();
    // execute waitBrick
    scheduler.execute();

    Timer timer =
        new Timer() {
          public void run() {
            System.out.println(thread.getStatus());
            assertTrue(thread.getStatus() == CatThread.SLEEPING);

            // tell the test system the test is now done
            finishTest();
          }
        };

    timer.schedule(900);
    // Set a delay period significantly longer than the
    // event is expected to take
    // delayTestFinish(1100);
  }
  @Override
  public boolean execute(Sprite sprite) {
    final Vector<BroadcastScript> receiver =
        Stage.getInstance().getMessageContainer().getReceiverOfMessage(message);

    CatrobatDebug.debug("BroadcastWait - message: " + message);

    if (receiver == null) {
      CatrobatDebug.debug("receiver is null");
      return false;
    }

    if (receiver.size() == 0) {
      CatrobatDebug.debug("no receivers");
      return true;
    }

    CatThread executiveThread = CatScheduler.get().getThread(script.getExecutor());

    WaitCount signaler = new WaitCount(executiveThread);

    for (BroadcastScript receiverScript : receiver) {

      receiverScript.resetBroadcastScript();

      receiverScript.executeBroadcastWait(signaler);

      CatrobatDebug.debug(
          "BroadcastWait - receiver, script: "
              + receiverScript.getName()
              + ", sprite: "
              + receiverScript.getSprite().getName());
    }

    executiveThread.awaitSignal(signaler);

    return true;
  }
  public void testWait() {

    String costumeName1 = "costume1";
    LookData costumeData1 = createCostumeData(costumeName1);

    LookData costumeData2 = createCostumeData(customNameResult_);

    String spriteName = "sprite1";
    sprite_ = spriteManager.getSprite(spriteName, true);

    sprite_.addLookData(costumeData1);
    sprite_.addLookData(costumeData2);

    // simulate SetLookBrick
    sprite_.getLook().setLookData(costumeData1);

    // create BroadcastScript which executes one NextLookBrick
    String startScriptName = "broadcastScript";
    StartScript startScript = new StartScript(sprite_, startScriptName);

    Formula time = new Formula(0.4);
    WaitBrick waitBrick = new WaitBrick(spriteName, time, startScript);
    NextLookBrick nextLookBrick = new NextLookBrick(spriteName);
    startScript.addBrick(waitBrick);
    startScript.addBrick(nextLookBrick);

    CatThread thread = new CatThread(spriteName + startScriptName, startScript);
    scheduler.schedule(thread);

    // execute WaitBrick
    scheduler.execute();

    // should have no effect because there is no --> currentThread.getStatus() == CatThread.READY
    scheduler.execute();
    scheduler.execute();
    scheduler.execute();
    scheduler.execute();
    scheduler.execute();
    scheduler.execute();
    //

    Timer timer =
        new Timer() {
          public void run() {

            // execute NextLookBrick
            scheduler.execute();

            assertEquals(customNameResult_, sprite_.getLook().getLookData().getName());

            // tell the test system the test is now done
            finishTest();
          }
        };

    // Set a delay period significantly longer than the
    // event is expected to take
    delayTestFinish(700);

    // Schedule the event and return control to the test system
    timer.schedule(500);
  }
 public void gwtTearDown() {
   stage.setCanvas(null);
   spriteManager.reset();
   scheduler.clear();
   sprite_ = null;
 }
 public WaitBrickTest() {
   stage = Stage.getInstance();
   scene = Scene.get();
   spriteManager = stage.getSpriteManager();
   scheduler = CatScheduler.get();
 }
 public void gwtTearDown() {
   stage.setCanvas(null);
   stage.getSpriteManager().reset();
   CatScheduler.get().clear();
 }
  public void testForeverNextCostume() {

    CatScheduler.get().clear();

    String spriteName = "spriteName";
    Sprite sprite = stage.getSpriteManager().getSprite(spriteName, true);

    String costumeName1 = "costume1";
    String costumeName2 = "costume2";
    String costumeName3 = "costume3";

    LookData costumeData1 = createCostumeData(costumeName1);
    LookData costumeData2 = createCostumeData(costumeName2);
    LookData costumeData3 = createCostumeData(costumeName3);

    sprite.addLookData(costumeData1);
    sprite.addLookData(costumeData2);
    sprite.addLookData(costumeData3);

    String scriptName = "scriptName";
    StartScript startScript = new StartScript(sprite, scriptName);

    NextLookBrick nextLookBrick = new NextLookBrick(spriteName);
    ForeverBrick repeatBrick = new ForeverBrick(spriteName);
    LoopEndBrick loopEndBrick = new LoopEndBrick(spriteName, repeatBrick);
    repeatBrick.setLoopEndBrick(loopEndBrick);

    startScript.addBrick(repeatBrick);
    startScript.addBrick(nextLookBrick);
    startScript.addBrick(loopEndBrick);

    sprite.addScript(startScript);

    // simulate SetLookBrick
    sprite.getLook().setLookData(costumeData1);
    sprite.getLook().hide();

    CatThread thread = new CatThread("threadName", startScript);
    CatScheduler.get().schedule(thread);

    CatScheduler.get().execute(); // repeat

    CatScheduler.get().execute(); // next
    CatScheduler.get().execute(); // loop end

    assertEquals(costumeName2, sprite.getLook().getLookData().getName());

    CatScheduler.get().execute(); // next
    CatScheduler.get().execute(); // loop end

    assertEquals(costumeName3, sprite.getLook().getLookData().getName());

    CatScheduler.get().execute(); // next
    CatScheduler.get().execute(); // loop end

    assertEquals(costumeName1, sprite.getLook().getLookData().getName());

    CatScheduler.get().execute(); // next
    CatScheduler.get().execute(); // loop end

    assertEquals(costumeName2, sprite.getLook().getLookData().getName());

    assertEquals(LoopEndBrick.FOREVER, loopEndBrick.getTimesToRepeat());

    assertEquals(1, CatScheduler.get().getThreadCount());

    CatScheduler.get().clear();
  }