1 // Copyright (c) 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 "components/sessions/serialized_navigation_entry.h"
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/pickle.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/time/time.h"
17 #include "components/sessions/core/serialized_navigation_driver.h"
18 #include "components/sessions/serialized_navigation_entry_test_helper.h"
19 #include "sync/protocol/session_specifics.pb.h"
20 #include "sync/util/time.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/base/page_transition_types.h"
28 // Create a sync_pb::TabNavigation from the constants above.
29 sync_pb::TabNavigation
MakeSyncDataForTest() {
30 sync_pb::TabNavigation sync_data
;
31 sync_data
.set_virtual_url(test_data::kVirtualURL
.spec());
32 sync_data
.set_referrer(test_data::kReferrerURL
.spec());
33 sync_data
.set_obsolete_referrer_policy(test_data::kReferrerPolicy
);
34 sync_data
.set_correct_referrer_policy(test_data::kReferrerPolicy
);
35 sync_data
.set_title(base::UTF16ToUTF8(test_data::kTitle
));
36 sync_data
.set_state(test_data::kEncodedPageState
);
37 sync_data
.set_page_transition(
38 sync_pb::SyncEnums_PageTransition_AUTO_SUBFRAME
);
39 sync_data
.set_unique_id(test_data::kUniqueID
);
40 sync_data
.set_timestamp_msec(syncer::TimeToProtoTime(test_data::kTimestamp
));
41 sync_data
.set_redirect_type(sync_pb::SyncEnums::CLIENT_REDIRECT
);
42 sync_data
.set_navigation_home_page(true);
43 sync_data
.set_search_terms(base::UTF16ToUTF8(test_data::kSearchTerms
));
44 sync_data
.set_favicon_url(test_data::kFaviconURL
.spec());
45 sync_data
.set_http_status_code(test_data::kHttpStatusCode
);
46 // The redirect chain only syncs one way.
50 // Create a default SerializedNavigationEntry. All its fields should be
51 // initialized to their respective default values.
52 TEST(SerializedNavigationEntryTest
, DefaultInitializer
) {
53 const SerializedNavigationEntry navigation
;
54 EXPECT_EQ(-1, navigation
.index());
55 EXPECT_EQ(0, navigation
.unique_id());
56 EXPECT_EQ(GURL(), navigation
.referrer_url());
58 SerializedNavigationDriver::Get()->GetDefaultReferrerPolicy(),
59 navigation
.referrer_policy());
60 EXPECT_EQ(GURL(), navigation
.virtual_url());
61 EXPECT_TRUE(navigation
.title().empty());
62 EXPECT_EQ(std::string(), navigation
.encoded_page_state());
63 EXPECT_EQ(ui::PAGE_TRANSITION_TYPED
, navigation
.transition_type());
64 EXPECT_FALSE(navigation
.has_post_data());
65 EXPECT_EQ(-1, navigation
.post_id());
66 EXPECT_EQ(GURL(), navigation
.original_request_url());
67 EXPECT_FALSE(navigation
.is_overriding_user_agent());
68 EXPECT_TRUE(navigation
.timestamp().is_null());
69 EXPECT_TRUE(navigation
.search_terms().empty());
70 EXPECT_FALSE(navigation
.favicon_url().is_valid());
71 EXPECT_EQ(0, navigation
.http_status_code());
72 EXPECT_EQ(0U, navigation
.redirect_chain().size());
75 // Create a SerializedNavigationEntry from a sync_pb::TabNavigation. All its
76 // fields should match the protocol buffer's if it exists there, and
77 // sbould be set to the default value otherwise.
78 TEST(SerializedNavigationEntryTest
, FromSyncData
) {
79 const sync_pb::TabNavigation sync_data
= MakeSyncDataForTest();
81 const SerializedNavigationEntry
& navigation
=
82 SerializedNavigationEntry::FromSyncData(test_data::kIndex
, sync_data
);
84 EXPECT_EQ(test_data::kIndex
, navigation
.index());
85 EXPECT_EQ(test_data::kUniqueID
, navigation
.unique_id());
86 EXPECT_EQ(test_data::kReferrerURL
, navigation
.referrer_url());
87 EXPECT_EQ(test_data::kReferrerPolicy
, navigation
.referrer_policy());
88 EXPECT_EQ(test_data::kVirtualURL
, navigation
.virtual_url());
89 EXPECT_EQ(test_data::kTitle
, navigation
.title());
90 EXPECT_EQ(test_data::kEncodedPageState
, navigation
.encoded_page_state());
91 EXPECT_EQ(test_data::kTransitionType
, navigation
.transition_type());
92 EXPECT_FALSE(navigation
.has_post_data());
93 EXPECT_EQ(-1, navigation
.post_id());
94 EXPECT_EQ(GURL(), navigation
.original_request_url());
95 EXPECT_FALSE(navigation
.is_overriding_user_agent());
96 EXPECT_TRUE(navigation
.timestamp().is_null());
97 EXPECT_EQ(test_data::kSearchTerms
, navigation
.search_terms());
98 EXPECT_EQ(test_data::kFaviconURL
, navigation
.favicon_url());
99 EXPECT_EQ(test_data::kHttpStatusCode
, navigation
.http_status_code());
100 // The redirect chain only syncs one way.
103 // Create a SerializedNavigationEntry, pickle it, then create another one by
104 // unpickling. The new one should match the old one except for fields
105 // that aren't pickled, which should be set to default values.
106 TEST(SerializedNavigationEntryTest
, Pickle
) {
107 const SerializedNavigationEntry old_navigation
=
108 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
111 old_navigation
.WriteToPickle(30000, &pickle
);
113 SerializedNavigationEntry new_navigation
;
114 base::PickleIterator
pickle_iterator(pickle
);
115 EXPECT_TRUE(new_navigation
.ReadFromPickle(&pickle_iterator
));
117 // Fields that are written to the pickle.
118 EXPECT_EQ(test_data::kIndex
, new_navigation
.index());
119 EXPECT_EQ(test_data::kReferrerURL
, new_navigation
.referrer_url());
120 EXPECT_EQ(test_data::kReferrerPolicy
, new_navigation
.referrer_policy());
121 EXPECT_EQ(test_data::kVirtualURL
, new_navigation
.virtual_url());
122 EXPECT_EQ(test_data::kTitle
, new_navigation
.title());
123 EXPECT_EQ(test_data::kTransitionType
, new_navigation
.transition_type());
124 EXPECT_EQ(test_data::kHasPostData
, new_navigation
.has_post_data());
125 EXPECT_EQ(test_data::kOriginalRequestURL
,
126 new_navigation
.original_request_url());
127 EXPECT_EQ(test_data::kIsOverridingUserAgent
,
128 new_navigation
.is_overriding_user_agent());
129 EXPECT_EQ(test_data::kTimestamp
, new_navigation
.timestamp());
130 EXPECT_EQ(test_data::kSearchTerms
, new_navigation
.search_terms());
131 EXPECT_EQ(test_data::kHttpStatusCode
, new_navigation
.http_status_code());
133 // Fields that are not written to the pickle.
134 EXPECT_EQ(0, new_navigation
.unique_id());
135 EXPECT_EQ(std::string(), new_navigation
.encoded_page_state());
136 EXPECT_EQ(-1, new_navigation
.post_id());
137 EXPECT_EQ(0U, new_navigation
.redirect_chain().size());
140 // Create a SerializedNavigationEntry, then create a sync protocol buffer from
141 // it. The protocol buffer should have matching fields to the
142 // SerializedNavigationEntry (when applicable).
143 TEST(SerializedNavigationEntryTest
, ToSyncData
) {
144 const SerializedNavigationEntry navigation
=
145 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
146 const sync_pb::TabNavigation sync_data
= navigation
.ToSyncData();
148 EXPECT_EQ(test_data::kVirtualURL
.spec(), sync_data
.virtual_url());
149 EXPECT_EQ(test_data::kReferrerURL
.spec(), sync_data
.referrer());
150 EXPECT_EQ(test_data::kTitle
, base::ASCIIToUTF16(sync_data
.title()));
151 EXPECT_TRUE(sync_data
.state().empty());
152 EXPECT_EQ(sync_pb::SyncEnums_PageTransition_AUTO_SUBFRAME
,
153 sync_data
.page_transition());
154 EXPECT_TRUE(sync_data
.has_redirect_type());
155 EXPECT_EQ(test_data::kUniqueID
, sync_data
.unique_id());
156 EXPECT_EQ(syncer::TimeToProtoTime(test_data::kTimestamp
),
157 sync_data
.timestamp_msec());
158 EXPECT_EQ(test_data::kTimestamp
.ToInternalValue(), sync_data
.global_id());
159 EXPECT_EQ(test_data::kFaviconURL
.spec(), sync_data
.favicon_url());
160 EXPECT_EQ(test_data::kHttpStatusCode
, sync_data
.http_status_code());
161 // The proto navigation redirects don't include the final chain entry
162 // (because it didn't redirect) so the lengths should differ by 1.
163 ASSERT_EQ(3, sync_data
.navigation_redirect_size() + 1);
164 EXPECT_EQ(test_data::kRedirectURL0
.spec(),
165 sync_data
.navigation_redirect(0).url());
166 EXPECT_EQ(test_data::kRedirectURL1
.spec(),
167 sync_data
.navigation_redirect(1).url());
168 EXPECT_FALSE(sync_data
.has_last_navigation_redirect_url());
171 // Test that the last_navigation_redirect_url is set when needed. This test is
172 // just like the above, but with a different virtual_url. Create a
173 // SerializedNavigationEntry, then create a sync protocol buffer from it. The
174 // protocol buffer should have a last_navigation_redirect_url.
175 TEST(SerializedNavigationEntryTest
, LastNavigationRedirectUrl
) {
176 SerializedNavigationEntry navigation
=
177 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
178 SerializedNavigationEntryTestHelper::SetVirtualURL(
179 test_data::kOtherURL
, &navigation
);
181 const sync_pb::TabNavigation sync_data
= navigation
.ToSyncData();
182 EXPECT_TRUE(sync_data
.has_last_navigation_redirect_url());
183 EXPECT_EQ(test_data::kVirtualURL
.spec(),
184 sync_data
.last_navigation_redirect_url());
186 // The redirect chain should be the same as in the above test.
187 ASSERT_EQ(3, sync_data
.navigation_redirect_size() + 1);
188 EXPECT_EQ(test_data::kRedirectURL0
.spec(),
189 sync_data
.navigation_redirect(0).url());
190 EXPECT_EQ(test_data::kRedirectURL1
.spec(),
191 sync_data
.navigation_redirect(1).url());
194 // Ensure all transition types and qualifiers are converted to/from the sync
195 // SerializedNavigationEntry representation properly.
196 TEST(SerializedNavigationEntryTest
, TransitionTypes
) {
197 SerializedNavigationEntry navigation
=
198 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
200 for (uint32 core_type
= ui::PAGE_TRANSITION_LINK
;
201 core_type
!= ui::PAGE_TRANSITION_LAST_CORE
; ++core_type
) {
202 // Because qualifier is a uint32, left shifting will eventually overflow
203 // and hit zero again. SERVER_REDIRECT, as the last qualifier and also
204 // in place of the sign bit, is therefore the last transition before
206 for (uint32 qualifier
= ui::PAGE_TRANSITION_FORWARD_BACK
;
207 qualifier
!= 0; qualifier
<<= 1) {
208 if (qualifier
== 0x08000000)
209 continue; // 0x08000000 is not a valid qualifier.
210 ui::PageTransition transition
=
211 ui::PageTransitionFromInt(core_type
| qualifier
);
212 SerializedNavigationEntryTestHelper::SetTransitionType(
213 transition
, &navigation
);
215 const sync_pb::TabNavigation
& sync_data
= navigation
.ToSyncData();
216 const SerializedNavigationEntry
& constructed_nav
=
217 SerializedNavigationEntry::FromSyncData(test_data::kIndex
, sync_data
);
218 const ui::PageTransition constructed_transition
=
219 constructed_nav
.transition_type();
221 EXPECT_EQ(transition
, constructed_transition
);
227 } // namespace sessions