private bool MatchPlayRequestMessage(object m, MediaItem mediaItem)
        {
            var message = m as PlayRequestMessage;
            if (message == null) return false;

            return object.ReferenceEquals(message.Media, mediaItem);
        }
Esempio n. 2
0
        public void Download_progress_should_be_constrained_from_zero_to_one_hundred()
        {
            var item = new MediaItem();
            Assert.AreEqual(0, item.DownloadProgress);

            item.DownloadProgress = -1;
            Assert.AreEqual(0, item.DownloadProgress);

            item.DownloadProgress = 1;
            Assert.AreEqual(1, item.DownloadProgress);

            item.DownloadProgress = 99;
            Assert.AreEqual(99, item.DownloadProgress);

            item.DownloadProgress = 100;
            Assert.AreEqual(100, item.DownloadProgress);

            item.DownloadProgress = 101;
            Assert.AreEqual(100, item.DownloadProgress);

            item.DownloadProgress = int.MaxValue;
            Assert.AreEqual(100, item.DownloadProgress);

            item.DownloadProgress = int.MinValue;
            Assert.AreEqual(0, item.DownloadProgress);
        }
 private PlaylistItemViewModel Create(bool isInPlaylist, int displayIndex, int totalPlaylistItems)
 {
     var item = new MediaItem { Id = "test-1", Name = "test 1" };
     var library = new Mock<ILibrary>();
     library.Setup(l => l.MediaItems).Returns(new ObservableCollection<MediaItem>(new[] { item }));
     var eventAgg = new Mock<IEventAggregator>().Object;
     var persister = new Mock<IPodcastPlayPersister>().Object;
     return new PlaylistItemViewModel(new PodcastPlayViewModel(library.Object, eventAgg, persister), new MediaItemViewModel(eventAgg, item), isInPlaylist, displayIndex, totalPlaylistItems);
 }
        public void Playing_media_should_send_a_play_message_to_the_event_agg()
        {
            var eventAgg = new Mock<IEventAggregator>();
            var mediaItem = new MediaItem { Name = "test-item-to-play", IsAvailable = true };
            var model = new MediaItemViewModel(eventAgg.Object, mediaItem);
            model.PlayMediaItem();

            eventAgg.Verify(e => e.Publish(Match.Create<object>(m => MatchPlayRequestMessage(m, mediaItem))));
        }
        public void When_saving_media_item_should_write_file_to_library_directory()
        {
            var persistence = new Mock<IPersistence>();
            var persister = new MediaItemPersister(persistence.Object);

            var mediaItem = new MediaItem { Id = "test1", Name = "test-1", IsAvailable = true, DataStream = () => new MemoryStream() };
            persister.Save(mediaItem);

            persistence.Verify(p => p.WriteTextFile("library", "test1", It.Is<string>(xml => MediaItemXml(mediaItem, xml))));
            persistence.Verify(p => p.WriteBinaryFile("library.data", "test1", It.IsAny<Stream>()));
        }
        public void Given_media_binary_already_exists_when_saving_media_item_should_not_write_file_to_library_directory()
        {
            var persistence = new Mock<IPersistence>(MockBehavior.Strict);
            var persister = new MediaItemPersister(persistence.Object);

            persistence.Setup(p => p.Filenames("library.data")).Returns(new[] { "test1" });
            persistence.Setup(p => p.WriteTextFile(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()));

            var mediaItem = new MediaItem { Id = "test1", Name = "test-1", IsAvailable = true, DataStream = () => new MemoryStream() };
            persister.Save(mediaItem);

            persistence.Verify(p => p.WriteTextFile("library", "test1", It.Is<string>(xml => MediaItemXml(mediaItem, xml))));
        }
        public void Can_only_play_or_delete_media_when_media_is_available_and_not_deleted()
        {
            var eventAgg = new Mock<IEventAggregator>();
            var mediaItem = new MediaItem();
            var model = new MediaItemViewModel(eventAgg.Object, mediaItem);

            Assert.IsFalse(model.CanPlayMediaItem);
            Assert.IsFalse(model.CanDeleteMediaItem);

            mediaItem.IsAvailable = true;
            Assert.IsTrue(model.CanPlayMediaItem);
            Assert.IsTrue(model.CanDeleteMediaItem);

            model.DeleteMediaItem();
            Assert.IsFalse(model.CanPlayMediaItem);
            Assert.IsFalse(model.CanDeleteMediaItem);
        }
        public void Given_item_playing_when_item_is_deleted_should_stop_playing()
        {
            var eventAgg = new Mock<IEventAggregator>();
            var mediaItemPersister = new Mock<IMediaItemPersister>();
            var vm = new NowPlayingViewModel(eventAgg.Object, mediaItemPersister.Object);

            var media1 = new MediaItem();
            media1.IsDeleted = false;

            vm.Handle(new PlayRequestMessage(media1));

            Assert.AreEqual(PlayerState.Playing, vm.MediaPlayerState);

            media1.IsDeleted = true;
            Assert.AreEqual(PlayerState.Stopped, vm.MediaPlayerState);
            Assert.AreEqual("(Nothing)", vm.CurrentlyPlaying);
        }
        public void Given_a_new_item_to_play_should_persist_the_previously_playing_item()
        {
            var eventAgg = new Mock<IEventAggregator>();
            var mediaItemPersister = new Mock<IMediaItemPersister>();
            var vm = new NowPlayingViewModel(eventAgg.Object, mediaItemPersister.Object);

            var media1 = new MediaItem();
            vm.Handle(new PlayRequestMessage(media1));

            var media2 = new MediaItem();
            vm.Handle(new PlayRequestMessage(media2));

            mediaItemPersister.Verify(p => p.Save(media1));

            vm.Handle(new PlayRequestMessage(media1));

            mediaItemPersister.Verify(p => p.Save(media2));
        }
