diff options
author | KatriHaapalinna <katri@vaadin.com> | 2018-04-19 15:03:16 +0300 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2018-04-19 15:03:16 +0300 |
commit | bdbb0b4328a36a66bb492973ca72497bfd34f6ad (patch) | |
tree | 62272a7e1fbe88f868b2fe13345f2d10fadc2c7e /server/src/test | |
parent | 07c6456da8f15d279340188dc86d7424cee36cd0 (diff) | |
download | vaadin-framework-bdbb0b4328a36a66bb492973ca72497bfd34f6ad.tar.gz vaadin-framework-bdbb0b4328a36a66bb492973ca72497bfd34f6ad.zip |
Colorpicker validation handling (#10821)
* ColorTextField, helper methods, and regex for validating and handling text input
* Refactored structure to avoid creating new component for validation
* Style to adapt to error indicator
* Tests for validating input in ColorPickerPreview component's TextField
* Merge branch 'master' into colorpicker_validation
* Fix path to server class
* Fix test: Submit the new value
* Fix test: ignore Phantom JS
* Fix hsl+hsla validation patterns to accept '%', test value tweaking
* Merge branch 'master' of github.com:vaadin/framework into colorpicker_validation
* Fix: remove warning when color is updated from elsewhere
* Revisions: input validation only once, Logging level WARN
* Revisions: unit tests for color pattern matching
* Revisions: moved parsing to utility class, tests for parsing all accepted input formats
* Fixed import in tests, comments
* Revisions: Logger as constant, ignore utility class in serialization test
* Corner case tests
* Revisions: protected method for parsing error text, fix to test
* Revisions: NPE fix
Diffstat (limited to 'server/src/test')
6 files changed, 433 insertions, 0 deletions
diff --git a/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java b/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java index 0b6d67f7a7..0827fc262c 100644 --- a/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java +++ b/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java @@ -94,6 +94,7 @@ public class ClassesSerializableTest { "com\\.vaadin\\.server\\.JsonCodec\\$1", // "com\\.vaadin\\.server\\.communication\\.PushConnection", // "com\\.vaadin\\.server\\.communication\\.AtmospherePushConnection.*", // + "com\\.vaadin\\.ui\\.components\\.colorpicker\\.ColorUtil", // "com\\.vaadin\\.util\\.ConnectorHelper", // "com\\.vaadin\\.server\\.VaadinSession\\$FutureAccess", // "com\\.vaadin\\.external\\..*", // diff --git a/server/src/test/java/com/vaadin/tests/server/component/colorpicker/HSLAPatternParsingTest.java b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/HSLAPatternParsingTest.java new file mode 100644 index 0000000000..548e61df26 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/HSLAPatternParsingTest.java @@ -0,0 +1,88 @@ +package com.vaadin.tests.server.component.colorpicker; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.regex.Matcher; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import com.vaadin.shared.ui.colorpicker.Color; +import com.vaadin.ui.components.colorpicker.ColorUtil; + +@RunWith(value = Parameterized.class) +public class HSLAPatternParsingTest { + + @Parameter(value = 0) + public String input; + + @Parameter(value = 1) + public int expectedHue; + @Parameter(value = 2) + public int expectedSaturation; + @Parameter(value = 3) + public int expectedLight; + @Parameter(value = 4) + public int expectedAlpha; + + @Parameter(value = 5) + public boolean expectedMatches; + + @Parameters(name = "{index}: testHSLAData({0}) = ({1},{2},{3},{4},{5})") + public static Collection<Object[]> hsladata() { + Object[][] validValues = { { "hsla(0,0,0,0)", 0, 0, 0, 0, true }, + { "HSLA(0, 0, 0, 0)", 0, 0, 0, 0, true }, + { "hsla(0,0%,0%, 0.1)", 0, 0, 0, 25, true }, + { "hsla(0 0 0 0.00 )", 0, 0, 0, 0, true }, + { "hsla(0 0% 0% 0.50)", 0, 0, 0, 127, true }, + { "hsla(360,100,100,1.0)", 360, 100, 100, 255, true }, + { "hsla(360, 100, 100, 1.0)", 360, 100, 100, 255, true }, + { "hsla(360, 100%, 100%, 1.00)", 360, 100, 100, 255, true }, + { "hsla(360 100% 100% 1.00)", 360, 100, 100, 255, true }, + { "hsla(20, 10, 10, 0.24)", 20, 10, 10, 61, true }, + { "hsla(100, 0, 50, 0.8)", 100, 0, 50, 204, true }, + { "hsla(269, 50, 0, .6)", 269, 50, 0, 153, true }, }; + Object[][] invalidValues = { { "hsla(361,0,0,0)", 0, 0, 0, 0, false }, + { "hsla(0.0, 0, 0, 0)", 0, 0, 0, 0, false }, + { "hsla(0,0%,0%, 1.1)", 0, 0, 0, 0, false }, + { "hsla(0 0 0 0.009 )", 0, 0, 0, 0, false }, + { "hsla(0 0% -100% 0.50)", 0, 0, 0, 0, false }, + { "hsla(360,1000,100,1.0)", 0, 0, 0, 0, false }, + { "hsla(0, 100, 100, 2.0)", 0, 0, 0, 0, false }, + { "hsla(360, 100%, 100%, 10.00)", 0, 0, 0, 0, false }, + { "hsl a(360 100% 100% 1.)", 0, 0, 0, 0, false }, + { "hsla(20, -10, 10, 0.24)", 0, 0, 0, 0, false }, + { "hsla(400, 0, 50, 0.8)", 0, 0, 0, 0, false }, + { "hsla(200, 50, 0, 0.996)", 0, 0, 0, 0, false }, + { "hsla 200, 50, 0, 0.9", 0, 0, 0, 0, false }, + { "hsla(0,0,0,0.)", 0, 0, 0, 0, false } }; + + ArrayList<Object[]> values = new ArrayList<>(); + Collections.addAll(values, validValues); + Collections.addAll(values, invalidValues); + + return values; + } + + @Test + public void testHSLAData() { + Matcher m = ColorUtil.HSLA_PATTERN.matcher(input); + boolean matches = m.matches(); + if (expectedMatches) { + Color expectedColor = new Color(Color.HSLtoRGB(expectedHue, + expectedSaturation, expectedLight)); + expectedColor.setAlpha(expectedAlpha); + Color c1 = ColorUtil.getHSLAPatternColor(m); + assertTrue(expectedColor.equals(c1)); + } else { + assertTrue(!matches); + } + } + +} diff --git a/server/src/test/java/com/vaadin/tests/server/component/colorpicker/HSLPatternParsingTest.java b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/HSLPatternParsingTest.java new file mode 100644 index 0000000000..738388a70d --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/HSLPatternParsingTest.java @@ -0,0 +1,89 @@ +package com.vaadin.tests.server.component.colorpicker; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.regex.Matcher; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import com.vaadin.shared.ui.colorpicker.Color; +import com.vaadin.ui.components.colorpicker.ColorUtil; + +@RunWith(value = Parameterized.class) +public class HSLPatternParsingTest { + + @Parameter(value = 0) + public String input; + + @Parameter(value = 1) + public Color expectedColor; + + @Parameter(value = 2) + public boolean expectedMatches; + + @Parameters(name = "{index}: testHSLData({0}) = ({1},{2})") + public static Collection<Object[]> hsldata() { + Object[][] validValues = { + { "hsl(0,0,0)", new Color(Color.HSLtoRGB(0, 0, 0)), true }, + { "hsl(0, 0, 0)", new Color(Color.HSLtoRGB(0, 0, 0)), true }, + { "hsl(0,0%,0% )", new Color(Color.HSLtoRGB(0, 0, 0)), true }, + { "hsl(0 0 0)", new Color(Color.HSLtoRGB(0, 0, 0)), true }, + { "hsl(0 0% 0%)", new Color(Color.HSLtoRGB(0, 0, 0)), true }, + { "hsl(360,100,100)", new Color(Color.HSLtoRGB(360, 100, 100)), + true }, + { "hsl(360, 100, 100)", + new Color(Color.HSLtoRGB(360, 100, 100)), true }, + { "hsl(360, 100%, 100%)", + new Color(Color.HSLtoRGB(360, 100, 100)), true }, + { "hsl(360 100% 100%)", + new Color(Color.HSLtoRGB(360, 100, 100)), true }, + { "hsl(20, 10, 10)", new Color(Color.HSLtoRGB(20, 10, 10)), + true }, + { "hsl(100, 0, 50)", new Color(Color.HSLtoRGB(100, 0, 50)), + true }, + { "hsl(200, 50, 0)", new Color(Color.HSLtoRGB(200, 50, 0)), + true }, + { "hsl(200, 50, 05)", new Color(Color.HSLtoRGB(200, 50, 5)), + true } }; + Object[][] invalidValues = { { "hsl(361,0,0)", null, false }, + { "hsl(-0, 0, 0)", null, false }, + { "hsl (100%,0%,0% )", null, false }, + { "hsl(0 101 0)", null, false }, + { "hsl(0 0% -99%)", null, false }, + { "hsl(360,100,10 0)", null, false }, + { "hsl(360, 100, 101)", null, false }, + { "hsl(360, 110%, 100%)", null, false }, + { "hsl(3600 100% 100%)", null, false }, + { "hs l(420, 10, 10)", null, false }, + { "hsl(100, 0, 5,0)", null, false }, + { "hsla(200, 50, 0)", null, false }, + { "hsl(0,0,0", null, false }, { "rgb\\(\\.*", null, false }, + { "hsl(\\.*)", null, false }, { "#\\d.*", null, false }, + { "", null, false } }; + ArrayList<Object[]> values = new ArrayList<>(); + Collections.addAll(values, validValues); + Collections.addAll(values, invalidValues); + + return values; + } + + @Test + public void testHSLData() { + Matcher m = ColorUtil.HSL_PATTERN.matcher(input); + boolean matches = m.matches(); + if (expectedMatches) { + Color c1 = ColorUtil.getHSLPatternColor(m); + assertTrue(expectedColor.equals(c1)); + } else { + assertTrue(!matches); + } + } + +} diff --git a/server/src/test/java/com/vaadin/tests/server/component/colorpicker/HexPatternParsingTest.java b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/HexPatternParsingTest.java new file mode 100644 index 0000000000..a79e928ddf --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/HexPatternParsingTest.java @@ -0,0 +1,76 @@ +package com.vaadin.tests.server.component.colorpicker; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.regex.Matcher; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import com.vaadin.shared.ui.colorpicker.Color; +import com.vaadin.ui.components.colorpicker.ColorUtil; + +@RunWith(value = Parameterized.class) +public class HexPatternParsingTest { + + @Parameter(value = 0) + public String input; + + @Parameter(value = 1) + public int expectedRed; + @Parameter(value = 2) + public int expectedGreen; + @Parameter(value = 3) + public int expectedBlue; + + @Parameter(value = 4) + public boolean expectedMatches; + + @Parameters(name = "{index}: textValidHEX({0}) = ({1},{2},{3},{4})") + public static Collection<Object[]> hexdata() { + Object[][] validValues = { { "#000000", 0, 0, 0, true }, + { "#ffffff", 255, 255, 255, true }, + { "#FF00ff", 255, 0, 255, true }, { "#aa90e3",170,144,227, true }, + { "#016953", 1, 105, 83, true }, + { "#bC64D0", 188, 100, 208, true }, + { "#F100FF", 241, 0, 255, true }, + { "#F0E9a5", 240, 233, 165, true }, + { "#990077", 153, 0, 119, true } }; + Object[][] invalidValues = { { "#0000000", 0, 0, 0, false }, + { "#ffgfff", 0, 0, 0, false }, { "#FF10f", 0, 0, 0, false }, + { "#aa9", 0, 0, 0, false }, { "#03", 0, 0, 0, false }, + { "#aab3c4c", 0, 0, 0, false }, { "#6010", 0, 0, 0, false }, + { "#CCCC", 0, 0, 0, false }, { "#9", 0, 0, 0, false }, + { "#10 10 10", 0, 0, 0, false }, { "101010", 0, 0, 0, false }, + { "#10101q", 0, 0, 0, false }, + { "\\s%\\d[0-9]", 0, 0, 0, false }, + { "#\\d.*", 0, 0, 0, false }, { "rgb\\(\\.*", 0, 0, 0, false }, + { "#\\d\\d\\d", 0, 0, 0, false }, { "#\\d.*", 0, 0, 0, false }, + { "", 0, 0, 0, false }, { "hsl(25,25,25)", 0, 0, 0, false } }; + ArrayList<Object[]> values = new ArrayList<>(); + Collections.addAll(values, validValues); + Collections.addAll(values, invalidValues); + + return values; + } + + @Test + public void testValidHEX() { + Matcher m = ColorUtil.HEX_PATTERN.matcher(input); + boolean matches = m.matches(); + if (expectedMatches) { + Color c = new Color(expectedRed, expectedGreen, expectedBlue); + Color c1 = ColorUtil.getHexPatternColor(m); + assertTrue(c.equals(c1)); + } else { + assertTrue(!matches); + } + } + +} diff --git a/server/src/test/java/com/vaadin/tests/server/component/colorpicker/RGBAPatternParsingTest.java b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/RGBAPatternParsingTest.java new file mode 100644 index 0000000000..71ea7c7d7b --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/RGBAPatternParsingTest.java @@ -0,0 +1,92 @@ +package com.vaadin.tests.server.component.colorpicker; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.regex.Matcher; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import com.vaadin.shared.ui.colorpicker.Color; +import com.vaadin.ui.components.colorpicker.ColorUtil; + +@RunWith(value = Parameterized.class) +public class RGBAPatternParsingTest { + + @Parameter(value = 0) + public String input; + + @Parameter(value = 1) + public int expectedRed; + @Parameter(value = 2) + public int expectedGreen; + @Parameter(value = 3) + public int expectedBlue; + @Parameter(value = 4) + public int expectedAlpha; + + @Parameter(value = 5) + public boolean expectedMatches; + + @Parameters(name = "{index}: testRGBAData({0}) = ({1},{2},{3},{4},{5})") + public static Collection<Object[]> rgbdata() { + Object[][] validValues = { { "rgba(0,0,0,0)", 0, 0, 0, 0, true }, + { "RGBA(0, 0, 0, 0 )", 0, 0, 0, 0, true }, + { "rgba(0 0 0 0.00)", 0, 0, 0, 0, true }, + { "rgba(1 1 1 1.00)", 1, 1, 1, 255, true }, + { "rgba(0 100 200 0.50)", 0, 100, 200, 127, true }, + { "rgba(255,255,255,1.0)", 255, 255, 255, 255, true }, + { "rgba(255, 255, 255, 1.0)", 255, 255, 255, 255, true }, + { "rgba(255 255 255 0)", 255, 255, 255, 0, true }, + { "rgba(1, 10, 100, 0.00)", 1, 10, 100, 0, true } }; + Object[][] invalidValues = { { "rgba(256,0,0,0)", 0, 0, 0, 0, false }, + { "rgba(0, 256, 0, -0 )", 0, 0, 0, 0, false }, + { "rgba(0,0,10.0, 00)", 0, 0, 0, 0, false }, + { "rgba(0 0 0 2.00)", 0, 0, 0, 0, false }, + { "rgba(0 -99 0 0.50)", 0, 0, 0, 0, false }, + { "rgba(0,255%,255,1.0)", 0, 0, 0, 0, false }, + { "rgba(255, 255, 255, 1.05)", 0, 0, 0, 0, false }, + { "rgba(255, 255, 255, 1.50)", 0, 0, 0, 0, false }, + { "rgb a(255 255 0.005)", 0, 0, 0, 0, false }, + { "rgba(163, 256, 1000, 0.24)", 0, 0, 0, 0, false }, + { "rgba(100, 0.5, 250, 0.8)", 0, 0, 0, 0, false }, + { "rgba(, 50, 0, 0.6)", 0, 0, 0, 0, false }, + { "rgba(200, 50, 0, 10.6)", 0, 0, 0, 0, false }, + { "rgba 200, 50, 0, 1.", 0, 0, 0, 0, false }, + { "rgba(0,0,0,0.)", 0, 0, 0, 0, false }, + { "rgb(200, 50, 0)", 0, 0, 0, 0, false }, + { "hsla,0(10,0,0)", 0, 0, 0, 0, false }, + { "rgba(\\s.*\\d[0-9])", 0, 0, 0, 0, false }, + { "rgba(\\.*,255,255, 0)", 0, 0, 0, 0, false }, + { "#\\d.*", 0, 0, 0, 0, false }, { "", 0, 0, 0, 0, false }, + { "rgba(\\d,\\d,\\d,0.0)", 0, 0, 0, 0, false }, + { "^rgba\\( \\.*)", 0, 0, 0, 0, false } }; + + ArrayList<Object[]> values = new ArrayList<>(); + Collections.addAll(values, validValues); + Collections.addAll(values, invalidValues); + + return values; + } + + @Test + public void testRGBAData() { + Matcher m = ColorUtil.RGBA_PATTERN.matcher(input); + boolean matches = m.matches(); + if (expectedMatches) { + Color expectedColor = new Color(expectedRed, expectedGreen, + expectedBlue, expectedAlpha); + Color c1 = ColorUtil.getRGBAPatternColor(m); + + assertTrue(expectedColor.equals(c1)); + } else { + assertTrue(!matches); + } + } +} diff --git a/server/src/test/java/com/vaadin/tests/server/component/colorpicker/RGBPatternParsingTest.java b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/RGBPatternParsingTest.java new file mode 100644 index 0000000000..f853663faa --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/RGBPatternParsingTest.java @@ -0,0 +1,87 @@ +package com.vaadin.tests.server.component.colorpicker; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.regex.Matcher; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import com.vaadin.shared.ui.colorpicker.Color; +import com.vaadin.ui.components.colorpicker.ColorUtil; + +@RunWith(value = Parameterized.class) +public class RGBPatternParsingTest { + + @Parameter(value = 0) + public String input; + + @Parameter(value = 1) + public int expectedRed; + @Parameter(value = 2) + public int expectedGreen; + @Parameter(value = 3) + public int expectedBlue; + + @Parameter(value = 4) + public boolean expectedMatches; + + @Parameters(name = "{index}: testRGBData({0}) = ({1},{2},{3},{4})") + public static Collection<Object[]> rgbdata() { + Object[][] validValues = { { "rgb(0,0,0)", 0, 0, 0, true }, + { "rgb(0, 0, 0)", 0, 0, 0, true }, + { "rgb(0 0 0)", 0, 0, 0, true }, + { "rgb(1 1 1)", 1, 1, 1, true }, + { "rgb(0 100 255)", 0, 100, 255, true }, + { "rgb(255,255,255)", 255, 255, 255, true }, + { "RGB(255, 255, 255 )", 255, 255, 255, true }, + { "rgb(255 255 255)", 255, 255, 255, true }, + { "rgb(1, 10, 100)", 1, 10, 100, true } }; + Object[][] invalidValues = { { "rgb(,0,0)", 0, 0, 0, false }, + { "rgb(0, 0, 0, )", 0, 0, 0, false }, + { "rgb(0.0,0,0)", 0, 0, 0, false }, + { "rgb( 0 0 -1 )", 0, 0, 0, false }, + { "rgb(1 00)", 0, 0, 0, false }, + { "rgb(255,255,255.)", 0, 0, 0, false }, + { "r gb(255, 256, 255)", 0, 0, 0, false }, + { "rgb( 255, 255, 256 )", 0, 0, 0, false }, + { "rgb(163, 2%, 210)", 0, 0, 0, false }, + { "rGb(000)", 0, 0, 0, false }, + { "rgb(255255255)", 0, 0, 0, false }, + { "rGBA(255,255,255)", 0, 0, 0, false }, + { "rgb 255 255 255)", 0, 0, 0, false }, + { "255, 255, 0", 0, 0, 0, false }, + { "hsl(10,0,0)", 0, 0, 0, false }, + { "\\s%\\d[0-9]", 0, 0, 0, false }, + { "rgb(\\.*,255,255)", 0, 0, 0, false }, + { "#\\d.*", 0, 0, 0, false }, { "", 0, 0, 0, false }, + { "rgb(\\d,\\d,\\d)", 0, 0, 0, false }, + { "^rgb\\( \\.*)", 0, 0, 0, false } }; + + ArrayList<Object[]> values = new ArrayList<>(); + Collections.addAll(values, validValues); + Collections.addAll(values, invalidValues); + + return values; + } + + @Test + public void testRGBData() { + Matcher m = ColorUtil.RGB_PATTERN.matcher(input); + boolean matches = m.matches(); + if (expectedMatches) { + Color expectedColor = new Color(expectedRed, expectedGreen, + expectedBlue); + Color c1 = ColorUtil.getRGBPatternColor(m); + assertTrue(expectedColor.equals(c1)); + } else { + assertTrue(!matches); + } + } +} |