1 // Copyright (c) 2009 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/Source/WebKit/chromium/public/WebCursorInfo.h"
8 #include "webkit/glue/webcursor.h"
9 #include "webkit/tools/test_shell/test_shell_test.h"
10
11 using WebKit::WebCursorInfo;
12
TEST(WebCursorTest,CursorSerialization)13 TEST(WebCursorTest, CursorSerialization) {
14 WebCursor custom_cursor;
15 // This is a valid custom cursor.
16 Pickle ok_custom_pickle;
17 // Type and hotspots.
18 ok_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
19 ok_custom_pickle.WriteInt(0);
20 ok_custom_pickle.WriteInt(0);
21 // X & Y
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 void* iter = NULL;
30 EXPECT_TRUE(custom_cursor.Deserialize(&ok_custom_pickle, &iter));
31
32 // This custom cursor has not been send with enough data.
33 Pickle short_custom_pickle;
34 // Type and hotspots.
35 short_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
36 short_custom_pickle.WriteInt(0);
37 short_custom_pickle.WriteInt(0);
38 // X & Y
39 short_custom_pickle.WriteInt(1);
40 short_custom_pickle.WriteInt(1);
41 // Data len not including enough data for a 1x1 image.
42 short_custom_pickle.WriteInt(3);
43 short_custom_pickle.WriteUInt32(0);
44 // Custom Windows message.
45 ok_custom_pickle.WriteUInt32(0);
46 iter = NULL;
47 EXPECT_FALSE(custom_cursor.Deserialize(&short_custom_pickle, &iter));
48
49 // This custom cursor has enough data but is too big.
50 Pickle large_custom_pickle;
51 // Type and hotspots.
52 large_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
53 large_custom_pickle.WriteInt(0);
54 large_custom_pickle.WriteInt(0);
55 // X & Y
56 static const int kTooBigSize = 4096 + 1;
57 large_custom_pickle.WriteInt(kTooBigSize);
58 large_custom_pickle.WriteInt(1);
59 // Data len including enough data for a 4097x1 image.
60 large_custom_pickle.WriteInt(kTooBigSize * 4);
61 for (int i = 0; i < kTooBigSize; ++i)
62 large_custom_pickle.WriteUInt32(0);
63 // Custom Windows message.
64 ok_custom_pickle.WriteUInt32(0);
65 iter = NULL;
66 EXPECT_FALSE(custom_cursor.Deserialize(&large_custom_pickle, &iter));
67
68 // This custom cursor uses negative lengths.
69 Pickle neg_custom_pickle;
70 // Type and hotspots.
71 neg_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
72 neg_custom_pickle.WriteInt(0);
73 neg_custom_pickle.WriteInt(0);
74 // X & Y
75 neg_custom_pickle.WriteInt(-1);
76 neg_custom_pickle.WriteInt(-1);
77 // Data len including enough data for a 1x1 image.
78 neg_custom_pickle.WriteInt(4);
79 neg_custom_pickle.WriteUInt32(0);
80 // Custom Windows message.
81 neg_custom_pickle.WriteUInt32(0);
82 iter = NULL;
83 EXPECT_FALSE(custom_cursor.Deserialize(&neg_custom_pickle, &iter));
84
85 #if defined(OS_WIN)
86 Pickle win32_custom_pickle;
87 WebCursor win32_custom_cursor;
88 win32_custom_cursor.InitFromExternalCursor(
89 reinterpret_cast<HCURSOR>(1000));
90 EXPECT_TRUE(win32_custom_cursor.Serialize(&win32_custom_pickle));
91 iter = NULL;
92 EXPECT_TRUE(custom_cursor.Deserialize(&win32_custom_pickle, &iter));
93 EXPECT_EQ(reinterpret_cast<HCURSOR>(1000), custom_cursor.GetCursor(NULL));
94 #endif // OS_WIN
95 }
96
TEST(WebCursorTest,ClampHotspot)97 TEST(WebCursorTest, ClampHotspot) {
98 WebCursor custom_cursor;
99 // This is a valid custom cursor.
100 Pickle ok_custom_pickle;
101 // Type and hotspots.
102 ok_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
103 // Hotspot is invalid --- outside the bounds of the image.
104 ok_custom_pickle.WriteInt(5);
105 ok_custom_pickle.WriteInt(5);
106 // X & Y
107 ok_custom_pickle.WriteInt(2);
108 ok_custom_pickle.WriteInt(2);
109 // Data len including enough data for a 2x2 image.
110 ok_custom_pickle.WriteInt(4 * 4);
111 for (size_t i = 0; i < 4; i++)
112 ok_custom_pickle.WriteUInt32(0);
113 // Custom Windows message.
114 ok_custom_pickle.WriteUInt32(0);
115 void* iter = NULL;
116 ASSERT_TRUE(custom_cursor.Deserialize(&ok_custom_pickle, &iter));
117
118 // Convert to WebCursorInfo, make sure the hotspot got clamped.
119 WebCursorInfo info;
120 custom_cursor.GetCursorInfo(&info);
121 EXPECT_EQ(gfx::Point(1, 1), gfx::Point(info.hotSpot));
122
123 // Set hotspot to an invalid point again, pipe back through WebCursor,
124 // and make sure the hotspot got clamped again.
125 info.hotSpot = gfx::Point(-1, -1);
126 custom_cursor.InitFromCursorInfo(info);
127 custom_cursor.GetCursorInfo(&info);
128 EXPECT_EQ(gfx::Point(0, 0), gfx::Point(info.hotSpot));
129 }
130