Chromium Code Reviews| Index: ios/web/navigation/crw_navigation_item_storage_unittest.mm |
| diff --git a/ios/web/navigation/crw_navigation_item_storage_unittest.mm b/ios/web/navigation/crw_navigation_item_storage_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f61165e99541820b7f6648640ef0e5c77372b129 |
| --- /dev/null |
| +++ b/ios/web/navigation/crw_navigation_item_storage_unittest.mm |
| @@ -0,0 +1,240 @@ |
| +// Copyright 2017 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 "ios/web/public/crw_navigation_item_storage.h" |
| + |
| +#import <Foundation/Foundation.h> |
| +#include <stdint.h> |
| + |
| +#include <utility> |
| + |
| +#import "base/mac/scoped_nsobject.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#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.
|
| +#import "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" |
| +#import "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" |
| + |
| +class CRWNavigationItemStorageTest : public PlatformTest { |
| + protected: |
| + void SetUp() override { |
| + // 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.
|
| + item_storage_.reset([[CRWNavigationItemStorage alloc] init]); |
| + [item_storage_ setVirtualURL:GURL("http://init.test")]; |
| + [item_storage_ setReferrer:web::Referrer(GURL("http://referrer.url"), |
| + web::ReferrerPolicyDefault)]; |
| + [item_storage_ setTimestamp:base::Time::Now()]; |
| + [item_storage_ setTitle:base::SysNSStringToUTF16(@"Title")]; |
| + [item_storage_ |
| + setDisplayState:web::PageDisplayState(0.0, 0.0, 0.0, 0.0, 0.0)]; |
| + [item_storage_ |
| + setPOSTData:[@"Test data" dataUsingEncoding:NSUTF8StringEncoding]]; |
| + [item_storage_ setHTTPRequestHeaders:@{ @"HeaderKey" : @"HeaderValue" }]; |
| + } |
| + |
| + // Convenience getter to facilitate dot notation in tests. |
| + CRWNavigationItemStorage* item_storage() { return item_storage_; } |
| + |
| + protected: |
| + base::scoped_nsobject<CRWNavigationItemStorage> item_storage_; |
| +}; |
| + |
| +@implementation OCMockComplexTypeHelper (CRWNavigationItemStorageTest) |
| +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 |
| + |
| +// Tests initializing with the legacy keys. |
| +TEST_F(CRWNavigationItemStorageTest, InitWithCoderLegacy) { |
| + NSURL* virtualUrl = net::NSURLWithGURL(item_storage().virtualURL); |
| + NSURL* referrer = net::NSURLWithGURL(item_storage().referrer.url); |
| + NSString* title = base::SysUTF16ToNSString(item_storage().title); |
| + // Legacy NavigationItems don't persist timestamp. |
| + item_storage().timestamp = base::Time::FromCFAbsoluteTime(0); |
| + 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::kNavigationItemStorageURLDeperecatedKey]; |
| + [[[decoder expect] andReturn:referrer] |
| + decodeObjectForKey:web::kNavigationItemStorageReferrerURLDeprecatedKey]; |
| + [[[decoder expect] andReturn:title] |
| + decodeObjectForKey:web::kNavigationItemStorageTitleKey]; |
| + NSDictionary* display_state_dict = [CRWNavigationItemStorage |
| + dictionaryFromDisplayState:item_storage().displayState]; |
| + [[[decoder expect] andReturn:display_state_dict] |
| + decodeObjectForKey:web::kNavigationItemStoragePageDisplayStateKey]; |
| + BOOL overriding_user_agent = item_storage().overridingUserAgent; |
| + [[[decoder expect] andReturnValue:OCMOCK_VALUE(overriding_user_agent)] |
| + decodeBoolForKey:web::kNavigationItemStorageUseDesktopUserAgentKey]; |
| + NSDictionary* request_headers = item_storage().HTTPRequestHeaders; |
| + [[[decoder expect] andReturn:request_headers] |
| + decodeObjectForKey:web::kNavigationItemStorageHTTPRequestHeadersKey]; |
| + [[[decoder expect] andReturn:item_storage().POSTData] |
| + decodeObjectForKey:web::kNavigationItemStoragePOSTDataKey]; |
| + BOOL skip_repost_form_confirmation = |
| + item_storage().shouldSkipRepostFormConfirmation; |
| + [[[decoder expect] andReturnValue:OCMOCK_VALUE(skip_repost_form_confirmation)] |
| + decodeBoolForKey:web:: |
| + kNavigationItemStorageSkipRepostFormConfirmationKey]; |
| + |
| + base::scoped_nsobject<CRWNavigationItemStorage> new_storage( |
| + [[CRWNavigationItemStorage alloc] initWithCoder:decoder]); |
| + |
| + EXPECT_OCMOCK_VERIFY(decoder); |
| + EXPECT_NSEQ(item_storage(), new_storage.get()); |
| +} |
| + |
| +// Tests initializing with the new keys. |
| +TEST_F(CRWNavigationItemStorageTest, InitWithCoder) { |
| + std::string virtual_url = item_storage().virtualURL.spec(); |
| + std::string referrer_url = item_storage().referrer.url.spec(); |
| + NSString* title = base::SysUTF16ToNSString(item_storage().title); |
| + int64_t timestamp = item_storage().timestamp.ToInternalValue(); |
| + base::scoped_nsobject<id> decoder([[OCMockComplexTypeHelper alloc] |
| + initWithRepresentedObject:[OCMockObject mockForClass:[NSCoder class]]]); |
| + |
| + const std::string empty_string; |
| + decodeBytesForKeyBlock block = |
| + ^const uint8_t*(NSString* key, NSUInteger* length) { |
| + const std::string* value = &empty_string; |
| + if ([key isEqualToString:web::kNavigationItemStorageURLKey]) |
| + value = &virtual_url; |
| + else if ([key isEqualToString:web::kNavigationItemStorageReferrerURLKey]) |
| + value = &referrer_url; |
| + 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 expected_policy = item_storage().referrer.policy; |
| + [[[decoder expect] andReturnValue:OCMOCK_VALUE(expected_policy)] |
| + decodeIntForKey:web::kNavigationItemStorageReferrerPolicyKey]; |
| + [[[decoder expect] andReturnValue:OCMOCK_VALUE(timestamp)] |
| + decodeInt64ForKey:web::kNavigationItemStorageTimestampKey]; |
| + [[[decoder expect] andReturn:title] |
| + decodeObjectForKey:web::kNavigationItemStorageTitleKey]; |
| + const web::PageDisplayState& display_state = item_storage().displayState; |
| + NSDictionary* display_state_dict = |
| + [CRWNavigationItemStorage dictionaryFromDisplayState:display_state]; |
| + [[[decoder expect] andReturn:display_state_dict] |
| + decodeObjectForKey:web::kNavigationItemStoragePageDisplayStateKey]; |
| + BOOL overriding_user_agent = item_storage().overridingUserAgent; |
| + [[[decoder expect] andReturnValue:OCMOCK_VALUE(overriding_user_agent)] |
| + decodeBoolForKey:web::kNavigationItemStorageUseDesktopUserAgentKey]; |
| + NSDictionary* request_headers = item_storage().HTTPRequestHeaders; |
| + [[[decoder expect] andReturn:request_headers] |
| + decodeObjectForKey:web::kNavigationItemStorageHTTPRequestHeadersKey]; |
| + NSData* post_data = item_storage().POSTData; |
| + [[[decoder expect] andReturn:post_data] |
| + decodeObjectForKey:web::kNavigationItemStoragePOSTDataKey]; |
| + BOOL skip_repost_form_confirmation = |
| + item_storage().shouldSkipRepostFormConfirmation; |
| + [[[decoder expect] andReturnValue:OCMOCK_VALUE(skip_repost_form_confirmation)] |
| + decodeBoolForKey:web:: |
| + kNavigationItemStorageSkipRepostFormConfirmationKey]; |
| + |
| + base::scoped_nsobject<CRWNavigationItemStorage> new_storage( |
| + [[CRWNavigationItemStorage alloc] initWithCoder:decoder]); |
| + |
| + EXPECT_OCMOCK_VERIFY(decoder); |
| + EXPECT_NSEQ(item_storage(), new_storage.get()); |
| +} |
| + |
| +// Tests that unarchiving CRWNavigationItemStorage data results in an equivalent |
| +// storage. |
| +TEST_F(CRWNavigationItemStorageTest, EncodeDecode) { |
| + NSData* data = [NSKeyedArchiver archivedDataWithRootObject:item_storage()]; |
| + id decoded = [NSKeyedUnarchiver unarchiveObjectWithData:data]; |
| + EXPECT_NSEQ(item_storage(), decoded); |
| +} |
| + |
| +// Tests that CRWNavigationItemStorage is encoded as expected. |
| +TEST_F(CRWNavigationItemStorageTest, EncodeWithCoder) { |
| + NSString* title = base::SysUTF16ToNSString(item_storage().title); |
| + |
| + 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::kNavigationItemStorageURLKey]) { |
| + ASSERT_EQ(item_storage().virtualURL.spec(), |
| + std::string(reinterpret_cast<const char*>(bytes), length)); |
| + return; |
| + } else if ([key |
| + isEqualToString:web::kNavigationItemStorageReferrerURLKey]) { |
| + ASSERT_EQ(item_storage().referrer.url.spec(), |
| + std::string(reinterpret_cast<const char*>(bytes), length)); |
| + return; |
| + } |
| + FAIL(); |
| + }; |
| + [coder onSelector:@selector(encodeBytes:length:forKey:) |
| + callBlockExpectation:block]; |
| + [[coder expect] encodeInt:item_storage().referrer.policy |
| + forKey:web::kNavigationItemStorageReferrerPolicyKey]; |
| + [[coder expect] encodeInt64:item_storage().timestamp.ToInternalValue() |
| + forKey:web::kNavigationItemStorageTimestampKey]; |
| + [[coder expect] encodeObject:title |
| + forKey:web::kNavigationItemStorageTitleKey]; |
| + const web::PageDisplayState& display_state = item_storage().displayState; |
| + NSDictionary* display_state_dict = |
| + [CRWNavigationItemStorage dictionaryFromDisplayState:display_state]; |
| + [[coder expect] encodeObject:display_state_dict |
| + forKey:web::kNavigationItemStoragePageDisplayStateKey]; |
| + BOOL useDesktopUserAgent = item_storage().overridingUserAgent; |
| + [[coder expect] encodeBool:useDesktopUserAgent |
| + forKey:web::kNavigationItemStorageUseDesktopUserAgentKey]; |
| + NSDictionary* request_headers = item_storage().HTTPRequestHeaders; |
| + [[coder expect] |
| + encodeObject:request_headers |
| + forKey:web::kNavigationItemStorageHTTPRequestHeadersKey]; |
| + [[coder expect] encodeObject:item_storage().POSTData |
| + forKey:web::kNavigationItemStoragePOSTDataKey]; |
| + BOOL skip_repost_form_confirmation = |
| + item_storage().shouldSkipRepostFormConfirmation; |
| + [[coder expect] |
| + encodeBool:skip_repost_form_confirmation |
| + forKey:web::kNavigationItemStorageSkipRepostFormConfirmationKey]; |
| + [item_storage() encodeWithCoder:coder]; |
| + EXPECT_OCMOCK_VERIFY(coder); |
| +} |