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 virtual void SetUp() {
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 PAGE_TRANSITION_TYPED
,
38 virtual void TearDown() {
42 scoped_ptr
<NavigationEntryImpl
> entry1_
;
43 scoped_ptr
<NavigationEntryImpl
> entry2_
;
44 // SiteInstances are deleted when their NavigationEntries are gone.
45 SiteInstanceImpl
* instance_
;
48 // Test unique ID accessors
49 TEST_F(NavigationEntryTest
, NavigationEntryUniqueIDs
) {
50 // Two entries should have different IDs by default
51 EXPECT_NE(entry1_
->GetUniqueID(), entry2_
->GetUniqueID());
53 // Can set an entry to have the same ID as another
54 entry2_
->set_unique_id(entry1_
->GetUniqueID());
55 EXPECT_EQ(entry1_
->GetUniqueID(), entry2_
->GetUniqueID());
59 TEST_F(NavigationEntryTest
, NavigationEntryURLs
) {
60 // Start with no virtual_url (even if a url is set)
61 EXPECT_FALSE(entry1_
->has_virtual_url());
62 EXPECT_FALSE(entry2_
->has_virtual_url());
64 EXPECT_EQ(GURL(), entry1_
->GetURL());
65 EXPECT_EQ(GURL(), entry1_
->GetVirtualURL());
66 EXPECT_TRUE(entry1_
->GetTitleForDisplay(std::string()).empty());
68 // Setting URL affects virtual_url and GetTitleForDisplay
69 entry1_
->SetURL(GURL("http://www.google.com"));
70 EXPECT_EQ(GURL("http://www.google.com"), entry1_
->GetURL());
71 EXPECT_EQ(GURL("http://www.google.com"), entry1_
->GetVirtualURL());
72 EXPECT_EQ(ASCIIToUTF16("www.google.com"),
73 entry1_
->GetTitleForDisplay(std::string()));
75 // file:/// URLs should only show the filename.
76 entry1_
->SetURL(GURL("file:///foo/bar baz.txt"));
77 EXPECT_EQ(ASCIIToUTF16("bar baz.txt"),
78 entry1_
->GetTitleForDisplay(std::string()));
80 // Title affects GetTitleForDisplay
81 entry1_
->SetTitle(ASCIIToUTF16("Google"));
82 EXPECT_EQ(ASCIIToUTF16("Google"), entry1_
->GetTitleForDisplay(std::string()));
84 // Setting virtual_url doesn't affect URL
85 entry2_
->SetVirtualURL(GURL("display:url"));
86 EXPECT_TRUE(entry2_
->has_virtual_url());
87 EXPECT_EQ(GURL("test:url"), entry2_
->GetURL());
88 EXPECT_EQ(GURL("display:url"), entry2_
->GetVirtualURL());
90 // Having a title set in constructor overrides virtual URL
91 EXPECT_EQ(ASCIIToUTF16("title"), entry2_
->GetTitleForDisplay(std::string()));
93 // User typed URL is independent of the others
94 EXPECT_EQ(GURL(), entry1_
->GetUserTypedURL());
95 EXPECT_EQ(GURL(), entry2_
->GetUserTypedURL());
96 entry2_
->set_user_typed_url(GURL("typedurl"));
97 EXPECT_EQ(GURL("typedurl"), entry2_
->GetUserTypedURL());
100 // Test Favicon inner class construction.
101 TEST_F(NavigationEntryTest
, NavigationEntryFavicons
) {
102 EXPECT_EQ(GURL(), entry1_
->GetFavicon().url
);
103 EXPECT_FALSE(entry1_
->GetFavicon().valid
);
106 // Test SSLStatus inner class
107 TEST_F(NavigationEntryTest
, NavigationEntrySSLStatus
) {
109 EXPECT_EQ(SECURITY_STYLE_UNKNOWN
, entry1_
->GetSSL().security_style
);
110 EXPECT_EQ(SECURITY_STYLE_UNKNOWN
, entry2_
->GetSSL().security_style
);
111 EXPECT_EQ(0, entry1_
->GetSSL().cert_id
);
112 EXPECT_EQ(0U, entry1_
->GetSSL().cert_status
);
113 EXPECT_EQ(-1, entry1_
->GetSSL().security_bits
);
114 int content_status
= entry1_
->GetSSL().content_status
;
115 EXPECT_FALSE(!!(content_status
& SSLStatus::DISPLAYED_INSECURE_CONTENT
));
116 EXPECT_FALSE(!!(content_status
& SSLStatus::RAN_INSECURE_CONTENT
));
119 // Test other basic accessors
120 TEST_F(NavigationEntryTest
, NavigationEntryAccessors
) {
122 EXPECT_TRUE(entry1_
->site_instance() == NULL
);
123 EXPECT_EQ(instance_
, entry2_
->site_instance());
124 entry1_
->set_site_instance(instance_
);
125 EXPECT_EQ(instance_
, entry1_
->site_instance());
128 EXPECT_EQ(PAGE_TYPE_NORMAL
, entry1_
->GetPageType());
129 EXPECT_EQ(PAGE_TYPE_NORMAL
, entry2_
->GetPageType());
130 entry2_
->set_page_type(PAGE_TYPE_INTERSTITIAL
);
131 EXPECT_EQ(PAGE_TYPE_INTERSTITIAL
, entry2_
->GetPageType());
134 EXPECT_EQ(GURL(), entry1_
->GetReferrer().url
);
135 EXPECT_EQ(GURL("from"), entry2_
->GetReferrer().url
);
136 entry2_
->SetReferrer(
137 Referrer(GURL("from2"), blink::WebReferrerPolicyDefault
));
138 EXPECT_EQ(GURL("from2"), entry2_
->GetReferrer().url
);
141 EXPECT_EQ(base::string16(), entry1_
->GetTitle());
142 EXPECT_EQ(ASCIIToUTF16("title"), entry2_
->GetTitle());
143 entry2_
->SetTitle(ASCIIToUTF16("title2"));
144 EXPECT_EQ(ASCIIToUTF16("title2"), entry2_
->GetTitle());
147 EXPECT_FALSE(entry1_
->GetPageState().IsValid());
148 EXPECT_FALSE(entry2_
->GetPageState().IsValid());
149 entry2_
->SetPageState(PageState::CreateFromEncodedData("state"));
150 EXPECT_EQ("state", entry2_
->GetPageState().ToEncodedData());
153 EXPECT_EQ(-1, entry1_
->GetPageID());
154 EXPECT_EQ(3, entry2_
->GetPageID());
155 entry2_
->SetPageID(2);
156 EXPECT_EQ(2, entry2_
->GetPageID());
159 EXPECT_EQ(PAGE_TRANSITION_LINK
, entry1_
->GetTransitionType());
160 EXPECT_EQ(PAGE_TRANSITION_TYPED
, entry2_
->GetTransitionType());
161 entry2_
->SetTransitionType(PAGE_TRANSITION_RELOAD
);
162 EXPECT_EQ(PAGE_TRANSITION_RELOAD
, entry2_
->GetTransitionType());
164 // Is renderer initiated
165 EXPECT_FALSE(entry1_
->is_renderer_initiated());
166 EXPECT_FALSE(entry2_
->is_renderer_initiated());
167 entry2_
->set_is_renderer_initiated(true);
168 EXPECT_TRUE(entry2_
->is_renderer_initiated());
171 EXPECT_FALSE(entry1_
->GetHasPostData());
172 EXPECT_FALSE(entry2_
->GetHasPostData());
173 entry2_
->SetHasPostData(true);
174 EXPECT_TRUE(entry2_
->GetHasPostData());
177 EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE
, entry1_
->restore_type());
178 EXPECT_FALSE(entry1_
->IsRestored());
179 EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE
, entry2_
->restore_type());
180 EXPECT_FALSE(entry2_
->IsRestored());
181 entry2_
->set_restore_type(
182 NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY
);
183 EXPECT_EQ(NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY
,
184 entry2_
->restore_type());
185 EXPECT_TRUE(entry2_
->IsRestored());
188 EXPECT_EQ(GURL(), entry1_
->GetOriginalRequestURL());
189 EXPECT_EQ(GURL(), entry2_
->GetOriginalRequestURL());
190 entry2_
->SetOriginalRequestURL(GURL("original_url"));
191 EXPECT_EQ(GURL("original_url"), entry2_
->GetOriginalRequestURL());
193 // User agent override
194 EXPECT_FALSE(entry1_
->GetIsOverridingUserAgent());
195 EXPECT_FALSE(entry2_
->GetIsOverridingUserAgent());
196 entry2_
->SetIsOverridingUserAgent(true);
197 EXPECT_TRUE(entry2_
->GetIsOverridingUserAgent());
199 // Browser initiated post data
200 EXPECT_EQ(NULL
, entry1_
->GetBrowserInitiatedPostData());
201 EXPECT_EQ(NULL
, entry2_
->GetBrowserInitiatedPostData());
202 const int length
= 11;
203 const unsigned char* raw_data
=
204 reinterpret_cast<const unsigned char*>("post\n\n\0data");
205 std::vector
<unsigned char> post_data_vector(raw_data
, raw_data
+length
);
206 scoped_refptr
<base::RefCountedBytes
> post_data
=
207 base::RefCountedBytes::TakeVector(&post_data_vector
);
208 entry2_
->SetBrowserInitiatedPostData(post_data
.get());
209 EXPECT_EQ(post_data
->front(),
210 entry2_
->GetBrowserInitiatedPostData()->front());
212 // Frame to navigate.
213 EXPECT_TRUE(entry1_
->GetFrameToNavigate().empty());
214 EXPECT_TRUE(entry2_
->GetFrameToNavigate().empty());
218 TEST_F(NavigationEntryTest
, NavigationEntryTimestamps
) {
219 EXPECT_EQ(base::Time(), entry1_
->GetTimestamp());
220 const base::Time now
= base::Time::Now();
221 entry1_
->SetTimestamp(now
);
222 EXPECT_EQ(now
, entry1_
->GetTimestamp());
225 // Test extra data stored in the navigation entry.
226 TEST_F(NavigationEntryTest
, NavigationEntryExtraData
) {
227 base::string16 test_data
= ASCIIToUTF16("my search terms");
228 base::string16 output
;
229 entry1_
->SetExtraData("search_terms", test_data
);
231 EXPECT_FALSE(entry1_
->GetExtraData("non_existent_key", &output
));
232 EXPECT_EQ(ASCIIToUTF16(""), output
);
233 EXPECT_TRUE(entry1_
->GetExtraData("search_terms", &output
));
234 EXPECT_EQ(test_data
, output
);
236 entry1_
->ClearExtraData("search_terms");
237 // Content in |output| is not modified if data is not present at the key.
238 EXPECT_FALSE(entry1_
->GetExtraData("search_terms", &output
));
239 EXPECT_EQ(test_data
, output
);
240 // Using an empty string shows that the data is not present in the map.
241 base::string16 output2
;
242 EXPECT_FALSE(entry1_
->GetExtraData("search_terms", &output2
));
243 EXPECT_EQ(ASCIIToUTF16(""), output2
);
246 } // namespace content