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.
5 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h"
8 #include "base/bind_helpers.h"
9 #include "base/stl_util.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 class TestCompletionCallback
{
17 TestCompletionCallback()
18 : have_result_(false) {
21 bool have_result() const { return have_result_
; }
33 class CannedBrowsingDataAppCacheHelperTest
: public testing::Test
{
35 CannedBrowsingDataAppCacheHelperTest()
36 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD
) {}
38 content::TestBrowserThreadBundle thread_bundle_
;
41 TEST_F(CannedBrowsingDataAppCacheHelperTest
, SetInfo
) {
42 TestingProfile profile
;
44 GURL
manifest1("http://example1.com/manifest.xml");
45 GURL
manifest2("http://example2.com/path1/manifest.xml");
46 GURL
manifest3("http://example2.com/path2/manifest.xml");
48 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
49 new CannedBrowsingDataAppCacheHelper(&profile
));
50 helper
->AddAppCache(manifest1
);
51 helper
->AddAppCache(manifest2
);
52 helper
->AddAppCache(manifest3
);
54 TestCompletionCallback callback
;
55 helper
->StartFetching(base::Bind(&TestCompletionCallback::callback
,
56 base::Unretained(&callback
)));
57 ASSERT_TRUE(callback
.have_result());
59 std::map
<GURL
, appcache::AppCacheInfoVector
>& collection
=
60 helper
->info_collection()->infos_by_origin
;
62 ASSERT_EQ(2u, collection
.size());
63 EXPECT_TRUE(ContainsKey(collection
, manifest1
.GetOrigin()));
64 ASSERT_EQ(1u, collection
[manifest1
.GetOrigin()].size());
65 EXPECT_EQ(manifest1
, collection
[manifest1
.GetOrigin()].at(0).manifest_url
);
67 EXPECT_TRUE(ContainsKey(collection
, manifest2
.GetOrigin()));
68 EXPECT_EQ(2u, collection
[manifest2
.GetOrigin()].size());
69 std::set
<GURL
> manifest_results
;
70 manifest_results
.insert(collection
[manifest2
.GetOrigin()].at(0).manifest_url
);
71 manifest_results
.insert(collection
[manifest2
.GetOrigin()].at(1).manifest_url
);
72 EXPECT_TRUE(ContainsKey(manifest_results
, manifest2
));
73 EXPECT_TRUE(ContainsKey(manifest_results
, manifest3
));
76 TEST_F(CannedBrowsingDataAppCacheHelperTest
, Unique
) {
77 TestingProfile profile
;
79 GURL
manifest("http://example.com/manifest.xml");
81 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
82 new CannedBrowsingDataAppCacheHelper(&profile
));
83 helper
->AddAppCache(manifest
);
84 helper
->AddAppCache(manifest
);
86 TestCompletionCallback callback
;
87 helper
->StartFetching(base::Bind(&TestCompletionCallback::callback
,
88 base::Unretained(&callback
)));
89 ASSERT_TRUE(callback
.have_result());
91 std::map
<GURL
, appcache::AppCacheInfoVector
>& collection
=
92 helper
->info_collection()->infos_by_origin
;
94 ASSERT_EQ(1u, collection
.size());
95 EXPECT_TRUE(ContainsKey(collection
, manifest
.GetOrigin()));
96 ASSERT_EQ(1u, collection
[manifest
.GetOrigin()].size());
97 EXPECT_EQ(manifest
, collection
[manifest
.GetOrigin()].at(0).manifest_url
);
100 TEST_F(CannedBrowsingDataAppCacheHelperTest
, Empty
) {
101 TestingProfile profile
;
103 GURL
manifest("http://example.com/manifest.xml");
105 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
106 new CannedBrowsingDataAppCacheHelper(&profile
));
108 ASSERT_TRUE(helper
->empty());
109 helper
->AddAppCache(manifest
);
110 ASSERT_FALSE(helper
->empty());
112 ASSERT_TRUE(helper
->empty());
115 TEST_F(CannedBrowsingDataAppCacheHelperTest
, Delete
) {
116 TestingProfile profile
;
118 GURL
manifest1("http://example.com/manifest1.xml");
119 GURL
manifest2("http://foo.example.com/manifest2.xml");
120 GURL
manifest3("http://bar.example.com/manifest3.xml");
122 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
123 new CannedBrowsingDataAppCacheHelper(&profile
));
125 EXPECT_TRUE(helper
->empty());
126 helper
->AddAppCache(manifest1
);
127 helper
->AddAppCache(manifest2
);
128 helper
->AddAppCache(manifest3
);
129 EXPECT_FALSE(helper
->empty());
130 EXPECT_EQ(3u, helper
->GetAppCacheCount());
131 helper
->DeleteAppCacheGroup(manifest2
);
132 EXPECT_EQ(2u, helper
->GetAppCacheCount());
133 EXPECT_TRUE(helper
->GetOriginAppCacheInfoMap().find(manifest2
) ==
134 helper
->GetOriginAppCacheInfoMap().end());
137 TEST_F(CannedBrowsingDataAppCacheHelperTest
, IgnoreExtensionsAndDevTools
) {
138 TestingProfile profile
;
140 GURL
manifest1("chrome-extension://abcdefghijklmnopqrstuvwxyz/manifest.xml");
141 GURL
manifest2("chrome-devtools://abcdefghijklmnopqrstuvwxyz/manifest.xml");
143 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
144 new CannedBrowsingDataAppCacheHelper(&profile
));
146 ASSERT_TRUE(helper
->empty());
147 helper
->AddAppCache(manifest1
);
148 ASSERT_TRUE(helper
->empty());
149 helper
->AddAppCache(manifest2
);
150 ASSERT_TRUE(helper
->empty());