Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: ios/web/navigation/crw_navigation_item_storage_unittest.mm

Issue 2664113003: Moved serialization out of CRWSessionEntry. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #import "ios/web/public/crw_navigation_item_storage.h"
6
7 #import <Foundation/Foundation.h>
8 #include <stdint.h>
9
10 #include <utility>
11
12 #import "base/mac/scoped_nsobject.h"
13 #include "base/strings/sys_string_conversions.h"
14 #import "ios/testing/ocmock_complex_type_helper.h"
Eugene But (OOO till 7-30) 2017/01/31 22:12:26 This test tests interaction (that specific methods
kkhorimoto 2017/02/03 00:36:09 Done.
15 #import "ios/web/navigation/navigation_item_impl.h"
16 #include "ios/web/public/referrer.h"
17 #import "net/base/mac/url_conversions.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #import "testing/gtest_mac.h"
20 #include "testing/platform_test.h"
21 #import "third_party/ocmock/OCMock/OCMock.h"
22 #include "third_party/ocmock/gtest_support.h"
23 #include "ui/base/page_transition_types.h"
24
25 class CRWNavigationItemStorageTest : public PlatformTest {
26 protected:
27 void SetUp() override {
28 // Set up |item_storage_|.
Eugene But (OOO till 7-30) 2017/01/31 22:12:26 Is it possible to move this code to constructor? I
kkhorimoto 2017/02/03 00:36:09 Done.
29 item_storage_.reset([[CRWNavigationItemStorage alloc] init]);
30 [item_storage_ setVirtualURL:GURL("http://init.test")];
31 [item_storage_ setReferrer:web::Referrer(GURL("http://referrer.url"),
32 web::ReferrerPolicyDefault)];
33 [item_storage_ setTimestamp:base::Time::Now()];
34 [item_storage_ setTitle:base::SysNSStringToUTF16(@"Title")];
35 [item_storage_
36 setDisplayState:web::PageDisplayState(0.0, 0.0, 0.0, 0.0, 0.0)];
37 [item_storage_
38 setPOSTData:[@"Test data" dataUsingEncoding:NSUTF8StringEncoding]];
39 [item_storage_ setHTTPRequestHeaders:@{ @"HeaderKey" : @"HeaderValue" }];
40 }
41
42 // Convenience getter to facilitate dot notation in tests.
43 CRWNavigationItemStorage* item_storage() { return item_storage_; }
44
45 protected:
46 base::scoped_nsobject<CRWNavigationItemStorage> item_storage_;
47 };
48
49 @implementation OCMockComplexTypeHelper (CRWNavigationItemStorageTest)
50 typedef void (^encodeBytes_length_forKey_block)(const uint8_t*,
51 NSUInteger,
52 NSString*);
53 - (void)encodeBytes:(const uint8_t*)bytes
54 length:(NSUInteger)length
55 forKey:(NSString*)key {
56 static_cast<encodeBytes_length_forKey_block>([self blockForSelector:_cmd])(
57 bytes, length, key);
58 }
59
60 typedef const uint8_t* (^decodeBytesForKeyBlock)(NSString*, NSUInteger*);
61 - (const uint8_t*)decodeBytesForKey:(NSString*)key
62 returnedLength:(NSUInteger*)lengthp {
63 return static_cast<decodeBytesForKeyBlock>([self blockForSelector:_cmd])(
64 key, lengthp);
65 }
66 @end
67
68 // Tests initializing with the legacy keys.
69 TEST_F(CRWNavigationItemStorageTest, InitWithCoderLegacy) {
70 NSURL* virtualUrl = net::NSURLWithGURL(item_storage().virtualURL);
71 NSURL* referrer = net::NSURLWithGURL(item_storage().referrer.url);
72 NSString* title = base::SysUTF16ToNSString(item_storage().title);
73 // Legacy NavigationItems don't persist timestamp.
74 item_storage().timestamp = base::Time::FromCFAbsoluteTime(0);
75 base::scoped_nsobject<id> decoder([[OCMockComplexTypeHelper alloc]
76 initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]);
77
78 decodeBytesForKeyBlock block =
79 ^const uint8_t*(NSString* key, NSUInteger* length) {
80 *length = 0;
81 return NULL;
82 };
83
84 [[[decoder stub] andReturnValue:[NSNumber numberWithBool:NO]]
85 containsValueForKey:[OCMArg any]];
86
87 [decoder onSelector:@selector(decodeBytesForKey:returnedLength:)
88 callBlockExpectation:block];
89 [[[decoder expect] andReturn:virtualUrl]
90 decodeObjectForKey:web::kNavigationItemStorageURLDeperecatedKey];
91 [[[decoder expect] andReturn:referrer]
92 decodeObjectForKey:web::kNavigationItemStorageReferrerURLDeprecatedKey];
93 [[[decoder expect] andReturn:title]
94 decodeObjectForKey:web::kNavigationItemStorageTitleKey];
95 NSDictionary* display_state_dict = [CRWNavigationItemStorage
96 dictionaryFromDisplayState:item_storage().displayState];
97 [[[decoder expect] andReturn:display_state_dict]
98 decodeObjectForKey:web::kNavigationItemStoragePageDisplayStateKey];
99 BOOL overriding_user_agent = item_storage().overridingUserAgent;
100 [[[decoder expect] andReturnValue:OCMOCK_VALUE(overriding_user_agent)]
101 decodeBoolForKey:web::kNavigationItemStorageUseDesktopUserAgentKey];
102 NSDictionary* request_headers = item_storage().HTTPRequestHeaders;
103 [[[decoder expect] andReturn:request_headers]
104 decodeObjectForKey:web::kNavigationItemStorageHTTPRequestHeadersKey];
105 [[[decoder expect] andReturn:item_storage().POSTData]
106 decodeObjectForKey:web::kNavigationItemStoragePOSTDataKey];
107 BOOL skip_repost_form_confirmation =
108 item_storage().shouldSkipRepostFormConfirmation;
109 [[[decoder expect] andReturnValue:OCMOCK_VALUE(skip_repost_form_confirmation)]
110 decodeBoolForKey:web::
111 kNavigationItemStorageSkipRepostFormConfirmationKey];
112
113 base::scoped_nsobject<CRWNavigationItemStorage> new_storage(
114 [[CRWNavigationItemStorage alloc] initWithCoder:decoder]);
115
116 EXPECT_OCMOCK_VERIFY(decoder);
117 EXPECT_NSEQ(item_storage(), new_storage.get());
118 }
119
120 // Tests initializing with the new keys.
121 TEST_F(CRWNavigationItemStorageTest, InitWithCoder) {
122 std::string virtual_url = item_storage().virtualURL.spec();
123 std::string referrer_url = item_storage().referrer.url.spec();
124 NSString* title = base::SysUTF16ToNSString(item_storage().title);
125 int64_t timestamp = item_storage().timestamp.ToInternalValue();
126 base::scoped_nsobject<id> decoder([[OCMockComplexTypeHelper alloc]
127 initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]);
128
129 const std::string empty_string;
130 decodeBytesForKeyBlock block =
131 ^const uint8_t*(NSString* key, NSUInteger* length) {
132 const std::string* value = &empty_string;
133 if ([key isEqualToString:web::kNavigationItemStorageURLKey])
134 value = &virtual_url;
135 else if ([key isEqualToString:web::kNavigationItemStorageReferrerURLKey])
136 value = &referrer_url;
137 else
138 EXPECT_TRUE(false);
139
140 *length = value->size();
141 return reinterpret_cast<const uint8_t*>(value->data());
142 };
143
144 [decoder onSelector:@selector(decodeBytesForKey:returnedLength:)
145 callBlockExpectation:block];
146 [[[decoder stub] andReturnValue:[NSNumber numberWithBool:YES]]
147 containsValueForKey:[OCMArg any]];
148 web::ReferrerPolicy expected_policy = item_storage().referrer.policy;
149 [[[decoder expect] andReturnValue:OCMOCK_VALUE(expected_policy)]
150 decodeIntForKey:web::kNavigationItemStorageReferrerPolicyKey];
151 [[[decoder expect] andReturnValue:OCMOCK_VALUE(timestamp)]
152 decodeInt64ForKey:web::kNavigationItemStorageTimestampKey];
153 [[[decoder expect] andReturn:title]
154 decodeObjectForKey:web::kNavigationItemStorageTitleKey];
155 const web::PageDisplayState& display_state = item_storage().displayState;
156 NSDictionary* display_state_dict =
157 [CRWNavigationItemStorage dictionaryFromDisplayState:display_state];
158 [[[decoder expect] andReturn:display_state_dict]
159 decodeObjectForKey:web::kNavigationItemStoragePageDisplayStateKey];
160 BOOL overriding_user_agent = item_storage().overridingUserAgent;
161 [[[decoder expect] andReturnValue:OCMOCK_VALUE(overriding_user_agent)]
162 decodeBoolForKey:web::kNavigationItemStorageUseDesktopUserAgentKey];
163 NSDictionary* request_headers = item_storage().HTTPRequestHeaders;
164 [[[decoder expect] andReturn:request_headers]
165 decodeObjectForKey:web::kNavigationItemStorageHTTPRequestHeadersKey];
166 NSData* post_data = item_storage().POSTData;
167 [[[decoder expect] andReturn:post_data]
168 decodeObjectForKey:web::kNavigationItemStoragePOSTDataKey];
169 BOOL skip_repost_form_confirmation =
170 item_storage().shouldSkipRepostFormConfirmation;
171 [[[decoder expect] andReturnValue:OCMOCK_VALUE(skip_repost_form_confirmation)]
172 decodeBoolForKey:web::
173 kNavigationItemStorageSkipRepostFormConfirmationKey];
174
175 base::scoped_nsobject<CRWNavigationItemStorage> new_storage(
176 [[CRWNavigationItemStorage alloc] initWithCoder:decoder]);
177
178 EXPECT_OCMOCK_VERIFY(decoder);
179 EXPECT_NSEQ(item_storage(), new_storage.get());
180 }
181
182 // Tests that unarchiving CRWNavigationItemStorage data results in an equivalent
183 // storage.
184 TEST_F(CRWNavigationItemStorageTest, EncodeDecode) {
185 NSData* data = [NSKeyedArchiver archivedDataWithRootObject:item_storage()];
186 id decoded = [NSKeyedUnarchiver unarchiveObjectWithData:data];
187 EXPECT_NSEQ(item_storage(), decoded);
188 }
189
190 // Tests that CRWNavigationItemStorage is encoded as expected.
191 TEST_F(CRWNavigationItemStorageTest, EncodeWithCoder) {
192 NSString* title = base::SysUTF16ToNSString(item_storage().title);
193
194 base::scoped_nsobject<id> coder([[OCMockComplexTypeHelper alloc]
195 initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]);
196
197 encodeBytes_length_forKey_block block = ^(const uint8_t* bytes,
198 NSUInteger length, NSString* key) {
199 if ([key isEqualToString:web::kNavigationItemStorageURLKey]) {
200 ASSERT_EQ(item_storage().virtualURL.spec(),
201 std::string(reinterpret_cast<const char*>(bytes), length));
202 return;
203 } else if ([key
204 isEqualToString:web::kNavigationItemStorageReferrerURLKey]) {
205 ASSERT_EQ(item_storage().referrer.url.spec(),
206 std::string(reinterpret_cast<const char*>(bytes), length));
207 return;
208 }
209 FAIL();
210 };
211 [coder onSelector:@selector(encodeBytes:length:forKey:)
212 callBlockExpectation:block];
213 [[coder expect] encodeInt:item_storage().referrer.policy
214 forKey:web::kNavigationItemStorageReferrerPolicyKey];
215 [[coder expect] encodeInt64:item_storage().timestamp.ToInternalValue()
216 forKey:web::kNavigationItemStorageTimestampKey];
217 [[coder expect] encodeObject:title
218 forKey:web::kNavigationItemStorageTitleKey];
219 const web::PageDisplayState& display_state = item_storage().displayState;
220 NSDictionary* display_state_dict =
221 [CRWNavigationItemStorage dictionaryFromDisplayState:display_state];
222 [[coder expect] encodeObject:display_state_dict
223 forKey:web::kNavigationItemStoragePageDisplayStateKey];
224 BOOL useDesktopUserAgent = item_storage().overridingUserAgent;
225 [[coder expect] encodeBool:useDesktopUserAgent
226 forKey:web::kNavigationItemStorageUseDesktopUserAgentKey];
227 NSDictionary* request_headers = item_storage().HTTPRequestHeaders;
228 [[coder expect]
229 encodeObject:request_headers
230 forKey:web::kNavigationItemStorageHTTPRequestHeadersKey];
231 [[coder expect] encodeObject:item_storage().POSTData
232 forKey:web::kNavigationItemStoragePOSTDataKey];
233 BOOL skip_repost_form_confirmation =
234 item_storage().shouldSkipRepostFormConfirmation;
235 [[coder expect]
236 encodeBool:skip_repost_form_confirmation
237 forKey:web::kNavigationItemStorageSkipRepostFormConfirmationKey];
238 [item_storage() encodeWithCoder:coder];
239 EXPECT_OCMOCK_VERIFY(coder);
240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698