You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RGBAPatternParsingTest.java 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.vaadin.tests.server.component.colorpicker;
  2. import static org.junit.Assert.assertTrue;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.regex.Matcher;
  7. import org.junit.Test;
  8. import org.junit.runner.RunWith;
  9. import org.junit.runners.Parameterized;
  10. import org.junit.runners.Parameterized.Parameter;
  11. import org.junit.runners.Parameterized.Parameters;
  12. import com.vaadin.shared.ui.colorpicker.Color;
  13. import com.vaadin.ui.components.colorpicker.ColorUtil;
  14. @RunWith(value = Parameterized.class)
  15. public class RGBAPatternParsingTest {
  16. @Parameter(value = 0)
  17. public String input;
  18. @Parameter(value = 1)
  19. public int expectedRed;
  20. @Parameter(value = 2)
  21. public int expectedGreen;
  22. @Parameter(value = 3)
  23. public int expectedBlue;
  24. @Parameter(value = 4)
  25. public int expectedAlpha;
  26. @Parameter(value = 5)
  27. public boolean expectedMatches;
  28. @Parameters(name = "{index}: testRGBAData({0}) = ({1},{2},{3},{4},{5})")
  29. public static Collection<Object[]> rgbdata() {
  30. Object[][] validValues = { { "rgba(0,0,0,0)", 0, 0, 0, 0, true },
  31. { "RGBA(0, 0, 0, 0 )", 0, 0, 0, 0, true },
  32. { "rgba(0 0 0 0.00)", 0, 0, 0, 0, true },
  33. { "rgba(1 1 1 1.00)", 1, 1, 1, 255, true },
  34. { "rgba(0 100 200 0.50)", 0, 100, 200, 127, true },
  35. { "rgba(255,255,255,1.0)", 255, 255, 255, 255, true },
  36. { "rgba(255, 255, 255, 1.0)", 255, 255, 255, 255, true },
  37. { "rgba(255 255 255 0)", 255, 255, 255, 0, true },
  38. { "rgba(1, 10, 100, 0.00)", 1, 10, 100, 0, true } };
  39. Object[][] invalidValues = { { "rgba(256,0,0,0)", 0, 0, 0, 0, false },
  40. { "rgba(0, 256, 0, -0 )", 0, 0, 0, 0, false },
  41. { "rgba(0,0,10.0, 00)", 0, 0, 0, 0, false },
  42. { "rgba(0 0 0 2.00)", 0, 0, 0, 0, false },
  43. { "rgba(0 -99 0 0.50)", 0, 0, 0, 0, false },
  44. { "rgba(0,255%,255,1.0)", 0, 0, 0, 0, false },
  45. { "rgba(255, 255, 255, 1.05)", 0, 0, 0, 0, false },
  46. { "rgba(255, 255, 255, 1.50)", 0, 0, 0, 0, false },
  47. { "rgb a(255 255 0.005)", 0, 0, 0, 0, false },
  48. { "rgba(163, 256, 1000, 0.24)", 0, 0, 0, 0, false },
  49. { "rgba(100, 0.5, 250, 0.8)", 0, 0, 0, 0, false },
  50. { "rgba(, 50, 0, 0.6)", 0, 0, 0, 0, false },
  51. { "rgba(200, 50, 0, 10.6)", 0, 0, 0, 0, false },
  52. { "rgba 200, 50, 0, 1.", 0, 0, 0, 0, false },
  53. { "rgba(0,0,0,0.)", 0, 0, 0, 0, false },
  54. { "rgb(200, 50, 0)", 0, 0, 0, 0, false },
  55. { "hsla,0(10,0,0)", 0, 0, 0, 0, false },
  56. { "rgba(\\s.*\\d[0-9])", 0, 0, 0, 0, false },
  57. { "rgba(\\.*,255,255, 0)", 0, 0, 0, 0, false },
  58. { "#\\d.*", 0, 0, 0, 0, false }, { "", 0, 0, 0, 0, false },
  59. { "rgba(\\d,\\d,\\d,0.0)", 0, 0, 0, 0, false },
  60. { "^rgba\\( \\.*)", 0, 0, 0, 0, false } };
  61. ArrayList<Object[]> values = new ArrayList<>();
  62. Collections.addAll(values, validValues);
  63. Collections.addAll(values, invalidValues);
  64. return values;
  65. }
  66. @Test
  67. public void testRGBAData() {
  68. Matcher m = ColorUtil.RGBA_PATTERN.matcher(input);
  69. boolean matches = m.matches();
  70. if (expectedMatches) {
  71. Color expectedColor = new Color(expectedRed, expectedGreen,
  72. expectedBlue, expectedAlpha);
  73. Color c1 = ColorUtil.getRGBAPatternColor(m);
  74. assertTrue(expectedColor.equals(c1));
  75. } else {
  76. assertTrue(!matches);
  77. }
  78. }
  79. }