Package org.loadui.testfx.controls

Source Code of org.loadui.testfx.controls.ListViews$ListContainsMatcher

/*
* Copyright 2013-2014 SmartBear Software
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by the European
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work
* except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the Licence for the specific language governing permissions
* and limitations under the Licence.
*/
package org.loadui.testfx.controls;

import javafx.scene.Node;
import javafx.scene.control.ListView;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.loadui.testfx.exceptions.NoNodesFoundException;

import static org.loadui.testfx.GuiTest.find;

public class ListViews {

    /**
     * Get the number of rows in the list.
     *
     * @param list
     * @return number of rows in list
     */
    public static int numberOfRowsIn(ListView<?> list) {
        return list.getItems().size();
    }

    /**
     * Get the number of rows in the list that matches the CSS query.
     *
     * @param listQuery
     * @return
     */
    public static int numberOfRowsIn(String listQuery) {
        ListView<?> table = find(listQuery);
        return table.getItems().size();
    }

    @SuppressWarnings("unchecked")
    @Factory
    public static <S> org.hamcrest.Matcher<S> containsRow(Object rowValue) {
        return new ListContainsMatcher(rowValue);
    }

    static boolean containsRow(ListView<?> list, Object rowValue) {
      return list.getItems()
          .stream()
          .anyMatch(rowData -> rowValue.equals(rowData) || rowValue.equals(rowData.toString()));
    }

    static ListView<?> getListView(String listSelector) {
        Node node = find(listSelector);
        if (!(node instanceof ListView)) {
            throw new NoNodesFoundException(listSelector + " selected " + node +
                " which is not a ListView!");
        }
        return (ListView<?>) node;
    }

    @SuppressWarnings("rawtypes")
    private static class ListContainsMatcher extends BaseMatcher {
        private Object valueToMatch;

        public ListContainsMatcher(Object valueToMatch) {
            this.valueToMatch = valueToMatch;
        }

        @Override
        public boolean matches(Object o) {
            if (o instanceof String) {
                String query = (String) o;
                return ListViews.containsRow(getListView(query), valueToMatch);
            }
            else if (o instanceof ListView) {
                ListView tableView = (ListView) o;
                return ListViews.containsRow(tableView, valueToMatch);
            }
            return false;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("The list does not contain a row with value '" +
                valueToMatch + "'");
        }
    }

}
TOP

Related Classes of org.loadui.testfx.controls.ListViews$ListContainsMatcher

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.