Skip to content

Commit

Permalink
Remove dependency from data to html-components (#5050)
Browse files Browse the repository at this point in the history
Fixes #5025
  • Loading branch information
pleku authored and caalador committed Feb 15, 2019
1 parent acf7177 commit 7fc26b3
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 45 deletions.
5 changes: 0 additions & 5 deletions flow-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
<artifactId>validation-api</artifactId>
<version>${javax.validation.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>flow-html-components</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
import java.util.Optional;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.data.provider.AbstractComponentDataGenerator;
import com.vaadin.flow.data.provider.DataGenerator;
import com.vaadin.flow.data.provider.DataKeyMapper;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.ElementFactory;
import com.vaadin.flow.function.ValueProvider;

import elemental.json.JsonObject;
Expand Down Expand Up @@ -157,7 +158,9 @@ protected String getTemplateForProperty(String property,

@Override
public Component createComponent(SOURCE item) {
return new Span(getFormattedValue(valueProvider.apply(item)));
Element span = ElementFactory
.createSpan(getFormattedValue(valueProvider.apply(item)));
return ComponentUtil.componentFromElement(span, Component.class, true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.util.List;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.ElementFactory;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.shared.Registration;

Expand Down Expand Up @@ -135,10 +137,12 @@ protected String getTemplateForProperty(String property,

@Override
public Component createComponent(SOURCE item) {
NativeButton button = new NativeButton(getValueProvider().apply(item));
button.addClickListener(event -> getItemClickListeners()
.forEach(listener -> listener.onItemClicked(item)));
return button;
Element button = ElementFactory
.createButton(getValueProvider().apply(item));
button.addEventListener("click", event -> getItemClickListeners()
.forEach(listeners -> listeners.onItemClicked(item)));
return ComponentUtil.componentFromElement(button, Component.class,
true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import com.vaadin.flow.data.binder.testcomponents.TestLabel;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.component.HasValidation;
import com.vaadin.flow.component.HasValue;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.data.binder.Binder.Binding;
import com.vaadin.flow.data.binder.Binder.BindingBuilder;
import com.vaadin.flow.data.binder.testcomponents.TestTextField;
Expand Down Expand Up @@ -657,7 +657,7 @@ public void binderBindAndLoad_clearsErrors() {
assertNotNull(lastNameField.getErrorMessage());

// add status label to show bean level error
Label statusLabel = new Label();
TestLabel statusLabel = new TestLabel();
binder.setStatusLabel(statusLabel);
nameField.setValue("error");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import com.vaadin.flow.data.binder.testcomponents.TestLabel;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.component.HasValue;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.data.binder.Binder.Binding;
import com.vaadin.flow.data.binder.Binder.BindingBuilder;
import com.vaadin.flow.data.binder.BindingValidationStatus.Status;
Expand Down Expand Up @@ -101,7 +101,7 @@ public void bindingWithStatusHandler_handlerGetsEvents() {

@Test
public void bindingWithStatusLabel_labelIsUpdatedAccordingStatus() {
Label label = new Label();
TestLabel label = new TestLabel();

Binding<Person, String> binding = binder.forField(nameField)
.withValidator(notEmpty).withStatusLabel(label)
Expand All @@ -128,7 +128,7 @@ public void bindingWithStatusLabel_labelIsUpdatedAccordingStatus() {

@Test
public void bindingWithStatusLabel_defaultStatusHandlerIsReplaced() {
Label label = new Label();
TestLabel label = new TestLabel();

Binding<Person, String> binding = binder.forField(nameField)
.withValidator(notEmpty).withStatusLabel(label)
Expand Down Expand Up @@ -157,7 +157,7 @@ public void bindingWithStatusHandler_addAfterBound() {

@Test(expected = IllegalStateException.class)
public void bindingWithStatusLabel_addAfterBound() {
Label label = new Label();
TestLabel label = new TestLabel();

BindingBuilder<Person, String> binding = binder.forField(nameField)
.withValidator(notEmpty);
Expand All @@ -168,7 +168,7 @@ public void bindingWithStatusLabel_addAfterBound() {

@Test(expected = IllegalStateException.class)
public void bindingWithStatusLabel_setAfterHandler() {
Label label = new Label();
TestLabel label = new TestLabel();

BindingBuilder<Person, String> binding = binder.forField(nameField);

Expand All @@ -179,7 +179,7 @@ public void bindingWithStatusLabel_setAfterHandler() {

@Test(expected = IllegalStateException.class)
public void bindingWithStatusHandler_setAfterLabel() {
Label label = new Label();
TestLabel label = new TestLabel();

BindingBuilder<Person, String> binding = binder.forField(nameField);

Expand Down Expand Up @@ -411,7 +411,7 @@ public void binderWithStatusHandler_addAfterBound() {

@Test(expected = IllegalStateException.class)
public void binderWithStatusLabel_addAfterBound() {
Label label = new Label();
TestLabel label = new TestLabel();

BindingBuilder<Person, String> binding = binder.forField(nameField)
.withValidator(notEmpty);
Expand All @@ -422,7 +422,7 @@ public void binderWithStatusLabel_addAfterBound() {

@Test(expected = IllegalStateException.class)
public void binderWithStatusLabel_setAfterHandler() {
Label label = new Label();
TestLabel label = new TestLabel();

BindingBuilder<Person, String> binding = binder.forField(nameField);
binding.bind(Person::getFirstName, Person::setFirstName);
Expand All @@ -435,7 +435,7 @@ public void binderWithStatusLabel_setAfterHandler() {

@Test(expected = IllegalStateException.class)
public void binderWithStatusHandler_setAfterLabel() {
Label label = new Label();
TestLabel label = new TestLabel();

BindingBuilder<Person, String> binding = binder.forField(nameField);
binding.bind(Person::getFirstName, Person::setFirstName);
Expand Down Expand Up @@ -472,7 +472,7 @@ public void binderWithStatusHandler_replaceHandler() {
assertValidField(nameField);
}

private void assertVisible(Label label, boolean visible) {
private void assertVisible(TestLabel label, boolean visible) {
if (visible) {
Assert.assertNull(label.getStyle().get("display"));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

import java.util.Objects;

import com.vaadin.flow.data.binder.testcomponents.TestLabel;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.shared.Registration;

/**
Expand All @@ -37,12 +37,12 @@ public class ReadOnlyHasValueTest {
private static final String SAY_SOMETHING = "Say something";
private static final String SAY_SOMETHING_ELSE = "Say something else";
private static final String NO_VALUE = "-no-value-";
private Label label;
private TestLabel label;
private ReadOnlyHasValue<String> hasValue;

@Before
public void setup() {
label = new Label();
label = new TestLabel();
hasValue = new ReadOnlyHasValue<>(label::setText);
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public void testIndicator() {
@Test
public void testBind() {
Binder<Bean> beanBinder = new Binder<>(Bean.class);
Label label = new Label();
TestLabel label = new TestLabel();
ReadOnlyHasValue<Long> intHasValue = new ReadOnlyHasValue<>(
i -> label.setText(Objects.toString(i, "")));

Expand Down Expand Up @@ -104,7 +104,7 @@ public void testBind() {
@Test
public void testEmptyValue() {
Binder<Bean> beanBinder = new Binder<>(Bean.class);
Label label = new Label();
TestLabel label = new TestLabel();
ReadOnlyHasValue<String> strHasValue = new ReadOnlyHasValue<>(
label::setText, NO_VALUE);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2000-2019 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.flow.data.binder.testcomponents;

import java.util.Optional;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HtmlContainer;
import com.vaadin.flow.component.PropertyDescriptor;
import com.vaadin.flow.component.PropertyDescriptors;
import com.vaadin.flow.component.Tag;


/* Test label so that we don't depend on the html components. */
@Tag(Tag.LABEL)
public class TestLabel extends HtmlContainer {
private static final PropertyDescriptor<String, Optional<String>> forDescriptor = PropertyDescriptors
.optionalAttributeWithDefault("for", "");

/**
* Creates a new empty label.
*/
public TestLabel() {
super();
}

/**
* Creates a new label with the given text content.
*
* @param text
* the text content
*/
public TestLabel(String text) {
this();
setText(text);
}

/**
* Sets the component that this label describes. The component (or its id)
* should be defined in case the described component is not an ancestor of
* the label.
* <p>
* The provided component must have an id set. This component will still use
* the old id if the id of the provided component is changed after this
* method has been called.
*
* @param forComponent
* the component that this label describes, not <code>null</code>
* , must have an id
* @throws IllegalArgumentException
* if the provided component has no id
*/
public void setFor(Component forComponent) {
if (forComponent == null) {
throw new IllegalArgumentException(
"The provided component cannot be null");
}
setFor(forComponent.getId()
.orElseThrow(() -> new IllegalArgumentException(
"The provided component must have an id")));
}

/**
* Sets the id of the component that this label describes. The id should be
* defined in case the described component is not an ancestor of the label.
*
* @param forId
* the id of the described component, or <code>null</code> if
* there is no value
*/
public void setFor(String forId) {
set(forDescriptor, forId);
}

/**
* Gets the id of the component that this label describes.
*
* @see #setFor(String)
*
* @return an optional id of the described component, or an empty optional
* if the attribute has not been set
*/
public Optional<String> getFor() {
return get(forDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@

import java.util.concurrent.atomic.AtomicInteger;

import com.vaadin.flow.data.binder.testcomponents.TestLabel;
import org.junit.Assert;
import org.junit.Test;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.Div;

public class ComponentRendererTest {

@Test
public void componentFunction_invokedOnCreate() {
AtomicInteger createInvocations = new AtomicInteger();
ComponentRenderer<Div, String> renderer = new ComponentRenderer<>(
ComponentRenderer<TestLabel, String> renderer = new ComponentRenderer<>(
item -> {
createInvocations.incrementAndGet();
Assert.assertEquals("New item", item);
return new Div();
return new TestLabel();
});

renderer.createComponent("New item");
Expand All @@ -45,8 +45,8 @@ public void componentFunction_invokedOnCreate() {
@Test
public void componentFunction_noUpdateFunction_invokedOnUpdate() {
AtomicInteger createInvocations = new AtomicInteger();
Div div = new Div();
ComponentRenderer<Div, String> renderer = new ComponentRenderer<>(
TestLabel div = new TestLabel();
ComponentRenderer<TestLabel, String> renderer = new ComponentRenderer<>(
item -> {
createInvocations.incrementAndGet();
Assert.assertEquals("New item", item);
Expand All @@ -67,18 +67,18 @@ public void componentFunction_noUpdateFunction_invokedOnUpdate() {
public void updateFunction_invokedOnUpdate() {
AtomicInteger createInvocations = new AtomicInteger();
AtomicInteger updateInvocations = new AtomicInteger();
ComponentRenderer<Div, String> renderer = new ComponentRenderer<>(
ComponentRenderer<TestLabel, String> renderer = new ComponentRenderer<>(
item -> {
createInvocations.incrementAndGet();
Assert.assertEquals("New item", item);
return new Div();
return new TestLabel();
}, (component, item) -> {
updateInvocations.incrementAndGet();
Assert.assertEquals("Updated item", item);
return component;
});

Div div = renderer.createComponent("New item");
TestLabel div = renderer.createComponent("New item");
Component updatedComponent = renderer.updateComponent(div,
"Updated item");

Expand Down
Loading

0 comments on commit 7fc26b3

Please sign in to comment.