<commit_before>﻿// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System.Linq;
using Moq;
using Perspex.Controls.Generators;
using Perspex.Controls.Presenters;
using Perspex.Controls.Templates;
using Xunit;

namespace Perspex.Controls.UnitTests.Presenters
{
    public class CarouselPresenterTests
    {
        [Fact]
        public void Should_Register_With_Host_When_TemplatedParent_Set()
        {
            var host = new Mock<IItemsPresenterHost>();
            var target = new CarouselPresenter();

            target.SetValue(Control.TemplatedParentProperty, host.Object);

            host.Verify(x => x.RegisterItemsPresenter(target));
        }

        [Fact]
        public void ApplyTemplate_Should_Create_Panel()
        {
            var target = new CarouselPresenter
            {
                ItemsPanel = new FuncTemplate<IPanel>(() => new Panel()),
            };

            target.ApplyTemplate();

            Assert.IsType<Panel>(target.Panel);
        }

        [Fact]
        public void ItemContainerGenerator_Should_Be_Picked_Up_From_TemplatedControl()
        {
            var parent = new TestItemsControl();
            var target = new CarouselPresenter
            {
                TemplatedParent = parent,
            };

            Assert.IsType<ItemContainerGenerator<TestItem>>(target.ItemContainerGenerator);
        }

        [Fact]
        public void Setting_SelectedIndex_Should_Show_Page()
        {
            var target = new CarouselPresenter
            {
                Items = new[] { "foo", "bar" },
                SelectedIndex = 0,
            };

            target.ApplyTemplate();

            Assert.IsType<TextBlock>(target.Panel.Children[0]);
            Assert.Equal("foo", ((TextBlock)target.Panel.Children[0]).Text);
        }

        [Fact]
        public void Changing_SelectedIndex_Should_Show_Page()
        {
            var target = new CarouselPresenter
            {
                Items = new[] { "foo", "bar" },
                SelectedIndex = 0,
            };

            target.ApplyTemplate();
            target.SelectedIndex = 1;

            Assert.IsType<TextBlock>(target.Panel.Children[0]);
            Assert.Equal("bar", ((TextBlock)target.Panel.Children[0]).Text);
        }

        [Fact]
        public void Should_Remove_NonCurrent_Page_When_IsVirtualized_True()
        {
            var target = new CarouselPresenter
            {
                Items = new[] { "foo", "bar" },
                IsVirtualized = true,
                SelectedIndex = 0,
            };

            target.ApplyTemplate();
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            target.SelectedIndex = 1;
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
        }

        [Fact]
        public void Should_Not_Remove_NonCurrent_Page_When_IsVirtualized_False()
        {
            var target = new CarouselPresenter
            {
                Items = new[] { "foo", "bar" },
                IsVirtualized = false,
                SelectedIndex = 0,
            };

            target.ApplyTemplate();
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(1, target.Panel.Children.Count);
            target.SelectedIndex = 1;
            Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(2, target.Panel.Children.Count);
            target.SelectedIndex = 0;
            Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(2, target.Panel.Children.Count);
        }

        private class TestItem : ContentControl
        {
        }

        private class TestItemsControl : ItemsControl
        {
            protected override IItemContainerGenerator CreateItemContainerGenerator()
            {
                return new ItemContainerGenerator<TestItem>(this, TestItem.ContentProperty);
            }
        }
    }
}<commit_msg>added unit tests to test carousel presenter.<commit_after>﻿// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System.Linq;
using Moq;
using Perspex.Controls.Generators;
using Perspex.Controls.Presenters;
using Perspex.Controls.Templates;
using Xunit;
using System.Collections.ObjectModel;

namespace Perspex.Controls.UnitTests.Presenters
{
    public class CarouselPresenterTests
    {
        [Fact]
        public void Should_Register_With_Host_When_TemplatedParent_Set()
        {
            var host = new Mock<IItemsPresenterHost>();
            var target = new CarouselPresenter();

            target.SetValue(Control.TemplatedParentProperty, host.Object);

            host.Verify(x => x.RegisterItemsPresenter(target));
        }

        [Fact]
        public void ApplyTemplate_Should_Create_Panel()
        {
            var target = new CarouselPresenter
            {
                ItemsPanel = new FuncTemplate<IPanel>(() => new Panel()),
            };

            target.ApplyTemplate();

            Assert.IsType<Panel>(target.Panel);
        }

