Daniel Murphy | efb35cc9 | 2024-04-18 16:56:37 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UI_GFX_TEST_SK_GMOCK_SUPPORT_H_ |
| 6 | #define UI_GFX_TEST_SK_GMOCK_SUPPORT_H_ |
| 7 | |
| 8 | #include "testing/gmock/include/gmock/gmock.h" |
| 9 | #include "third_party/skia/include/core/SkColor.h" |
| 10 | #include "ui/gfx/color_utils.h" |
| 11 | #include "ui/gfx/geometry/rect.h" |
| 12 | #include "ui/gfx/test/sk_color_eq.h" |
| 13 | |
| 14 | namespace gfx::test { |
| 15 | |
| 16 | MATCHER_P2(IsCloseToBitmap, expected_bmp, max_per_channel_deviation, "") { |
| 17 | // Number of pixels with an error |
| 18 | int error_pixels_count = 0; |
| 19 | |
| 20 | gfx::Rect error_bounding_rect = gfx::Rect(); |
| 21 | |
| 22 | // Check that bitmaps have identical dimensions. |
| 23 | if (arg.width() != expected_bmp.width()) { |
| 24 | *result_listener << "where widths do not match, actual: " << arg.width() |
| 25 | << ", expected: " << expected_bmp.width(); |
| 26 | return false; |
| 27 | } |
| 28 | if (arg.height() != expected_bmp.height()) { |
| 29 | *result_listener << "where heights do not match, actual: " << arg.height() |
| 30 | << ", expected: " << expected_bmp.height(); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | for (int x = 0; x < arg.width(); ++x) { |
| 35 | for (int y = 0; y < arg.height(); ++y) { |
| 36 | SkColor actual_color = arg.getColor(x, y); |
| 37 | SkColor expected_color = expected_bmp.getColor(x, y); |
| 38 | if (!ColorsClose(actual_color, expected_color, |
| 39 | max_per_channel_deviation)) { |
| 40 | ++error_pixels_count; |
| 41 | error_bounding_rect.Union(gfx::Rect(x, y, 1, 1)); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (error_pixels_count != 0) { |
| 47 | *result_listener |
| 48 | << "Number of pixel with an error, given max_per_channel_deviation of " |
| 49 | << max_per_channel_deviation << ": " << error_pixels_count |
| 50 | << "\nError Bounding Box : " << error_bounding_rect.ToString() << "\n"; |
| 51 | int sample_x = error_bounding_rect.x(); |
| 52 | int sample_y = error_bounding_rect.y(); |
| 53 | std::string expected_color = color_utils::SkColorToRgbaString( |
| 54 | expected_bmp.getColor(sample_x, sample_y)); |
| 55 | std::string actual_color = |
| 56 | color_utils::SkColorToRgbaString(arg.getColor(sample_x, sample_y)); |
| 57 | *result_listener << "Sample pixel comparison at " << sample_x << "x" |
| 58 | << sample_y << ": Expected " << expected_color |
| 59 | << ", actual " << actual_color; |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | MATCHER_P(EqualsBitmap, expected_bmp, "") { |
| 67 | return testing::ExplainMatchResult( |
| 68 | IsCloseToBitmap(expected_bmp, |
| 69 | /*max_per_channel_deviation=*/0), |
| 70 | arg, result_listener); |
| 71 | } |
| 72 | |
| 73 | } // namespace gfx::test |
| 74 | |
| 75 | #endif // UI_GFX_TEST_SK_GMOCK_SUPPORT_H_ |