1 // Copyright (c) 2012 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.
5 #include "base/pickle.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/WebKit/public/web/WebCursorInfo.h"
8 #include "webkit/common/cursors/webcursor.h"
10 using WebKit::WebCursorInfo
;
12 TEST(WebCursorTest
, OKCursorSerialization
) {
13 WebCursor custom_cursor
;
14 // This is a valid custom cursor.
15 Pickle ok_custom_pickle
;
17 ok_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
18 ok_custom_pickle
.WriteInt(0);
19 ok_custom_pickle
.WriteInt(0);
21 ok_custom_pickle
.WriteInt(1);
22 ok_custom_pickle
.WriteInt(1);
24 ok_custom_pickle
.WriteFloat(1.0);
25 // Data len including enough data for a 1x1 image.
26 ok_custom_pickle
.WriteInt(4);
27 ok_custom_pickle
.WriteUInt32(0);
28 // Custom Windows message.
29 ok_custom_pickle
.WriteUInt32(0);
30 PickleIterator
iter(ok_custom_pickle
);
31 EXPECT_TRUE(custom_cursor
.Deserialize(&iter
));
33 #if defined(TOOLKIT_GTK)
34 // On GTK+ using platforms, we should get a real native GdkCursor object back
35 // (and the memory used should automatically be freed by the WebCursor object
36 // for valgrind tests).
37 EXPECT_TRUE(custom_cursor
.GetCustomCursor());
41 TEST(WebCursorTest
, BrokenCursorSerialization
) {
42 WebCursor custom_cursor
;
43 // This custom cursor has not been send with enough data.
44 Pickle short_custom_pickle
;
46 short_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
47 short_custom_pickle
.WriteInt(0);
48 short_custom_pickle
.WriteInt(0);
50 short_custom_pickle
.WriteInt(1);
51 short_custom_pickle
.WriteInt(1);
53 short_custom_pickle
.WriteFloat(1.0);
54 // Data len not including enough data for a 1x1 image.
55 short_custom_pickle
.WriteInt(3);
56 short_custom_pickle
.WriteUInt32(0);
57 PickleIterator
iter(short_custom_pickle
);
58 EXPECT_FALSE(custom_cursor
.Deserialize(&iter
));
60 // This custom cursor has enough data but is too big.
61 Pickle large_custom_pickle
;
63 large_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
64 large_custom_pickle
.WriteInt(0);
65 large_custom_pickle
.WriteInt(0);
67 static const int kTooBigSize
= 4096 + 1;
68 large_custom_pickle
.WriteInt(kTooBigSize
);
69 large_custom_pickle
.WriteInt(1);
71 large_custom_pickle
.WriteFloat(1.0);
72 // Data len including enough data for a 4097x1 image.
73 large_custom_pickle
.WriteInt(kTooBigSize
* 4);
74 for (int i
= 0; i
< kTooBigSize
; ++i
)
75 large_custom_pickle
.WriteUInt32(0);
76 iter
= PickleIterator(large_custom_pickle
);
77 EXPECT_FALSE(custom_cursor
.Deserialize(&iter
));
79 // This custom cursor uses negative lengths.
80 Pickle neg_custom_pickle
;
82 neg_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
83 neg_custom_pickle
.WriteInt(0);
84 neg_custom_pickle
.WriteInt(0);
86 neg_custom_pickle
.WriteInt(-1);
87 neg_custom_pickle
.WriteInt(-1);
89 neg_custom_pickle
.WriteFloat(1.0);
90 // Data len including enough data for a 1x1 image.
91 neg_custom_pickle
.WriteInt(4);
92 neg_custom_pickle
.WriteUInt32(0);
93 // Custom Windows message.
94 neg_custom_pickle
.WriteUInt32(0);
95 iter
= PickleIterator(neg_custom_pickle
);
96 EXPECT_FALSE(custom_cursor
.Deserialize(&iter
));
98 // This custom cursor uses zero scale.
99 Pickle scale_zero_custom_pickle
;
100 // Type and hotspots.
101 scale_zero_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
102 scale_zero_custom_pickle
.WriteInt(0);
103 scale_zero_custom_pickle
.WriteInt(0);
105 scale_zero_custom_pickle
.WriteInt(1);
106 scale_zero_custom_pickle
.WriteInt(1);
108 scale_zero_custom_pickle
.WriteFloat(0);
109 // Data len including enough data for a 1x1 image.
110 scale_zero_custom_pickle
.WriteInt(4);
111 scale_zero_custom_pickle
.WriteUInt32(0);
112 // Custom Windows message.
113 scale_zero_custom_pickle
.WriteUInt32(0);
114 iter
= PickleIterator(scale_zero_custom_pickle
);
115 EXPECT_FALSE(custom_cursor
.Deserialize(&iter
));
117 // This custom cursor uses tiny scale.
118 Pickle scale_tiny_custom_pickle
;
119 // Type and hotspots.
120 scale_tiny_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
121 scale_tiny_custom_pickle
.WriteInt(0);
122 scale_tiny_custom_pickle
.WriteInt(0);
124 scale_tiny_custom_pickle
.WriteInt(1);
125 scale_tiny_custom_pickle
.WriteInt(1);
127 scale_tiny_custom_pickle
.WriteFloat(0.001f
);
128 // Data len including enough data for a 1x1 image.
129 scale_tiny_custom_pickle
.WriteInt(4);
130 scale_tiny_custom_pickle
.WriteUInt32(0);
131 // Custom Windows message.
132 scale_tiny_custom_pickle
.WriteUInt32(0);
133 iter
= PickleIterator(scale_tiny_custom_pickle
);
134 EXPECT_FALSE(custom_cursor
.Deserialize(&iter
));
137 #if defined(OS_WIN) && !defined(USE_AURA)
138 TEST(WebCursorTest
, WindowsCursorConversion
) {
139 WebCursor custom_cursor
;
140 Pickle win32_custom_pickle
;
141 WebCursor win32_custom_cursor
;
142 win32_custom_cursor
.InitFromExternalCursor(
143 reinterpret_cast<HCURSOR
>(1000));
144 EXPECT_TRUE(win32_custom_cursor
.Serialize(&win32_custom_pickle
));
145 PickleIterator
iter(win32_custom_pickle
);
146 EXPECT_TRUE(custom_cursor
.Deserialize(&iter
));
147 EXPECT_EQ(reinterpret_cast<HCURSOR
>(1000), custom_cursor
.GetCursor(NULL
));
151 TEST(WebCursorTest
, ClampHotspot
) {
152 WebCursor custom_cursor
;
153 // This is a valid custom cursor.
154 Pickle ok_custom_pickle
;
155 // Type and hotspots.
156 ok_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
157 // Hotspot is invalid --- outside the bounds of the image.
158 ok_custom_pickle
.WriteInt(5);
159 ok_custom_pickle
.WriteInt(5);
161 ok_custom_pickle
.WriteInt(2);
162 ok_custom_pickle
.WriteInt(2);
164 ok_custom_pickle
.WriteFloat(1.0);
165 // Data len including enough data for a 2x2 image.
166 ok_custom_pickle
.WriteInt(4 * 4);
167 for (size_t i
= 0; i
< 4; i
++)
168 ok_custom_pickle
.WriteUInt32(0);
169 // Custom Windows message.
170 ok_custom_pickle
.WriteUInt32(0);
171 PickleIterator
iter(ok_custom_pickle
);
172 ASSERT_TRUE(custom_cursor
.Deserialize(&iter
));
174 // Convert to WebCursorInfo, make sure the hotspot got clamped.
175 WebCursor::CursorInfo info
;
176 custom_cursor
.GetCursorInfo(&info
);
177 EXPECT_EQ(gfx::Point(1, 1), info
.hotspot
);
179 // Set hotspot to an invalid point again, pipe back through WebCursor,
180 // and make sure the hotspot got clamped again.
181 info
.hotspot
= gfx::Point(-1, -1);
182 custom_cursor
.InitFromCursorInfo(info
);
183 custom_cursor
.GetCursorInfo(&info
);
184 EXPECT_EQ(gfx::Point(0, 0), info
.hotspot
);
187 TEST(WebCursorTest
, EmptyImage
) {
188 WebCursor custom_cursor
;
189 Pickle broken_cursor_pickle
;
190 broken_cursor_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
191 // Hotspot is at origin
192 broken_cursor_pickle
.WriteInt(0);
193 broken_cursor_pickle
.WriteInt(0);
195 broken_cursor_pickle
.WriteInt(0);
196 broken_cursor_pickle
.WriteInt(0);
198 broken_cursor_pickle
.WriteFloat(1.0);
199 // No data for the image since the size is 0.
200 broken_cursor_pickle
.WriteInt(0);
201 // Custom Windows message.
202 broken_cursor_pickle
.WriteInt(0);
204 // Make sure we can read this on all platforms; it is technicaally a valid
206 PickleIterator
iter(broken_cursor_pickle
);
207 ASSERT_TRUE(custom_cursor
.Deserialize(&iter
));
209 #if defined(TOOLKIT_GTK)
210 // On GTK+ using platforms, we make sure that we get NULL back from this
211 // method; the relevant GDK methods take NULL as a request to use the default
213 EXPECT_EQ(NULL
, custom_cursor
.GetCustomCursor());
217 TEST(WebCursorTest
, Scale2
) {
218 WebCursor custom_cursor
;
219 // This is a valid custom cursor.
220 Pickle ok_custom_pickle
;
221 // Type and hotspots.
222 ok_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
223 ok_custom_pickle
.WriteInt(0);
224 ok_custom_pickle
.WriteInt(0);
226 ok_custom_pickle
.WriteInt(1);
227 ok_custom_pickle
.WriteInt(1);
228 // Scale - 2 image pixels per UI pixel.
229 ok_custom_pickle
.WriteFloat(2.0);
230 // Data len including enough data for a 1x1 image.
231 ok_custom_pickle
.WriteInt(4);
232 ok_custom_pickle
.WriteUInt32(0);
233 // Custom Windows message.
234 ok_custom_pickle
.WriteUInt32(0);
235 PickleIterator
iter(ok_custom_pickle
);
236 EXPECT_TRUE(custom_cursor
.Deserialize(&iter
));
238 #if defined(TOOLKIT_GTK)
239 // On GTK+ using platforms, we should get a real native GdkCursor object back
240 // (and the memory used should automatically be freed by the WebCursor object
241 // for valgrind tests).
242 EXPECT_TRUE(custom_cursor
.GetCustomCursor());