Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / appcache / appcache_storage_unittest.cc
blobc6b50bf043a9d72922235252513cfbd8d5df5640
1 // Copyright 2014 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 "base/message_loop/message_loop.h"
6 #include "content/browser/appcache/appcache.h"
7 #include "content/browser/appcache/appcache_group.h"
8 #include "content/browser/appcache/appcache_response.h"
9 #include "content/browser/appcache/appcache_storage.h"
10 #include "content/browser/appcache/mock_appcache_service.h"
11 #include "content/browser/quota/mock_quota_manager_proxy.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace content {
16 namespace {
17 const storage::StorageType kTemp = storage::kStorageTypeTemporary;
20 class AppCacheStorageTest : public testing::Test {
21 public:
22 class MockStorageDelegate : public AppCacheStorage::Delegate {
23 public:
27 TEST_F(AppCacheStorageTest, AddRemoveCache) {
28 MockAppCacheService service;
29 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111));
31 EXPECT_EQ(cache.get(),
32 service.storage()->working_set()->GetCache(111));
34 service.storage()->working_set()->RemoveCache(cache.get());
36 EXPECT_TRUE(!service.storage()->working_set()->GetCache(111));
38 // Removing non-existing cache from service should not fail.
39 MockAppCacheService dummy;
40 dummy.storage()->working_set()->RemoveCache(cache.get());
43 TEST_F(AppCacheStorageTest, AddRemoveGroup) {
44 MockAppCacheService service;
45 scoped_refptr<AppCacheGroup> group(
46 new AppCacheGroup(service.storage(), GURL(), 111));
48 EXPECT_EQ(group.get(), service.storage()->working_set()->GetGroup(GURL()));
50 service.storage()->working_set()->RemoveGroup(group.get());
52 EXPECT_TRUE(!service.storage()->working_set()->GetGroup(GURL()));
54 // Removing non-existing group from service should not fail.
55 MockAppCacheService dummy;
56 dummy.storage()->working_set()->RemoveGroup(group.get());
59 TEST_F(AppCacheStorageTest, AddRemoveResponseInfo) {
60 MockAppCacheService service;
61 scoped_refptr<AppCacheResponseInfo> info(
62 new AppCacheResponseInfo(service.storage(), GURL(),
63 111, new net::HttpResponseInfo,
64 kUnkownResponseDataSize));
66 EXPECT_EQ(info.get(),
67 service.storage()->working_set()->GetResponseInfo(111));
69 service.storage()->working_set()->RemoveResponseInfo(info.get());
71 EXPECT_TRUE(!service.storage()->working_set()->GetResponseInfo(111));
73 // Removing non-existing info from service should not fail.
74 MockAppCacheService dummy;
75 dummy.storage()->working_set()->RemoveResponseInfo(info.get());
78 TEST_F(AppCacheStorageTest, DelegateReferences) {
79 typedef scoped_refptr<AppCacheStorage::DelegateReference>
80 ScopedDelegateReference;
81 MockAppCacheService service;
82 MockStorageDelegate delegate;
83 ScopedDelegateReference delegate_reference1;
84 ScopedDelegateReference delegate_reference2;
86 EXPECT_FALSE(service.storage()->GetDelegateReference(&delegate));
88 delegate_reference1 =
89 service.storage()->GetOrCreateDelegateReference(&delegate);
90 EXPECT_TRUE(delegate_reference1.get());
91 EXPECT_TRUE(delegate_reference1->HasOneRef());
92 EXPECT_TRUE(service.storage()->GetDelegateReference(&delegate));
93 EXPECT_EQ(&delegate,
94 service.storage()->GetDelegateReference(&delegate)->delegate);
95 EXPECT_EQ(service.storage()->GetDelegateReference(&delegate),
96 service.storage()->GetOrCreateDelegateReference(&delegate));
97 delegate_reference1 = NULL;
98 EXPECT_FALSE(service.storage()->GetDelegateReference(&delegate));
100 delegate_reference1 =
101 service.storage()->GetOrCreateDelegateReference(&delegate);
102 service.storage()->CancelDelegateCallbacks(&delegate);
103 EXPECT_TRUE(delegate_reference1.get());
104 EXPECT_TRUE(delegate_reference1->HasOneRef());
105 EXPECT_FALSE(delegate_reference1->delegate);
106 EXPECT_FALSE(service.storage()->GetDelegateReference(&delegate));
108 delegate_reference2 =
109 service.storage()->GetOrCreateDelegateReference(&delegate);
110 EXPECT_TRUE(delegate_reference2.get());
111 EXPECT_TRUE(delegate_reference2->HasOneRef());
112 EXPECT_EQ(&delegate, delegate_reference2->delegate);
113 EXPECT_NE(delegate_reference1.get(), delegate_reference2.get());
116 TEST_F(AppCacheStorageTest, UsageMap) {
117 const GURL kOrigin("http://origin/");
118 const GURL kOrigin2("http://origin2/");
120 MockAppCacheService service;
121 scoped_refptr<MockQuotaManagerProxy> mock_proxy(
122 new MockQuotaManagerProxy(NULL, NULL));
123 service.set_quota_manager_proxy(mock_proxy.get());
125 service.storage()->UpdateUsageMapAndNotify(kOrigin, 0);
126 EXPECT_EQ(0, mock_proxy->notify_storage_modified_count());
128 service.storage()->UpdateUsageMapAndNotify(kOrigin, 10);
129 EXPECT_EQ(1, mock_proxy->notify_storage_modified_count());
130 EXPECT_EQ(10, mock_proxy->last_notified_delta());
131 EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin());
132 EXPECT_EQ(kTemp, mock_proxy->last_notified_type());
134 service.storage()->UpdateUsageMapAndNotify(kOrigin, 100);
135 EXPECT_EQ(2, mock_proxy->notify_storage_modified_count());
136 EXPECT_EQ(90, mock_proxy->last_notified_delta());
137 EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin());
138 EXPECT_EQ(kTemp, mock_proxy->last_notified_type());
140 service.storage()->UpdateUsageMapAndNotify(kOrigin, 0);
141 EXPECT_EQ(3, mock_proxy->notify_storage_modified_count());
142 EXPECT_EQ(-100, mock_proxy->last_notified_delta());
143 EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin());
144 EXPECT_EQ(kTemp, mock_proxy->last_notified_type());
146 service.storage()->NotifyStorageAccessed(kOrigin2);
147 EXPECT_EQ(0, mock_proxy->notify_storage_accessed_count());
149 service.storage()->usage_map_[kOrigin2] = 1;
150 service.storage()->NotifyStorageAccessed(kOrigin2);
151 EXPECT_EQ(1, mock_proxy->notify_storage_accessed_count());
152 EXPECT_EQ(kOrigin2, mock_proxy->last_notified_origin());
153 EXPECT_EQ(kTemp, mock_proxy->last_notified_type());
155 service.storage()->usage_map_.clear();
156 service.storage()->usage_map_[kOrigin] = 5000;
157 service.storage()->ClearUsageMapAndNotify();
158 EXPECT_EQ(4, mock_proxy->notify_storage_modified_count());
159 EXPECT_EQ(-5000, mock_proxy->last_notified_delta());
160 EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin());
161 EXPECT_EQ(kTemp, mock_proxy->last_notified_type());
162 EXPECT_TRUE(service.storage()->usage_map_.empty());
165 } // namespace content