Exemplo n.º 1
0
        public void Bounds_Should_Be_Set_After_Layout_Pass()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();
                PerspexLocator.CurrentMutable.Bind <ILayoutManager>().ToConstant(new LayoutManager());

                var impl = new Mock <ITopLevelImpl>();
                impl.SetupProperty(x => x.ClientSize);
                impl.SetupProperty(x => x.Resized);

                var target = new TestTopLevel(impl.Object)
                {
                    Template = CreateTemplate(),
                    Content  = new TextBlock
                    {
                        Width  = 321,
                        Height = 432,
                    }
                };

                target.LayoutManager.ExecuteLayoutPass();

                Assert.Equal(new Rect(0, 0, 321, 432), target.Bounds);
            }
        }
Exemplo n.º 2
0
        private static IDisposable CreateServices()
        {
            var result = PerspexLocator.EnterScope();

            var styles = new Styles
            {
                new Style(x => x.OfType <PopupRoot>())
                {
                    Setters = new[]
                    {
                        new Setter(TemplatedControl.TemplateProperty, new ControlTemplate <PopupRoot>(PopupRootTemplate)),
                    }
                },
            };

            var globalStyles = new Mock <IGlobalStyles>();

            globalStyles.Setup(x => x.Styles).Returns(styles);


            PerspexLocator.CurrentMutable
            .Bind <ILayoutManager>().ToTransient <LayoutManager>()
            .Bind <IGlobalStyles>().ToFunc(() => globalStyles.Object)
            .Bind <IPopupImpl>().ToConstant(new Mock <IPopupImpl>().Object)
            .Bind <IStyler>().ToTransient <Styler>();

            return(result);
        }
Exemplo n.º 3
0
        public void Invalidating_Child_Should_Remeasure_Parent()
        {
            var layoutManager = new LayoutManager();

            using (PerspexLocator.EnterScope())
            {
                PerspexLocator.CurrentMutable.Bind <ILayoutManager>().ToConstant(layoutManager);

                Border     border;
                StackPanel panel;

                var root = new TestLayoutRoot
                {
                    Child = panel = new StackPanel
                    {
                        Children = new Controls.Controls
                        {
                            (border = new Border())
                        }
                    }
                };

                layoutManager.ExecuteInitialLayoutPass(root);
                Assert.Equal(new Size(0, 0), root.DesiredSize);

                border.Width  = 100;
                border.Height = 100;

                layoutManager.ExecuteLayoutPass();
                Assert.Equal(new Size(100, 100), panel.DesiredSize);
            }
        }
Exemplo n.º 4
0
        public void Should_Measure_Expander_Triangle_With_Stroke_Correctly()
        {
            using (PerspexLocator.EnterScope())
            {
                Direct2D1Platform.Initialize();

                var target = new Path
                {
                    Data                = StreamGeometry.Parse("M 0 2 L 4 6 L 0 10 Z"),
                    Stroke              = Brushes.Black,
                    StrokeThickness     = 2,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment   = VerticalAlignment.Top,
                    UseLayoutRounding   = false,
                };

                target.Measure(new Size(100, 100));
                target.Arrange(new Rect(0, 0, 100, 100));

                // Measured geometry with stroke of 2px is:
                //
                //     {-1, -0.414, 6.414, 12.828} (see GeometryTests)
                //
                // With origin at 0,0 the bounds should equal:
                //
                //     Assert.Equal(new Rect(0, 0, 5.414, 12.414), target.Bounds, compare);
                //
                // However Path.Measure doesn't correctly handle strokes currently, so testing for
                // the (incorrect) current output for now...
                Assert.Equal(new Rect(0, 0, 4, 10), target.Bounds, Compare);
            }
        }
Exemplo n.º 5
0
        public static IDisposable Start(TestServices services = null)
        {
            var scope = PerspexLocator.EnterScope();
            var app   = new UnitTestApplication(services);

            return(scope);
        }
