Пример #1
0
 public void NameTest()
 {
     var feed = new Feed(new Uri("http://www.test.com/rss/feed"));
     Assert.AreEqual("http://www.test.com/rss/feed", feed.Name);
     feed.Name = "name";
     Assert.AreEqual("name", feed.Name);
 }
Пример #2
0
        public void SetDataManager()
        {
            var feedManagerA = new FeedManager();
            Assert.AreSame(feedManagerA, feedManagerA.Feeds.Single().DataManager);

            var feedA1 = new Feed(new Uri("http://www.test.com/rss/feed"));
            Assert.IsNull(feedA1.DataManager);

            feedManagerA.Feeds.Add(feedA1);
            Assert.AreSame(feedManagerA, feedA1.DataManager);

            feedManagerA.Feeds.Remove(feedA1);
            Assert.IsNull(feedA1.DataManager);

            // Now use the serialzer with one Feed added

            feedManagerA.Feeds.Add(feedA1);
            var feedManagerB = SerializerHelper.Clone(feedManagerA);
            Assert.AreSame(feedManagerB, feedManagerB.Feeds.First().DataManager);

            var feedB1 = feedManagerB.Feeds.Last();
            Assert.AreSame(feedManagerB, feedB1.DataManager);

            feedManagerB.Feeds.Remove(feedB1);
            Assert.IsNull(feedB1.DataManager);

            feedManagerB.Feeds.Add(feedB1);
            Assert.AreSame(feedManagerB, feedB1.DataManager);
        }
