⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 panelcomponentsearchtest.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
  public void testComponentsAreFoundDeepInTheComponentsHierarchy() throws Exception {
    JPanel newPanel = new JPanel();
    JButton button = new JButton("button");
    newPanel.add(button);
    jPanel.add(newPanel);
    TestUtils.assertUIComponentRefersTo(button, panel.getButton("button"));
  }

  public void testContainmentSkipsScrollpaneButtons() throws Exception {
    JPanel rootPanel = new JPanel();
    rootPanel.setName("rootPanel");
    JPanel innerUnnamedPanel = new JPanel();
    rootPanel.add(innerUnnamedPanel);
    JScrollPane scroll = new JScrollPane();
    innerUnnamedPanel.add(scroll);
    XmlAssert.assertEquivalent("<panel name='rootPanel'/>",
                               new Panel(rootPanel).getDescription());
  }

  public void testContainmentSkipsUnnamedContainers() throws Exception {
    JPanel rootPanel = new JPanel();
    rootPanel.setName("rootPanel");
    JPanel innerUnnamedPanel = new JPanel();
    rootPanel.add(innerUnnamedPanel);
    JScrollPane scroll = new JScrollPane();
    innerUnnamedPanel.add(scroll);
    JButton button = new JButton("ok");
    JPanel viewportPanel = new JPanel();
    viewportPanel.add(button);
    scroll.getViewport().add(viewportPanel);
    XmlAssert.assertEquivalent("<panel name='rootPanel'>" +
                               "  <button label='ok'/>" +
                               "</panel>",
                               new Panel(rootPanel).getDescription());
  }

  public void testSelectedPanelIsTheOneVisibleInCardLayout() throws Exception {
    JPanel cardPanel = new JPanel();
    JPanel firstPanel = new JPanel();
    firstPanel.add(new JList());
    JButton firstButton = new JButton("myButton");
    firstPanel.add(firstButton);

    JPanel secondPanel = new JPanel();
    JButton secondButton = new JButton("myButton");
    secondPanel.add(secondButton);

    CardLayout cardLayout = new CardLayout();
    cardPanel.setLayout(cardLayout);
    cardPanel.add(firstPanel, "first");
    cardPanel.add(secondPanel, "second");
    addComponentToPanelWithName(cardPanel, "panelWithCardLayout");

    Panel panelWithCardLayoutChecker = panel.getPanel("panelWithCardLayout");

    cardLayout.show(cardPanel, "first");
    ListBox list1Checker = panelWithCardLayoutChecker.getListBox();
    assertNotNull(list1Checker);
    assertNotNull(panelWithCardLayoutChecker.getButton("myButton"));
    XmlAssert.assertEquivalent("<panel name='panelWithCardLayout'>" +
                               "  <listBox/>" +
                               "  <button label='myButton'/>" +
                               "</panel>",
                               panelWithCardLayoutChecker.getDescription());

    cardLayout.show(cardPanel, "second");
    try {
      panelWithCardLayoutChecker.getListBox();
      fail();
    }
    catch (ItemNotFoundException e) {
    }
    assertNotNull(panelWithCardLayoutChecker.getButton("myButton"));
    XmlAssert.assertEquivalent("<panel name='panelWithCardLayout'>" +
                               "  <button label='myButton'/>" +
                               "</panel>",
                               panelWithCardLayoutChecker.getDescription());
  }

  public void testAmbiguityMessageContents() throws Exception {
    JButton apply = new JButton("apply");
    JButton noApply = new JButton(" don't apply");
    Component[] components = new Component[]{
      apply, noApply
    };
    StringBuffer message = new StringBuffer();
    message.append("Several components are of type '")
      .append(Button.TYPE_NAME)
      .append("' in this panel: [");
    for (int i = 0; i < components.length; i++) {
      Component component = components[i];
      String displayedName = ComponentUtils.getDisplayedName(component);
      message.append((displayedName == null) ? component.getName() : displayedName);
      message.append((i < components.length - 1) ? ',' : ']');
    }
    assertEquals("Several components are of type 'button' in this panel: [apply, don't apply]",
                 message.toString());
  }

  public void testGetInputTextBoxExcludesJLabels() throws Exception {
    JLabel label = new JLabel("label");
    label.setName("name");
    JTextField textField = new JTextField("textField");
    textField.setName("name");
    jPanel.add(label);
    jPanel.add(textField);

    panel.getInputTextBox("name").textEquals("textField");
    panel.getInputTextBox().textEquals("textField");
  }

  public void testGetPanelSearchesForSpecificClasses() throws Exception {
    checkGetPanel(new JPanel());
    checkGetPanel(new JInternalFrame());
  }

  private void checkGetPanel(Container innerPanel) throws Exception {
    jPanel.removeAll();
    JTextField textField = new JTextField();
    textField.setName("innerText");
    jPanel.add(textField);
    innerPanel.setName("innerPanel");
    jPanel.add(innerPanel);
    assertNotNull(panel.getPanel("inner"));
  }

  private void addComponentToPanelWithName(Component component, String name) {
    component.setName(name);
    jPanel.add(component);
  }

  private void createTwoComponentsWithSameName(Class component, String componentName) throws Exception {
    addComponentToPanelWithName(((JComponent)component.newInstance()), componentName);
    addComponentToPanelWithName(((JComponent)component.newInstance()), componentName);
  }

  private void checkComponentNotFound(String uiComponentType) throws Exception {
    try {
      getAccessor(uiComponentType).getComponent();
      fail();
    }
    catch (ItemNotFoundException e) {
      assertEquals(Messages.computeNotFoundMessage(uiComponentType, null, null),
                   e.getMessage());
    }

    String componentName = "unknown";
    try {
      getAccessor(uiComponentType).getComponent(componentName);
      fail();
    }
    catch (ItemNotFoundException e) {
      assertEquals(Messages.computeNotFoundMessage(uiComponentType, componentName, null),
                   e.getMessage());
    }

    try {
      getAccessor(uiComponentType).getComponent(new ComponentMatcher() {
        public boolean matches(Component component) {
          return false;
        }
      });
      fail();
    }
    catch (ItemNotFoundException e) {
      assertEquals(Messages.computeNotFoundMessage(null, null, null),
                   e.getMessage());
    }
  }

  private void checkComponentTypeMismatch(String componentName,
                                          Class actualComponentType,
                                          String uiComponentType) throws Exception {
    addComponentToPanelWithName((Component)actualComponentType.newInstance(), componentName);
    try {
      getAccessor(uiComponentType).getComponent(componentName);
      fail();
    }
    catch (ItemNotFoundException e) {
      assertEquals(Messages.computeNotFoundMessage(uiComponentType, componentName, null),
                   e.getMessage());
    }
  }

  private void checkComponentNameAmbiguity(String componentName, String uiComponentType, String[] candidates) throws Exception {
    try {
      getAccessor(uiComponentType).getComponent(componentName);
      fail();
    }
    catch (ComponentAmbiguityException e) {
      assertEquals(Messages.computeAmbiguityMessage(candidates, uiComponentType, componentName),
                   e.getMessage());
    }
  }

  private void checkComponentUnicityAmbiguity(Class componentClass, String uiComponentType, String componentName)
    throws Exception {
    Component component1 = createSwingInstance(componentClass);
    addComponentToPanelWithName(component1, componentName + "1");
    Component component2 = createSwingInstance(componentClass);
    addComponentToPanelWithName(component2, componentName + "2");
    try {
      getAccessor(uiComponentType).getComponent();
      fail();
    }
    catch (ComponentAmbiguityException e) {
      Component[] components = new Component[]{component1, component2};
      String[] names = new String[components.length];
      for (int i = 0; i < components.length; i++) {
        Component component = components[i];
        String displayedName = ComponentUtils.getDisplayedName(component);
        names[i] = (displayedName == null || displayedName.length() == 0) ? component.getName() : displayedName;
      }
      assertEquals(Messages.computeAmbiguityMessage(names, uiComponentType, null),
                   e.getMessage());
    }
  }

  private void checkGetComponentWithText(Class componentClass,
                                         String uiComponentType) throws Exception {
    String name = "wholename";
    String shortName = "lena";
    Component component = createComponentWithText(componentClass, name);
    jPanel.add(component);
    checkGetComponent(component, getAccessor(uiComponentType), name, shortName);
  }

  private void checkGetComponentWithName(Class componentClass,
                                         String uiComponentType) throws Exception {
    String componentName = componentClass.getName() + "#componentName";
    String shortName = componentClass.getName() + "#c";
    Component component = createSwingInstance(componentClass);
    addComponentToPanelWithName(component, componentName);
    checkGetComponent(component, getAccessor(uiComponentType), componentName, shortName);
  }

  private Component createSwingInstance(Class componentClass) throws InstantiationException, IllegalAccessException {
    if (componentClass.equals(JTextComponent.class)) {
      return new JTextField();
    }
    return (Component)componentClass.newInstance();
  }

  private void checkGetComponentWithClassAndName(Class uiComponentClass) throws Exception {
    String typeName = UIComponentAnalyzer.getTypeName(uiComponentClass);
    Class[] swingClasses = UIComponentAnalyzer.getSwingClasses(uiComponentClass);
    for (int i = 0; i < swingClasses.length; i++) {
      checkGetComponentWithName(swingClasses[i], typeName);
    }
  }

  private void checkGetComponent(Component expectedComponent, TypedComponentAccessor accessor, String name, String shortName)
    throws Exception {
    TestUtils.assertUIComponentRefersTo(expectedComponent, accessor.getComponent(name));
    TestUtils.assertUIComponentRefersTo(expectedComponent, accessor.getComponent(name.toUpperCase()));
    TestUtils.assertUIComponentRefersTo(expectedComponent, accessor.getComponent(name.toLowerCase()));
    TestUtils.assertUIComponentRefersTo(expectedComponent, accessor.getComponent(shortName));
    TestUtils.assertUIComponentRefersTo(expectedComponent, accessor.getComponent(shortName.toUpperCase()));
    TestUtils.assertUIComponentRefersTo(expectedComponent, accessor.getComponent(shortName.toLowerCase()));
    try {
      accessor.getComponent("toto");
      fail();
    }
    catch (Throwable e) {
    }
  }

  private void checkGetComponentWithClass(Class uiComponentClass) throws Exception {
    Class[] swingClasses = UIComponentAnalyzer.getSwingClasses(uiComponentClass);
    String uiComponentType = UIComponentAnalyzer.getTypeName(uiComponentClass);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -