Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / content_settings / cookie_details_unittest.mm
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.
4
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"
11 #include "url/gurl.h"
12
13 namespace {
14
15 class CookiesDetailsTest : public CocoaTest {
16 };
17
18 TEST_F(CookiesDetailsTest, CreateForFolder) {
19   base::scoped_nsobject<CocoaCookieDetails> details;
20   details.reset([[CocoaCookieDetails alloc] initAsFolder]);
21
22   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeFolder);
23 }
24
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]);
34
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]);
44
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 }
55
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");
61   int64 size = 1234;
62   base::Time last_modified = base::Time::Now();
63   BrowsingDataDatabaseHelper::DatabaseInfo info(
64       webkit_database::DatabaseIdentifier::CreateFromOrigin(origin),
65       database_name, description, size, last_modified);
66   details.reset([[CocoaCookieDetails alloc] initWithDatabase:&info]);
67
68   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeDatabase);
69   EXPECT_NSEQ(@"a great place to climb", [details.get() databaseDescription]);
70   EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
71   EXPECT_NSNE(@"", [details.get() lastModified]);
72
73   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
74   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
75   EXPECT_TRUE([details.get() shouldShowDatabaseTreeDetailsView]);
76   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
77   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
78   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
79   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
80   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
81   EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
82 }
83
84 TEST_F(CookiesDetailsTest, CreateForTreeLocalStorage) {
85   base::scoped_nsobject<CocoaCookieDetails> details;
86   const GURL kOrigin("http://chromium.org/");
87   int64 size = 1234;
88   base::Time last_modified = base::Time::Now();
89   BrowsingDataLocalStorageHelper::LocalStorageInfo info(
90       kOrigin, size, last_modified);
91   details.reset([[CocoaCookieDetails alloc] initWithLocalStorage:&info]);
92
93   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeLocalStorage);
94   EXPECT_NSEQ(@"http://chromium.org/", [details.get() domain]);
95   EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
96   EXPECT_NSNE(@"", [details.get() lastModified]);
97
98   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
99   EXPECT_TRUE([details.get() shouldShowLocalStorageTreeDetailsView]);
100   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
101   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
102   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
103   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
104   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
105   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
106   EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
107 }
108
109 TEST_F(CookiesDetailsTest, CreateForTreeAppCache) {
110   base::scoped_nsobject<CocoaCookieDetails> details;
111
112   GURL url("http://chromium.org/stuff.manifest");
113   content::AppCacheInfo info;
114   info.creation_time = base::Time::Now();
115   info.last_update_time = base::Time::Now();
116   info.last_access_time = base::Time::Now();
117   info.size=2678;
118   info.manifest_url = url;
119   details.reset([[CocoaCookieDetails alloc] initWithAppCacheInfo:&info]);
120
121   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeAppCache);
122   EXPECT_NSEQ(@"http://chromium.org/stuff.manifest",
123               [details.get() manifestURL]);
124   EXPECT_NSEQ(@"2,678 B", [details.get() fileSize]);
125   EXPECT_NSNE(@"", [details.get() lastAccessed]);
126   EXPECT_NSNE(@"", [details.get() created]);
127
128   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
129   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
130   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
131   EXPECT_TRUE([details.get() shouldShowAppCacheTreeDetailsView]);
132   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
133   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
134   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
135   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
136   EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
137 }
138
139 TEST_F(CookiesDetailsTest, CreateForTreeIndexedDB) {
140   base::scoped_nsobject<CocoaCookieDetails> details;
141
142   GURL origin("http://moose.org/");
143   int64 size = 1234;
144   base::Time last_modified = base::Time::Now();
145   base::FilePath file_path;
146   content::IndexedDBInfo info(origin,
147                               size,
148                               last_modified,
149                               file_path,
150                               0);
151
152   details.reset([[CocoaCookieDetails alloc] initWithIndexedDBInfo:&info]);
153
154   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeIndexedDB);
155   EXPECT_NSEQ(@"http://moose.org/", [details.get() domain]);
156   EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
157   EXPECT_NSNE(@"", [details.get() lastModified]);
158
159   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
160   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
161   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
162   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
163   EXPECT_TRUE([details.get() shouldShowIndexedDBTreeDetailsView]);
164   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
165   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
166   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
167   EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
168 }
169
170 TEST_F(CookiesDetailsTest, CreateForPromptDatabase) {
171   base::scoped_nsobject<CocoaCookieDetails> details;
172   std::string domain("chromium.org");
173   base::string16 name(base::SysNSStringToUTF16(@"wicked_name"));
174   base::string16 desc(base::SysNSStringToUTF16(@"desc"));
175   details.reset([[CocoaCookieDetails alloc] initWithDatabase:domain
176                                                 databaseName:name
177                                          databaseDescription:desc
178                                                     fileSize:94]);
179
180   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptDatabase);
181   EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
182   EXPECT_NSEQ(@"wicked_name", [details.get() name]);
183   EXPECT_NSEQ(@"desc", [details.get() databaseDescription]);
184   EXPECT_NSEQ(@"94 B", [details.get() fileSize]);
185
186   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
187   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
188   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
189   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
190   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
191   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
192   EXPECT_TRUE([details.get() shouldShowDatabasePromptDetailsView]);
193   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
194   EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
195 }
196
197 TEST_F(CookiesDetailsTest, CreateForPromptLocalStorage) {
198   base::scoped_nsobject<CocoaCookieDetails> details;
199   std::string domain("chromium.org");
200   base::string16 key(base::SysNSStringToUTF16(@"testKey"));
201   base::string16 value(base::SysNSStringToUTF16(@"testValue"));
202   details.reset([[CocoaCookieDetails alloc] initWithLocalStorage:domain
203                                                              key:key
204                                                            value:value]);
205
206   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptLocalStorage);
207   EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
208   EXPECT_NSEQ(@"testKey", [details.get() localStorageKey]);
209   EXPECT_NSEQ(@"testValue", [details.get() localStorageValue]);
210
211   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
212   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
213   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
214   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
215   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
216   EXPECT_TRUE([details.get() shouldShowLocalStoragePromptDetailsView]);
217   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
218   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
219   EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
220 }
221
222 TEST_F(CookiesDetailsTest, CreateForPromptAppCache) {
223   base::scoped_nsobject<CocoaCookieDetails> details;
224   std::string manifestURL("http://html5demos.com/html5demo.manifest");
225   details.reset([[CocoaCookieDetails alloc]
226       initWithAppCacheManifestURL:manifestURL.c_str()]);
227
228   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptAppCache);
229   EXPECT_NSEQ(@"http://html5demos.com/html5demo.manifest",
230               [details.get() manifestURL]);
231
232   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
233   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
234   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
235   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
236   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
237   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
238   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
239   EXPECT_TRUE([details.get() shouldShowAppCachePromptDetailsView]);
240   EXPECT_FALSE([details.get() shouldShowServiceWorkerTreeDetailsView]);
241 }
242
243 TEST_F(CookiesDetailsTest, CreateForTreeServiceWorker) {
244   base::scoped_nsobject<CocoaCookieDetails> details;
245
246   GURL origin("https://example.com/");
247   std::vector<GURL> scopes;
248   scopes.push_back(GURL("https://example.com/app1/*"));
249   scopes.push_back(GURL("https://example.com/app2/*"));
250   content::ServiceWorkerUsageInfo info(origin,
251                                        scopes);
252
253   details.reset(
254       [[CocoaCookieDetails alloc] initWithServiceWorkerUsageInfo:&info]);
255
256   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeServiceWorker);
257   EXPECT_NSEQ(@"https://example.com/", [details.get() domain]);
258   EXPECT_NSEQ(@"https://example.com/app1/*,https://example.com/app2/*", [details.get() scopes]);
259   // TODO(jsbell): Plumb these through.
260   EXPECT_NSEQ(nil, [details.get() fileSize]);
261   EXPECT_NSEQ(nil, [details.get() lastModified]);
262
263   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
264   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
265   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
266   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
267   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
268   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
269   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
270   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
271   EXPECT_TRUE([details.get() shouldShowServiceWorkerTreeDetailsView]);
272 }
273 }