1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sessions/content/content_serialized_navigation_builder.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/sessions/content/content_record_password_state.h"
#include "components/sessions/content/content_serialized_navigation_driver.h"
#include "components/sessions/content/extended_info_handler.h"
#include "components/sessions/core/serialized_navigation_entry.h"
#include "components/sessions/core/serialized_navigation_entry_test_helper.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/common/page_state.h"
#include "content/public/common/referrer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace sessions {
namespace {
const char kExtendedInfoKey1[] = "Key 1";
const char kExtendedInfoValue1[] = "Value 1";
const char kExtendedInfoKey2[] = "Key 2";
const char kExtendedInfoValue2[] = "Value 2";
class TestExtendedInfoHandler : public ExtendedInfoHandler {
public:
explicit TestExtendedInfoHandler(const std::string& key) : key_(key) {}
~TestExtendedInfoHandler() override {}
std::string GetExtendedInfo(
const content::NavigationEntry& entry) const override {
base::string16 data;
entry.GetExtraData(key_, &data);
return base::UTF16ToASCII(data);
}
void RestoreExtendedInfo(const std::string& info,
content::NavigationEntry* entry) override {
entry->SetExtraData(key_, base::ASCIIToUTF16(info));
}
private:
std::string key_;
DISALLOW_COPY_AND_ASSIGN(TestExtendedInfoHandler);
};
// Create a NavigationEntry from the test_data constants in
// serialized_navigation_entry_test_helper.h.
std::unique_ptr<content::NavigationEntry> MakeNavigationEntryForTest() {
std::unique_ptr<content::NavigationEntry> navigation_entry(
content::NavigationEntry::Create());
navigation_entry->SetReferrer(content::Referrer(
test_data::kReferrerURL,
static_cast<blink::WebReferrerPolicy>(test_data::kReferrerPolicy)));
navigation_entry->SetVirtualURL(test_data::kVirtualURL);
navigation_entry->SetTitle(test_data::kTitle);
navigation_entry->SetPageState(
content::PageState::CreateFromEncodedData(test_data::kEncodedPageState));
navigation_entry->SetTransitionType(test_data::kTransitionType);
navigation_entry->SetHasPostData(test_data::kHasPostData);
navigation_entry->SetPostID(test_data::kPostID);
navigation_entry->SetOriginalRequestURL(test_data::kOriginalRequestURL);
navigation_entry->SetIsOverridingUserAgent(test_data::kIsOverridingUserAgent);
navigation_entry->SetTimestamp(test_data::kTimestamp);
navigation_entry->SetExtraData(kSearchTermsKey,
test_data::kSearchTerms);
SetPasswordStateInNavigation(test_data::kPasswordState,
navigation_entry.get());
navigation_entry->GetFavicon().valid = true;
navigation_entry->GetFavicon().url = test_data::kFaviconURL;
navigation_entry->SetHttpStatusCode(test_data::kHttpStatusCode);
std::vector<GURL> redirect_chain;
redirect_chain.push_back(test_data::kRedirectURL0);
redirect_chain.push_back(test_data::kRedirectURL1);
redirect_chain.push_back(test_data::kVirtualURL);
navigation_entry->SetRedirectChain(redirect_chain);
return navigation_entry;
}
void SetExtendedInfoForTest(content::NavigationEntry* entry) {
entry->SetExtraData(kExtendedInfoKey1,
base::ASCIIToUTF16(kExtendedInfoValue1));
entry->SetExtraData(kExtendedInfoKey2,
base::ASCIIToUTF16(kExtendedInfoValue2));
}
} // namespace
class ContentSerializedNavigationBuilderTest : public testing::Test {
public:
ContentSerializedNavigationBuilderTest() {}
~ContentSerializedNavigationBuilderTest() override {}
void SetUp() override {
ContentSerializedNavigationDriver* driver =
ContentSerializedNavigationDriver::GetInstance();
driver->RegisterExtendedInfoHandler(
kExtendedInfoKey1, base::WrapUnique<ExtendedInfoHandler>(
new TestExtendedInfoHandler(kExtendedInfoKey1)));
driver->RegisterExtendedInfoHandler(
kExtendedInfoKey2, base::WrapUnique<ExtendedInfoHandler>(
new TestExtendedInfoHandler(kExtendedInfoKey2)));
}
void TearDown() override {
ContentSerializedNavigationDriver* driver =
ContentSerializedNavigationDriver::GetInstance();
driver->extended_info_handler_map_.clear();
}
private:
DISALLOW_COPY_AND_ASSIGN(ContentSerializedNavigationBuilderTest);
};
// Create a SerializedNavigationEntry from a NavigationEntry. All its fields
// should match the NavigationEntry's.
TEST_F(ContentSerializedNavigationBuilderTest, FromNavigationEntry) {
const std::unique_ptr<content::NavigationEntry> navigation_entry(
MakeNavigationEntryForTest());
SetExtendedInfoForTest(navigation_entry.get());
const SerializedNavigationEntry& navigation =
ContentSerializedNavigationBuilder::FromNavigationEntry(
test_data::kIndex, *navigation_entry);
EXPECT_EQ(test_data::kIndex, navigation.index());
EXPECT_EQ(navigation_entry->GetUniqueID(), navigation.unique_id());
EXPECT_EQ(test_data::kReferrerURL, navigation.referrer_url());
EXPECT_EQ(test_data::kReferrerPolicy, navigation.referrer_policy());
EXPECT_EQ(test_data::kVirtualURL, navigation.virtual_url());
EXPECT_EQ(test_data::kTitle, navigation.title());
EXPECT_EQ(test_data::kEncodedPageState, navigation.encoded_page_state());
EXPECT_TRUE(ui::PageTransitionTypeIncludingQualifiersIs(
navigation.transition_type(), test_data::kTransitionType));
EXPECT_EQ(test_data::kHasPostData, navigation.has_post_data());
EXPECT_EQ(test_data::kPostID, navigation.post_id());
EXPECT_EQ(test_data::kOriginalRequestURL, navigation.original_request_url());
EXPECT_EQ(test_data::kIsOverridingUserAgent,
navigation.is_overriding_user_agent());
EXPECT_EQ(test_data::kTimestamp, navigation.timestamp());
EXPECT_EQ(test_data::kFaviconURL, navigation.favicon_url());
EXPECT_EQ(test_data::kHttpStatusCode, navigation.http_status_code());
ASSERT_EQ(3U, navigation.redirect_chain().size());
EXPECT_EQ(test_data::kRedirectURL0, navigation.redirect_chain()[0]);
EXPECT_EQ(test_data::kRedirectURL1, navigation.redirect_chain()[1]);
EXPECT_EQ(test_data::kVirtualURL, navigation.redirect_chain()[2]);
EXPECT_EQ(test_data::kPasswordState, navigation.password_state());
ASSERT_EQ(2U, navigation.extended_info_map().size());
ASSERT_EQ(1U, navigation.extended_info_map().count(kExtendedInfoKey1));
EXPECT_EQ(kExtendedInfoValue1,
navigation.extended_info_map().at(kExtendedInfoKey1));
ASSERT_EQ(1U, navigation.extended_info_map().count(kExtendedInfoKey2));
EXPECT_EQ(kExtendedInfoValue2,
navigation.extended_info_map().at(kExtendedInfoKey2));
}
// Create a NavigationEntry, then create another one by converting to
// a SerializedNavigationEntry and back. The new one should match the old one
// except for fields that aren't preserved, which should be set to
// expected values.
TEST_F(ContentSerializedNavigationBuilderTest, ToNavigationEntry) {
const std::unique_ptr<content::NavigationEntry> old_navigation_entry(
MakeNavigationEntryForTest());
SetExtendedInfoForTest(old_navigation_entry.get());
const SerializedNavigationEntry& navigation =
ContentSerializedNavigationBuilder::FromNavigationEntry(
test_data::kIndex, *old_navigation_entry);
const std::unique_ptr<content::NavigationEntry> new_navigation_entry(
ContentSerializedNavigationBuilder::ToNavigationEntry(
&navigation, NULL));
EXPECT_EQ(test_data::kReferrerURL, new_navigation_entry->GetReferrer().url);
EXPECT_EQ(test_data::kReferrerPolicy,
new_navigation_entry->GetReferrer().policy);
EXPECT_EQ(test_data::kVirtualURL, new_navigation_entry->GetVirtualURL());
EXPECT_EQ(test_data::kTitle, new_navigation_entry->GetTitle());
EXPECT_EQ(test_data::kEncodedPageState,
new_navigation_entry->GetPageState().ToEncodedData());
EXPECT_TRUE(ui::PageTransitionTypeIncludingQualifiersIs(
new_navigation_entry->GetTransitionType(), ui::PAGE_TRANSITION_RELOAD));
EXPECT_EQ(test_data::kHasPostData, new_navigation_entry->GetHasPostData());
EXPECT_EQ(test_data::kPostID, new_navigation_entry->GetPostID());
EXPECT_EQ(test_data::kOriginalRequestURL,
new_navigation_entry->GetOriginalRequestURL());
EXPECT_EQ(test_data::kIsOverridingUserAgent,
new_navigation_entry->GetIsOverridingUserAgent());
base::string16 search_terms;
new_navigation_entry->GetExtraData(kSearchTermsKey, &search_terms);
EXPECT_EQ(test_data::kSearchTerms, search_terms);
EXPECT_EQ(test_data::kHttpStatusCode,
new_navigation_entry->GetHttpStatusCode());
ASSERT_EQ(3U, new_navigation_entry->GetRedirectChain().size());
EXPECT_EQ(test_data::kRedirectURL0,
new_navigation_entry->GetRedirectChain()[0]);
EXPECT_EQ(test_data::kRedirectURL1,
new_navigation_entry->GetRedirectChain()[1]);
EXPECT_EQ(test_data::kVirtualURL,
new_navigation_entry->GetRedirectChain()[2]);
base::string16 extra_data;
EXPECT_TRUE(
new_navigation_entry->GetExtraData(kExtendedInfoKey1, &extra_data));
EXPECT_EQ(kExtendedInfoValue1, base::UTF16ToASCII(extra_data));
EXPECT_TRUE(
new_navigation_entry->GetExtraData(kExtendedInfoKey2, &extra_data));
EXPECT_EQ(kExtendedInfoValue2, base::UTF16ToASCII(extra_data));
}
TEST_F(ContentSerializedNavigationBuilderTest, SetPasswordState) {
std::unique_ptr<content::NavigationEntry> entry(
content::NavigationEntry::Create());
EXPECT_EQ(SerializedNavigationEntry::PASSWORD_STATE_UNKNOWN,
GetPasswordStateFromNavigation(*entry));
SetPasswordStateInNavigation(SerializedNavigationEntry::NO_PASSWORD_FIELD,
entry.get());
EXPECT_EQ(SerializedNavigationEntry::NO_PASSWORD_FIELD,
GetPasswordStateFromNavigation(*entry));
}
} // namespace sessions
|