Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / web / navigation / navigation_item_impl_unittest.mm
blob0189ea04c3a9be95a75a02c36ce1d1bcdb151e65
1 // Copyright 2014 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.
5 #include "base/logging.h"
6 #include "base/mac/scoped_nsobject.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "ios/web/navigation/navigation_item_impl.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/gtest_mac.h"
12 #include "testing/platform_test.h"
14 namespace web {
15 namespace {
17 static NSString* const kHTTPHeaderKey1 = @"key1";
18 static NSString* const kHTTPHeaderKey2 = @"key2";
19 static NSString* const kHTTPHeaderValue1 = @"value1";
20 static NSString* const kHTTPHeaderValue2 = @"value2";
22 class NavigationItemTest : public PlatformTest {
23  protected:
24   void SetUp() override {
25     item_.reset(new web::NavigationItemImpl());
26     item_->SetURL(GURL("http://init.test"));
27     item_->SetTransitionType(ui::PAGE_TRANSITION_AUTO_BOOKMARK);
28     item_->SetTimestamp(base::Time::Now());
29     item_->AddHttpRequestHeaders(@{kHTTPHeaderKey1 : kHTTPHeaderValue1});
30     item_->SetPostData([@"Test data" dataUsingEncoding:NSUTF8StringEncoding]);
31   }
33   // The NavigationItemImpl instance being tested.
34   scoped_ptr<NavigationItemImpl> item_;
37 // TODO(rohitrao): Add and adapt tests from NavigationEntryImpl.
38 TEST_F(NavigationItemTest, Dummy) {
39   const GURL url("http://init.test");
40   item_->SetURL(url);
41   EXPECT_TRUE(item_->GetURL().is_valid());
44 // Tests that copied NavigationItemImpls create copies of data members that are
45 // objects.
46 TEST_F(NavigationItemTest, Copy) {
47   // Create objects to be copied.
48   NSString* postData0 = @"postData0";
49   NSMutableData* mutablePostData =
50       [[postData0 dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
51   item_->SetPostData(mutablePostData);
52   NSString* state0 = @"state0";
53   NSMutableString* mutableState = [state0 mutableCopy];
54   item_->SetSerializedStateObject(mutableState);
56   // Create copy.
57   web::NavigationItemImpl copy(*item_.get());
59   // Modify the objects.
60   NSString* postData1 = @"postData1";
61   [mutablePostData setData:[postData1 dataUsingEncoding:NSUTF8StringEncoding]];
62   NSString* state1 = @"state1";
63   [mutableState setString:state1];
65   // Check that changes occurred in |item_|, but not in |copy|.
66   EXPECT_NSEQ([postData1 dataUsingEncoding:NSUTF8StringEncoding],
67               item_->GetPostData());
68   EXPECT_NSEQ(state1, item_->GetSerializedStateObject());
69   EXPECT_NSEQ([postData0 dataUsingEncoding:NSUTF8StringEncoding],
70               copy.GetPostData());
71   EXPECT_NSEQ(state0, copy.GetSerializedStateObject());
74 // Tests whether |NavigationItem::AddHttpRequestHeaders()| adds the passed
75 // headers to the item's request http headers.
76 TEST_F(NavigationItemTest, AddHttpRequestHeaders) {
77   EXPECT_NSEQ(@{kHTTPHeaderKey1 : kHTTPHeaderValue1},
78               item_->GetHttpRequestHeaders());
80   item_->AddHttpRequestHeaders(@{kHTTPHeaderKey1 : kHTTPHeaderValue2});
81   EXPECT_NSEQ(@{kHTTPHeaderKey1 : kHTTPHeaderValue2},
82               item_->GetHttpRequestHeaders());
84   item_->AddHttpRequestHeaders(@{kHTTPHeaderKey2 : kHTTPHeaderValue1});
85   NSDictionary* expected = @{
86     kHTTPHeaderKey1 : kHTTPHeaderValue2,
87     kHTTPHeaderKey2 : kHTTPHeaderValue1
88   };
89   EXPECT_NSEQ(expected, item_->GetHttpRequestHeaders());
92 // Tests whether |NavigationItem::AddHttpRequestHeaders()| removes the header
93 // value associated with the passed key from the item's request http headers.
94 TEST_F(NavigationItemTest, RemoveHttpRequestHeaderForKey) {
95   NSDictionary* httpHeaders = @{
96     kHTTPHeaderKey1 : kHTTPHeaderValue1,
97     kHTTPHeaderKey2 : kHTTPHeaderValue2
98   };
99   item_->AddHttpRequestHeaders(httpHeaders);
100   EXPECT_NSEQ(httpHeaders, item_->GetHttpRequestHeaders());
102   item_->RemoveHttpRequestHeaderForKey(kHTTPHeaderKey1);
103   EXPECT_NSEQ(@{kHTTPHeaderKey2 : kHTTPHeaderValue2},
104               item_->GetHttpRequestHeaders());
106   item_->RemoveHttpRequestHeaderForKey(kHTTPHeaderKey2);
107   EXPECT_FALSE(item_->GetHttpRequestHeaders());
110 }  // namespace
111 }  // namespace web