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
3 // found in the LICENSE file.
5 #include "components/sessions/content/content_serialized_navigation_builder.h"
7 #include "components/sessions/serialized_navigation_entry.h"
8 #include "components/sessions/serialized_navigation_entry_test_helper.h"
9 #include "content/public/browser/favicon_status.h"
10 #include "content/public/browser/navigation_entry.h"
11 #include "content/public/common/page_state.h"
12 #include "content/public/common/referrer.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 // Create a NavigationEntry from the test_data constants in
19 // serialized_navigation_entry_test_helper.h.
20 scoped_ptr
<content::NavigationEntry
> MakeNavigationEntryForTest() {
21 scoped_ptr
<content::NavigationEntry
> navigation_entry(
22 content::NavigationEntry::Create());
23 navigation_entry
->SetReferrer(content::Referrer(
24 test_data::kReferrerURL
,
25 static_cast<blink::WebReferrerPolicy
>(test_data::kReferrerPolicy
)));
26 navigation_entry
->SetVirtualURL(test_data::kVirtualURL
);
27 navigation_entry
->SetTitle(test_data::kTitle
);
28 navigation_entry
->SetPageState(
29 content::PageState::CreateFromEncodedData(test_data::kEncodedPageState
));
30 navigation_entry
->SetTransitionType(test_data::kTransitionType
);
31 navigation_entry
->SetHasPostData(test_data::kHasPostData
);
32 navigation_entry
->SetPostID(test_data::kPostID
);
33 navigation_entry
->SetOriginalRequestURL(test_data::kOriginalRequestURL
);
34 navigation_entry
->SetIsOverridingUserAgent(test_data::kIsOverridingUserAgent
);
35 navigation_entry
->SetTimestamp(test_data::kTimestamp
);
36 navigation_entry
->SetExtraData(kSearchTermsKey
,
37 test_data::kSearchTerms
);
38 navigation_entry
->GetFavicon().valid
= true;
39 navigation_entry
->GetFavicon().url
= test_data::kFaviconURL
;
40 navigation_entry
->SetHttpStatusCode(test_data::kHttpStatusCode
);
41 std::vector
<GURL
> redirect_chain
;
42 redirect_chain
.push_back(test_data::kRedirectURL0
);
43 redirect_chain
.push_back(test_data::kRedirectURL1
);
44 redirect_chain
.push_back(test_data::kVirtualURL
);
45 navigation_entry
->SetRedirectChain(redirect_chain
);
46 return navigation_entry
.Pass();
52 // Create a SerializedNavigationEntry from a NavigationEntry. All its fields
53 // should match the NavigationEntry's.
54 TEST(ContentSerializedNavigationBuilderTest
, FromNavigationEntry
) {
55 const scoped_ptr
<content::NavigationEntry
> navigation_entry(
56 MakeNavigationEntryForTest());
58 const SerializedNavigationEntry
& navigation
=
59 ContentSerializedNavigationBuilder::FromNavigationEntry(
60 test_data::kIndex
, *navigation_entry
);
62 EXPECT_EQ(test_data::kIndex
, navigation
.index());
64 EXPECT_EQ(navigation_entry
->GetUniqueID(), navigation
.unique_id());
65 EXPECT_EQ(test_data::kReferrerURL
, navigation
.referrer_url());
66 EXPECT_EQ(test_data::kReferrerPolicy
, navigation
.referrer_policy());
67 EXPECT_EQ(test_data::kVirtualURL
, navigation
.virtual_url());
68 EXPECT_EQ(test_data::kTitle
, navigation
.title());
69 EXPECT_EQ(test_data::kEncodedPageState
, navigation
.encoded_page_state());
70 EXPECT_EQ(test_data::kTransitionType
, navigation
.transition_type());
71 EXPECT_EQ(test_data::kHasPostData
, navigation
.has_post_data());
72 EXPECT_EQ(test_data::kPostID
, navigation
.post_id());
73 EXPECT_EQ(test_data::kOriginalRequestURL
, navigation
.original_request_url());
74 EXPECT_EQ(test_data::kIsOverridingUserAgent
,
75 navigation
.is_overriding_user_agent());
76 EXPECT_EQ(test_data::kTimestamp
, navigation
.timestamp());
77 EXPECT_EQ(test_data::kFaviconURL
, navigation
.favicon_url());
78 EXPECT_EQ(test_data::kHttpStatusCode
, navigation
.http_status_code());
79 ASSERT_EQ(3U, navigation
.redirect_chain().size());
80 EXPECT_EQ(test_data::kRedirectURL0
, navigation
.redirect_chain()[0]);
81 EXPECT_EQ(test_data::kRedirectURL1
, navigation
.redirect_chain()[1]);
82 EXPECT_EQ(test_data::kVirtualURL
, navigation
.redirect_chain()[2]);
85 // Create a NavigationEntry, then create another one by converting to
86 // a SerializedNavigationEntry and back. The new one should match the old one
87 // except for fields that aren't preserved, which should be set to
89 TEST(ContentSerializedNavigationBuilderTest
, ToNavigationEntry
) {
90 const scoped_ptr
<content::NavigationEntry
> old_navigation_entry(
91 MakeNavigationEntryForTest());
93 const SerializedNavigationEntry
& navigation
=
94 ContentSerializedNavigationBuilder::FromNavigationEntry(
95 test_data::kIndex
, *old_navigation_entry
);
97 const scoped_ptr
<content::NavigationEntry
> new_navigation_entry(
98 ContentSerializedNavigationBuilder::ToNavigationEntry(
99 &navigation
, test_data::kPageID
, NULL
));
101 EXPECT_EQ(test_data::kReferrerURL
, new_navigation_entry
->GetReferrer().url
);
102 EXPECT_EQ(test_data::kReferrerPolicy
,
103 new_navigation_entry
->GetReferrer().policy
);
104 EXPECT_EQ(test_data::kVirtualURL
, new_navigation_entry
->GetVirtualURL());
105 EXPECT_EQ(test_data::kTitle
, new_navigation_entry
->GetTitle());
106 EXPECT_EQ(test_data::kEncodedPageState
,
107 new_navigation_entry
->GetPageState().ToEncodedData());
108 EXPECT_EQ(test_data::kPageID
, new_navigation_entry
->GetPageID());
109 EXPECT_EQ(ui::PAGE_TRANSITION_RELOAD
,
110 new_navigation_entry
->GetTransitionType());
111 EXPECT_EQ(test_data::kHasPostData
, new_navigation_entry
->GetHasPostData());
112 EXPECT_EQ(test_data::kPostID
, new_navigation_entry
->GetPostID());
113 EXPECT_EQ(test_data::kOriginalRequestURL
,
114 new_navigation_entry
->GetOriginalRequestURL());
115 EXPECT_EQ(test_data::kIsOverridingUserAgent
,
116 new_navigation_entry
->GetIsOverridingUserAgent());
117 base::string16 search_terms
;
118 new_navigation_entry
->GetExtraData(kSearchTermsKey
, &search_terms
);
119 EXPECT_EQ(test_data::kSearchTerms
, search_terms
);
120 EXPECT_EQ(test_data::kHttpStatusCode
,
121 new_navigation_entry
->GetHttpStatusCode());
122 ASSERT_EQ(3U, new_navigation_entry
->GetRedirectChain().size());
123 EXPECT_EQ(test_data::kRedirectURL0
,
124 new_navigation_entry
->GetRedirectChain()[0]);
125 EXPECT_EQ(test_data::kRedirectURL1
,
126 new_navigation_entry
->GetRedirectChain()[1]);
127 EXPECT_EQ(test_data::kVirtualURL
,
128 new_navigation_entry
->GetRedirectChain()[2]);
131 } // namespace sessions