Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: webkit/common/cursors/webcursor_unittest.cc

Issue 201473002: Move webcursors code from webkit/ to content. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: gfx image include Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/common/cursors/webcursor_null.cc ('k') | webkit/common/cursors/webcursor_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/pickle.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
8 #include "webkit/common/cursors/webcursor.h"
9
10 using blink::WebCursorInfo;
11
12 TEST(WebCursorTest, OKCursorSerialization) {
13 WebCursor custom_cursor;
14 // This is a valid custom cursor.
15 Pickle ok_custom_pickle;
16 // Type and hotspots.
17 ok_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
18 ok_custom_pickle.WriteInt(0);
19 ok_custom_pickle.WriteInt(0);
20 // X & Y
21 ok_custom_pickle.WriteInt(1);
22 ok_custom_pickle.WriteInt(1);
23 // Scale
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));
32
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());
38 #endif
39 }
40
41 TEST(WebCursorTest, BrokenCursorSerialization) {
42 WebCursor custom_cursor;
43 // This custom cursor has not been send with enough data.
44 Pickle short_custom_pickle;
45 // Type and hotspots.
46 short_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
47 short_custom_pickle.WriteInt(0);
48 short_custom_pickle.WriteInt(0);
49 // X & Y
50 short_custom_pickle.WriteInt(1);
51 short_custom_pickle.WriteInt(1);
52 // Scale
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));
59
60 // This custom cursor has enough data but is too big.
61 Pickle large_custom_pickle;
62 // Type and hotspots.
63 large_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
64 large_custom_pickle.WriteInt(0);
65 large_custom_pickle.WriteInt(0);
66 // X & Y
67 static const int kTooBigSize = 4096 + 1;
68 large_custom_pickle.WriteInt(kTooBigSize);
69 large_custom_pickle.WriteInt(1);
70 // Scale
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));
78
79 // This custom cursor uses negative lengths.
80 Pickle neg_custom_pickle;
81 // Type and hotspots.
82 neg_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
83 neg_custom_pickle.WriteInt(0);
84 neg_custom_pickle.WriteInt(0);
85 // X & Y
86 neg_custom_pickle.WriteInt(-1);
87 neg_custom_pickle.WriteInt(-1);
88 // Scale
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));
97
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);
104 // X & Y
105 scale_zero_custom_pickle.WriteInt(1);
106 scale_zero_custom_pickle.WriteInt(1);
107 // Scale
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));
116
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);
123 // X & Y
124 scale_tiny_custom_pickle.WriteInt(1);
125 scale_tiny_custom_pickle.WriteInt(1);
126 // Scale
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));
135 }
136
137 TEST(WebCursorTest, ClampHotspot) {
138 WebCursor custom_cursor;
139 // This is a valid custom cursor.
140 Pickle ok_custom_pickle;
141 // Type and hotspots.
142 ok_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
143 // Hotspot is invalid --- outside the bounds of the image.
144 ok_custom_pickle.WriteInt(5);
145 ok_custom_pickle.WriteInt(5);
146 // X & Y
147 ok_custom_pickle.WriteInt(2);
148 ok_custom_pickle.WriteInt(2);
149 // Scale
150 ok_custom_pickle.WriteFloat(1.0);
151 // Data len including enough data for a 2x2 image.
152 ok_custom_pickle.WriteInt(4 * 4);
153 for (size_t i = 0; i < 4; i++)
154 ok_custom_pickle.WriteUInt32(0);
155 // Custom Windows message.
156 ok_custom_pickle.WriteUInt32(0);
157 PickleIterator iter(ok_custom_pickle);
158 ASSERT_TRUE(custom_cursor.Deserialize(&iter));
159
160 // Convert to WebCursorInfo, make sure the hotspot got clamped.
161 WebCursor::CursorInfo info;
162 custom_cursor.GetCursorInfo(&info);
163 EXPECT_EQ(gfx::Point(1, 1), info.hotspot);
164
165 // Set hotspot to an invalid point again, pipe back through WebCursor,
166 // and make sure the hotspot got clamped again.
167 info.hotspot = gfx::Point(-1, -1);
168 custom_cursor.InitFromCursorInfo(info);
169 custom_cursor.GetCursorInfo(&info);
170 EXPECT_EQ(gfx::Point(0, 0), info.hotspot);
171 }
172
173 TEST(WebCursorTest, EmptyImage) {
174 WebCursor custom_cursor;
175 Pickle broken_cursor_pickle;
176 broken_cursor_pickle.WriteInt(WebCursorInfo::TypeCustom);
177 // Hotspot is at origin
178 broken_cursor_pickle.WriteInt(0);
179 broken_cursor_pickle.WriteInt(0);
180 // X & Y are empty
181 broken_cursor_pickle.WriteInt(0);
182 broken_cursor_pickle.WriteInt(0);
183 // Scale
184 broken_cursor_pickle.WriteFloat(1.0);
185 // No data for the image since the size is 0.
186 broken_cursor_pickle.WriteInt(0);
187 // Custom Windows message.
188 broken_cursor_pickle.WriteInt(0);
189
190 // Make sure we can read this on all platforms; it is technicaally a valid
191 // cursor.
192 PickleIterator iter(broken_cursor_pickle);
193 ASSERT_TRUE(custom_cursor.Deserialize(&iter));
194
195 #if defined(TOOLKIT_GTK)
196 // On GTK+ using platforms, we make sure that we get NULL back from this
197 // method; the relevant GDK methods take NULL as a request to use the default
198 // cursor.
199 EXPECT_EQ(NULL, custom_cursor.GetCustomCursor());
200 #endif
201 }
202
203 TEST(WebCursorTest, Scale2) {
204 WebCursor custom_cursor;
205 // This is a valid custom cursor.
206 Pickle ok_custom_pickle;
207 // Type and hotspots.
208 ok_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
209 ok_custom_pickle.WriteInt(0);
210 ok_custom_pickle.WriteInt(0);
211 // X & Y
212 ok_custom_pickle.WriteInt(1);
213 ok_custom_pickle.WriteInt(1);
214 // Scale - 2 image pixels per UI pixel.
215 ok_custom_pickle.WriteFloat(2.0);
216 // Data len including enough data for a 1x1 image.
217 ok_custom_pickle.WriteInt(4);
218 ok_custom_pickle.WriteUInt32(0);
219 // Custom Windows message.
220 ok_custom_pickle.WriteUInt32(0);
221 PickleIterator iter(ok_custom_pickle);
222 EXPECT_TRUE(custom_cursor.Deserialize(&iter));
223
224 #if defined(TOOLKIT_GTK)
225 // On GTK+ using platforms, we should get a real native GdkCursor object back
226 // (and the memory used should automatically be freed by the WebCursor object
227 // for valgrind tests).
228 EXPECT_TRUE(custom_cursor.GetCustomCursor());
229 #endif
230 }
OLDNEW
« no previous file with comments | « webkit/common/cursors/webcursor_null.cc ('k') | webkit/common/cursors/webcursor_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698