OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "android_webview/native/state_serializer.h" | 5 #include "android_webview/native/state_serializer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/pickle.h" | 10 #include "base/pickle.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "content/public/browser/content_browser_client.h" | 13 #include "content/public/browser/content_browser_client.h" |
14 #include "content/public/browser/navigation_entry.h" | 14 #include "content/public/browser/navigation_entry.h" |
15 #include "content/public/common/content_client.h" | 15 #include "content/public/common/content_client.h" |
16 #include "content/public/common/page_state.h" | 16 #include "content/public/common/page_state.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 using std::string; | 20 using std::string; |
21 | 21 |
22 namespace android_webview { | 22 namespace android_webview { |
23 | 23 |
24 TEST(AndroidWebViewStateSerializerTest, TestHeaderSerialization) { | 24 namespace { |
25 base::Pickle pickle; | |
26 bool result = internal::WriteHeaderToPickle(&pickle); | |
27 EXPECT_TRUE(result); | |
28 | 25 |
29 base::PickleIterator iterator(pickle); | 26 scoped_ptr<content::NavigationEntry> CreateNavigationEntry() { |
30 result = internal::RestoreHeaderFromPickle(&iterator); | |
31 EXPECT_TRUE(result); | |
32 } | |
33 | |
34 TEST(AndroidWebViewStateSerializerTest, TestNavigationEntrySerialization) { | |
35 // This is required for NavigationEntry::Create. | |
36 content::ContentClient content_client; | |
37 content::SetContentClient(&content_client); | |
38 content::ContentBrowserClient browser_client; | |
39 content::SetBrowserClientForTesting(&browser_client); | |
40 | |
41 scoped_ptr<content::NavigationEntry> entry( | 27 scoped_ptr<content::NavigationEntry> entry( |
42 content::NavigationEntry::Create()); | 28 content::NavigationEntry::Create()); |
43 | 29 |
44 const GURL url("http://url"); | 30 const GURL url("http://url"); |
45 const GURL virtual_url("http://virtual_url"); | 31 const GURL virtual_url("http://virtual_url"); |
46 content::Referrer referrer; | 32 content::Referrer referrer; |
47 referrer.url = GURL("http://referrer_url"); | 33 referrer.url = GURL("http://referrer_url"); |
48 referrer.policy = blink::WebReferrerPolicyOrigin; | 34 referrer.policy = blink::WebReferrerPolicyOrigin; |
49 const base::string16 title(base::UTF8ToUTF16("title")); | 35 const base::string16 title(base::UTF8ToUTF16("title")); |
50 const content::PageState page_state = | 36 const content::PageState page_state = |
(...skipping 15 matching lines...) Expand all Loading... |
66 entry->SetOriginalRequestURL(original_request_url); | 52 entry->SetOriginalRequestURL(original_request_url); |
67 entry->SetBaseURLForDataURL(base_url_for_data_url); | 53 entry->SetBaseURLForDataURL(base_url_for_data_url); |
68 { | 54 { |
69 scoped_refptr<base::RefCountedString> s = new base::RefCountedString(); | 55 scoped_refptr<base::RefCountedString> s = new base::RefCountedString(); |
70 s->data().assign(data_url_as_string); | 56 s->data().assign(data_url_as_string); |
71 entry->SetDataURLAsString(s); | 57 entry->SetDataURLAsString(s); |
72 } | 58 } |
73 entry->SetIsOverridingUserAgent(is_overriding_user_agent); | 59 entry->SetIsOverridingUserAgent(is_overriding_user_agent); |
74 entry->SetTimestamp(timestamp); | 60 entry->SetTimestamp(timestamp); |
75 entry->SetHttpStatusCode(http_status_code); | 61 entry->SetHttpStatusCode(http_status_code); |
| 62 return entry; |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
| 67 TEST(AndroidWebViewStateSerializerTest, TestHeaderSerialization) { |
| 68 base::Pickle pickle; |
| 69 bool result = internal::WriteHeaderToPickle(&pickle); |
| 70 EXPECT_TRUE(result); |
| 71 |
| 72 base::PickleIterator iterator(pickle); |
| 73 uint32_t version = internal::RestoreHeaderFromPickle(&iterator); |
| 74 EXPECT_GT(version, 0U); |
| 75 } |
| 76 |
| 77 TEST(AndroidWebViewStateSerializerTest, TestLegacyVersionHeaderSerialization) { |
| 78 base::Pickle pickle; |
| 79 bool result = internal::WriteHeaderToPickle( |
| 80 internal::AW_STATE_VERSION_INITIAL, &pickle); |
| 81 EXPECT_TRUE(result); |
| 82 |
| 83 base::PickleIterator iterator(pickle); |
| 84 uint32_t version = internal::RestoreHeaderFromPickle(&iterator); |
| 85 EXPECT_EQ(version, internal::AW_STATE_VERSION_INITIAL); |
| 86 } |
| 87 |
| 88 TEST(AndroidWebViewStateSerializerTest, |
| 89 TestUnsupportedVersionHeaderSerialization) { |
| 90 base::Pickle pickle; |
| 91 bool result = internal::WriteHeaderToPickle(20000101, &pickle); |
| 92 EXPECT_TRUE(result); |
| 93 |
| 94 base::PickleIterator iterator(pickle); |
| 95 uint32_t version = internal::RestoreHeaderFromPickle(&iterator); |
| 96 EXPECT_EQ(version, 0U); |
| 97 } |
| 98 |
| 99 TEST(AndroidWebViewStateSerializerTest, TestNavigationEntrySerialization) { |
| 100 // This is required for NavigationEntry::Create. |
| 101 content::ContentClient content_client; |
| 102 content::SetContentClient(&content_client); |
| 103 content::ContentBrowserClient browser_client; |
| 104 content::SetBrowserClientForTesting(&browser_client); |
| 105 |
| 106 scoped_ptr<content::NavigationEntry> entry(CreateNavigationEntry()); |
76 | 107 |
77 base::Pickle pickle; | 108 base::Pickle pickle; |
78 bool result = internal::WriteNavigationEntryToPickle(*entry, &pickle); | 109 bool result = internal::WriteNavigationEntryToPickle(*entry, &pickle); |
79 EXPECT_TRUE(result); | 110 EXPECT_TRUE(result); |
80 | 111 |
81 scoped_ptr<content::NavigationEntry> copy(content::NavigationEntry::Create()); | 112 scoped_ptr<content::NavigationEntry> copy(content::NavigationEntry::Create()); |
82 base::PickleIterator iterator(pickle); | 113 base::PickleIterator iterator(pickle); |
83 result = internal::RestoreNavigationEntryFromPickle(&iterator, copy.get()); | 114 result = internal::RestoreNavigationEntryFromPickle(&iterator, copy.get()); |
84 EXPECT_TRUE(result); | 115 EXPECT_TRUE(result); |
85 | 116 |
86 EXPECT_EQ(url, copy->GetURL()); | 117 EXPECT_EQ(entry->GetURL(), copy->GetURL()); |
87 EXPECT_EQ(virtual_url, copy->GetVirtualURL()); | 118 EXPECT_EQ(entry->GetVirtualURL(), copy->GetVirtualURL()); |
88 EXPECT_EQ(referrer.url, copy->GetReferrer().url); | 119 EXPECT_EQ(entry->GetReferrer().url, copy->GetReferrer().url); |
89 EXPECT_EQ(referrer.policy, copy->GetReferrer().policy); | 120 EXPECT_EQ(entry->GetReferrer().policy, copy->GetReferrer().policy); |
90 EXPECT_EQ(title, copy->GetTitle()); | 121 EXPECT_EQ(entry->GetTitle(), copy->GetTitle()); |
91 EXPECT_EQ(page_state, copy->GetPageState()); | 122 EXPECT_EQ(entry->GetPageState(), copy->GetPageState()); |
92 EXPECT_EQ(has_post_data, copy->GetHasPostData()); | 123 EXPECT_EQ(entry->GetHasPostData(), copy->GetHasPostData()); |
93 EXPECT_EQ(original_request_url, copy->GetOriginalRequestURL()); | 124 EXPECT_EQ(entry->GetOriginalRequestURL(), copy->GetOriginalRequestURL()); |
94 EXPECT_EQ(base_url_for_data_url, copy->GetBaseURLForDataURL()); | 125 EXPECT_EQ(entry->GetBaseURLForDataURL(), copy->GetBaseURLForDataURL()); |
95 EXPECT_EQ(data_url_as_string, copy->GetDataURLAsString()->data()); | 126 EXPECT_EQ(entry->GetDataURLAsString()->data(), |
96 EXPECT_EQ(is_overriding_user_agent, copy->GetIsOverridingUserAgent()); | 127 copy->GetDataURLAsString()->data()); |
97 EXPECT_EQ(timestamp, copy->GetTimestamp()); | 128 EXPECT_EQ(entry->GetIsOverridingUserAgent(), |
98 EXPECT_EQ(http_status_code, copy->GetHttpStatusCode()); | 129 copy->GetIsOverridingUserAgent()); |
| 130 EXPECT_EQ(entry->GetTimestamp(), copy->GetTimestamp()); |
| 131 EXPECT_EQ(entry->GetHttpStatusCode(), copy->GetHttpStatusCode()); |
| 132 } |
| 133 |
| 134 TEST(AndroidWebViewStateSerializerTest, |
| 135 TestLegacyNavigationEntrySerialization) { |
| 136 // This is required for NavigationEntry::Create. |
| 137 content::ContentClient content_client; |
| 138 content::SetContentClient(&content_client); |
| 139 content::ContentBrowserClient browser_client; |
| 140 content::SetBrowserClientForTesting(&browser_client); |
| 141 |
| 142 scoped_ptr<content::NavigationEntry> entry(CreateNavigationEntry()); |
| 143 |
| 144 base::Pickle pickle; |
| 145 bool result = internal::WriteNavigationEntryToPickle( |
| 146 internal::AW_STATE_VERSION_INITIAL, *entry, &pickle); |
| 147 EXPECT_TRUE(result); |
| 148 |
| 149 scoped_ptr<content::NavigationEntry> copy(content::NavigationEntry::Create()); |
| 150 base::PickleIterator iterator(pickle); |
| 151 result = internal::RestoreNavigationEntryFromPickle( |
| 152 internal::AW_STATE_VERSION_INITIAL, &iterator, copy.get()); |
| 153 EXPECT_TRUE(result); |
| 154 |
| 155 EXPECT_EQ(entry->GetURL(), copy->GetURL()); |
| 156 EXPECT_EQ(entry->GetVirtualURL(), copy->GetVirtualURL()); |
| 157 EXPECT_EQ(entry->GetReferrer().url, copy->GetReferrer().url); |
| 158 EXPECT_EQ(entry->GetReferrer().policy, copy->GetReferrer().policy); |
| 159 EXPECT_EQ(entry->GetTitle(), copy->GetTitle()); |
| 160 EXPECT_EQ(entry->GetPageState(), copy->GetPageState()); |
| 161 EXPECT_EQ(entry->GetHasPostData(), copy->GetHasPostData()); |
| 162 EXPECT_EQ(entry->GetOriginalRequestURL(), copy->GetOriginalRequestURL()); |
| 163 EXPECT_EQ(entry->GetBaseURLForDataURL(), copy->GetBaseURLForDataURL()); |
| 164 // DataURL not supported by 20130814 format |
| 165 EXPECT_FALSE(copy->GetDataURLAsString()); |
| 166 EXPECT_EQ(entry->GetIsOverridingUserAgent(), |
| 167 copy->GetIsOverridingUserAgent()); |
| 168 EXPECT_EQ(entry->GetTimestamp(), copy->GetTimestamp()); |
| 169 EXPECT_EQ(entry->GetHttpStatusCode(), copy->GetHttpStatusCode()); |
99 } | 170 } |
100 | 171 |
101 TEST(AndroidWebViewStateSerializerTest, TestEmptyDataURLSerialization) { | 172 TEST(AndroidWebViewStateSerializerTest, TestEmptyDataURLSerialization) { |
102 // This is required for NavigationEntry::Create. | 173 // This is required for NavigationEntry::Create. |
103 content::ContentClient content_client; | 174 content::ContentClient content_client; |
104 content::SetContentClient(&content_client); | 175 content::SetContentClient(&content_client); |
105 content::ContentBrowserClient browser_client; | 176 content::ContentBrowserClient browser_client; |
106 content::SetBrowserClientForTesting(&browser_client); | 177 content::SetBrowserClientForTesting(&browser_client); |
107 | 178 |
108 scoped_ptr<content::NavigationEntry> entry( | 179 scoped_ptr<content::NavigationEntry> entry( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 EXPECT_TRUE(result); | 213 EXPECT_TRUE(result); |
143 | 214 |
144 scoped_ptr<content::NavigationEntry> copy(content::NavigationEntry::Create()); | 215 scoped_ptr<content::NavigationEntry> copy(content::NavigationEntry::Create()); |
145 base::PickleIterator iterator(pickle); | 216 base::PickleIterator iterator(pickle); |
146 result = internal::RestoreNavigationEntryFromPickle(&iterator, copy.get()); | 217 result = internal::RestoreNavigationEntryFromPickle(&iterator, copy.get()); |
147 EXPECT_TRUE(result); | 218 EXPECT_TRUE(result); |
148 EXPECT_EQ(huge_data_url, copy->GetDataURLAsString()->data()); | 219 EXPECT_EQ(huge_data_url, copy->GetDataURLAsString()->data()); |
149 } | 220 } |
150 | 221 |
151 } // namespace android_webview | 222 } // namespace android_webview |
OLD | NEW |