summaryrefslogtreecommitdiffstats
path: root/chrome/browser/tab_contents/navigation_entry_unittest.cc
blob: 4ecd1df75f317a1e0d697ea683769fbdbc2ea5d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2010 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.

#include "base/string16.h"
#include "base/string_util.h"
#include "chrome/browser/renderer_host/site_instance.h"
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "testing/gtest/include/gtest/gtest.h"

class NavigationEntryTest : public testing::Test {
 public:
  NavigationEntryTest() : instance_(NULL) {
  }

  virtual void SetUp() {
    entry1_.reset(new NavigationEntry);

    instance_ = SiteInstance::CreateSiteInstance(NULL);
    entry2_.reset(new NavigationEntry(instance_, 3,
                                      GURL("test:url"),
                                      GURL("from"),
                                      ASCIIToUTF16("title"),
                                      PageTransition::TYPED));
  }

  virtual void TearDown() {
  }

 protected:
  scoped_ptr<NavigationEntry> entry1_;
  scoped_ptr<NavigationEntry> entry2_;
  // SiteInstances are deleted when their NavigationEntries are gone.
  SiteInstance* instance_;
};

// Test unique ID accessors
TEST_F(NavigationEntryTest, NavigationEntryUniqueIDs) {
  // Two entries should have different IDs by default
  EXPECT_NE(entry1_.get()->unique_id(), entry2_.get()->unique_id());

  // Can set an entry to have the same ID as another
  entry2_.get()->set_unique_id(entry1_.get()->unique_id());
  EXPECT_EQ(entry1_.get()->unique_id(), entry2_.get()->unique_id());
}

// Test URL accessors
TEST_F(NavigationEntryTest, NavigationEntryURLs) {
  // Start with no virtual_url (even if a url is set)
  EXPECT_FALSE(entry1_.get()->has_virtual_url());
  EXPECT_FALSE(entry2_.get()->has_virtual_url());

  EXPECT_EQ(GURL(), entry1_.get()->url());
  EXPECT_EQ(GURL(), entry1_.get()->virtual_url());
  EXPECT_TRUE(entry1_.get()->GetTitleForDisplay(NULL).empty());

  // Setting URL affects virtual_url and GetTitleForDisplay
  entry1_.get()->set_url(GURL("http://www.google.com"));
  EXPECT_EQ(GURL("http://www.google.com"), entry1_.get()->url());
  EXPECT_EQ(GURL("http://www.google.com"), entry1_.get()->virtual_url());
  EXPECT_EQ(ASCIIToUTF16("www.google.com"),
            entry1_.get()->GetTitleForDisplay(NULL));

  // Title affects GetTitleForDisplay
  entry1_.get()->set_title(ASCIIToUTF16("Google"));
  EXPECT_EQ(ASCIIToUTF16("Google"), entry1_.get()->GetTitleForDisplay(NULL));

  // Setting virtual_url doesn't affect URL
  entry2_.get()->set_virtual_url(GURL("display:url"));
  EXPECT_TRUE(entry2_.get()->has_virtual_url());
  EXPECT_EQ(GURL("test:url"), entry2_.get()->url());
  EXPECT_EQ(GURL("display:url"), entry2_.get()->virtual_url());

  // Having a title set in constructor overrides virtual URL
  EXPECT_EQ(ASCIIToUTF16("title"), entry2_.get()->GetTitleForDisplay(NULL));

  // User typed URL is independent of the others
  EXPECT_EQ(GURL(), entry1_.get()->user_typed_url());
  EXPECT_EQ(GURL(), entry2_.get()->user_typed_url());
  entry2_.get()->set_user_typed_url(GURL("typedurl"));
  EXPECT_EQ(GURL("typedurl"), entry2_.get()->user_typed_url());
}

// Test Favicon inner class
TEST_F(NavigationEntryTest, NavigationEntryFavicons) {
  EXPECT_EQ(GURL(), entry1_.get()->favicon().url());
  entry1_.get()->favicon().set_url(GURL("icon"));
  EXPECT_EQ(GURL("icon"), entry1_.get()->favicon().url());

  // Validity not affected by setting URL
  EXPECT_FALSE(entry1_.get()->favicon().is_valid());
  entry1_.get()->favicon().set_is_valid(true);
  EXPECT_TRUE(entry1_.get()->favicon().is_valid());
}

