1 // Copyright 2013 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 "base/strings/string16.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/time/time.h"
9 #include "content/browser/frame_host/navigation_entry_impl.h"
10 #include "content/browser/site_instance_impl.h"
11 #include "content/public/common/ssl_status.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using base::ASCIIToUTF16
;
18 class NavigationEntryTest
: public testing::Test
{
20 NavigationEntryTest() : instance_(NULL
) {
23 void SetUp() override
{
24 entry1_
.reset(new NavigationEntryImpl
);
27 instance_
= static_cast<SiteInstanceImpl
*>(SiteInstance::Create(NULL
));
29 entry2_
.reset(new NavigationEntryImpl(
32 Referrer(GURL("from"), blink::WebReferrerPolicyDefault
),
33 ASCIIToUTF16("title"),
34 ui::PAGE_TRANSITION_TYPED
,
38 void TearDown() override
{}
41 scoped_ptr
<NavigationEntryImpl
> entry1_
;
42 scoped_ptr
<NavigationEntryImpl
> entry2_
;
43 // SiteInstances are deleted when their NavigationEntries are gone.
44 SiteInstanceImpl
* instance_
;
47 // Test unique ID accessors
48 TEST_F(NavigationEntryTest
, NavigationEntryUniqueIDs
) {
49 // Two entries should have different IDs by default
50 EXPECT_NE(entry1_
->GetUniqueID(), entry2_
->GetUniqueID());
52 // Can set an entry to have the same ID as another
53 entry2_
->set_unique_id(entry1_
->GetUniqueID());
54 EXPECT_EQ(entry1_
->GetUniqueID(), entry2_
->GetUniqueID());
58 TEST_F(NavigationEntryTest
, NavigationEntryURLs
) {
59 // Start with no virtual_url (even if a url is set)
60 EXPECT_FALSE(entry1_
->has_virtual_url());
61 EXPECT_FALSE(entry2_
->has_virtual_url());
63 EXPECT_EQ(GURL(), entry1_
->GetURL());
64 EXPECT_EQ(GURL(), entry1_
->GetVirtualURL());
65 EXPECT_TRUE(entry1_
->GetTitleForDisplay(std::string()).empty());
67 // Setting URL affects virtual_url and GetTitleForDisplay
68 entry1_
->SetURL(GURL("http://www.google.com"));
69 EXPECT_EQ(GURL("http://www.google.com"), entry1_
->GetURL());
70 EXPECT_EQ(GURL("http://www.google.com"), entry1_
->GetVirtualURL());
71 EXPECT_EQ(ASCIIToUTF16("www.google.com"),
72 entry1_
->GetTitleForDisplay(std::string()));
74 // file:/// URLs should only show the filename.
75 entry1_
->SetURL(GURL("file:///foo/bar baz.txt"));
76 EXPECT_EQ(ASCIIToUTF16("bar baz.txt"),
77 entry1_
->GetTitleForDisplay(std::string()));
79 // For file:/// URLs, make sure that slashes after the filename are ignored.
80 // Regression test for https://crbug.com/503003.
81 entry1_
->SetURL(GURL("file:///foo/bar baz.txt#foo/bar"));
82 EXPECT_EQ(ASCIIToUTF16("bar baz.txt#foo/bar"),
83 entry1_
->GetTitleForDisplay(std::string()));
84 entry1_
->SetURL(GURL("file:///foo/bar baz.txt?x=foo/bar"));
85 EXPECT_EQ(ASCIIToUTF16("bar baz.txt?x=foo/bar"),
86 entry1_
->GetTitleForDisplay(std::string()));
87 entry1_
->SetURL(GURL("file:///foo/bar baz.txt#baz/boo?x=foo/bar"));
88 EXPECT_EQ(ASCIIToUTF16("bar baz.txt#baz/boo?x=foo/bar"),
89 entry1_
->GetTitleForDisplay(std::string()));
90 entry1_
->SetURL(GURL("file:///foo/bar baz.txt?x=foo/bar#baz/boo"));
91 EXPECT_EQ(ASCIIToUTF16("bar baz.txt?x=foo/bar#baz/boo"),
92 entry1_
->GetTitleForDisplay(std::string()));
93 entry1_
->SetURL(GURL("file:///foo/bar baz.txt#foo/bar#baz/boo"));
94 EXPECT_EQ(ASCIIToUTF16("bar baz.txt#foo/bar#baz/boo"),
95 entry1_
->GetTitleForDisplay(std::string()));
96 entry1_
->SetURL(GURL("file:///foo/bar baz.txt?x=foo/bar?y=baz/boo"));
97 EXPECT_EQ(ASCIIToUTF16("bar baz.txt?x=foo/bar?y=baz/boo"),
98 entry1_
->GetTitleForDisplay(std::string()));
100 // Title affects GetTitleForDisplay
101 entry1_
->SetTitle(ASCIIToUTF16("Google"));
102 EXPECT_EQ(ASCIIToUTF16("Google"), entry1_
->GetTitleForDisplay(std::string()));
104 // Setting virtual_url doesn't affect URL
105 entry2_
->SetVirtualURL(GURL("display:url"));
106 EXPECT_TRUE(entry2_
->has_virtual_url());
107 EXPECT_EQ(GURL("test:url"), entry2_
->GetURL());
108 EXPECT_EQ(GURL("display:url"), entry2_
->GetVirtualURL());
110 // Having a title set in constructor overrides virtual URL
111 EXPECT_EQ(ASCIIToUTF16("title"), entry2_
->GetTitleForDisplay(std::string()));
113 // User typed URL is independent of the others
114 EXPECT_EQ(GURL(), entry1_
->GetUserTypedURL());
115 EXPECT_EQ(GURL(), entry2_
->GetUserTypedURL());
116 entry2_
->set_user_typed_url(GURL("typedurl"));
117 EXPECT_EQ(GURL("typedurl"), entry2_
->GetUserTypedURL());
120 // Test Favicon inner class construction.
121 TEST_F(NavigationEntryTest
, NavigationEntryFavicons
) {
122 EXPECT_EQ(GURL(), entry1_
->GetFavicon().url
);
123 EXPECT_FALSE(entry1_
->GetFavicon().valid
);
126 // Test SSLStatus inner class
127 TEST_F(NavigationEntryTest
, NavigationEntrySSLStatus
) {
129 EXPECT_EQ(SECURITY_STYLE_UNKNOWN
, entry1_
->GetSSL().security_style
);
130 EXPECT_EQ(SECURITY_STYLE_UNKNOWN
, entry2_
->GetSSL().security_style
);
131 EXPECT_EQ(0, entry1_
->GetSSL().cert_id
);
132 EXPECT_EQ(0U, entry1_
->GetSSL().cert_status
);
133 EXPECT_EQ(-1, entry1_
->GetSSL().security_bits
);
134 int content_status
= entry1_
->GetSSL().content_status
;
135 EXPECT_FALSE(!!(content_status
& SSLStatus::DISPLAYED_INSECURE_CONTENT
));
136 EXPECT_FALSE(!!(content_status
& SSLStatus::RAN_INSECURE_CONTENT
));
139 // Test other basic accessors
140 TEST_F(NavigationEntryTest
, NavigationEntryAccessors
) {
142 EXPECT_TRUE(entry1_
->site_instance() == NULL
);
143 EXPECT_EQ(instance_
, entry2_
->site_instance());
144 entry1_
->set_site_instance(instance_
);
145 EXPECT_EQ(instance_
, entry1_
->site_instance());
148 EXPECT_EQ(PAGE_TYPE_NORMAL
, entry1_
->GetPageType());
149 EXPECT_EQ(PAGE_TYPE_NORMAL
, entry2_
->GetPageType());
150 entry2_
->set_page_type(PAGE_TYPE_INTERSTITIAL
);
151 EXPECT_EQ(PAGE_TYPE_INTERSTITIAL
, entry2_
->GetPageType());
154 EXPECT_EQ(GURL(), entry1_
->GetReferrer().url
);
155 EXPECT_EQ(GURL("from"), entry2_
->GetReferrer().url
);
156 entry2_
->SetReferrer(
157 Referrer(GURL("from2"), blink::WebReferrerPolicyDefault
));
158 EXPECT_EQ(GURL("from2"), entry2_
->GetReferrer().url
);
161 EXPECT_EQ(base::string16(), entry1_
->GetTitle());
162 EXPECT_EQ(ASCIIToUTF16("title"), entry2_
->GetTitle());
163 entry2_
->SetTitle(ASCIIToUTF16("title2"));
164 EXPECT_EQ(ASCIIToUTF16("title2"), entry2_
->GetTitle());
167 EXPECT_FALSE(entry1_
->GetPageState().IsValid());
168 EXPECT_FALSE(entry2_
->GetPageState().IsValid());
169 entry2_
->SetPageState(PageState::CreateFromEncodedData("state"));
170 EXPECT_EQ("state", entry2_
->GetPageState().ToEncodedData());
173 EXPECT_EQ(-1, entry1_
->GetPageID());
174 EXPECT_EQ(3, entry2_
->GetPageID());
175 entry2_
->SetPageID(2);
176 EXPECT_EQ(2, entry2_
->GetPageID());
179 EXPECT_EQ(ui::PAGE_TRANSITION_LINK
, entry1_
->GetTransitionType());
180 EXPECT_EQ(ui::PAGE_TRANSITION_TYPED
, entry2_
->GetTransitionType());
181 entry2_
->SetTransitionType(ui::PAGE_TRANSITION_RELOAD
);
182 EXPECT_EQ(ui::PAGE_TRANSITION_RELOAD
, entry2_
->GetTransitionType());
184 // Is renderer initiated
185 EXPECT_FALSE(entry1_
->is_renderer_initiated());
186 EXPECT_FALSE(entry2_
->is_renderer_initiated());
187 entry2_
->set_is_renderer_initiated(true);
188 EXPECT_TRUE(entry2_
->is_renderer_initiated());
191 EXPECT_FALSE(entry1_
->GetHasPostData());
192 EXPECT_FALSE(entry2_
->GetHasPostData());
193 entry2_
->SetHasPostData(true);
194 EXPECT_TRUE(entry2_
->GetHasPostData());
197 EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE
, entry1_
->restore_type());
198 EXPECT_FALSE(entry1_
->IsRestored());
199 EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE
, entry2_
->restore_type());
200 EXPECT_FALSE(entry2_
->IsRestored());
201 entry2_
->set_restore_type(
202 NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY
);
203 EXPECT_EQ(NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY
,
204 entry2_
->restore_type());
205 EXPECT_TRUE(entry2_
->IsRestored());
208 EXPECT_EQ(GURL(), entry1_
->GetOriginalRequestURL());
209 EXPECT_EQ(GURL(), entry2_
->GetOriginalRequestURL());
210 entry2_
->SetOriginalRequestURL(GURL("original_url"));
211 EXPECT_EQ(GURL("original_url"), entry2_
->GetOriginalRequestURL());
213 // User agent override
214 EXPECT_FALSE(entry1_
->GetIsOverridingUserAgent());
215 EXPECT_FALSE(entry2_
->GetIsOverridingUserAgent());
216 entry2_
->SetIsOverridingUserAgent(true);
217 EXPECT_TRUE(entry2_
->GetIsOverridingUserAgent());
219 // Browser initiated post data
220 EXPECT_EQ(NULL
, entry1_
->GetBrowserInitiatedPostData());
221 EXPECT_EQ(NULL
, entry2_
->GetBrowserInitiatedPostData());
222 const int length
= 11;
223 const unsigned char* raw_data
=
224 reinterpret_cast<const unsigned char*>("post\n\n\0data");
225 std::vector
<unsigned char> post_data_vector(raw_data
, raw_data
+length
);
226 scoped_refptr
<base::RefCountedBytes
> post_data
=
227 base::RefCountedBytes::TakeVector(&post_data_vector
);
228 entry2_
->SetBrowserInitiatedPostData(post_data
.get());
229 EXPECT_EQ(post_data
->front(),
230 entry2_
->GetBrowserInitiatedPostData()->front());
233 // Test basic Clone behavior.
234 TEST_F(NavigationEntryTest
, NavigationEntryClone
) {
235 // Set some additional values.
236 entry2_
->SetTransitionType(ui::PAGE_TRANSITION_RELOAD
);
237 entry2_
->set_should_replace_entry(true);
239 scoped_ptr
<NavigationEntryImpl
> clone(entry2_
->Clone());
241 // Value from FrameNavigationEntry.
242 EXPECT_EQ(entry2_
->site_instance(), clone
->site_instance());
244 // Value from constructor.
245 EXPECT_EQ(entry2_
->GetTitle(), clone
->GetTitle());
247 // Value set after constructor.
248 EXPECT_EQ(entry2_
->GetTransitionType(), clone
->GetTransitionType());
250 // Value not copied due to ResetForCommit.
251 EXPECT_NE(entry2_
->should_replace_entry(), clone
->should_replace_entry());
255 TEST_F(NavigationEntryTest
, NavigationEntryTimestamps
) {
256 EXPECT_EQ(base::Time(), entry1_
->GetTimestamp());
257 const base::Time now
= base::Time::Now();
258 entry1_
->SetTimestamp(now
);
259 EXPECT_EQ(now
, entry1_
->GetTimestamp());
262 // Test extra data stored in the navigation entry.
263 TEST_F(NavigationEntryTest
, NavigationEntryExtraData
) {
264 base::string16 test_data
= ASCIIToUTF16("my search terms");
265 base::string16 output
;
266 entry1_
->SetExtraData("search_terms", test_data
);
268 EXPECT_FALSE(entry1_
->GetExtraData("non_existent_key", &output
));
269 EXPECT_EQ(ASCIIToUTF16(""), output
);
270 EXPECT_TRUE(entry1_
->GetExtraData("search_terms", &output
));
271 EXPECT_EQ(test_data
, output
);
273 entry1_
->ClearExtraData("search_terms");
274 // Content in |output| is not modified if data is not present at the key.
275 EXPECT_FALSE(entry1_
->GetExtraData("search_terms", &output
));
276 EXPECT_EQ(test_data
, output
);
277 // Using an empty string shows that the data is not present in the map.
278 base::string16 output2
;
279 EXPECT_FALSE(entry1_
->GetExtraData("search_terms", &output2
));
280 EXPECT_EQ(ASCIIToUTF16(""), output2
);
283 } // namespace content