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 namespace { |
| 24 |
| 25 // Checks equality between item1 and item2. |
| 26 BOOL ItemStoragesAreEqual(CRWNavigationItemStorage* item1, |
| 27 CRWNavigationItemStorage* item2) { |
| 28 return item1.virtualURL == item2.virtualURL && |
| 29 item1.referrer.url == item2.referrer.url && |
| 30 item1.referrer.policy == item2.referrer.policy && |
| 31 item1.timestamp == item2.timestamp && item1.title == item2.title && |
| 32 item1.displayState == item2.displayState && |
| 33 item1.shouldSkipRepostFormConfirmation == |
| 34 item2.shouldSkipRepostFormConfirmation && |
| 35 item1.overridingUserAgent == item2.overridingUserAgent && |
| 36 [item1.POSTData isEqualToData:item2.POSTData] && |
| 37 [item1.HTTPRequestHeaders |
| 38 isEqualToDictionary:item2.HTTPRequestHeaders]; |
| 39 } |
| 40 } // namespace |
| 41 |
| 42 class CRWNavigationItemStorageTest : public PlatformTest { |
| 43 protected: |
| 44 CRWNavigationItemStorageTest() |
| 45 : item_storage_([[CRWNavigationItemStorage alloc] init]) { |
| 46 // Set up |item_storage_|. |
| 47 [item_storage_ setVirtualURL:GURL("http://init.test")]; |
| 48 [item_storage_ setReferrer:web::Referrer(GURL("http://referrer.url"), |
| 49 web::ReferrerPolicyDefault)]; |
| 50 [item_storage_ setTimestamp:base::Time::Now()]; |
| 51 [item_storage_ setTitle:base::SysNSStringToUTF16(@"Title")]; |
| 52 [item_storage_ |
| 53 setDisplayState:web::PageDisplayState(0.0, 0.0, 0.0, 0.0, 0.0)]; |
| 54 [item_storage_ |
| 55 setPOSTData:[@"Test data" dataUsingEncoding:NSUTF8StringEncoding]]; |
| 56 [item_storage_ setHTTPRequestHeaders:@{ @"HeaderKey" : @"HeaderValue" }]; |
| 57 } |
| 58 |
| 59 // Convenience getter to facilitate dot notation in tests. |
| 60 CRWNavigationItemStorage* item_storage() { return item_storage_; } |
| 61 |
| 62 protected: |
| 63 base::scoped_nsobject<CRWNavigationItemStorage> item_storage_; |
| 64 }; |
| 65 |
| 66 // Tests initializing with the legacy keys. |
| 67 TEST_F(CRWNavigationItemStorageTest, InitWithCoderLegacy) { |
| 68 NSURL* virtualURL = net::NSURLWithGURL(item_storage().virtualURL); |
| 69 NSURL* referrerURL = net::NSURLWithGURL(item_storage().referrer.url); |
| 70 NSString* title = base::SysUTF16ToNSString(item_storage().title); |
| 71 // Legacy NavigationItems don't persist timestamp. |
| 72 item_storage().timestamp = base::Time::FromCFAbsoluteTime(0); |
| 73 |
| 74 // Set up archiver and unarchiver. |
| 75 base::scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]); |
| 76 base::scoped_nsobject<NSKeyedArchiver> archiver( |
| 77 [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]); |
| 78 [archiver encodeObject:virtualURL |
| 79 forKey:web::kNavigationItemStorageURLDeperecatedKey]; |
| 80 [archiver encodeObject:referrerURL |
| 81 forKey:web::kNavigationItemStorageReferrerURLDeprecatedKey]; |
| 82 [archiver encodeObject:title forKey:web::kNavigationItemStorageTitleKey]; |
| 83 NSDictionary* display_state_dict = |
| 84 item_storage().displayState.GetSerialization(); |
| 85 [archiver encodeObject:display_state_dict |
| 86 forKey:web::kNavigationItemStoragePageDisplayStateKey]; |
| 87 BOOL overriding_user_agent = item_storage().overridingUserAgent; |
| 88 [archiver encodeBool:overriding_user_agent |
| 89 forKey:web::kNavigationItemStorageUseDesktopUserAgentKey]; |
| 90 NSDictionary* request_headers = item_storage().HTTPRequestHeaders; |
| 91 [archiver encodeObject:request_headers |
| 92 forKey:web::kNavigationItemStorageHTTPRequestHeadersKey]; |
| 93 [archiver encodeObject:item_storage().POSTData |
| 94 forKey:web::kNavigationItemStoragePOSTDataKey]; |
| 95 BOOL skip_repost_form_confirmation = |
| 96 item_storage().shouldSkipRepostFormConfirmation; |
| 97 [archiver |
| 98 encodeBool:skip_repost_form_confirmation |
| 99 forKey:web::kNavigationItemStorageSkipRepostFormConfirmationKey]; |
| 100 [archiver finishEncoding]; |
| 101 base::scoped_nsobject<NSKeyedUnarchiver> unarchiver( |
| 102 [[NSKeyedUnarchiver alloc] initForReadingWithData:data]); |
| 103 |
| 104 // Create a CRWNavigationItemStorage and verify that it is equivalent. |
| 105 base::scoped_nsobject<CRWNavigationItemStorage> new_storage( |
| 106 [[CRWNavigationItemStorage alloc] initWithCoder:unarchiver]); |
| 107 EXPECT_TRUE(ItemStoragesAreEqual(item_storage(), new_storage.get())); |
| 108 } |
| 109 |
| 110 // Tests that unarchiving CRWNavigationItemStorage data results in an equivalent |
| 111 // storage. |
| 112 TEST_F(CRWNavigationItemStorageTest, EncodeDecode) { |
| 113 NSData* data = [NSKeyedArchiver archivedDataWithRootObject:item_storage()]; |
| 114 id decoded = [NSKeyedUnarchiver unarchiveObjectWithData:data]; |
| 115 EXPECT_TRUE(ItemStoragesAreEqual(item_storage(), decoded)); |
| 116 } |
OLD | NEW |