Пример #1
0
        public void ValidateShape(string shapeName, PixelTolerance?tolerance = null)
        {
            Run("UITests.Windows_UI_Xaml_Shapes.Basic_Shapes", skipInitialScreenshot: true);

            var ctrl = new QueryEx(q => q.Marked("_basicShapesTestRoot"));
            var expectedDirectory = Path.Combine(
                TestContext.CurrentContext.TestDirectory,
                "Windows_UI_Xaml_Shapes/Basics_Shapes_Tests_EpectedResults");
            var actualDirectory = Path.Combine(
                TestContext.CurrentContext.WorkDirectory,
                nameof(Windows_UI_Xaml_Shapes),
                nameof(Basics_Shapes_Tests),
                shapeName);

            tolerance = tolerance ?? (new PixelTolerance()
                                      .WithColor(132) // We are almost only trying to detect edges
                                      .WithOffset(3, 3, LocationToleranceKind.PerPixel)
                                      .Discrete(2));

            var failures = new List <(string test, Exception error)>();
            // To improve performance, we run all test for a given stretch at once.
            var testGroups = _tests
                             .Where(t => t.StartsWith(shapeName))
                             .GroupBy(t => string.Join("_", t.Split(new[] { '_' }, 3, StringSplitOptions.RemoveEmptyEntries).Take(2)));

            foreach (var testGroup in testGroups)
            {
                ctrl.SetDependencyPropertyValue("RunTest", string.Join(";", testGroup));
                _app.WaitFor(() => !string.IsNullOrWhiteSpace(ctrl.GetDependencyPropertyValue <string>("TestResult")), timeout: TimeSpan.FromMinutes(1));
                var testResultsRaw = ctrl.GetDependencyPropertyValue <string>("TestResult");

                var testResults = testResultsRaw
                                  .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                                  .Select(line => line.Split(new[] { ';' }, 3, StringSplitOptions.RemoveEmptyEntries))
                                  .Where(line => line.Length == 3)
                                  .ToDictionary(
                    line => line[0],
                    line =>
                {
                    var testName  = line[0];
                    var isSuccess = line[1] == "SUCCESS";
                    var data      = Convert.FromBase64String(line[2]);

                    var target = Path
                                 .Combine(actualDirectory, testName + (isSuccess ? ".png" : ".txt"))
                                 .GetNormalizedLongPath();
                    var targetFile = new FileInfo(target);

                    targetFile.Directory.Create();
                    File.WriteAllBytes(target, data);
                    SetOptions(targetFile, new ScreenshotOptions {
                        IgnoreInSnapshotCompare = true
                    });
                    TestContext.AddTestAttachment(target, testName);

                    return(isSuccess
                                                                ? new Bitmap(new MemoryStream(Convert.FromBase64String(line[2])))
                                                                : new Exception(Encoding.UTF8.GetString(Convert.FromBase64String(line[2]))) as object);
                });

                foreach (var test in testGroup)
                {
                    try
                    {
                        var expected = new FileInfo(Path.Combine(expectedDirectory, $"{test}.png"));
                        if (!expected.Exists)
                        {
                            Assert.Fail($"Expected screenshot does not exists ({expected.FullName})");
                        }

                        if (!testResults.TryGetValue(test, out var testResult))
                        {
                            Assert.Fail($"No test result for {test}.");
                        }

                        if (testResult is Exception error)
                        {
                            Assert.Fail($"Test failed: {error.Message}");
                        }

                        using (var actual = (Bitmap)testResult)
                        {
                            var scale = _app.GetDisplayScreenScaling();
                            ImageAssert.AreAlmostEqual(expected, ImageAssert.FirstQuadrant, actual, ImageAssert.FirstQuadrant, scale, tolerance.Value);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e);                         // Ease debug while reading log from CI
                        failures.Add((test, e));
                    }
                }
            }

            if (failures.Any())
            {
                throw new AggregateException(
                          $"Failed tests ({failures.Count} of {testGroups.Sum(g => g.Count())}):\r\n{string.Join("\r\n", failures.Select(t => t.test))}\r\n",
                          failures.Select(t => t.error));
            }
            else
            {
                Console.WriteLine($"All {testGroups.Sum(g => g.Count())} ran successfully.");
            }
        }