Exemplo n.º 6
0
        public void Test_ScrollViewer_With_TextBlock()
        {
            using (var context = PerspexLocator.EnterScope())
            {
                RegisterServices();

                ScrollViewer scrollViewer;
                TextBlock    textBlock;

                var window = new Window()
                {
                    Width         = 800,
                    Height        = 600,
                    SizeToContent = SizeToContent.WidthAndHeight,
                    Content       = scrollViewer = new ScrollViewer
                    {
                        Width  = 200,
                        Height = 200,
                        CanScrollHorizontally = true,
                        HorizontalAlignment   = HorizontalAlignment.Center,
                        VerticalAlignment     = VerticalAlignment.Center,
                        Content = textBlock = new TextBlock
                        {
                            Width  = 400,
                            Height = 400,
                            Text   = "Hello World!",
                        },
                    }
                };

                window.LayoutManager.ExecuteLayoutPass();

                Assert.Equal(new Size(800, 600), window.Bounds.Size);
                Assert.Equal(new Size(200, 200), scrollViewer.Bounds.Size);
                Assert.Equal(new Point(300, 200), Position(scrollViewer));
                Assert.Equal(new Size(400, 400), textBlock.Bounds.Size);

                var scrollBars = scrollViewer.GetTemplateChildren().OfType <ScrollBar>().ToList();
                var presenters = scrollViewer.GetTemplateChildren().OfType <ScrollContentPresenter>().ToList();

                Assert.Equal(2, scrollBars.Count);
                Assert.Equal(1, presenters.Count);

                var presenter = presenters[0];
                Assert.Equal(new Size(190, 190), presenter.Bounds.Size);

                var horzScroll = scrollBars.Single(x => x.Orientation == Orientation.Horizontal);
                var vertScroll = scrollBars.Single(x => x.Orientation == Orientation.Vertical);

                Assert.True(horzScroll.IsVisible);
                Assert.True(vertScroll.IsVisible);
                Assert.Equal(new Size(190, 10), horzScroll.Bounds.Size);
                Assert.Equal(new Size(10, 190), vertScroll.Bounds.Size);
                Assert.Equal(new Point(0, 190), Position(horzScroll));
                Assert.Equal(new Point(190, 0), Position(vertScroll));
            }
        }
Exemplo n.º 7
0
        private IDisposable RegisterServices()
        {
            var result          = PerspexLocator.EnterScope();
            var fixture         = new Fixture().Customize(new AutoMoqCustomization());
            var renderInterface = fixture.Create <IPlatformRenderInterface>();

            PerspexLocator.CurrentMutable.Bind <IPlatformRenderInterface>().ToConstant(renderInterface);

            return(result);
        }
Exemplo n.º 8
0
        public void Should_Measure_Expander_Triangle_Correctly()
        {
            using (PerspexLocator.EnterScope())
            {
                Direct2D1Platform.Initialize();

                var target = StreamGeometry.Parse("M 0 2 L 4 6 L 0 10 Z");

                Assert.Equal(new Rect(0, 2, 4, 8), target.Bounds, Compare);
            }
        }
Exemplo n.º 9
0
        public void Should_Measure_Expander_Triangle_With_Stroke_Correctly()
        {
            using (PerspexLocator.EnterScope())
            {
                Direct2D1Platform.Initialize();

                var target = StreamGeometry.Parse("M 0 2 L 4 6 L 0 10 Z");

                Assert.Equal(new Rect(-1, -0.414, 6.414, 12.828), target.GetRenderBounds(2), Compare);
            }
        }
Exemplo n.º 10
0
        public void Name_Cannot_Be_Set_After_Added_To_Logical_Tree()
        {
            using (PerspexLocator.EnterScope())
            {
                var root  = new TestRoot();
                var child = new Border();

                root.Child = child;

                Assert.Throws <InvalidOperationException>(() => child.Name = "foo");
            }
        }
Exemplo n.º 11
0
        public void Adding_To_Logical_Tree_Should_Register_With_NameScope()
        {
            using (PerspexLocator.EnterScope())
            {
                var root  = new TestRoot();
                var child = new Border();

                child.Name = "foo";
                root.Child = child;

                Assert.Same(root.FindControl <Border>("foo"), child);
            }
        }
