Use the provided connection type to determine if online.
[chromium-blink-merge.git] / content / common / common_param_traits_unittest.cc
blob95669507179543958a59d9f6b678582b3327f4fe
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/rect.h"
19 #include "url/gurl.h"
21 // Tests that serialize/deserialize correctly understand each other
22 TEST(IPCMessageTest, Serialize) {
23 const char* serialize_cases[] = {
24 "http://www.google.com/",
25 "http://user:pass@host.com:888/foo;bar?baz#nop",
28 for (size_t i = 0; i < arraysize(serialize_cases); i++) {
29 GURL input(serialize_cases[i]);
30 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
31 IPC::ParamTraits<GURL>::Write(&msg, input);
33 GURL output;
34 PickleIterator iter(msg);
35 EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
37 // We want to test each component individually to make sure its range was
38 // correctly serialized and deserialized, not just the spec.
39 EXPECT_EQ(input.possibly_invalid_spec(), output.possibly_invalid_spec());
40 EXPECT_EQ(input.is_valid(), output.is_valid());
41 EXPECT_EQ(input.scheme(), output.scheme());
42 EXPECT_EQ(input.username(), output.username());
43 EXPECT_EQ(input.password(), output.password());
44 EXPECT_EQ(input.host(), output.host());
45 EXPECT_EQ(input.port(), output.port());
46 EXPECT_EQ(input.path(), output.path());
47 EXPECT_EQ(input.query(), output.query());
48 EXPECT_EQ(input.ref(), output.ref());
51 // Test an invalid GURL.
53 IPC::Message msg;
54 msg.WriteString("#inva://idurl/");
55 GURL output;
56 PickleIterator iter(msg);
57 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
60 // Also test the corrupt case.
61 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
62 msg.WriteInt(99);
63 GURL output;
64 PickleIterator iter(msg);
65 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
68 // Tests std::pair serialization
69 TEST(IPCMessageTest, Pair) {
70 typedef std::pair<std::string, std::string> TestPair;
72 TestPair input("foo", "bar");
73 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
74 IPC::ParamTraits<TestPair>::Write(&msg, input);
76 TestPair output;
77 PickleIterator iter(msg);
78 EXPECT_TRUE(IPC::ParamTraits<TestPair>::Read(&msg, &iter, &output));
79 EXPECT_EQ(output.first, "foo");
80 EXPECT_EQ(output.second, "bar");
83 // Tests bitmap serialization.
84 TEST(IPCMessageTest, Bitmap) {
85 SkBitmap bitmap;
87 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 5);
88 bitmap.allocPixels();
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.config(), output.config());
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());