RenderText: handle center-aligned text in UpdateCachedBoundsAndOffset()
[chromium-blink-merge.git] / content / common / common_param_traits_unittest.cc
blobc1f763af4e2390677e2481b6c79b5ad79a7525cd
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 <string.h>
6 #include <utility>
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "content/public/common/common_param_traits.h"
11 #include "ipc/ipc_message.h"
12 #include "ipc/ipc_message_utils.h"
13 #include "net/base/host_port_pair.h"
14 #include "printing/backend/print_backend.h"
15 #include "printing/page_range.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/gfx/ipc/gfx_param_traits.h"
19 #include "ui/gfx/rect.h"
20 #include "url/gurl.h"
22 // Tests that serialize/deserialize correctly understand each other
23 TEST(IPCMessageTest, Serialize) {
24 const char* serialize_cases[] = {
25 "http://www.google.com/",
26 "http://user:pass@host.com:888/foo;bar?baz#nop",
29 for (size_t i = 0; i < arraysize(serialize_cases); i++) {
30 GURL input(serialize_cases[i]);
31 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
32 IPC::ParamTraits<GURL>::Write(&msg, input);
34 GURL output;
35 PickleIterator iter(msg);
36 EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
38 // We want to test each component individually to make sure its range was
39 // correctly serialized and deserialized, not just the spec.
40 EXPECT_EQ(input.possibly_invalid_spec(), output.possibly_invalid_spec());
41 EXPECT_EQ(input.is_valid(), output.is_valid());
42 EXPECT_EQ(input.scheme(), output.scheme());
43 EXPECT_EQ(input.username(), output.username());
44 EXPECT_EQ(input.password(), output.password());
45 EXPECT_EQ(input.host(), output.host());
46 EXPECT_EQ(input.port(), output.port());
47 EXPECT_EQ(input.path(), output.path());
48 EXPECT_EQ(input.query(), output.query());
49 EXPECT_EQ(input.ref(), output.ref());
52 // Test an invalid GURL.
54 IPC::Message msg;
55 msg.WriteString("#inva://idurl/");
56 GURL output;
57 PickleIterator iter(msg);
58 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
61 // Also test the corrupt case.
62 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
63 msg.WriteInt(99);
64 GURL output;
65 PickleIterator iter(msg);
66 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
69 // Tests std::pair serialization
70 TEST(IPCMessageTest, Pair) {
71 typedef std::pair<std::string, std::string> TestPair;
73 TestPair input("foo", "bar");
74 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
75 IPC::ParamTraits<TestPair>::Write(&msg, input);
77 TestPair output;
78 PickleIterator iter(msg);
79 EXPECT_TRUE(IPC::ParamTraits<TestPair>::Read(&msg, &iter, &output));
80 EXPECT_EQ(output.first, "foo");
81 EXPECT_EQ(output.second, "bar");
84 // Tests bitmap serialization.
85 TEST(IPCMessageTest, Bitmap) {
86 SkBitmap bitmap;
88 bitmap.allocN32Pixels(10, 5);
89 memset(bitmap.getPixels(), 'A', bitmap.getSize());
91 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
92 IPC::ParamTraits<SkBitmap>::Write(&msg, bitmap);
94 SkBitmap output;
95 PickleIterator iter(msg);
96 EXPECT_TRUE(IPC::ParamTraits<SkBitmap>::Read(&msg, &iter, &output));
98 EXPECT_EQ(bitmap.colorType(), output.colorType());
99 EXPECT_EQ(bitmap.width(), output.width());
100 EXPECT_EQ(bitmap.height(), output.height());
101 EXPECT_EQ(bitmap.rowBytes(), output.rowBytes());
102 EXPECT_EQ(bitmap.getSize(), output.getSize());
103 EXPECT_EQ(memcmp(bitmap.getPixels(), output.getPixels(), bitmap.getSize()),
106 // Also test the corrupt case.
107 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
108 // Copy the first message block over to |bad_msg|.
109 const char* fixed_data;
110 int fixed_data_size;
111 iter = PickleIterator(msg);
112 EXPECT_TRUE(msg.ReadData(&iter, &fixed_data, &fixed_data_size));
113 bad_msg.WriteData(fixed_data, fixed_data_size);
114 // Add some bogus pixel data.
115 const size_t bogus_pixels_size = bitmap.getSize() * 2;
116 scoped_ptr<char[]> bogus_pixels(new char[bogus_pixels_size]);
117 memset(bogus_pixels.get(), 'B', bogus_pixels_size);
118 bad_msg.WriteData(bogus_pixels.get(), bogus_pixels_size);
119 // Make sure we don't read out the bitmap!
120 SkBitmap bad_output;
121 iter = PickleIterator(bad_msg);
122 EXPECT_FALSE(IPC::ParamTraits<SkBitmap>::Read(&bad_msg, &iter, &bad_output));
125 TEST(IPCMessageTest, ListValue) {
126 base::ListValue input;
127 input.Set(0, new base::FundamentalValue(42.42));
128 input.Set(1, new base::StringValue("forty"));
129 input.Set(2, base::Value::CreateNullValue());
131 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
132 IPC::WriteParam(&msg, input);
134 base::ListValue output;
135 PickleIterator iter(msg);
136 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
138 EXPECT_TRUE(input.Equals(&output));
140 // Also test the corrupt case.
141 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
142 bad_msg.WriteInt(99);
143 iter = PickleIterator(bad_msg);
144 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
147 TEST(IPCMessageTest, DictionaryValue) {
148 base::DictionaryValue input;
149 input.Set("null", base::Value::CreateNullValue());
150 input.Set("bool", new base::FundamentalValue(true));
151 input.Set("int", new base::FundamentalValue(42));
153 scoped_ptr<base::DictionaryValue> subdict(new base::DictionaryValue());
154 subdict->Set("str", new base::StringValue("forty two"));
155 subdict->Set("bool", new base::FundamentalValue(false));
157 scoped_ptr<base::ListValue> sublist(new base::ListValue());
158 sublist->Set(0, new base::FundamentalValue(42.42));
159 sublist->Set(1, new base::StringValue("forty"));
160 sublist->Set(2, new base::StringValue("two"));
161 subdict->Set("list", sublist.release());
163 input.Set("dict", subdict.release());
165 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
166 IPC::WriteParam(&msg, input);
168 base::DictionaryValue output;
169 PickleIterator iter(msg);
170 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
172 EXPECT_TRUE(input.Equals(&output));
174 // Also test the corrupt case.
175 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
176 bad_msg.WriteInt(99);
177 iter = PickleIterator(bad_msg);
178 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
181 // Tests net::HostPortPair serialization
182 TEST(IPCMessageTest, HostPortPair) {
183 net::HostPortPair input("host.com", 12345);
185 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
186 IPC::ParamTraits<net::HostPortPair>::Write(&msg, input);
188 net::HostPortPair output;
189 PickleIterator iter(msg);
190 EXPECT_TRUE(IPC::ParamTraits<net::HostPortPair>::Read(&msg, &iter, &output));
191 EXPECT_EQ(input.host(), output.host());
192 EXPECT_EQ(input.port(), output.port());