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"
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"
17 class TestCompletionCallback
{
19 TestCompletionCallback() {}
21 bool have_result() const { return info_collection_
.get(); }
23 content::AppCacheInfoCollection
* info_collection() const {
24 return info_collection_
.get();
27 void set_info_collection(
28 scoped_refptr
<content::AppCacheInfoCollection
> info_collection
) {
29 info_collection_
= info_collection
;
33 scoped_refptr
<content::AppCacheInfoCollection
> info_collection_
;
35 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback
);
40 class CannedBrowsingDataAppCacheHelperTest
: public testing::Test
{
42 CannedBrowsingDataAppCacheHelperTest()
43 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD
) {}
45 content::TestBrowserThreadBundle thread_bundle_
;
48 TEST_F(CannedBrowsingDataAppCacheHelperTest
, SetInfo
) {
49 TestingProfile profile
;
51 GURL
manifest1("http://example1.com/manifest.xml");
52 GURL
manifest2("http://example2.com/path1/manifest.xml");
53 GURL
manifest3("http://example2.com/path2/manifest.xml");
55 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
56 new CannedBrowsingDataAppCacheHelper(&profile
));
57 helper
->AddAppCache(manifest1
);
58 helper
->AddAppCache(manifest2
);
59 helper
->AddAppCache(manifest3
);
61 TestCompletionCallback callback
;
62 helper
->StartFetching(base::Bind(&TestCompletionCallback::set_info_collection
,
63 base::Unretained(&callback
)));
64 ASSERT_TRUE(callback
.have_result());
66 std::map
<GURL
, content::AppCacheInfoVector
>& collection
=
67 callback
.info_collection()->infos_by_origin
;
69 ASSERT_EQ(2u, collection
.size());
70 EXPECT_TRUE(ContainsKey(collection
, manifest1
.GetOrigin()));
71 ASSERT_EQ(1u, collection
[manifest1
.GetOrigin()].size());
72 EXPECT_EQ(manifest1
, collection
[manifest1
.GetOrigin()].at(0).manifest_url
);
74 EXPECT_TRUE(ContainsKey(collection
, manifest2
.GetOrigin()));
75 EXPECT_EQ(2u, collection
[manifest2
.GetOrigin()].size());
76 std::set
<GURL
> manifest_results
;
77 manifest_results
.insert(collection
[manifest2
.GetOrigin()].at(0).manifest_url
);
78 manifest_results
.insert(collection
[manifest2
.GetOrigin()].at(1).manifest_url
);
79 EXPECT_TRUE(ContainsKey(manifest_results
, manifest2
));
80 EXPECT_TRUE(ContainsKey(manifest_results
, manifest3
));
83 TEST_F(CannedBrowsingDataAppCacheHelperTest
, Unique
) {
84 TestingProfile profile
;
86 GURL
manifest("http://example.com/manifest.xml");
88 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
89 new CannedBrowsingDataAppCacheHelper(&profile
));
90 helper
->AddAppCache(manifest
);
91 helper
->AddAppCache(manifest
);
93 TestCompletionCallback callback
;
94 helper
->StartFetching(base::Bind(&TestCompletionCallback::set_info_collection
,
95 base::Unretained(&callback
)));
96 ASSERT_TRUE(callback
.have_result());
98 std::map
<GURL
, content::AppCacheInfoVector
>& collection
=
99 callback
.info_collection()->infos_by_origin
;
101 ASSERT_EQ(1u, collection
.size());
102 EXPECT_TRUE(ContainsKey(collection
, manifest
.GetOrigin()));
103 ASSERT_EQ(1u, collection
[manifest
.GetOrigin()].size());
104 EXPECT_EQ(manifest
, collection
[manifest
.GetOrigin()].at(0).manifest_url
);
107 TEST_F(CannedBrowsingDataAppCacheHelperTest
, Empty
) {
108 TestingProfile profile
;
110 GURL
manifest("http://example.com/manifest.xml");
112 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
113 new CannedBrowsingDataAppCacheHelper(&profile
));
115 ASSERT_TRUE(helper
->empty());
116 helper
->AddAppCache(manifest
);
117 ASSERT_FALSE(helper
->empty());
119 ASSERT_TRUE(helper
->empty());
122 TEST_F(CannedBrowsingDataAppCacheHelperTest
, Delete
) {
123 TestingProfile profile
;
125 GURL
manifest1("http://example.com/manifest1.xml");
126 GURL
manifest2("http://foo.example.com/manifest2.xml");
127 GURL
manifest3("http://bar.example.com/manifest3.xml");
129 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
130 new CannedBrowsingDataAppCacheHelper(&profile
));
132 EXPECT_TRUE(helper
->empty());
133 helper
->AddAppCache(manifest1
);
134 helper
->AddAppCache(manifest2
);
135 helper
->AddAppCache(manifest3
);
136 EXPECT_FALSE(helper
->empty());
137 EXPECT_EQ(3u, helper
->GetAppCacheCount());
138 helper
->DeleteAppCacheGroup(manifest2
);
139 EXPECT_EQ(2u, helper
->GetAppCacheCount());
140 EXPECT_FALSE(ContainsKey(helper
->GetOriginAppCacheInfoMap(), manifest2
));
143 TEST_F(CannedBrowsingDataAppCacheHelperTest
, IgnoreExtensionsAndDevTools
) {
144 TestingProfile profile
;
146 GURL
manifest1("chrome-extension://abcdefghijklmnopqrstuvwxyz/manifest.xml");
147 GURL
manifest2("chrome-devtools://abcdefghijklmnopqrstuvwxyz/manifest.xml");
149 scoped_refptr
<CannedBrowsingDataAppCacheHelper
> helper(
150 new CannedBrowsingDataAppCacheHelper(&profile
));
152 ASSERT_TRUE(helper
->empty());
153 helper
->AddAppCache(manifest1
);
154 ASSERT_TRUE(helper
->empty());
155 helper
->AddAppCache(manifest2
);
156 ASSERT_TRUE(helper
->empty());