TextBoxForRawTextComponentTest.java :  » Testing » UISpec4J » org » uispec4j » Java Open Source

Java Open Source » Testing » UISpec4J 
UISpec4J » org » uispec4j » TextBoxForRawTextComponentTest.java
package org.uispec4j;

import junit.framework.AssertionFailedError;
import org.uispec4j.utils.AssertionFailureNotDetectedError;
import org.uispec4j.utils.DummyActionListener;
import org.uispec4j.utils.Functor;
import org.uispec4j.utils.UIComponentFactory;
import org.uispec4j.xml.XmlAssert;

import javax.swing.*;
import javax.swing.text.*;

public class TextBoxForRawTextComponentTest extends TextBoxComponentTestCase {
  private JTextComponent jTextComponent;

  protected void setUp() throws Exception {
    super.setUp();
    init(new JTextArea());
  }

  private void init(JTextComponent swingComponent) {
    jTextComponent = swingComponent;
    jTextComponent.setName("myText");
    createTextBox("");
  }

  protected void createTextBox(String text) {
    textBox = new TextBox(jTextComponent);
    textBox.setText(text);
  }

  public void testGetComponentTypeName() throws Exception {
    assertEquals("textBox", UIComponentFactory.createUIComponent(new JTextField()).getDescriptionTypeName());
  }

  public void testGetDescription() throws Exception {
    XmlAssert.assertEquivalent("<textBox name='myText'/>", textBox.getDescription());
  }

  public void testFactory() throws Exception {
    checkFactory(new JTextArea(), TextBox.class);
    checkFactory(new JTextPane(), TextBox.class);
    checkFactory(new JEditorPane(), TextBox.class);
    checkFactory(new JTextField(), TextBox.class);
  }

  public void testAssertTextEquals() throws Exception {
    assertTrue(textBox.textEquals(""));
    jTextComponent.setText("some text");
    assertTrue(textBox.textEquals("some text"));
    assertFalse(textBox.textEquals(""));
    try {
      assertTrue(textBox.textEquals("error"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("expected:<error> but was:<some text>", e.getMessage());
    }
  }

  public void testAssertTextContains() throws Exception {
    jTextComponent.setText("some text");
    assertTrue(textBox.textContains("some"));
    try {
      assertTrue(textBox.textContains("error"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The component text does not contain 'error' - actual content is:some text",
                   e.getMessage());
    }
  }

  public void testAssertTextDoesNotContain() throws Exception {
    jTextComponent.setText("some text");
    assertTrue(textBox.textDoesNotContain("xxx"));
    try {
      assertTrue(textBox.textDoesNotContain("some"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The component text should not contain 'some' - actual content is:some text",
                   e.getMessage());
    }
  }

  public void testAssertTextIsEditable() throws Exception {
    jTextComponent.setEditable(true);
    assertTrue(textBox.isEditable());
    jTextComponent.setEditable(false);
    assertFalse(textBox.isEditable());
  }

  public void testAssertEmptyWithPlainText() throws Exception {
    jTextComponent.setText("");
    assertTrue(textBox.textIsEmpty());
    jTextComponent.setText("a");
    try {
      assertTrue(textBox.textIsEmpty());
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Text should be empty but contains: a", e.getMessage());
    }
  }

  public void testSetText() throws Exception {
    textBox.setText("new text");
    assertEquals("new text", jTextComponent.getText());
  }

  public void testSetTextChecksThatTheComponentIsEditable() throws Exception {
    textBox.setText("text");
    jTextComponent.setEditable(false);
    try {
      textBox.setText("new text");
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The text box is not editable", e.getMessage());
    }
    assertEquals("text", jTextComponent.getText());
  }

  public void testInsertText() throws Exception {
    jTextComponent.setEditable(true);
    textBox.insertText("text", 0);
    assertEquals("text", textBox.getText());
    textBox.insertText("this is some ", 0);
    assertEquals("this is some text", textBox.getText());
    textBox.insertText("interesting ", 13);
    assertEquals("this is some interesting text", textBox.getText());
    textBox.insertText(", isn't it?", textBox.getText().length());
    assertEquals("this is some interesting text, isn't it?", textBox.getText());
  }

  public void testInsertTextAtABadPosition() throws Exception {
    textBox.setText("text");
    try {
      textBox.insertText("a", 10);
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Position should be between 0 and 4", e.getMessage());
    }
  }

  public void testInsertTextDoesNotNotifyActionListeners() throws Exception {
    JTextField textField = new JTextField();
    DummyActionListener actionListener = new DummyActionListener();
    textField.addActionListener(actionListener);
    textBox = new TextBox(textField);

    textBox.insertText("text", 0);
    assertEquals(0, actionListener.getCallCount());
  }

  public void testInsertTextChecksThatTheComponentIsEditable() throws Exception {
    textBox.setText("text");
    jTextComponent.setEditable(false);
    try {
      textBox.insertText("new text", 0);
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The text box is not editable", e.getMessage());
    }
    assertEquals("text", jTextComponent.getText());
  }

  public void testGetText() throws Exception {
    jTextComponent.setText("some text");
    assertEquals("some text", textBox.getText());
    textBox.setText("new text");
    assertEquals("new text", textBox.getText());
    textBox.setText("new <b>text</b>");
    assertEquals("new <b>text</b>", textBox.getText());
  }

  public void testSetTextNotifiesActionListenersForJTextField() throws Exception {
    JTextField textField = new JTextField();
    DummyActionListener actionListener = new DummyActionListener();
    textField.addActionListener(actionListener);
    textBox = new TextBox(textField);

    textBox.setText("text");
    assertEquals(1, actionListener.getCallCount());
  }

  public void testClickOnHyperlinkIsNotSupported() throws Exception {
    checkAssertionFailedError(new Functor() {
      public void run() throws Exception {
        textBox.clickOnHyperlink("text");
      }
    }, "This component does not support hyperlinks.");
    checkAssertionFailedError(new Functor() {
      public void run() throws Exception {
        textBox.triggerClickOnHyperlink("text").run();
      }
    }, "This component does not support hyperlinks.");
  }

  public void testPressingPrintableKeyAddsItToText() throws Exception {
    JTextField textField = new JTextField();
    TextBox textBox = new TextBox(textField);
    textBox.pressKey(Key.A);
    assertTrue(textBox.textEquals("A"));
    textBox.pressKey(Key.B);
    assertTrue(textBox.textEquals("AB"));
    textBox.pressKey(Key.C);
    assertTrue(textBox.textEquals("ABC"));
  }

  public void testPressingPrintableKeyRejectedByTextField() throws Exception {
    JTextField textField = new JTextField();
    ((AbstractDocument)textField.getDocument()).setDocumentFilter(new DocumentFilter() {
      public void insertString(FilterBypass bypass, int i, String string, AttributeSet set) throws BadLocationException {
        if (!string.equals("A")) {
          super.insertString(bypass, i, string, set);
        }
      }
    });
    TextBox textBox = new TextBox(textField);
    textBox.pressKey(Key.A);
    assertTrue(textBox.textEquals(""));
    textBox.pressKey(Key.B);
    assertTrue(textBox.textEquals("B"));
    textBox.pressKey(Key.A);
    assertTrue(textBox.textEquals("B"));
    textBox.pressKey(Key.C);
    assertTrue(textBox.textEquals("BC"));
  }

  public void testPressingPrintableKeyInANonEmptyTextBoxStartsAtPosition0() throws Exception {
    JTextField textField = new JTextField("text");
    TextBox textBox = new TextBox(textField);
    textBox.pressKey(Key.A);
    assertTrue(textBox.textEquals("Atext"));
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.