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/browsing_data_appcache_helper.h" 6 7 #include <set> 8 9 #include "base/bind.h" 10 #include "base/bind_helpers.h" 11 #include "base/stl_util.h" 12 #include "chrome/test/base/testing_profile.h" 13 #include "content/public/test/test_browser_thread_bundle.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace { 17 class TestCompletionCallback { 18 public: 19 TestCompletionCallback() 20 : have_result_(false) { 21 } 22 23 bool have_result() const { return have_result_; } 24 25 void callback() { 26 have_result_ = true; 27 } 28 29 private: 30 bool have_result_; 31 }; 32 33 } // namespace 34 35 class CannedBrowsingDataAppCacheHelperTest : public testing::Test { 36 public: 37 CannedBrowsingDataAppCacheHelperTest() 38 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} 39 40 content::TestBrowserThreadBundle thread_bundle_; 41 }; 42 43 TEST_F(CannedBrowsingDataAppCacheHelperTest, SetInfo) { 44 TestingProfile profile; 45 46 GURL manifest1("http://example1.com/manifest.xml"); 47 GURL manifest2("http://example2.com/path1/manifest.xml"); 48 GURL manifest3("http://example2.com/path2/manifest.xml"); 49 50 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( 51 new CannedBrowsingDataAppCacheHelper(&profile)); 52 helper->AddAppCache(manifest1); 53 helper->AddAppCache(manifest2); 54 helper->AddAppCache(manifest3); 55 56 TestCompletionCallback callback; 57 helper->StartFetching(base::Bind(&TestCompletionCallback::callback, 58 base::Unretained(&callback))); 59 ASSERT_TRUE(callback.have_result()); 60 61 std::map<GURL, content::AppCacheInfoVector>& collection = 62 helper->info_collection()->infos_by_origin; 63 64 ASSERT_EQ(2u, collection.size()); 65 EXPECT_TRUE(ContainsKey(collection, manifest1.GetOrigin())); 66 ASSERT_EQ(1u, collection[manifest1.GetOrigin()].size()); 67 EXPECT_EQ(manifest1, collection[manifest1.GetOrigin()].at(0).manifest_url); 68 69 EXPECT_TRUE(ContainsKey(collection, manifest2.GetOrigin())); 70 EXPECT_EQ(2u, collection[manifest2.GetOrigin()].size()); 71 std::set<GURL> manifest_results; 72 manifest_results.insert(collection[manifest2.GetOrigin()].at(0).manifest_url); 73 manifest_results.insert(collection[manifest2.GetOrigin()].at(1).manifest_url); 74 EXPECT_TRUE(ContainsKey(manifest_results, manifest2)); 75 EXPECT_TRUE(ContainsKey(manifest_results, manifest3)); 76 } 77 78 TEST_F(CannedBrowsingDataAppCacheHelperTest, Unique) { 79 TestingProfile profile; 80 81 GURL manifest("http://example.com/manifest.xml"); 82 83 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( 84 new CannedBrowsingDataAppCacheHelper(&profile)); 85 helper->AddAppCache(manifest); 86 helper->AddAppCache(manifest); 87 88 TestCompletionCallback callback; 89 helper->StartFetching(base::Bind(&TestCompletionCallback::callback, 90 base::Unretained(&callback))); 91 ASSERT_TRUE(callback.have_result()); 92 93 std::map<GURL, content::AppCacheInfoVector>& collection = 94 helper->info_collection()->infos_by_origin; 95 96 ASSERT_EQ(1u, collection.size()); 97 EXPECT_TRUE(ContainsKey(collection, manifest.GetOrigin())); 98 ASSERT_EQ(1u, collection[manifest.GetOrigin()].size()); 99 EXPECT_EQ(manifest, collection[manifest.GetOrigin()].at(0).manifest_url); 100 } 101 102 TEST_F(CannedBrowsingDataAppCacheHelperTest, Empty) { 103 TestingProfile profile; 104 105 GURL manifest("http://example.com/manifest.xml"); 106 107 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( 108 new CannedBrowsingDataAppCacheHelper(&profile)); 109 110 ASSERT_TRUE(helper->empty()); 111 helper->AddAppCache(manifest); 112 ASSERT_FALSE(helper->empty()); 113 helper->Reset(); 114 ASSERT_TRUE(helper->empty()); 115 } 116 117 TEST_F(CannedBrowsingDataAppCacheHelperTest, Delete) { 118 TestingProfile profile; 119 120 GURL manifest1("http://example.com/manifest1.xml"); 121 GURL manifest2("http://foo.example.com/manifest2.xml"); 122 GURL manifest3("http://bar.example.com/manifest3.xml"); 123 124 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( 125 new CannedBrowsingDataAppCacheHelper(&profile)); 126 127 EXPECT_TRUE(helper->empty()); 128 helper->AddAppCache(manifest1); 129 helper->AddAppCache(manifest2); 130 helper->AddAppCache(manifest3); 131 EXPECT_FALSE(helper->empty()); 132 EXPECT_EQ(3u, helper->GetAppCacheCount()); 133 helper->DeleteAppCacheGroup(manifest2); 134 EXPECT_EQ(2u, helper->GetAppCacheCount()); 135 EXPECT_TRUE(helper->GetOriginAppCacheInfoMap().find(manifest2) == 136 helper->GetOriginAppCacheInfoMap().end()); 137 } 138 139 TEST_F(CannedBrowsingDataAppCacheHelperTest, IgnoreExtensionsAndDevTools) { 140 TestingProfile profile; 141 142 GURL manifest1("chrome-extension://abcdefghijklmnopqrstuvwxyz/manifest.xml"); 143 GURL manifest2("chrome-devtools://abcdefghijklmnopqrstuvwxyz/manifest.xml"); 144 145 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( 146 new CannedBrowsingDataAppCacheHelper(&profile)); 147 148 ASSERT_TRUE(helper->empty()); 149 helper->AddAppCache(manifest1); 150 ASSERT_TRUE(helper->empty()); 151 helper->AddAppCache(manifest2); 152 ASSERT_TRUE(helper->empty()); 153 } 154