// Test SSLStatus inner class
TEST_F(NavigationEntryTest, NavigationEntrySSLStatus) {
  // Default (not secure)
  EXPECT_EQ(SECURITY_STYLE_UNKNOWN, entry1_.get()->ssl().security_style());
  EXPECT_EQ(SECURITY_STYLE_UNKNOWN, entry2_.get()->ssl().security_style());
  EXPECT_EQ(0, entry1_.get()->ssl().cert_id());
  EXPECT_EQ(0, entry1_.get()->ssl().cert_status());
  EXPECT_EQ(-1, entry1_.get()->ssl().security_bits());
  EXPECT_FALSE(entry1_.get()->ssl().displayed_insecure_content());
  EXPECT_FALSE(entry1_.get()->ssl().ran_insecure_content());

  // Change from the defaults
  entry2_.get()->ssl().set_security_style(SECURITY_STYLE_AUTHENTICATED);
  entry2_.get()->ssl().set_cert_id(4);
  entry2_.get()->ssl().set_cert_status(1);
  entry2_.get()->ssl().set_security_bits(0);
  entry2_.get()->ssl().set_displayed_insecure_content();
  EXPECT_EQ(SECURITY_STYLE_AUTHENTICATED,
            entry2_.get()->ssl().security_style());
  EXPECT_EQ(4, entry2_.get()->ssl().cert_id());
  EXPECT_EQ(1, entry2_.get()->ssl().cert_status());
  EXPECT_EQ(0, entry2_.get()->ssl().security_bits());
  EXPECT_TRUE(entry2_.get()->ssl().displayed_insecure_content());

  entry2_.get()->ssl().set_security_style(SECURITY_STYLE_AUTHENTICATION_BROKEN);
  entry2_.get()->ssl().set_ran_insecure_content();
  EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN,
            entry2_.get()->ssl().security_style());
  EXPECT_TRUE(entry2_.get()->ssl().ran_insecure_content());
}

// Test other basic accessors
TEST_F(NavigationEntryTest, NavigationEntryAccessors) {
  // SiteInstance
  EXPECT_TRUE(entry1_.get()->site_instance() == NULL);
  EXPECT_EQ(instance_, entry2_.get()->site_instance());
  entry1_.get()->set_site_instance(instance_);
  EXPECT_EQ(instance_, entry1_.get()->site_instance());

  // Page type
  EXPECT_EQ(NavigationEntry::NORMAL_PAGE, entry1_.get()->page_type());
  EXPECT_EQ(NavigationEntry::NORMAL_PAGE, entry2_.get()->page_type());
  entry2_.get()->set_page_type(NavigationEntry::INTERSTITIAL_PAGE);
  EXPECT_EQ(NavigationEntry::INTERSTITIAL_PAGE, entry2_.get()->page_type());

  // Referrer
  EXPECT_EQ(GURL(), entry1_.get()->referrer());
  EXPECT_EQ(GURL("from"), entry2_.get()->referrer());
  entry2_.get()->set_referrer(GURL("from2"));
  EXPECT_EQ(GURL("from2"), entry2_.get()->referrer());

  // Title
  EXPECT_EQ(string16(), entry1_.get()->title());
  EXPECT_EQ(ASCIIToUTF16("title"), entry2_.get()->title());
  entry2_.get()->set_title(ASCIIToUTF16("title2"));
  EXPECT_EQ(ASCIIToUTF16("title2"), entry2_.get()->title());

  // State
  EXPECT_EQ(std::string(), entry1_.get()->content_state());
  EXPECT_EQ(std::string(), entry2_.get()->content_state());
  entry2_.get()->set_content_state("state");
  EXPECT_EQ("state", entry2_.get()->content_state());

  // Page ID
  EXPECT_EQ(-1, entry1_.get()->page_id());
  EXPECT_EQ(3, entry2_.get()->page_id());
  entry2_.get()->set_page_id(2);
  EXPECT_EQ(2, entry2_.get()->page_id());

  // Transition type
  EXPECT_EQ(PageTransition::LINK, entry1_.get()->transition_type());
  EXPECT_EQ(PageTransition::TYPED, entry2_.get()->transition_type());
  entry2_.get()->set_transition_type(PageTransition::RELOAD);
  EXPECT_EQ(PageTransition::RELOAD, entry2_.get()->transition_type());

  // Post Data
  EXPECT_FALSE(entry1_.get()->has_post_data());
  EXPECT_FALSE(entry2_.get()->has_post_data());
  entry2_.get()->set_has_post_data(true);
  EXPECT_TRUE(entry2_.get()->has_post_data());

  // Restored
  EXPECT_EQ(NavigationEntry::RESTORE_NONE, entry1_->restore_type());
  EXPECT_EQ(NavigationEntry::RESTORE_NONE, entry2_->restore_type());
  entry2_->set_restore_type(NavigationEntry::RESTORE_LAST_SESSION);
  EXPECT_EQ(NavigationEntry::RESTORE_LAST_SESSION, entry2_->restore_type());
}