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]);
54 EXPECT_FALSE([details.get() shouldShowCacheStorageTreeDetailsView]);
57 TEST_F(CookiesDetailsTest, CreateForTreeDatabase) {
58 base::scoped_nsobject<CocoaCookieDetails> details;
59 GURL origin("http://chromium.org");
60 std::string database_name("sassolungo");
61 std::string description("a great place to climb");
63 base::Time last_modified = base::Time::Now();
64 BrowsingDataDatabaseHelper::DatabaseInfo info(
65 storage::DatabaseIdentifier::CreateFromOrigin(origin),
70 details.reset([[CocoaCookieDetails alloc] initWithDatabase:&info]);
72 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeDatabase);
73 EXPECT_NSEQ(@"a great place to climb", [details.get() databaseDescription]);
74 EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
75 EXPECT_NSNE(@"", [details.get() lastModified]);
77 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
78 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
79 EXPECT_TRUE([details.get() shouldShowDatabaseTreeDetailsView]);
80 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
81 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
82 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
83 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
84 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
85 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
86 EXPECT_FALSE([details.get() shouldShowCacheStorageTreeDetailsView]);
89 TEST_F(CookiesDetailsTest, CreateForTreeLocalStorage) {
90 base::scoped_nsobject<CocoaCookieDetails> details;
91 const GURL kOrigin("http://chromium.org/");
93 base::Time last_modified = base::Time::Now();
94 BrowsingDataLocalStorageHelper::LocalStorageInfo info(
95 kOrigin, size, last_modified);
96 details.reset([[CocoaCookieDetails alloc] initWithLocalStorage:&info]);
98 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeLocalStorage);
99 EXPECT_NSEQ(@"http://chromium.org/", [details.get() domain]);
100 EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
101 EXPECT_NSNE(@"", [details.get() lastModified]);
103 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
104 EXPECT_TRUE([details.get() shouldShowLocalStorageTreeDetailsView]);
105 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
106 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
107 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
108 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
109 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
110 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
111 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
112 EXPECT_FALSE([details.get() shouldShowCacheStorageTreeDetailsView]);
115 TEST_F(CookiesDetailsTest, CreateForTreeAppCache) {
116 base::scoped_nsobject<CocoaCookieDetails> details;
118 GURL url("http://chromium.org/stuff.manifest");
119 content::AppCacheInfo info;
120 info.creation_time = base::Time::Now();
121 info.last_update_time = base::Time::Now();
122 info.last_access_time = base::Time::Now();
124 info.manifest_url = url;
125 details.reset([[CocoaCookieDetails alloc] initWithAppCacheInfo:&info]);
127 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeAppCache);
128 EXPECT_NSEQ(@"http://chromium.org/stuff.manifest",
129 [details.get() manifestURL]);
130 EXPECT_NSEQ(@"2,678 B", [details.get() fileSize]);
131 EXPECT_NSNE(@"", [details.get() lastAccessed]);
132 EXPECT_NSNE(@"", [details.get() created]);
134 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
135 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
136 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
137 EXPECT_TRUE([details.get() shouldShowAppCacheTreeDetailsView]);
138 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
139 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
140 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
141 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
142 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
143 EXPECT_FALSE([details.get() shouldShowCacheStorageTreeDetailsView]);
146 TEST_F(CookiesDetailsTest, CreateForTreeIndexedDB) {
147 base::scoped_nsobject<CocoaCookieDetails> details;
149 GURL origin("http://moose.org/");
151 base::Time last_modified = base::Time::Now();
152 content::IndexedDBInfo info(origin,
157 details.reset([[CocoaCookieDetails alloc] initWithIndexedDBInfo:&info]);
159 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeIndexedDB);
160 EXPECT_NSEQ(@"http://moose.org/", [details.get() domain]);
161 EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
162 EXPECT_NSNE(@"", [details.get() lastModified]);
164 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
165 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
166 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
167 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
168 EXPECT_TRUE([details.get() shouldShowIndexedDBTreeDetailsView]);
169 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
170 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
171 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
172 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
173 EXPECT_FALSE([details.get() shouldShowCacheStorageTreeDetailsView]);
176 TEST_F(CookiesDetailsTest, CreateForPromptDatabase) {
177 base::scoped_nsobject<CocoaCookieDetails> details;
178 std::string domain("chromium.org");
179 base::string16 name(base::SysNSStringToUTF16(@"wicked_name"));
180 base::string16 desc(base::SysNSStringToUTF16(@"desc"));
181 details.reset([[CocoaCookieDetails alloc] initWithDatabase:domain
183 databaseDescription:desc
186 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptDatabase);
187 EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
188 EXPECT_NSEQ(@"wicked_name", [details.get() name]);
189 EXPECT_NSEQ(@"desc", [details.get() databaseDescription]);
190 EXPECT_NSEQ(@"94 B", [details.get() fileSize]);
192 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
193 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
194 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
195 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
196 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
197 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
198 EXPECT_TRUE([details.get() shouldShowDatabasePromptDetailsView]);
199 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
200 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
201 EXPECT_FALSE([details.get() shouldShowCacheStorageTreeDetailsView]);
204 TEST_F(CookiesDetailsTest, CreateForPromptLocalStorage) {
205 base::scoped_nsobject<CocoaCookieDetails> details;
206 std::string domain("chromium.org");
207 base::string16 key(base::SysNSStringToUTF16(@"testKey"));
208 base::string16 value(base::SysNSStringToUTF16(@"testValue"));
209 details.reset([[CocoaCookieDetails alloc] initWithLocalStorage:domain
213 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptLocalStorage);
214 EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
215 EXPECT_NSEQ(@"testKey", [details.get() localStorageKey]);
216 EXPECT_NSEQ(@"testValue", [details.get() localStorageValue]);
218 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
219 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
220 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
221 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
222 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
223 EXPECT_TRUE([details.get() shouldShowLocalStoragePromptDetailsView]);
224 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
225 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
226 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
227 EXPECT_FALSE([details.get() shouldShowCacheStorageTreeDetailsView]);
230 TEST_F(CookiesDetailsTest, CreateForPromptAppCache) {
231 base::scoped_nsobject<CocoaCookieDetails> details;
232 std::string manifestURL("http://html5demos.com/html5demo.manifest");
233 details.reset([[CocoaCookieDetails alloc]
234 initWithAppCacheManifestURL:manifestURL.c_str()]);
236 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptAppCache);
237 EXPECT_NSEQ(@"http://html5demos.com/html5demo.manifest",
238 [details.get() manifestURL]);
240 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
241 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
242 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
243 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
244 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
245 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
246 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
247 EXPECT_TRUE([details.get() shouldShowAppCachePromptDetailsView]);
248 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
249 EXPECT_FALSE([details.get() shouldShowCacheStorageTreeDetailsView]);
252 TEST_F(CookiesDetailsTest, CreateForTreeServiceWorker) {
253 base::scoped_nsobject<CocoaCookieDetails> details;
255 GURL origin("https://example.com/");
256 std::vector<GURL> scopes;
257 scopes.push_back(GURL("https://example.com/app1/*"));
258 scopes.push_back(GURL("https://example.com/app2/*"));
259 content::ServiceWorkerUsageInfo info(origin,
263 [[CocoaCookieDetails alloc] initWithServiceWorkerUsageInfo:&info]);
265 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeServiceWorker);
266 EXPECT_NSEQ(@"https://example.com/", [details.get() domain]);
267 EXPECT_NSEQ(@"https://example.com/app1/*,https://example.com/app2/*", [details.get() scopes]);
268 EXPECT_NSEQ(@"0 B", [details.get() fileSize]);
269 // TODO(jsbell): Plumb this through.
270 EXPECT_NSEQ(nil, [details.get() lastModified]);
272 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
273 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
274 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
275 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
276 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
277 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
278 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
279 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
280 EXPECT_TRUE([details.get() shouldShowServiceWorkerTreeDetailsView]);
281 EXPECT_FALSE([details.get() shouldShowCacheStorageTreeDetailsView]);
284 TEST_F(CookiesDetailsTest, CreateForTreeCacheStorage) {
285 base::scoped_nsobject<CocoaCookieDetails> details;
287 GURL origin("https://example.com/");
289 base::Time last_modified = base::Time::Now();
290 content::CacheStorageUsageInfo info(origin, size, last_modified);
293 [[CocoaCookieDetails alloc] initWithCacheStorageUsageInfo:&info]);
295 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeCacheStorage);
296 EXPECT_NSEQ(@"https://example.com/", [details.get() domain]);
297 EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
298 EXPECT_NSNE(@"", [details.get() lastModified]);
300 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
301 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
302 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
303 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
304 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
305 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
306 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
307 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
308 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
309 EXPECT_TRUE([details.get() shouldShowCacheStorageTreeDetailsView]);