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