Skip to content

Commit

Permalink
Update tests to support 'sprite-scale' directive
Browse files Browse the repository at this point in the history
Added a few new tests to validate the sprite-scale directive is working
as expected.
  • Loading branch information
dpolivy committed Feb 15, 2013
1 parent 885e4b5 commit beeecf8
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 6 deletions.
57 changes: 57 additions & 0 deletions src/test/java/org/carrot2/labs/smartsprites/SpriteBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,63 @@ public void testNonDefaultOutputDir() throws FileNotFoundException, IOException
org.carrot2.util.FileUtils.deleteThrowingExceptions(absoluteSpriteFile);
}

@Test
public void testScaledSpriteImage() throws FileNotFoundException, IOException
{
final File testDir = testDir("scaled-sprite");
final File documentRootDir = testDir("scaled-sprite");
buildSprites(filesystemSmartSpritesParameters(testDir, null,
documentRootDir, MessageLevel.INFO,
SmartSpritesParameters.DEFAULT_CSS_FILE_SUFFIX,
SmartSpritesParameters.DEFAULT_SPRITE_PNG_DEPTH,
SmartSpritesParameters.DEFAULT_SPRITE_PNG_IE6,
SmartSpritesParameters.DEFAULT_CSS_FILE_ENCODING));

assertThat(processedCss()).hasSameContentAs(expectedCss());

final File absoluteSpriteFile = new File(documentRootDir, "img/absolute.png");
assertThat(absoluteSpriteFile).exists();
org.fest.assertions.Assertions.assertThat(ImageIO.read(absoluteSpriteFile))
.hasSize(new Dimension(17, 17));

assertThat(messages).doesNotHaveMessagesOfLevel(MessageLevel.WARN);

org.carrot2.util.FileUtils.deleteThrowingExceptions(absoluteSpriteFile);
}

@Test
public void testFractionalScaledSpriteImage() throws FileNotFoundException, IOException
{
final File testDir = testDir("scaled-sprite-fractional");
final File documentRootDir = testDir("scaled-sprite-fractional");
buildSprites(filesystemSmartSpritesParameters(testDir, null,
documentRootDir, MessageLevel.INFO,
SmartSpritesParameters.DEFAULT_CSS_FILE_SUFFIX,
SmartSpritesParameters.DEFAULT_SPRITE_PNG_DEPTH,
SmartSpritesParameters.DEFAULT_SPRITE_PNG_IE6,
SmartSpritesParameters.DEFAULT_CSS_FILE_ENCODING));

assertThat(processedCss()).hasSameContentAs(expectedCss());

final File absoluteSpriteFile = new File(documentRootDir, "img/absolute.png");
assertThat(absoluteSpriteFile).exists();
org.fest.assertions.Assertions.assertThat(ImageIO.read(absoluteSpriteFile))
.hasSize(new Dimension(17, 17));

// assertThat(messages).doesNotHaveMessagesOfLevel(MessageLevel.WARN);

assertThat(messages).isEquivalentTo(
Message.MessageLevel.WARN,
new Message(Message.MessageLevel.WARN,
Message.MessageType.IMAGE_FRACTIONAL_SCALE_VALUE, null, 8, "../img/web.gif",
8.5f, 8.5f),
new Message(Message.MessageLevel.WARN,
Message.MessageType.FRACTIONAL_SCALE_VALUE, null, 8, "absolute",
8.5f, 8.5f));

org.carrot2.util.FileUtils.deleteThrowingExceptions(absoluteSpriteFile);
}

@Test
public void testCssOutputDir() throws FileNotFoundException, IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,22 @@ public void testNegativeMarginValues()
assertEquals(0, directive.spriteLayoutProperties.marginBottom);
}

@Test
public void testSpriteScalingProperty()
{
final SpriteImageDirective directive = SpriteImageDirective
.parse(
"sprite: sprite; sprite-image: url('../sprite.png'); sprite-layout: horizontal; "
+ "sprite-scale: 2;",
messageLog);

assertThat(messages).doesNotHaveMessagesOfLevel(MessageLevel.WARN);
assertNotNull(directive);
assertEquals(directive.spriteId, "sprite");
assertEquals(directive.imagePath, "../sprite.png");
assertEquals(directive.scaleRatio, 2f);
}

