public void TestMeasureOverride() { var rand = new Random(); var imageSize = new Vector3(100, 50, 0); var sprite = new Sprite { Region = new Rectangle(0, 0, (int)imageSize.X, (int)imageSize.Y), Borders = new Vector4(1, 2, 3, 4) }; var image = new ImageElement { Source = (SpriteFromTexture)sprite }; // Fixed sized image.StretchType = StretchType.None; image.Measure(rand.NextVector3()); Assert.Equal(imageSize, image.DesiredSizeWithMargins); // Uniform sized image.StretchType = StretchType.Uniform; image.Measure(new Vector3(50)); Assert.Equal(new Vector3(50, 25, 0), image.DesiredSizeWithMargins); // Uniform to fill sized image.StretchType = StretchType.UniformToFill; image.Measure(new Vector3(50)); Assert.Equal(new Vector3(100, 50, 0), image.DesiredSizeWithMargins); // Fill on stretch image.StretchType = StretchType.FillOnStretch; image.Measure(new Vector3(50)); Assert.Equal(new Vector3(50, 25, 0), image.DesiredSizeWithMargins); // Fill image.StretchType = StretchType.Fill; image.Measure(new Vector3(50)); Assert.Equal(new Vector3(50, 50, 0), image.DesiredSizeWithMargins); // Test minimal size due to borders image.StretchType = StretchType.Fill; image.Measure(new Vector3()); Assert.Equal(new Vector3(4, 6, 0), image.DesiredSizeWithMargins); // Test with infinite value for (var type = 0; type < 5; ++type) { TestMeasureOverrideInfiniteValues((StretchType)type); } // Test stretch directions image.StretchType = StretchType.Fill; image.StretchDirection = StretchDirection.DownOnly; image.Measure(new Vector3(200, 300, 220)); Assert.Equal(new Vector3(100, 50, 0), image.DesiredSizeWithMargins); image.Measure(new Vector3(20, 15, 30)); Assert.Equal(new Vector3(20, 15, 0), image.DesiredSizeWithMargins); image.StretchDirection = StretchDirection.UpOnly; image.Measure(new Vector3(200, 300, 220)); Assert.Equal(new Vector3(200, 300, 0), image.DesiredSizeWithMargins); image.Measure(new Vector3(20, 30, 22)); Assert.Equal(new Vector3(100, 50, 0), image.DesiredSizeWithMargins); }
private void TestMeasureOverrideInfiniteValues(StretchType stretch) { var imageSize = new Vector3(100, 50, 0); var sprite = new Sprite { Region = new Rectangle(0, 0, (int)imageSize.X, (int)imageSize.Y), Borders = new Vector4(1, 2, 3, 4) }; var image = new ImageElement { Source = (SpriteFromTexture)sprite, StretchType = stretch }; image.Measure(new Vector3(float.PositiveInfinity)); Assert.Equal(imageSize, image.DesiredSizeWithMargins); image.Measure(new Vector3(150, float.PositiveInfinity, 10)); Assert.Equal(stretch == StretchType.None ? imageSize : new Vector3(150, 75, 0), image.DesiredSizeWithMargins); }