ash_pixel_diff_test_helper.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2022 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. #ifndef ASH_TEST_ASH_PIXEL_DIFF_TEST_HELPER_H_
  5. #define ASH_TEST_ASH_PIXEL_DIFF_TEST_HELPER_H_
  6. #include "ui/views/test/view_skia_gold_pixel_diff.h"
  7. namespace ash {
  8. // A helper class that provides utility functions for performing pixel diff
  9. // tests via the Skia Gold.
  10. class AshPixelDiffTestHelper {
  11. public:
  12. // Lists the UI components supported by pixel tests.
  13. enum class UiComponent {
  14. // The shelf widget that shows the shelf background.
  15. kShelfWidget,
  16. };
  17. AshPixelDiffTestHelper();
  18. AshPixelDiffTestHelper(const AshPixelDiffTestHelper&) = delete;
  19. AshPixelDiffTestHelper& operator=(const AshPixelDiffTestHelper&) = delete;
  20. ~AshPixelDiffTestHelper();
  21. // Takes a screenshot of the primary fullscreen then uploads it to the Skia
  22. // Gold to perform pixel comparison. Returns the comparison result.
  23. // NOTE: use this function only when necessary. Otherwise, a tiny UI change
  24. // may break many pixel tests.
  25. bool ComparePrimaryFullScreen(const std::string& screenshot_name);
  26. // Takes a screenshot of the area associated to `ui_component` then compares
  27. // it with the benchmark image. Returns the comparison result.
  28. bool CompareUiComponentScreenshot(const std::string& screenshot_name,
  29. UiComponent ui_component);
  30. // Compares the screenshot of the area specified by `screen_bounds` with the
  31. // benchmark image. Returns the comparison result.
  32. bool ComparePrimaryScreenshotWithBoundsInScreen(
  33. const std::string& screenshot_name,
  34. const gfx::Rect& screen_bounds);
  35. // Initializes the underlying utility class for Skia Gold pixel tests.
  36. // NOTE: this function has to be called before any pixel comparison.
  37. void InitSkiaGoldPixelDiff(const std::string& screenshot_prefix,
  38. const std::string& corpus = std::string());
  39. private:
  40. // Returns the screen bounds of the given UI component.
  41. // NOTE: this function assumes that the UI component is on the primary screen.
  42. gfx::Rect GetUiComponentBoundsInScreen(UiComponent ui_component) const;
  43. // Used to take screenshots and upload images to the Skia Gold server to
  44. // perform pixel comparison.
  45. // NOTE: the user of `ViewSkiaGoldPixelDiff` has the duty to initialize
  46. // `pixel_diff` before performing any pixel comparison.
  47. views::ViewSkiaGoldPixelDiff pixel_diff_;
  48. };
  49. } // namespace ash
  50. #endif // ASH_TEST_ASH_PIXEL_DIFF_TEST_HELPER_H_