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 // Title affects GetTitleForDisplay
80 entry1_
->SetTitle(ASCIIToUTF16("Google"));
81 EXPECT_EQ(ASCIIToUTF16("Google"), entry1_
->GetTitleForDisplay(std::string()));
83 // Setting virtual_url doesn't affect URL
84 entry2_
->SetVirtualURL(GURL("display:url"));
85 EXPECT_TRUE(entry2_
->has_virtual_url());
86 EXPECT_EQ(GURL("test:url"), entry2_
->GetURL());
87 EXPECT_EQ(GURL("display:url"), entry2_
->GetVirtualURL());
89 // Having a title set in constructor overrides virtual URL
90 EXPECT_EQ(ASCIIToUTF16("title"), entry2_
->GetTitleForDisplay(std::string()));
92 // User typed URL is independent of the others
93 EXPECT_EQ(GURL(), entry1_
->GetUserTypedURL());
94 EXPECT_EQ(GURL(), entry2_
->GetUserTypedURL());
95 entry2_
->set_user_typed_url(GURL("typedurl"));
96 EXPECT_EQ(GURL("typedurl"), entry2_
->GetUserTypedURL());
99 // Test Favicon inner class construction.
100 TEST_F(NavigationEntryTest
, NavigationEntryFavicons
) {
101 EXPECT_EQ(GURL(), entry1_
->GetFavicon().url
);
102 EXPECT_FALSE(entry1_
->GetFavicon().valid
);
105 // Test SSLStatus inner class
106 TEST_F(NavigationEntryTest
, NavigationEntrySSLStatus
) {
108 EXPECT_EQ(SECURITY_STYLE_UNKNOWN
, entry1_
->GetSSL().security_style
);
109 EXPECT_EQ(SECURITY_STYLE_UNKNOWN
, entry2_
->GetSSL().security_style
);
110 EXPECT_EQ(0, entry1_
->GetSSL().cert_id
);
111 EXPECT_EQ(0U, entry1_
->GetSSL().cert_status
);
112 EXPECT_EQ(-1, entry1_
->GetSSL().security_bits
);
113 int content_status
= entry1_
->GetSSL().content_status
;
114 EXPECT_FALSE(!!(content_status
& SSLStatus::DISPLAYED_INSECURE_CONTENT
));
115 EXPECT_FALSE(!!(content_status
& SSLStatus::RAN_INSECURE_CONTENT
));
118 // Test other basic accessors
119 TEST_F(NavigationEntryTest
, NavigationEntryAccessors
) {
121 EXPECT_TRUE(entry1_
->site_instance() == NULL
);
122 EXPECT_EQ(instance_
, entry2_
->site_instance());
123 entry1_
->set_site_instance(instance_
);
124 EXPECT_EQ(instance_
, entry1_
->site_instance());
127 EXPECT_EQ(PAGE_TYPE_NORMAL
, entry1_
->GetPageType());
128 EXPECT_EQ(PAGE_TYPE_NORMAL
, entry2_
->GetPageType());
129 entry2_
->set_page_type(PAGE_TYPE_INTERSTITIAL
);
130 EXPECT_EQ(PAGE_TYPE_INTERSTITIAL
, entry2_
->GetPageType());
133 EXPECT_EQ(GURL(), entry1_
->GetReferrer().url
);
134 EXPECT_EQ(GURL("from"), entry2_
->GetReferrer().url
);
135 entry2_
->SetReferrer(
136 Referrer(GURL("from2"), blink::WebReferrerPolicyDefault
));
137 EXPECT_EQ(GURL("from2"), entry2_
->GetReferrer().url
);
140 EXPECT_EQ(base::string16(), entry1_
->GetTitle());
141 EXPECT_EQ(ASCIIToUTF16("title"), entry2_
->GetTitle());
142 entry2_
->SetTitle(ASCIIToUTF16("title2"));
143 EXPECT_EQ(ASCIIToUTF16("title2"), entry2_
->GetTitle());
146 EXPECT_FALSE(entry1_
->GetPageState().IsValid());
147 EXPECT_FALSE(entry2_
->GetPageState().IsValid());
148 entry2_
->SetPageState(PageState::CreateFromEncodedData("state"));
149 EXPECT_EQ("state", entry2_
->GetPageState().ToEncodedData());
152 EXPECT_EQ(-1, entry1_
->GetPageID());
153 EXPECT_EQ(3, entry2_
->GetPageID());
154 entry2_
->SetPageID(2);
155 EXPECT_EQ(2, entry2_
->GetPageID());
158 EXPECT_EQ(ui::PAGE_TRANSITION_LINK
, entry1_
->GetTransitionType());
159 EXPECT_EQ(ui::PAGE_TRANSITION_TYPED
, entry2_
->GetTransitionType());
160 entry2_
->SetTransitionType(ui::PAGE_TRANSITION_RELOAD
);
161 EXPECT_EQ(ui::PAGE_TRANSITION_RELOAD
, entry2_
->GetTransitionType());
163 // Is renderer initiated
164 EXPECT_FALSE(entry1_
->is_renderer_initiated());
165 EXPECT_FALSE(entry2_
->is_renderer_initiated());
166 entry2_
->set_is_renderer_initiated(true);
167 EXPECT_TRUE(entry2_
->is_renderer_initiated());
170 EXPECT_FALSE(entry1_
->GetHasPostData());
171 EXPECT_FALSE(entry2_
->GetHasPostData());
172 entry2_
->SetHasPostData(true);
173 EXPECT_TRUE(entry2_
->GetHasPostData());
176 EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE
, entry1_
->restore_type());
177 EXPECT_FALSE(entry1_
->IsRestored());
178 EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE
, entry2_
->restore_type());
179 EXPECT_FALSE(entry2_
->IsRestored());
180 entry2_
->set_restore_type(
181 NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY
);
182 EXPECT_EQ(NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY
,
183 entry2_
->restore_type());
184 EXPECT_TRUE(entry2_
->IsRestored());
187 EXPECT_EQ(GURL(), entry1_
->GetOriginalRequestURL());
188 EXPECT_EQ(GURL(), entry2_
->GetOriginalRequestURL());
189 entry2_
->SetOriginalRequestURL(GURL("original_url"));
190 EXPECT_EQ(GURL("original_url"), entry2_
->GetOriginalRequestURL());
192 // User agent override
193 EXPECT_FALSE(entry1_
->GetIsOverridingUserAgent());
194 EXPECT_FALSE(entry2_
->GetIsOverridingUserAgent());
195 entry2_
->SetIsOverridingUserAgent(true);
196 EXPECT_TRUE(entry2_
->GetIsOverridingUserAgent());
198 // Browser initiated post data
199 EXPECT_EQ(NULL
, entry1_
->GetBrowserInitiatedPostData());
200 EXPECT_EQ(NULL
, entry2_
->GetBrowserInitiatedPostData());
201 const int length
= 11;
202 const unsigned char* raw_data
=
203 reinterpret_cast<const unsigned char*>("post\n\n\0data");
204 std::vector
<unsigned char> post_data_vector(raw_data
, raw_data
+length
);
205 scoped_refptr
<base::RefCountedBytes
> post_data
=
206 base::RefCountedBytes::TakeVector(&post_data_vector
);
207 entry2_
->SetBrowserInitiatedPostData(post_data
.get());
208 EXPECT_EQ(post_data
->front(),
209 entry2_
->GetBrowserInitiatedPostData()->front());
211 // Frame to navigate.
212 EXPECT_TRUE(entry1_
->GetFrameToNavigate().empty());
213 EXPECT_TRUE(entry2_
->GetFrameToNavigate().empty());
217 TEST_F(NavigationEntryTest
, NavigationEntryTimestamps
) {
218 EXPECT_EQ(base::Time(), entry1_
->GetTimestamp());
219 const base::Time now
= base::Time::Now();
220 entry1_
->SetTimestamp(now
);
221 EXPECT_EQ(now
, entry1_
->GetTimestamp());
224 // Test extra data stored in the navigation entry.
225 TEST_F(NavigationEntryTest
, NavigationEntryExtraData
) {
226 base::string16 test_data
= ASCIIToUTF16("my search terms");
227 base::string16 output
;
228 entry1_
->SetExtraData("search_terms", test_data
);
230 EXPECT_FALSE(entry1_
->GetExtraData("non_existent_key", &output
));
231 EXPECT_EQ(ASCIIToUTF16(""), output
);
232 EXPECT_TRUE(entry1_
->GetExtraData("search_terms", &output
));
233 EXPECT_EQ(test_data
, output
);
235 entry1_
->ClearExtraData("search_terms");
236 // Content in |output| is not modified if data is not present at the key.
237 EXPECT_FALSE(entry1_
->GetExtraData("search_terms", &output
));
238 EXPECT_EQ(test_data
, output
);
239 // Using an empty string shows that the data is not present in the map.
240 base::string16 output2
;
241 EXPECT_FALSE(entry1_
->GetExtraData("search_terms", &output2
));
242 EXPECT_EQ(ASCIIToUTF16(""), output2
);
245 } // namespace content