|
| 1 | +/* |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.component.customfield; |
| 17 | + |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import com.vaadin.flow.dom.DomEvent; |
| 23 | + |
| 24 | +import elemental.json.Json; |
| 25 | + |
| 26 | +public class ManualValueUpdateTest { |
| 27 | + |
| 28 | + private IncrementingCustomField automaticField; |
| 29 | + private IncrementingCustomField manualField; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setup() { |
| 33 | + automaticField = new IncrementingCustomField(false); |
| 34 | + manualField = new IncrementingCustomField(true); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void automaticValueUpdate_changeEventUpdatesValue() { |
| 39 | + Assert.assertEquals(Integer.valueOf(0), automaticField.getValue()); |
| 40 | + |
| 41 | + fireChangeEvent(automaticField); |
| 42 | + Assert.assertEquals(Integer.valueOf(1), automaticField.getValue()); |
| 43 | + |
| 44 | + fireChangeEvent(automaticField); |
| 45 | + Assert.assertEquals(Integer.valueOf(2), automaticField.getValue()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void manualValueUpdate_changeEventDoesNotUpdateValue() { |
| 50 | + Assert.assertEquals(Integer.valueOf(0), manualField.getValue()); |
| 51 | + |
| 52 | + fireChangeEvent(manualField); |
| 53 | + Assert.assertEquals(Integer.valueOf(0), manualField.getValue()); |
| 54 | + |
| 55 | + fireChangeEvent(manualField); |
| 56 | + Assert.assertEquals(Integer.valueOf(0), manualField.getValue()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void manualValueUpdate_manuallyUpdateValue() { |
| 61 | + Assert.assertEquals(Integer.valueOf(0), manualField.getValue()); |
| 62 | + |
| 63 | + manualField.updateValue(); |
| 64 | + Assert.assertEquals(Integer.valueOf(1), manualField.getValue()); |
| 65 | + |
| 66 | + manualField.updateValue(); |
| 67 | + Assert.assertEquals(Integer.valueOf(2), manualField.getValue()); |
| 68 | + } |
| 69 | + |
| 70 | + private void fireChangeEvent(CustomField<?> field) { |
| 71 | + DomEvent changeEvent = new DomEvent(field.getElement(), "change", |
| 72 | + Json.createObject()); |
| 73 | + field.getElement().getNode().getFeature( |
| 74 | + com.vaadin.flow.internal.nodefeature.ElementListenerMap.class) |
| 75 | + .fireEvent(changeEvent); |
| 76 | + } |
| 77 | + |
| 78 | + private static class IncrementingCustomField extends CustomField<Integer> { |
| 79 | + private int counter = 0; |
| 80 | + |
| 81 | + public IncrementingCustomField(boolean manualValueUpdate) { |
| 82 | + super(0, manualValueUpdate); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected Integer generateModelValue() { |
| 87 | + return ++counter; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected void setPresentationValue(Integer newPresentationValue) { |
| 92 | + // Not needed for this test |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments