示例#1
0
		public void NavigateResultTransitionTest()
		{
			StateController.Navigate("d0");
			NavigateResult result = new NavigateResult("t0");
			result.ExecuteResult(GetControllerContext(false));
			Assert.AreEqual("s1", StateContext.State.Key);
		}
示例#2
0
		public void NavigateResultDataTest()
		{
			StateController.Navigate("d0");
			NavigateResult result = new NavigateResult("t0", new NavigationData { { "a", 1 } });
			result.ExecuteResult(GetControllerContext(false));
			Assert.AreEqual(1, StateContext.Bag.a);
		}
        public void LeaveTestCase()
        {
            NavigationViewModel firstViewModel = null;

            Given("Init view-model", frame => ViewModelBase.CreateViewModel <NavigationViewModel>(frame))
            .And("First navigate", viewModel =>
            {
                firstViewModel = viewModel;
                return(ViewModelBase.Navigate <NavigationPage>(viewModel));
            })
            .AndAsync("Wait load page", async nResult => await WaitLoadPageAndCheckViewModelAsync <NavigationPage, NavigationViewModel>(nResult))
            .And("Init second view-model and check properties", viewModel =>
            {
                var secondViewModel = viewModel.GetViewModel <NavigationViewModel>();

                // check first properties
                Assert.IsTrue(viewModel.IsNavigated, "IsNavigated must be true");
                Assert.IsTrue(viewModel.IsLoaded, "IsNavigated must be true");
                Assert.IsFalse(viewModel.IsLeaved, "IsNavigated must be false");
                Assert.AreEqual(2, viewModel.MethodCallLog.Count, "long call log should be 2");
                Assert.AreEqual("OnGoPageAsync", viewModel.MethodCallLog[0], "1st must be called OnGoPageAsync");
                Assert.AreEqual("OnLoadPageAsync", viewModel.MethodCallLog[1], "2st must be called OnLoadPageAsync");

                // check second properties
                Assert.IsFalse(secondViewModel.IsNavigated, "second IsNavigated must be false");
                Assert.IsFalse(secondViewModel.IsLoaded, "second IsNavigated must be false");
                Assert.IsFalse(secondViewModel.IsLeaved, "second IsNavigated must be false");
                Assert.AreEqual(0, secondViewModel.MethodCallLog.Count, "second call log must be is empty");
                return(secondViewModel);
            })
            .WhenAsync("Second navigate and wait load page", async viewModel =>
            {
                NavigateResult <ViewModelBase> nResult = ViewModelBase.Navigate <NavigationPage>(viewModel);
                return(await WaitLoadPageAndCheckViewModelAsync <NavigationPage, NavigationViewModel>(nResult));
            })
            .Then("Check properties after second navigate", viewModel =>
            {
                // check first properties
                Assert.IsFalse(firstViewModel.IsNavigated, "IsNavigated must be false");
                Assert.IsFalse(firstViewModel.IsLoaded, "IsNavigated must be false");
                Assert.IsTrue(firstViewModel.IsLeaved, "IsNavigated must be true");
                Assert.AreEqual(3, firstViewModel.MethodCallLog.Count, "long call log should be 3");
                Assert.AreEqual("OnGoPageAsync", firstViewModel.MethodCallLog[0], "1st must be called OnGoPageAsync");
                Assert.AreEqual("OnLoadPageAsync", firstViewModel.MethodCallLog[1], "2st must be called OnLoadPageAsync");
                Assert.AreEqual("OnLeavePageAsync", firstViewModel.MethodCallLog[2], "3st must be called OnLeavePageAsync");

                // check second properties
                Assert.IsTrue(viewModel.IsNavigated, "IsNavigated must be true");
                Assert.IsTrue(viewModel.IsLoaded, "IsNavigated must be true");
                Assert.IsFalse(viewModel.IsLeaved, "IsNavigated must be false");
                Assert.AreEqual(2, viewModel.MethodCallLog.Count, "long call log should be 2");
                Assert.AreEqual("OnGoPageAsync", viewModel.MethodCallLog[0], "1st must be called OnGoPageAsync");
                Assert.AreEqual("OnLoadPageAsync", viewModel.MethodCallLog[1], "2st must be called OnLoadPageAsync");
            })
            .Run <TestWindow>(window => window.Frame, Timeouts.Second.Five);
        }
