Home | History | Annotate | Download | only in content_settings
      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 }
     54 
     55 TEST_F(CookiesDetailsTest, CreateForTreeDatabase) {
     56   base::scoped_nsobject<CocoaCookieDetails> details;
     57   GURL origin("http://chromium.org");
     58   std::string database_name("sassolungo");
     59   std::string description("a great place to climb");
     60   int64 size = 1234;
     61   base::Time last_modified = base::Time::Now();
     62   BrowsingDataDatabaseHelper::DatabaseInfo info(
     63       webkit_database::DatabaseIdentifier::CreateFromOrigin(origin),
     64       database_name, description, size, last_modified);
     65   details.reset([[CocoaCookieDetails alloc] initWithDatabase:&info]);
     66 
     67   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeDatabase);
     68   EXPECT_NSEQ(@"a great place to climb", [details.get() databaseDescription]);
     69   EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
     70   EXPECT_NSNE(@"", [details.get() lastModified]);
     71 
     72   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
     73   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
     74   EXPECT_TRUE([details.get() shouldShowDatabaseTreeDetailsView]);
     75   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
     76   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
     77   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
     78   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
     79   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
     80 }
     81 
     82 TEST_F(CookiesDetailsTest, CreateForTreeLocalStorage) {
     83   base::scoped_nsobject<CocoaCookieDetails> details;
     84   const GURL kOrigin("http://chromium.org/");
     85   int64 size = 1234;
     86   base::Time last_modified = base::Time::Now();
     87   BrowsingDataLocalStorageHelper::LocalStorageInfo info(
     88       kOrigin, size, last_modified);
     89   details.reset([[CocoaCookieDetails alloc] initWithLocalStorage:&info]);
     90 
     91   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeLocalStorage);
     92   EXPECT_NSEQ(@"http://chromium.org/", [details.get() domain]);
     93   EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
     94   EXPECT_NSNE(@"", [details.get() lastModified]);
     95 
     96   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
     97   EXPECT_TRUE([details.get() shouldShowLocalStorageTreeDetailsView]);
     98   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
     99   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
    100   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
    101   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
    102   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
    103   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
    104 }
    105 
    106 TEST_F(CookiesDetailsTest, CreateForTreeAppCache) {
    107   base::scoped_nsobject<CocoaCookieDetails> details;
    108 
    109   GURL url("http://chromium.org/stuff.manifest");
    110   appcache::AppCacheInfo info;
    111   info.creation_time = base::Time::Now();
    112   info.last_update_time = base::Time::Now();
    113   info.last_access_time = base::Time::Now();
    114   info.size=2678;
    115   info.manifest_url = url;
    116   details.reset([[CocoaCookieDetails alloc] initWithAppCacheInfo:&info]);
    117 
    118   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeAppCache);
    119   EXPECT_NSEQ(@"http://chromium.org/stuff.manifest",
    120               [details.get() manifestURL]);
    121   EXPECT_NSEQ(@"2,678 B", [details.get() fileSize]);
    122   EXPECT_NSNE(@"", [details.get() lastAccessed]);
    123   EXPECT_NSNE(@"", [details.get() created]);
    124 
    125   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
    126   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
    127   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
    128   EXPECT_TRUE([details.get() shouldShowAppCacheTreeDetailsView]);
    129   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
    130   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
    131   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
    132   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
    133 }
    134 
    135 TEST_F(CookiesDetailsTest, CreateForTreeIndexedDB) {
    136   base::scoped_nsobject<CocoaCookieDetails> details;
    137 
    138   GURL origin("http://moose.org/");
    139   int64 size = 1234;
    140   base::Time last_modified = base::Time::Now();
    141   base::FilePath file_path;
    142   content::IndexedDBInfo info(origin,
    143                               size,
    144                               last_modified,
    145                               file_path,
    146                               0);
    147 
    148   details.reset([[CocoaCookieDetails alloc] initWithIndexedDBInfo:&info]);
    149 
    150   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypeTreeIndexedDB);
    151   EXPECT_NSEQ(@"http://moose.org/", [details.get() domain]);
    152   EXPECT_NSEQ(@"1,234 B", [details.get() fileSize]);
    153   EXPECT_NSNE(@"", [details.get() lastModified]);
    154 
    155   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
    156   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
    157   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
    158   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
    159   EXPECT_TRUE([details.get() shouldShowIndexedDBTreeDetailsView]);
    160   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
    161   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
    162   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
    163 }
    164 
    165 TEST_F(CookiesDetailsTest, CreateForPromptDatabase) {
    166   base::scoped_nsobject<CocoaCookieDetails> details;
    167   std::string domain("chromium.org");
    168   string16 name(base::SysNSStringToUTF16(@"wicked_name"));
    169   string16 desc(base::SysNSStringToUTF16(@"desc"));
    170   details.reset([[CocoaCookieDetails alloc] initWithDatabase:domain
    171                                                 databaseName:name
    172                                          databaseDescription:desc
    173                                                     fileSize:94]);
    174 
    175   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptDatabase);
    176   EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
    177   EXPECT_NSEQ(@"wicked_name", [details.get() name]);
    178   EXPECT_NSEQ(@"desc", [details.get() databaseDescription]);
    179   EXPECT_NSEQ(@"94 B", [details.get() fileSize]);
    180 
    181   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
    182   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
    183   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
    184   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
    185   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
    186   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
    187   EXPECT_TRUE([details.get() shouldShowDatabasePromptDetailsView]);
    188   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
    189 }
    190 
    191 TEST_F(CookiesDetailsTest, CreateForPromptLocalStorage) {
    192   base::scoped_nsobject<CocoaCookieDetails> details;
    193   std::string domain("chromium.org");
    194   string16 key(base::SysNSStringToUTF16(@"testKey"));
    195   string16 value(base::SysNSStringToUTF16(@"testValue"));
    196   details.reset([[CocoaCookieDetails alloc] initWithLocalStorage:domain
    197                                                              key:key
    198                                                            value:value]);
    199 
    200   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptLocalStorage);
    201   EXPECT_NSEQ(@"chromium.org", [details.get() domain]);
    202   EXPECT_NSEQ(@"testKey", [details.get() localStorageKey]);
    203   EXPECT_NSEQ(@"testValue", [details.get() localStorageValue]);
    204 
    205   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
    206   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
    207   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
    208   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
    209   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
    210   EXPECT_TRUE([details.get() shouldShowLocalStoragePromptDetailsView]);
    211   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
    212   EXPECT_FALSE([details.get() shouldShowAppCachePromptDetailsView]);
    213 }
    214 
    215 TEST_F(CookiesDetailsTest, CreateForPromptAppCache) {
    216   base::scoped_nsobject<CocoaCookieDetails> details;
    217   std::string manifestURL("http://html5demos.com/html5demo.manifest");
    218   details.reset([[CocoaCookieDetails alloc]
    219       initWithAppCacheManifestURL:manifestURL.c_str()]);
    220 
    221   EXPECT_EQ([details.get() type], kCocoaCookieDetailsTypePromptAppCache);
    222   EXPECT_NSEQ(@"http://html5demos.com/html5demo.manifest",
    223               [details.get() manifestURL]);
    224 
    225   EXPECT_TRUE([details.get() shouldHideCookieDetailsView]);
    226   EXPECT_FALSE([details.get() shouldShowLocalStorageTreeDetailsView]);
    227   EXPECT_FALSE([details.get() shouldShowDatabaseTreeDetailsView]);
    228   EXPECT_FALSE([details.get() shouldShowAppCacheTreeDetailsView]);
    229   EXPECT_FALSE([details.get() shouldShowIndexedDBTreeDetailsView]);
    230   EXPECT_FALSE([details.get() shouldShowLocalStoragePromptDetailsView]);
    231   EXPECT_FALSE([details.get() shouldShowDatabasePromptDetailsView]);
    232   EXPECT_TRUE([details.get() shouldShowAppCachePromptDetailsView]);
    233 }
    234 
    235 }
    236