gn format: Special case for single arg function calls containing boolean ops
[chromium-blink-merge.git] / components / sessions / serialized_navigation_entry_unittest.cc
blob70a3086a0fcecec7c505f6d81d0917e2ed2254c6
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"
7 #include <cstddef>
8 #include <string>
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"
23 #include "url/gurl.h"
25 namespace sessions {
26 namespace {
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_referrer_policy(test_data::kReferrerPolicy);
34 sync_data.set_title(base::UTF16ToUTF8(test_data::kTitle));
35 sync_data.set_state(test_data::kEncodedPageState);
36 sync_data.set_page_transition(
37 sync_pb::SyncEnums_PageTransition_AUTO_SUBFRAME);
38 sync_data.set_unique_id(test_data::kUniqueID);
39 sync_data.set_timestamp_msec(syncer::TimeToProtoTime(test_data::kTimestamp));
40 sync_data.set_redirect_type(sync_pb::SyncEnums::CLIENT_REDIRECT);
41 sync_data.set_navigation_home_page(true);
42 sync_data.set_search_terms(base::UTF16ToUTF8(test_data::kSearchTerms));
43 sync_data.set_favicon_url(test_data::kFaviconURL.spec());
44 sync_data.set_http_status_code(test_data::kHttpStatusCode);
45 // The redirect chain only syncs one way.
46 return sync_data;
49 // Create a default SerializedNavigationEntry. All its fields should be
50 // initialized to their respective default values.
51 TEST(SerializedNavigationEntryTest, DefaultInitializer) {
52 const SerializedNavigationEntry navigation;
53 EXPECT_EQ(-1, navigation.index());
54 EXPECT_EQ(0, navigation.unique_id());
55 EXPECT_EQ(GURL(), navigation.referrer_url());
56 EXPECT_EQ(
57 SerializedNavigationDriver::Get()->GetDefaultReferrerPolicy(),
58 navigation.referrer_policy());
59 EXPECT_EQ(GURL(), navigation.virtual_url());
60 EXPECT_TRUE(navigation.title().empty());
61 EXPECT_EQ(std::string(), navigation.encoded_page_state());
62 EXPECT_EQ(ui::PAGE_TRANSITION_TYPED, navigation.transition_type());
63 EXPECT_FALSE(navigation.has_post_data());
64 EXPECT_EQ(-1, navigation.post_id());
65 EXPECT_EQ(GURL(), navigation.original_request_url());
66 EXPECT_FALSE(navigation.is_overriding_user_agent());
67 EXPECT_TRUE(navigation.timestamp().is_null());
68 EXPECT_TRUE(navigation.search_terms().empty());
69 EXPECT_FALSE(navigation.favicon_url().is_valid());
70 EXPECT_EQ(0, navigation.http_status_code());
71 EXPECT_EQ(0U, navigation.redirect_chain().size());
74 // Create a SerializedNavigationEntry from a sync_pb::TabNavigation. All its
75 // fields should match the protocol buffer's if it exists there, and
76 // sbould be set to the default value otherwise.
77 TEST(SerializedNavigationEntryTest, FromSyncData) {
78 const sync_pb::TabNavigation sync_data = MakeSyncDataForTest();
80 const SerializedNavigationEntry& navigation =
81 SerializedNavigationEntry::FromSyncData(test_data::kIndex, sync_data);
83 EXPECT_EQ(test_data::kIndex, navigation.index());
84 EXPECT_EQ(test_data::kUniqueID, navigation.unique_id());
85 EXPECT_EQ(test_data::kReferrerURL, navigation.referrer_url());
86 EXPECT_EQ(test_data::kReferrerPolicy, navigation.referrer_policy());
87 EXPECT_EQ(test_data::kVirtualURL, navigation.virtual_url());
88 EXPECT_EQ(test_data::kTitle, navigation.title());
89 EXPECT_EQ(test_data::kEncodedPageState, navigation.encoded_page_state());
90 EXPECT_EQ(test_data::kTransitionType, navigation.transition_type());
91 EXPECT_FALSE(navigation.has_post_data());
92 EXPECT_EQ(-1, navigation.post_id());
93 EXPECT_EQ(GURL(), navigation.original_request_url());
94 EXPECT_FALSE(navigation.is_overriding_user_agent());
95 EXPECT_TRUE(navigation.timestamp().is_null());
96 EXPECT_EQ(test_data::kSearchTerms, navigation.search_terms());
97 EXPECT_EQ(test_data::kFaviconURL, navigation.favicon_url());
98 EXPECT_EQ(test_data::kHttpStatusCode, navigation.http_status_code());
99 // The redirect chain only syncs one way.
102 // Create a SerializedNavigationEntry, pickle it, then create another one by
103 // unpickling. The new one should match the old one except for fields
104 // that aren't pickled, which should be set to default values.
105 TEST(SerializedNavigationEntryTest, Pickle) {
106 const SerializedNavigationEntry old_navigation =
107 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
109 Pickle pickle;
110 old_navigation.WriteToPickle(30000, &pickle);
112 SerializedNavigationEntry new_navigation;
113 PickleIterator pickle_iterator(pickle);
114 EXPECT_TRUE(new_navigation.ReadFromPickle(&pickle_iterator));
116 // Fields that are written to the pickle.
117 EXPECT_EQ(test_data::kIndex, new_navigation.index());
118 EXPECT_EQ(test_data::kReferrerURL, new_navigation.referrer_url());
119 EXPECT_EQ(test_data::kReferrerPolicy, new_navigation.referrer_policy());
120 EXPECT_EQ(test_data::kVirtualURL, new_navigation.virtual_url());
121 EXPECT_EQ(test_data::kTitle, new_navigation.title());
122 EXPECT_EQ(test_data::kTransitionType, new_navigation.transition_type());
123 EXPECT_EQ(test_data::kHasPostData, new_navigation.has_post_data());
124 EXPECT_EQ(test_data::kOriginalRequestURL,
125 new_navigation.original_request_url());
126 EXPECT_EQ(test_data::kIsOverridingUserAgent,
127 new_navigation.is_overriding_user_agent());
128 EXPECT_EQ(test_data::kTimestamp, new_navigation.timestamp());
129 EXPECT_EQ(test_data::kSearchTerms, new_navigation.search_terms());
130 EXPECT_EQ(test_data::kHttpStatusCode, new_navigation.http_status_code());
132 // Fields that are not written to the pickle.
133 EXPECT_EQ(0, new_navigation.unique_id());
134 EXPECT_EQ(std::string(), new_navigation.encoded_page_state());
135 EXPECT_EQ(-1, new_navigation.post_id());
136 EXPECT_EQ(0U, new_navigation.redirect_chain().size());
139 // Create a SerializedNavigationEntry, then create a sync protocol buffer from
140 // it. The protocol buffer should have matching fields to the
141 // SerializedNavigationEntry (when applicable).
142 TEST(SerializedNavigationEntryTest, ToSyncData) {
143 const SerializedNavigationEntry navigation =
144 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
145 const sync_pb::TabNavigation sync_data = navigation.ToSyncData();
147 EXPECT_EQ(test_data::kVirtualURL.spec(), sync_data.virtual_url());
148 EXPECT_EQ(test_data::kReferrerURL.spec(), sync_data.referrer());
149 EXPECT_EQ(test_data::kTitle, base::ASCIIToUTF16(sync_data.title()));
150 EXPECT_TRUE(sync_data.state().empty());
151 EXPECT_EQ(sync_pb::SyncEnums_PageTransition_AUTO_SUBFRAME,
152 sync_data.page_transition());
153 EXPECT_TRUE(sync_data.has_redirect_type());
154 EXPECT_EQ(test_data::kUniqueID, sync_data.unique_id());
155 EXPECT_EQ(syncer::TimeToProtoTime(test_data::kTimestamp),
156 sync_data.timestamp_msec());
157 EXPECT_EQ(test_data::kTimestamp.ToInternalValue(), sync_data.global_id());
158 EXPECT_EQ(test_data::kFaviconURL.spec(), sync_data.favicon_url());
159 EXPECT_EQ(test_data::kHttpStatusCode, sync_data.http_status_code());
160 // The proto navigation redirects don't include the final chain entry
161 // (because it didn't redirect) so the lengths should differ by 1.
162 ASSERT_EQ(3, sync_data.navigation_redirect_size() + 1);
163 EXPECT_EQ(test_data::kRedirectURL0.spec(),
164 sync_data.navigation_redirect(0).url());
165 EXPECT_EQ(test_data::kRedirectURL1.spec(),
166 sync_data.navigation_redirect(1).url());
167 EXPECT_FALSE(sync_data.has_last_navigation_redirect_url());
170 // Test that the last_navigation_redirect_url is set when needed. This test is
171 // just like the above, but with a different virtual_url. Create a
172 // SerializedNavigationEntry, then create a sync protocol buffer from it. The
173 // protocol buffer should have a last_navigation_redirect_url.
174 TEST(SerializedNavigationEntryTest, LastNavigationRedirectUrl) {
175 SerializedNavigationEntry navigation =
176 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
177 SerializedNavigationEntryTestHelper::SetVirtualURL(
178 test_data::kOtherURL, &navigation);
180 const sync_pb::TabNavigation sync_data = navigation.ToSyncData();
181 EXPECT_TRUE(sync_data.has_last_navigation_redirect_url());
182 EXPECT_EQ(test_data::kVirtualURL.spec(),
183 sync_data.last_navigation_redirect_url());
185 // The redirect chain should be the same as in the above test.
186 ASSERT_EQ(3, sync_data.navigation_redirect_size() + 1);
187 EXPECT_EQ(test_data::kRedirectURL0.spec(),
188 sync_data.navigation_redirect(0).url());
189 EXPECT_EQ(test_data::kRedirectURL1.spec(),
190 sync_data.navigation_redirect(1).url());
193 // Ensure all transition types and qualifiers are converted to/from the sync
194 // SerializedNavigationEntry representation properly.
195 TEST(SerializedNavigationEntryTest, TransitionTypes) {
196 SerializedNavigationEntry navigation =
197 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
199 for (uint32 core_type = ui::PAGE_TRANSITION_LINK;
200 core_type != ui::PAGE_TRANSITION_LAST_CORE; ++core_type) {
201 // Because qualifier is a uint32, left shifting will eventually overflow
202 // and hit zero again. SERVER_REDIRECT, as the last qualifier and also
203 // in place of the sign bit, is therefore the last transition before
204 // breaking.
205 for (uint32 qualifier = ui::PAGE_TRANSITION_FORWARD_BACK;
206 qualifier != 0; qualifier <<= 1) {
207 if (qualifier == 0x08000000)
208 continue; // 0x08000000 is not a valid qualifier.
209 ui::PageTransition transition =
210 ui::PageTransitionFromInt(core_type | qualifier);
211 SerializedNavigationEntryTestHelper::SetTransitionType(
212 transition, &navigation);
214 const sync_pb::TabNavigation& sync_data = navigation.ToSyncData();
215 const SerializedNavigationEntry& constructed_nav =
216 SerializedNavigationEntry::FromSyncData(test_data::kIndex, sync_data);
217 const ui::PageTransition constructed_transition =
218 constructed_nav.transition_type();
220 EXPECT_EQ(transition, constructed_transition);
225 } // namespace
226 } // namespace sessions