1 // Copyright (c) 2010 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_appcache_helper.h" 6 7 #include "base/stl_util-inl.h" 8 #include "chrome/test/testing_browser_process_test.h" 9 #include "chrome/test/testing_profile.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace { 13 class TestCompletionCallback { 14 public: 15 TestCompletionCallback() 16 : have_result_(false) { 17 } 18 19 bool have_result() const { return have_result_; } 20 21 void callback() { 22 have_result_ = true; 23 } 24 25 private: 26 bool have_result_; 27 }; 28 29 } // namespace 30 31 typedef TestingBrowserProcessTest CannedBrowsingDataAppCacheHelperTest; 32 33 TEST_F(CannedBrowsingDataAppCacheHelperTest, SetInfo) { 34 TestingProfile profile; 35 36 GURL manifest1("http://example1.com/manifest.xml"); 37 GURL manifest2("http://example2.com/path1/manifest.xml"); 38 GURL manifest3("http://example2.com/path2/manifest.xml"); 39 40 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( 41 new CannedBrowsingDataAppCacheHelper(&profile)); 42 helper->AddAppCache(manifest1); 43 helper->AddAppCache(manifest2); 44 helper->AddAppCache(manifest3); 45 46 TestCompletionCallback callback; 47 helper->StartFetching( 48 NewCallback(&callback, &TestCompletionCallback::callback)); 49 ASSERT_TRUE(callback.have_result()); 50 51 std::map<GURL, appcache::AppCacheInfoVector>& collection = 52 helper->info_collection()->infos_by_origin; 53 54 ASSERT_EQ(2u, collection.size()); 55 EXPECT_TRUE(ContainsKey(collection, manifest1.GetOrigin())); 56 ASSERT_EQ(1u, collection[manifest1.GetOrigin()].size()); 57 EXPECT_EQ(manifest1, collection[manifest1.GetOrigin()].at(0).manifest_url); 58 59 EXPECT_TRUE(ContainsKey(collection, manifest2.GetOrigin())); 60 EXPECT_EQ(2u, collection[manifest2.GetOrigin()].size()); 61 std::set<GURL> manifest_results; 62 manifest_results.insert(collection[manifest2.GetOrigin()].at(0).manifest_url); 63 manifest_results.insert(collection[manifest2.GetOrigin()].at(1).manifest_url); 64 EXPECT_TRUE(ContainsKey(manifest_results, manifest2)); 65 EXPECT_TRUE(ContainsKey(manifest_results, manifest3)); 66 } 67 68 TEST_F(CannedBrowsingDataAppCacheHelperTest, Unique) { 69 TestingProfile profile; 70 71 GURL manifest("http://example.com/manifest.xml"); 72 73 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( 74 new CannedBrowsingDataAppCacheHelper(&profile)); 75 helper->AddAppCache(manifest); 76 helper->AddAppCache(manifest); 77 78 TestCompletionCallback callback; 79 helper->StartFetching( 80 NewCallback(&callback, &TestCompletionCallback::callback)); 81 ASSERT_TRUE(callback.have_result()); 82 83 std::map<GURL, appcache::AppCacheInfoVector>& collection = 84 helper->info_collection()->infos_by_origin; 85 86 ASSERT_EQ(1u, collection.size()); 87 EXPECT_TRUE(ContainsKey(collection, manifest.GetOrigin())); 88 ASSERT_EQ(1u, collection[manifest.GetOrigin()].size()); 89 EXPECT_EQ(manifest, collection[manifest.GetOrigin()].at(0).manifest_url); 90 } 91 92 TEST_F(CannedBrowsingDataAppCacheHelperTest, Empty) { 93 TestingProfile profile; 94 95 GURL manifest("http://example.com/manifest.xml"); 96 97 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( 98 new CannedBrowsingDataAppCacheHelper(&profile)); 99 100 ASSERT_TRUE(helper->empty()); 101 helper->AddAppCache(manifest); 102 ASSERT_FALSE(helper->empty()); 103 helper->Reset(); 104 ASSERT_TRUE(helper->empty()); 105 } 106