Exemplo n.º 12
0
        public void TabItem_Templates_Should_Be_Set_Before_TabItem_ApplyTemplate()
        {
            var collection = new[]
            {
                new TabItem
                {
                    Name    = "first",
                    Content = "foo",
                },
                new TabItem
                {
                    Name    = "second",
                    Content = "bar",
                },
                new TabItem
                {
                    Name    = "3rd",
                    Content = "barf",
                },
            };

            var template = new FuncControlTemplate <TabItem>(x => new Decorator());

            using (PerspexLocator.EnterScope())
            {
                PerspexLocator.CurrentMutable.Bind <IStyler>().ToConstant(new Styler());

                var root = new TestRoot
                {
                    Styles = new Styles
                    {
                        new Style(x => x.OfType <TabItem>())
                        {
                            Setters = new[]
                            {
                                new Setter(TemplatedControl.TemplateProperty, template)
                            }
                        }
                    },
                    Child = new TabControl
                    {
                        Template = new FuncControlTemplate <TabControl>(CreateTabControlTemplate),
                        Items    = collection,
                    }
                };
            }

            Assert.Same(collection[0].Template, template);
            Assert.Same(collection[1].Template, template);
            Assert.Same(collection[2].Template, template);
        }
Exemplo n.º 13
0
        public void Activate_Should_Call_Impl_Activate()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();

                var impl   = new Mock <ITopLevelImpl>();
                var target = new TestTopLevel(impl.Object);

                target.Activate();

                impl.Verify(x => x.Activate());
            }
        }
Exemplo n.º 14
0
        public void Height_Should_Not_Be_Set_On_Construction()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();

                var impl = new Mock <ITopLevelImpl>();
                impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));

                var target = new TestTopLevel(impl.Object);

                Assert.Equal(double.NaN, target.Height);
            }
        }
Exemplo n.º 15
0
        public void Layout_Pass_Should_Not_Be_Automatically_Scheduled()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();

                var impl   = new Mock <ITopLevelImpl>();
                var target = new TestTopLevel(impl.Object);

                // The layout pass should be scheduled by the derived class.
                var layoutManagerMock = Mock.Get(target.LayoutManager);
                layoutManagerMock.Verify(x => x.ExecuteLayoutPass(), Times.Never);
            }
        }
Exemplo n.º 16
0
        public void Adding_Control_To_IRenderRoot_Should_Style_Control()
        {
            using (PerspexLocator.EnterScope())
            {
                var root   = new TestRoot();
                var target = new Control();
                var styler = new Mock <IStyler>();

                PerspexLocator.CurrentMutable.Bind <IStyler>().ToConstant(styler.Object);

                root.Child = target;

                styler.Verify(x => x.ApplyStyles(target), Times.Once());
            }
        }
Exemplo n.º 17
0
        public void Width_And_Height_Should_Not_Be_Set_After_Layout_Pass()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();

                var impl = new Mock <ITopLevelImpl>();
                impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));

                var target = new TestTopLevel(impl.Object);
                target.LayoutManager.ExecuteLayoutPass();

                Assert.Equal(double.NaN, target.Width);
                Assert.Equal(double.NaN, target.Height);
            }
        }
Exemplo n.º 18
0
        public void Binding_Should_Be_Assigned_To_ItemsSource_Instead_Of_Bound()
        {
            using (PerspexLocator.EnterScope())
            {
                PerspexLocator.CurrentMutable
                .Bind <IPclPlatformWrapper>()
                .ToConstant(Mock.Of <IPclPlatformWrapper>());

                var xaml      = "<DataTemplates xmlns='https://github.com/perspex'><TreeDataTemplate ItemsSource='{Binding}'/></DataTemplates>";
                var loader    = new PerspexXamlLoader();
                var templates = (DataTemplates)loader.Load(xaml);
                var template  = (TreeDataTemplate)(templates.First());

                Assert.IsType <Binding>(template.ItemsSource);
            }
        }
