Пример #1
0
        public void Format_Handles_StretchAlignment()
        {
            var layoutText = @"<TestWidget MyAlignment=""Stretch,Stretch"" />";
            var target     = (TestWidget)TestUtils.LoadFromText(this, layoutText, "(root)");

            target.Prepare(null);
            target.UpdateFormatting(new Vector2(5, 8));
            Assert.AreEqual(new Vector2(5, 8), target.Size);
            Assert.AreEqual(new Vector2(0, 0), target.Offset);

            // margins should constrain the stretch
            layoutText = @"<TestWidget MyAlignment=""Stretch,Stretch"" Margin=""1"" />";
            target     = (TestWidget)TestUtils.LoadFromText(this, layoutText, "(root)");
            target.Prepare(null);
            target.UpdateFormatting(new Vector2(5, 8));
            Assert.AreEqual(new Vector2(3, 6), target.Size);
            Assert.AreEqual(new Vector2(1, 1), target.Offset);

            layoutText = @"<TestWidget MyAlignment=""Stretch,Stretch"" Margin=""1.5,2"" />";
            target     = (TestWidget)TestUtils.LoadFromText(this, layoutText, "(root)");
            target.Prepare(null);
            target.UpdateFormatting(new Vector2(5, 8));
            Assert.AreEqual(new Vector2(2, 4), target.Size);
            Assert.AreEqual(new Vector2(1.5f, 2), target.Offset);

            layoutText = @"<TestWidget MyAlignment=""Stretch,Stretch"" Margin="",,.5"" />";
            target     = (TestWidget)TestUtils.LoadFromText(this, layoutText, "(root)");
            target.Prepare(null);
            target.UpdateFormatting(new Vector2(5, 8));
            Assert.AreEqual(new Vector2(4.5f, 8), target.Size);
            Assert.AreEqual(new Vector2(0, 0), target.Offset);

            // Should throw if Size is specified with stretch
            layoutText = @"<TestWidget MyAlignment=""Stretch,Stretch"" Size=""1,1"" />";
            target     = (TestWidget)TestUtils.LoadFromText(this, layoutText, "(root)");
            target.Prepare(null);
            Assert.ThrowsException <ArgumentException>(() => target.UpdateFormatting(new Vector2(5, 8)));

            layoutText = @"<TestWidget MyAlignment="",Stretch"" Size="",1"" />";
            target     = (TestWidget)TestUtils.LoadFromText(this, layoutText, "(root)");
            target.Prepare(null);
            Assert.ThrowsException <ArgumentException>(() => target.UpdateFormatting(new Vector2(5, 8)));

            layoutText = @"<TestWidget MyAlignment=""Stretch,"" Size=""1,"" />";
            target     = (TestWidget)TestUtils.LoadFromText(this, layoutText, "(root)");
            target.Prepare(null);
            Assert.ThrowsException <ArgumentException>(() => target.UpdateFormatting(new Vector2(5, 8)));

            // Specifying no alignment and a size should default to Left or Top
            layoutText = @"<TestWidget MyAlignment="",Stretch"" Size=""1,"" />";
            target     = (TestWidget)TestUtils.LoadFromText(this, layoutText, "(root)");
            target.Prepare(null);
            target.UpdateFormatting(new Vector2(5, 8));
            Assert.AreEqual(null, target.MyAlignment.X);
            Assert.AreEqual(Alignment.Stretch, target.MyAlignment.Y);
            Assert.AreEqual(new Vector2(0, 0), target.Offset);

            layoutText = @"<TestWidget MyAlignment=""Stretch,"" Size="",1"" />";
            target     = (TestWidget)TestUtils.LoadFromText(this, layoutText, "(root)");
            target.Prepare(null);
            target.UpdateFormatting(new Vector2(5, 8));
            Assert.AreEqual(Alignment.Stretch, target.MyAlignment.X);
            Assert.AreEqual(null, target.MyAlignment.Y);
            Assert.AreEqual(new Vector2(0, 0), target.Offset);
        }