private void checkUidType(String uidDeclaration, SpriteUidType expectedUidType)
{
final SpriteImageDirective directive = SpriteImageDirective.parse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public class SpriteReferenceDirectiveTest extends TestWithMemoryMessageSink
{
private static final SpriteImageDirective VERTICAL_SPRITE_IMAGE_DIRECTIVE = new SpriteImageDirective(
"vsprite", "sprite.png", SpriteImageLayout.VERTICAL, SpriteImageFormat.PNG,
Ie6Mode.AUTO, Color.WHITE, SpriteUidType.NONE);
Ie6Mode.AUTO, Color.WHITE, SpriteUidType.NONE, 1);

private static final SpriteImageDirective HORIZONTAL_SPRITE_IMAGE_DIRECTIVE = new SpriteImageDirective(
"hsprite", "hsprite.png", SpriteImageLayout.HORIZONTAL, SpriteImageFormat.PNG,
Ie6Mode.AUTO, Color.WHITE, SpriteUidType.NONE);
Ie6Mode.AUTO, Color.WHITE, SpriteUidType.NONE, 1);

private static final SpriteImageDirective VERTICAL_SPRITE_IMAGE_DIRECTIVE_WITH_LAYOUT = new SpriteImageDirective(
"vsprite", "sprite.png", SpriteImageLayout.VERTICAL, SpriteImageFormat.PNG,
Ie6Mode.AUTO, Color.WHITE, SpriteUidType.NONE, new SpriteLayoutProperties(
Ie6Mode.AUTO, Color.WHITE, SpriteUidType.NONE, 1, new SpriteLayoutProperties(
SpriteAlignment.REPEAT, 1, 2, 3, 4));

private static final Map<String, SpriteImageDirective> SPRITE_IMAGE_DIRECTIVES = ImmutableMap
Expand Down Expand Up @@ -288,23 +288,23 @@ public void testOverriddenSpriteLayoutFromSpriteImageDirective()

assertThat(messages).isEmpty();
}

@Test
public void testNegativeMarginValues()
{
final SpriteReferenceDirective directive = SpriteReferenceDirective
.parse(
"sprite-ref: vlsprite; sprite-alignment: right; sprite-margin-left: -10px; sprite-margin-right: 20; sprite-margin-top: 30px; sprite-margin-bottom: -40;",
SPRITE_IMAGE_DIRECTIVES, messageLog);

assertNotNull(directive);
assertEquals("vlsprite", directive.spriteRef);
assertEquals(SpriteAlignment.RIGHT, directive.spriteLayoutProperties.alignment);
assertEquals(0, directive.spriteLayoutProperties.marginLeft);
assertEquals(20, directive.spriteLayoutProperties.marginRight);
assertEquals(30, directive.spriteLayoutProperties.marginTop);
assertEquals(0, directive.spriteLayoutProperties.marginBottom);

assertThat(messages).contains(
new Message(Message.MessageLevel.WARN,
Message.MessageType.IGNORING_NEGATIVE_MARGIN_VALUE, null, 0,
Expand Down
11 changes: 11 additions & 0 deletions test/scaled-sprite-fractional/css/style-expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* A test of writing a scaled image sprite for highdpi displays.
*/

div.web {
width: 17px;
height: 17px;
background-image: url('/img/absolute.png');
background-position: -0px top;
background-size: 9px 9px;
}
10 changes: 10 additions & 0 deletions test/scaled-sprite-fractional/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* A test of writing a scaled image sprite for highdpi displays.
*/

/** sprite: absolute; sprite-image: url('/img/absolute.png'); sprite-layout: horizontal; sprite-scale: 2; */
div.web {
width: 17px;
height: 17px;
background-image: url(../img/web.gif); /** sprite-ref: absolute */
}
Binary file added test/scaled-sprite-fractional/img/web.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions test/scaled-sprite/css/style-expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* A test of writing a scaled image sprite for highdpi displays.
*/

div.web {
width: 17px;
height: 17px;
background-image: url('/img/absolute.png');
background-position: -0px top;
background-size: 10px 10px;
}
10 changes: 10 additions & 0 deletions test/scaled-sprite/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* A test of writing a scaled image sprite for highdpi displays.
*/

/** sprite: absolute; sprite-image: url('/img/absolute.png'); sprite-layout: horizontal; sprite-scale: 1.7; */
div.web {
width: 17px;
height: 17px;
background-image: url(../img/web.gif); /** sprite-ref: absolute */
}
Binary file added test/scaled-sprite/img/web.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit beeecf8

Please sign in to comment.