OLD | NEW |
(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/web/navigation/navigation_item_impl.h" |
| 15 #include "ios/web/public/referrer.h" |
| 16 #import "net/base/mac/url_conversions.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #import "testing/gtest_mac.h" |
| 19 #include "testing/platform_test.h" |
| 20 #include "third_party/ocmock/gtest_support.h" |
| 21 #include "ui/base/page_transition_types.h" |
| 22 |
| 23 class CRWNavigationItemStorageTest : public PlatformTest { |
| 24 protected: |
| 25 CRWNavigationItemStorageTest() |
| 26 : item_storage_([[CRWNavigationItemStorage alloc] init]) { |
| 27 // Set up |item_storage_|. |
| 28 [item_storage_ setVirtualURL:GURL("http://init.test")]; |
| 29 [item_storage_ setReferrer:web::Referrer(GURL("http://referrer.url"), |
| 30 web::ReferrerPolicyDefault)]; |
| 31 [item_storage_ setTimestamp:base::Time::Now()]; |
| 32 [item_storage_ setTitle:base::SysNSStringToUTF16(@"Title")]; |
| 33 [item_storage_ |
| 34 setDisplayState:web::PageDisplayState(0.0, 0.0, 0.0, 0.0, 0.0)]; |
| 35 [item_storage_ |
| 36 setPOSTData:[@"Test data" dataUsingEncoding:NSUTF8StringEncoding]]; |
| 37 [item_storage_ setHTTPRequestHeaders:@{ @"HeaderKey" : @"HeaderValue" }]; |
| 38 } |
| 39 |
| 40 // Convenience getter to facilitate dot notation in tests. |
| 41 CRWNavigationItemStorage* item_storage() { return item_storage_; } |
| 42 |
| 43 protected: |
| 44 base::scoped_nsobject<CRWNavigationItemStorage> item_storage_; |
| 45 }; |
| 46 |
| 47 // Tests initializing with the legacy keys. |
| 48 TEST_F(CRWNavigationItemStorageTest, InitWithCoderLegacy) { |
| 49 NSURL* virtualURL = net::NSURLWithGURL(item_storage().virtualURL); |
| 50 NSURL* referrerURL = net::NSURLWithGURL(item_storage().referrer.url); |
| 51 NSString* title = base::SysUTF16ToNSString(item_storage().title); |
| 52 // Legacy NavigationItems don't persist timestamp. |
| 53 item_storage().timestamp = base::Time::FromCFAbsoluteTime(0); |
| 54 |
| 55 // Set up archiver and unarchiver. |
| 56 base::scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]); |
| 57 base::scoped_nsobject<NSKeyedArchiver> archiver( |
| 58 [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]); |
| 59 [archiver encodeObject:virtualURL |
| 60 forKey:web::kNavigationItemStorageURLDeperecatedKey]; |
| 61 [archiver encodeObject:referrerURL |
| 62 forKey:web::kNavigationItemStorageReferrerURLDeprecatedKey]; |
| 63 [archiver encodeObject:title forKey:web::kNavigationItemStorageTitleKey]; |
| 64 NSDictionary* display_state_dict = |
| 65 item_storage().displayState.GetSerialization(); |
| 66 [archiver encodeObject:display_state_dict |
| 67 forKey:web::kNavigationItemStoragePageDisplayStateKey]; |
| 68 BOOL overriding_user_agent = item_storage().overridingUserAgent; |
| 69 [archiver encodeBool:overriding_user_agent |
| 70 forKey:web::kNavigationItemStorageUseDesktopUserAgentKey]; |
| 71 NSDictionary* request_headers = item_storage().HTTPRequestHeaders; |
| 72 [archiver encodeObject:request_headers |
| 73 forKey:web::kNavigationItemStorageHTTPRequestHeadersKey]; |
| 74 [archiver encodeObject:item_storage().POSTData |
| 75 forKey:web::kNavigationItemStoragePOSTDataKey]; |
| 76 BOOL skip_repost_form_confirmation = |
| 77 item_storage().shouldSkipRepostFormConfirmation; |
| 78 [archiver |
| 79 encodeBool:skip_repost_form_confirmation |
| 80 forKey:web::kNavigationItemStorageSkipRepostFormConfirmationKey]; |
| 81 [archiver finishEncoding]; |
| 82 base::scoped_nsobject<NSKeyedUnarchiver> unarchiver( |
| 83 [[NSKeyedUnarchiver alloc] initForReadingWithData:data]); |
| 84 |
| 85 // Create a CRWNavigationItemStorage and verify that it is equivalent. |
| 86 base::scoped_nsobject<CRWNavigationItemStorage> new_storage( |
| 87 [[CRWNavigationItemStorage alloc] initWithCoder:unarchiver]); |
| 88 EXPECT_NSEQ(item_storage(), new_storage.get()); |
| 89 } |
| 90 |
| 91 // Tests that unarchiving CRWNavigationItemStorage data results in an equivalent |
| 92 // storage. |
| 93 TEST_F(CRWNavigationItemStorageTest, EncodeDecode) { |
| 94 NSData* data = [NSKeyedArchiver archivedDataWithRootObject:item_storage()]; |
| 95 id decoded = [NSKeyedUnarchiver unarchiveObjectWithData:data]; |
| 96 EXPECT_NSEQ(item_storage(), decoded); |
| 97 } |
OLD | NEW |