AddRange() public method

public AddRange ( IEnumerable items ) : void
items IEnumerable
return void
示例#1
0
        /// <summary>
        /// Adds all MediaFiles to the Queue and starts playing the first item
        /// </summary>
        /// <param name="mediaFiles"></param>
        /// <returns></returns>
        public async Task Play(IEnumerable <IMediaFile> mediaFiles)
        {
            MediaQueue.Clear();
            MediaQueue.AddRange(mediaFiles);

            await PlayNext();

            MediaNotificationManager?.StartNotification(CurrentMediaFile);
        }
示例#2
0
        /// <summary>
        /// Adds all MediaFiles to the Queue and starts playing the first item
        /// </summary>
        /// <param name="mediaFiles"></param>
        /// <returns></returns>
        public async Task Play(IEnumerable <IMediaFile> mediaFiles)
        {
            MediaQueue.Clear();
            MediaQueue.AddRange(mediaFiles);

            // Play from index 0
            MediaQueue.SetIndexAsCurrent(0);
            await PlayCurrent();

            MediaNotificationManager?.StartNotification(CurrentMediaFile);
        }
        /// <summary>
        /// Adds all MediaFiles to the Queue and starts playing the first item
        /// </summary>
        /// <param name="mediaFiles"></param>
        /// <returns></returns>
        public async Task Play(IEnumerable <IMediaFile> mediaFiles)
        {
            var enumerable = mediaFiles as IList <IMediaFile> ?? mediaFiles.ToList();

            MediaQueue.Clear();
            MediaQueue.AddRange(enumerable);

            await Task.WhenAll(
                PlayNext(),
                GetMediaInformation(enumerable),
                Task.Run(() => MediaNotificationManager?.StartNotification(MediaQueue.Current)));
        }
            public void Basic()
            {
                var queue = new MediaQueue();

                var tracks = new[]
                    {
                        new MediaFile(),
                        new MediaFile(),
                    };

                queue.AddRange(tracks);

                IList<NotifyCollectionChangedEventArgs> collectionChangedEvents = new List<NotifyCollectionChangedEventArgs>();
                IList<PropertyChangedEventArgs> propertyChangedEvents = new List<PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged += (sender, e) => propertyChangedEvents.Add(e);

                queue.Clear();

                Assert.AreEqual(0, queue.Count);
                Assert.AreEqual(null, queue.Current);
                Assert.AreEqual(-1, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(false, queue.Shuffle);

                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Current").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Index").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Count").Count());
                Assert.AreEqual(3, propertyChangedEvents.Count);
                Assert.AreEqual(1, collectionChangedEvents.Where(e => e.NewItems == null && e.OldItems == null).Count());
                Assert.AreEqual(1, collectionChangedEvents.Count);
            }
            public void ShuffleTwice_WhenRandomNumberGeneratorWorks_OrderIsDifferent()
            {
                var arr = new[]
                              {
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile()
                              };

                Console.WriteLine("Original: {0}", string.Join(",", arr.Select(x => x.Id)));

                var queue1 = new MediaQueue();
                queue1.AddRange(arr);

                var queue2 = new MediaQueue();
                queue2.AddRange(arr);

                // Randomize in quick succession to prove that the result is not the same
                // Using Random() here would create two equal lists, hence the use of PCLCrypto.
                queue1.ToggleShuffle();
                queue2.ToggleShuffle();

                Console.WriteLine("Queue1: {0}", string.Join(",", queue1.Cast<MediaFile>().Select(x => x.Id)));
                Console.WriteLine("Queue2: {0}", string.Join(",", queue2.Cast<MediaFile>().Select(x => x.Id)));

                CollectionAssert.AllItemsAreUnique(queue1.Cast<MediaFile>().Select(x => x.Id));
                CollectionAssert.AllItemsAreUnique(queue2.Cast<MediaFile>().Select(x => x.Id));
                CollectionAssert.AreNotEqual(queue1.Cast<MediaFile>(), queue2.Cast<MediaFile>());
                Assert.AreEqual(arr[queue1.Index].Id, queue1.Cast<MediaFile>().ElementAt(queue1.Index).Id, "The current item has been moved");
                Assert.AreEqual(arr[queue2.Index].Id, queue2.Cast<MediaFile>().ElementAt(queue2.Index).Id, "The current item has been moved");
            }
            public void WhenAddingItemsWhileShuffled_OrderIsSameAsBeforeButWithExtraItem()
            {
                var arr = new[]
                              {
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile()
                              };

                Console.WriteLine("Original: {0}", string.Join(",", arr.Select(x => x.Id)));

                var queue = new MediaQueue();
                Console.WriteLine("Current Index: {0}", queue.Index);
                queue.AddRange(arr);

                queue.ToggleShuffle();
                queue.Add(new MediaFile());

                Console.WriteLine("Shuffled: {0}", string.Join(",", queue.Cast<MediaFile>().Select(x => x.Id)));

                queue.ToggleShuffle();

                Console.WriteLine("Unshuffled: {0}", string.Join(",", queue.Cast<MediaFile>().Select(x => x.Id)));

                CollectionAssert.AllItemsAreUnique(queue.Cast<MediaFile>().Select(x => x.Id));
                Assert.AreEqual(arr[0].Id, queue.Cast<MediaFile>().ElementAt(0).Id);
                Assert.AreEqual(arr[1].Id, queue.Cast<MediaFile>().ElementAt(1).Id);
                Assert.AreEqual(arr[2].Id, queue.Cast<MediaFile>().ElementAt(2).Id);
                Assert.AreEqual(arr[3].Id, queue.Cast<MediaFile>().ElementAt(3).Id);
                Assert.AreEqual(arr[4].Id, queue.Cast<MediaFile>().ElementAt(4).Id);
                Assert.AreEqual(arr[5].Id, queue.Cast<MediaFile>().ElementAt(5).Id);
                Assert.AreEqual(arr[6].Id, queue.Cast<MediaFile>().ElementAt(6).Id);
                Assert.AreEqual(arr[7].Id, queue.Cast<MediaFile>().ElementAt(7).Id);
                Assert.AreEqual(arr[8].Id, queue.Cast<MediaFile>().ElementAt(8).Id);
                Assert.AreEqual(arr[9].Id, queue.Cast<MediaFile>().ElementAt(9).Id);
                Assert.AreEqual(arr[10].Id, queue.Cast<MediaFile>().ElementAt(10).Id);
                Assert.AreEqual(arr.Length + 1, queue.Count, "The array length is different");
            }
            public void WhenUnshufflingWorks_OrderIsSameAsBefore()
            {
                var arr = new[]
                              {
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile()
                              };

                Console.WriteLine("Original: {0}", string.Join(",", arr.Select(x => x.Id)));

                var queue = new MediaQueue();
                Console.WriteLine("Current Index: {0}", queue.Index);
                queue.AddRange(arr);

                queue.ToggleShuffle();

                Console.WriteLine("Shuffled: {0}", string.Join(",", queue.Cast<MediaFile>().Select(x => x.Id)));

                queue.ToggleShuffle();

                Console.WriteLine("Unshuffled: {0}", string.Join(",", queue.Cast<MediaFile>().Select(x => x.Id)));

                CollectionAssert.AllItemsAreUnique(queue.Cast<MediaFile>().Select(x => x.Id));
                CollectionAssert.AreEqual(queue.Cast<MediaFile>().Select(x => x.Id), arr.Cast<MediaFile>().Select(x => x.Id));
                Assert.AreEqual(arr[queue.Index].Id, queue.Cast<MediaFile>().ElementAt(queue.Index).Id, "The current item has been moved");
                Assert.AreEqual(arr.Length, queue.Count, "The array length is different");
            }
			public void Insert_OutOfRange()
			{
				var queue = new MediaQueue();

                var tracks = new[]
					{
						new MediaFile(),
					};

				queue.AddRange(tracks);
				queue.SetIndexAsCurrent(0);

				IList<NotifyCollectionChangedEventArgs> collectionChangedEvents = new List<NotifyCollectionChangedEventArgs>();
				IList<PropertyChangedEventArgs> propertyChangedEvents = new List<PropertyChangedEventArgs>();

				queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
				queue.PropertyChanged += (sender, e) => propertyChangedEvents.Add(e);

				Assert.Throws<ArgumentOutOfRangeException>(() =>
				{
					queue.Insert(2, new MediaFile());
				});

				Assert.AreEqual(1, queue.Count);
				Assert.AreEqual(tracks[0], queue.Current);
				Assert.AreEqual(0, queue.Index);
				Assert.AreEqual(RepeatType.None, queue.Repeat);
				Assert.AreEqual(false, queue.Shuffle);

				Assert.AreEqual(0, propertyChangedEvents.Count);
				Assert.AreEqual(0, collectionChangedEvents.Count);
			}
			public void Remove_CurrentAndLastItem()
			{
				var queue = new MediaQueue();

                var tracks = new[]
					{
						new MediaFile(),
						new MediaFile(),
						new MediaFile(),
					};

				queue.AddRange(tracks);
				queue.SetIndexAsCurrent(2);

				IList<NotifyCollectionChangedEventArgs> collectionChangedEvents = new List<NotifyCollectionChangedEventArgs>();
				IList<PropertyChangedEventArgs> propertyChangedEvents = new List<PropertyChangedEventArgs>();

				queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
				queue.PropertyChanged += (sender, e) => propertyChangedEvents.Add(e);

				queue.Remove(tracks[2]);

				Assert.AreEqual(2, queue.Count);
				Assert.AreEqual(tracks[1], queue.Current);
				Assert.AreEqual(1, queue.Index);
				Assert.AreEqual(RepeatType.None, queue.Repeat);
				Assert.AreEqual(false, queue.Shuffle);

				Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Current").Count());
				Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Index").Count());
				Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Count").Count());
				Assert.AreEqual(3, propertyChangedEvents.Count);
				Assert.AreEqual(1, collectionChangedEvents.Where(e => e.NewItems == null && e.OldItems?.Count == 1).Count());
				Assert.AreEqual(1, collectionChangedEvents.Count);
			}
			public void SetNotCurrent()
			{
				var queue = new MediaQueue();

                var tracks = new[]
					{
						new MediaFile(),
						new MediaFile(),
						new MediaFile(),
					};

				queue.AddRange(tracks);
				queue.SetIndexAsCurrent(1);

				IList<NotifyCollectionChangedEventArgs> collectionChangedEvents = new List<NotifyCollectionChangedEventArgs>();
				IList<PropertyChangedEventArgs> propertyChangedEvents = new List<PropertyChangedEventArgs>();

				queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
				queue.PropertyChanged += (sender, e) => propertyChangedEvents.Add(e);

				queue[0] = new MediaFile();

				Assert.AreEqual(3, queue.Count);
				Assert.AreEqual(tracks[1], queue.Current);
				Assert.AreEqual(1, queue.Index);
				Assert.AreEqual(RepeatType.None, queue.Repeat);
				Assert.AreEqual(false, queue.Shuffle);

				Assert.AreEqual(0, propertyChangedEvents.Count);
				Assert.AreEqual(1, collectionChangedEvents.Where(e => e.NewItems?.Count == 1 && e.OldItems?.Count == 1).Count());
				Assert.AreEqual(1, collectionChangedEvents.Count);
			}