Esempio n. 10
0
        public MediaItem AddNewMediaItem(string name, Uri originalUri)
        {
            var newMediaItem = new MediaItem { Name = name, IsAvailable = false };
            MediaItems.Add(newMediaItem);

            log.Info("Adding item {0} (url: {1}) to library", name, originalUri);

            var client = new WebClient();
            client.OpenReadCompleted += (s, e) =>
            {
                log.Info("Item {0} (url: {1}) download complete.", name, originalUri);
                newMediaItem.DataStream = () => e.Result;
                newMediaItem.IsAvailable = true;
                mediaItemPersister.Save(newMediaItem);
                mediaItems.RaiseCollectionChanged();
            };
            client.DownloadProgressChanged += (s, e) =>
            {
                newMediaItem.DownloadProgress = e.ProgressPercentage;
            };
            client.OpenReadAsync(originalUri);

            return newMediaItem;
        }
 public MediaItemDeletedMessage(MediaItem media)
 {
     this.Media = media;
 }
 public NowPlayingMediaEndedMessage(MediaItem media)
 {
     this.media = media;
 }
        private bool MediaItemXml(MediaItem item, string xml)
        {
            var xdoc = XDocument.Parse(xml);
            using (var writer = new StringWriter())
            {
                xdoc.Save(writer, SaveOptions.None);

                Assert.AreEqual(
                    string.Format(@"<?xml version=""1.0"" encoding=""utf-16""?>
            <MediaItem Name=""{0}"" Deleted=""{1}"" MediaPosition=""{2}"" UtcDateAddedToLibrary=""{3}"">
              <ExtendedProperties />
            </MediaItem>", item.Name, item.IsDeleted.ToString().ToLower(), item.MediaPosition, item.UtcDateAddedToLibrary.ToString("u")),
                    writer.ToString());

                return true;
            }
        }
 public PlayRequestMessage(MediaItem media)
 {
     this.Media = media;
 }
        public void When_items_are_moved_in_playlists_then_persister_should_be_called()
        {
            var item1 = new MediaItem { Id = "test-1", Name = "test 1", IsAvailable = true };
            var item2 = new MediaItem { Id = "test-2", Name = "test 2", IsAvailable = true };
            var item3 = new MediaItem { Id = "test-3", Name = "test 3", IsAvailable = true };
            var library = new Mock<ILibrary>();
            library.Setup(l => l.MediaItems).Returns(new ObservableCollection<MediaItem>(new[] { item1, item2, item3 }));
            var eventAgg = new Mock<IEventAggregator>().Object;
            var persister = new Mock<IPodcastPlayPersister>();
            persister.Setup(p => p.IsEmpty).Returns(true);

            var vm = new PodcastPlayViewModel(library.Object, eventAgg, persister.Object);
            Assert.AreEqual(3, vm.PlaylistItems.Count());
            Assert.AreEqual(0, vm.ExcludedItems.Count());

            vm.Move(vm.PlaylistItems.ElementAt(0), false);
            persister.Verify(p => p.Save(It.Is<IEnumerable<string>>(pl => pl.SequenceEqual(new[] { "test-2", "test-1", "test-3" })), It.Is<IEnumerable<string>>(pl => pl.SequenceEqual(new string[0]))));

            vm.SwitchPlaylist(vm.PlaylistItems.ElementAt(2));
            persister.Verify(p => p.Save(It.Is<IEnumerable<string>>(pl => pl.SequenceEqual(new[] { "test-2", "test-1" })), It.Is<IEnumerable<string>>(pl => pl.SequenceEqual(new[] { "test-3" }))));
        }
        public void When_media_is_opened_should_forward_to_previous_media_position()
        {
            var eventAgg = new Mock<IEventAggregator>();
            var vm = new NowPlayingViewModel(eventAgg.Object, new Mock<IMediaItemPersister>().Object);

            var media = new MediaItem();
            media.MediaPosition = 100;
            vm.Handle(new PlayRequestMessage(media));
            Assert.AreEqual(0, vm.SliderPosition);

            vm.MediaOpened();
            Assert.AreEqual(100, vm.SliderPosition);
        }
        public void When_requesting_to_play_currently_playing_media_should_just_resume_playing()
        {
            var eventAgg = new Mock<IEventAggregator>();
            var vm = new NowPlayingViewModel(eventAgg.Object, new Mock<IMediaItemPersister>().Object);

            var media = new MediaItem();
            media.MediaPosition = 100;
            vm.Handle(new PlayRequestMessage(media));
            Assert.AreEqual(0, vm.SliderPosition);
            Assert.AreEqual(PlayerState.Playing, vm.MediaPlayerState);

            vm.SliderPosition = 10;

            vm.Handle(new PlayRequestMessage(media));
            Assert.AreEqual(10, vm.SliderPosition);
            Assert.AreEqual(PlayerState.Playing, vm.MediaPlayerState);
        }
Esempio n. 18
0
 public void Update(MediaItem item)
 {
     mediaItemPersister.Save(item);
     mediaItems.RaiseCollectionChanged();
 }