-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced HasLitRenderer with LitRendererTestUtils and associated refa…
…ctorings.
- Loading branch information
Showing
4 changed files
with
161 additions
and
108 deletions.
There are no files selected for viewing
96 changes: 0 additions & 96 deletions
96
vaadin-testbench-unit-shared/src/main/java/com/vaadin/flow/component/HasLitRenderer.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
...in-testbench-unit-shared/src/main/java/com/vaadin/testbench/unit/LitRendererTestUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright (C) 2000-2024 Vaadin Ltd | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See <https://vaadin.com/commercial-license-and-service-terms> for the full | ||
* license. | ||
*/ | ||
package com.vaadin.testbench.unit; | ||
|
||
import com.vaadin.flow.data.renderer.LitRenderer; | ||
import com.vaadin.flow.function.SerializableBiConsumer; | ||
import com.vaadin.flow.function.ValueProvider; | ||
import elemental.json.JsonArray; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiFunction; | ||
import java.util.function.IntFunction; | ||
|
||
/** | ||
* Utility methods for unit testing properties and functions of LitRenderers. | ||
*/ | ||
public class LitRendererTestUtil { | ||
|
||
private LitRendererTestUtil() throws InstantiationException { | ||
throw new InstantiationException(LitRendererTestUtil.class.getName() + " should not be instantiated"); | ||
} | ||
|
||
/** | ||
* Gets the property names for the supplied {@link LitRenderer} using the given field getter. | ||
* | ||
* @param litRenderer the LitRenderer with properties to get | ||
* @param fieldGetter the field getter of the ComponentTester | ||
* @return the set of property names of the LitRenderer | ||
* @param <Y> the type being renderer by the LitRenderer | ||
*/ | ||
public static <Y> Set<String> getProperties(LitRenderer<Y> litRenderer, | ||
BiFunction<Class<?>, String, Field> fieldGetter) { | ||
var valueProvidersField = fieldGetter.apply(LitRenderer.class, "valueProviders"); | ||
try { | ||
@SuppressWarnings("unchecked") | ||
var valueProviders = (Map<String, ValueProvider<Y, ?>>) valueProvidersField.get(litRenderer); | ||
return valueProviders.keySet(); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static <Y> ValueProvider<Y, ?> findProperty(LitRenderer<Y> litRenderer, String propertyName, | ||
BiFunction<Class<?>, String, Field> fieldGetter) { | ||
var valueProvidersField = fieldGetter.apply(LitRenderer.class, "valueProviders"); | ||
try { | ||
@SuppressWarnings("unchecked") | ||
var valueProviders = (Map<String, ValueProvider<Y, ?>>) valueProvidersField.get(litRenderer); | ||
var valueProvider = valueProviders.get(propertyName); | ||
if (valueProvider == null) { | ||
throw new IllegalArgumentException("Property " + propertyName + " is not registered in LitRenderer."); | ||
} | ||
return valueProvider; | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the property value for the supplied {@link LitRenderer}. | ||
* | ||
* @param index the index of the item rendered by the LitRenderer | ||
* @param propertyName the name of the property of the LitRenderer | ||
* @param propertyClass the type of the property value | ||
* @param fieldGetter the field getter of the ComponentTester | ||
* @param litRenderer the LitRenderer with properties to get | ||
* @param itemGetter the getter for the item rendered by the LitRenderer | ||
* @return the property value | ||
* @param <Y> the type being renderer by the LitRenderer | ||
* @param <V> the type of the property value | ||
*/ | ||
public static <Y, V> V getPropertyValue(int index, | ||
String propertyName, Class<V> propertyClass, | ||
BiFunction<Class<?>, String, Field> fieldGetter, | ||
LitRenderer<Y> litRenderer, | ||
IntFunction<Y> itemGetter) { | ||
var valueProvider = findProperty(litRenderer, propertyName, fieldGetter); | ||
var untypedValue = valueProvider.apply(itemGetter.apply(index)); | ||
if (propertyClass.isInstance(untypedValue)) { | ||
return propertyClass.cast(untypedValue); | ||
} else { | ||
throw new IllegalArgumentException("Type of property value does not match propertyClass - expected %s, found %s." | ||
.formatted(propertyClass.getCanonicalName(), untypedValue.getClass().getCanonicalName())); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Gets the function names for the supplied {@link LitRenderer} using the given field getter. | ||
* | ||
* @param litRenderer the LitRenderer with properties to get | ||
* @param fieldGetter the field getter of the ComponentTester | ||
* @return the set of function names of the LitRenderer | ||
* @param <Y> the type being renderer by the LitRenderer | ||
*/ | ||
public static <Y> Set<String> getFunctionNames(LitRenderer<Y> litRenderer, | ||
BiFunction<Class<?>, String, Field> fieldGetter) { | ||
var clientCallablesField = fieldGetter.apply(LitRenderer.class, "clientCallables"); | ||
try { | ||
@SuppressWarnings("unchecked") | ||
var clientCallables = (Map<String, SerializableBiConsumer<Y, JsonArray>>) clientCallablesField.get(litRenderer); | ||
return clientCallables.keySet(); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static <Y> SerializableBiConsumer<Y, JsonArray> findFunction(LitRenderer<Y> litRenderer, String functionName, | ||
BiFunction<Class<?>, String, Field> fieldGetter) { | ||
var clientCallablesField = fieldGetter.apply(LitRenderer.class, "clientCallables"); | ||
try { | ||
@SuppressWarnings("unchecked") | ||
var clientCallables = (Map<String, SerializableBiConsumer<Y, JsonArray>>) clientCallablesField.get(litRenderer); | ||
var callable = clientCallables.get(functionName); | ||
if (callable == null) { | ||
throw new IllegalArgumentException("Function " + functionName + " is not registered in LitRenderer."); | ||
} | ||
return callable; | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Invokes the function by name for the supplied {@link LitRenderer} using the given field getter. | ||
* | ||
* @param index the index of the item rendered by the LitRenderer | ||
* @param functionName the name of the function of the LitRenderer | ||
* @param jsonArray additional parameters to pass to the function | ||
* @param fieldGetter the field getter of the ComponentTester | ||
* @param litRenderer the LitRenderer with properties to get | ||
* @param itemGetter the getter for the item rendered by the LitRenderer | ||
* @param <Y> the type being renderer by the LitRenderer | ||
*/ | ||
public static <Y> void invokeFunction(int index, String functionName, JsonArray jsonArray, | ||
BiFunction<Class<?>, String, Field> fieldGetter, | ||
LitRenderer<Y> litRenderer, | ||
IntFunction<Y> itemGetter) { | ||
var callable = findFunction(litRenderer, functionName, fieldGetter); | ||
callable.accept(itemGetter.apply(index), jsonArray); | ||
} | ||
|
||
} |