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/Source/WebKit/chromium/public/WebCursorInfo.h"
8 #include "webkit/glue/webcursor.h"
9 #include "webkit/tools/test_shell/test_shell_test.h"
11 using WebKit::WebCursorInfo
;
13 TEST(WebCursorTest
, OKCursorSerialization
) {
14 WebCursor custom_cursor
;
15 // This is a valid custom cursor.
16 Pickle ok_custom_pickle
;
18 ok_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
19 ok_custom_pickle
.WriteInt(0);
20 ok_custom_pickle
.WriteInt(0);
22 ok_custom_pickle
.WriteInt(1);
23 ok_custom_pickle
.WriteInt(1);
24 // Data len including enough data for a 1x1 image.
25 ok_custom_pickle
.WriteInt(4);
26 ok_custom_pickle
.WriteUInt32(0);
27 // Custom Windows message.
28 ok_custom_pickle
.WriteUInt32(0);
29 PickleIterator
iter(ok_custom_pickle
);
30 EXPECT_TRUE(custom_cursor
.Deserialize(&iter
));
32 #if defined(TOOLKIT_GTK)
33 // On GTK+ using platforms, we should get a real native GdkCursor object back
34 // (and the memory used should automatically be freed by the WebCursor object
35 // for valgrind tests).
36 EXPECT_TRUE(custom_cursor
.GetCustomCursor());
40 TEST(WebCursorTest
, BrokenCursorSerialization
) {
41 WebCursor custom_cursor
;
42 // This custom cursor has not been send with enough data.
43 Pickle short_custom_pickle
;
45 short_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
46 short_custom_pickle
.WriteInt(0);
47 short_custom_pickle
.WriteInt(0);
49 short_custom_pickle
.WriteInt(1);
50 short_custom_pickle
.WriteInt(1);
51 // Data len not including enough data for a 1x1 image.
52 short_custom_pickle
.WriteInt(3);
53 short_custom_pickle
.WriteUInt32(0);
54 PickleIterator
iter(short_custom_pickle
);
55 EXPECT_FALSE(custom_cursor
.Deserialize(&iter
));
57 // This custom cursor has enough data but is too big.
58 Pickle large_custom_pickle
;
60 large_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
61 large_custom_pickle
.WriteInt(0);
62 large_custom_pickle
.WriteInt(0);
64 static const int kTooBigSize
= 4096 + 1;
65 large_custom_pickle
.WriteInt(kTooBigSize
);
66 large_custom_pickle
.WriteInt(1);
67 // Data len including enough data for a 4097x1 image.
68 large_custom_pickle
.WriteInt(kTooBigSize
* 4);
69 for (int i
= 0; i
< kTooBigSize
; ++i
)
70 large_custom_pickle
.WriteUInt32(0);
71 iter
= PickleIterator(large_custom_pickle
);
72 EXPECT_FALSE(custom_cursor
.Deserialize(&iter
));
74 // This custom cursor uses negative lengths.
75 Pickle neg_custom_pickle
;
77 neg_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
78 neg_custom_pickle
.WriteInt(0);
79 neg_custom_pickle
.WriteInt(0);
81 neg_custom_pickle
.WriteInt(-1);
82 neg_custom_pickle
.WriteInt(-1);
83 // Data len including enough data for a 1x1 image.
84 neg_custom_pickle
.WriteInt(4);
85 neg_custom_pickle
.WriteUInt32(0);
86 // Custom Windows message.
87 neg_custom_pickle
.WriteUInt32(0);
88 iter
= PickleIterator(neg_custom_pickle
);
89 EXPECT_FALSE(custom_cursor
.Deserialize(&iter
));
93 TEST(WebCursorTest
, WindowsCursorConversion
) {
94 WebCursor custom_cursor
;
95 Pickle win32_custom_pickle
;
96 WebCursor win32_custom_cursor
;
97 win32_custom_cursor
.InitFromExternalCursor(
98 reinterpret_cast<HCURSOR
>(1000));
99 EXPECT_TRUE(win32_custom_cursor
.Serialize(&win32_custom_pickle
));
100 PickleIterator
iter(win32_custom_pickle
);
101 EXPECT_TRUE(custom_cursor
.Deserialize(&iter
));
102 EXPECT_EQ(reinterpret_cast<HCURSOR
>(1000), custom_cursor
.GetCursor(NULL
));
106 TEST(WebCursorTest
, ClampHotspot
) {
107 WebCursor custom_cursor
;
108 // This is a valid custom cursor.
109 Pickle ok_custom_pickle
;
110 // Type and hotspots.
111 ok_custom_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
112 // Hotspot is invalid --- outside the bounds of the image.
113 ok_custom_pickle
.WriteInt(5);
114 ok_custom_pickle
.WriteInt(5);
116 ok_custom_pickle
.WriteInt(2);
117 ok_custom_pickle
.WriteInt(2);
118 // Data len including enough data for a 2x2 image.
119 ok_custom_pickle
.WriteInt(4 * 4);
120 for (size_t i
= 0; i
< 4; i
++)
121 ok_custom_pickle
.WriteUInt32(0);
122 // Custom Windows message.
123 ok_custom_pickle
.WriteUInt32(0);
124 PickleIterator
iter(ok_custom_pickle
);
125 ASSERT_TRUE(custom_cursor
.Deserialize(&iter
));
127 // Convert to WebCursorInfo, make sure the hotspot got clamped.
129 custom_cursor
.GetCursorInfo(&info
);
130 EXPECT_EQ(gfx::Point(1, 1), gfx::Point(info
.hotSpot
));
132 // Set hotspot to an invalid point again, pipe back through WebCursor,
133 // and make sure the hotspot got clamped again.
134 info
.hotSpot
= gfx::Point(-1, -1);
135 custom_cursor
.InitFromCursorInfo(info
);
136 custom_cursor
.GetCursorInfo(&info
);
137 EXPECT_EQ(gfx::Point(0, 0), gfx::Point(info
.hotSpot
));
140 TEST(WebCursorTest
, EmptyImage
) {
141 WebCursor custom_cursor
;
142 Pickle broken_cursor_pickle
;
143 broken_cursor_pickle
.WriteInt(WebCursorInfo::TypeCustom
);
144 // Hotspot is at origin
145 broken_cursor_pickle
.WriteInt(0);
146 broken_cursor_pickle
.WriteInt(0);
148 broken_cursor_pickle
.WriteInt(0);
149 broken_cursor_pickle
.WriteInt(0);
150 // No data for the image since the size is 0.
151 broken_cursor_pickle
.WriteInt(0);
152 // Custom Windows message.
153 broken_cursor_pickle
.WriteInt(0);
155 // Make sure we can read this on all platforms; it is technicaally a valid
157 PickleIterator
iter(broken_cursor_pickle
);
158 ASSERT_TRUE(custom_cursor
.Deserialize(&iter
));
160 #if defined(TOOLKIT_GTK)
161 // On GTK+ using platforms, we make sure that we get NULL back from this
162 // method; the relevant GDK methods take NULL as a request to use the default
164 EXPECT_EQ(NULL
, custom_cursor
.GetCustomCursor());