示例#4
0
        protected async ValueTask <TPage> NavigateAndWaitLoadPageAsync <TPage, TViewModel>(TViewModel viewModel)
            where TPage : Page, IPage, new()
            where TViewModel : ViewModelBase
        {
            NavigateResult <ViewModelBase> nResult = ViewModelBase.Navigate <TPage>(viewModel);

            Assert.IsTrue(nResult.IsNavigate, "navigate method not worked");

            await Task.Delay(Timeuots.Second.One);

            TPage page = CheckTypeAndGetPage <TPage>();
            await page.WaitLoadAsync();

            Assert.AreEqual(viewModel, page.DataContext, "view-model must be DataContext");

            return(page);
        }
示例#5
0
        /// <summary>
        /// Adds the link.
        /// </summary>
        /// <param name="url">The parent URL.</param>
        /// <param name="result">The result.</param>
        /// <param name="nextLink">The next link.</param>
        private static void AddLink(
            Uri url,
            NavigateResult result,
            HtmlNode nextLink)
        {
            string uriString = GetValidUriString(nextLink);
            if (string.IsNullOrEmpty(uriString))
            {
                return;
            }

            var rawUrl = new Uri(uriString, UriKind.RelativeOrAbsolute);
            var nextUrl = rawUrl;
            if (!rawUrl.IsAbsoluteUri)
            {
                nextUrl = new Uri(url, rawUrl);
            }

            result.AddLink(new HyperLink(nextUrl, url));
        }
示例#6
0
        /// <summary>
        /// Navigates to the given URL.
        /// </summary>
        /// <param name="url">The URL to navigate to.</param>
        /// <returns>The navigate result.</returns>
        public NavigateResult NavigateTo(Uri url)
        {
            var result = new NavigateResult();
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            this.browser.GoTo(url);
            stopwatch.Stop();
            result.StatusCode = this.browser.StatusCode;
            result.LoadTime = stopwatch.Elapsed;
            result.Images = this.browser.Images.Count;
            result.HtmlLength = this.browser.Html.Length;
            result.DocumentElements = this.browser.Elements.Count;
            foreach (var link in this.browser.Links)
            {
                if (string.IsNullOrEmpty(link.Url))
                {
                    continue;
                }

                result.AddLink(new HyperLink(new Uri(link.Url), url));
            }

            return result;
        }
示例#7
0
		public void NavigateResultChildActionTest()
		{
			StateController.Navigate("d0");
			NavigateResult result = new NavigateResult("t0");
			result.ExecuteResult(GetControllerContext(true));
		}
示例#8
0
		public void NavigateResultNullContextTest()
		{
			StateController.Navigate("d0");
			NavigateResult result = new NavigateResult("t0");
			result.ExecuteResult(null);
		}
示例#9
0
		public void NavigateResultDialogTest()
		{
			NavigateResult result = new NavigateResult("d0");
			result.ExecuteResult(GetControllerContext(false));
			Assert.AreEqual(StateInfoConfig.Dialogs[0].States[0], StateContext.State);
		}
示例#10
0
 private ValueTask <TViewModel> WaitLoadPageAndCheckViewModelAsync <TPage, TViewModel>(NavigateResult <ViewModelBase> navigateResult)
     where TPage : Page, IPage
     where TViewModel : ViewModelBase
 {
     return(WaitLoadPageAndCheckViewModelAsync <TPage, TViewModel>((TViewModel)navigateResult.HasSuccessAndGetViewModel()));
 }
示例#11
0
        /// <summary>
        /// Populates the result.
        /// </summary>
        /// <param name="url">The URL to populate from.</param>
        /// <param name="response">The response.</param>
        /// <returns>The navigate result.</returns>
        private static NavigateResult GetResult(
            Uri url,
            HttpWebResponse response)
        {
            if (response == null)
            {
                return NavigateResult.Empty;
            }

            HttpStatusCode statusCode = response.StatusCode;
            var responseStream =
                response.GetResponseStream();
            var doc = new HtmlDocument();
            doc.Load(responseStream);

            // TODO: Demeter:
            int documentElements =
                doc.DocumentNode.DescendantNodes().Count();

            // TODO: Demeter:
            int htmlLength =
                doc.DocumentNode.InnerHtml.Length;

            // TODO: Demeter:
            int images = doc.DocumentNode.Descendants("img").Count();
            NavigateResult result =
                new NavigateResult(
                statusCode,
                documentElements,
                htmlLength,
                images);
            var links = doc.DocumentNode.Descendants("a");
            foreach (var nextLink in links)
            {
                AddLink(url, result, nextLink);
            }

            return result;
        }
