OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h" | 5 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "content/public/test/test_utils.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 class TestCompletionCallback { | 19 class TestCompletionCallback { |
19 public: | 20 public: |
20 TestCompletionCallback() {} | 21 TestCompletionCallback() {} |
21 | 22 |
22 bool have_result() const { return info_collection_.get(); } | 23 bool have_result() const { return info_collection_.get(); } |
23 | 24 |
24 content::AppCacheInfoCollection* info_collection() const { | 25 content::AppCacheInfoCollection* info_collection() const { |
(...skipping 11 matching lines...) Expand all Loading... |
36 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); | 37 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); |
37 }; | 38 }; |
38 | 39 |
39 } // namespace | 40 } // namespace |
40 | 41 |
41 class CannedBrowsingDataAppCacheHelperTest : public testing::Test { | 42 class CannedBrowsingDataAppCacheHelperTest : public testing::Test { |
42 public: | 43 public: |
43 CannedBrowsingDataAppCacheHelperTest() | 44 CannedBrowsingDataAppCacheHelperTest() |
44 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} | 45 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} |
45 | 46 |
| 47 void TearDown() override { |
| 48 // Make sure we run all pending tasks on IO thread before testing |
| 49 // profile is destructed. |
| 50 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); |
| 51 } |
| 52 |
| 53 protected: |
46 content::TestBrowserThreadBundle thread_bundle_; | 54 content::TestBrowserThreadBundle thread_bundle_; |
| 55 TestingProfile profile_; |
47 }; | 56 }; |
48 | 57 |
49 TEST_F(CannedBrowsingDataAppCacheHelperTest, SetInfo) { | 58 TEST_F(CannedBrowsingDataAppCacheHelperTest, SetInfo) { |
50 TestingProfile profile; | |
51 | |
52 GURL manifest1("http://example1.com/manifest.xml"); | 59 GURL manifest1("http://example1.com/manifest.xml"); |
53 GURL manifest2("http://example2.com/path1/manifest.xml"); | 60 GURL manifest2("http://example2.com/path1/manifest.xml"); |
54 GURL manifest3("http://example2.com/path2/manifest.xml"); | 61 GURL manifest3("http://example2.com/path2/manifest.xml"); |
55 | 62 |
56 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( | 63 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( |
57 new CannedBrowsingDataAppCacheHelper(&profile)); | 64 new CannedBrowsingDataAppCacheHelper(&profile_)); |
58 helper->AddAppCache(manifest1); | 65 helper->AddAppCache(manifest1); |
59 helper->AddAppCache(manifest2); | 66 helper->AddAppCache(manifest2); |
60 helper->AddAppCache(manifest3); | 67 helper->AddAppCache(manifest3); |
61 | 68 |
62 TestCompletionCallback callback; | 69 TestCompletionCallback callback; |
63 helper->StartFetching(base::Bind(&TestCompletionCallback::set_info_collection, | 70 helper->StartFetching(base::Bind(&TestCompletionCallback::set_info_collection, |
64 base::Unretained(&callback))); | 71 base::Unretained(&callback))); |
65 ASSERT_TRUE(callback.have_result()); | 72 ASSERT_TRUE(callback.have_result()); |
66 | 73 |
67 std::map<GURL, content::AppCacheInfoVector>& collection = | 74 std::map<GURL, content::AppCacheInfoVector>& collection = |
68 callback.info_collection()->infos_by_origin; | 75 callback.info_collection()->infos_by_origin; |
69 | 76 |
70 ASSERT_EQ(2u, collection.size()); | 77 ASSERT_EQ(2u, collection.size()); |
71 EXPECT_TRUE(ContainsKey(collection, manifest1.GetOrigin())); | 78 EXPECT_TRUE(ContainsKey(collection, manifest1.GetOrigin())); |
72 ASSERT_EQ(1u, collection[manifest1.GetOrigin()].size()); | 79 ASSERT_EQ(1u, collection[manifest1.GetOrigin()].size()); |
73 EXPECT_EQ(manifest1, collection[manifest1.GetOrigin()].at(0).manifest_url); | 80 EXPECT_EQ(manifest1, collection[manifest1.GetOrigin()].at(0).manifest_url); |
74 | 81 |
75 EXPECT_TRUE(ContainsKey(collection, manifest2.GetOrigin())); | 82 EXPECT_TRUE(ContainsKey(collection, manifest2.GetOrigin())); |
76 EXPECT_EQ(2u, collection[manifest2.GetOrigin()].size()); | 83 EXPECT_EQ(2u, collection[manifest2.GetOrigin()].size()); |
77 std::set<GURL> manifest_results; | 84 std::set<GURL> manifest_results; |
78 manifest_results.insert(collection[manifest2.GetOrigin()].at(0).manifest_url); | 85 manifest_results.insert(collection[manifest2.GetOrigin()].at(0).manifest_url); |
79 manifest_results.insert(collection[manifest2.GetOrigin()].at(1).manifest_url); | 86 manifest_results.insert(collection[manifest2.GetOrigin()].at(1).manifest_url); |
80 EXPECT_TRUE(ContainsKey(manifest_results, manifest2)); | 87 EXPECT_TRUE(ContainsKey(manifest_results, manifest2)); |
81 EXPECT_TRUE(ContainsKey(manifest_results, manifest3)); | 88 EXPECT_TRUE(ContainsKey(manifest_results, manifest3)); |
82 } | 89 } |
83 | 90 |
84 TEST_F(CannedBrowsingDataAppCacheHelperTest, Unique) { | 91 TEST_F(CannedBrowsingDataAppCacheHelperTest, Unique) { |
85 TestingProfile profile; | |
86 | |
87 GURL manifest("http://example.com/manifest.xml"); | 92 GURL manifest("http://example.com/manifest.xml"); |
88 | 93 |
89 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( | 94 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( |
90 new CannedBrowsingDataAppCacheHelper(&profile)); | 95 new CannedBrowsingDataAppCacheHelper(&profile_)); |
91 helper->AddAppCache(manifest); | 96 helper->AddAppCache(manifest); |
92 helper->AddAppCache(manifest); | 97 helper->AddAppCache(manifest); |
93 | 98 |
94 TestCompletionCallback callback; | 99 TestCompletionCallback callback; |
95 helper->StartFetching(base::Bind(&TestCompletionCallback::set_info_collection, | 100 helper->StartFetching(base::Bind(&TestCompletionCallback::set_info_collection, |
96 base::Unretained(&callback))); | 101 base::Unretained(&callback))); |
97 ASSERT_TRUE(callback.have_result()); | 102 ASSERT_TRUE(callback.have_result()); |
98 | 103 |
99 std::map<GURL, content::AppCacheInfoVector>& collection = | 104 std::map<GURL, content::AppCacheInfoVector>& collection = |
100 callback.info_collection()->infos_by_origin; | 105 callback.info_collection()->infos_by_origin; |
101 | 106 |
102 ASSERT_EQ(1u, collection.size()); | 107 ASSERT_EQ(1u, collection.size()); |
103 EXPECT_TRUE(ContainsKey(collection, manifest.GetOrigin())); | 108 EXPECT_TRUE(ContainsKey(collection, manifest.GetOrigin())); |
104 ASSERT_EQ(1u, collection[manifest.GetOrigin()].size()); | 109 ASSERT_EQ(1u, collection[manifest.GetOrigin()].size()); |
105 EXPECT_EQ(manifest, collection[manifest.GetOrigin()].at(0).manifest_url); | 110 EXPECT_EQ(manifest, collection[manifest.GetOrigin()].at(0).manifest_url); |
106 } | 111 } |
107 | 112 |
108 TEST_F(CannedBrowsingDataAppCacheHelperTest, Empty) { | 113 TEST_F(CannedBrowsingDataAppCacheHelperTest, Empty) { |
109 TestingProfile profile; | |
110 | |
111 GURL manifest("http://example.com/manifest.xml"); | 114 GURL manifest("http://example.com/manifest.xml"); |
112 | 115 |
113 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( | 116 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( |
114 new CannedBrowsingDataAppCacheHelper(&profile)); | 117 new CannedBrowsingDataAppCacheHelper(&profile_)); |
115 | 118 |
116 ASSERT_TRUE(helper->empty()); | 119 ASSERT_TRUE(helper->empty()); |
117 helper->AddAppCache(manifest); | 120 helper->AddAppCache(manifest); |
118 ASSERT_FALSE(helper->empty()); | 121 ASSERT_FALSE(helper->empty()); |
119 helper->Reset(); | 122 helper->Reset(); |
120 ASSERT_TRUE(helper->empty()); | 123 ASSERT_TRUE(helper->empty()); |
121 } | 124 } |
122 | 125 |
123 TEST_F(CannedBrowsingDataAppCacheHelperTest, Delete) { | 126 TEST_F(CannedBrowsingDataAppCacheHelperTest, Delete) { |
124 TestingProfile profile; | |
125 | |
126 GURL manifest1("http://example.com/manifest1.xml"); | 127 GURL manifest1("http://example.com/manifest1.xml"); |
127 GURL manifest2("http://foo.example.com/manifest2.xml"); | 128 GURL manifest2("http://foo.example.com/manifest2.xml"); |
128 GURL manifest3("http://bar.example.com/manifest3.xml"); | 129 GURL manifest3("http://bar.example.com/manifest3.xml"); |
129 | 130 |
130 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( | 131 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( |
131 new CannedBrowsingDataAppCacheHelper(&profile)); | 132 new CannedBrowsingDataAppCacheHelper(&profile_)); |
132 | 133 |
133 EXPECT_TRUE(helper->empty()); | 134 EXPECT_TRUE(helper->empty()); |
134 helper->AddAppCache(manifest1); | 135 helper->AddAppCache(manifest1); |
135 helper->AddAppCache(manifest2); | 136 helper->AddAppCache(manifest2); |
136 helper->AddAppCache(manifest3); | 137 helper->AddAppCache(manifest3); |
137 EXPECT_FALSE(helper->empty()); | 138 EXPECT_FALSE(helper->empty()); |
138 EXPECT_EQ(3u, helper->GetAppCacheCount()); | 139 EXPECT_EQ(3u, helper->GetAppCacheCount()); |
139 helper->DeleteAppCacheGroup(manifest2); | 140 helper->DeleteAppCacheGroup(manifest2); |
140 EXPECT_EQ(2u, helper->GetAppCacheCount()); | 141 EXPECT_EQ(2u, helper->GetAppCacheCount()); |
141 EXPECT_FALSE(ContainsKey(helper->GetOriginAppCacheInfoMap(), manifest2)); | 142 EXPECT_FALSE(ContainsKey(helper->GetOriginAppCacheInfoMap(), manifest2)); |
142 } | 143 } |
143 | 144 |
144 TEST_F(CannedBrowsingDataAppCacheHelperTest, IgnoreExtensionsAndDevTools) { | 145 TEST_F(CannedBrowsingDataAppCacheHelperTest, IgnoreExtensionsAndDevTools) { |
145 TestingProfile profile; | |
146 | |
147 GURL manifest1("chrome-extension://abcdefghijklmnopqrstuvwxyz/manifest.xml"); | 146 GURL manifest1("chrome-extension://abcdefghijklmnopqrstuvwxyz/manifest.xml"); |
148 GURL manifest2("chrome-devtools://abcdefghijklmnopqrstuvwxyz/manifest.xml"); | 147 GURL manifest2("chrome-devtools://abcdefghijklmnopqrstuvwxyz/manifest.xml"); |
149 | 148 |
150 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( | 149 scoped_refptr<CannedBrowsingDataAppCacheHelper> helper( |
151 new CannedBrowsingDataAppCacheHelper(&profile)); | 150 new CannedBrowsingDataAppCacheHelper(&profile_)); |
152 | 151 |
153 ASSERT_TRUE(helper->empty()); | 152 ASSERT_TRUE(helper->empty()); |
154 helper->AddAppCache(manifest1); | 153 helper->AddAppCache(manifest1); |
155 ASSERT_TRUE(helper->empty()); | 154 ASSERT_TRUE(helper->empty()); |
156 helper->AddAppCache(manifest2); | 155 helper->AddAppCache(manifest2); |
157 ASSERT_TRUE(helper->empty()); | 156 ASSERT_TRUE(helper->empty()); |
158 } | 157 } |
OLD | NEW |