package gwt.material.design.client.ui;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.HasEnabled;
import gwt.material.design.client.base.MaterialWidget;
import gwt.material.design.client.constants.CssName;
import gwt.material.design.client.ui.base.MaterialWidgetTest;
import gwt.material.design.client.ui.dto.User;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class MaterialListValueBoxTest extends MaterialWidgetTest {

    @Test
    public void testListValueBox() {
        MaterialListValueBox<User> valueBox = new MaterialListValueBox<>();
        checkWidget(valueBox);
        checkValues(valueBox);
        checkProperties(valueBox);
    }

    public <T extends MaterialListValueBox<User>> void checkValues(T widget) {
        List<User> users = new ArrayList<>();
        // Add Item test
        for (int i = 1; i <= 5; i++) {
            User user = new User("user" + i, "User " + i);
            users.add(user);
            widget.addItem(user);
        }

        for (int i = 0; i <=4; i++) {
            assertNotNull(widget.getValue(i));
        }

        // Set selected item test
        widget.setSelectedIndex(0);
        assertEquals(widget.getSelectedIndex(), 0);
        assertNotNull(widget.getSelectedValue());
        assertEquals(users.get(0), widget.getSelectedValue());

        // Remove item test
        widget.removeItem(0);
        users.remove(0);
        assertEquals(widget.getItemCount(), users.size());
        assertEquals(users.get(0), widget.getSelectedValue());

        // Insert item test
        User inserted = new User("inserted", "Inserted");
        widget.insertItem(inserted, 1);
        assertNotNull(inserted);
        assertEquals(widget.getValue(1), inserted);

        // Clear test
        widget.clear();
        assertNull(widget.getSelectedValue());
        assertEquals(widget.getItemCount(), 0);
    }

    public <T extends MaterialListValueBox<User>> void checkProperties(T widget) {
        widget.setOld(true);
        assertTrue(widget.isOld());
        widget.setOld(false);
        assertFalse(widget.isOld());
        widget.setMultipleSelect(true);
        assertTrue(widget.isMultipleSelect());
    }

    @Override
    public <T extends MaterialWidget & HasEnabled> void checkEnabled(T widget) {
        assertTrue(widget instanceof MaterialListValueBox);
        final Element element = ((MaterialListValueBox<User>)widget).getListBox().getElement();
        assertFalse(element.hasAttribute(CssName.DISABLED));
        widget.setEnabled(true);
        assertFalse(element.hasAttribute(CssName.DISABLED));
        assertEquals(widget.isEnabled(), true);
        widget.setEnabled(false);
        assertTrue(element.hasAttribute(CssName.DISABLED));
    }
}

@@ -701,24 +701,119 @@
 (valueBox);%0A
+        MaterialListBox listBox = new MaterialListBox();%0A        checkValueAsListBox(listBox);%0A
     %7D%0A%0A    p
@@ -2019,32 +2019,1169 @@
 t(), 0);%0A    %7D%0A%0A
+    public %3CT extends MaterialListValueBox%3CString%3E%3E void checkValueAsListBox(T widget) %7B%0A        List%3CString%3E users = new ArrayList%3C%3E();%0A        // Add Item test%0A        for (int i = 1; i %3C= 5; i++) %7B%0A            users.add(%22Item%22 + i);%0A        %7D%0A%0A        for (int i = 0; i %3C=4; i++) %7B%0A            assertNotNull(widget.getValue(i));%0A        %7D%0A%0A        // Set selected item test%0A        widget.setSelectedIndex(0);%0A        assertEquals(widget.getSelectedIndex(), 0);%0A        assertNotNull(widget.getSelectedValue());%0A        assertEquals(users.get(0), widget.getSelectedValue());%0A%0A        // Remove item test%0A        widget.removeItem(0);%0A        users.remove(0);%0A        assertEquals(widget.getItemCount(), users.size());%0A        assertEquals(users.get(0), widget.getSelectedValue());%0A%0A        // Insert item test%0A        String inserted = new String(%22inserted%22);%0A        widget.insertItem(inserted, 1);%0A        assertNotNull(inserted);%0A        assertEquals(widget.getValue(1), inserted);%0A%0A        // Clear test%0A        widget.clear();%0A        assertNull(widget.getSelectedValue());%0A        assertEquals(widget.getItemCount(), 0);%0A    %7D%0A%0A
     public %3CT ex

Test - ListBox as String