]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix initially disabled items in RadioButtonGroup (#10719)
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>
Mon, 19 Mar 2018 16:50:17 +0000 (18:50 +0200)
committerIlia Motornyi <elmot@vaadin.com>
Mon, 19 Mar 2018 16:50:17 +0000 (18:50 +0200)
client/src/main/java/com/vaadin/client/ui/VRadioButtonGroup.java
client/src/main/java/com/vaadin/client/ui/optiongroup/RadioButtonGroupConnector.java
testbench-api/src/main/java/com/vaadin/testbench/elements/RadioButtonGroupElement.java
uitest/src/main/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupItemDisabled.java [new file with mode: 0644]
uitest/src/main/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupItemDisabled.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupItemDisabledTest.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupItemDisabledTest.java [new file with mode: 0644]

index 479e41a9b5b7539c71284bfdbb04750348c24143..30335fd975b991509729926fc9b0f14beb8d0209 100644 (file)
@@ -139,6 +139,13 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite
 
     private void updateItem(RadioButton button, JsonObject item,
             boolean requireInitialization) {
+        if (requireInitialization) {
+            getWidget().add(button);
+            button.setStyleName("v-radiobutton");
+            button.addStyleName(CLASSNAME_OPTION);
+            button.addClickHandler(this);
+        }
+
         String itemHtml = item
                 .getString(ListingJsonConstants.JSONKEY_ITEM_VALUE);
         if (!isHtmlContentAllowed()) {
@@ -154,22 +161,12 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite
         button.setHTML(itemHtml);
         boolean optionEnabled = !item
                 .getBoolean(ListingJsonConstants.JSONKEY_ITEM_DISABLED);
-        boolean enabled = optionEnabled && !isReadonly() && isEnabled();
-        button.setEnabled(enabled);
-        // #9258 apply the v-disabled class when disabled for UX
-        button.setStyleName(StyleConstants.DISABLED,
-                !isEnabled() || !optionEnabled);
+        updateItemEnabled(button, optionEnabled);
         updateItemSelection(button,
                 item.getBoolean(ListingJsonConstants.JSONKEY_ITEM_SELECTED));
 
         String key = item.getString(DataCommunicatorConstants.KEY);
 
-        if (requireInitialization) {
-            getWidget().add(button);
-            button.setStyleName("v-radiobutton");
-            button.addStyleName(CLASSNAME_OPTION);
-            button.addClickHandler(this);
-        }
         optionsToItems.put(button, item);
         keyToOptions.put(key, button);
     }
@@ -205,7 +202,6 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite
     }
 
     protected void updateEnabledState() {
-        boolean radioButtonEnabled = isEnabled() && !isReadonly();
         // sets options enabled according to the widget's enabled,
         // readonly and each options own enabled
         for (Map.Entry<RadioButton, JsonObject> entry : optionsToItems
@@ -214,10 +210,7 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite
             JsonObject value = entry.getValue();
             boolean optionEnabled = !value
                     .getBoolean(ListingJsonConstants.JSONKEY_ITEM_DISABLED);
-            radioButton.setEnabled(radioButtonEnabled && optionEnabled);
-            // #9258 apply the v-disabled class when disabled for UX
-            radioButton.setStyleName(StyleConstants.DISABLED,
-                    !isEnabled() || !optionEnabled);
+            updateItemEnabled(radioButton, optionEnabled);
         }
     }
 
@@ -305,6 +298,23 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite
     protected void updateItemSelection(RadioButton radioButton, boolean value) {
         radioButton.setValue(value);
         radioButton.setStyleName(CLASSNAME_OPTION_SELECTED, value);
+    }
 
+    /**
+     * Updates the enabled state of a radio button.
+     *
+     * @param radioButton
+     *            the radio button to update
+     * @param value
+     *            {@code true} if enabled; {@code false} if not
+     *
+     * @since
+     */
+    protected void updateItemEnabled(RadioButton radioButton, boolean value) {
+        boolean enabled = value && !isReadonly() && isEnabled();
+        radioButton.setEnabled(enabled);
+        // #9258 apply the v-disabled class when disabled for UX
+        boolean hasDisabledStyle = !isEnabled() || !value;
+        radioButton.setStyleName(StyleConstants.DISABLED, hasDisabledStyle);
     }
 }
index ca008d0087bdbf6fab337dc0d11998f562fa495d..0af2d1f487442dcad2f01cc852d3b3e7e82a7e19 100644 (file)
@@ -85,7 +85,6 @@ public class RadioButtonGroupConnector
     }
 
     @OnStateChange("readOnly")
-    @SuppressWarnings("deprecation")
     void updateWidgetReadOnly() {
         getWidget().setEnabled(isEnabled() && !isReadOnly());
     }
