@Test
  public void consumedFocusGainedEventsShouldNotStartTheCaret() throws Exception {
    assertEquals(false, panel.isCaretBlinking());

    new FocusGainedEvent().consumed().dispatch(panel);

    assertEquals(false, panel.isCaretBlinking());
  }
  @Test
  public void caretAnimationIsStoppedWhenPanelIsDisowned() throws Exception {
    stage.getKeyListener().focusOn(panel);
    assertEquals(true, panel.isCaretBlinking());

    panel.setParent(null);

    assertEquals(false, panel.isCaretBlinking());
  }
  @Test
  public void consumedCharTypedEventsDoNothing() throws Exception {
    panel = new TextBoxPanel();
    root.add(panel);
    panel.getModel().setText("Some Text");
    new CharTypedEvent(0, 'A').consumed().dispatch(panel);

    assertEquals("Some Text", panel.getText());
  }
  @Test
  public void consumedKeyPressEventsDoNothing() throws Exception {
    panel = new TextBoxPanel();
    root.add(panel);
    panel.getModel().setText("Some Text");
    new KeyPressedEvent(0, KeyEvent.KEY_BACK_SPACE, 0).consumed().dispatch(panel);

    assertEquals("Some Text", panel.getText());
  }
  @Test
  public void shouldRequireLayoutAfterConsumableSizeChanges() throws Exception {
    FakeScene root = new FakeScene();
    root.add(panel);
    panel.getRoot();
    panel.resetLayout();

    panel.consumableAreaChanged();

    assertEquals(true, panel.needsLayout());
  }
  @Test
  public void canLoseFocus() {
    stage.getKeyListener().focusOn(panel);
    root.dirtyRegions.clear();
    stage.getKeyListener().focusOn(root);

    assertEquals(false, panel.hasFocus());
    assertEquals(false, panel.isCaretBlinking());
    assertEquals(true, root.dirtyRegions.contains(panel.getBounds()));
    assertEquals(true, root.dirtyRegions.contains(parent.getBounds()));
  }
  @Test
  public void canGainFocus() {
    assertEquals(0, root.dirtyRegions.size());

    stage.getKeyListener().focusOn(panel);

    assertEquals(true, panel.hasFocus());
    assertEquals(true, panel.isCaretBlinking());
    assertEquals(true, root.dirtyRegions.contains(panel.getBounds()));
    assertEquals(true, root.dirtyRegions.contains(parent.getBounds()));
  }
  @Test
  public void valuChangedEventInvokedWhenChangingText() throws Exception {
    panel.setText("foo");
    final MockEventAction action = new MockEventAction();
    panel.getEventHandler().add(ValueChangedEvent.class, action);

    panel.setText("foo");
    assertEquals(false, action.invoked);

    panel.setText("bar");
    assertEquals(true, action.invoked);
  }
  @Test
  public void typingACharMakesThePanelDirty() throws Exception {
    assertEquals(0, root.dirtyRegions.size());

    model.setCaretLocation(TextLocation.origin);
    new CharTypedEvent(0, 'Z').dispatch(panel);

    assertEquals(1, root.dirtyRegions.size());
    assertEquals(panel.getBounds(), root.dirtyRegions.get(0));
  }
  @Test
  public void changesToModelAreReportedOnFocusLost() throws Exception {
    final MockEventAction action = new MockEventAction();
    panel.getEventHandler().add(ValueChangedEvent.class, action);

    new FocusGainedEvent().dispatch(panel);
    model.insertChar('a');
    assertEquals(true, model.hasChanged());
    new FocusLostEvent().dispatch(panel);

    assertEquals(true, action.invoked);
  }
 @Before
 public void setUp() {
   assumeTrue(TestUtil.notHeadless());
   root = new FakeScene();
   panel = new MockTextInputPanel();
   parent = new PropPanel(new FakePropProxy());
   parent.add(panel);
   root.add(parent);
   stage = new MockStage();
   root.setStage(stage);
   model = panel.getModel();
   model.setText("Some Text");
 }
 @Test
 public void shouldDefaultLayout() throws Exception {
   assertSame(TextInputPanelLayout.instance, panel.getDefaultLayout());
 }