Пример #3
0
        private void UpdateItemsCoreTest(bool cloneItemsBeforeInsert, bool useSerializer)
        {
            var feed = new Feed(new Uri("http://www.test.com/rss/feed"));
            feed.UpdateItems(new[] {
                new FeedItem(new Uri("http://www.test.com/rss/feed/1"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name1", "desc", "author"),
                new FeedItem(new Uri("http://www.test.com/rss/feed/2"), new DateTimeOffset(2020, 5, 7, 12, 0, 0, new TimeSpan(1, 0, 0)), "name2", "desc", "author"),
            });
            feed = !useSerializer ? feed : SerializerHelper.Clone(feed);

            Assert.AreEqual(2, feed.Items.Count);
            Assert.IsTrue(new[] { "name2", "name1" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            var newItems = new[]
            {
                new FeedItem(new Uri("http://www.test.com/rss/feed/2"), new DateTimeOffset(2020, 5, 7, 12, 0, 0, new TimeSpan(1, 0, 0)), "name2b", "desc", "author"),
                new FeedItem(new Uri("http://www.test.com/rss/feed/3"), new DateTimeOffset(2020, 5, 6, 12, 0, 0, new TimeSpan(1, 0, 0)), "name3", "desc", "author"),
            };
            feed.UpdateItems(newItems, cloneItemsBeforeInsert: cloneItemsBeforeInsert);

            Assert.AreEqual(3, feed.Items.Count);
            Assert.IsTrue(new[] { "name2b", "name3", "name1" }.SequenceEqual(feed.Items.Select(x => x.Name)));
            if (cloneItemsBeforeInsert)
            {
                Assert.AreNotSame(feed.Items[1], newItems[1]);
            }
            else
            {
                Assert.AreSame(feed.Items[1], newItems[1]);
            }
        }
Пример #4
0
 public void SetDefaultSelectedFeedItem(Feed feed, FeedItem feedItem)
 {
     if (feedItem != null && !lastSelectedFeedItems.ContainsKey(feed))
     {
         lastSelectedFeedItems[feed] = feedItem;
     }
 }
Пример #5
0
        public void MergeTest()
        {
            var feedManagerA = new FeedManager();
            feedManagerA.Feeds.Remove(feedManagerA.Feeds.Single());
            var feedA1 = new Feed(new Uri("http://www.test.com/rss/feedA1"));
            var feedA2 = new Feed(new Uri("http://www.test.com/rss/feedA2"));
            feedManagerA.Feeds.Add(feedA1);
            feedManagerA.Feeds.Add(feedA2);
            var item = new FeedItem(new Uri("http://www.test.com/rss/feed"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name", "desc", "author");
            feedA2.UpdateItems(new[] { item });
            Assert.IsFalse(feedManagerA.Feeds.Last().Items.Single().MarkAsRead);

            var feedManagerB = new FeedManager()
            {
                ItemLifetime = TimeSpan.FromDays(42),
                MaxItemsLimit = 43
            };
            feedManagerB.Feeds.Remove(feedManagerB.Feeds.Single());
            var feedB1 = new Feed(new Uri("http://www.test.com/rss/feedB1"));
            var feedB2 = new Feed(new Uri("http://www.test.com/rss/feedA2"));
            feedManagerB.Feeds.Add(feedB1);
            feedManagerB.Feeds.Add(feedB2);
            feedB2.UpdateItems(new[] { item }, cloneItemsBeforeInsert: true);
            feedB2.Items.Single().MarkAsRead = true;

            feedManagerA.Merge(feedManagerB);

            Assert.AreEqual(42, feedManagerA.ItemLifetime.Value.Days);
            Assert.AreEqual(43u, feedManagerA.MaxItemsLimit.Value);
            Assert.IsTrue(new[] { "http://www.test.com/rss/feedB1", "http://www.test.com/rss/feedA2" }.SequenceEqual(feedManagerA.Feeds.Select(x => x.Uri.ToString())));
            Assert.IsTrue(feedManagerA.Feeds.Last().Items.Single().MarkAsRead);


            // Just remove one element => this way the Merge does not create new instances
            var feedManagerC = new FeedManager();
            feedManagerC.Feeds.Remove(feedManagerC.Feeds.Single());
            var feedC2 = new Feed(new Uri("http://www.test.com/rss/feedA2"));
            feedManagerC.Feeds.Add(feedC2);
            feedC2.UpdateItems(new[] { item }, cloneItemsBeforeInsert: true);
            feedC2.Items.Single().MarkAsRead = false;
            var itemA = feedManagerA.Feeds.Last().Items.Single();

            feedManagerA.Merge(feedManagerC);

            Assert.IsTrue(new[] { "http://www.test.com/rss/feedA2" }.SequenceEqual(feedManagerA.Feeds.Select(x => x.Uri.ToString())));
            Assert.AreSame(itemA, feedManagerA.Feeds.Last().Items.Single());
            Assert.IsFalse(itemA.MarkAsRead);
        }
Пример #6
0
        private void IsLoadingCoreTest(bool useSerializer)
        {
            var feed1 = new Feed(new Uri("http://www.test.com/rss/feed"));
            var feed2 = new Feed(new Uri("http://www.test.com/rss/feed"));
            feed1 = !useSerializer ? feed1 : SerializerHelper.Clone(feed1);
            feed2 = !useSerializer ? feed2 : SerializerHelper.Clone(feed2);

            var feedManager = new FeedManager();
            feedManager.Feeds.Add(feed1);
            feedManager.Feeds.Add(feed2);

            Assert.IsTrue(feed1.IsLoading);
            feed1.LoadError = new InvalidOperationException("test");
            Assert.IsFalse(feed1.IsLoading);

            Assert.IsTrue(feed2.IsLoading);
            feed2.UpdateItems(new FeedItem[0]);
            Assert.IsFalse(feed2.IsLoading);
        }
Пример #7
0
 private async Task AddNewFeed()
 {
     Uri feedUri;
     var uriString = feedListViewModel.Value.AddNewFeedUri.Trim();
     if (!uriString.StartsWith("http", StringComparison.CurrentCultureIgnoreCase))
     {
         uriString = "http://" + uriString;
     }
     else if (uriString.StartsWith("http://http://", StringComparison.CurrentCultureIgnoreCase)
         || uriString.StartsWith("http://https://", StringComparison.CurrentCultureIgnoreCase))
     {
         uriString = uriString.Substring(7);
     }
     if (Uri.TryCreate(uriString, UriKind.RelativeOrAbsolute, out feedUri))
     {
         var newFeed = new Feed(feedUri);
         await LoadFeedAsync(newFeed);
         feedListViewModel.Value.LoadErrorMessage = newFeed.LoadErrorMessage;
         if (newFeed.LoadError == null)
         {
             FeedManager.Feeds.Add(newFeed);
             selectionService.SelectedFeed = newFeed;
             feedListViewModel.Value.FeedAdded();
         }
     }
     else
     {
         feedListViewModel.Value.LoadErrorMessage = resourceService.GetString("UrlMustBeginWithHttp");
     }
 }
Пример #8
0
        private async Task LoadFeedAsync(Feed feed)
        {
            try
            {
                if (feed.Uri.IsAbsoluteUri && (feed.Uri.Scheme == "http" || feed.Uri.Scheme == "https"))
                {
                    selectionService.SetDefaultSelectedFeedItem(feed, feed.Items.FirstOrDefault());
                    if (selectionService.SelectedFeed == null)
                    {
                        selectionService.SelectedFeed = feed;                        
                    }
                    var syndicationFeed = await client.RetrieveFeedAsync(feed.Uri);
                    var items = syndicationFeed.Items.Select(x => new FeedItem(x.Uri, x.Date, x.Name, x.Description, x.Author)).ToArray();

                    feed.Name = syndicationFeed.Title;
                    feed.UpdateItems(items, excludeMarkAsRead: true);
                }
                else
                {
                    feed.LoadErrorMessage = resourceService.GetString("UrlMustBeginWithHttp");
                    feed.LoadError = new InvalidOperationException(@"The URL must begin with http:// or https://");
                }
            }
            catch (Exception ex)
            {
                feed.LoadErrorMessage = resourceService.GetString("ErrorLoadRssFeed");
                feed.LoadError = ex;
            }
            finally
            {
                if (selectionService.SelectedFeedItem == null && selectionService.SelectedFeed == feed)
                {
                    selectionService.SelectedFeedItem = feed.Items.FirstOrDefault();
                }
            }
        }
Пример #9
0
        private void UnreadItemsCountCoreTest(bool useSerializer)
        {
            var feed = new Feed(new Uri("http://www.test.com/rss/feed"));
            var feedManager = new FeedManager();
            feedManager.Feeds.Add(feed);
            feed.UpdateItems(new[] {
                new FeedItem(new Uri("http://www.test.com/rss/feed/1"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name1", "desc", "author"),
                new FeedItem(new Uri("http://www.test.com/rss/feed/2"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name2", "desc", "author"),
            });
            feed = !useSerializer ? feed : SerializerHelper.Clone(feed);
            
            Assert.AreEqual(2, feed.UnreadItemsCount);

            feed.Items[0].MarkAsRead = true;

            Assert.AreEqual(1, feed.UnreadItemsCount);
        }
Пример #10
0
 private void UpdateFeedItems(Feed feed)
 {
     feed.UpdateItems(new[] {
         new FeedItem(new Uri("http://www.test.com/rss/feed/1"), DateTimeOffset.Now - TimeSpan.FromDays(10), "name1", "desc", "author"),
         new FeedItem(new Uri("http://www.test.com/rss/feed/2"), DateTimeOffset.Now - TimeSpan.FromDays(5), "name2", "desc", "author"),
         new FeedItem(new Uri("http://www.test.com/rss/feed/3"), DateTimeOffset.Now - TimeSpan.FromDays(1), "name3", "desc", "author"),
     });
 }
Пример #11
0
        private void TrimItemsListWithItemLifetimeTest(bool useSerializer)
        {
            var feed = new Feed(new Uri("http://www.test.com/rss/feed"));
            feed = !useSerializer ? feed : SerializerHelper.Clone(feed);

            var feedManager = new FeedManager();
            feedManager.Feeds.Add(feed);

            UpdateFeedItems(feed);
            Assert.IsTrue(new[] { "name3", "name2", "name1" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            feedManager.ItemLifetime = TimeSpan.FromDays(6);
            Assert.IsTrue(new[] { "name3", "name2" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            UpdateFeedItems(feed);
            Assert.IsTrue(new[] { "name3", "name2" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            feedManager.ItemLifetime = TimeSpan.FromDays(11);
            UpdateFeedItems(feed);
            Assert.IsTrue(new[] { "name3", "name2", "name1" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            feedManager.ItemLifetime = TimeSpan.FromDays(2);
            Assert.IsTrue(new[] { "name3" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            feedManager.ItemLifetime = TimeSpan.FromDays(0.5);
            Assert.IsTrue(new string[0].SequenceEqual(feed.Items.Select(x => x.Name)));
        }
Пример #12
0
        private void TrimItemsListWithMaxItemsLimitTest(bool useSerializer)
        {
            var feed = new Feed(new Uri("http://www.test.com/rss/feed"));
            feed = !useSerializer ? feed : SerializerHelper.Clone(feed);

            var feedManager = new FeedManager();
            feedManager.Feeds.Add(feed);

            UpdateFeedItems(feed);
            Assert.IsTrue(new[] { "name3", "name2", "name1" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            feedManager.MaxItemsLimit = 2;
            Assert.IsTrue(new[] { "name3", "name2" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            UpdateFeedItems(feed);
            Assert.IsTrue(new[] { "name3", "name2" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            feedManager.MaxItemsLimit = 3;
            UpdateFeedItems(feed);
            Assert.IsTrue(new[] { "name3", "name2", "name1" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            feedManager.MaxItemsLimit = 1;
            Assert.IsTrue(new[] { "name3" }.SequenceEqual(feed.Items.Select(x => x.Name)));

            feedManager.MaxItemsLimit = 0;
            Assert.IsTrue(new string[0].SequenceEqual(feed.Items.Select(x => x.Name)));
        }