Example #1
0
        public void Image_XmlSerialize_Test()
        {
            var texture = AssetManager.Load(@"Test\Default_color.dds") as Texture;
            var parent = new TestHelpers.RootElement();
            var expected = new Image(parent, AssetManager)
            {
                Texture = texture,
                DestRect = new Rectangle(0.1f, 0.2f, 0.4f, 0.5f),
                SourceRect = new Rectangle(1, 2, texture.Width - 1, texture.Height - 2)
            };

            var stream = new MemoryStream();
            using (var xmlWriter = XmlWriter.Create(stream))
            {
                expected.WriteXml(xmlWriter);
            }

            stream.Position = 0;
            var actual = new Image(parent, AssetManager);
            using (var xmlReader = XmlReader.Create(stream))
            {
                actual.ReadXml(xmlReader);
            }

            Assert.AreEqual(expected.Texture, texture);
            Assert.AreEqual(expected.DestRect, actual.DestRect);
            Assert.AreEqual(expected.AbsRect, actual.AbsRect);
            Assert.AreEqual(expected.SourceRect, actual.SourceRect);
        }
Example #2
0
        public void Element_GetVisualTree_Test()
        {
            var parent = new TestHelpers.RootElement();
            var element = new ElementMock(parent, AssetManager);
            var element2 = new ElementMock(element, AssetManager);
            var element3 = new ElementMock(element2, AssetManager);

            var expected = new Element[] { element, element2, element3 };
            Assert.IsTrue(expected.SequenceEqual(element.GetVisualTree()));
        }
Example #3
0
 public void Element_DefaultValues_Test()
 {
     var parent = new TestHelpers.RootElement();
     var element = new ElementMock(parent, AssetManager) { Name = "ElementMock" };
     Assert.AreEqual("ElementMock", element.Name);
     Assert.AreEqual(new Rectangle(0.0f, 0.0f, 1.0f, 1.0f), element.DestRect);
     var expectedAbsRect = new Rectangle(parent.AbsRect.Left + parent.AbsRect.Width * element.DestRect.Left,
                                         parent.AbsRect.Top + parent.AbsRect.Height * element.DestRect.Top,
                                         parent.AbsRect.Width * element.DestRect.Width,
                                         parent.AbsRect.Height * element.DestRect.Height);
     Assert.AreEqual(expectedAbsRect, element.AbsRect);
     Assert.IsTrue(element.IsLogical);
 }
Example #4
0
        public void Element_IsVisible_Test()
        {
            var parent = new TestHelpers.RootElement();
            var element = new ElementMock(parent, AssetManager);
            var element2 = new ElementMock(element, AssetManager);
            Assert.IsTrue(element.IsVisible);

            element.IsVisible = false;
            Assert.IsFalse(element2.IsVisible);
            TestHelpers.RenderSprite(Device, s => Assert.IsFalse((element as ISprite).Render(s)));

            var element3 = new ElementMock(element2, AssetManager);
            Assert.IsFalse(element3.IsVisible);
        }
Example #5
0
        public void Image_SomePropertiesChangeIfTextureChanged_Test()
        {
            var destRect = new Rectangle(0.1f, 0.2f, 0.4f, 0.5f);
            var sourceRect = new Rectangle(1, 2, 100, 100);

            var parent = new TestHelpers.RootElement();
            var img = new Image(parent, AssetManager)
            {
                DestRect = destRect,
                SourceRect = sourceRect
            };

            img.Texture = AssetManager.Load(@"Test\Default_color.dds") as Texture;
            Assert.AreEqual(destRect, img.DestRect);
            Assert.AreEqual(sourceRect, img.SourceRect);
        }
Example #6
0
        public void Element_RemoveFromParent_Test()
        {
            var parent = new TestHelpers.RootElement();
            var element = new ElementMock(parent, AssetManager);
            var element2 = new ElementMock(element, AssetManager);

            // Element whose parent is root cannot be removed
            Assert.IsFalse(element.RemoveFromParent());

            Assert.IsTrue(element2.RemoveFromParent());
        }
Example #7
0
        public void Element_ParentChangesPositionAndSize_Test()
        {
            var parent = new TestHelpers.RootElement();
            var element = new ElementMock(parent, AssetManager) { Name = "ElementMock" };

            parent.AbsRect = new Rectangle(10.0f, 20.0f, 110.0f, 120.0f);

            var expectedAbsRect = new Rectangle(parent.AbsRect.Left + parent.AbsRect.Width * element.DestRect.Left,
                                                parent.AbsRect.Top + parent.AbsRect.Height * element.DestRect.Top,
                                                parent.AbsRect.Width * element.DestRect.Width,
                                                parent.AbsRect.Height * element.DestRect.Height);
            Assert.AreEqual(expectedAbsRect, element.AbsRect);
        }
Example #8
0
        public void Element_MouseMove_Test()
        {
            var parent = new TestHelpers.RootElement();
            var element = new ElementMock(parent, AssetManager)
            {
                DestRect = new Rectangle(0.5f, 0.5f, 0.5f, 0.5f)
            };
            var elementPos = new Vector2(); // element-relative position
            var parentPos = new Vector2();  // parent(root)-relative position
            element.MouseMove += (o, e) =>
                {
                    elementPos = e.GetPosition(element);
                    parentPos = e.GetPosition(parent);
                };

            float moveX = 0.6f * parent.AbsRect.Width,
                  moveY = 0.7f * parent.AbsRect.Height;
            element.InjectMouseMoveEvent(moveX, moveY);
            Assert.AreEqual(moveX, parentPos.x);
            Assert.AreEqual(moveY, parentPos.y);
            Assert.AreEqual(moveX - element.AbsRect.Left, elementPos.x);
            Assert.AreEqual(moveY - element.AbsRect.Top, elementPos.y);
        }
Example #9
0
        public void Element_MouseEnterLeave_Test()
        {
            var parent = new TestHelpers.RootElement();
            var element = new ElementMock(parent, AssetManager)
            {
                DestRect = new Rectangle(0.5f, 0.5f, 0.5f, 0.5f)
            };
            bool entered = false, left = false;
            element.MouseEnter += ((o, e) => entered = true);
            element.MouseLeave += ((o, e) => left = true);

            element.InjectMouseMoveEvent(0.0f, 0.0f);
            Assert.IsFalse(entered);

            element.InjectMouseMoveEvent(element.AbsRect.Left + 1, element.AbsRect.Top + 1);
            Assert.IsTrue(entered);
            Assert.IsFalse(left);

            element.InjectMouseMoveEvent(0.0f, 0.0f);
            Assert.IsTrue(left);
        }
Example #10
0
        public void Button_XmlSerialize_Test()
        {
            var parent = new TestHelpers.RootElement();
            var expected = new Button(parent, AssetManager);
            var block = new TextBlock(expected, AssetManager) { Name = "Normal TextBlock" };
            expected.SetTemplate(ButtonState.Normal, block.Name);

            var stream = new MemoryStream();
            using (var xmlWriter = XmlWriter.Create(stream))
            {
                expected.WriteXml(xmlWriter);
            }

            stream.Position = 0;
            var actual = new Button(parent, AssetManager);
            using (var xmlReader = XmlReader.Create(stream))
            {
                actual.ReadXml(xmlReader);
            }

            Assert.AreEqual(block.Name, actual.GetTemplate(ButtonState.Normal).Name);
            Assert.IsNull(actual.GetTemplate(ButtonState.Hover));
        }