OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/pickle.h" | 7 #include "base/pickle.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebHTTPBody.
h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebHTTPBody.
h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 item.setURLString(WebString::fromUTF8("urlString")); | 55 item.setURLString(WebString::fromUTF8("urlString")); |
56 item.setOriginalURLString(WebString::fromUTF8("originalURLString")); | 56 item.setOriginalURLString(WebString::fromUTF8("originalURLString")); |
57 item.setTarget(WebString::fromUTF8("target")); | 57 item.setTarget(WebString::fromUTF8("target")); |
58 item.setParent(WebString::fromUTF8("parent")); | 58 item.setParent(WebString::fromUTF8("parent")); |
59 item.setTitle(WebString::fromUTF8("title")); | 59 item.setTitle(WebString::fromUTF8("title")); |
60 item.setAlternateTitle(WebString::fromUTF8("alternateTitle")); | 60 item.setAlternateTitle(WebString::fromUTF8("alternateTitle")); |
61 item.setLastVisitedTime(13.37); | 61 item.setLastVisitedTime(13.37); |
62 item.setScrollOffset(WebPoint(42, -42)); | 62 item.setScrollOffset(WebPoint(42, -42)); |
63 item.setIsTargetItem(true); | 63 item.setIsTargetItem(true); |
64 item.setVisitCount(42*42); | 64 item.setVisitCount(42*42); |
| 65 item.setPageScaleFactor(2.0f); |
| 66 item.setItemSequenceNumber(123); |
| 67 item.setDocumentSequenceNumber(456); |
65 | 68 |
66 WebVector<WebString> document_state(size_t(3)); | 69 WebVector<WebString> document_state(size_t(3)); |
67 document_state[0] = WebString::fromUTF8("state1"); | 70 document_state[0] = WebString::fromUTF8("state1"); |
68 document_state[1] = WebString::fromUTF8("state2"); | 71 document_state[1] = WebString::fromUTF8("state2"); |
69 document_state[2] = WebString::fromUTF8("state AWESOME"); | 72 document_state[2] = WebString::fromUTF8("state AWESOME"); |
70 item.setDocumentState(document_state); | 73 item.setDocumentState(document_state); |
71 item.setPageScaleFactor(1.0f); | |
72 | 74 |
73 // Form Data | 75 // Form Data |
74 if (with_form_data) { | 76 if (with_form_data) { |
75 item.setHTTPBody(MakeFormData()); | 77 item.setHTTPBody(MakeFormData()); |
76 item.setHTTPContentType(WebString::fromUTF8("formContentType")); | 78 item.setHTTPContentType(WebString::fromUTF8("formContentType")); |
77 } | 79 } |
78 | 80 |
79 // Setting the FormInfo causes the referrer to be set, so we set the | 81 // Setting the FormInfo causes the referrer to be set, so we set the |
80 // referrer after setting the form info. | 82 // referrer after setting the form info. |
81 item.setReferrer(WebString::fromUTF8("referrer")); | 83 item.setReferrer(WebString::fromUTF8("referrer")); |
82 | 84 |
83 // Children | 85 // Children |
84 if (pregnant) | 86 if (pregnant) |
85 item.appendToChildren(MakeHistoryItem(false, false)); | 87 item.appendToChildren(MakeHistoryItem(false, false)); |
86 | 88 |
87 return item; | 89 return item; |
88 } | 90 } |
89 | 91 |
90 WebHistoryItem MakeHistoryItemWithPasswordData(bool pregnant) { | 92 WebHistoryItem MakeHistoryItemWithPasswordData(bool pregnant) { |
91 WebHistoryItem item = MakeHistoryItem(false, pregnant); | 93 WebHistoryItem item = MakeHistoryItem(false, pregnant); |
92 item.setHTTPBody(MakeFormDataWithPasswords()); | 94 item.setHTTPBody(MakeFormDataWithPasswords()); |
93 item.setHTTPContentType(WebString::fromUTF8("formContentType")); | 95 item.setHTTPContentType(WebString::fromUTF8("formContentType")); |
94 return item; | 96 return item; |
95 } | 97 } |
96 | 98 |
97 // Checks that a == b. | 99 // Checks that a == b. |
98 void HistoryItemExpectEqual(const WebHistoryItem& a, | 100 void HistoryItemExpectEqual(const WebHistoryItem& a, |
99 const WebHistoryItem& b) { | 101 const WebHistoryItem& b, |
100 HistoryItemExpectBaseDataEqual(a, b); | 102 int version) { |
| 103 HistoryItemExpectBaseDataEqual(a, b, version); |
101 HistoryItemExpectFormDataEqual(a, b); | 104 HistoryItemExpectFormDataEqual(a, b); |
102 HistoryItemExpectChildrenEqual(a, b); | 105 HistoryItemExpectChildrenEqual(a, b); |
103 } | 106 } |
104 | 107 |
105 void HistoryItemExpectBaseDataEqual(const WebHistoryItem& a, | 108 void HistoryItemExpectBaseDataEqual(const WebHistoryItem& a, |
106 const WebHistoryItem& b) { | 109 const WebHistoryItem& b, |
| 110 int version) { |
107 EXPECT_EQ(string16(a.urlString()), string16(b.urlString())); | 111 EXPECT_EQ(string16(a.urlString()), string16(b.urlString())); |
108 EXPECT_EQ(string16(a.originalURLString()), string16(b.originalURLString())); | 112 EXPECT_EQ(string16(a.originalURLString()), string16(b.originalURLString())); |
109 EXPECT_EQ(string16(a.target()), string16(b.target())); | 113 EXPECT_EQ(string16(a.target()), string16(b.target())); |
110 EXPECT_EQ(string16(a.parent()), string16(b.parent())); | 114 EXPECT_EQ(string16(a.parent()), string16(b.parent())); |
111 EXPECT_EQ(string16(a.title()), string16(b.title())); | 115 EXPECT_EQ(string16(a.title()), string16(b.title())); |
112 EXPECT_EQ(string16(a.alternateTitle()), string16(b.alternateTitle())); | 116 EXPECT_EQ(string16(a.alternateTitle()), string16(b.alternateTitle())); |
113 EXPECT_EQ(a.lastVisitedTime(), b.lastVisitedTime()); | 117 EXPECT_EQ(a.lastVisitedTime(), b.lastVisitedTime()); |
114 EXPECT_EQ(a.scrollOffset(), b.scrollOffset()); | 118 EXPECT_EQ(a.scrollOffset(), b.scrollOffset()); |
115 EXPECT_EQ(a.isTargetItem(), b.isTargetItem()); | 119 EXPECT_EQ(a.isTargetItem(), b.isTargetItem()); |
116 EXPECT_EQ(a.visitCount(), b.visitCount()); | 120 EXPECT_EQ(a.visitCount(), b.visitCount()); |
117 EXPECT_EQ(string16(a.referrer()), string16(b.referrer())); | 121 EXPECT_EQ(string16(a.referrer()), string16(b.referrer())); |
118 EXPECT_EQ(a.pageScaleFactor(), b.pageScaleFactor()); | 122 if (version >= 11) |
| 123 EXPECT_EQ(a.pageScaleFactor(), b.pageScaleFactor()); |
| 124 if (version >= 9) |
| 125 EXPECT_EQ(a.itemSequenceNumber(), b.itemSequenceNumber()); |
| 126 if (version >= 6) |
| 127 EXPECT_EQ(a.documentSequenceNumber(), b.documentSequenceNumber()); |
119 | 128 |
120 const WebVector<WebString>& a_docstate = a.documentState(); | 129 const WebVector<WebString>& a_docstate = a.documentState(); |
121 const WebVector<WebString>& b_docstate = b.documentState(); | 130 const WebVector<WebString>& b_docstate = b.documentState(); |
122 EXPECT_EQ(a_docstate.size(), b_docstate.size()); | 131 EXPECT_EQ(a_docstate.size(), b_docstate.size()); |
123 for (size_t i = 0, c = a_docstate.size(); i < c; ++i) | 132 for (size_t i = 0, c = a_docstate.size(); i < c; ++i) |
124 EXPECT_EQ(string16(a_docstate[i]), string16(b_docstate[i])); | 133 EXPECT_EQ(string16(a_docstate[i]), string16(b_docstate[i])); |
125 } | 134 } |
126 | 135 |
127 void HistoryItemExpectFormDataEqual(const WebHistoryItem& a, | 136 void HistoryItemExpectFormDataEqual(const WebHistoryItem& a, |
128 const WebHistoryItem& b) { | 137 const WebHistoryItem& b) { |
(...skipping 17 matching lines...) Expand all Loading... |
146 } | 155 } |
147 EXPECT_EQ(string16(a.httpContentType()), string16(b.httpContentType())); | 156 EXPECT_EQ(string16(a.httpContentType()), string16(b.httpContentType())); |
148 } | 157 } |
149 | 158 |
150 void HistoryItemExpectChildrenEqual(const WebHistoryItem& a, | 159 void HistoryItemExpectChildrenEqual(const WebHistoryItem& a, |
151 const WebHistoryItem& b) { | 160 const WebHistoryItem& b) { |
152 const WebVector<WebHistoryItem>& a_children = a.children(); | 161 const WebVector<WebHistoryItem>& a_children = a.children(); |
153 const WebVector<WebHistoryItem>& b_children = b.children(); | 162 const WebVector<WebHistoryItem>& b_children = b.children(); |
154 EXPECT_EQ(a_children.size(), b_children.size()); | 163 EXPECT_EQ(a_children.size(), b_children.size()); |
155 for (size_t i = 0, c = a_children.size(); i < c; ++i) | 164 for (size_t i = 0, c = a_children.size(); i < c; ++i) |
156 HistoryItemExpectEqual(a_children[i], b_children[i]); | 165 HistoryItemExpectEqual(a_children[i], b_children[i], |
| 166 webkit_glue::HistoryItemCurrentVersion()); |
157 } | 167 } |
158 }; | 168 }; |
159 | 169 |
160 // Test old versions of serialized data to ensure that newer versions of code | 170 // Test old versions of serialized data to ensure that newer versions of code |
161 // can still read history items written by previous versions. | 171 // can still read history items written by previous versions. |
162 TEST_F(GlueSerializeTest, BackwardsCompatibleTest) { | 172 TEST_F(GlueSerializeTest, BackwardsCompatibleTest) { |
163 const WebHistoryItem& item = MakeHistoryItem(false, false); | 173 const WebHistoryItem& item = MakeHistoryItem(false, false); |
164 | 174 |
165 // Make sure version 12 (current version) can read versions 1 through 11. | 175 // Make sure current version can read all previous versions. |
166 for (int i = 1; i <= 11; i++) { | 176 for (int i = 1; i < webkit_glue::HistoryItemCurrentVersion(); i++) { |
167 std::string serialized_item; | 177 std::string serialized_item; |
168 webkit_glue::HistoryItemToVersionedString(item, i, &serialized_item); | 178 webkit_glue::HistoryItemToVersionedString(item, i, &serialized_item); |
169 const WebHistoryItem& deserialized_item = | 179 const WebHistoryItem& deserialized_item = |
170 webkit_glue::HistoryItemFromString(serialized_item); | 180 webkit_glue::HistoryItemFromString(serialized_item); |
171 ASSERT_FALSE(item.isNull()); | 181 ASSERT_FALSE(item.isNull()); |
172 ASSERT_FALSE(deserialized_item.isNull()); | 182 ASSERT_FALSE(deserialized_item.isNull()); |
173 HistoryItemExpectEqual(item, deserialized_item); | 183 HistoryItemExpectEqual(item, deserialized_item, i); |
174 } | 184 } |
175 } | 185 } |
176 | 186 |
177 // Makes sure that a HistoryItem remains intact after being serialized and | 187 // Makes sure that a HistoryItem remains intact after being serialized and |
178 // deserialized. | 188 // deserialized. |
179 TEST_F(GlueSerializeTest, HistoryItemSerializeTest) { | 189 TEST_F(GlueSerializeTest, HistoryItemSerializeTest) { |
180 const WebHistoryItem& item = MakeHistoryItem(true, true); | 190 const WebHistoryItem& item = MakeHistoryItem(true, true); |
181 const std::string& serialized_item = webkit_glue::HistoryItemToString(item); | 191 const std::string& serialized_item = webkit_glue::HistoryItemToString(item); |
182 const WebHistoryItem& deserialized_item = | 192 const WebHistoryItem& deserialized_item = |
183 webkit_glue::HistoryItemFromString(serialized_item); | 193 webkit_glue::HistoryItemFromString(serialized_item); |
184 | 194 |
185 ASSERT_FALSE(item.isNull()); | 195 ASSERT_FALSE(item.isNull()); |
186 ASSERT_FALSE(deserialized_item.isNull()); | 196 ASSERT_FALSE(deserialized_item.isNull()); |
187 HistoryItemExpectEqual(item, deserialized_item); | 197 HistoryItemExpectEqual(item, deserialized_item, |
| 198 webkit_glue::HistoryItemCurrentVersion()); |
188 } | 199 } |
189 | 200 |
190 // Checks that broken messages don't take out our process. | 201 // Checks that broken messages don't take out our process. |
191 TEST_F(GlueSerializeTest, BadMessagesTest) { | 202 TEST_F(GlueSerializeTest, BadMessagesTest) { |
192 { | 203 { |
193 Pickle p; | 204 Pickle p; |
194 // Version 1 | 205 // Version 1 |
195 p.WriteInt(1); | 206 p.WriteInt(1); |
196 // Empty strings. | 207 // Empty strings. |
197 for (int i = 0; i < 6; ++i) | 208 for (int i = 0; i < 6; ++i) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 const WebHistoryItem& item1 = MakeHistoryItem(true, true); | 240 const WebHistoryItem& item1 = MakeHistoryItem(true, true); |
230 std::string serialized_item = webkit_glue::HistoryItemToString(item1); | 241 std::string serialized_item = webkit_glue::HistoryItemToString(item1); |
231 serialized_item = | 242 serialized_item = |
232 webkit_glue::RemoveFormDataFromHistoryState(serialized_item); | 243 webkit_glue::RemoveFormDataFromHistoryState(serialized_item); |
233 const WebHistoryItem& item2 = | 244 const WebHistoryItem& item2 = |
234 webkit_glue::HistoryItemFromString(serialized_item); | 245 webkit_glue::HistoryItemFromString(serialized_item); |
235 | 246 |
236 ASSERT_FALSE(item1.isNull()); | 247 ASSERT_FALSE(item1.isNull()); |
237 ASSERT_FALSE(item2.isNull()); | 248 ASSERT_FALSE(item2.isNull()); |
238 | 249 |
239 HistoryItemExpectBaseDataEqual(item1, item2); | 250 HistoryItemExpectBaseDataEqual(item1, item2, |
| 251 webkit_glue::HistoryItemCurrentVersion()); |
240 HistoryItemExpectChildrenEqual(item1, item2); | 252 HistoryItemExpectChildrenEqual(item1, item2); |
241 | 253 |
242 // Form data was removed, but the identifier was kept. | 254 // Form data was removed, but the identifier was kept. |
243 const WebHTTPBody& body1 = item1.httpBody(); | 255 const WebHTTPBody& body1 = item1.httpBody(); |
244 const WebHTTPBody& body2 = item2.httpBody(); | 256 const WebHTTPBody& body2 = item2.httpBody(); |
245 EXPECT_FALSE(body1.isNull()); | 257 EXPECT_FALSE(body1.isNull()); |
246 EXPECT_FALSE(body2.isNull()); | 258 EXPECT_FALSE(body2.isNull()); |
247 EXPECT_GT(body1.elementCount(), 0U); | 259 EXPECT_GT(body1.elementCount(), 0U); |
248 EXPECT_EQ(0U, body2.elementCount()); | 260 EXPECT_EQ(0U, body2.elementCount()); |
249 EXPECT_EQ(body1.identifier(), body2.identifier()); | 261 EXPECT_EQ(body1.identifier(), body2.identifier()); |
(...skipping 22 matching lines...) Expand all Loading... |
272 // Makes sure that a HistoryItem containing password data remains intact after | 284 // Makes sure that a HistoryItem containing password data remains intact after |
273 // being serialized and deserialized. | 285 // being serialized and deserialized. |
274 TEST_F(GlueSerializeTest, HistoryItemWithPasswordsSerializeTest) { | 286 TEST_F(GlueSerializeTest, HistoryItemWithPasswordsSerializeTest) { |
275 const WebHistoryItem& item = MakeHistoryItemWithPasswordData(true); | 287 const WebHistoryItem& item = MakeHistoryItemWithPasswordData(true); |
276 const std::string& serialized_item = webkit_glue::HistoryItemToString(item); | 288 const std::string& serialized_item = webkit_glue::HistoryItemToString(item); |
277 const WebHistoryItem& deserialized_item = | 289 const WebHistoryItem& deserialized_item = |
278 webkit_glue::HistoryItemFromString(serialized_item); | 290 webkit_glue::HistoryItemFromString(serialized_item); |
279 | 291 |
280 ASSERT_FALSE(item.isNull()); | 292 ASSERT_FALSE(item.isNull()); |
281 ASSERT_FALSE(deserialized_item.isNull()); | 293 ASSERT_FALSE(deserialized_item.isNull()); |
282 HistoryItemExpectEqual(item, deserialized_item); | 294 HistoryItemExpectEqual(item, deserialized_item, |
| 295 webkit_glue::HistoryItemCurrentVersion()); |
283 } | 296 } |
284 | 297 |
285 TEST_F(GlueSerializeTest, RemovePasswordData) { | 298 TEST_F(GlueSerializeTest, RemovePasswordData) { |
286 const WebHistoryItem& item1 = MakeHistoryItemWithPasswordData(true); | 299 const WebHistoryItem& item1 = MakeHistoryItemWithPasswordData(true); |
287 std::string serialized_item = webkit_glue::HistoryItemToString(item1); | 300 std::string serialized_item = webkit_glue::HistoryItemToString(item1); |
288 serialized_item = | 301 serialized_item = |
289 webkit_glue::RemovePasswordDataFromHistoryState(serialized_item); | 302 webkit_glue::RemovePasswordDataFromHistoryState(serialized_item); |
290 const WebHistoryItem& item2 = | 303 const WebHistoryItem& item2 = |
291 webkit_glue::HistoryItemFromString(serialized_item); | 304 webkit_glue::HistoryItemFromString(serialized_item); |
292 | 305 |
293 ASSERT_FALSE(item1.isNull()); | 306 ASSERT_FALSE(item1.isNull()); |
294 ASSERT_FALSE(item2.isNull()); | 307 ASSERT_FALSE(item2.isNull()); |
295 | 308 |
296 HistoryItemExpectBaseDataEqual(item1, item2); | 309 HistoryItemExpectBaseDataEqual(item1, item2, |
| 310 webkit_glue::HistoryItemCurrentVersion()); |
297 HistoryItemExpectChildrenEqual(item1, item2); | 311 HistoryItemExpectChildrenEqual(item1, item2); |
298 | 312 |
299 // Form data was removed, but the identifier was kept. | 313 // Form data was removed, but the identifier was kept. |
300 const WebHTTPBody& body1 = item1.httpBody(); | 314 const WebHTTPBody& body1 = item1.httpBody(); |
301 const WebHTTPBody& body2 = item2.httpBody(); | 315 const WebHTTPBody& body2 = item2.httpBody(); |
302 EXPECT_FALSE(body1.isNull()); | 316 EXPECT_FALSE(body1.isNull()); |
303 EXPECT_FALSE(body2.isNull()); | 317 EXPECT_FALSE(body2.isNull()); |
304 EXPECT_GT(body1.elementCount(), 0U); | 318 EXPECT_GT(body1.elementCount(), 0U); |
305 EXPECT_EQ(0U, body2.elementCount()); | 319 EXPECT_EQ(0U, body2.elementCount()); |
306 EXPECT_EQ(body1.identifier(), body2.identifier()); | 320 EXPECT_EQ(body1.identifier(), body2.identifier()); |
307 } | 321 } |
308 | 322 |
309 TEST_F(GlueSerializeTest, RemovePasswordDataWithNoPasswordData) { | 323 TEST_F(GlueSerializeTest, RemovePasswordDataWithNoPasswordData) { |
310 const WebHistoryItem& item1 = MakeHistoryItem(true, true); | 324 const WebHistoryItem& item1 = MakeHistoryItem(true, true); |
311 std::string serialized_item = webkit_glue::HistoryItemToString(item1); | 325 std::string serialized_item = webkit_glue::HistoryItemToString(item1); |
312 serialized_item = | 326 serialized_item = |
313 webkit_glue::RemovePasswordDataFromHistoryState(serialized_item); | 327 webkit_glue::RemovePasswordDataFromHistoryState(serialized_item); |
314 const WebHistoryItem& item2 = | 328 const WebHistoryItem& item2 = |
315 webkit_glue::HistoryItemFromString(serialized_item); | 329 webkit_glue::HistoryItemFromString(serialized_item); |
316 | 330 |
317 ASSERT_FALSE(item1.isNull()); | 331 ASSERT_FALSE(item1.isNull()); |
318 ASSERT_FALSE(item2.isNull()); | 332 ASSERT_FALSE(item2.isNull()); |
319 | 333 |
320 // Form data was not removed. | 334 // Form data was not removed. |
321 HistoryItemExpectEqual(item1, item2); | 335 HistoryItemExpectEqual(item1, item2, |
| 336 webkit_glue::HistoryItemCurrentVersion()); |
322 } | 337 } |
323 | 338 |
324 } // namespace | 339 } // namespace |
OLD | NEW |