public async Task Png384Dpi() { var fileName = Path.Combine(_artifacts, "png384.sprite"); var imgFile = fileName + ".png"; var cssFile = fileName + ".css"; try { var doc = await SpriteDocument.FromFile(fileName); await _generator.Generate(doc); using (var image = Image.FromFile(imgFile)) { Assert.AreEqual(384, Math.Round(image.HorizontalResolution), "Not 384 DPI"); Assert.AreEqual(166, image.Height); // 16 + padding Assert.AreEqual(36, image.Width); // 16 * 6 + padding } string css = File.ReadAllText(cssFile); Assert.IsTrue(css.Contains(".png384.a"), "Sprite \"a.png\" not generated"); Assert.IsTrue(css.Contains("url('png384.sprite.png')"), "Incorrect url value"); Assert.IsTrue(css.Contains("display: inline-block"), "Incorrect custom style"); } finally { File.Delete(imgFile); File.Delete(cssFile); } }
public async Task Png96Dpi() { var fileName = Path.Combine(_artifacts, "png96.sprite"); var imgFile = fileName + ".jpg"; var lessFile = fileName + ".less"; try { var doc = await SpriteDocument.FromFile(fileName); await _generator.Generate(doc); using (var image = Image.FromFile(imgFile)) { Assert.AreEqual(56, image.Height); // 16 + padding Assert.AreEqual(236, image.Width); // 16 * 6 + padding } string less = File.ReadAllText(lessFile); Assert.IsTrue(less.Contains(".png96-a()"), "Sprite \"a.png\" not generated"); Assert.IsTrue(less.Contains("url('png96.sprite.jpg')"), "Incorrect url value"); Assert.IsTrue(less.Contains("margin: 0"), "Incorrect custom style"); } finally { File.Delete(imgFile); File.Delete(lessFile); } }
public async Task CacheBustHash() { var fileName = Path.Combine(_artifacts, "hash.sprite"); var imgFile = fileName + ".jpg"; var cssFile = fileName + ".css"; try { var doc = await SpriteDocument.FromFile(fileName); await _generator.Generate(doc); using (HashAlgorithm hash = new SHA256Managed()) { string imgHash = Convert.ToBase64String(hash.ComputeHash(File.ReadAllBytes(imgFile))); string css = File.ReadAllText(cssFile); Assert.IsTrue(css.Contains($"?hash={WebUtility.UrlEncode(imgHash)}"), "Sprite hash not generated"); } } finally { File.Delete(imgFile); File.Delete(cssFile); } }
public static async Task GenerateSpriteAsync(string fileName) { try { SpriteDocument doc = await SpriteDocument.FromFile(fileName); await GenerateSpriteAsync(doc); } catch (SpriteParseException ex) { MessageBox.Show(ex.Message, Vsix.Name, MessageBoxButtons.OK, MessageBoxIcon.Error); ProjectHelpers.DTE.ItemOperations.OpenFile(fileName); } catch (Exception ex) { Logger.Log(ex); } }
public async Task CreateDocument() { var img = Path.Combine(_artifacts, "images/96dpi/a.png"); var original = new SpriteDocument(_fileName, new[] { img }); original.Output = ImageType.Jpg; await original.Save(); var read = await SpriteDocument.FromFile(_fileName); Assert.AreEqual(original.Orientation, read.Orientation); Assert.AreEqual(original.FileName, read.FileName); Assert.AreEqual(original.Output, read.Output); Assert.AreEqual(original.Images.Count(), read.Images.Count()); var input = new FileInfo(Path.Combine(_artifacts, "images/96dpi/a.png")).FullName; Assert.AreEqual(input, read.ToAbsoluteImages().First().Value); }
public async Task FromFileFail() { await SpriteDocument.FromFile("doesntexist.json"); }