blob: 1e3395cd910aac6d2fb87e93481502ceb30d60fc [file] [log] [blame]
alex-ac2cb37ad82014-11-13 18:03:291// Copyright 2014 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 "ui/gfx/path_win.h"
6
7#include <algorithm>
8#include <vector>
9
10#include "base/win/scoped_gdi_object.h"
11#include "skia/ext/skia_utils_win.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "third_party/skia/include/core/SkRRect.h"
14#include "ui/gfx/path.h"
15
16namespace gfx {
17
18namespace {
19
20// Get rectangles from the |region| and convert them to SkIRect.
21std::vector<SkIRect> GetRectsFromHRGN(HRGN region) {
22 // Determine the size of output buffer required to receive the region.
23 DWORD bytes_size = GetRegionData(region, 0, NULL);
24 CHECK_NE((DWORD)0, bytes_size);
25
26 // Fetch the Windows RECTs that comprise the region.
27 std::vector<char> buffer(bytes_size);
28 LPRGNDATA region_data = reinterpret_cast<LPRGNDATA>(buffer.data());
29 DWORD result = GetRegionData(region, bytes_size, region_data);
30 CHECK_EQ(bytes_size, result);
31
32 // Pull out the rectangles into a SkIRect vector to return to caller.
33 const LPRECT rects = reinterpret_cast<LPRECT>(&region_data->Buffer[0]);
34 std::vector<SkIRect> sk_rects(region_data->rdh.nCount);
35 std::transform(rects, rects + region_data->rdh.nCount,
36 sk_rects.begin(), skia::RECTToSkIRect);
37
38 return sk_rects;
39}
40
41} // namespace
42
43// Test that rectangle with round corners stil has round corners after
44// converting from SkPath to the HRGN.
fmalita22146632015-02-16 21:00:4145// FIXME: this test is fragile (it depends on rrect rasterization impl)
alex-ac2cb37ad82014-11-13 18:03:2946TEST(CreateHRGNFromSkPathTest, RoundCornerTest) {
47 const SkIRect rects[] = {
fmalita22146632015-02-16 21:00:4148 { 16, 0, 34, 1 },
49 { 12, 1, 38, 2 },
50 { 10, 2, 40, 3 },
51 { 9, 3, 41, 4 },
52 { 7, 4, 43, 5 },
53 { 6, 5, 44, 6 },
54 { 5, 6, 45, 7 },
55 { 4, 7, 45, 8 },
56 { 4, 8, 46, 9 },
57 { 3, 9, 47, 10 },
58 { 2, 10, 47, 11 },
59 { 2, 11, 48, 12 },
60 { 1, 12, 49, 16 },
61 { 0, 16, 50, 34 },
62 { 1, 34, 49, 38 },
63 { 2, 38, 48, 39 },
64 { 2, 39, 47, 40 },
65 { 3, 40, 47, 41 },
66 { 4, 41, 46, 42 },
67 { 4, 42, 45, 43 },
68 { 5, 43, 45, 44 },
69 { 6, 44, 44, 45 },
70 { 8, 45, 42, 46 },
71 { 9, 46, 41, 47 },
72 { 11, 47, 39, 48 },
73 { 12, 48, 38, 49 },
74 { 16, 49, 34, 50 },
alex-ac2cb37ad82014-11-13 18:03:2975 };
76
77 Path path;
78 SkRRect rrect;
79 rrect.setRectXY(SkRect::MakeWH(50, 50), 20, 20);
80 path.addRRect(rrect);
81 base::win::ScopedRegion region(CreateHRGNFromSkPath(path));
anpol280746c2015-12-18 08:39:0382 const std::vector<SkIRect>& region_rects = GetRectsFromHRGN(region.get());
alex-ac2cb37ad82014-11-13 18:03:2983 EXPECT_EQ(arraysize(rects), region_rects.size());
84 for (size_t i = 0; i < arraysize(rects) && i < region_rects.size(); ++i)
85 EXPECT_EQ(rects[i], region_rects[i]);
86}
87
88// Check that a path enclosing two non-adjacent areas is correctly translated
89// to a non-contiguous region.
90TEST(CreateHRGNFromSkPathTest, NonContiguousPath) {
91 const SkIRect rects[] = {
92 { 0, 0, 50, 50},
93 { 100, 100, 150, 150},
94 };
95
96 Path path;
97 for (const SkIRect& rect : rects) {
98 path.addRect(SkRect::Make(rect));
99 }
100 base::win::ScopedRegion region(CreateHRGNFromSkPath(path));
anpol280746c2015-12-18 08:39:03101 const std::vector<SkIRect>& region_rects = GetRectsFromHRGN(region.get());
alex-ac2cb37ad82014-11-13 18:03:29102 ASSERT_EQ(arraysize(rects), region_rects.size());
103 for (size_t i = 0; i < arraysize(rects); ++i)
104 EXPECT_EQ(rects[i], region_rects[i]);
105}
106
107// Check that empty region is returned for empty path.
108TEST(CreateHRGNFromSkPathTest, EmptyPath) {
109 Path path;
110 base::win::ScopedRegion empty_region(::CreateRectRgn(0, 0, 0, 0));
111 base::win::ScopedRegion region(CreateHRGNFromSkPath(path));
anpol280746c2015-12-18 08:39:03112 EXPECT_TRUE(::EqualRgn(empty_region.get(), region.get()));
alex-ac2cb37ad82014-11-13 18:03:29113}
114
115} // namespace gfx
116