Exemplo n.º 1
0
        public void Should_Add_Containers_For_Items_After_Clear()
        {
            var target       = CreateTarget(itemCount: 10);
            var defaultItems = (IList <string>)target.Items;
            var items        = new AvaloniaList <string>(defaultItems);

            target.Items = items;

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

            Assert.Equal(10, target.Panel.Children.Count);

            items.Clear();

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

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

            Assert.Empty(target.Panel.Children);

            items.AddRange(defaultItems.Select(s => s + " new"));

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

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

            Assert.Equal(10, target.Panel.Children.Count);
        }
Exemplo n.º 2
0
        public void Containers_Correct_After_Clear_Add_Remove()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                // Issue #1936
                var items  = new AvaloniaList <string>(Enumerable.Range(0, 11).Select(x => $"Item {x}"));
                var target = new ListBox
                {
                    Template     = ListBoxTemplate(),
                    Items        = items,
                    ItemTemplate = new FuncDataTemplate <string>((x, _) => new TextBlock {
                        Width = 20, Height = 10
                    }),
                    SelectedIndex = 0,
                };

                Prepare(target);

                items.Clear();
                items.AddRange(Enumerable.Range(0, 11).Select(x => $"Item {x}"));
                items.Remove("Item 2");

                Assert.Equal(
                    items,
                    target.Presenter.Panel.Children.Cast <ListBoxItem>().Select(x => (string)x.Content));
            }
        }
Exemplo n.º 3
0
 protected override void Initialize(AvaloniaList <TreeNode> nodes)
 {
     _subscription = _control.VisualChildren.ForEachItem(
         (i, item) => nodes.Insert(i, new VisualTreeNode(item, Owner)),
         (i, item) => nodes.RemoveAt(i),
         () => nodes.Clear());
 }
        public void CreateDerivedList_Handles_Clear()
        {
            var source = new AvaloniaList <int>(new[] { 0, 1, 2, 3 });
            var target = source.CreateDerivedList(x => new Wrapper(x));

            source.Clear();

            var result = target.Select(x => x.Value).ToList();

            Assert.Equal(source, result);
        }
Exemplo n.º 5
0
        public void Clearing_Items_Should_Raise_CollectionChanged_Reset()
        {
            var target = new AvaloniaList <int>(new[] { 1, 2, 3 });
            var raised = false;

            target.CollectionChanged += (s, e) =>
            {
                Assert.Equal(target, s);
                Assert.Equal(NotifyCollectionChangedAction.Reset, e.Action);

                raised = true;
            };

            target.Clear();

            Assert.True(raised);
        }
Exemplo n.º 6
0
        public void Clearing_Items_Should_Raise_CollectionChanged_Remove()
        {
            var target = new AvaloniaList <int>(new[] { 1, 2, 3 });
            var raised = false;

            target.ResetBehavior      = ResetBehavior.Remove;
            target.CollectionChanged += (s, e) =>
            {
                Assert.Equal(target, s);
                Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action);
                Assert.Equal(new[] { 1, 2, 3 }, e.OldItems.Cast <int>());
                Assert.Equal(0, e.OldStartingIndex);

                raised = true;
            };

            target.Clear();

            Assert.True(raised);
        }
        public void HotKeyManager_Should_Release_Reference_When_Control_In_Item_Template_Detached()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var styler = new Mock <Styler>();

                AvaloniaLocator.CurrentMutable
                .Bind <IWindowingPlatform>().ToConstant(new WindowingPlatformMock())
                .Bind <IStyler>().ToConstant(styler.Object);

                var gesture1 = new KeyGesture(Key.A, KeyModifiers.Control);

                var weakReferences = new List <WeakReference>();
                var tl             = new Window {
                    SizeToContent = SizeToContent.WidthAndHeight, IsVisible = true
                };
                var lm = tl.LayoutManager;

                var keyGestures = new AvaloniaList <KeyGesture> {
                    gesture1
                };
                var listBox = new ListBox
                {
                    Width              = 100,
                    Height             = 100,
                    VirtualizationMode = ItemVirtualizationMode.None,
                    // Create a button with binding to the KeyGesture in the template and add it to references list
                    ItemTemplate = new FuncDataTemplate(typeof(KeyGesture), (o, scope) =>
                    {
                        var keyGesture = o as KeyGesture;
                        var button     = new Button
                        {
                            DataContext = keyGesture, [!Button.HotKeyProperty] = new Binding("")
                        };
                        weakReferences.Add(new WeakReference(button, true));
                        return(button);
                    })
                };
                // Add the listbox and render it
                tl.Content = listBox;
                lm.ExecuteInitialLayoutPass();
                listBox.Items = keyGestures;
                lm.ExecuteLayoutPass();

                // Let the button detach when clearing the source items
                keyGestures.Clear();
                lm.ExecuteLayoutPass();

                // Add it again to double check,and render
                keyGestures.Add(gesture1);
                lm.ExecuteLayoutPass();

                keyGestures.Clear();
                lm.ExecuteLayoutPass();

                // The button should be collected since it's detached from the listbox
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();

                Assert.True(weakReferences.Count > 0);
                foreach (var weakReference in weakReferences)
                {
                    Assert.Null(weakReference.Target);
                }
            }
        }