示例#12
0
 public static TViewModel HasSuccessAndGetViewModel <TViewModel>(this NavigateResult <TViewModel> navigateResult)
     where TViewModel : ViewModelBase
 {
     Assert.IsTrue(navigateResult.IsNavigate, "navigate method not worked");
     return(navigateResult.ViewModel);
 }
示例#13
0
        protected override async Task NavigateAsync(NavigateArgs args, NavigateResult result)
        {
            // Download robots.txt and sitemap.xml
            if (args.IsFileName("/", out string fileNameRoot))
            {
                if (fileNameRoot == "robots.txt")
                {
                    var robots = string.Format("Sitemap: {0}sitemap.xml", args.RequestUrlHost);
                    result.Data = Encoding.UTF8.GetBytes(robots);
                    return;
                }
                if (fileNameRoot == "sitemap.xml")
                {
                    var rowList = (await Data.Query <ContentSitemap>().QueryExecuteAsync());
                    rowList = rowList.OrderBy(item => item.NavigatePath).ThenBy(item => item.FileName).ToList(); // In memory sort
                    var sitemap = new StringBuilder();
                    sitemap.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                    sitemap.Append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\">");
                    for (int i = 0; i < rowList.Count; i++)
                    {
                        var row = rowList[i];
                        if (row.NavigatePath != null) // Not a group
                        {
                            sitemap.Append("<url>");
                            var loc = string.Format("{0}{1}", args.RequestUrlHost, row.NavigatePath?.Substring(1));
                            sitemap.Append("<loc>" + loc + "</loc>");
                            var navigatePath = row.NavigatePath;
                            while (i < rowList.Count && (row = rowList[i]).FileName != null && row.NavigatePath == navigatePath)
                            {
                                // See also: https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps
                                sitemap.Append("<image:image>");
                                sitemap.Append($"<image:loc>{ args.RequestUrlHost + row.FileName.Substring(1) }</image:loc>");
                                sitemap.Append("</image:image>");
                                i += 1;
                            }
                            if (row.SitemapDate is DateTime sitemapDate)
                            {
                                var lastmod = sitemapDate.ToString("yyyy-MM-dd");
                                sitemap.Append("<lastmod>");
                                sitemap.Append(lastmod);
                                sitemap.Append("</lastmod>");
                            }
                            sitemap.Append("</url>");
                        }
                    }
                    sitemap.Append("</urlset>");
                    result.Data = Encoding.UTF8.GetBytes(sitemap.ToString());
                    return;
                }
                if (fileNameRoot == "ads.txt" && args.ConfigCustom is JsonElement jsonElement && jsonElement.TryGetProperty("GoogleAdsTxt", out var googleAdsTxt))
                {
                    result.Data = Encoding.UTF8.GetBytes(googleAdsTxt.GetString());
                    return;
                }
            }

            // Download file (assets)
            if (args.IsFileName("/assets/", out string fileName))
            {
                var row = (await Data.Query <StorageFile>().Where(item => item.FileName == fileName).QueryExecuteAsync()).SingleOrDefault();
                if (row != null)
                {
                    result.Data = row.Data;
                    if (args.HttpQuery.ContainsKey("imageThumbnail"))
                    {
                        result.Data = row.DataImageThumbnail;
                    }
                    return;
                }
            }

            // Download File (feedback)
            if (args.IsFileName("/feedback/", out var fileNameFeedback))
            {
                if (Guid.TryParse(fileNameFeedback, out var name))
                {
                    var row = (await Data.Query <Feedback>().Where(item => item.Name == name).QueryExecuteAsync()).SingleOrDefault();
                    if (row != null)
                    {
                        result.Data = row.AttachmentData;
                        return;
                    }
                }
            }

            // Request session deserialize
            result.IsSession = true;
        }