Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / browsing_data / cookies_tree_model_unittest.cc
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 "chrome/browser/browsing_data/cookies_tree_model.h"
6
7 #include <string>
8
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/browsing_data/mock_browsing_data_appcache_helper.h"
12 #include "chrome/browser/browsing_data/mock_browsing_data_cookie_helper.h"
13 #include "chrome/browser/browsing_data/mock_browsing_data_database_helper.h"
14 #include "chrome/browser/browsing_data/mock_browsing_data_file_system_helper.h"
15 #include "chrome/browser/browsing_data/mock_browsing_data_flash_lso_helper.h"
16 #include "chrome/browser/browsing_data/mock_browsing_data_indexed_db_helper.h"
17 #include "chrome/browser/browsing_data/mock_browsing_data_local_storage_helper.h"
18 #include "chrome/browser/browsing_data/mock_browsing_data_quota_helper.h"
19 #include "chrome/browser/browsing_data/mock_browsing_data_server_bound_cert_helper.h"
20 #include "chrome/browser/content_settings/cookie_settings.h"
21 #include "chrome/browser/content_settings/host_content_settings_map.h"
22 #include "chrome/browser/content_settings/mock_settings_observer.h"
23 #include "chrome/browser/extensions/extension_special_storage_policy.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_types.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "net/url_request/url_request_context.h"
29 #include "net/url_request/url_request_context_getter.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31
32 #include "base/strings/utf_string_conversions.h"
33
34 using ::testing::_;
35 using content::BrowserThread;
36
37 namespace {
38
39 class CookiesTreeModelTest : public testing::Test {
40  public:
41   virtual ~CookiesTreeModelTest() {
42     // Avoid memory leaks.
43     special_storage_policy_ = NULL;
44     profile_.reset();
45     base::MessageLoop::current()->RunUntilIdle();
46   }
47
48   virtual void SetUp() OVERRIDE {
49     profile_.reset(new TestingProfile());
50     mock_browsing_data_cookie_helper_ =
51         new MockBrowsingDataCookieHelper(profile_->GetRequestContext());
52     mock_browsing_data_database_helper_ =
53         new MockBrowsingDataDatabaseHelper(profile_.get());
54     mock_browsing_data_local_storage_helper_ =
55         new MockBrowsingDataLocalStorageHelper(profile_.get());
56     mock_browsing_data_session_storage_helper_ =
57         new MockBrowsingDataLocalStorageHelper(profile_.get());
58     mock_browsing_data_appcache_helper_ =
59         new MockBrowsingDataAppCacheHelper(profile_.get());
60     mock_browsing_data_indexed_db_helper_ =
61         new MockBrowsingDataIndexedDBHelper(profile_.get());
62     mock_browsing_data_file_system_helper_ =
63         new MockBrowsingDataFileSystemHelper(profile_.get());
64     mock_browsing_data_quota_helper_ =
65         new MockBrowsingDataQuotaHelper(profile_.get());
66     mock_browsing_data_server_bound_cert_helper_ =
67         new MockBrowsingDataServerBoundCertHelper();
68     mock_browsing_data_flash_lso_helper_ =
69         new MockBrowsingDataFlashLSOHelper(profile_.get());
70
71     scoped_refptr<CookieSettings> cookie_settings =
72         new CookieSettings(profile_->GetHostContentSettingsMap(),
73                            profile_->GetPrefs());
74     special_storage_policy_ =
75         new ExtensionSpecialStoragePolicy(cookie_settings.get());
76   }
77
78   virtual void TearDown() OVERRIDE {
79     mock_browsing_data_server_bound_cert_helper_ = NULL;
80     mock_browsing_data_quota_helper_ = NULL;
81     mock_browsing_data_file_system_helper_ = NULL;
82     mock_browsing_data_indexed_db_helper_ = NULL;
83     mock_browsing_data_appcache_helper_ = NULL;
84     mock_browsing_data_session_storage_helper_ = NULL;
85     mock_browsing_data_local_storage_helper_ = NULL;
86     mock_browsing_data_database_helper_ = NULL;
87     mock_browsing_data_flash_lso_helper_ = NULL;
88     base::MessageLoop::current()->RunUntilIdle();
89   }
90
91   scoped_ptr<CookiesTreeModel> CreateCookiesTreeModelWithInitialSample() {
92     LocalDataContainer* container = new LocalDataContainer(
93         mock_browsing_data_cookie_helper_.get(),
94         mock_browsing_data_database_helper_.get(),
95         mock_browsing_data_local_storage_helper_.get(),
96         mock_browsing_data_session_storage_helper_.get(),
97         mock_browsing_data_appcache_helper_.get(),
98         mock_browsing_data_indexed_db_helper_.get(),
99         mock_browsing_data_file_system_helper_.get(),
100         mock_browsing_data_quota_helper_.get(),
101         mock_browsing_data_server_bound_cert_helper_.get(),
102         mock_browsing_data_flash_lso_helper_.get());
103
104     CookiesTreeModel* cookies_model =
105         new CookiesTreeModel(container, special_storage_policy_.get(), false);
106     mock_browsing_data_cookie_helper_->
107         AddCookieSamples(GURL("http://foo1"), "A=1");
108     mock_browsing_data_cookie_helper_->
109         AddCookieSamples(GURL("http://foo2"), "B=1");
110     mock_browsing_data_cookie_helper_->
111         AddCookieSamples(GURL("http://foo3"), "C=1");
112     mock_browsing_data_cookie_helper_->Notify();
113     mock_browsing_data_database_helper_->AddDatabaseSamples();
114     mock_browsing_data_database_helper_->Notify();
115     mock_browsing_data_local_storage_helper_->AddLocalStorageSamples();
116     mock_browsing_data_local_storage_helper_->Notify();
117     mock_browsing_data_session_storage_helper_->AddLocalStorageSamples();
118     mock_browsing_data_session_storage_helper_->Notify();
119     mock_browsing_data_indexed_db_helper_->AddIndexedDBSamples();
120     mock_browsing_data_indexed_db_helper_->Notify();
121     mock_browsing_data_file_system_helper_->AddFileSystemSamples();
122     mock_browsing_data_file_system_helper_->Notify();
123     mock_browsing_data_quota_helper_->AddQuotaSamples();
124     mock_browsing_data_quota_helper_->Notify();
125     mock_browsing_data_server_bound_cert_helper_->AddServerBoundCertSample(
126         "sbc1");
127     mock_browsing_data_server_bound_cert_helper_->AddServerBoundCertSample(
128         "sbc2");
129     mock_browsing_data_server_bound_cert_helper_->Notify();
130     mock_browsing_data_flash_lso_helper_->AddFlashLSODomain("xyz.com");
131     mock_browsing_data_flash_lso_helper_->Notify();
132
133     {
134       SCOPED_TRACE("Initial State 3 cookies, 2 databases, 2 local storages, "
135                    "2 session storages, 2 indexed DBs, 3 filesystems, "
136                    "2 quotas, 2 server bound certs, 1 Flash LSO");
137       // 51 because there's the root, then
138       // foo1 -> cookies -> a,
139       // foo2 -> cookies -> b,
140       // foo3 -> cookies -> c,
141       // dbhost1 -> database -> db1,
142       // dbhost2 -> database -> db2,
143       // host1 -> localstorage -> http://host1:1/,
144       //       -> sessionstorage -> http://host1:1/,
145       // host2 -> localstorage -> http://host2:2/.
146       //       -> sessionstorage -> http://host2:2/,
147       // idbhost1 -> indexeddb -> http://idbhost1:1/,
148       // idbhost2 -> indexeddb -> http://idbhost2:2/,
149       // fshost1 -> filesystem -> http://fshost1:1/,
150       // fshost2 -> filesystem -> http://fshost2:1/,
151       // fshost3 -> filesystem -> http://fshost3:1/,
152       // quotahost1 -> quotahost1,
153       // quotahost2 -> quotahost2,
154       // sbc1 -> sbcerts -> sbc1,
155       // sbc2 -> sbcerts -> sbc2.
156       // xyz.com -> flash_lsos
157       EXPECT_EQ(53, cookies_model->GetRoot()->GetTotalNodeCount());
158       EXPECT_EQ("A,B,C", GetDisplayedCookies(cookies_model));
159       EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model));
160       EXPECT_EQ("http://host1:1/,http://host2:2/",
161                 GetDisplayedLocalStorages(cookies_model));
162       EXPECT_EQ("http://host1:1/,http://host2:2/",
163                 GetDisplayedSessionStorages(cookies_model));
164       EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
165                 GetDisplayedIndexedDBs(cookies_model));
166       EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
167                 GetDisplayedFileSystems(cookies_model));
168       EXPECT_EQ("quotahost1,quotahost2",
169                 GetDisplayedQuotas(cookies_model));
170       EXPECT_EQ("sbc1,sbc2",
171                 GetDisplayedServerBoundCerts(cookies_model));
172       EXPECT_EQ("xyz.com",
173                 GetDisplayedFlashLSOs(cookies_model));
174     }
175     return make_scoped_ptr(cookies_model);
176   }
177
178   std::string GetNodesOfChildren(
179       const CookieTreeNode* node,
180       CookieTreeNode::DetailedInfo::NodeType node_type) {
181     if (!node->empty()) {
182       std::string retval;
183       for (int i = 0; i < node->child_count(); ++i) {
184         retval += GetNodesOfChildren(node->GetChild(i), node_type);
185       }
186       return retval;
187     }
188
189     if (node->GetDetailedInfo().node_type != node_type)
190       return std::string();
191
192     switch (node_type) {
193       case CookieTreeNode::DetailedInfo::TYPE_SESSION_STORAGE:
194         return node->GetDetailedInfo().
195             session_storage_info->origin_url.spec() + ",";
196       case CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE:
197         return node->GetDetailedInfo().
198             local_storage_info->origin_url.spec() + ",";
199       case CookieTreeNode::DetailedInfo::TYPE_DATABASE:
200         return node->GetDetailedInfo().database_info->database_name + ",";
201       case CookieTreeNode::DetailedInfo::TYPE_COOKIE:
202         return node->GetDetailedInfo().cookie->Name() + ",";
203       case CookieTreeNode::DetailedInfo::TYPE_APPCACHE:
204         return node->GetDetailedInfo().appcache_info->manifest_url.spec() +
205                ",";
206       case CookieTreeNode::DetailedInfo::TYPE_INDEXED_DB:
207         return node->GetDetailedInfo().indexed_db_info->origin_.spec() +
208                ",";
209       case CookieTreeNode::DetailedInfo::TYPE_FILE_SYSTEM:
210         return node->GetDetailedInfo().file_system_info->origin.spec() +
211                ",";
212       case CookieTreeNode::DetailedInfo::TYPE_QUOTA:
213         return node->GetDetailedInfo().quota_info->host + ",";
214       case CookieTreeNode::DetailedInfo::TYPE_SERVER_BOUND_CERT:
215         return node->GetDetailedInfo(
216             ).server_bound_cert->server_identifier() + ",";
217       case CookieTreeNode::DetailedInfo::TYPE_FLASH_LSO:
218         return node->GetDetailedInfo().flash_lso_domain + ",";
219       default:
220         return std::string();
221     }
222   }
223
224   std::string GetCookiesOfChildren(const CookieTreeNode* node) {
225     return GetNodesOfChildren(node, CookieTreeNode::DetailedInfo::TYPE_COOKIE);
226   }
227
228   std::string GetDatabasesOfChildren(const CookieTreeNode* node) {
229     return GetNodesOfChildren(node,
230                               CookieTreeNode::DetailedInfo::TYPE_DATABASE);
231   }
232
233   std::string GetLocalStoragesOfChildren(const CookieTreeNode* node) {
234     return GetNodesOfChildren(node,
235                               CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE);
236   }
237
238   std::string GetSessionStoragesOfChildren(const CookieTreeNode* node) {
239     return GetNodesOfChildren(
240         node, CookieTreeNode::DetailedInfo::TYPE_SESSION_STORAGE);
241   }
242
243   std::string GetIndexedDBsOfChildren(const CookieTreeNode* node) {
244     return GetNodesOfChildren(
245         node, CookieTreeNode::DetailedInfo::TYPE_INDEXED_DB);
246   }
247
248   std::string GetFileSystemsOfChildren(const CookieTreeNode* node) {
249     return GetNodesOfChildren(
250         node, CookieTreeNode::DetailedInfo::TYPE_FILE_SYSTEM);
251   }
252
253   std::string GetFileQuotaOfChildren(const CookieTreeNode* node) {
254     return GetNodesOfChildren(
255         node, CookieTreeNode::DetailedInfo::TYPE_QUOTA);
256   }
257
258   std::string GetFlashLSOsOfChildren(const CookieTreeNode* node) {
259     return GetNodesOfChildren(
260         node, CookieTreeNode::DetailedInfo::TYPE_FLASH_LSO);
261   }
262
263   // Get the nodes names displayed in the view (if we had one) in the order
264   // they are displayed, as a comma seperated string.
265   // Ex: EXPECT_STREQ("X,Y", GetDisplayedNodes(cookies_view, type).c_str());
266   std::string GetDisplayedNodes(CookiesTreeModel* cookies_model,
267                                 CookieTreeNode::DetailedInfo::NodeType type) {
268     CookieTreeRootNode* root = static_cast<CookieTreeRootNode*>(
269         cookies_model->GetRoot());
270     std::string retval = GetNodesOfChildren(root, type);
271     if (retval.length() && retval[retval.length() - 1] == ',')
272       retval.erase(retval.length() - 1);
273     return retval;
274   }
275
276   std::string GetDisplayedCookies(CookiesTreeModel* cookies_model) {
277     return GetDisplayedNodes(cookies_model,
278                              CookieTreeNode::DetailedInfo::TYPE_COOKIE);
279   }
280
281   std::string GetDisplayedDatabases(CookiesTreeModel* cookies_model) {
282     return GetDisplayedNodes(cookies_model,
283                              CookieTreeNode::DetailedInfo::TYPE_DATABASE);
284   }
285
286   std::string GetDisplayedLocalStorages(CookiesTreeModel* cookies_model) {
287     return GetDisplayedNodes(cookies_model,
288                              CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE);
289   }
290
291   std::string GetDisplayedSessionStorages(CookiesTreeModel* cookies_model) {
292     return GetDisplayedNodes(
293         cookies_model, CookieTreeNode::DetailedInfo::TYPE_SESSION_STORAGE);
294   }
295
296   std::string GetDisplayedAppCaches(CookiesTreeModel* cookies_model) {
297     return GetDisplayedNodes(cookies_model,
298                              CookieTreeNode::DetailedInfo::TYPE_APPCACHE);
299   }
300
301   std::string GetDisplayedIndexedDBs(CookiesTreeModel* cookies_model) {
302     return GetDisplayedNodes(cookies_model,
303                              CookieTreeNode::DetailedInfo::TYPE_INDEXED_DB);
304   }
305
306   std::string GetDisplayedFileSystems(CookiesTreeModel* cookies_model) {
307     return GetDisplayedNodes(cookies_model,
308                              CookieTreeNode::DetailedInfo::TYPE_FILE_SYSTEM);
309   }
310
311   std::string GetDisplayedQuotas(CookiesTreeModel* cookies_model) {
312     return GetDisplayedNodes(cookies_model,
313                              CookieTreeNode::DetailedInfo::TYPE_QUOTA);
314   }
315
316   std::string GetDisplayedServerBoundCerts(CookiesTreeModel* cookies_model) {
317     return GetDisplayedNodes(
318         cookies_model, CookieTreeNode::DetailedInfo::TYPE_SERVER_BOUND_CERT);
319   }
320
321   std::string GetDisplayedFlashLSOs(CookiesTreeModel* cookies_model) {
322     return GetDisplayedNodes(
323         cookies_model, CookieTreeNode::DetailedInfo::TYPE_FLASH_LSO);
324   }
325
326   // Do not call on the root.
327   void DeleteStoredObjects(CookieTreeNode* node) {
328     node->DeleteStoredObjects();
329     CookieTreeNode* parent_node = node->parent();
330     DCHECK(parent_node);
331     delete parent_node->GetModel()->Remove(parent_node, node);
332   }
333
334  protected:
335   content::TestBrowserThreadBundle thread_bundle_;
336   scoped_ptr<TestingProfile> profile_;
337   scoped_refptr<MockBrowsingDataCookieHelper>
338       mock_browsing_data_cookie_helper_;
339   scoped_refptr<MockBrowsingDataDatabaseHelper>
340       mock_browsing_data_database_helper_;
341   scoped_refptr<MockBrowsingDataLocalStorageHelper>
342       mock_browsing_data_local_storage_helper_;
343   scoped_refptr<MockBrowsingDataLocalStorageHelper>
344       mock_browsing_data_session_storage_helper_;
345   scoped_refptr<MockBrowsingDataAppCacheHelper>
346       mock_browsing_data_appcache_helper_;
347   scoped_refptr<MockBrowsingDataIndexedDBHelper>
348       mock_browsing_data_indexed_db_helper_;
349   scoped_refptr<MockBrowsingDataFileSystemHelper>
350       mock_browsing_data_file_system_helper_;
351   scoped_refptr<MockBrowsingDataQuotaHelper>
352       mock_browsing_data_quota_helper_;
353   scoped_refptr<MockBrowsingDataServerBoundCertHelper>
354       mock_browsing_data_server_bound_cert_helper_;
355   scoped_refptr<MockBrowsingDataFlashLSOHelper>
356       mock_browsing_data_flash_lso_helper_;
357
358   scoped_refptr<ExtensionSpecialStoragePolicy> special_storage_policy_;
359 };
360
361 TEST_F(CookiesTreeModelTest, RemoveAll) {
362   scoped_ptr<CookiesTreeModel> cookies_model(
363       CreateCookiesTreeModelWithInitialSample());
364
365   // Reset the selection of the first row.
366   {
367     SCOPED_TRACE("Before removing");
368     EXPECT_EQ("A,B,C",
369               GetDisplayedCookies(cookies_model.get()));
370     EXPECT_EQ("db1,db2",
371               GetDisplayedDatabases(cookies_model.get()));
372     EXPECT_EQ("http://host1:1/,http://host2:2/",
373               GetDisplayedLocalStorages(cookies_model.get()));
374     EXPECT_EQ("http://host1:1/,http://host2:2/",
375               GetDisplayedSessionStorages(cookies_model.get()));
376     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
377               GetDisplayedIndexedDBs(cookies_model.get()));
378     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
379               GetDisplayedFileSystems(cookies_model.get()));
380     EXPECT_EQ("quotahost1,quotahost2",
381               GetDisplayedQuotas(cookies_model.get()));
382     EXPECT_EQ("sbc1,sbc2",
383               GetDisplayedServerBoundCerts(cookies_model.get()));
384     EXPECT_EQ("xyz.com",
385               GetDisplayedFlashLSOs(cookies_model.get()));
386   }
387
388   mock_browsing_data_cookie_helper_->Reset();
389   mock_browsing_data_database_helper_->Reset();
390   mock_browsing_data_local_storage_helper_->Reset();
391   mock_browsing_data_session_storage_helper_->Reset();
392   mock_browsing_data_indexed_db_helper_->Reset();
393   mock_browsing_data_file_system_helper_->Reset();
394
395   cookies_model->DeleteAllStoredObjects();
396
397   // Make sure the nodes are also deleted from the model's cache.
398   // http://crbug.com/43249
399   cookies_model->UpdateSearchResults(base::string16());
400
401   {
402     // 2 nodes - root and app
403     SCOPED_TRACE("After removing");
404     EXPECT_EQ(1, cookies_model->GetRoot()->GetTotalNodeCount());
405     EXPECT_EQ(0, cookies_model->GetRoot()->child_count());
406     EXPECT_EQ(std::string(), GetDisplayedCookies(cookies_model.get()));
407     EXPECT_TRUE(mock_browsing_data_cookie_helper_->AllDeleted());
408     EXPECT_TRUE(mock_browsing_data_database_helper_->AllDeleted());
409     EXPECT_TRUE(mock_browsing_data_local_storage_helper_->AllDeleted());
410     EXPECT_FALSE(mock_browsing_data_session_storage_helper_->AllDeleted());
411     EXPECT_TRUE(mock_browsing_data_indexed_db_helper_->AllDeleted());
412     EXPECT_TRUE(mock_browsing_data_file_system_helper_->AllDeleted());
413     EXPECT_TRUE(mock_browsing_data_server_bound_cert_helper_->AllDeleted());
414     EXPECT_TRUE(mock_browsing_data_flash_lso_helper_->AllDeleted());
415   }
416 }
417
418 TEST_F(CookiesTreeModelTest, Remove) {
419   scoped_ptr<CookiesTreeModel> cookies_model(
420       CreateCookiesTreeModelWithInitialSample());
421
422   // Children start out arranged as follows:
423   //
424   // 0. `foo1`
425   // 1. `foo2`
426   // 2. `foo3`
427   // 3. `fshost1`
428   // 4. `fshost2`
429   // 5. `fshost3`
430   // 6. `gdbhost1`
431   // 7. `gdbhost2`
432   // 8. `host1`
433   // 9. `host2`
434   // 10. `idbhost1`
435   // 11. `idbhost2`
436   // 12. `quotahost1`
437   // 13. `quotahost2`
438   // 14. `sbc1`
439   // 15. `sbc2`
440   // 16. `xyz.com`
441   //
442   // Here, we'll remove them one by one, starting from the end, and
443   // check that the state makes sense.
444
445   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(16));
446   {
447     SCOPED_TRACE("`xyz.com` removed.");
448     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
449     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
450     EXPECT_EQ("http://host1:1/,http://host2:2/",
451               GetDisplayedLocalStorages(cookies_model.get()));
452     EXPECT_EQ("http://host1:1/,http://host2:2/",
453               GetDisplayedSessionStorages(cookies_model.get()));
454     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
455               GetDisplayedFileSystems(cookies_model.get()));
456     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
457               GetDisplayedIndexedDBs(cookies_model.get()));
458     EXPECT_EQ("quotahost1,quotahost2",
459               GetDisplayedQuotas(cookies_model.get()));
460     EXPECT_EQ("sbc1,sbc2",
461               GetDisplayedServerBoundCerts(cookies_model.get()));
462     EXPECT_EQ(51, cookies_model->GetRoot()->GetTotalNodeCount());
463   }
464   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(15));
465   {
466     SCOPED_TRACE("`sbc2` removed.");
467     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
468     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
469     EXPECT_EQ("http://host1:1/,http://host2:2/",
470               GetDisplayedLocalStorages(cookies_model.get()));
471     EXPECT_EQ("http://host1:1/,http://host2:2/",
472               GetDisplayedSessionStorages(cookies_model.get()));
473     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
474               GetDisplayedFileSystems(cookies_model.get()));
475     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
476               GetDisplayedIndexedDBs(cookies_model.get()));
477     EXPECT_EQ("quotahost1,quotahost2",
478               GetDisplayedQuotas(cookies_model.get()));
479     EXPECT_EQ("sbc1",
480               GetDisplayedServerBoundCerts(cookies_model.get()));
481     EXPECT_EQ(48, cookies_model->GetRoot()->GetTotalNodeCount());
482   }
483   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(14));
484   {
485     SCOPED_TRACE("`sbc1` removed.");
486     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
487     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
488     EXPECT_EQ("http://host1:1/,http://host2:2/",
489               GetDisplayedLocalStorages(cookies_model.get()));
490     EXPECT_EQ("http://host1:1/,http://host2:2/",
491               GetDisplayedSessionStorages(cookies_model.get()));
492     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
493               GetDisplayedFileSystems(cookies_model.get()));
494     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
495               GetDisplayedIndexedDBs(cookies_model.get()));
496     EXPECT_EQ("quotahost1,quotahost2",
497               GetDisplayedQuotas(cookies_model.get()));
498     EXPECT_EQ(45, cookies_model->GetRoot()->GetTotalNodeCount());
499   }
500   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(13));
501   {
502     SCOPED_TRACE("`quotahost2` removed.");
503     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
504     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
505     EXPECT_EQ("http://host1:1/,http://host2:2/",
506               GetDisplayedLocalStorages(cookies_model.get()));
507     EXPECT_EQ("http://host1:1/,http://host2:2/",
508               GetDisplayedSessionStorages(cookies_model.get()));
509     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
510               GetDisplayedFileSystems(cookies_model.get()));
511     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
512               GetDisplayedIndexedDBs(cookies_model.get()));
513     EXPECT_EQ("quotahost1",
514               GetDisplayedQuotas(cookies_model.get()));
515     EXPECT_EQ(43, cookies_model->GetRoot()->GetTotalNodeCount());
516   }
517   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(12));
518   {
519     SCOPED_TRACE("`quotahost1` removed.");
520     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
521     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
522     EXPECT_EQ("http://host1:1/,http://host2:2/",
523               GetDisplayedLocalStorages(cookies_model.get()));
524     EXPECT_EQ("http://host1:1/,http://host2:2/",
525               GetDisplayedSessionStorages(cookies_model.get()));
526     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
527               GetDisplayedFileSystems(cookies_model.get()));
528     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
529               GetDisplayedIndexedDBs(cookies_model.get()));
530     EXPECT_EQ(41, cookies_model->GetRoot()->GetTotalNodeCount());
531   }
532   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(11));
533   {
534     SCOPED_TRACE("`idbhost2` removed.");
535     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
536     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
537     EXPECT_EQ("http://host1:1/,http://host2:2/",
538               GetDisplayedLocalStorages(cookies_model.get()));
539     EXPECT_EQ("http://host1:1/,http://host2:2/",
540               GetDisplayedSessionStorages(cookies_model.get()));
541     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
542               GetDisplayedFileSystems(cookies_model.get()));
543     EXPECT_EQ("http://idbhost1:1/",
544               GetDisplayedIndexedDBs(cookies_model.get()));
545     EXPECT_EQ(38, cookies_model->GetRoot()->GetTotalNodeCount());
546   }
547   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(10));
548   {
549     SCOPED_TRACE("`idbhost1` removed.");
550     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
551     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
552     EXPECT_EQ("http://host1:1/,http://host2:2/",
553               GetDisplayedLocalStorages(cookies_model.get()));
554     EXPECT_EQ("http://host1:1/,http://host2:2/",
555               GetDisplayedSessionStorages(cookies_model.get()));
556     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
557               GetDisplayedFileSystems(cookies_model.get()));
558     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
559     EXPECT_EQ(35, cookies_model->GetRoot()->GetTotalNodeCount());
560   }
561   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(9));
562   {
563     SCOPED_TRACE("`host2` removed.");
564     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
565     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
566     EXPECT_EQ("http://host1:1/",
567               GetDisplayedLocalStorages(cookies_model.get()));
568     EXPECT_EQ("http://host1:1/",
569               GetDisplayedSessionStorages(cookies_model.get()));
570     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
571               GetDisplayedFileSystems(cookies_model.get()));
572     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
573     EXPECT_EQ(30, cookies_model->GetRoot()->GetTotalNodeCount());
574   }
575   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(8));
576   {
577     SCOPED_TRACE("`host1` removed.");
578     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
579     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
580     EXPECT_EQ("", GetDisplayedLocalStorages(cookies_model.get()));
581     EXPECT_EQ("", GetDisplayedSessionStorages(cookies_model.get()));
582     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
583               GetDisplayedFileSystems(cookies_model.get()));
584     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
585     EXPECT_EQ(25, cookies_model->GetRoot()->GetTotalNodeCount());
586   }
587   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(7));
588   {
589     SCOPED_TRACE("`gdbhost2` removed.");
590     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
591     EXPECT_EQ("db1", GetDisplayedDatabases(cookies_model.get()));
592     EXPECT_EQ("", GetDisplayedLocalStorages(cookies_model.get()));
593     EXPECT_EQ("", GetDisplayedSessionStorages(cookies_model.get()));
594     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
595               GetDisplayedFileSystems(cookies_model.get()));
596     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
597     EXPECT_EQ(22, cookies_model->GetRoot()->GetTotalNodeCount());
598   }
599   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(6));
600   {
601     SCOPED_TRACE("`gdbhost1` removed.");
602     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
603     EXPECT_EQ("", GetDisplayedDatabases(cookies_model.get()));
604     EXPECT_EQ("", GetDisplayedLocalStorages(cookies_model.get()));
605     EXPECT_EQ("", GetDisplayedSessionStorages(cookies_model.get()));
606     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
607               GetDisplayedFileSystems(cookies_model.get()));
608     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
609     EXPECT_EQ(19, cookies_model->GetRoot()->GetTotalNodeCount());
610   }
611   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(5));
612   {
613     SCOPED_TRACE("`fshost3` removed.");
614     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
615     EXPECT_EQ("", GetDisplayedDatabases(cookies_model.get()));
616     EXPECT_EQ("", GetDisplayedLocalStorages(cookies_model.get()));
617     EXPECT_EQ("", GetDisplayedSessionStorages(cookies_model.get()));
618     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/",
619               GetDisplayedFileSystems(cookies_model.get()));
620     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
621     EXPECT_EQ(16, cookies_model->GetRoot()->GetTotalNodeCount());
622   }
623   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(4));
624   {
625     SCOPED_TRACE("`fshost2` removed.");
626     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
627     EXPECT_EQ("", GetDisplayedDatabases(cookies_model.get()));
628     EXPECT_EQ("", GetDisplayedLocalStorages(cookies_model.get()));
629     EXPECT_EQ("", GetDisplayedSessionStorages(cookies_model.get()));
630     EXPECT_EQ("http://fshost1:1/",
631               GetDisplayedFileSystems(cookies_model.get()));
632     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
633     EXPECT_EQ(13, cookies_model->GetRoot()->GetTotalNodeCount());
634   }
635   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(3));
636   {
637     SCOPED_TRACE("`fshost1` removed.");
638     EXPECT_STREQ("A,B,C", GetDisplayedCookies(cookies_model.get()).c_str());
639     EXPECT_EQ("", GetDisplayedDatabases(cookies_model.get()));
640     EXPECT_EQ("", GetDisplayedLocalStorages(cookies_model.get()));
641     EXPECT_EQ("", GetDisplayedSessionStorages(cookies_model.get()));
642     EXPECT_EQ("", GetDisplayedFileSystems(cookies_model.get()));
643     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
644     EXPECT_EQ(10, cookies_model->GetRoot()->GetTotalNodeCount());
645   }
646   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(2));
647   {
648     SCOPED_TRACE("`foo3` removed.");
649     EXPECT_STREQ("A,B", GetDisplayedCookies(cookies_model.get()).c_str());
650     EXPECT_EQ("", GetDisplayedDatabases(cookies_model.get()));
651     EXPECT_EQ("", GetDisplayedLocalStorages(cookies_model.get()));
652     EXPECT_EQ("", GetDisplayedSessionStorages(cookies_model.get()));
653     EXPECT_EQ("", GetDisplayedFileSystems(cookies_model.get()));
654     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
655     EXPECT_EQ(7, cookies_model->GetRoot()->GetTotalNodeCount());
656   }
657   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(1));
658   {
659     SCOPED_TRACE("`foo2` removed.");
660     EXPECT_STREQ("A", GetDisplayedCookies(cookies_model.get()).c_str());
661     EXPECT_EQ("", GetDisplayedDatabases(cookies_model.get()));
662     EXPECT_EQ("", GetDisplayedLocalStorages(cookies_model.get()));
663     EXPECT_EQ("", GetDisplayedSessionStorages(cookies_model.get()));
664     EXPECT_EQ("", GetDisplayedFileSystems(cookies_model.get()));
665     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
666     EXPECT_EQ(4, cookies_model->GetRoot()->GetTotalNodeCount());
667   }
668   DeleteStoredObjects(cookies_model->GetRoot()->GetChild(0));
669   {
670     SCOPED_TRACE("`foo1` removed.");
671     EXPECT_STREQ("", GetDisplayedCookies(cookies_model.get()).c_str());
672     EXPECT_EQ("", GetDisplayedDatabases(cookies_model.get()));
673     EXPECT_EQ("", GetDisplayedLocalStorages(cookies_model.get()));
674     EXPECT_EQ("", GetDisplayedSessionStorages(cookies_model.get()));
675     EXPECT_EQ("", GetDisplayedFileSystems(cookies_model.get()));
676     EXPECT_EQ("", GetDisplayedIndexedDBs(cookies_model.get()));
677     EXPECT_EQ(1, cookies_model->GetRoot()->GetTotalNodeCount());
678   }
679 }
680
681 TEST_F(CookiesTreeModelTest, RemoveCookiesNode) {
682   scoped_ptr<CookiesTreeModel> cookies_model(
683       CreateCookiesTreeModelWithInitialSample());
684
685   DeleteStoredObjects(
686       cookies_model->GetRoot()->GetChild(0)->GetChild(0));
687   {
688     SCOPED_TRACE("First origin removed");
689     EXPECT_STREQ("B,C", GetDisplayedCookies(cookies_model.get()).c_str());
690     // 51 because in this case, the origin remains, although the COOKIES
691     // node beneath it has been deleted.
692     EXPECT_EQ(51, cookies_model->GetRoot()->GetTotalNodeCount());
693     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
694     EXPECT_EQ("http://host1:1/,http://host2:2/",
695               GetDisplayedLocalStorages(cookies_model.get()));
696     EXPECT_EQ("http://host1:1/,http://host2:2/",
697               GetDisplayedSessionStorages(cookies_model.get()));
698     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
699               GetDisplayedIndexedDBs(cookies_model.get()));
700     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
701               GetDisplayedFileSystems(cookies_model.get()));
702     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(cookies_model.get()));
703     EXPECT_EQ("sbc1,sbc2", GetDisplayedServerBoundCerts(cookies_model.get()));
704   }
705
706   DeleteStoredObjects(
707       cookies_model->GetRoot()->GetChild(6)->GetChild(0));
708   {
709     SCOPED_TRACE("First database removed");
710     EXPECT_STREQ("B,C", GetDisplayedCookies(cookies_model.get()).c_str());
711     EXPECT_EQ("db2", GetDisplayedDatabases(cookies_model.get()));
712     EXPECT_EQ("http://host1:1/,http://host2:2/",
713               GetDisplayedLocalStorages(cookies_model.get()));
714     EXPECT_EQ("http://host1:1/,http://host2:2/",
715               GetDisplayedSessionStorages(cookies_model.get()));
716     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
717               GetDisplayedIndexedDBs(cookies_model.get()));
718     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
719               GetDisplayedFileSystems(cookies_model.get()));
720     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(cookies_model.get()));
721     EXPECT_EQ("sbc1,sbc2", GetDisplayedServerBoundCerts(cookies_model.get()));
722     EXPECT_EQ(49, cookies_model->GetRoot()->GetTotalNodeCount());
723   }
724
725   DeleteStoredObjects(
726       cookies_model->GetRoot()->GetChild(8)->GetChild(0));
727   {
728     SCOPED_TRACE("First origin removed");
729     EXPECT_STREQ("B,C", GetDisplayedCookies(cookies_model.get()).c_str());
730     EXPECT_EQ("db2", GetDisplayedDatabases(cookies_model.get()));
731     EXPECT_EQ("http://host2:2/",
732               GetDisplayedLocalStorages(cookies_model.get()));
733     EXPECT_EQ("http://host1:1/,http://host2:2/",
734               GetDisplayedSessionStorages(cookies_model.get()));
735     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
736               GetDisplayedIndexedDBs(cookies_model.get()));
737     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
738               GetDisplayedFileSystems(cookies_model.get()));
739     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(cookies_model.get()));
740     EXPECT_EQ("sbc1,sbc2", GetDisplayedServerBoundCerts(cookies_model.get()));
741     EXPECT_EQ(47, cookies_model->GetRoot()->GetTotalNodeCount());
742   }
743 }
744
745 TEST_F(CookiesTreeModelTest, RemoveCookieNode) {
746   scoped_ptr<CookiesTreeModel> cookies_model(
747       CreateCookiesTreeModelWithInitialSample());
748
749   DeleteStoredObjects(
750       cookies_model->GetRoot()->GetChild(1)->GetChild(0));
751   {
752     SCOPED_TRACE("Second origin COOKIES node removed");
753     EXPECT_STREQ("A,C", GetDisplayedCookies(cookies_model.get()).c_str());
754     EXPECT_EQ("db1,db2", GetDisplayedDatabases(cookies_model.get()));
755     EXPECT_EQ("http://host1:1/,http://host2:2/",
756               GetDisplayedLocalStorages(cookies_model.get()));
757     EXPECT_EQ("http://host1:1/,http://host2:2/",
758               GetDisplayedSessionStorages(cookies_model.get()));
759     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
760               GetDisplayedIndexedDBs(cookies_model.get()));
761     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
762               GetDisplayedFileSystems(cookies_model.get()));
763     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(cookies_model.get()));
764     EXPECT_EQ("sbc1,sbc2", GetDisplayedServerBoundCerts(cookies_model.get()));
765     // 51 because in this case, the origin remains, although the COOKIES
766     // node beneath it has been deleted.
767     EXPECT_EQ(51, cookies_model->GetRoot()->GetTotalNodeCount());
768   }
769
770   DeleteStoredObjects(
771       cookies_model->GetRoot()->GetChild(6)->GetChild(0));
772   {
773     SCOPED_TRACE("First database removed");
774     EXPECT_STREQ("A,C", GetDisplayedCookies(cookies_model.get()).c_str());
775     EXPECT_EQ("db2", GetDisplayedDatabases(cookies_model.get()));
776     EXPECT_EQ("http://host1:1/,http://host2:2/",
777               GetDisplayedLocalStorages(cookies_model.get()));
778     EXPECT_EQ("http://host1:1/,http://host2:2/",
779               GetDisplayedSessionStorages(cookies_model.get()));
780     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
781               GetDisplayedIndexedDBs(cookies_model.get()));
782     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
783               GetDisplayedFileSystems(cookies_model.get()));
784     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(cookies_model.get()));
785     EXPECT_EQ("sbc1,sbc2", GetDisplayedServerBoundCerts(cookies_model.get()));
786     EXPECT_EQ(49, cookies_model->GetRoot()->GetTotalNodeCount());
787   }
788
789   DeleteStoredObjects(
790       cookies_model->GetRoot()->GetChild(8)->GetChild(0));
791   {
792     SCOPED_TRACE("First origin removed");
793     EXPECT_STREQ("A,C", GetDisplayedCookies(cookies_model.get()).c_str());
794     EXPECT_EQ("db2", GetDisplayedDatabases(cookies_model.get()));
795     EXPECT_EQ("http://host2:2/",
796               GetDisplayedLocalStorages(cookies_model.get()));
797     EXPECT_EQ("http://host1:1/,http://host2:2/",
798               GetDisplayedSessionStorages(cookies_model.get()));
799     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
800               GetDisplayedIndexedDBs(cookies_model.get()));
801     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
802               GetDisplayedFileSystems(cookies_model.get()));
803     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(cookies_model.get()));
804     EXPECT_EQ("sbc1,sbc2", GetDisplayedServerBoundCerts(cookies_model.get()));
805     EXPECT_EQ(47, cookies_model->GetRoot()->GetTotalNodeCount());
806   }
807 }
808
809 TEST_F(CookiesTreeModelTest, RemoveSingleCookieNode) {
810   LocalDataContainer* container =
811       new LocalDataContainer(mock_browsing_data_cookie_helper_.get(),
812                              mock_browsing_data_database_helper_.get(),
813                              mock_browsing_data_local_storage_helper_.get(),
814                              mock_browsing_data_session_storage_helper_.get(),
815                              mock_browsing_data_appcache_helper_.get(),
816                              mock_browsing_data_indexed_db_helper_.get(),
817                              mock_browsing_data_file_system_helper_.get(),
818                              mock_browsing_data_quota_helper_.get(),
819                              mock_browsing_data_server_bound_cert_helper_.get(),
820                              mock_browsing_data_flash_lso_helper_.get());
821   CookiesTreeModel cookies_model(
822       container, special_storage_policy_.get(), false);
823
824   mock_browsing_data_cookie_helper_->
825       AddCookieSamples(GURL("http://foo1"), "A=1");
826   mock_browsing_data_cookie_helper_->
827       AddCookieSamples(GURL("http://foo2"), "B=1");
828   mock_browsing_data_cookie_helper_->
829       AddCookieSamples(GURL("http://foo3"), "C=1");
830   mock_browsing_data_cookie_helper_->
831       AddCookieSamples(GURL("http://foo3"), "D=1");
832   mock_browsing_data_cookie_helper_->Notify();
833   mock_browsing_data_database_helper_->AddDatabaseSamples();
834   mock_browsing_data_database_helper_->Notify();
835   mock_browsing_data_local_storage_helper_->AddLocalStorageSamples();
836   mock_browsing_data_local_storage_helper_->Notify();
837   mock_browsing_data_session_storage_helper_->AddLocalStorageSamples();
838   mock_browsing_data_session_storage_helper_->Notify();
839   mock_browsing_data_indexed_db_helper_->AddIndexedDBSamples();
840   mock_browsing_data_indexed_db_helper_->Notify();
841   mock_browsing_data_file_system_helper_->AddFileSystemSamples();
842   mock_browsing_data_file_system_helper_->Notify();
843   mock_browsing_data_quota_helper_->AddQuotaSamples();
844   mock_browsing_data_quota_helper_->Notify();
845
846   {
847     SCOPED_TRACE("Initial State 4 cookies, 2 databases, 2 local storages, "
848                  "2 session storages, 2 indexed DBs, 3 file systems, "
849                  "2 quotas.");
850     // 46 because there's the root, then
851     // foo1 -> cookies -> a,
852     // foo2 -> cookies -> b,
853     // foo3 -> cookies -> c,d
854     // dbhost1 -> database -> db1,
855     // dbhost2 -> database -> db2,
856     // host1 -> localstorage -> http://host1:1/,
857     //       -> sessionstorage -> http://host1:1/,
858     // host2 -> localstorage -> http://host2:2/,
859     //       -> sessionstorage -> http://host2:2/,
860     // idbhost1 -> sessionstorage -> http://idbhost1:1/,
861     // idbhost2 -> sessionstorage -> http://idbhost2:2/,
862     // fshost1 -> filesystem -> http://fshost1:1/,
863     // fshost2 -> filesystem -> http://fshost2:1/,
864     // fshost3 -> filesystem -> http://fshost3:1/,
865     // quotahost1 -> quotahost1,
866     // quotahost2 -> quotahost2.
867     EXPECT_EQ(46, cookies_model.GetRoot()->GetTotalNodeCount());
868     EXPECT_STREQ("A,B,C,D", GetDisplayedCookies(&cookies_model).c_str());
869     EXPECT_EQ("db1,db2", GetDisplayedDatabases(&cookies_model));
870     EXPECT_EQ("http://host1:1/,http://host2:2/",
871               GetDisplayedLocalStorages(&cookies_model));
872     EXPECT_EQ("http://host1:1/,http://host2:2/",
873               GetDisplayedSessionStorages(&cookies_model));
874     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
875               GetDisplayedIndexedDBs(&cookies_model));
876     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
877               GetDisplayedFileSystems(&cookies_model));
878     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(&cookies_model));
879   }
880   DeleteStoredObjects(cookies_model.GetRoot()->GetChild(2));
881   {
882     SCOPED_TRACE("Third origin removed");
883     EXPECT_STREQ("A,B", GetDisplayedCookies(&cookies_model).c_str());
884     EXPECT_EQ("db1,db2", GetDisplayedDatabases(&cookies_model));
885     EXPECT_EQ("http://host1:1/,http://host2:2/",
886               GetDisplayedLocalStorages(&cookies_model));
887     EXPECT_EQ("http://host1:1/,http://host2:2/",
888               GetDisplayedSessionStorages(&cookies_model));
889     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
890               GetDisplayedIndexedDBs(&cookies_model));
891     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
892               GetDisplayedFileSystems(&cookies_model));
893     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(&cookies_model));
894     EXPECT_EQ(42, cookies_model.GetRoot()->GetTotalNodeCount());
895   }
896 }
897
898 TEST_F(CookiesTreeModelTest, RemoveSingleCookieNodeOf3) {
899   LocalDataContainer* container =
900       new LocalDataContainer(mock_browsing_data_cookie_helper_.get(),
901                              mock_browsing_data_database_helper_.get(),
902                              mock_browsing_data_local_storage_helper_.get(),
903                              mock_browsing_data_session_storage_helper_.get(),
904                              mock_browsing_data_appcache_helper_.get(),
905                              mock_browsing_data_indexed_db_helper_.get(),
906                              mock_browsing_data_file_system_helper_.get(),
907                              mock_browsing_data_quota_helper_.get(),
908                              mock_browsing_data_server_bound_cert_helper_.get(),
909                              mock_browsing_data_flash_lso_helper_.get());
910   CookiesTreeModel cookies_model(
911       container, special_storage_policy_.get(), false);
912
913   mock_browsing_data_cookie_helper_->
914       AddCookieSamples(GURL("http://foo1"), "A=1");
915   mock_browsing_data_cookie_helper_->
916       AddCookieSamples(GURL("http://foo2"), "B=1");
917   mock_browsing_data_cookie_helper_->
918       AddCookieSamples(GURL("http://foo3"), "C=1");
919   mock_browsing_data_cookie_helper_->
920       AddCookieSamples(GURL("http://foo3"), "D=1");
921   mock_browsing_data_cookie_helper_->
922       AddCookieSamples(GURL("http://foo3"), "E=1");
923   mock_browsing_data_cookie_helper_->Notify();
924   mock_browsing_data_database_helper_->AddDatabaseSamples();
925   mock_browsing_data_database_helper_->Notify();
926   mock_browsing_data_local_storage_helper_->AddLocalStorageSamples();
927   mock_browsing_data_local_storage_helper_->Notify();
928   mock_browsing_data_session_storage_helper_->AddLocalStorageSamples();
929   mock_browsing_data_session_storage_helper_->Notify();
930   mock_browsing_data_indexed_db_helper_->AddIndexedDBSamples();
931   mock_browsing_data_indexed_db_helper_->Notify();
932   mock_browsing_data_file_system_helper_->AddFileSystemSamples();
933   mock_browsing_data_file_system_helper_->Notify();
934   mock_browsing_data_quota_helper_->AddQuotaSamples();
935   mock_browsing_data_quota_helper_->Notify();
936
937   {
938     SCOPED_TRACE("Initial State 5 cookies, 2 databases, 2 local storages, "
939                  "2 session storages, 2 indexed DBs, 3 filesystems, "
940                  "2 quotas.");
941     // 47 because there's the root, then
942     // foo1 -> cookies -> a,
943     // foo2 -> cookies -> b,
944     // foo3 -> cookies -> c,d,e
945     // dbhost1 -> database -> db1,
946     // dbhost2 -> database -> db2,
947     // host1 -> localstorage -> http://host1:1/,
948     //       -> sessionstorage -> http://host1:1/,
949     // host2 -> localstorage -> http://host2:2/,
950     //       -> sessionstorage -> http://host2:2/,
951     // idbhost1 -> sessionstorage -> http://idbhost1:1/,
952     // idbhost2 -> sessionstorage -> http://idbhost2:2/,
953     // fshost1 -> filesystem -> http://fshost1:1/,
954     // fshost2 -> filesystem -> http://fshost2:1/,
955     // fshost3 -> filesystem -> http://fshost3:1/,
956     // quotahost1 -> quotahost1,
957     // quotahost2 -> quotahost2.
958     EXPECT_EQ(47, cookies_model.GetRoot()->GetTotalNodeCount());
959     EXPECT_STREQ("A,B,C,D,E", GetDisplayedCookies(&cookies_model).c_str());
960     EXPECT_EQ("db1,db2", GetDisplayedDatabases(&cookies_model));
961     EXPECT_EQ("http://host1:1/,http://host2:2/",
962               GetDisplayedLocalStorages(&cookies_model));
963     EXPECT_EQ("http://host1:1/,http://host2:2/",
964               GetDisplayedSessionStorages(&cookies_model));
965     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
966               GetDisplayedIndexedDBs(&cookies_model));
967     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
968               GetDisplayedFileSystems(&cookies_model));
969     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(&cookies_model));
970   }
971   DeleteStoredObjects(cookies_model.GetRoot()->GetChild(2)->GetChild(0)->
972       GetChild(1));
973   {
974     SCOPED_TRACE("Middle cookie in third origin removed");
975     EXPECT_STREQ("A,B,C,E", GetDisplayedCookies(&cookies_model).c_str());
976     EXPECT_EQ(46, cookies_model.GetRoot()->GetTotalNodeCount());
977     EXPECT_EQ("db1,db2", GetDisplayedDatabases(&cookies_model));
978     EXPECT_EQ("http://host1:1/,http://host2:2/",
979               GetDisplayedLocalStorages(&cookies_model));
980     EXPECT_EQ("http://host1:1/,http://host2:2/",
981               GetDisplayedSessionStorages(&cookies_model));
982     EXPECT_EQ("http://idbhost1:1/,http://idbhost2:2/",
983               GetDisplayedIndexedDBs(&cookies_model));
984     EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
985               GetDisplayedFileSystems(&cookies_model));
986     EXPECT_EQ("quotahost1,quotahost2", GetDisplayedQuotas(&cookies_model));
987   }
988 }
989
990 TEST_F(CookiesTreeModelTest, RemoveSecondOrigin) {
991   LocalDataContainer* container =
992       new LocalDataContainer(mock_browsing_data_cookie_helper_.get(),
993                              mock_browsing_data_database_helper_.get(),
994                              mock_browsing_data_local_storage_helper_.get(),
995                              mock_browsing_data_session_storage_helper_.get(),
996                              mock_browsing_data_appcache_helper_.get(),
997                              mock_browsing_data_indexed_db_helper_.get(),
998                              mock_browsing_data_file_system_helper_.get(),
999                              mock_browsing_data_quota_helper_.get(),
1000                              mock_browsing_data_server_bound_cert_helper_.get(),
1001                              mock_browsing_data_flash_lso_helper_.get());
1002   CookiesTreeModel cookies_model(
1003       container, special_storage_policy_.get(), false);
1004
1005   mock_browsing_data_cookie_helper_->
1006       AddCookieSamples(GURL("http://foo1"), "A=1");
1007   mock_browsing_data_cookie_helper_->
1008       AddCookieSamples(GURL("http://foo2"), "B=1");
1009   mock_browsing_data_cookie_helper_->
1010       AddCookieSamples(GURL("http://foo3"), "C=1");
1011   mock_browsing_data_cookie_helper_->
1012       AddCookieSamples(GURL("http://foo3"), "D=1");
1013   mock_browsing_data_cookie_helper_->
1014       AddCookieSamples(GURL("http://foo3"), "E=1");
1015   mock_browsing_data_cookie_helper_->Notify();
1016
1017   {
1018     SCOPED_TRACE("Initial State 5 cookies");
1019     // 12 because there's the root, then foo1 -> cookies -> a,
1020     // foo2 -> cookies -> b, foo3 -> cookies -> c,d,e
1021     EXPECT_EQ(12, cookies_model.GetRoot()->GetTotalNodeCount());
1022     EXPECT_STREQ("A,B,C,D,E", GetDisplayedCookies(&cookies_model).c_str());
1023   }
1024   DeleteStoredObjects(cookies_model.GetRoot()->GetChild(1));
1025   {
1026     SCOPED_TRACE("Second origin removed");
1027     EXPECT_STREQ("A,C,D,E", GetDisplayedCookies(&cookies_model).c_str());
1028     // Left with root -> foo1 -> cookies -> a, foo3 -> cookies -> c,d,e
1029     EXPECT_EQ(9, cookies_model.GetRoot()->GetTotalNodeCount());
1030   }
1031 }
1032
1033 TEST_F(CookiesTreeModelTest, OriginOrdering) {
1034   LocalDataContainer* container =
1035       new LocalDataContainer(mock_browsing_data_cookie_helper_.get(),
1036                              mock_browsing_data_database_helper_.get(),
1037                              mock_browsing_data_local_storage_helper_.get(),
1038                              mock_browsing_data_session_storage_helper_.get(),
1039                              mock_browsing_data_appcache_helper_.get(),
1040                              mock_browsing_data_indexed_db_helper_.get(),
1041                              mock_browsing_data_file_system_helper_.get(),
1042                              mock_browsing_data_quota_helper_.get(),
1043                              mock_browsing_data_server_bound_cert_helper_.get(),
1044                              mock_browsing_data_flash_lso_helper_.get());
1045   CookiesTreeModel cookies_model(
1046       container, special_storage_policy_.get(), false);
1047
1048   mock_browsing_data_cookie_helper_->
1049       AddCookieSamples(GURL("http://a.foo2.com"), "A=1");
1050   mock_browsing_data_cookie_helper_->
1051       AddCookieSamples(GURL("http://foo2.com"), "B=1");
1052   mock_browsing_data_cookie_helper_->
1053       AddCookieSamples(GURL("http://b.foo1.com"), "C=1");
1054   // Leading dot on the foo4
1055   mock_browsing_data_cookie_helper_->AddCookieSamples(
1056       GURL("http://foo4.com"), "D=1; domain=.foo4.com; path=/;");
1057   mock_browsing_data_cookie_helper_->
1058       AddCookieSamples(GURL("http://a.foo1.com"), "E=1");
1059   mock_browsing_data_cookie_helper_->
1060       AddCookieSamples(GURL("http://foo1.com"), "F=1");
1061   mock_browsing_data_cookie_helper_->
1062       AddCookieSamples(GURL("http://foo3.com"), "G=1");
1063   mock_browsing_data_cookie_helper_->
1064       AddCookieSamples(GURL("http://foo4.com"), "H=1");
1065   mock_browsing_data_cookie_helper_->Notify();
1066
1067   {
1068     SCOPED_TRACE("Initial State 8 cookies");
1069     EXPECT_EQ(23, cookies_model.GetRoot()->GetTotalNodeCount());
1070     EXPECT_STREQ("F,E,C,B,A,G,D,H",
1071         GetDisplayedCookies(&cookies_model).c_str());
1072   }
1073   // Delete "E"
1074   DeleteStoredObjects(cookies_model.GetRoot()->GetChild(1));
1075   {
1076     EXPECT_STREQ("F,C,B,A,G,D,H", GetDisplayedCookies(&cookies_model).c_str());
1077   }
1078 }
1079
1080 TEST_F(CookiesTreeModelTest, ContentSettings) {
1081   GURL host("http://xyz.com/");
1082   LocalDataContainer* container =
1083       new LocalDataContainer(mock_browsing_data_cookie_helper_.get(),
1084                              mock_browsing_data_database_helper_.get(),
1085                              mock_browsing_data_local_storage_helper_.get(),
1086                              mock_browsing_data_session_storage_helper_.get(),
1087                              mock_browsing_data_appcache_helper_.get(),
1088                              mock_browsing_data_indexed_db_helper_.get(),
1089                              mock_browsing_data_file_system_helper_.get(),
1090                              mock_browsing_data_quota_helper_.get(),
1091                              mock_browsing_data_server_bound_cert_helper_.get(),
1092                              mock_browsing_data_flash_lso_helper_.get());
1093   CookiesTreeModel cookies_model(
1094       container, special_storage_policy_.get(), false);
1095
1096   mock_browsing_data_cookie_helper_->AddCookieSamples(host, "A=1");
1097   mock_browsing_data_cookie_helper_->Notify();
1098
1099   TestingProfile profile;
1100   HostContentSettingsMap* content_settings =
1101       profile.GetHostContentSettingsMap();
1102   CookieSettings* cookie_settings =
1103       CookieSettings::Factory::GetForProfile(&profile).get();
1104   MockSettingsObserver observer;
1105
1106   CookieTreeRootNode* root =
1107       static_cast<CookieTreeRootNode*>(cookies_model.GetRoot());
1108   CookieTreeHostNode* origin =
1109       root->GetOrCreateHostNode(host);
1110
1111   EXPECT_EQ(1, origin->child_count());
1112   EXPECT_TRUE(origin->CanCreateContentException());
1113   EXPECT_CALL(observer,
1114               OnContentSettingsChanged(
1115                   content_settings,
1116                   CONTENT_SETTINGS_TYPE_COOKIES,
1117                   false,
1118                   ContentSettingsPattern::FromURLNoWildcard(host),
1119                   ContentSettingsPattern::Wildcard(),
1120                   false));
1121   EXPECT_CALL(observer,
1122               OnContentSettingsChanged(content_settings,
1123                   CONTENT_SETTINGS_TYPE_COOKIES,
1124                   false,
1125                   ContentSettingsPattern::FromURL(host),
1126                   ContentSettingsPattern::Wildcard(),
1127                   false));
1128   origin->CreateContentException(
1129       cookie_settings, CONTENT_SETTING_SESSION_ONLY);
1130   EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(host, host));
1131   EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(host));
1132 }
1133
1134 TEST_F(CookiesTreeModelTest, FileSystemFilter) {
1135   scoped_ptr<CookiesTreeModel> cookies_model(
1136       CreateCookiesTreeModelWithInitialSample());
1137
1138   cookies_model->UpdateSearchResults(base::ASCIIToUTF16("fshost1"));
1139   EXPECT_EQ("http://fshost1:1/",
1140             GetDisplayedFileSystems(cookies_model.get()));
1141
1142   cookies_model->UpdateSearchResults(base::ASCIIToUTF16("fshost2"));
1143   EXPECT_EQ("http://fshost2:2/",
1144             GetDisplayedFileSystems(cookies_model.get()));
1145
1146   cookies_model->UpdateSearchResults(base::ASCIIToUTF16("fshost3"));
1147   EXPECT_EQ("http://fshost3:3/",
1148             GetDisplayedFileSystems(cookies_model.get()));
1149
1150   cookies_model->UpdateSearchResults(base::string16());
1151   EXPECT_EQ("http://fshost1:1/,http://fshost2:2/,http://fshost3:3/",
1152             GetDisplayedFileSystems(cookies_model.get()));
1153 }
1154
1155 TEST_F(CookiesTreeModelTest, CookiesFilter) {
1156   LocalDataContainer* container =
1157       new LocalDataContainer(mock_browsing_data_cookie_helper_.get(),
1158                              mock_browsing_data_database_helper_.get(),
1159                              mock_browsing_data_local_storage_helper_.get(),
1160                              mock_browsing_data_session_storage_helper_.get(),
1161                              mock_browsing_data_appcache_helper_.get(),
1162                              mock_browsing_data_indexed_db_helper_.get(),
1163                              mock_browsing_data_file_system_helper_.get(),
1164                              mock_browsing_data_quota_helper_.get(),
1165                              mock_browsing_data_server_bound_cert_helper_.get(),
1166                              mock_browsing_data_flash_lso_helper_.get());
1167   CookiesTreeModel cookies_model(
1168       container, special_storage_policy_.get(), false);
1169
1170   mock_browsing_data_cookie_helper_->
1171       AddCookieSamples(GURL("http://123.com"), "A=1");
1172   mock_browsing_data_cookie_helper_->
1173       AddCookieSamples(GURL("http://foo1.com"), "B=1");
1174   mock_browsing_data_cookie_helper_->
1175       AddCookieSamples(GURL("http://foo2.com"), "C=1");
1176   mock_browsing_data_cookie_helper_->
1177       AddCookieSamples(GURL("http://foo3.com"), "D=1");
1178   mock_browsing_data_cookie_helper_->Notify();
1179   EXPECT_EQ("A,B,C,D", GetDisplayedCookies(&cookies_model));
1180
1181   cookies_model.UpdateSearchResults(base::string16(base::ASCIIToUTF16("foo")));
1182   EXPECT_EQ("B,C,D", GetDisplayedCookies(&cookies_model));
1183
1184   cookies_model.UpdateSearchResults(base::string16(base::ASCIIToUTF16("2")));
1185   EXPECT_EQ("A,C", GetDisplayedCookies(&cookies_model));
1186
1187   cookies_model.UpdateSearchResults(base::string16(base::ASCIIToUTF16("foo3")));
1188   EXPECT_EQ("D", GetDisplayedCookies(&cookies_model));
1189
1190   cookies_model.UpdateSearchResults(base::string16());
1191   EXPECT_EQ("A,B,C,D", GetDisplayedCookies(&cookies_model));
1192 }
1193
1194 }  // namespace