MediaExpression { public boolean matches(MediaDeviceDescription deviceDescription) { switch (feature) { case MediaFeature.COLOR: { Integer val = CssUtils.parseInteger(value); if (minPrefix) { return val != null && deviceDescription.getBitsPerComponent() >= val; } else if (maxPrefix) { return val != null && deviceDescription.getBitsPerComponent() <= val; } else { return val == null ? deviceDescription.getBitsPerComponent() != 0 : val == deviceDescription.getBitsPerComponent(); } } case MediaFeature.COLOR_INDEX: { Integer val = CssUtils.parseInteger(value); if (minPrefix) { return val != null && deviceDescription.getColorIndex() >= val; } else if (maxPrefix) { return val != null && deviceDescription.getColorIndex() <= val; } else { return val == null ? deviceDescription.getColorIndex() != 0 : val == deviceDescription.getColorIndex(); } } case MediaFeature.ASPECT_RATIO: { int[] aspectRatio = CssUtils.parseAspectRatio(value); if (minPrefix) { return aspectRatio != null && aspectRatio[0] * deviceDescription.getHeight() >= aspectRatio[1] * deviceDescription.getWidth(); } else if (maxPrefix) { return aspectRatio != null && aspectRatio[0] * deviceDescription.getHeight() <= aspectRatio[1] * deviceDescription.getWidth(); } else { return aspectRatio != null && CssUtils.compareFloats(aspectRatio[0] * deviceDescription.getHeight(), aspectRatio[1] * deviceDescription.getWidth()); } } case MediaFeature.GRID: { Integer val = CssUtils.parseInteger(value); return val != null && val == 0 && !deviceDescription.isGrid() || deviceDescription.isGrid(); } case MediaFeature.SCAN: { return Objects.equals(value, deviceDescription.getScan()); } case MediaFeature.ORIENTATION: { return Objects.equals(value, deviceDescription.getOrientation()); } case MediaFeature.MONOCHROME: { Integer val = CssUtils.parseInteger(value); if (minPrefix) { return val != null && deviceDescription.getMonochrome() >= val; } else if (maxPrefix) { return val != null && deviceDescription.getMonochrome() <= val; } else { return val == null ? deviceDescription.getMonochrome() > 0 : val == deviceDescription.getMonochrome(); } } case MediaFeature.HEIGHT: { float val = parseAbsoluteLength(value); if (minPrefix) { return deviceDescription.getHeight() >= val; } else if (maxPrefix) { return deviceDescription.getHeight() <= val; } else { return deviceDescription.getHeight() > 0; } } case MediaFeature.WIDTH: { float val = parseAbsoluteLength(value); if (minPrefix) { return deviceDescription.getWidth() >= val; } else if (maxPrefix) { return deviceDescription.getWidth() <= val; } else { return deviceDescription.getWidth() > 0; } } case MediaFeature.RESOLUTION: { float val = CssUtils.parseResolution(value); if (minPrefix) { return deviceDescription.getResolution() >= val; } else if (maxPrefix) { return deviceDescription.getResolution() <= val; } else { return deviceDescription.getResolution() > 0; } } default: return false; } } MediaExpression(String feature, String value); boolean matches(MediaDeviceDescription deviceDescription);  }
@Test public void mediaExpressionTestTest01() { MediaExpression minWidth = new MediaExpression("min-width", "600px"); MediaExpression minHeight = new MediaExpression("min-height", "600px"); MediaDeviceDescription deviceDescription = new MediaDeviceDescription("all"); deviceDescription.setWidth(600); deviceDescription.setHeight(600); Assert.assertTrue(minHeight.matches(deviceDescription)); Assert.assertTrue(minWidth.matches(deviceDescription)); }
@Test public void mediaExpressionTestTest02() { MediaExpression maxWidth = new MediaExpression("max-width", "600px"); MediaExpression maxHeight = new MediaExpression("max-height", "600px"); MediaDeviceDescription deviceDescription = new MediaDeviceDescription("all"); deviceDescription.setWidth(450); deviceDescription.setHeight(450); Assert.assertTrue(maxHeight.matches(deviceDescription)); Assert.assertTrue(maxWidth.matches(deviceDescription)); }
@Test public void mediaExpressionTestTest03() { MediaExpression orientation = new MediaExpression("orientation", "landscape"); MediaExpression resolution = new MediaExpression("resolution", "150dpi"); MediaExpression grid = new MediaExpression("grid", "0"); MediaExpression colorIndex = new MediaExpression("max-color-index", "15000"); MediaExpression monochrome = new MediaExpression("monochrome", "0"); MediaExpression scan = new MediaExpression("scan", "interlace"); MediaExpression color = new MediaExpression("color", "15000"); MediaExpression minAspectRatio = new MediaExpression("max-aspect-ratio", "8/5"); MediaDeviceDescription deviceDescription = new MediaDeviceDescription("all"); deviceDescription.setOrientation("landscape"); deviceDescription.setResolution(150); deviceDescription.setGrid(false); deviceDescription.setColorIndex(15000); deviceDescription.setMonochrome(0); deviceDescription.setScan("interlace"); deviceDescription.setBitsPerComponent(15000); deviceDescription.setWidth(32); deviceDescription.setHeight(20); Assert.assertTrue(resolution.matches(deviceDescription)); Assert.assertTrue(orientation.matches(deviceDescription)); Assert.assertTrue(grid.matches(deviceDescription)); Assert.assertTrue(colorIndex.matches(deviceDescription)); Assert.assertTrue(monochrome.matches(deviceDescription)); Assert.assertTrue(scan.matches(deviceDescription)); Assert.assertTrue(color.matches(deviceDescription)); Assert.assertTrue(minAspectRatio.matches(deviceDescription)); }
@Test public void mediaExpressionTestTest04() { MediaExpression minColorIndex = new MediaExpression("min-color-index", "15000"); MediaExpression minResolution = new MediaExpression("min-resolution", "150dpi"); MediaExpression minColor = new MediaExpression("min-color", "8"); MediaExpression minAspectRatio = new MediaExpression("min-aspect-ratio", "8/5"); MediaDeviceDescription deviceDescription = new MediaDeviceDescription("all"); deviceDescription.setColorIndex(15000); deviceDescription.setBitsPerComponent(8); deviceDescription.setResolution(150); deviceDescription.setWidth(32); deviceDescription.setHeight(20); Assert.assertTrue(minAspectRatio.matches(deviceDescription)); Assert.assertTrue(minColorIndex.matches(deviceDescription)); Assert.assertTrue(minColor.matches(deviceDescription)); Assert.assertTrue(minResolution.matches(deviceDescription)); }
@Test public void mediaExpressionTestTest05() { MediaExpression maxColorIndex = new MediaExpression("max-color-index", null); MediaExpression maxColor = new MediaExpression("max-color", null); MediaExpression maxWidth = new MediaExpression("width", "600ex"); MediaExpression maxHeight = new MediaExpression("height", "600ex"); MediaExpression maxMonochrome = new MediaExpression("max-monochrome", "0"); MediaExpression maxResolution = new MediaExpression("max-resolution", "150dpi"); MediaDeviceDescription deviceDescription = new MediaDeviceDescription("all"); deviceDescription.setHeight(450); deviceDescription.setWidth(450); deviceDescription.setColorIndex(15000); deviceDescription.setBitsPerComponent(8); deviceDescription.setMonochrome(0); deviceDescription.setResolution(150); Assert.assertTrue(maxMonochrome.matches(deviceDescription)); Assert.assertTrue(maxHeight.matches(deviceDescription)); Assert.assertTrue(maxWidth.matches(deviceDescription)); Assert.assertFalse(maxColorIndex.matches(deviceDescription)); Assert.assertFalse(maxColor.matches(deviceDescription)); Assert.assertTrue(maxResolution.matches(deviceDescription)); }
@Test public void mediaExpressionTestTest06() { MediaExpression minColorIndex = new MediaExpression("min-color-index", null); MediaExpression minColor = new MediaExpression("min-color", null); MediaExpression colorIndex = new MediaExpression("color-index", "1500"); MediaExpression minMonochrome = new MediaExpression("min-monochrome", "0"); MediaExpression resolution = new MediaExpression("resolution", "150dpi"); MediaExpression defaultExpression = new MediaExpression("none", "none"); MediaExpression aspectRatio = new MediaExpression("aspect-ratio", "8/8"); MediaDeviceDescription deviceDescription = new MediaDeviceDescription("all"); deviceDescription.setColorIndex(15000); deviceDescription.setBitsPerComponent(8); deviceDescription.setMonochrome(0); deviceDescription.setWidth(1.99999999f); deviceDescription.setHeight(2.00000000f); deviceDescription.setColorIndex(15000); Assert.assertTrue(aspectRatio.matches(deviceDescription)); Assert.assertTrue(minMonochrome.matches(deviceDescription)); Assert.assertFalse(minColorIndex.matches(deviceDescription)); Assert.assertFalse(minColor.matches(deviceDescription)); Assert.assertFalse(resolution.matches(deviceDescription)); Assert.assertFalse(defaultExpression.matches(deviceDescription)); Assert.assertFalse(colorIndex.matches(deviceDescription)); }