Exemplo n.º 19
0
        public void Binding_Should_Be_Assigned_To_Setter_Value_Instead_Of_Bound()
        {
            using (PerspexLocator.EnterScope())
            {
                PerspexLocator.CurrentMutable
                .Bind <IPclPlatformWrapper>()
                .ToConstant(Mock.Of <IPclPlatformWrapper>());

                var xaml   = "<Style xmlns='https://github.com/perspex'><Setter Value='{Binding}'/></Style>";
                var loader = new PerspexXamlLoader();
                var style  = (Style)loader.Load(xaml);
                var setter = (Setter)(style.Setters.First());

                Assert.IsType <Binding>(setter.Value);
            }
        }
Exemplo n.º 20
0
        public void Name_Can_Be_Set_While_Initializing()
        {
            using (PerspexLocator.EnterScope())
            {
                var root  = new TestRoot();
                var child = new Border();

                ((ISupportInitialize)child).BeginInit();
                root.Child = child;
                child.Name = "foo";
                Assert.Null(root.FindControl <Border>("foo"));
                ((ISupportInitialize)child).EndInit();

                Assert.Same(root.FindControl <Border>("foo"), child);
            }
        }
Exemplo n.º 21
0
        public void StyleDetach_Is_Triggered_When_Control_Removed_From_Logical_Tree()
        {
            using (PerspexLocator.EnterScope())
            {
                var root  = new TestRoot();
                var child = new Border();

                root.Child = child;

                bool styleDetachTriggered = false;
                ((IStyleable)child).StyleDetach.Subscribe(_ => styleDetachTriggered = true);
                root.Child = null;

                Assert.True(styleDetachTriggered);
            }
        }
Exemplo n.º 22
0
        public void Adding_Top_Level_As_Child_Should_Throw_Exception()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();

                var impl = new Mock <ITopLevelImpl>();
                impl.SetupAllProperties();
                var target = new TestTopLevel(impl.Object);
                var child  = new TestTopLevel(impl.Object);

                target.Template = CreateTemplate();
                target.Content  = child;

                Assert.Throws <InvalidOperationException>(() => target.ApplyTemplate());
            }
        }
Exemplo n.º 23
0
        public void HotKeyManager_Should_Register_And_Unregister_Key_Binding()
        {
            using (PerspexLocator.EnterScope())
            {
                var windowImpl = new Mock <IWindowImpl>();
                var styler     = new Mock <Styler>();

                PerspexLocator.CurrentMutable
                .Bind <IWindowImpl>().ToConstant(windowImpl.Object)
                .Bind <IStyler>().ToConstant(styler.Object);

                var gesture1 = new KeyGesture {
                    Key = Key.A, Modifiers = InputModifiers.Control
                };
                var gesture2 = new KeyGesture {
                    Key = Key.B, Modifiers = InputModifiers.Control
                };

                var tl     = new Window();
                var button = new Button();
                tl.Content  = button;
                tl.Template = CreateWindowTemplate();
                tl.ApplyTemplate();

                HotKeyManager.SetHotKey(button, gesture1);

                Assert.Equal(gesture1, tl.KeyBindings[0].Gesture);

                HotKeyManager.SetHotKey(button, gesture2);
                Assert.Equal(gesture2, tl.KeyBindings[0].Gesture);

                tl.Content = null;
                tl.Presenter.ApplyTemplate();

                Assert.Empty(tl.KeyBindings);

                tl.Content = button;
                tl.Presenter.ApplyTemplate();

                Assert.Equal(gesture2, tl.KeyBindings[0].Gesture);

                HotKeyManager.SetHotKey(button, null);
                Assert.Empty(tl.KeyBindings);
            }
        }
Exemplo n.º 24
0
        public void Styles_Not_Applied_Until_Initialization_Finished()
        {
            using (PerspexLocator.EnterScope())
            {
                var root   = new TestRoot();
                var child  = new Border();
                var styler = new Mock <IStyler>();

                PerspexLocator.CurrentMutable.Bind <IStyler>().ToConstant(styler.Object);

                ((ISupportInitialize)child).BeginInit();
                root.Child = child;
                styler.Verify(x => x.ApplyStyles(It.IsAny <IStyleable>()), Times.Never());

                ((ISupportInitialize)child).EndInit();
                styler.Verify(x => x.ApplyStyles(child), Times.Once());
            }
        }
