OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Foundation/Foundation.h> | 5 #import <Foundation/Foundation.h> |
6 | 6 |
7 #include "base/mac/scoped_nsobject.h" | 7 #include "base/mac/scoped_nsobject.h" |
8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
9 #import "ios/testing/ocmock_complex_type_helper.h" | 9 #import "ios/testing/ocmock_complex_type_helper.h" |
10 #import "ios/web/navigation/crw_session_entry.h" | 10 #import "ios/web/navigation/crw_session_entry.h" |
11 #include "ios/web/navigation/navigation_item_impl.h" | 11 #include "ios/web/navigation/navigation_item_impl.h" |
12 #include "ios/web/public/referrer.h" | 12 #include "ios/web/public/referrer.h" |
13 #import "net/base/mac/url_conversions.h" | 13 #import "net/base/mac/url_conversions.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "testing/gtest_mac.h" | 15 #include "testing/gtest_mac.h" |
16 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
17 #import "third_party/ocmock/OCMock/OCMock.h" | 17 #import "third_party/ocmock/OCMock/OCMock.h" |
18 #include "third_party/ocmock/gtest_support.h" | 18 #include "third_party/ocmock/gtest_support.h" |
19 #include "ui/base/page_transition_types.h" | 19 #include "ui/base/page_transition_types.h" |
20 | 20 |
21 @interface CRWSessionEntry (ExposedForTesting) | 21 @interface CRWSessionEntry (ExposedForTesting) |
22 + (web::PageScrollState)scrollStateFromDictionary:(NSDictionary*)dictionary; | 22 + (web::PageScrollState)scrollStateFromDictionary:(NSDictionary*)dictionary; |
23 + (NSDictionary*)dictionaryFromScrollState: | 23 + (NSDictionary*)dictionaryFromScrollState: |
24 (const web::PageScrollState&)scrollState; | 24 (const web::PageScrollState&)scrollState; |
25 @end | 25 @end |
26 | 26 |
27 static NSString* const kHTTPHeaderKey1 = @"key1"; | |
28 static NSString* const kHTTPHeaderKey2 = @"key2"; | |
29 static NSString* const kHTTPHeaderValue1 = @"value1"; | |
30 static NSString* const kHTTPHeaderValue2 = @"value2"; | |
31 | |
32 class CRWSessionEntryTest : public PlatformTest { | 27 class CRWSessionEntryTest : public PlatformTest { |
33 public: | 28 public: |
34 static void expectEqualSessionEntries(CRWSessionEntry* entry1, | 29 static void expectEqualSessionEntries(CRWSessionEntry* entry1, |
35 CRWSessionEntry* entry2, | 30 CRWSessionEntry* entry2, |
36 ui::PageTransition transition); | 31 ui::PageTransition transition); |
37 | 32 |
38 protected: | 33 protected: |
39 void SetUp() override { | 34 void SetUp() override { |
40 GURL url("http://init.test"); | 35 GURL url("http://init.test"); |
41 ui::PageTransition transition = | 36 ui::PageTransition transition = |
42 ui::PAGE_TRANSITION_AUTO_BOOKMARK; | 37 ui::PAGE_TRANSITION_AUTO_BOOKMARK; |
43 sessionEntry_.reset([[CRWSessionEntry alloc] initWithUrl:url | 38 scoped_ptr<web::NavigationItemImpl> item(new web::NavigationItemImpl()); |
44 referrer:web::Referrer() | 39 item->SetURL(url); |
45 transition:transition | 40 item->SetTransitionType(transition); |
46 useDesktopUserAgent:NO | 41 item->SetTimestamp(base::Time::Now()); |
47 rendererInitiated:NO]); | 42 item->SetPostData([@"Test data" dataUsingEncoding:NSUTF8StringEncoding]); |
48 [sessionEntry_ navigationItem]->SetTimestamp(base::Time::Now()); | 43 sessionEntry_.reset( |
49 [sessionEntry_ addHTTPHeaders:@{ kHTTPHeaderKey1 : kHTTPHeaderValue1 }]; | 44 [[CRWSessionEntry alloc] initWithNavigationItem:item.Pass()]); |
50 [sessionEntry_ | |
51 setPOSTData:[@"Test data" dataUsingEncoding:NSUTF8StringEncoding]]; | |
52 } | 45 } |
53 void TearDown() override { sessionEntry_.reset(); } | 46 void TearDown() override { sessionEntry_.reset(); } |
54 | 47 |
55 protected: | 48 protected: |
56 base::scoped_nsobject<CRWSessionEntry> sessionEntry_; | 49 base::scoped_nsobject<CRWSessionEntry> sessionEntry_; |
57 }; | 50 }; |
58 | 51 |
59 @implementation OCMockComplexTypeHelper (CRWSessionEntryTest) | 52 @implementation OCMockComplexTypeHelper (CRWSessionEntryTest) |
60 typedef void (^encodeBytes_length_forKey_block)( | 53 typedef void (^encodeBytes_length_forKey_block)( |
61 const uint8_t*, NSUInteger, NSString*); | 54 const uint8_t*, NSUInteger, NSString*); |
62 - (void)encodeBytes:(const uint8_t*)bytes | 55 - (void)encodeBytes:(const uint8_t*)bytes |
63 length:(NSUInteger)length | 56 length:(NSUInteger)length |
64 forKey:(NSString*)key { | 57 forKey:(NSString*)key { |
65 static_cast<encodeBytes_length_forKey_block>([self blockForSelector:_cmd])( | 58 static_cast<encodeBytes_length_forKey_block>([self blockForSelector:_cmd])( |
66 bytes, length, key); | 59 bytes, length, key); |
67 } | 60 } |
68 | 61 |
69 typedef const uint8_t* (^decodeBytesForKeyBlock)(NSString*, NSUInteger*); | 62 typedef const uint8_t* (^decodeBytesForKeyBlock)(NSString*, NSUInteger*); |
70 - (const uint8_t*)decodeBytesForKey:(NSString*)key | 63 - (const uint8_t*)decodeBytesForKey:(NSString*)key |
71 returnedLength:(NSUInteger*)lengthp { | 64 returnedLength:(NSUInteger*)lengthp { |
72 return static_cast<decodeBytesForKeyBlock>([self blockForSelector:_cmd])( | 65 return static_cast<decodeBytesForKeyBlock>([self blockForSelector:_cmd])( |
73 key, lengthp); | 66 key, lengthp); |
74 } | 67 } |
75 @end | 68 @end |
76 | 69 |
77 void CRWSessionEntryTest::expectEqualSessionEntries( | 70 void CRWSessionEntryTest::expectEqualSessionEntries( |
78 CRWSessionEntry* entry1, | 71 CRWSessionEntry* entry1, |
79 CRWSessionEntry* entry2, | 72 CRWSessionEntry* entry2, |
80 ui::PageTransition transition) { | 73 ui::PageTransition transition) { |
81 EXPECT_EQ(entry1.index, entry2.index); | 74 web::NavigationItemImpl* navItem1 = entry1.navigationItemImpl; |
82 web::NavigationItem* navItem1 = entry1.navigationItem; | 75 web::NavigationItemImpl* navItem2 = entry2.navigationItemImpl; |
83 web::NavigationItem* navItem2 = entry2.navigationItem; | |
84 // url is not compared because it could differ after copy or archive. | 76 // url is not compared because it could differ after copy or archive. |
85 EXPECT_EQ(navItem1->GetVirtualURL(), navItem2->GetVirtualURL()); | 77 EXPECT_EQ(navItem1->GetVirtualURL(), navItem2->GetVirtualURL()); |
86 EXPECT_EQ(navItem1->GetReferrer().url, navItem2->GetReferrer().url); | 78 EXPECT_EQ(navItem1->GetReferrer().url, navItem2->GetReferrer().url); |
87 EXPECT_EQ(navItem1->GetTimestamp(), navItem2->GetTimestamp()); | 79 EXPECT_EQ(navItem1->GetTimestamp(), navItem2->GetTimestamp()); |
88 EXPECT_EQ(navItem1->GetTitle(), navItem2->GetTitle()); | 80 EXPECT_EQ(navItem1->GetTitle(), navItem2->GetTitle()); |
89 EXPECT_EQ(navItem1->GetPageScrollState(), navItem2->GetPageScrollState()); | 81 EXPECT_EQ(navItem1->GetPageScrollState(), navItem2->GetPageScrollState()); |
90 EXPECT_EQ(entry1.useDesktopUserAgent, entry2.useDesktopUserAgent); | 82 EXPECT_EQ(navItem1->ShouldSkipResubmitDataConfirmation(), |
91 EXPECT_EQ(entry1.usedDataReductionProxy, entry2.usedDataReductionProxy); | 83 navItem2->ShouldSkipResubmitDataConfirmation()); |
| 84 EXPECT_EQ(navItem1->IsOverridingUserAgent(), |
| 85 navItem2->IsOverridingUserAgent()); |
| 86 EXPECT_TRUE((!navItem1->HasPostData() && !navItem2->HasPostData()) || |
| 87 [navItem1->GetPostData() isEqualToData:navItem2->GetPostData()]); |
92 EXPECT_EQ(navItem2->GetTransitionType(), transition); | 88 EXPECT_EQ(navItem2->GetTransitionType(), transition); |
93 EXPECT_NSEQ(entry1.httpHeaders, entry2.httpHeaders); | 89 EXPECT_NSEQ(navItem1->GetHttpRequestHeaders(), |
94 EXPECT_TRUE((!entry1.POSTData && !entry2.POSTData) || | 90 navItem2->GetHttpRequestHeaders()); |
95 [entry1.POSTData isEqualToData:entry2.POSTData]); | |
96 EXPECT_EQ(entry1.skipResubmitDataConfirmation, | |
97 entry2.skipResubmitDataConfirmation); | |
98 } | 91 } |
99 | 92 |
100 TEST_F(CRWSessionEntryTest, Description) { | 93 TEST_F(CRWSessionEntryTest, Description) { |
101 [sessionEntry_ navigationItem]->SetTitle(base::SysNSStringToUTF16(@"Title")); | 94 [sessionEntry_ navigationItem]->SetTitle(base::SysNSStringToUTF16(@"Title")); |
102 EXPECT_NSEQ([sessionEntry_ description], @"url:http://init.test/ " | 95 EXPECT_NSEQ([sessionEntry_ description], |
103 @"originalurl:http://init.test/ " @"title:Title " @"transition:2 " | 96 @"url:http://init.test/ originalurl:http://init.test/ " |
104 @"scrollState:{ scrollOffset:(nan, nan), zoomScaleRange:(nan, " | 97 @"title:Title transition:2 scrollState:{ scrollOffset:(nan, " |
105 @"nan), zoomScale:nan } " @"desktopUA:0 " @"proxy:0"); | 98 @"nan), zoomScaleRange:(nan, nan), zoomScale:nan } desktopUA:0"); |
106 } | 99 } |
107 | 100 |
108 TEST_F(CRWSessionEntryTest, InitWithCoder) { | 101 TEST_F(CRWSessionEntryTest, InitWithCoder) { |
109 web::NavigationItem* item = [sessionEntry_ navigationItem]; | 102 web::NavigationItem* item = [sessionEntry_ navigationItem]; |
110 item->SetVirtualURL(GURL("http://user.friendly")); | 103 item->SetVirtualURL(GURL("http://user.friendly")); |
111 item->SetTitle(base::SysNSStringToUTF16(@"Title")); | 104 item->SetTitle(base::SysNSStringToUTF16(@"Title")); |
112 int index = sessionEntry_.get().index; | |
113 // Old serialized entries have no timestamp. | 105 // Old serialized entries have no timestamp. |
114 item->SetTimestamp(base::Time::FromInternalValue(0)); | 106 item->SetTimestamp(base::Time::FromInternalValue(0)); |
115 | 107 |
116 NSURL* virtualUrl = net::NSURLWithGURL(item->GetVirtualURL()); | 108 NSURL* virtualUrl = net::NSURLWithGURL(item->GetVirtualURL()); |
117 NSURL* referrer = net::NSURLWithGURL(item->GetReferrer().url); | 109 NSURL* referrer = net::NSURLWithGURL(item->GetReferrer().url); |
118 NSString* title = base::SysUTF16ToNSString(item->GetTitle()); | 110 NSString* title = base::SysUTF16ToNSString(item->GetTitle()); |
119 base::scoped_nsobject<id> decoder([[OCMockComplexTypeHelper alloc] | 111 base::scoped_nsobject<id> decoder([[OCMockComplexTypeHelper alloc] |
120 initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]); | 112 initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]); |
121 | 113 |
122 decodeBytesForKeyBlock block = ^ const uint8_t* (NSString* key, | 114 decodeBytesForKeyBlock block = ^ const uint8_t* (NSString* key, |
123 NSUInteger* length) { | 115 NSUInteger* length) { |
124 *length = 0; | 116 *length = 0; |
125 return NULL; | 117 return NULL; |
126 }; | 118 }; |
127 | 119 |
128 [[[decoder stub] andReturnValue:[NSNumber numberWithBool:NO]] | 120 [[[decoder stub] andReturnValue:[NSNumber numberWithBool:NO]] |
129 containsValueForKey:[OCMArg any]]; | 121 containsValueForKey:[OCMArg any]]; |
130 | 122 |
131 [decoder onSelector:@selector(decodeBytesForKey:returnedLength:) | 123 [decoder onSelector:@selector(decodeBytesForKey:returnedLength:) |
132 callBlockExpectation:block]; | 124 callBlockExpectation:block]; |
133 [[[decoder expect] andReturnValue:OCMOCK_VALUE(index)] | |
134 decodeIntForKey:@"index"]; | |
135 [[[decoder expect] andReturn:virtualUrl] | 125 [[[decoder expect] andReturn:virtualUrl] |
136 decodeObjectForKey:@"virtualUrl"]; | 126 decodeObjectForKey:@"virtualUrl"]; |
137 [[[decoder expect] andReturn:referrer] | 127 [[[decoder expect] andReturn:referrer] |
138 decodeObjectForKey:@"referrer"]; | 128 decodeObjectForKey:@"referrer"]; |
139 [[[decoder expect] andReturn:title] | 129 [[[decoder expect] andReturn:title] |
140 decodeObjectForKey:@"title"]; | 130 decodeObjectForKey:@"title"]; |
141 const web::PageScrollState& scrollState = | 131 const web::PageScrollState& scrollState = |
142 [sessionEntry_ navigationItem]->GetPageScrollState(); | 132 [sessionEntry_ navigationItem]->GetPageScrollState(); |
143 NSDictionary* serializedScrollState = | 133 NSDictionary* serializedScrollState = |
144 [CRWSessionEntry dictionaryFromScrollState:scrollState]; | 134 [CRWSessionEntry dictionaryFromScrollState:scrollState]; |
145 [[[decoder expect] andReturn:serializedScrollState] | 135 [[[decoder expect] andReturn:serializedScrollState] |
146 decodeObjectForKey:@"state"]; | 136 decodeObjectForKey:@"state"]; |
147 BOOL useDesktopUserAgent = sessionEntry_.get().useDesktopUserAgent; | 137 BOOL useDesktopUserAgent = |
| 138 [sessionEntry_ navigationItem]->IsOverridingUserAgent(); |
148 [[[decoder expect] andReturnValue:OCMOCK_VALUE(useDesktopUserAgent)] | 139 [[[decoder expect] andReturnValue:OCMOCK_VALUE(useDesktopUserAgent)] |
149 decodeBoolForKey:@"useDesktopUserAgent"]; | 140 decodeBoolForKey:@"useDesktopUserAgent"]; |
150 BOOL usedDataReductionProxy = sessionEntry_.get().usedDataReductionProxy; | 141 NSDictionary* requestHeaders = |
151 [[[decoder expect] andReturnValue:OCMOCK_VALUE(usedDataReductionProxy)] | 142 [sessionEntry_ navigationItem]->GetHttpRequestHeaders(); |
152 decodeBoolForKey:@"usedDataReductionProxy"]; | 143 [[[decoder expect] andReturn:requestHeaders] |
153 [[[decoder expect] andReturn:sessionEntry_.get().httpHeaders] | |
154 decodeObjectForKey:@"httpHeaders"]; | 144 decodeObjectForKey:@"httpHeaders"]; |
155 [[[decoder expect] andReturn:sessionEntry_.get().POSTData] | 145 [[[decoder expect] |
| 146 andReturn:[sessionEntry_ navigationItemImpl]->GetPostData()] |
156 decodeObjectForKey:@"POSTData"]; | 147 decodeObjectForKey:@"POSTData"]; |
157 BOOL skipResubmitDataConfirmation = | 148 BOOL skipResubmitDataConfirmation = |
158 sessionEntry_.get().skipResubmitDataConfirmation; | 149 [sessionEntry_ navigationItemImpl]->ShouldSkipResubmitDataConfirmation(); |
159 [[[decoder expect] andReturnValue:OCMOCK_VALUE(skipResubmitDataConfirmation)] | 150 [[[decoder expect] andReturnValue:OCMOCK_VALUE(skipResubmitDataConfirmation)] |
160 decodeBoolForKey:@"skipResubmitDataConfirmation"]; | 151 decodeBoolForKey:@"skipResubmitDataConfirmation"]; |
161 | 152 |
162 base::scoped_nsobject<CRWSessionEntry> newSessionEntry( | 153 base::scoped_nsobject<CRWSessionEntry> newSessionEntry( |
163 [[CRWSessionEntry alloc] initWithCoder:decoder]); | 154 [[CRWSessionEntry alloc] initWithCoder:decoder]); |
164 web::NavigationItem* newItem = [newSessionEntry navigationItem]; | 155 web::NavigationItem* newItem = [newSessionEntry navigationItem]; |
165 | 156 |
166 EXPECT_OCMOCK_VERIFY(decoder); | 157 EXPECT_OCMOCK_VERIFY(decoder); |
167 expectEqualSessionEntries(sessionEntry_, newSessionEntry, | 158 expectEqualSessionEntries(sessionEntry_, newSessionEntry, |
168 ui::PAGE_TRANSITION_RELOAD); | 159 ui::PAGE_TRANSITION_RELOAD); |
169 EXPECT_NE(item->GetURL(), newItem->GetURL()); | 160 EXPECT_NE(item->GetURL(), newItem->GetURL()); |
170 EXPECT_EQ(item->GetVirtualURL(), newItem->GetURL()); | 161 EXPECT_EQ(item->GetVirtualURL(), newItem->GetURL()); |
171 } | 162 } |
172 | 163 |
173 TEST_F(CRWSessionEntryTest, InitWithCoderNewStyle) { | 164 TEST_F(CRWSessionEntryTest, InitWithCoderNewStyle) { |
174 web::NavigationItem* item = [sessionEntry_ navigationItem]; | 165 web::NavigationItem* item = [sessionEntry_ navigationItem]; |
175 item->SetVirtualURL(GURL("http://user.friendly")); | 166 item->SetVirtualURL(GURL("http://user.friendly")); |
176 item->SetTitle(base::SysNSStringToUTF16(@"Title")); | 167 item->SetTitle(base::SysNSStringToUTF16(@"Title")); |
177 int index = sessionEntry_.get().index; | |
178 int64 timestamp = item->GetTimestamp().ToInternalValue(); | 168 int64 timestamp = item->GetTimestamp().ToInternalValue(); |
179 | 169 |
180 std::string virtualUrl = item->GetVirtualURL().spec(); | 170 std::string virtualUrl = item->GetVirtualURL().spec(); |
181 std::string referrerUrl = item->GetReferrer().url.spec(); | 171 std::string referrerUrl = item->GetReferrer().url.spec(); |
182 NSString* title = base::SysUTF16ToNSString(item->GetTitle()); | 172 NSString* title = base::SysUTF16ToNSString(item->GetTitle()); |
183 base::scoped_nsobject<id> decoder([[OCMockComplexTypeHelper alloc] | 173 base::scoped_nsobject<id> decoder([[OCMockComplexTypeHelper alloc] |
184 initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]); | 174 initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]); |
185 | 175 |
186 const std::string emptyString; | 176 const std::string emptyString; |
187 decodeBytesForKeyBlock block = ^ const uint8_t* (NSString* key, | 177 decodeBytesForKeyBlock block = ^ const uint8_t* (NSString* key, |
188 NSUInteger* length) { | 178 NSUInteger* length) { |
189 const std::string *value = &emptyString; | 179 const std::string *value = &emptyString; |
190 if ([key isEqualToString:@"virtualUrlString"]) | 180 if ([key isEqualToString:@"virtualUrlString"]) |
191 value = &virtualUrl; | 181 value = &virtualUrl; |
192 else if ([key isEqualToString:@"referrerUrlString"]) | 182 else if ([key isEqualToString:@"referrerUrlString"]) |
193 value = &referrerUrl; | 183 value = &referrerUrl; |
194 else | 184 else |
195 EXPECT_TRUE(false); | 185 EXPECT_TRUE(false); |
196 | 186 |
197 *length = value->size(); | 187 *length = value->size(); |
198 return reinterpret_cast<const uint8_t*>(value->data()); | 188 return reinterpret_cast<const uint8_t*>(value->data()); |
199 }; | 189 }; |
200 | 190 |
201 [decoder onSelector:@selector(decodeBytesForKey:returnedLength:) | 191 [decoder onSelector:@selector(decodeBytesForKey:returnedLength:) |
202 callBlockExpectation:block]; | 192 callBlockExpectation:block]; |
203 [[[decoder stub] andReturnValue:[NSNumber numberWithBool:YES]] | 193 [[[decoder stub] andReturnValue:[NSNumber numberWithBool:YES]] |
204 containsValueForKey:[OCMArg any]]; | 194 containsValueForKey:[OCMArg any]]; |
205 [[[decoder expect] andReturnValue:OCMOCK_VALUE(index)] | |
206 decodeIntForKey:@"index"]; | |
207 web::ReferrerPolicy expectedPolicy = item->GetReferrer().policy; | 195 web::ReferrerPolicy expectedPolicy = item->GetReferrer().policy; |
208 [[[decoder expect] | 196 [[[decoder expect] |
209 andReturnValue:OCMOCK_VALUE(expectedPolicy)] | 197 andReturnValue:OCMOCK_VALUE(expectedPolicy)] |
210 decodeIntForKey:@"referrerPolicy"]; | 198 decodeIntForKey:@"referrerPolicy"]; |
211 [[[decoder expect] andReturnValue:OCMOCK_VALUE(timestamp)] | 199 [[[decoder expect] andReturnValue:OCMOCK_VALUE(timestamp)] |
212 decodeInt64ForKey:@"timestamp"]; | 200 decodeInt64ForKey:@"timestamp"]; |
213 [[[decoder expect] andReturn:title] | 201 [[[decoder expect] andReturn:title] |
214 decodeObjectForKey:@"title"]; | 202 decodeObjectForKey:@"title"]; |
215 const web::PageScrollState& scrollState = | 203 const web::PageScrollState& scrollState = |
216 [sessionEntry_ navigationItem]->GetPageScrollState(); | 204 [sessionEntry_ navigationItem]->GetPageScrollState(); |
217 NSDictionary* serializedScrollState = | 205 NSDictionary* serializedScrollState = |
218 [CRWSessionEntry dictionaryFromScrollState:scrollState]; | 206 [CRWSessionEntry dictionaryFromScrollState:scrollState]; |
219 [[[decoder expect] andReturn:serializedScrollState] | 207 [[[decoder expect] andReturn:serializedScrollState] |
220 decodeObjectForKey:@"state"]; | 208 decodeObjectForKey:@"state"]; |
221 BOOL useDesktopUserAgent = sessionEntry_.get().useDesktopUserAgent; | 209 BOOL useDesktopUserAgent = |
| 210 [sessionEntry_ navigationItem]->IsOverridingUserAgent(); |
222 [[[decoder expect] andReturnValue:OCMOCK_VALUE(useDesktopUserAgent)] | 211 [[[decoder expect] andReturnValue:OCMOCK_VALUE(useDesktopUserAgent)] |
223 decodeBoolForKey:@"useDesktopUserAgent"]; | 212 decodeBoolForKey:@"useDesktopUserAgent"]; |
224 BOOL usedDataReductionProxy = sessionEntry_.get().usedDataReductionProxy; | 213 NSDictionary* requestHeaders = |
225 [[[decoder expect] andReturnValue:OCMOCK_VALUE(usedDataReductionProxy)] | 214 [sessionEntry_ navigationItem]->GetHttpRequestHeaders(); |
226 decodeBoolForKey:@"usedDataReductionProxy"]; | 215 [[[decoder expect] andReturn:requestHeaders] |
227 [[[decoder expect] andReturn:sessionEntry_.get().httpHeaders] | |
228 decodeObjectForKey:@"httpHeaders"]; | 216 decodeObjectForKey:@"httpHeaders"]; |
229 [[[decoder expect] andReturn:sessionEntry_.get().POSTData] | 217 NSData* POSTData = [sessionEntry_ navigationItemImpl]->GetPostData(); |
230 decodeObjectForKey:@"POSTData"]; | 218 [[[decoder expect] andReturn:POSTData] decodeObjectForKey:@"POSTData"]; |
231 BOOL skipResubmitDataConfirmation = | 219 BOOL skipResubmitDataConfirmation = |
232 sessionEntry_.get().skipResubmitDataConfirmation; | 220 [sessionEntry_ navigationItemImpl]->ShouldSkipResubmitDataConfirmation(); |
233 [[[decoder expect] andReturnValue:OCMOCK_VALUE(skipResubmitDataConfirmation)] | 221 [[[decoder expect] andReturnValue:OCMOCK_VALUE(skipResubmitDataConfirmation)] |
234 decodeBoolForKey:@"skipResubmitDataConfirmation"]; | 222 decodeBoolForKey:@"skipResubmitDataConfirmation"]; |
235 | 223 |
236 base::scoped_nsobject<CRWSessionEntry> newSessionEntry( | 224 base::scoped_nsobject<CRWSessionEntry> newSessionEntry( |
237 [[CRWSessionEntry alloc] initWithCoder:decoder]); | 225 [[CRWSessionEntry alloc] initWithCoder:decoder]); |
238 web::NavigationItem* newItem = [newSessionEntry navigationItem]; | 226 web::NavigationItem* newItem = [newSessionEntry navigationItem]; |
239 | 227 |
240 EXPECT_OCMOCK_VERIFY(decoder); | 228 EXPECT_OCMOCK_VERIFY(decoder); |
241 expectEqualSessionEntries(sessionEntry_, newSessionEntry, | 229 expectEqualSessionEntries(sessionEntry_, newSessionEntry, |
242 ui::PAGE_TRANSITION_RELOAD); | 230 ui::PAGE_TRANSITION_RELOAD); |
(...skipping 26 matching lines...) Expand all Loading... |
269 return; | 257 return; |
270 } else if ([key isEqualToString:@"referrerUrlString"]) { | 258 } else if ([key isEqualToString:@"referrerUrlString"]) { |
271 ASSERT_EQ(item->GetReferrer().url.spec(), | 259 ASSERT_EQ(item->GetReferrer().url.spec(), |
272 std::string(reinterpret_cast<const char*>(bytes), length)); | 260 std::string(reinterpret_cast<const char*>(bytes), length)); |
273 return; | 261 return; |
274 } | 262 } |
275 FAIL(); | 263 FAIL(); |
276 }; | 264 }; |
277 [coder onSelector:@selector(encodeBytes:length:forKey:) | 265 [coder onSelector:@selector(encodeBytes:length:forKey:) |
278 callBlockExpectation:block]; | 266 callBlockExpectation:block]; |
279 [[coder expect] encodeInt:[sessionEntry_ index] forKey:@"index"]; | |
280 [[coder expect] encodeInt:item->GetReferrer().policy | 267 [[coder expect] encodeInt:item->GetReferrer().policy |
281 forKey:@"referrerPolicy"]; | 268 forKey:@"referrerPolicy"]; |
282 [[coder expect] encodeInt64:item->GetTimestamp().ToInternalValue() | 269 [[coder expect] encodeInt64:item->GetTimestamp().ToInternalValue() |
283 forKey:@"timestamp"]; | 270 forKey:@"timestamp"]; |
284 [[coder expect] encodeObject:title forKey:@"title"]; | 271 [[coder expect] encodeObject:title forKey:@"title"]; |
285 const web::PageScrollState& scrollState = | 272 const web::PageScrollState& scrollState = |
286 [sessionEntry_ navigationItem]->GetPageScrollState(); | 273 [sessionEntry_ navigationItem]->GetPageScrollState(); |
287 NSDictionary* serializedScrollState = | 274 NSDictionary* serializedScrollState = |
288 [CRWSessionEntry dictionaryFromScrollState:scrollState]; | 275 [CRWSessionEntry dictionaryFromScrollState:scrollState]; |
289 [[coder expect] encodeObject:serializedScrollState forKey:@"state"]; | 276 [[coder expect] encodeObject:serializedScrollState forKey:@"state"]; |
290 [[coder expect] encodeBool:[sessionEntry_ useDesktopUserAgent] | 277 BOOL useDesktopUserAgent = |
291 forKey:@"useDesktopUserAgent"]; | 278 [sessionEntry_ navigationItem]->IsOverridingUserAgent(); |
292 [[coder expect] encodeBool:[sessionEntry_ usedDataReductionProxy] | 279 [[coder expect] encodeBool:useDesktopUserAgent forKey:@"useDesktopUserAgent"]; |
293 forKey:@"usedDataReductionProxy"]; | 280 NSDictionary* requestHeaders = |
294 [[coder expect] encodeObject:[sessionEntry_ httpHeaders] | 281 [sessionEntry_ navigationItem]->GetHttpRequestHeaders(); |
| 282 [[coder expect] encodeObject:requestHeaders |
295 forKey:@"httpHeaders"]; | 283 forKey:@"httpHeaders"]; |
296 [[coder expect] encodeObject:[sessionEntry_ POSTData] forKey:@"POSTData"]; | 284 [[coder expect] encodeObject:[sessionEntry_ navigationItemImpl]->GetPostData() |
297 [[coder expect] encodeBool:[sessionEntry_ skipResubmitDataConfirmation] | 285 forKey:@"POSTData"]; |
| 286 BOOL skipResubmitDataConfirmation = |
| 287 [sessionEntry_ navigationItemImpl]->ShouldSkipResubmitDataConfirmation(); |
| 288 [[coder expect] encodeBool:skipResubmitDataConfirmation |
298 forKey:@"skipResubmitDataConfirmation"]; | 289 forKey:@"skipResubmitDataConfirmation"]; |
299 [sessionEntry_ encodeWithCoder:coder]; | 290 [sessionEntry_ encodeWithCoder:coder]; |
300 EXPECT_OCMOCK_VERIFY(coder); | 291 EXPECT_OCMOCK_VERIFY(coder); |
301 } | 292 } |
302 | 293 |
303 TEST_F(CRWSessionEntryTest, CodingEncoding) { | 294 TEST_F(CRWSessionEntryTest, CodingEncoding) { |
304 web::NavigationItem* item = [sessionEntry_ navigationItem]; | 295 web::NavigationItem* item = [sessionEntry_ navigationItem]; |
305 item->SetVirtualURL(GURL("http://user.friendly")); | 296 item->SetVirtualURL(GURL("http://user.friendly")); |
306 NSData* data = [NSKeyedArchiver archivedDataWithRootObject:sessionEntry_]; | 297 NSData* data = [NSKeyedArchiver archivedDataWithRootObject:sessionEntry_]; |
307 EXPECT_TRUE(data != nil); | 298 EXPECT_TRUE(data != nil); |
(...skipping 23 matching lines...) Expand all Loading... |
331 TEST_F(CRWSessionEntryTest, NonEmptyVirtualUrl) { | 322 TEST_F(CRWSessionEntryTest, NonEmptyVirtualUrl) { |
332 web::NavigationItem* item = [sessionEntry_ navigationItem]; | 323 web::NavigationItem* item = [sessionEntry_ navigationItem]; |
333 item->SetVirtualURL(GURL("http://user.friendly")); | 324 item->SetVirtualURL(GURL("http://user.friendly")); |
334 EXPECT_EQ(GURL("http://user.friendly/"), item->GetVirtualURL()); | 325 EXPECT_EQ(GURL("http://user.friendly/"), item->GetVirtualURL()); |
335 EXPECT_EQ(GURL("http://init.test/"), item->GetURL()); | 326 EXPECT_EQ(GURL("http://init.test/"), item->GetURL()); |
336 } | 327 } |
337 | 328 |
338 TEST_F(CRWSessionEntryTest, EmptyDescription) { | 329 TEST_F(CRWSessionEntryTest, EmptyDescription) { |
339 EXPECT_GT([[sessionEntry_ description] length], 0U); | 330 EXPECT_GT([[sessionEntry_ description] length], 0U); |
340 } | 331 } |
341 | |
342 TEST_F(CRWSessionEntryTest, CreateWithNavigationItem) { | |
343 int index = 5; // Just pick something non-zero. | |
344 GURL url("http://www.virtualurl.com"); | |
345 web::Referrer referrer(GURL("http://www.referrer.com"), | |
346 web::ReferrerPolicyDefault); | |
347 base::string16 title = base::SysNSStringToUTF16(@"Title"); | |
348 std::string state; | |
349 ui::PageTransition transition = ui::PAGE_TRANSITION_GENERATED; | |
350 | |
351 scoped_ptr<web::NavigationItem> navigation_item( | |
352 new web::NavigationItemImpl()); | |
353 navigation_item->SetURL(url); | |
354 navigation_item->SetReferrer(referrer); | |
355 navigation_item->SetTitle(title); | |
356 navigation_item->SetTransitionType(transition); | |
357 | |
358 base::scoped_nsobject<CRWSessionEntry> sessionEntry( | |
359 [[CRWSessionEntry alloc] initWithNavigationItem:navigation_item.Pass() | |
360 index:index]); | |
361 web::NavigationItem* item = [sessionEntry navigationItem]; | |
362 // Validate everything was set correctly. | |
363 EXPECT_EQ(sessionEntry.get().index, index); | |
364 // Desktop only persists the virtual url, all three fields are initialized | |
365 // by it. | |
366 EXPECT_EQ(item->GetURL(), url); | |
367 EXPECT_EQ(item->GetVirtualURL(), url); | |
368 EXPECT_EQ(sessionEntry.get().originalUrl, url); | |
369 EXPECT_EQ(item->GetReferrer().url, referrer.url); | |
370 EXPECT_EQ(item->GetTitle(), title); | |
371 EXPECT_EQ(item->GetTransitionType(), transition); | |
372 } | |
373 | |
374 TEST_F(CRWSessionEntryTest, AddHTTPHeaders) { | |
375 EXPECT_NSEQ(@{ kHTTPHeaderKey1 : kHTTPHeaderValue1 }, | |
376 [sessionEntry_ httpHeaders]); | |
377 | |
378 [sessionEntry_ addHTTPHeaders:@{ kHTTPHeaderKey1 : kHTTPHeaderValue2 }]; | |
379 EXPECT_NSEQ(@{ kHTTPHeaderKey1 : kHTTPHeaderValue2 }, | |
380 [sessionEntry_ httpHeaders]); | |
381 | |
382 [sessionEntry_ addHTTPHeaders:@{ kHTTPHeaderKey2 : kHTTPHeaderValue1 }]; | |
383 NSDictionary* expected = @{ kHTTPHeaderKey1 : kHTTPHeaderValue2, | |
384 kHTTPHeaderKey2 : kHTTPHeaderValue1 }; | |
385 EXPECT_NSEQ(expected, [sessionEntry_ httpHeaders]); | |
386 } | |
387 | |
388 TEST_F(CRWSessionEntryTest, RemoveHTTPHeaderForKey) { | |
389 NSDictionary* httpHeaders = @{ kHTTPHeaderKey1 : kHTTPHeaderValue1, | |
390 kHTTPHeaderKey2 : kHTTPHeaderValue2 }; | |
391 [sessionEntry_ addHTTPHeaders:httpHeaders]; | |
392 EXPECT_NSEQ(httpHeaders, [sessionEntry_ httpHeaders]); | |
393 | |
394 [sessionEntry_ removeHTTPHeaderForKey:kHTTPHeaderKey1]; | |
395 EXPECT_NSEQ(@{ kHTTPHeaderKey2 : kHTTPHeaderValue2 }, | |
396 [sessionEntry_ httpHeaders]); | |
397 | |
398 [sessionEntry_ removeHTTPHeaderForKey:kHTTPHeaderKey2]; | |
399 EXPECT_TRUE([sessionEntry_ httpHeaders] == nil); | |
400 } | |
OLD | NEW |