#include "base/pickle.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
#include "webkit/glue/webcursor.h"
#include "webkit/tools/test_shell/test_shell_test.h"
using WebKit::WebCursorInfo;
TEST(WebCursorTest, OKCursorSerialization) {
WebCursor custom_cursor;
Pickle ok_custom_pickle;
ok_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
ok_custom_pickle.WriteInt(0);
ok_custom_pickle.WriteInt(0);
ok_custom_pickle.WriteInt(1);
ok_custom_pickle.WriteInt(1);
ok_custom_pickle.WriteInt(4);
ok_custom_pickle.WriteUInt32(0);
ok_custom_pickle.WriteUInt32(0);
PickleIterator iter(ok_custom_pickle);
EXPECT_TRUE(custom_cursor.Deserialize(&iter));
#if defined(TOOLKIT_GTK)
EXPECT_TRUE(custom_cursor.GetCustomCursor());
#endif
}
TEST(WebCursorTest, BrokenCursorSerialization) {
WebCursor custom_cursor;
Pickle short_custom_pickle;
short_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
short_custom_pickle.WriteInt(0);
short_custom_pickle.WriteInt(0);
short_custom_pickle.WriteInt(1);
short_custom_pickle.WriteInt(1);
short_custom_pickle.WriteInt(3);
short_custom_pickle.WriteUInt32(0);
PickleIterator iter(short_custom_pickle);
EXPECT_FALSE(custom_cursor.Deserialize(&iter));
Pickle large_custom_pickle;
large_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
large_custom_pickle.WriteInt(0);
large_custom_pickle.WriteInt(0);
static const int kTooBigSize = 4096 + 1;
large_custom_pickle.WriteInt(kTooBigSize);
large_custom_pickle.WriteInt(1);
large_custom_pickle.WriteInt(kTooBigSize * 4);
for (int i = 0; i < kTooBigSize; ++i)
large_custom_pickle.WriteUInt32(0);
iter = PickleIterator(large_custom_pickle);
EXPECT_FALSE(custom_cursor.Deserialize(&iter));
Pickle neg_custom_pickle;
neg_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
neg_custom_pickle.WriteInt(0);
neg_custom_pickle.WriteInt(0);
neg_custom_pickle.WriteInt(-1);
neg_custom_pickle.WriteInt(-1);
neg_custom_pickle.WriteInt(4);
neg_custom_pickle.WriteUInt32(0);
neg_custom_pickle.WriteUInt32(0);
iter = PickleIterator(neg_custom_pickle);
EXPECT_FALSE(custom_cursor.Deserialize(&iter));
}
#if defined(OS_WIN)
TEST(WebCursorTest, WindowsCursorConversion) {
WebCursor custom_cursor;
Pickle win32_custom_pickle;
WebCursor win32_custom_cursor;
win32_custom_cursor.InitFromExternalCursor(
reinterpret_cast<HCURSOR>(1000));
EXPECT_TRUE(win32_custom_cursor.Serialize(&win32_custom_pickle));
PickleIterator iter(win32_custom_pickle);
EXPECT_TRUE(custom_cursor.Deserialize(&iter));
EXPECT_EQ(reinterpret_cast<HCURSOR>(1000), custom_cursor.GetCursor(NULL));
}
#endif
TEST(WebCursorTest, ClampHotspot) {
WebCursor custom_cursor;
Pickle ok_custom_pickle;
ok_custom_pickle.WriteInt(WebCursorInfo::TypeCustom);
ok_custom_pickle.WriteInt(5);
ok_custom_pickle.WriteInt(5);
ok_custom_pickle.WriteInt(2);
ok_custom_pickle.WriteInt(2);
ok_custom_pickle.WriteInt(4 * 4);
for (size_t i = 0; i < 4; i++)
ok_custom_pickle.WriteUInt32(0);
ok_custom_pickle.WriteUInt32(0);
PickleIterator iter(ok_custom_pickle);
ASSERT_TRUE(custom_cursor.Deserialize(&iter));
WebCursorInfo info;
custom_cursor.GetCursorInfo(&info);
EXPECT_EQ(gfx::Point(1, 1), gfx::Point(info.hotSpot));
info.hotSpot = gfx::Point(-1, -1);
custom_cursor.InitFromCursorInfo(info);
custom_cursor.GetCursorInfo(&info);
EXPECT_EQ(gfx::Point(0, 0), gfx::Point(info.hotSpot));
}
TEST(WebCursorTest, EmptyImage) {
WebCursor custom_cursor;
Pickle broken_cursor_pickle;
broken_cursor_pickle.WriteInt(WebCursorInfo::TypeCustom);
broken_cursor_pickle.WriteInt(0);
broken_cursor_pickle.WriteInt(0);
broken_cursor_pickle.WriteInt(0);
broken_cursor_pickle.WriteInt(0);
broken_cursor_pickle.WriteInt(0);
broken_cursor_pickle.WriteInt(0);
PickleIterator iter(broken_cursor_pickle);
ASSERT_TRUE(custom_cursor.Deserialize(&iter));
#if defined(TOOLKIT_GTK)
EXPECT_EQ(NULL, custom_cursor.GetCustomCursor());
#endif
}