Exemplo n.º 25
0
        public void Width_And_Height_Should_Be_Set_After_Window_Resize_Notification()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();

                var impl = new Mock <ITopLevelImpl>();
                impl.SetupAllProperties();
                impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));

                // The user has resized the window, so we can no longer auto-size.
                var target = new TestTopLevel(impl.Object);
                impl.Object.Resized(new Size(100, 200));

                Assert.Equal(100, target.Width);
                Assert.Equal(200, target.Height);
            }
        }
Exemplo n.º 26
0
        public void Impl_Deactivate_Should_Call_Raise_Activated_Event()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();

                var impl = new Mock <ITopLevelImpl>();
                impl.SetupAllProperties();

                bool raised = false;
                var  target = new TestTopLevel(impl.Object);
                target.Deactivated += (s, e) => raised = true;

                impl.Object.Deactivated();

                Assert.True(raised);
            }
        }
Exemplo n.º 27
0
        public void Render_Should_Be_Scheduled_After_Layout_Pass()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();
                var completed         = new Subject <Unit>();
                var layoutManagerMock = Mock.Get(PerspexLocator.Current.GetService <ILayoutManager>());
                layoutManagerMock.Setup(x => x.LayoutCompleted).Returns(completed);

                var impl = new Mock <ITopLevelImpl>();
                impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));

                var target = new TestTopLevel(impl.Object);
                completed.OnNext(Unit.Default);

                var renderManagerMock = Mock.Get(PerspexLocator.Current.GetService <IRenderQueueManager>());
                renderManagerMock.Verify(x => x.InvalidateRender(target));
            }
        }
Exemplo n.º 28
0
        public void Should_Measure_Expander_Triangle_Correctly()
        {
            using (PerspexLocator.EnterScope())
            {
                Direct2D1Platform.Initialize();

                var target = new Path
                {
                    Data                = StreamGeometry.Parse("M 0 2 L 4 6 L 0 10 Z"),
                    StrokeThickness     = 0,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment   = VerticalAlignment.Top,
                    UseLayoutRounding   = false,
                };

                target.Measure(new Size(100, 100));
                target.Arrange(new Rect(0, 0, 100, 100));

                Assert.Equal(new Rect(0, 0, 4, 10), target.Bounds, Compare);
            }
        }
Exemplo n.º 29
0
        public void Impl_Input_Should_Pass_Input_To_InputManager()
        {
            using (PerspexLocator.EnterScope())
            {
                RegisterServices();

                var impl = new Mock <ITopLevelImpl>();
                impl.SetupAllProperties();
                var target = new TestTopLevel(impl.Object);

                var input = new RawKeyEventArgs(
                    new Mock <IKeyboardDevice>().Object,
                    0,
                    RawKeyEventType.KeyDown,
                    Key.A, InputModifiers.None);
                impl.Object.Input(input);

                var inputManagerMock = Mock.Get(InputManager.Instance);
                inputManagerMock.Verify(x => x.Process(input));
            }
        }
Exemplo n.º 30
0
        public void Setter_With_TwoWay_Binding_And_Activator_Should_Update_Source()
        {
            using (PerspexLocator.EnterScope())
            {
                PerspexLocator.CurrentMutable
                .Bind <IPlatformThreadingInterface>()
                .ToConstant(Mock.Of <IPlatformThreadingInterface>(x =>
                                                                  x.CurrentThreadIsLoopThread == true));

                var data = new Data
                {
                    Foo = "foo",
                };

                var control = new TextBox
                {
                    DataContext = data,
                };

                var setter = new Setter
                {
                    Property = TextBox.TextProperty,
                    Value    = new Binding
                    {
                        Path = "Foo",
                        Mode = BindingMode.TwoWay
                    }
                };

                var activator = Observable.Never <bool>().StartWith(true);

                setter.Apply(null, control, activator);
                Assert.Equal("foo", control.Text);

                control.Text = "bar";
                Assert.Equal("bar", data.Foo);
            }
        }