1 // Copyright (c) 2012 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/strings/sys_string_conversions.h"
6 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
7 #include "chrome/browser/ui/cocoa/content_settings/cookie_details.h"
8 #include "net/cookies/canonical_cookie.h"
9 #include "net/cookies/parsed_cookie.h"
10 #import "testing/gtest_mac.h"
15 class CookiesDetailsTest : public CocoaTest {
18 TEST_F(CookiesDetailsTest, CreateForFolder) {
19 base::scoped_nsobject<CocoaCookieDetails> details;
20 details.reset([[CocoaCookieDetails alloc] initAsFolder]);
22 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeFolder);
25 TEST_F(CookiesDetailsTest, CreateForCookie) {
26 base::scoped_nsobject<CocoaCookieDetails> details;
27 GURL url("http://chromium.org");
28 std::string cookieLine(
29 "PHPSESSID=0123456789abcdef0123456789abcdef; path=/");
30 net::ParsedCookie pc(cookieLine);
31 net::CanonicalCookie cookie(url, pc);
32 details.reset([[CocoaCookieDetails alloc] initWithCookie:&cookie
33 canEditExpiration:NO]);
35 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeCookie);
36 EXPECT_NSEQ(@"PHPSESSID", [details.get() name]);
37 EXPECT_NSEQ(@"0123456789abcdef0123456789abcdef",
38 [details.get() content]);
39 EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
40 EXPECT_NSEQ(@"/", [details.get() path]);
41 EXPECT_NSNE(@"", [details.get() lastModified]);
42 EXPECT_NSNE(@"", [details.get() created]);
43 EXPECT_NSNE(@"", [details.get() sendFor]);
45 EXPECT_FALSE([details.get() shouldHideCookieDetailsView]);
46 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
47 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
48 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
49 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
50 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
51 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
52 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
53 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
56 TEST_F(CookiesDetailsTest, CreateForTreeDatabase) {
57 base::scoped_nsobject<CocoaCookieDetails> details;
58 GURL origin("http://chromium.org");
59 std::string database_name("sassolungo");
60 std::string description("a great place to climb");
62 base::Time last_modified = base::Time::Now();
63 BrowsingDataDatabaseHelper::DatabaseInfo info(
64 storage::DatabaseIdentifier::CreateFromOrigin(origin),
69 details.reset([[CocoaCookieDetails alloc] initWithDatabase:&info]);
71 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeDatabase);
72 EXPECT_NSEQ(@"a great place to climb", [details.get() databaseDescription]);
73 EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
74 EXPECT_NSNE(@"", [details.get() lastModified]);
76 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
77 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
78 EXPECT_TRUE([details.get() shouldShowDatabaseTreeDetailsView]);
79 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
80 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
81 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
82 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
83 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
84 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
87 TEST_F(CookiesDetailsTest, CreateForTreeLocalStorage) {
88 base::scoped_nsobject<CocoaCookieDetails> details;
89 const GURL kOrigin("http://chromium.org/");
91 base::Time last_modified = base::Time::Now();
92 BrowsingDataLocalStorageHelper::LocalStorageInfo info(
93 kOrigin, size, last_modified);
94 details.reset([[CocoaCookieDetails alloc] initWithLocalStorage:&info]);
96 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeLocalStorage);
97 EXPECT_NSEQ(@"http://chromium.org/", [details.get() domain]);
98 EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
99 EXPECT_NSNE(@"", [details.get() lastModified]);
101 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
102 EXPECT_TRUE([details.get() shouldShowLocalStorageTreeDetailsView]);
103 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
104 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
105 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
106 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
107 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
108 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
109 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
112 TEST_F(CookiesDetailsTest, CreateForTreeAppCache) {
113 base::scoped_nsobject<CocoaCookieDetails> details;
115 GURL url("http://chromium.org/stuff.manifest");
116 content::AppCacheInfo info;
117 info.creation_time = base::Time::Now();
118 info.last_update_time = base::Time::Now();
119 info.last_access_time = base::Time::Now();
121 info.manifest_url = url;
122 details.reset([[CocoaCookieDetails alloc] initWithAppCacheInfo:&info]);
124 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeAppCache);
125 EXPECT_NSEQ(@"http://chromium.org/stuff.manifest",
126 [details.get() manifestURL]);
127 EXPECT_NSEQ(@"2,678 B", [details.get() fileSize]);
128 EXPECT_NSNE(@"", [details.get() lastAccessed]);
129 EXPECT_NSNE(@"", [details.get() created]);
131 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
132 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
133 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
134 EXPECT_TRUE([details.get() shouldShowAppCacheTreeDetailsView]);
135 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
136 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
137 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
138 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
139 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
142 TEST_F(CookiesDetailsTest, CreateForTreeIndexedDB) {
143 base::scoped_nsobject<CocoaCookieDetails> details;
145 GURL origin("http://moose.org/");
147 base::Time last_modified = base::Time::Now();
148 base::FilePath file_path;
149 content::IndexedDBInfo info(origin,
155 details.reset([[CocoaCookieDetails alloc] initWithIndexedDBInfo:&info]);
157 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeIndexedDB);
158 EXPECT_NSEQ(@"http://moose.org/", [details.get() domain]);
159 EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
160 EXPECT_NSNE(@"", [details.get() lastModified]);
162 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
163 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
164 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
165 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
166 EXPECT_TRUE([details.get() shouldShowIndexedDBTreeDetailsView]);
167 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
168 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
169 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
170 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
173 TEST_F(CookiesDetailsTest, CreateForPromptDatabase) {
174 base::scoped_nsobject<CocoaCookieDetails> details;
175 std::string domain("chromium.org");
176 base::string16 name(base::SysNSStringToUTF16(@"wicked_name"));
177 base::string16 desc(base::SysNSStringToUTF16(@"desc"));
178 details.reset([[CocoaCookieDetails alloc] initWithDatabase:domain
180 databaseDescription:desc
183 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptDatabase);
184 EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
185 EXPECT_NSEQ(@"wicked_name", [details.get() name]);
186 EXPECT_NSEQ(@"desc", [details.get() databaseDescription]);
187 EXPECT_NSEQ(@"94 B", [details.get() fileSize]);
189 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
190 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
191 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
192 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
193 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
194 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
195 EXPECT_TRUE([details.get() shouldShowDatabasePromptDetailsView]);
196 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
197 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
200 TEST_F(CookiesDetailsTest, CreateForPromptLocalStorage) {
201 base::scoped_nsobject<CocoaCookieDetails> details;
202 std::string domain("chromium.org");
203 base::string16 key(base::SysNSStringToUTF16(@"testKey"));
204 base::string16 value(base::SysNSStringToUTF16(@"testValue"));
205 details.reset([[CocoaCookieDetails alloc] initWithLocalStorage:domain
209 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptLocalStorage);
210 EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
211 EXPECT_NSEQ(@"testKey", [details.get() localStorageKey]);
212 EXPECT_NSEQ(@"testValue", [details.get() localStorageValue]);
214 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
215 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
216 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
217 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
218 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
219 EXPECT_TRUE([details.get() shouldShowLocalStoragePromptDetailsView]);
220 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
221 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
222 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
225 TEST_F(CookiesDetailsTest, CreateForPromptAppCache) {
226 base::scoped_nsobject<CocoaCookieDetails> details;
227 std::string manifestURL("http://html5demos.com/html5demo.manifest");
228 details.reset([[CocoaCookieDetails alloc]
229 initWithAppCacheManifestURL:manifestURL.c_str()]);
231 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptAppCache);
232 EXPECT_NSEQ(@"http://html5demos.com/html5demo.manifest",
233 [details.get() manifestURL]);
235 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
236 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
237 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
238 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
239 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
240 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
241 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
242 EXPECT_TRUE([details.get() shouldShowAppCachePromptDetailsView]);
243 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
246 TEST_F(CookiesDetailsTest, CreateForTreeServiceWorker) {
247 base::scoped_nsobject<CocoaCookieDetails> details;
249 GURL origin("https://example.com/");
250 std::vector<GURL> scopes;
251 scopes.push_back(GURL("https://example.com/app1/*"));
252 scopes.push_back(GURL("https://example.com/app2/*"));
253 content::ServiceWorkerUsageInfo info(origin,
257 [[CocoaCookieDetails alloc] initWithServiceWorkerUsageInfo:&info]);
259 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeServiceWorker);
260 EXPECT_NSEQ(@"https://example.com/", [details.get() domain]);
261 EXPECT_NSEQ(@"https://example.com/app1/*,https://example.com/app2/*", [details.get() scopes]);
262 // TODO(jsbell): Plumb these through.
263 EXPECT_NSEQ(nil, [details.get() fileSize]);
264 EXPECT_NSEQ(nil, [details.get() lastModified]);
266 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
267 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
268 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
269 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
270 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
271 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
272 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
273 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
274 EXPECT_TRUE([details.get() shouldShowServiceWorkerTreeDetailsView]);