        [Fact]
        public void ItemContainerGenerator_Should_Be_Picked_Up_From_TemplatedControl()
        {
            var parent = new TestItemsControl();
            var target = new CarouselPresenter
            {
                TemplatedParent = parent,
            };

            Assert.IsType<ItemContainerGenerator<TestItem>>(target.ItemContainerGenerator);
        }

        [Fact]
        public void Setting_SelectedIndex_Should_Show_Page()
        {
            var target = new CarouselPresenter
            {
                Items = new[] { "foo", "bar" },
                SelectedIndex = 0,
            };

            target.ApplyTemplate();

            Assert.IsType<TextBlock>(target.Panel.Children[0]);
            Assert.Equal("foo", ((TextBlock)target.Panel.Children[0]).Text);
        }

        [Fact]
        public void Changing_SelectedIndex_Should_Show_Page()
        {
            var target = new CarouselPresenter
            {
                Items = new[] { "foo", "bar" },
                SelectedIndex = 0,
            };

            target.ApplyTemplate();
            target.SelectedIndex = 1;

            Assert.IsType<TextBlock>(target.Panel.Children[0]);
            Assert.Equal("bar", ((TextBlock)target.Panel.Children[0]).Text);
        }

        [Fact]
        public void Should_Remove_NonCurrent_Page_When_IsVirtualized_True()
        {
            var target = new CarouselPresenter
            {
                Items = new[] { "foo", "bar" },
                IsVirtualized = true,
                SelectedIndex = 0,
            };

            target.ApplyTemplate();
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            target.SelectedIndex = 1;
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
        }

        [Fact]
        public void Should_Not_Remove_NonCurrent_Page_When_IsVirtualized_False()
        {
            var target = new CarouselPresenter
            {
                Items = new[] { "foo", "bar" },
                IsVirtualized = false,
                SelectedIndex = 0,
            };

            target.ApplyTemplate();
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(1, target.Panel.Children.Count);
            target.SelectedIndex = 1;
            Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(2, target.Panel.Children.Count);
            target.SelectedIndex = 0;
            Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(2, target.Panel.Children.Count);
        }

        [Fact]
        public void Should_Remove_Controls_When_IsVirtualized_Is_False()
        {
            ObservableCollection<string> items = new ObservableCollection<string>();
            
            var target = new CarouselPresenter
            {
                Items = items,              
                SelectedIndex = 0,
                IsVirtualized = false,
            };

            target.ApplyTemplate();
            target.SelectedIndex = 0;
            items.Add("foo");
            target.SelectedIndex = 0;

            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(1, target.Panel.Children.Count);

            items.Add("bar");
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(1, target.Panel.Children.Count);

            target.SelectedIndex = 1;
            Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(2, target.Panel.Children.Count);            

            items.Remove(items[0]);
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(1, target.Panel.Children.Count);            

            items.Remove(items[0]);
            Assert.Equal(0, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(0, target.Panel.Children.Count);            
        }

        [Fact]
        public void Should_have_correct_index_itemscontainer()
        {
            ObservableCollection<string> items = new ObservableCollection<string>();

            var target = new CarouselPresenter
            {
                Items = items,
                SelectedIndex = 0,
                IsVirtualized = false,
            };

            target.ApplyTemplate();
            target.SelectedIndex = 0;
            items.Add("foo");
            target.SelectedIndex = 0;

            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(1, target.Panel.Children.Count);

            items.Add("bar");
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(1, target.Panel.Children.Count);

            target.SelectedIndex = 1;
            Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(2, target.Panel.Children.Count);
            Assert.Equal(0, target.ItemContainerGenerator.Containers.First().Index);

            items.Remove(items[0]);
            Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(1, target.Panel.Children.Count);
            Assert.Equal(1, target.ItemContainerGenerator.Containers.First().Index);

            items.Remove(items[0]);
            Assert.Equal(0, target.ItemContainerGenerator.Containers.Count());
            Assert.Equal(0, target.Panel.Children.Count);            
        }

        private class TestItem : ContentControl
        {
        }

        private class TestItemsControl : ItemsControl
        {
            protected override IItemContainerGenerator CreateItemContainerGenerator()
            {
                return new ItemContainerGenerator<TestItem>(this, TestItem.ContentProperty);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09838096080686544, 'fraction_numerical': 0.006546934442183491, 'mean_word_length': 1.855987872662961, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4556434', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2772, 'n_words': 506}