color_recipe_unittest.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019 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/color/color_recipe.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "ui/color/color_mixer.h"
  7. #include "ui/color/color_test_ids.h"
  8. #include "ui/color/color_transform.h"
  9. #include "ui/gfx/color_palette.h"
  10. namespace ui {
  11. namespace {
  12. // Tests that a recipe with no transforms passes through its input color
  13. // unchanged.
  14. TEST(ColorRecipeTest, EmptyRecipeIsPassthrough) {
  15. const ColorRecipe recipe;
  16. const auto verify_passthrough = [&](SkColor input) {
  17. EXPECT_EQ(input, recipe.GenerateResult(input, ColorMixer()));
  18. };
  19. verify_passthrough(SK_ColorBLACK);
  20. verify_passthrough(SK_ColorWHITE);
  21. verify_passthrough(SK_ColorRED);
  22. }
  23. // Tests that a transform in a recipe has an effect.
  24. TEST(ColorRecipeTest, OneTransform) {
  25. constexpr SkColor kOutput = SK_ColorGREEN;
  26. ColorRecipe recipe = {kOutput};
  27. const auto verify_transform = [&](SkColor input) {
  28. EXPECT_EQ(kOutput, recipe.GenerateResult(input, ColorMixer()));
  29. };
  30. verify_transform(SK_ColorBLACK);
  31. verify_transform(SK_ColorWHITE);
  32. verify_transform(SK_ColorRED);
  33. }
  34. // Tests that in a recipe with multiple transforms, each is applied.
  35. TEST(ColorRecipeTest, ChainedTransforms) {
  36. ColorRecipe recipe = DeriveDefaultIconColor(FromTransformInput()) +
  37. BlendForMinContrast(FromTransformInput(), kColorTest0);
  38. constexpr SkColor kBackground = SK_ColorWHITE;
  39. ColorMixer mixer;
  40. mixer[kColorTest0] = {kBackground};
  41. const auto verify_chain = [&](SkColor input) {
  42. const SkColor color = recipe.GenerateResult(input, mixer);
  43. // The DeriveDefaultIconColor transform should change the output color even
  44. // when the BlendForMinContrast transform takes no action.
  45. EXPECT_NE(input, color);
  46. // The BlendForMinContrast transform should always be able to guarantee
  47. // readable contrast against white.
  48. EXPECT_GE(color_utils::GetContrastRatio(color, kBackground),
  49. color_utils::kMinimumReadableContrastRatio);
  50. };
  51. verify_chain(SK_ColorBLACK);
  52. verify_chain(SK_ColorWHITE);
  53. verify_chain(SK_ColorRED);
  54. }
  55. } // namespace
  56. } // namespace ui