skia_gold_matching_algorithm.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/base/test/skia_gold_matching_algorithm.h"
  5. #include "base/command_line.h"
  6. #include "base/notreached.h"
  7. #include "base/strings/string_number_conversions.h"
  8. #include "base/strings/string_util.h"
  9. namespace {
  10. constexpr char kMaxDifferentPixels[] = "fuzzy_max_different_pixels";
  11. constexpr char kPixelDeltaThreshold[] = "fuzzy_pixel_delta_threshold";
  12. constexpr char kEdgeThreshold[] = "sobel_edge_threshold";
  13. constexpr char kIgnoredBorderThickness[] = "fuzzy_ignored_border_thickness";
  14. } // namespace
  15. namespace ui {
  16. namespace test {
  17. SkiaGoldMatchingAlgorithm::SkiaGoldMatchingAlgorithm() = default;
  18. SkiaGoldMatchingAlgorithm::~SkiaGoldMatchingAlgorithm() = default;
  19. void SkiaGoldMatchingAlgorithm::AppendAlgorithmToCmdline(
  20. base::CommandLine& cmd) const {
  21. cmd.AppendSwitchASCII(
  22. "add-test-optional-key",
  23. base::JoinString({"image_matching_algorithm", GetCommandLineSwitchName()},
  24. ":"));
  25. }
  26. ExactSkiaGoldMatchingAlgorithm::ExactSkiaGoldMatchingAlgorithm() = default;
  27. ExactSkiaGoldMatchingAlgorithm::~ExactSkiaGoldMatchingAlgorithm() = default;
  28. std::string ExactSkiaGoldMatchingAlgorithm::GetCommandLineSwitchName() const {
  29. return "exact";
  30. }
  31. void ExactSkiaGoldMatchingAlgorithm::AppendAlgorithmToCmdline(
  32. base::CommandLine& cmd) const {
  33. // Do not call base class AppendAlgorithmToCmdline.
  34. // Nothing to append.
  35. }
  36. FuzzySkiaGoldMatchingAlgorithm::~FuzzySkiaGoldMatchingAlgorithm() = default;
  37. std::string FuzzySkiaGoldMatchingAlgorithm::GetCommandLineSwitchName() const {
  38. return "fuzzy";
  39. }
  40. FuzzySkiaGoldMatchingAlgorithm::FuzzySkiaGoldMatchingAlgorithm(
  41. int max_different_pixels,
  42. int pixel_delta_threshold,
  43. int ignored_border_thickness)
  44. : max_different_pixels_(max_different_pixels),
  45. pixel_delta_threshold_(pixel_delta_threshold),
  46. ignored_border_thickness_(ignored_border_thickness) {
  47. DCHECK_GT(max_different_pixels, 0);
  48. DCHECK_GT(pixel_delta_threshold, 0);
  49. DCHECK_LE(pixel_delta_threshold, 255 * 4);
  50. DCHECK_GE(ignored_border_thickness, 0);
  51. }
  52. void FuzzySkiaGoldMatchingAlgorithm::AppendAlgorithmToCmdline(
  53. base::CommandLine& cmd) const {
  54. SkiaGoldMatchingAlgorithm::AppendAlgorithmToCmdline(cmd);
  55. cmd.AppendSwitchASCII(
  56. "add-test-optional-key",
  57. base::JoinString(
  58. {kMaxDifferentPixels, base::NumberToString(max_different_pixels_)},
  59. ":"));
  60. cmd.AppendSwitchASCII(
  61. "add-test-optional-key",
  62. base::JoinString(
  63. {kPixelDeltaThreshold, base::NumberToString(pixel_delta_threshold_)},
  64. ":"));
  65. if (ignored_border_thickness_)
  66. cmd.AppendSwitchASCII(
  67. "add-test-optional-key",
  68. base::JoinString({kIgnoredBorderThickness,
  69. base::NumberToString(ignored_border_thickness_)},
  70. ":"));
  71. }
  72. SobelSkiaGoldMatchingAlgorithm::~SobelSkiaGoldMatchingAlgorithm() = default;
  73. std::string SobelSkiaGoldMatchingAlgorithm::GetCommandLineSwitchName() const {
  74. return "sobel";
  75. }
  76. SobelSkiaGoldMatchingAlgorithm::SobelSkiaGoldMatchingAlgorithm(
  77. int max_different_pixels,
  78. int pixel_delta_threshold,
  79. int edge_threshold,
  80. int ignored_border_thickness)
  81. : FuzzySkiaGoldMatchingAlgorithm(max_different_pixels,
  82. pixel_delta_threshold,
  83. ignored_border_thickness),
  84. edge_threshold_(edge_threshold) {
  85. DCHECK_GE(edge_threshold, 0);
  86. DCHECK_LE(edge_threshold, 255);
  87. }
  88. void SobelSkiaGoldMatchingAlgorithm::AppendAlgorithmToCmdline(
  89. base::CommandLine& cmd) const {
  90. FuzzySkiaGoldMatchingAlgorithm::AppendAlgorithmToCmdline(cmd);
  91. cmd.AppendSwitchASCII(
  92. "add-test-optional-key",
  93. base::JoinString({kEdgeThreshold, base::NumberToString(edge_threshold_)},
  94. ":"));
  95. }
  96. } // namespace test
  97. } // namespace ui