All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.gwt.test.assertions.BaseListBoxAssert Maven / Gradle / Ivy

There is a newer version: 0.63
Show newest version
package com.googlecode.gwt.test.assertions;

import com.google.gwt.user.client.ui.ListBox;
import com.googlecode.gwt.test.utils.WidgetUtils;

import static org.assertj.core.util.Objects.areEqual;

/**
 * Base class for {@link ListBox} assertions.
 *
 * @param  used to simulate "self types." For more information please read "Emulating 'self types' using Java Generics to simplify fluent API
 *            implementation."
 * @param  the type of the "actual" value.
 * @author Gael Lazzari
 */
public class BaseListBoxAssert, A extends ListBox> extends
        BaseFocusWidgetAssert {

    /**
     * Creates a new {@link BaseListBoxAssert}.
     *
     * @param actual   the actual value to verify.
     * @param selfType the "self type."
     */
    protected BaseListBoxAssert(A actual, Class selfType) {
        super(actual, selfType);
    }

    /**
     * Verifies that the actual {@link ListBox} text content exactly matches the supplied one.
     *
     * @param expected the expected content to compare the actual content to.
     * @return this assertion object.
     */
    public S dataMatches(String... expected) {
        int contentSize = expected.length;
        if (contentSize != actual.getItemCount()) {
            failWithMessage("does not match actual listbox's content: [%s]",
                    WidgetUtils.getListBoxContentToString(actual));
        }
        for (int i = 0; i < contentSize; i++) {
            if (!expected[i].equals(actual.getItemText(i))) {
                failWithMessage("does not match actual listbox's content: [%s]",
                        WidgetUtils.getListBoxContentToString(actual));
            }
        }

        return myself;
    }

    /**
     * Verifies that the actual {@link ListBox} selected value is equal to the given one.
     *
     * @param expected the given selected value to compare the actual selected value to.
     * @return this assertion object.
     * @throws AssertionError if the actual selected value is null or not equal to the given one.
     */
    public S selectedValueEquals(String expected) {
        int selectedIndex = actual.getSelectedIndex();
        if (selectedIndex == -1) {
            failWithMessage("listbox does not have a selected value");
        }

        String selected = actual.getItemText(selectedIndex);
        if (areEqual(selected, expected)) {
            return myself;
        } else {
            throw propertyComparisonFailed("selected value", selected, expected);
        }
    }

}