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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
// Copyright 2012 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.
#import <Foundation/Foundation.h>
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/testing/ocmock_complex_type_helper.h"
#import "ios/web/navigation/crw_session_entry.h"
#include "ios/web/navigation/navigation_item_impl.h"
#include "ios/web/public/referrer.h"
#import "net/base/mac/url_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
#include "ui/base/page_transition_types.h"
@interface CRWSessionEntry (ExposedForTesting)
+ (web::PageScrollState)pageStateFromDictionary:(NSDictionary*)dictionary;
+ (NSDictionary*)dictionaryFromPageDisplayState:
(const web::PageDisplayState&)displayState;
@end
class CRWSessionEntryTest : public PlatformTest {
public:
static void expectEqualSessionEntries(CRWSessionEntry* entry1,
CRWSessionEntry* entry2,
ui::PageTransition transition);
protected:
void SetUp() override {
GURL url("http://init.test");
ui::PageTransition transition =
ui::PAGE_TRANSITION_AUTO_BOOKMARK;
scoped_ptr<web::NavigationItemImpl> item(new web::NavigationItemImpl());
item->SetURL(url);
item->SetTransitionType(transition);
item->SetTimestamp(base::Time::Now());
item->SetPostData([@"Test data" dataUsingEncoding:NSUTF8StringEncoding]);
sessionEntry_.reset(
[[CRWSessionEntry alloc] initWithNavigationItem:item.Pass()]);
}
void TearDown() override { sessionEntry_.reset(); }
protected:
base::scoped_nsobject<CRWSessionEntry> sessionEntry_;
};
@implementation OCMockComplexTypeHelper (CRWSessionEntryTest)
typedef void (^encodeBytes_length_forKey_block)(
const uint8_t*, NSUInteger, NSString*);
- (void)encodeBytes:(const uint8_t*)bytes
length:(NSUInteger)length
forKey:(NSString*)key {
static_cast<encodeBytes_length_forKey_block>([self blockForSelector:_cmd])(
bytes, length, key);
}
typedef const uint8_t* (^decodeBytesForKeyBlock)(NSString*, NSUInteger*);
- (const uint8_t*)decodeBytesForKey:(NSString*)key
returnedLength:(NSUInteger*)lengthp {
return static_cast<decodeBytesForKeyBlock>([self blockForSelector:_cmd])(
key, lengthp);
}
@end
void CRWSessionEntryTest::expectEqualSessionEntries(
CRWSessionEntry* entry1,
CRWSessionEntry* entry2,
ui::PageTransition transition) {
web::NavigationItemImpl* navItem1 = entry1.navigationItemImpl;
web::NavigationItemImpl* navItem2 = entry2.navigationItemImpl;
// url is not compared because it could differ after copy or archive.
EXPECT_EQ(navItem1->GetVirtualURL(), navItem2->GetVirtualURL());
EXPECT_EQ(navItem1->GetReferrer().url, navItem2->GetReferrer().url);
EXPECT_EQ(navItem1->GetTimestamp(), navItem2->GetTimestamp());
EXPECT_EQ(navItem1->GetTitle(), navItem2->GetTitle());
EXPECT_EQ(navItem1->GetPageDisplayState(), navItem2->GetPageDisplayState());
EXPECT_EQ(navItem1->ShouldSkipResubmitDataConfirmation(),
navItem2->ShouldSkipResubmitDataConfirmation());
EXPECT_EQ(navItem1->IsOverridingUserAgent(),
navItem2->IsOverridingUserAgent());
EXPECT_TRUE((!navItem1->HasPostData() && !navItem2->HasPostData()) ||
[navItem1->GetPostData() isEqualToData:navItem2->GetPostData()]);
EXPECT_EQ(navItem2->GetTransitionType(), transition);
EXPECT_NSEQ(navItem1->GetHttpRequestHeaders(),
navItem2->GetHttpRequestHeaders());
}
TEST_F(CRWSessionEntryTest, Description) {
[sessionEntry_ navigationItem]->SetTitle(base::SysNSStringToUTF16(@"Title"));
EXPECT_NSEQ([sessionEntry_ description],
@"url:http://init.test/ originalurl:http://init.test/ "
@"title:Title transition:2 displayState:{ scrollOffset:(nan, "
@"nan), zoomScaleRange:(nan, nan), zoomScale:nan } desktopUA:0");
}
TEST_F(CRWSessionEntryTest, InitWithCoder) {
web::NavigationItem* item = [sessionEntry_ navigationItem];
item->SetVirtualURL(GURL("http://user.friendly"));
item->SetTitle(base::SysNSStringToUTF16(@"Title"));
// Old serialized entries have no timestamp.
item->SetTimestamp(base::Time::FromInternalValue(0));
NSURL* virtualUrl = net::NSURLWithGURL(item->GetVirtualURL());
NSURL* referrer = net::NSURLWithGURL(item->GetReferrer().url);
NSString* title = base::SysUTF16ToNSString(item->GetTitle());
base::scoped_nsobject<id> decoder([[OCMockComplexTypeHelper alloc]
initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]);
decodeBytesForKeyBlock block = ^ const uint8_t* (NSString* key,
NSUInteger* length) {
*length = 0;
return NULL;
};
[[[decoder stub] andReturnValue:[NSNumber numberWithBool:NO]]
containsValueForKey:[OCMArg any]];
[decoder onSelector:@selector(decodeBytesForKey:returnedLength:)
callBlockExpectation:block];
[[[decoder expect] andReturn:virtualUrl]
decodeObjectForKey:web::kSessionEntryURLDeperecatedKey];
[[[decoder expect] andReturn:referrer]
decodeObjectForKey:web::kSessionEntryReferrerURLDeprecatedKey];
[[[decoder expect] andReturn:title]
decodeObjectForKey:web::kSessionEntryTitleKey];
const web::PageDisplayState& pageState =
[sessionEntry_ navigationItem]->GetPageDisplayState();
NSDictionary* serializedPageDisplayState =
[CRWSessionEntry dictionaryFromPageDisplayState:pageState];
[[[decoder expect] andReturn:serializedPageDisplayState]
decodeObjectForKey:web::kSessionEntryPageScrollStateKey];
BOOL useDesktopUserAgent =
[sessionEntry_ navigationItem]->IsOverridingUserAgent();
[[[decoder expect] andReturnValue:OCMOCK_VALUE(useDesktopUserAgent)]
decodeBoolForKey:web::kSessionEntryUseDesktopUserAgentKey];
NSDictionary* requestHeaders =
[sessionEntry_ navigationItem]->GetHttpRequestHeaders();
[[[decoder expect] andReturn:requestHeaders]
decodeObjectForKey:web::kSessionEntryHTTPRequestHeadersKey];
[[[decoder expect]
andReturn:[sessionEntry_ navigationItemImpl]->GetPostData()]
decodeObjectForKey:web::kSessionEntryPOSTDataKey];
BOOL skipResubmitDataConfirmation =
[sessionEntry_ navigationItemImpl]->ShouldSkipResubmitDataConfirmation();
[[[decoder expect] andReturnValue:OCMOCK_VALUE(skipResubmitDataConfirmation)]
decodeBoolForKey:web::kSessionEntrySkipResubmitConfirmationKey];
base::scoped_nsobject<CRWSessionEntry> newSessionEntry(
[[CRWSessionEntry alloc] initWithCoder:decoder]);
web::NavigationItem* newItem = [newSessionEntry navigationItem];
EXPECT_OCMOCK_VERIFY(decoder);
expectEqualSessionEntries(sessionEntry_, newSessionEntry,
ui::PAGE_TRANSITION_RELOAD);
EXPECT_NE(item->GetURL(), newItem->GetURL());
EXPECT_EQ(item->GetVirtualURL(), newItem->GetURL());
}
TEST_F(CRWSessionEntryTest, InitWithCoderNewStyle) {
web::NavigationItem* item = [sessionEntry_ navigationItem];
item->SetVirtualURL(GURL("http://user.friendly"));
item->SetTitle(base::SysNSStringToUTF16(@"Title"));
int64 timestamp = item->GetTimestamp().ToInternalValue();
std::string virtualUrl = item->GetVirtualURL().spec();
std::string referrerUrl = item->GetReferrer().url.spec();
NSString* title = base::SysUTF16ToNSString(item->GetTitle());
base::scoped_nsobject<id> decoder([[OCMockComplexTypeHelper alloc]
initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]);
const std::string emptyString;
decodeBytesForKeyBlock block = ^ const uint8_t* (NSString* key,
NSUInteger* length) {
const std::string *value = &emptyString;
if ([key isEqualToString:web::kSessionEntryURLKey])
value = &virtualUrl;
else if ([key isEqualToString:web::kSessionEntryReferrerURLKey])
value = &referrerUrl;
else
EXPECT_TRUE(false);
*length = value->size();
return reinterpret_cast<const uint8_t*>(value->data());
};
[decoder onSelector:@selector(decodeBytesForKey:returnedLength:)
callBlockExpectation:block];
[[[decoder stub] andReturnValue:[NSNumber numberWithBool:YES]]
containsValueForKey:[OCMArg any]];
web::ReferrerPolicy expectedPolicy = item->GetReferrer().policy;
[[[decoder expect] andReturnValue:OCMOCK_VALUE(expectedPolicy)]
decodeIntForKey:web::kSessionEntryReferrerPolicyKey];
[[[decoder expect] andReturnValue:OCMOCK_VALUE(timestamp)]
decodeInt64ForKey:web::kSessionEntryTimestampKey];
[[[decoder expect] andReturn:title]
decodeObjectForKey:web::kSessionEntryTitleKey];
const web::PageDisplayState& pageState =
[sessionEntry_ navigationItem]->GetPageDisplayState();
NSDictionary* serializedPageDisplayState =
[CRWSessionEntry dictionaryFromPageDisplayState:pageState];
[[[decoder expect] andReturn:serializedPageDisplayState]
decodeObjectForKey:web::kSessionEntryPageScrollStateKey];
BOOL useDesktopUserAgent =
[sessionEntry_ navigationItem]->IsOverridingUserAgent();
[[[decoder expect] andReturnValue:OCMOCK_VALUE(useDesktopUserAgent)]
decodeBoolForKey:web::kSessionEntryUseDesktopUserAgentKey];
NSDictionary* requestHeaders =
[sessionEntry_ navigationItem]->GetHttpRequestHeaders();
[[[decoder expect] andReturn:requestHeaders]
decodeObjectForKey:web::kSessionEntryHTTPRequestHeadersKey];
NSData* POSTData = [sessionEntry_ navigationItemImpl]->GetPostData();
[[[decoder expect] andReturn:POSTData]
decodeObjectForKey:web::kSessionEntryPOSTDataKey];
BOOL skipResubmitDataConfirmation =
[sessionEntry_ navigationItemImpl]->ShouldSkipResubmitDataConfirmation();
[[[decoder expect] andReturnValue:OCMOCK_VALUE(skipResubmitDataConfirmation)]
decodeBoolForKey:web::kSessionEntrySkipResubmitConfirmationKey];
base::scoped_nsobject<CRWSessionEntry> newSessionEntry(
[[CRWSessionEntry alloc] initWithCoder:decoder]);
web::NavigationItem* newItem = [newSessionEntry navigationItem];
EXPECT_OCMOCK_VERIFY(decoder);
expectEqualSessionEntries(sessionEntry_, newSessionEntry,
ui::PAGE_TRANSITION_RELOAD);
EXPECT_NE(item->GetURL(), newItem->GetURL());
EXPECT_EQ(item->GetVirtualURL(), newItem->GetVirtualURL());
}
TEST_F(CRWSessionEntryTest, EncodeDecode) {
NSData *data =
[NSKeyedArchiver archivedDataWithRootObject:sessionEntry_];
id decoded = [NSKeyedUnarchiver unarchiveObjectWithData:data];
expectEqualSessionEntries(sessionEntry_, decoded,
ui::PAGE_TRANSITION_RELOAD);
}
TEST_F(CRWSessionEntryTest, EncodeWithCoder) {
web::NavigationItem* item = [sessionEntry_ navigationItem];
NSString* title = base::SysUTF16ToNSString(item->GetTitle());
base::scoped_nsobject<id> coder([[OCMockComplexTypeHelper alloc]
initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]);
encodeBytes_length_forKey_block block =
^(const uint8_t* bytes, NSUInteger length, NSString* key) {
if ([key isEqualToString:web::kSessionEntryURLKey]) {
ASSERT_EQ(item->GetVirtualURL().spec(),
std::string(reinterpret_cast<const char*>(bytes), length));
return;
} else if ([key isEqualToString:web::kSessionEntryReferrerURLKey]) {
ASSERT_EQ(item->GetReferrer().url.spec(),
std::string(reinterpret_cast<const char*>(bytes), length));
return;
}
FAIL();
};
[coder onSelector:@selector(encodeBytes:length:forKey:)
callBlockExpectation:block];
[[coder expect] encodeInt:item->GetReferrer().policy
forKey:web::kSessionEntryReferrerPolicyKey];
[[coder expect] encodeInt64:item->GetTimestamp().ToInternalValue()
forKey:web::kSessionEntryTimestampKey];
[[coder expect] encodeObject:title forKey:web::kSessionEntryTitleKey];
const web::PageDisplayState& pageState =
[sessionEntry_ navigationItem]->GetPageDisplayState();
NSDictionary* serializedPageDisplayState =
[CRWSessionEntry dictionaryFromPageDisplayState:pageState];
[[coder expect] encodeObject:serializedPageDisplayState
forKey:web::kSessionEntryPageScrollStateKey];
BOOL useDesktopUserAgent =
[sessionEntry_ navigationItem]->IsOverridingUserAgent();
[[coder expect] encodeBool:useDesktopUserAgent
forKey:web::kSessionEntryUseDesktopUserAgentKey];
NSDictionary* requestHeaders =
[sessionEntry_ navigationItem]->GetHttpRequestHeaders();
[[coder expect] encodeObject:requestHeaders
forKey:web::kSessionEntryHTTPRequestHeadersKey];
[[coder expect] encodeObject:[sessionEntry_ navigationItemImpl]->GetPostData()
forKey:web::kSessionEntryPOSTDataKey];
BOOL skipResubmitDataConfirmation =
[sessionEntry_ navigationItemImpl]->ShouldSkipResubmitDataConfirmation();
[[coder expect] encodeBool:skipResubmitDataConfirmation
forKey:web::kSessionEntrySkipResubmitConfirmationKey];
[sessionEntry_ encodeWithCoder:coder];
EXPECT_OCMOCK_VERIFY(coder);
}
TEST_F(CRWSessionEntryTest, CodingEncoding) {
web::NavigationItem* item = [sessionEntry_ navigationItem];
item->SetVirtualURL(GURL("http://user.friendly"));
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:sessionEntry_];
EXPECT_TRUE(data != nil);
CRWSessionEntry* unarchivedSessionEntry =
[NSKeyedUnarchiver unarchiveObjectWithData:data];
ASSERT_TRUE(unarchivedSessionEntry != nil);
web::NavigationItem* unarchivedItem = [unarchivedSessionEntry navigationItem];
expectEqualSessionEntries(sessionEntry_, unarchivedSessionEntry,
ui::PAGE_TRANSITION_RELOAD);
EXPECT_EQ(unarchivedItem->GetURL(), item->GetVirtualURL());
EXPECT_NE(unarchivedItem->GetURL(), item->GetURL());
}
TEST_F(CRWSessionEntryTest, CopyWithZone) {
CRWSessionEntry* sessionEntry2 = [sessionEntry_ copy];
EXPECT_NE(sessionEntry_, sessionEntry2);
expectEqualSessionEntries(
sessionEntry_, sessionEntry2,
[sessionEntry_ navigationItem]->GetTransitionType());
}
TEST_F(CRWSessionEntryTest, EmptyVirtualUrl) {
EXPECT_EQ(GURL("http://init.test/"),
[sessionEntry_ navigationItem]->GetURL());
}
TEST_F(CRWSessionEntryTest, NonEmptyVirtualUrl) {
web::NavigationItem* item = [sessionEntry_ navigationItem];
item->SetVirtualURL(GURL("http://user.friendly"));
EXPECT_EQ(GURL("http://user.friendly/"), item->GetVirtualURL());
EXPECT_EQ(GURL("http://init.test/"), item->GetURL());
}
TEST_F(CRWSessionEntryTest, EmptyDescription) {
EXPECT_GT([[sessionEntry_ description] length], 0U);
}
|