@Test public void width_percent_scales_keeps_ratio() { final Rect rect = new Rect(0, 0, 10, 20); assertEquals( new Rect(0, 0, 50, 100), def.resolveImageSize( new ImageSize(new Dimension(50.F, UNIT_PERCENT), null), rect, 100, Float.NaN ) ); }
@Test public void width_text_size_relative_height_keeps_ratio() { final Rect rect = new Rect(0, 0, 15, 30); assertEquals( new Rect(0, 0, 10, 20), def.resolveImageSize( new ImageSize(new Dimension(1.F, UNIT_EM), null), rect, 42, 10.F ) ); }
@Test public void absolute_height_keeps_width_ratio() { final Rect rect = new Rect(0, 0, 50, 25); assertEquals( new Rect(0, 0, 100, 50), def.resolveImageSize( new ImageSize(null, new Dimension(50, "px")), rect, 200, Float.NaN ) ); }
@Test public void relative_text_size_height_keeps_width_ratio() { final Rect rect = new Rect(0, 0, 4, 12); assertEquals( new Rect(0, 0, 10, 30), def.resolveImageSize( new ImageSize(null, new Dimension(3.F, UNIT_EM)), rect, 40, 10.F ) ); } }
@Test public void width_height_from_style() { final String style = "width: 123; height: 321"; assertImageSize( new ImageSize(dimension(123, null), dimension(321, null)), impl.parse(Collections.singletonMap("style", style)) ); }
@Test public void width_height_text_size_relative() { final Rect rect = new Rect(0, 0, 100, 200); assertEquals( new Rect(0, 0, 20, 40), def.resolveImageSize( new ImageSize(new Dimension(2.f, UNIT_EM), new Dimension(4.F, UNIT_EM)), rect, 999, 10.F ) ); }
@Test public void height_percent_not_used() { final Rect rect = new Rect(1, 2, 3, 4); assertEquals( rect, def.resolveImageSize( new ImageSize(null, new Dimension(100.F, UNIT_PERCENT)), rect, -1, Float.NaN ) ); }
@Test public void unknown_dimension_considered_absolute() { final Rect rect = new Rect(0, 0, 22, 33); assertEquals( new Rect(0, 0, 7, 9), def.resolveImageSize( new ImageSize(new Dimension(7, "width"), new Dimension(9, "height")), rect, 90, Float.NaN ) ); }
@Test public void style_has_higher_priority_width() { // if property is found in styles, do not lookup raw attribute final Map<String, String> attributes = new HashMap<String, String>() {{ put("style", "width: 43"); put("width", "991"); }}; assertImageSize( new ImageSize(dimension(43, null), null), impl.parse(attributes) ); }
@Test public void style_has_higher_priority_height() { // if property is found in styles, do not lookup raw attribute final Map<String, String> attributes = new HashMap<String, String>() {{ put("style", "height: 177"); put("height", "8"); }}; assertImageSize( new ImageSize(null, dimension(177, null)), impl.parse(attributes) ); }
@Test public void no_dimensions_await() { // when drawable have no known dimensions yet, it will await for them final AsyncDrawable drawable = new AsyncDrawable("", mock(AsyncDrawable.Loader.class), imageSizeResolver, new ImageSize(new ImageSize.Dimension(100.F, "%"), null)); final Drawable result = new AbstractDrawable(); result.setBounds(0, 0, 0, 0); assertFalse(drawable.hasResult()); drawable.setResult(result); assertTrue(drawable.hasResult()); assertTrue(result.getBounds().isEmpty()); drawable.initWithKnownDimensions(100, 1); assertEquals( new Rect(0, 0, 100, 0), result.getBounds() ); }
@Test public void width_style_height_attributes() { final Map<String, String> attributes = new HashMap<String, String>() {{ put("style", "width: 99"); put("height", "7"); }}; assertImageSize( new ImageSize(dimension(99, null), dimension(7, null)), impl.parse(attributes) ); }
@Test public void height_style_width_attributes() { final Map<String, String> attributes = new HashMap<String, String>() {{ put("style", "height: 15"); put("width", "88"); }}; assertImageSize( new ImageSize(dimension(88, null), dimension(15, null)), impl.parse(attributes) ); }
@Test public void non_empty_styles_width_height_attributes() { final Map<String, String> attributes = new HashMap<String, String>() {{ put("style", "key1: value1; width0: 123; height0: 99"); put("width", "40"); put("height", "77"); }}; assertImageSize( new ImageSize(dimension(40, null), dimension(77, null)), impl.parse(attributes) ); }
@Override public void visit(CustomBlock customBlock) { if (!(customBlock instanceof JLatexMathBlock)) { super.visit(customBlock); return; } final String latex = ((JLatexMathBlock) customBlock).latex(); final int length = builder.length(); builder.append(latex); SpannableBuilder.setSpans( builder, configuration.factory().image( configuration.theme(), JLatexMathMedia.makeDestination(latex), configuration.asyncDrawableLoader(), configuration.imageSizeResolver(), new ImageSize(new ImageSize.Dimension(100, "%"), null), false ), length, builder.length() ); } };