Exemplo n.º 8
0
        public void LayoutManager_Should_Measure_Arrange_All()
        {
            var virtualizationMode = ItemVirtualizationMode.Simple;

            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var items = new AvaloniaList <string>(Enumerable.Range(1, 7).Select(v => v.ToString()));

                var wnd = new Window()
                {
                    SizeToContent = SizeToContent.WidthAndHeight
                };

                wnd.IsVisible = true;

                var target = new ListBox();

                wnd.Content = target;

                var lm = wnd.LayoutManager;

                target.Height             = 110;
                target.Width              = 50;
                target.DataContext        = items;
                target.VirtualizationMode = virtualizationMode;

                target.ItemTemplate = new FuncDataTemplate <object>((c, _) =>
                {
                    var tb = new TextBlock()
                    {
                        Height = 10, Width = 30
                    };
                    tb.Bind(TextBlock.TextProperty, new Data.Binding());
                    return(tb);
                }, true);

                lm.ExecuteInitialLayoutPass();

                target.Items = items;

                lm.ExecuteLayoutPass();

                items.Insert(3, "3+");
                lm.ExecuteLayoutPass();

                items.Insert(4, "4+");
                lm.ExecuteLayoutPass();

                //RESET
                items.Clear();
                foreach (var i in Enumerable.Range(1, 7))
                {
                    items.Add(i.ToString());
                }

                //working bit better with this line no outof memory or remaining to arrange/measure ???
                //lm.ExecuteLayoutPass();

                items.Insert(2, "2+");

                lm.ExecuteLayoutPass();
                //after few more layout cycles layoutmanager shouldn't hold any more visual for measure/arrange
                lm.ExecuteLayoutPass();
                lm.ExecuteLayoutPass();

                var flags     = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
                var toMeasure = lm.GetType().GetField("_toMeasure", flags).GetValue(lm) as System.Collections.Generic.IEnumerable <Layout.ILayoutable>;
                var toArrange = lm.GetType().GetField("_toArrange", flags).GetValue(lm) as System.Collections.Generic.IEnumerable <Layout.ILayoutable>;

                Assert.Equal(0, toMeasure.Count());
                Assert.Equal(0, toArrange.Count());
            }
        }
Exemplo n.º 9
0
        public void Clearing_Items_Should_Raise_CollectionChanged_Remove()
        {
            var target = new AvaloniaList<int>(new[] { 1, 2, 3 });
            var raised = false;

            target.ResetBehavior = ResetBehavior.Remove;
            target.CollectionChanged += (s, e) =>
            {
                Assert.Equal(target, s);
                Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action);
                Assert.Equal(new[] { 1, 2, 3 }, e.OldItems.Cast<int>());
                Assert.Equal(0, e.OldStartingIndex);

                raised = true;
            };

            target.Clear();

            Assert.True(raised);
        }
Exemplo n.º 10
0
        public void Clearing_Items_Should_Raise_CollectionChanged_Reset()
        {
            var target = new AvaloniaList<int>(new[] { 1, 2, 3 });
            var raised = false;

            target.CollectionChanged += (s, e) =>
            {
                Assert.Equal(target, s);
                Assert.Equal(NotifyCollectionChangedAction.Reset, e.Action);

                raised = true;
            };

            target.Clear();

            Assert.True(raised);
        }
        public void Should_Add_Containers_For_Items_After_Clear()
        {
            var target = CreateTarget(itemCount: 10);
            var defaultItems = (IList<string>)target.Items;
            var items = new AvaloniaList<string>(defaultItems);
            target.Items = items;

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

            Assert.Equal(10, target.Panel.Children.Count);

            items.Clear();

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

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

            Assert.Equal(0, target.Panel.Children.Count);

            items.AddRange(defaultItems.Select(s => s + " new"));

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

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

            Assert.Equal(10, target.Panel.Children.Count);
        }