public Task <SurveyNewsFeed> GetFeedAsync(string feedUrl) { // We can't use a simple WebRequest, because that doesn't have access // to the browser's session cookies. Cookies are used to remember // which survey/news item the user has submitted/accepted. The server // checks the cookies and returns the survey/news urls that are // currently available (availability is determined via the survey/news // item start and end date). var tcs = new TaskCompletionSource <SurveyNewsFeed>(); try { var thread = new Thread(() => { var browser = new WebBrowser(); browser.DocumentCompleted += (sender, e) => { try { if (browser.Url == e.Url) { SurveyNewsFeed feed = ParseFeed(browser); tcs.SetResult(feed); Application.ExitThread(); } } catch (Exception ex2) { tcs.SetException(ex2); } }; browser.Navigate(new Uri(feedUrl)); Application.Run(); }); thread.Name = "SurveyNewsFeedClient"; thread.SetApartmentState(ApartmentState.STA); thread.Start(); } catch (Exception ex1) { tcs.SetException(ex1); } return(tcs.Task); }
public MockSurveyNewsFeedClient(SurveyNewsFeed feed) { _feed = feed; }
private async Task CheckSurveyNews(SurveyNewsFeed feed, SurveyNewsPolicy policy, DateTime lastChecked, bool forceCheck, string expectedNavigatedUrl, DateTime? expectedLastChecked) { string navigatedUrl = null; // Create the test objects var browser = Substitute.For<IWebBrowserServices>(); browser.When(x => x.OpenBrowser(Arg.Any<WebBrowserRole>(), Arg.Any<string>(), Arg.Any<bool>())).Do(x => { ((WebBrowserRole)x.Args()[0]).Should().Be(WebBrowserRole.News); navigatedUrl = (string)x.Args()[1]; }); var options = new MockSurveyNewsOptions(policy, lastChecked); var feedClient = new MockSurveyNewsFeedClient(feed); // Invoke the real survey/news service var service = new SurveyNewsService(feedClient, options, browser, Substitute.For<ICoreShell>()); await service.CheckSurveyNewsAsync(forceCheck); // Check that we navigated to the right url (or didn't navigate at all) navigatedUrl.Should().Be(expectedNavigatedUrl); // Check that the last checked date has been updated (or not updated at all) if (expectedLastChecked.HasValue) { var delta = options.SurveyNewsLastCheck - expectedLastChecked.Value; delta.Duration().Should().BeLessOrEqualTo(TimeSpan.FromSeconds(5)); } else { options.SurveyNewsLastCheck.Should().Be(lastChecked); } }