org.assertj.swing.fixture.JComboBoxFixture Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of assertj-swing Show documentation
Show all versions of assertj-swing Show documentation
Assertj-Swing test framework
The newest version!
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2018 the original author or authors.
*/
package org.assertj.swing.fixture;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.JComboBox;
import javax.swing.JList;
import org.assertj.swing.cell.JComboBoxCellReader;
import org.assertj.swing.core.Robot;
import org.assertj.swing.driver.JComboBoxDriver;
/**
*
* Supports functional testing of {@code JComboBox}es.
*
*
*
* The conversion between the values given in tests and the values being displayed by a {@code JComboBox} renderer is
* performed by a {@link JComboBoxCellReader}. This fixture uses a {@link JComboBoxCellReader} by default.
*
*
* @author Alex Ruiz
* @author Yvonne Wang
*/
public class JComboBoxFixture extends AbstractJPopupMenuInvokerFixture
implements EditableComponentFixture, ItemGroupFixture {
/**
* Creates a new {@link JComboBoxFixture}.
*
* @param robot performs simulation of user events on the given {@code JComboBox}.
* @param target the {@code JComboBox} to be managed by this fixture.
* @throws NullPointerException if {@code robot} is {@code null}.
* @throws NullPointerException if {@code target} is {@code null}.
*/
public JComboBoxFixture(@NotNull Robot robot, @NotNull JComboBox target) {
super(JComboBoxFixture.class, robot, target);
}
/**
* Creates a new {@link JComboBoxFixture}.
*
* @param robot performs simulation of user events on a {@code JComboBox}.
* @param comboBoxName the name of the {@code JComboBox} to find using the given {@code Robot}.
* @throws NullPointerException if {@code robot} is {@code null}.
* @throws org.assertj.swing.exception.ComponentLookupException if a matching {@code JComboBox} could not be found.
* @throws org.assertj.swing.exception.ComponentLookupException if more than one matching {@code JComboBox} is found.
*/
public JComboBoxFixture(@NotNull Robot robot, @Nullable String comboBoxName) {
super(JComboBoxFixture.class, robot, comboBoxName, JComboBox.class);
}
@Override
@NotNull
protected JComboBoxDriver createDriver(@NotNull Robot robot) {
return new JComboBoxDriver(robot);
}
/**
* @return the {@code String} representation of the elements in this fixture's {@code JComboBox}, using this fixture's
* {@link JComboBoxCellReader}.
* @see #replaceCellReader(JComboBoxCellReader)
*/
@Override
@NotNull
public String[] contents() {
return driver().contentsOf(target());
}
/**
* Clears the selection in this fixture's {@code JComboBox}. Since this method does not simulate user input, it does
* not verifies that this fixture's {@code JComboBox} is enabled and showing.
*
* @return this fixture.
*/
@Override
@NotNull
public JComboBoxFixture clearSelection() {
driver().clearSelection(target());
return this;
}
/**
* Simulates a user selecting an item in this fixture's {@code JComboBox}.
*
* @param index the index of the item to select.
* @return this fixture.
* @throws IllegalStateException if this fixture's {@code JComboBox} is disabled.
* @throws IllegalStateException if this fixture's {@code JComboBox} is not showing on the screen.
* @throws IndexOutOfBoundsException if the given index is negative or greater than the index of the last item in the
* {@code JComboBox}.
*/
@Override
@NotNull
public JComboBoxFixture selectItem(int index) {
driver().selectItem(target(), index);
return this;
}
/**
* Simulates a user selecting an item in this fixture's {@code JComboBox}. The text of the item to select must match
* the given {@code String}. Such text is retrieved by this fixture's {@link JComboBoxCellReader}.
*
* @param text the text of the item to select. It can be a regular expression.
* @return this fixture.
* @throws org.assertj.swing.exception.LocationUnavailableException if an element matching the given text cannot be found.
* @throws IllegalStateException if this fixture's {@code JComboBox} is disabled.
* @throws IllegalStateException if this fixture's {@code JComboBox} is not showing on the screen.
* @see #replaceCellReader(JComboBoxCellReader)
*/
@Override
@NotNull
public JComboBoxFixture selectItem(@Nullable String text) {
driver().selectItem(target(), text);
return this;
}
/**
* Simulates a user selecting an item in this fixture's {@code JComboBox}. The text of the item to select must match
* the given regular expression pattern. Such text is retrieved by this fixture's {@link JComboBoxCellReader}.
*
* @param pattern the regular expression pattern to match.
* @return this fixture.
* @throws org.assertj.swing.exception.LocationUnavailableException if an element matching the given pattern cannot be found.
* @throws IllegalStateException if this fixture's {@code JComboBox} is disabled.
* @throws IllegalStateException if this fixture's {@code JComboBox} is not showing on the screen.
* @throws NullPointerException if the given regular expression pattern is {@code null}.
* @see #replaceCellReader(JComboBoxCellReader)
*/
@Override
@NotNull
public JComboBoxFixture selectItem(@NotNull Pattern pattern) {
driver().selectItem(target(), pattern);
return this;
}
/**
* Returns the {@code String} representation of the value of an item in this fixture's {@code JComboBox}, using this
* fixture's {@link JComboBoxCellReader}.
*
* @param index the index of the item to return.
* @return the {@code String} representation of the value of an item in this fixture's {@code JComboBox}.
* @throws IndexOutOfBoundsException if the given index is negative or greater than the index of the last item in the
* {@code JComboBox}.
* @see #replaceCellReader(JComboBoxCellReader)
*/
@Override
@Nullable
public String valueAt(int index) {
return driver().value(target(), index);
}
/**
* Verifies that the {@code String} representation of the selected item in this fixture's {@code JComboBox} matches
* the given text.
*
* @param value the text to match. It can be a regular expression.
* @return this fixture.
* @throws AssertionError if the selected item does not match the given text.
* @see #replaceCellReader(JComboBoxCellReader)
*/
@Override
@NotNull
public JComboBoxFixture requireSelection(@Nullable String value) {
driver().requireSelection(target(), value);
return this;
}
/**
* Verifies that the {@code String} representation of the selected item in this fixture's {@code JComboBox} matches
* the given regular expression pattern.
*
* @param pattern the regular expression pattern to match.
* @return this fixture.
* @throws NullPointerException if the given regular expression pattern is {@code null}.
* @throws AssertionError if the selected item does not match the given regular expression pattern.
* @see #replaceCellReader(JComboBoxCellReader)
*/
@Override
@NotNull
public JComboBoxFixture requireSelection(@NotNull Pattern pattern) {
driver().requireSelection(target(), pattern);
return this;
}
/**
* Verifies that the index of the selected item in this fixture's {@code JComboBox} is equal to the given value.
*
* @param index the expected selection index.
* @return this fixture.
* @throws AssertionError if the selected index is not equal to the given one.
*/
@Override
@NotNull
public JComboBoxFixture requireSelection(int index) {
driver().requireSelection(target(), index);
return this;
}
/**
* Verifies that this fixture's {@code JComboBox} does not have any selection.
*
* @return this fixture.
* @throws AssertionError if this fixture's {@code JComboBox} has a selection.
*/
@Override
@NotNull
public JComboBoxFixture requireNoSelection() {
driver().requireNoSelection(target());
return this;
}
/**
* Verifies that this fixture's {@code JComboBox} has the expected number of items
*
* @param expected the expected number of items.
* @return this fixture.
* @throws AssertionError if the number of items in this fixture's {@code JComboBox} is not equal to the expected one.
*/
@Override
@NotNull
public JComboBoxFixture requireItemCount(int expected) {
driver().requireItemCount(target(), expected);
return this;
}
/**
* Returns the selected value of this fixture's {@code JComboBox} as plain text. This method returns {@code null} if
* the {code JComboBox} does not have any selection.
*
* @return the selected value of this fixture's {code JComboBox} as plain text, or {@code null} if the {code
* JComboBox} does not have any selection.
*/
@Nullable
public String selectedItem() {
return driver().selectedItemOf(target());
}
/**
* Asserts that this fixture's {@code JComboBox} is editable.
*
* @throws AssertionError if this fixture's {@code JComboBox} is not editable.
* @return this fixture.
*/
@Override
@NotNull
public JComboBoxFixture requireEditable() {
driver().requireEditable(target());
return this;
}
/**
* Asserts that this fixture's {@code JComboBox} is not editable.
*
* @throws AssertionError if this fixture's {@code JComboBox} is editable.
* @return this fixture.
*/
@Override
@NotNull
public JComboBoxFixture requireNotEditable() {
driver().requireNotEditable(target());
return this;
}
/**
* Simulates a user entering the specified text in the {@code JComboBox}, replacing any text. This action is executed
* only if the {@code JComboBox} is editable.
*
* @param text the text to enter.
* @return this fixture.
* @throws IllegalStateException if this fixture's {@code JComboBox} is disabled.
* @throws IllegalStateException if this fixture's {@code JComboBox} is not showing on the screen.
* @throws IllegalStateException if this fixture's {@code JComboBox} is not editable.
*/
@NotNull
public JComboBoxFixture replaceText(@NotNull String text) {
driver().replaceText(target(), text);
return this;
}
/**
* Simulates a user selecting the text in the {@code JComboBox}. This action is executed only if the {@code JComboBox}
* is editable.
*
* @return this fixture.
* @throws IllegalStateException if this fixture's {@code JComboBox} is disabled.
* @throws IllegalStateException if this fixture's {@code JComboBox} is not showing on the screen.
* @throws IllegalStateException if this fixture's {@code JComboBox} is not editable.
*/
@NotNull
public JComboBoxFixture selectAllText() {
driver().selectAllText(target());
return this;
}
/**
* Simulates a user entering the specified text in this fixture's {@code JComboBox}. This action is executed only if
* the {@code JComboBox} is editable.
*
* @param text the text to enter.
* @return this fixture.
* @throws IllegalStateException if this fixture's {@code JComboBox} is disabled.
* @throws IllegalStateException if this fixture's {@code JComboBox} is not showing on the screen.
* @throws IllegalStateException if this fixture's {@code JComboBox} is not editable.
* @throws org.assertj.swing.exception.ActionFailedException if this fixture's {@code JComboBox} does not have an editor.
*/
@NotNull
public JComboBoxFixture enterText(@NotNull String text) {
driver().enterText(target(), text);
return this;
}
/**
* Finds and returns the {@code JList} in the pop-up raised by this fixture's {@code JComboBox}.
*
* @return the {@code JList} in the pop-up raised by this fixture's {@code JComboBox}.
* @throws org.assertj.swing.exception.ComponentLookupException if the {@code JList} in the pop-up could not be found.
*/
@NotNull
public JList list() {
return driver().dropDownList();
}
public void replaceCellReader(@NotNull JComboBoxCellReader cellReader) {
driver().replaceCellReader(cellReader);
}
}