index 30952c1e008b8ed483a31f9ef01a671b391dff76..67df824fbd9d937b08be7d7b55f7af6b637562be 100644 (file)
@@ -41,6 +41,16 @@ public class RadioButtonGroupElement extends AbstractSingleSelectElement {
         return optionTexts;
     }
 
+    /**
+     * Gets the list of option elements for this check box group.
+     *
+     * @return list of option elements
+     * @since
+     */
+    public List<WebElement> getOptionElements() {
+        return findElements(bySelectOption);
+    }
+
     public void selectByText(String text) throws ReadOnlyException {
         if (isReadOnly()) {
             throw new ReadOnlyException();
diff --git a/uitest/src/main/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupItemDisabled.java b/uitest/src/main/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupItemDisabled.java
new file mode 100644 (file)
index 0000000..d929b43
--- /dev/null
@@ -0,0 +1,21 @@
+package com.vaadin.tests.components.checkboxgroup;
+
+import com.vaadin.annotations.Widgetset;
+import com.vaadin.server.SerializablePredicate;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.CheckBoxGroup;
+
+@Widgetset("com.vaadin.DefaultWidgetSet")
+public class CheckBoxGroupItemDisabled extends AbstractTestUI {
+
+    public static final SerializablePredicate<Integer> ENABLED_PROVIDER = i -> i != 3;
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        CheckBoxGroup<Integer> cbg = new CheckBoxGroup<>();
+        cbg.setItems(1, 2, 3, 4);
+        cbg.setItemEnabledProvider(ENABLED_PROVIDER);
+        addComponent(cbg);
+    }
+}
diff --git a/uitest/src/main/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupItemDisabled.java b/uitest/src/main/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupItemDisabled.java
new file mode 100644 (file)
index 0000000..5a3a467
--- /dev/null
@@ -0,0 +1,21 @@
+package com.vaadin.tests.components.radiobuttongroup;
+
+import com.vaadin.annotations.Widgetset;
+import com.vaadin.server.SerializablePredicate;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.RadioButtonGroup;
+
+@Widgetset("com.vaadin.DefaultWidgetSet")
+public class RadioButtonGroupItemDisabled extends AbstractTestUI {
+
+    public static final SerializablePredicate<Integer> ENABLED_PROVIDER = i -> i != 3;
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        RadioButtonGroup<Integer> rbg = new RadioButtonGroup<>();
+        rbg.setItems(1, 2, 3, 4);
+        rbg.setItemEnabledProvider(ENABLED_PROVIDER);
+        addComponent(rbg);
+    }
+}
\ No newline at end of file
diff --git a/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupItemDisabledTest.java b/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupItemDisabledTest.java
new file mode 100644 (file)
index 0000000..979b8de
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2000-2016 Vaadin Ltd.
+ *
+ * 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.
+ */
+package com.vaadin.tests.components.checkboxgroup;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.testbench.elements.CheckBoxGroupElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class CheckBoxGroupItemDisabledTest extends MultiBrowserTest {
+
+    @Test
+    public void itemDisabledOnInit() {
+        openTestURL();
+        List<WebElement> options = $(CheckBoxGroupElement.class).first()
+                .getOptionElements();
+        options.stream().forEach(option -> {
+            Integer value = Integer.parseInt(option.getText());
+            boolean disabled = !CheckBoxGroupItemDisabled.ENABLED_PROVIDER
+                    .test(value);
+            assertEquals(
+                    "Unexpected status of v-disabled stylename for item "
+                            + value,
+                    disabled,
+                    option.getAttribute("class").contains("v-disabled"));
+        });
+    }
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupItemDisabledTest.java b/uitest/src/test/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupItemDisabledTest.java
new file mode 100644 (file)
index 0000000..a56662f
--- /dev/null
@@ -0,0 +1,31 @@
+package com.vaadin.tests.components.radiobuttongroup;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.testbench.elements.RadioButtonGroupElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class RadioButtonGroupItemDisabledTest extends MultiBrowserTest {
+
+    @Test
+    public void itemDisabledOnInit() {
+        openTestURL();
+        List<WebElement> options = $(RadioButtonGroupElement.class).first()
+                .getOptionElements();
+        options.stream().forEach(option -> {
+            Integer value = Integer.parseInt(option.getText());
+            boolean disabled = !RadioButtonGroupItemDisabled.ENABLED_PROVIDER
+                    .test(value);
+            assertEquals(
+                    "Unexpected status of v-disabled stylename for item "
+                            + value,
+                    disabled,
+                    option.getAttribute("class").contains("v-disabled"));
+        });
+    }
+}