skia_conversions_unittest.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) 2013 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/gfx/geometry/skia_conversions.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "ui/gfx/geometry/rect.h"
  7. namespace gfx {
  8. TEST(SkiaConversionsTest, SkiaRectConversions) {
  9. Rect isrc(10, 20, 30, 40);
  10. RectF fsrc(10.5f, 20.5f, 30.5f, 40.5f);
  11. SkIRect skirect = RectToSkIRect(isrc);
  12. EXPECT_EQ(isrc.ToString(), SkIRectToRect(skirect).ToString());
  13. SkRect skrect = RectToSkRect(isrc);
  14. EXPECT_EQ(gfx::RectF(isrc).ToString(), SkRectToRectF(skrect).ToString());
  15. skrect = RectFToSkRect(fsrc);
  16. EXPECT_EQ(fsrc.ToString(), SkRectToRectF(skrect).ToString());
  17. }
  18. TEST(SkiaConversionsTest, SkIRectToRectClamping) {
  19. // This clamping only makes sense if SkIRect and gfx::Rect have the same size.
  20. // Otherwise, either other overflows can occur that we don't handle, or no
  21. // overflows can ocur.
  22. if (sizeof(int) != sizeof(int32_t))
  23. return;
  24. using Limits = std::numeric_limits<int>;
  25. // right-left and bottom-top would overflow.
  26. // These should be mapped to max width/height, which is as close as gfx::Rect
  27. // can represent.
  28. Rect result = SkIRectToRect(SkIRect::MakeLTRB(Limits::min(), Limits::min(),
  29. Limits::max(), Limits::max()));
  30. EXPECT_EQ(gfx::Size(Limits::max(), Limits::max()), result.size());
  31. // right-left and bottom-top would underflow.
  32. // These should be mapped to zero, like all negative values.
  33. result = SkIRectToRect(SkIRect::MakeLTRB(Limits::max(), Limits::max(),
  34. Limits::min(), Limits::min()));
  35. EXPECT_EQ(gfx::Rect(Limits::max(), Limits::max(), 0, 0), result);
  36. }
  37. } // namespace gfx