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 content::IndexedDBInfo info(origin,
153 details.reset([[CocoaCookieDetails alloc] initWithIndexedDBInfo:&info]);
155 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeIndexedDB);
156 EXPECT_NSEQ(@"http://moose.org/", [details.get() domain]);
157 EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
158 EXPECT_NSNE(@"", [details.get() lastModified]);
160 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
161 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
162 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
163 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
164 EXPECT_TRUE([details.get() shouldShowIndexedDBTreeDetailsView]);
165 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
166 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
167 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
168 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
171 TEST_F(CookiesDetailsTest, CreateForPromptDatabase) {
172 base::scoped_nsobject<CocoaCookieDetails> details;
173 std::string domain("chromium.org");
174 base::string16 name(base::SysNSStringToUTF16(@"wicked_name"));
175 base::string16 desc(base::SysNSStringToUTF16(@"desc"));
176 details.reset([[CocoaCookieDetails alloc] initWithDatabase:domain
178 databaseDescription:desc
181 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptDatabase);
182 EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
183 EXPECT_NSEQ(@"wicked_name", [details.get() name]);
184 EXPECT_NSEQ(@"desc", [details.get() databaseDescription]);
185 EXPECT_NSEQ(@"94 B", [details.get() fileSize]);
187 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
188 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
189 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
190 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
191 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
192 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
193 EXPECT_TRUE([details.get() shouldShowDatabasePromptDetailsView]);
194 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
195 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
198 TEST_F(CookiesDetailsTest, CreateForPromptLocalStorage) {
199 base::scoped_nsobject<CocoaCookieDetails> details;
200 std::string domain("chromium.org");
201 base::string16 key(base::SysNSStringToUTF16(@"testKey"));
202 base::string16 value(base::SysNSStringToUTF16(@"testValue"));
203 details.reset([[CocoaCookieDetails alloc] initWithLocalStorage:domain
207 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptLocalStorage);
208 EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
209 EXPECT_NSEQ(@"testKey", [details.get() localStorageKey]);
210 EXPECT_NSEQ(@"testValue", [details.get() localStorageValue]);
212 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
213 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
214 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
215 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
216 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
217 EXPECT_TRUE([details.get() shouldShowLocalStoragePromptDetailsView]);
218 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
219 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
220 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
223 TEST_F(CookiesDetailsTest, CreateForPromptAppCache) {
224 base::scoped_nsobject<CocoaCookieDetails> details;
225 std::string manifestURL("http://html5demos.com/html5demo.manifest");
226 details.reset([[CocoaCookieDetails alloc]
227 initWithAppCacheManifestURL:manifestURL.c_str()]);
229 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptAppCache);
230 EXPECT_NSEQ(@"http://html5demos.com/html5demo.manifest",
231 [details.get() manifestURL]);
233 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
234 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
235 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
236 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
237 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
238 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
239 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
240 EXPECT_TRUE([details.get() shouldShowAppCachePromptDetailsView]);
241 EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
244 TEST_F(CookiesDetailsTest, CreateForTreeServiceWorker) {
245 base::scoped_nsobject<CocoaCookieDetails> details;
247 GURL origin("https://example.com/");
248 std::vector<GURL> scopes;
249 scopes.push_back(GURL("https://example.com/app1/*"));
250 scopes.push_back(GURL("https://example.com/app2/*"));
251 content::ServiceWorkerUsageInfo info(origin,
255 [[CocoaCookieDetails alloc] initWithServiceWorkerUsageInfo:&info]);
257 EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeServiceWorker);
258 EXPECT_NSEQ(@"https://example.com/", [details.get() domain]);
259 EXPECT_NSEQ(@"https://example.com/app1/*,https://example.com/app2/*", [details.get() scopes]);
260 EXPECT_NSEQ(@"0 B", [details.get() fileSize]);
261 // TODO(jsbell): Plumb this through.
262 EXPECT_NSEQ(nil, [details.get() lastModified]);
264 EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
265 EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
266 EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
267 EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
268 EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
269 EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
270 EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
271 EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
272 EXPECT_TRUE([details.get() shouldShowServiceWorkerTreeDetailsView]);